@@ -44,6 +44,8 @@ public class AdaptiveBigQueryWriter extends BigQueryWriter {
44
44
45
45
// The maximum number of retries we will attempt to write rows after updating a BQ table schema.
46
46
private static final int AFTER_UPDATE_RETRY_LIMIT = 5 ;
47
+ // Wait for about 30s between each retry since both creating table and updating schema take up to 2 minutes to take effect.
48
+ private static final int RETRY_WAIT_TIME = 30000 ;
47
49
48
50
private final BigQuery bigQuery ;
49
51
private final SchemaManager schemaManager ;
@@ -105,7 +107,8 @@ && onlyContainsInvalidSchemaErrors(writeResponse.getInsertErrors()) && updateSch
105
107
attemptSchemaUpdate (tableId , topic );
106
108
}
107
109
} catch (BigQueryException exception ) {
108
- if (isTableNotExisted (exception ) && autoCreateTables ) {
110
+ // Should only perform one table creation attempt.
111
+ if (isTableNotExisted (exception ) && autoCreateTables && bigQuery .getTable (tableId .getBaseTableId ()) == null ) {
109
112
attemptTableCreate (tableId , topic );
110
113
} else if (isTableMissingSchema (exception ) && updateSchemas ) {
111
114
attemptSchemaUpdate (tableId , topic );
@@ -136,6 +139,11 @@ && onlyContainsInvalidSchemaErrors(writeResponse.getInsertErrors()) && updateSch
136
139
"Failed to write rows after BQ schema update within "
137
140
+ AFTER_UPDATE_RETRY_LIMIT + " attempts for: " + tableId .getBaseTableId ());
138
141
}
142
+ try {
143
+ Thread .sleep (RETRY_WAIT_TIME );
144
+ } catch (InterruptedException e ) {
145
+ // no-op, we want to keep retrying the insert
146
+ }
139
147
}
140
148
logger .debug ("table insertion completed successfully" );
141
149
return new HashMap <>();
0 commit comments