Skip to content

Commit a354041

Browse files
Backport to branch(3) : Improve ExecutorService lifecycle handling by moving shutdown to finally block (#3198)
Co-authored-by: inv-jishnu <[email protected]>
1 parent db043aa commit a354041

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

data-loader/core/src/main/java/com/scalar/db/dataloader/core/dataexport/ExportManager.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ public ExportReport startExport(
8080
ExecutorService executorService =
8181
Executors.newFixedThreadPool(exportOptions.getMaxThreadCount());
8282

83-
BufferedWriter bufferedWriter = new BufferedWriter(writer);
8483
boolean isJson = exportOptions.getOutputFileFormat() == FileFormat.JSON;
8584

8685
try (TransactionManagerCrudOperable.Scanner scanner =
87-
createScanner(exportOptions, dao, manager)) {
86+
createScanner(exportOptions, dao, manager);
87+
BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
8888

8989
Iterator<Result> iterator = scanner.iterator();
9090
AtomicBoolean isFirstBatch = new AtomicBoolean(true);
@@ -106,18 +106,20 @@ public ExportReport startExport(
106106
executorService.shutdown();
107107
if (executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) {
108108
logger.info("All tasks completed");
109+
processFooter(exportOptions, tableMetadata, bufferedWriter);
109110
} else {
110111
logger.error("Timeout occurred while waiting for tasks to complete");
111112
// TODO: handle this
112113
}
113-
processFooter(exportOptions, tableMetadata, bufferedWriter);
114114
} catch (InterruptedException
115115
| IOException
116116
| UnknownTransactionStatusException
117117
| CrudException e) {
118118
logger.error("Error during export: ", e);
119119
} finally {
120-
bufferedWriter.flush();
120+
if (!executorService.isShutdown()) {
121+
executorService.shutdownNow();
122+
}
121123
}
122124
} catch (ExportOptionsValidationException | IOException | ScalarDbDaoException e) {
123125
logger.error("Error during export: {}", e.getMessage());

0 commit comments

Comments
 (0)