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