@@ -26,6 +26,7 @@ public class SnowflakeCopyBatchInsert implements BatchInsert {
26
26
protected static final String nullString = "\\ N" ;
27
27
protected static final String newLineString = "\n " ;
28
28
protected static final String delimiterString = "\t " ;
29
+ private final int maxUploadRetries ;
29
30
30
31
private SnowflakeOutputConnection connection = null ;
31
32
private TableIdentifier tableIdentifier = null ;
@@ -39,7 +40,10 @@ public class SnowflakeCopyBatchInsert implements BatchInsert {
39
40
private List <Future <Void >> uploadAndCopyFutures ;
40
41
41
42
public SnowflakeCopyBatchInsert (
42
- JdbcOutputConnector connector , StageIdentifier stageIdentifier , boolean deleteStageFile )
43
+ JdbcOutputConnector connector ,
44
+ StageIdentifier stageIdentifier ,
45
+ boolean deleteStageFile ,
46
+ int maxUploadRetries )
43
47
throws IOException {
44
48
this .index = 0 ;
45
49
openNewFile ();
@@ -48,6 +52,7 @@ public SnowflakeCopyBatchInsert(
48
52
this .executorService = Executors .newCachedThreadPool ();
49
53
this .deleteStageFile = deleteStageFile ;
50
54
this .uploadAndCopyFutures = new ArrayList ();
55
+ this .maxUploadRetries = maxUploadRetries ;
51
56
}
52
57
53
58
@ Override
@@ -251,7 +256,7 @@ public void flush() throws IOException, SQLException {
251
256
String snowflakeStageFileName = "embulk_snowflake_" + SnowflakeUtils .randomString (8 );
252
257
253
258
UploadTask uploadTask =
254
- new UploadTask (file , batchRows , stageIdentifier , snowflakeStageFileName );
259
+ new UploadTask (file , batchRows , stageIdentifier , snowflakeStageFileName , maxUploadRetries );
255
260
Future <Void > uploadFuture = executorService .submit (uploadTask );
256
261
uploadAndCopyFutures .add (uploadFuture );
257
262
@@ -330,28 +335,49 @@ private class UploadTask implements Callable<Void> {
330
335
private final int batchRows ;
331
336
private final String snowflakeStageFileName ;
332
337
private final StageIdentifier stageIdentifier ;
338
+ private final int maxUploadRetries ;
333
339
334
340
public UploadTask (
335
- File file , int batchRows , StageIdentifier stageIdentifier , String snowflakeStageFileName ) {
341
+ File file ,
342
+ int batchRows ,
343
+ StageIdentifier stageIdentifier ,
344
+ String snowflakeStageFileName ,
345
+ int maxUploadRetries ) {
336
346
this .file = file ;
337
347
this .batchRows = batchRows ;
338
348
this .snowflakeStageFileName = snowflakeStageFileName ;
339
349
this .stageIdentifier = stageIdentifier ;
350
+ this .maxUploadRetries = maxUploadRetries ;
340
351
}
341
352
342
- public Void call () throws IOException , SQLException {
353
+ public Void call () throws IOException , SQLException , InterruptedException {
343
354
logger .info (
344
355
String .format (
345
356
"Uploading file id %s to Snowflake (%,d bytes %,d rows)" ,
346
357
snowflakeStageFileName , file .length (), batchRows ));
347
358
359
+ int retries = 0 ;
348
360
try {
349
361
long startTime = System .currentTimeMillis ();
350
362
// put file to snowflake internal storage
351
363
SnowflakeOutputConnection con = (SnowflakeOutputConnection ) connector .connect (true );
352
364
353
365
FileInputStream fileInputStream = new FileInputStream (file );
354
- con .runUploadFile (stageIdentifier , snowflakeStageFileName , fileInputStream );
366
+ do {
367
+ try {
368
+ con .runUploadFile (stageIdentifier , snowflakeStageFileName , fileInputStream );
369
+ } catch (SQLException e ) {
370
+ retries ++;
371
+ if (retries > this .maxUploadRetries ) {
372
+ throw e ;
373
+ }
374
+ logger .warn (
375
+ String .format (
376
+ "Upload error %s file %s retries: %d" , e , snowflakeStageFileName , retries ));
377
+ Thread .sleep (retries * retries * 1000 );
378
+ }
379
+ break ;
380
+ } while (retries < this .maxUploadRetries );
355
381
356
382
double seconds = (System .currentTimeMillis () - startTime ) / 1000.0 ;
357
383
0 commit comments