Source:
private void initialize() { workerStateChangeListener.onWorkerStateChange(WorkerStateChangeListener.WorkerState.INITIALIZING); boolean isDone = false; Exception lastException = null; for (int i = 0; (!isDone) && (i < maxInitializationAttempts); i++) { try { log.info("Initialization attempt {}", (i + 1)); log.info("Initializing LeaseCoordinator"); leaseCoordinator.initialize(); TaskResult result = null; if (!skipShardSyncAtWorkerInitializationIfLeasesExist || leaseRefresher.isLeaseTableEmpty()) { log.info("Syncing Kinesis shard info"); ShardSyncTask shardSyncTask = new ShardSyncTask(shardDetector, leaseRefresher, initialPosition, cleanupLeasesUponShardCompletion, ignoreUnexpetedChildShards, 0L, hierarchichalShardSyncer, metricsFactory); result = new MetricsCollectingTaskDecorator(shardSyncTask, metricsFactory).call(); } else { log.info("Skipping shard sync per configuration setting (and lease table is not empty)"); } if (result == null || result.getException() == null) { if (!leaseCoordinator.isRunning()) { log.info("Starting LeaseCoordinator"); leaseCoordinator.start(); } else { log.info("LeaseCoordinator is already running. No need to start it."); } isDone = true; } else { lastException = result.getException(); } } catch (LeasingException e) { log.error("Caught exception when initializing LeaseCoordinator", e); lastException = e; } catch (Exception e) { lastException = e; } try { Thread.sleep(parentShardPollIntervalMillis); } catch (InterruptedException e) { log.debug("Sleep interrupted while initializing worker."); } } if (!isDone) { throw new RuntimeException(lastException); } workerStateChangeListener.onWorkerStateChange(WorkerStateChangeListener.WorkerState.STARTED); }

Target:
private void initialize() { synchronized (lock) { workerStateChangeListener.onWorkerStateChange(WorkerStateChangeListener.WorkerState.INITIALIZING); boolean isDone = false; Exception lastException = null; for (int i = 0; (!isDone) && (i < maxInitializationAttempts); i++) { try { log.info("Initialization attempt {}", (i + 1)); log.info("Initializing LeaseCoordinator"); leaseCoordinator.initialize(); TaskResult result = null; if (!skipShardSyncAtWorkerInitializationIfLeasesExist || leaseRefresher.isLeaseTableEmpty()) { log.info("Syncing Kinesis shard info"); ShardSyncTask shardSyncTask = new ShardSyncTask(shardDetector, leaseRefresher, initialPosition, cleanupLeasesUponShardCompletion, ignoreUnexpetedChildShards, 0L, hierarchicalShardSyncer, metricsFactory); result = new MetricsCollectingTaskDecorator(shardSyncTask, metricsFactory).call(); } else { log.info("Skipping shard sync per configuration setting (and lease table is not empty)"); } if (result == null || result.getException() == null) { if (!leaseCoordinator.isRunning()) { log.info("Starting LeaseCoordinator"); leaseCoordinator.start(); } else { log.info("LeaseCoordinator is already running. No need to start it."); } isDone = true; } else { lastException = result.getException(); } } catch (LeasingException e) { log.error("Caught exception when initializing LeaseCoordinator", e); lastException = e; } catch (Exception e) { lastException = e; } try { Thread.sleep(parentShardPollIntervalMillis); } catch (InterruptedException e) { log.debug("Sleep interrupted while initializing worker."); } } if (!isDone) { throw new RuntimeException(lastException); } workerStateChangeListener.onWorkerStateChange(WorkerStateChangeListener.WorkerState.STARTED); } }