From dfee2b9e7fb6d3fcaf48871058b1df43fd09f686 Mon Sep 17 00:00:00 2001 From: Shinichi Umegane Date: Wed, 29 May 2024 20:14:43 +0900 Subject: [PATCH] Fix: Handle termination for online-only mode without a timer --- .../phonebill/app/billing/PhoneBill.java | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/src/main/java/com/tsurugidb/benchmark/phonebill/app/billing/PhoneBill.java b/src/src/main/java/com/tsurugidb/benchmark/phonebill/app/billing/PhoneBill.java index 94945ab..ad8df20 100644 --- a/src/src/main/java/com/tsurugidb/benchmark/phonebill/app/billing/PhoneBill.java +++ b/src/src/main/java/com/tsurugidb/benchmark/phonebill/app/billing/PhoneBill.java @@ -62,7 +62,6 @@ public class PhoneBill extends ExecutableCommand { private AtomicInteger tryCounter = new AtomicInteger(0); private AtomicInteger abortCounter = new AtomicInteger(0); - Config config; // UTからConfigを書き換え可能にするためにパッケージプライベートにしている public static void main(String[] args) throws Exception { @@ -91,6 +90,9 @@ public void execute(Config config) throws Exception { PhoneBillDbManager.initCounter(); // オンラインアプリを実行する list.stream().forEach(task -> service.submit(task)); + if (service != null) { + service.shutdown(); + } // 指定の実行時間になったら停止するためのタイマーをセット CountDownLatch latch = new CountDownLatch(1); @@ -106,11 +108,20 @@ public void run() { timer.schedule(timerTask, config.execTimeLimitSecs * 1000L); } - // バッチの実行 if (config.onlineOnly) { - // online onlyが指定された場合は、タイマーが呼び出されるまで待つ - latch.await(); + // online onlyが指定された場合 + if (config.execTimeLimitSecs > 0) { + // タイマーが呼び出されるまで待つ + latch.await(); + } else { + // オンラインアプリのスレッドが生きている限り無限に待ち続ける + while (service != null && !service.awaitTermination(5, TimeUnit.MINUTES)) { + // do nothing + } + LOG.warn("All online applications have been terminated. Exiting."); + } } else { + // バッチの実行 Duration d = toDuration(config.targetMonth); doCalc(d.getStatDate(), d.getEndDate()); } @@ -119,7 +130,6 @@ public void run() { // オンラインアプリを終了する list.stream().forEach(task -> task.terminate()); if (service != null) { - service.shutdown(); service.awaitTermination(5, TimeUnit.MINUTES); } // 終了していないオンラインアプリがある場合は異常終了する。 @@ -131,15 +141,13 @@ public void run() { } } - - /** * Configに従ってオンラインアプリのインスタンスを生成する * * @return オンラインアプリのインスタンスのリスト * @throws IOException */ - @SuppressFBWarnings(value={"DMI_RANDOM_USED_ONLY_ONCE"}) + @SuppressFBWarnings(value = { "DMI_RANDOM_USED_ONLY_ONCE" }) public static List createOnlineApps(Config config, ContractBlockInfoAccessor accessor) throws IOException { if (!config.hasOnlineApp()) { @@ -161,7 +169,6 @@ public static List createOnlineApps(Config config, ContractBl config.onlineAppRandomCoverRate); } - List list = new ArrayList(); if (config.historyInsertThreadCount > 0 && config.historyInsertTransactionPerMin != 0) { list.addAll(HistoryInsertApp.createHistoryInsertApps(config, new Random(random.nextInt()), accessor, @@ -191,7 +198,6 @@ public static List createOnlineApps(Config config, ContractBl return list; } - /** * 指定の日付の一日から月の最終日までのDurationを作成する * @@ -205,7 +211,6 @@ public static Duration toDuration(Date date) { return new Duration(start, end); } - /** * 料金計算のメイン処理 * @@ -264,7 +269,8 @@ void doCalc(Date start, Date end) throws Exception { SessionHoldingType.INSTANCE_FIELD); managers.add(managerForTask); } - CalculationTask task = new CalculationTask(queue, managerForTask, config, batchExecId, abortRequested, tryCounter, abortCounter); + CalculationTask task = new CalculationTask(queue, managerForTask, config, batchExecId, abortRequested, + tryCounter, abortCounter); futures.add(service.submit(task)); } } catch (RuntimeException e) { @@ -284,11 +290,9 @@ void doCalc(Date start, Date end) throws Exception { } public String getStatus() { - return queue == null ? "Initializing": queue.getStatus(); + return queue == null ? "Initializing" : queue.getStatus(); } - - /** * @param conn * @param futures @@ -330,7 +334,7 @@ private void cleanup(Set> futures, List ma } } } - managers.stream().forEach(m->m.close()); + managers.stream().forEach(m -> m.close()); if (cause != null) { LOG.error("Phone bill batch aborting by exception."); throw cause;