@@ -53,15 +53,24 @@ public final class ChannelFinder {
5353 private final KeyRecipeCache recipeCache = new KeyRecipeCache ();
5454 private final KeyRangeCache rangeCache ;
5555 @ Nullable private final EndpointLifecycleManager lifecycleManager ;
56+ @ Nullable private final String finderKey ;
5657
5758 public ChannelFinder (ChannelEndpointCache endpointCache ) {
58- this (endpointCache , null );
59+ this (endpointCache , null , null );
5960 }
6061
6162 public ChannelFinder (
6263 ChannelEndpointCache endpointCache , @ Nullable EndpointLifecycleManager lifecycleManager ) {
64+ this (endpointCache , lifecycleManager , null );
65+ }
66+
67+ ChannelFinder (
68+ ChannelEndpointCache endpointCache ,
69+ @ Nullable EndpointLifecycleManager lifecycleManager ,
70+ @ Nullable String finderKey ) {
6371 this .rangeCache = new KeyRangeCache (Objects .requireNonNull (endpointCache ), lifecycleManager );
6472 this .lifecycleManager = lifecycleManager ;
73+ this .finderKey = finderKey ;
6574 }
6675
6776 void useDeterministicRandom () {
@@ -84,22 +93,21 @@ public void update(CacheUpdate update) {
8493 rangeCache .addRanges (update );
8594
8695 // Notify the lifecycle manager about server addresses so it can create endpoints
87- // in the background and start probing.
88- if (lifecycleManager != null ) {
96+ // in the background and start probing, and evict stale endpoints atomically .
97+ if (lifecycleManager != null && finderKey != null ) {
8998 Set <String > currentAddresses = new HashSet <>();
9099 for (Group group : update .getGroupList ()) {
91100 for (Tablet tablet : group .getTabletsList ()) {
92101 String addr = tablet .getServerAddress ();
93102 if (!addr .isEmpty ()) {
94103 currentAddresses .add (addr );
95- lifecycleManager .ensureEndpointExists (addr );
96104 }
97105 }
98106 }
99107 // Also include addresses from existing cached tablets not in this update.
100108 currentAddresses .addAll (rangeCache .getActiveAddresses ());
101- // Evict endpoints no longer referenced by any tablet across all finders .
102- lifecycleManager .updateActiveAddresses (this , currentAddresses );
109+ // Atomically ensure endpoints exist and evict stale ones .
110+ lifecycleManager .updateActiveAddresses (finderKey , currentAddresses );
103111 }
104112 }
105113 }
0 commit comments