Source:
<code>void initialize() { synchronized (lock) { registerErrorHandlerForUndeliverableAsyncTaskExceptions(); 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()) { <START> for (int j = 0; j < 10 && leaseRefresher.isLeaseTableEmpty(); j++) { long waitTime = ThreadLocalRandom.current().nextLong(1000L, 5000L); Thread.sleep(waitTime); } <END> log.info("Syncing Kinesis shard info"); result = leaderElectedPeriodicShardSyncManager.syncShardsOnce(); } 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."); } log.info("Scheduling periodicShardSync)"); isDone = true; } else { lastException = result.getException(); } } catch (LeasingException e) { log.error("Caught exception when initializing LeaseCoordinator", e); lastException = e; } catch (Exception e) { lastException = e; } if (!isDone || !leaderElectedPeriodicShardSyncManager.hashRangeCovered()) { try { Thread.sleep(schedulerInitializationBackoffTimeMillis); leaderElectedPeriodicShardSyncManager.stop(); } catch (InterruptedException e) { log.debug("Sleep interrupted while initializing worker."); } } } if (!isDone) { throw new RuntimeException(lastException); } workerStateChangeListener.onWorkerStateChange(WorkerStateChangeListener.WorkerState.STARTED); } }</code><technical_language>need unit test cases this logic</technical_language>

Target:
void initialize() { synchronized (lock) { registerErrorHandlerForUndeliverableAsyncTaskExceptions(); 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()) { waitUntilLeaseTableIsReady(); log.info("Syncing Kinesis shard info"); result = leaderElectedPeriodicShardSyncManager.syncShardsOnce(); } 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."); } log.info("Scheduling periodicShardSync)"); waitUntilHashRangeCovered(); isDone = true; } else { lastException = result.getException(); } } catch (LeasingException e) { log.error("Caught exception when initializing LeaseCoordinator", e); lastException = e; } catch (Exception e) { lastException = e; } if (!isDone) { try { Thread.sleep(schedulerInitializationBackoffTimeMillis); leaderElectedPeriodicShardSyncManager.stop(); } catch (InterruptedException e) { log.debug("Sleep interrupted while initializing worker."); } } } if (!isDone) { throw new RuntimeException(lastException); } workerStateChangeListener.onWorkerStateChange(WorkerStateChangeListener.WorkerState.STARTED); } }