44
44
import java .util .SortedMap ;
45
45
import java .util .TreeMap ;
46
46
import java .util .concurrent .ConcurrentLinkedQueue ;
47
+ import java .util .concurrent .Future ;
47
48
import java .util .concurrent .TimeUnit ;
48
49
import java .util .concurrent .atomic .AtomicReference ;
49
50
@@ -114,6 +115,9 @@ public final class MemcachedConnection extends SpyObject {
114
115
private final DelayedSwitchoverGroups delayedSwitchoverGroups =
115
116
new DelayedSwitchoverGroups (DELAYED_SWITCHOVER_TIMEOUT_MILLISECONDS );
116
117
/* ENABLE_REPLICATION end */
118
+ private final HashRingUpdateService hashUpdateService = new HashRingUpdateService ();
119
+
120
+ private Future <Boolean > hashUpdateResult ;
117
121
118
122
/**
119
123
* Construct a memcached connection.
@@ -312,7 +316,7 @@ public void handleIO() throws IOException {
312
316
}
313
317
}
314
318
315
- private void handleNodesToRemove (final List <MemcachedNode > nodesToRemove ) {
319
+ void handleNodesToRemove (final List <MemcachedNode > nodesToRemove ) {
316
320
for (MemcachedNode node : nodesToRemove ) {
317
321
getLogger ().info ("old memcached node removed %s" , node );
318
322
reconnectQueue .remove (node );
@@ -339,10 +343,9 @@ private void handleNodesToRemove(final List<MemcachedNode> nodesToRemove) {
339
343
}
340
344
}
341
345
342
- private void updateConnections (List <InetSocketAddress > addrs ) throws IOException {
343
- List <MemcachedNode > attachNodes = new ArrayList <MemcachedNode >();
344
- List <MemcachedNode > removeNodes = new ArrayList <MemcachedNode >();
345
-
346
+ private void getUpdateNodes (List <InetSocketAddress > addrs ,
347
+ List <MemcachedNode > attachNodes ,
348
+ List <MemcachedNode > removeNodes ) throws IOException {
346
349
for (MemcachedNode node : locator .getAll ()) {
347
350
if (addrs .contains (node .getSocketAddress ())) {
348
351
addrs .remove (node .getSocketAddress ());
@@ -355,12 +358,6 @@ private void updateConnections(List<InetSocketAddress> addrs) throws IOException
355
358
for (SocketAddress sa : addrs ) {
356
359
attachNodes .add (attachMemcachedNode (sa ));
357
360
}
358
-
359
- // Update the hash.
360
- locator .update (attachNodes , removeNodes );
361
-
362
- // Remove the unavailable nodes.
363
- handleNodesToRemove (removeNodes );
364
361
}
365
362
366
363
/* ENABLE_REPLICATION if */
@@ -703,7 +700,12 @@ void handleCacheNodesChange() throws IOException {
703
700
return ;
704
701
}
705
702
/* ENABLE_REPLICATION end */
706
- updateConnections (AddrUtil .getAddresses (cacheList ));
703
+ List <MemcachedNode > attachNodes = new ArrayList <MemcachedNode >();
704
+ List <MemcachedNode > removeNodes = new ArrayList <MemcachedNode >();
705
+ getUpdateNodes (AddrUtil .getAddresses (cacheList ), attachNodes , removeNodes );
706
+ // Update the hash.
707
+ CacheListUpdateTask task = new CacheListUpdateTask (this , attachNodes , removeNodes );
708
+ hashUpdateResult = hashUpdateService .updateHashes (task );
707
709
}
708
710
/* ENABLE_MIGRATION if */
709
711
if (arcusMigrEnabled && alterList != null ) {
@@ -724,6 +726,17 @@ void handleCacheNodesChange() throws IOException {
724
726
/* ENABLE_MIGRATION end */
725
727
}
726
728
729
+ // Called By MemcachedConnectionTest.
730
+ boolean getHashUpdateResult () {
731
+ try {
732
+ hashUpdateResult .get ();
733
+ } catch (Exception e ) {
734
+ getLogger ().warn ("Failed to update hash." , e );
735
+ return false ;
736
+ }
737
+ return true ;
738
+ }
739
+
727
740
// Called by CacheManger to add the memcached server group.
728
741
public void setCacheNodesChange (String addrs ) {
729
742
String old = cacheNodesChange .getAndSet (addrs );
@@ -1509,6 +1522,7 @@ public void shutdown() throws IOException {
1509
1522
}
1510
1523
}
1511
1524
selector .close ();
1525
+ hashUpdateService .shutdown ();
1512
1526
getLogger ().debug ("Shut down selector %s" , selector );
1513
1527
}
1514
1528
0 commit comments