-
Notifications
You must be signed in to change notification settings - Fork 113
[controller] Add flag to block first leader transition until init routines complete #2347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,14 +28,25 @@ public class ClusterLeaderInitializationManager implements ClusterLeaderInitiali | |||||||
| new VeniceConcurrentHashMap<>(); | ||||||||
| private final List<ClusterLeaderInitializationRoutine> initRoutines; | ||||||||
| private final boolean concurrentInit; | ||||||||
| private final boolean blockUntilComplete; | ||||||||
| private final LogContext logContext; | ||||||||
| private volatile boolean hasExecutedOnce = false; | ||||||||
|
|
||||||||
| public ClusterLeaderInitializationManager( | ||||||||
| List<ClusterLeaderInitializationRoutine> initRoutines, | ||||||||
| boolean concurrentInit, | ||||||||
| LogContext logContext) { | ||||||||
| this(initRoutines, concurrentInit, false, logContext); | ||||||||
| } | ||||||||
|
|
||||||||
| public ClusterLeaderInitializationManager( | ||||||||
| List<ClusterLeaderInitializationRoutine> initRoutines, | ||||||||
| boolean concurrentInit, | ||||||||
| boolean blockUntilComplete, | ||||||||
| LogContext logContext) { | ||||||||
| this.initRoutines = initRoutines; | ||||||||
| this.concurrentInit = concurrentInit; | ||||||||
| this.blockUntilComplete = blockUntilComplete; | ||||||||
| this.logContext = logContext; | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -47,31 +58,90 @@ public void execute(String clusterToInit) { | |||||||
| Map<ClusterLeaderInitializationRoutine, Object> initializedRoutinesForCluster = | ||||||||
| initializedClusters.computeIfAbsent(clusterToInit, k -> new VeniceConcurrentHashMap<>()); | ||||||||
|
|
||||||||
| // Check if this is the first time execute() has been called on this manager instance | ||||||||
| boolean isFirstExecution = !hasExecutedOnce; | ||||||||
|
|
||||||||
| LOGGER.info( | ||||||||
| "Starting initialization routines for cluster: {}. Mode: {}, BlockUntilComplete: {}, FirstExecution: {}, RoutineCount: {}", | ||||||||
| clusterToInit, | ||||||||
| concurrentInit ? "CONCURRENT" : "SEQUENTIAL", | ||||||||
| blockUntilComplete, | ||||||||
| isFirstExecution, | ||||||||
| initRoutines.size()); | ||||||||
|
|
||||||||
| long startTime = System.currentTimeMillis(); | ||||||||
| CompletableFuture<Void> allRoutinesFuture; | ||||||||
| if (concurrentInit) { | ||||||||
| initRoutines.forEach( | ||||||||
| routine -> CompletableFuture | ||||||||
| .runAsync(() -> initRoutine(clusterToInit, initializedRoutinesForCluster, routine))); | ||||||||
| CompletableFuture<?>[] futures = initRoutines.stream() | ||||||||
| .map( | ||||||||
| routine -> CompletableFuture | ||||||||
| .runAsync(() -> initRoutine(clusterToInit, initializedRoutinesForCluster, routine))) | ||||||||
| .toArray(CompletableFuture[]::new); | ||||||||
| allRoutinesFuture = CompletableFuture.allOf(futures); | ||||||||
| } else { | ||||||||
| CompletableFuture.runAsync( | ||||||||
| allRoutinesFuture = CompletableFuture.runAsync( | ||||||||
| () -> initRoutines.forEach(routine -> initRoutine(clusterToInit, initializedRoutinesForCluster, routine))); | ||||||||
| } | ||||||||
|
|
||||||||
| // Block on the first execution if the flag is enabled | ||||||||
| if (blockUntilComplete && isFirstExecution) { | ||||||||
| try { | ||||||||
| LOGGER.info( | ||||||||
| "Blocking until all initialization routines complete for cluster: {} (first time execute called)", | ||||||||
| clusterToInit); | ||||||||
| allRoutinesFuture.join(); | ||||||||
| long elapsedMs = System.currentTimeMillis() - startTime; | ||||||||
| LOGGER.info( | ||||||||
| "All initialization routines completed for cluster: {} (first time execute called). Elapsed time: {} ms", | ||||||||
| clusterToInit, | ||||||||
| elapsedMs); | ||||||||
| } catch (Exception e) { | ||||||||
| long elapsedMs = System.currentTimeMillis() - startTime; | ||||||||
| LOGGER.error( | ||||||||
| "Error while waiting for initialization routines to complete for cluster: {}. Elapsed time: {} ms", | ||||||||
| clusterToInit, | ||||||||
| elapsedMs, | ||||||||
| e); | ||||||||
| } finally { | ||||||||
| hasExecutedOnce = true; | ||||||||
| } | ||||||||
| } else { | ||||||||
| if (!hasExecutedOnce) { | ||||||||
| hasExecutedOnce = true; | ||||||||
| } | ||||||||
|
Comment on lines
+109
to
+111
|
||||||||
| if (!hasExecutedOnce) { | |
| hasExecutedOnce = true; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hasExecutedOnce flag is read without synchronization at line 62, then potentially updated at lines 106 and 110. This creates a race condition where multiple threads calling execute() concurrently could both read false for isFirstExecution, causing both to block. This violates the stated intent that only the first execution should block. Consider using AtomicBoolean.compareAndSet() to atomically check and set the flag, ensuring only one thread can observe isFirstExecution as true.