@@ -104,80 +104,79 @@ public void initializeAndWriteRecords(TableName tableName, List<ConvertedRecord>
104
104
StorageWriteApiRetryHandler retryHandler = new StorageWriteApiRetryHandler (tableName , getSinkRecords (rows ), retry , retryWait , time );
105
105
logger .debug ("Sending {} records to write Api Application stream {}" , rows .size (), streamName );
106
106
RecordBatches <ConvertedRecord > batches = new RecordBatches <>(rows );
107
- try (StreamWriter writer = streamWriter (tableName , streamName , rows )) {
108
- do {
109
- try {
110
- List <ConvertedRecord > batch = batches .currentBatch ();
111
- JSONArray jsonRecords = getJsonRecords (batch );
112
- logger .trace ("Sending records to Storage API writer for batch load" );
113
- ApiFuture <AppendRowsResponse > response = writer .appendRows (jsonRecords );
114
- AppendRowsResponse writeResult = response .get ();
115
- logger .trace ("Received response from Storage API writer batch" );
116
-
117
- if (writeResult .hasUpdatedSchema ()) {
118
- logger .warn ("Sent records schema does not match with table schema, will attempt to update schema" );
119
- if (!canAttemptSchemaUpdate ()) {
120
- throw new BigQueryStorageWriteApiConnectException ("Connector is not configured to perform schema updates." );
121
- }
122
- retryHandler .attemptTableOperation (schemaManager ::updateSchema );
123
- } else if (writeResult .hasError ()) {
124
- Status errorStatus = writeResult .getError ();
125
- String errorMessage = String .format ("Failed to write rows on table %s due to %s" , tableName , writeResult .getError ().getMessage ());
126
- retryHandler .setMostRecentException (new BigQueryStorageWriteApiConnectException (errorMessage ));
127
- if (BigQueryStorageWriteApiErrorResponses .isMalformedRequest (errorMessage )) {
128
- rows = maybeHandleDlqRoutingAndFilterRecords (rows , convertToMap (writeResult .getRowErrorsList ()), tableName .getTable ());
129
- if (rows .isEmpty ()) {
130
- writer .onSuccess ();
131
- return ;
132
- }
133
- } else if (!BigQueryStorageWriteApiErrorResponses .isRetriableError (errorStatus .getMessage ())) {
134
- failTask (retryHandler .getMostRecentException ());
135
- }
136
- logger .warn (errorMessage + " Retry attempt " + retryHandler .getAttempt ());
137
- } else {
138
- if (!writeResult .hasAppendResult ()) {
139
- logger .warn (
140
- "Write result did not report any errors, but also did not succeed. "
141
- + "This may be indicative of a bug in the BigQuery Java client library or back end; "
142
- + "please report it to the maintainers of the connector to investigate."
143
- );
144
- }
145
- logger .trace ("Append call completed successfully on stream {}" , streamName );
146
- writer .onSuccess ();
147
- return ;
107
+ StreamWriter writer = streamWriter (tableName , streamName , rows );
108
+ do {
109
+ try {
110
+ List <ConvertedRecord > batch = batches .currentBatch ();
111
+ JSONArray jsonRecords = getJsonRecords (batch );
112
+ logger .trace ("Sending records to Storage API writer for batch load" );
113
+ ApiFuture <AppendRowsResponse > response = writer .appendRows (jsonRecords );
114
+ AppendRowsResponse writeResult = response .get ();
115
+ logger .trace ("Received response from Storage API writer batch" );
116
+
117
+ if (writeResult .hasUpdatedSchema ()) {
118
+ logger .warn ("Sent records schema does not match with table schema, will attempt to update schema" );
119
+ if (!canAttemptSchemaUpdate ()) {
120
+ throw new BigQueryStorageWriteApiConnectException ("Connector is not configured to perform schema updates." );
148
121
}
149
- } catch (BigQueryStorageWriteApiConnectException exception ) {
150
- throw exception ;
151
- } catch (Exception e ) {
152
- String message = e .getMessage ();
153
- String errorMessage = String .format ("Failed to write rows on table %s due to %s" , tableName , message );
154
- retryHandler .setMostRecentException (new BigQueryStorageWriteApiConnectException (errorMessage , e ));
155
-
156
- if (shouldHandleSchemaMismatch (e )) {
157
- logger .warn ("Sent records schema does not match with table schema, will attempt to update schema" );
158
- retryHandler .attemptTableOperation (schemaManager ::updateSchema );
159
- } else if (BigQueryStorageWriteApiErrorResponses .isMalformedRequest (message )) {
160
- rows = maybeHandleDlqRoutingAndFilterRecords (rows , getRowErrorMapping (e ), tableName .getTable ());
122
+ retryHandler .attemptTableOperation (schemaManager ::updateSchema );
123
+ } else if (writeResult .hasError ()) {
124
+ Status errorStatus = writeResult .getError ();
125
+ String errorMessage = String .format ("Failed to write rows on table %s due to %s" , tableName , writeResult .getError ().getMessage ());
126
+ retryHandler .setMostRecentException (new BigQueryStorageWriteApiConnectException (errorMessage ));
127
+ if (BigQueryStorageWriteApiErrorResponses .isMalformedRequest (errorMessage )) {
128
+ rows = maybeHandleDlqRoutingAndFilterRecords (rows , convertToMap (writeResult .getRowErrorsList ()), tableName .getTable ());
161
129
if (rows .isEmpty ()) {
162
130
writer .onSuccess ();
163
131
return ;
164
132
}
165
- } else if (BigQueryStorageWriteApiErrorResponses .isStreamClosed (message )) {
166
- writer .refresh ();
167
- } else if (BigQueryStorageWriteApiErrorResponses .isTableMissing (message ) && getAutoCreateTables ()) {
168
- retryHandler .attemptTableOperation (schemaManager ::createTable );
169
- } else if (!BigQueryStorageWriteApiErrorResponses .isRetriableError (e .getMessage ())
170
- && BigQueryStorageWriteApiErrorResponses .isNonRetriableStorageError (e )
171
- ) {
133
+ } else if (!BigQueryStorageWriteApiErrorResponses .isRetriableError (errorStatus .getMessage ())) {
172
134
failTask (retryHandler .getMostRecentException ());
173
135
}
174
136
logger .warn (errorMessage + " Retry attempt " + retryHandler .getAttempt ());
137
+ } else {
138
+ if (!writeResult .hasAppendResult ()) {
139
+ logger .warn (
140
+ "Write result did not report any errors, but also did not succeed. "
141
+ + "This may be indicative of a bug in the BigQuery Java client library or back end; "
142
+ + "please report it to the maintainers of the connector to investigate."
143
+ );
144
+ }
145
+ logger .trace ("Append call completed successfully on stream {}" , streamName );
146
+ writer .onSuccess ();
147
+ return ;
175
148
}
176
- } while (retryHandler .maybeRetry ());
177
- throw new BigQueryStorageWriteApiConnectException (
178
- String .format ("Exceeded %s attempts to write to table %s " , retryHandler .getAttempt (), tableName ),
179
- retryHandler .getMostRecentException ());
180
- }
149
+ } catch (BigQueryStorageWriteApiConnectException exception ) {
150
+ throw exception ;
151
+ } catch (Exception e ) {
152
+ String message = e .getMessage ();
153
+ String errorMessage = String .format ("Failed to write rows on table %s due to %s" , tableName , message );
154
+ retryHandler .setMostRecentException (new BigQueryStorageWriteApiConnectException (errorMessage , e ));
155
+
156
+ if (shouldHandleSchemaMismatch (e )) {
157
+ logger .warn ("Sent records schema does not match with table schema, will attempt to update schema" );
158
+ retryHandler .attemptTableOperation (schemaManager ::updateSchema );
159
+ } else if (BigQueryStorageWriteApiErrorResponses .isMalformedRequest (message )) {
160
+ rows = maybeHandleDlqRoutingAndFilterRecords (rows , getRowErrorMapping (e ), tableName .getTable ());
161
+ if (rows .isEmpty ()) {
162
+ writer .onSuccess ();
163
+ return ;
164
+ }
165
+ } else if (BigQueryStorageWriteApiErrorResponses .isStreamClosed (message )) {
166
+ writer .refresh ();
167
+ } else if (BigQueryStorageWriteApiErrorResponses .isTableMissing (message ) && getAutoCreateTables ()) {
168
+ retryHandler .attemptTableOperation (schemaManager ::createTable );
169
+ } else if (!BigQueryStorageWriteApiErrorResponses .isRetriableError (e .getMessage ())
170
+ && BigQueryStorageWriteApiErrorResponses .isNonRetriableStorageError (e )
171
+ ) {
172
+ failTask (retryHandler .getMostRecentException ());
173
+ }
174
+ logger .warn (errorMessage + " Retry attempt " + retryHandler .getAttempt ());
175
+ }
176
+ } while (retryHandler .maybeRetry ());
177
+ throw new BigQueryStorageWriteApiConnectException (
178
+ String .format ("Exceeded %s attempts to write to table %s " , retryHandler .getAttempt (), tableName ),
179
+ retryHandler .getMostRecentException ());
181
180
}
182
181
183
182
/**
0 commit comments