@@ -355,6 +355,7 @@ public LocalDistributor add(Node node) {
355355 // An exception occurs if Node heartbeat has started but the server is not ready.
356356 // Unhandled exception blocks the event-bus thread from processing any event henceforth.
357357 NodeStatus initialNodeStatus ;
358+ Runnable healthCheck ;
358359 try {
359360 initialNodeStatus = node .getStatus ();
360361 if (initialNodeStatus .getAvailability () != UP ) {
@@ -363,8 +364,17 @@ public LocalDistributor add(Node node) {
363364 // We do not need to add this Node for now.
364365 return this ;
365366 }
366- model .add (initialNodeStatus );
367- nodes .put (node .getId (), node );
367+ // Extract the health check
368+ healthCheck = asRunnableHealthCheck (node );
369+ Lock writeLock = lock .writeLock ();
370+ writeLock .lock ();
371+ try {
372+ nodes .put (node .getId (), node );
373+ model .add (initialNodeStatus );
374+ allChecks .put (node .getId (), healthCheck );
375+ } finally {
376+ writeLock .unlock ();
377+ }
368378 } catch (Exception e ) {
369379 LOG .log (
370380 Debug .getDebugLogLevel (),
@@ -373,10 +383,6 @@ public LocalDistributor add(Node node) {
373383 return this ;
374384 }
375385
376- // Extract the health check
377- Runnable healthCheck = asRunnableHealthCheck (node );
378- allChecks .put (node .getId (), healthCheck );
379-
380386 updateNodeStatus (initialNodeStatus , healthCheck );
381387
382388 LOG .info (
@@ -415,7 +421,15 @@ private void updateNodeStatus(NodeStatus status, Runnable healthCheck) {
415421
416422 private Runnable runNodeHealthChecks () {
417423 return () -> {
418- ImmutableMap <NodeId , Runnable > nodeHealthChecks = ImmutableMap .copyOf (allChecks );
424+ ImmutableMap <NodeId , Runnable > nodeHealthChecks ;
425+ Lock readLock = this .lock .readLock ();
426+ readLock .lock ();
427+ try {
428+ nodeHealthChecks = ImmutableMap .copyOf (allChecks );
429+ } finally {
430+ readLock .unlock ();
431+ }
432+
419433 for (Runnable nodeHealthCheck : nodeHealthChecks .values ()) {
420434 GuardedRunnable .guard (nodeHealthCheck ).run ();
421435 }
0 commit comments