Skip to content

Commit

Permalink
FIX: Concurrency problem in locator allNodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
brido4125 committed Jul 3, 2024
1 parent 715f73a commit dfcd905
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/main/java/net/spy/memcached/ArcusKetamaNodeLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class ArcusKetamaNodeLocator extends SpyObject implements NodeLocator {

private final TreeMap<Long, SortedSet<MemcachedNode>> ketamaNodes;
private final Collection<MemcachedNode> allNodes;
private Collection<MemcachedNode> allNodes;

/* ENABLE_MIGRATION if */
private TreeMap<Long, SortedSet<MemcachedNode>> ketamaAlterNodes;
Expand Down Expand Up @@ -206,15 +206,16 @@ public void update(Collection<MemcachedNode> toAttach,
Collection<MemcachedNode> toDelete) {
lock.lock();
try {
ArrayList<MemcachedNode> newAllNodes = new ArrayList<>(allNodes);
// Add memcached nodes.
for (MemcachedNode node : toAttach) {
allNodes.add(node);
newAllNodes.add(node);
insertHash(node);
}

// Remove memcached nodes.
for (MemcachedNode node : toDelete) {
allNodes.remove(node);
newAllNodes.remove(node);
removeHash(node);
try {
node.closeChannel();
Expand All @@ -223,6 +224,7 @@ public void update(Collection<MemcachedNode> toAttach,
"Failed to closeChannel the node : " + node);
}
}
allNodes = newAllNodes;
} finally {
/* ENABLE_MIGRATION if */
if (migrationInProgress && alterNodes.isEmpty()) {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/net/spy/memcached/ArcusReplKetamaNodeLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

Expand All @@ -42,7 +43,7 @@ public class ArcusReplKetamaNodeLocator extends SpyObject implements NodeLocator

private final TreeMap<Long, SortedSet<MemcachedReplicaGroup>> ketamaGroups;
private final HashMap<String, MemcachedReplicaGroup> allGroups;
private final Collection<MemcachedNode> allNodes;
private Collection<MemcachedNode> allNodes;

/* ENABLE_MIGRATION if */
private TreeMap<Long, SortedSet<MemcachedReplicaGroup>> ketamaAlterGroups;
Expand All @@ -65,7 +66,7 @@ public class ArcusReplKetamaNodeLocator extends SpyObject implements NodeLocator

public ArcusReplKetamaNodeLocator(List<MemcachedNode> nodes) {
super();
allNodes = nodes;
allNodes = new CopyOnWriteArrayList<>(nodes);
ketamaGroups = new TreeMap<>();
allGroups = new HashMap<>();

Expand Down Expand Up @@ -251,9 +252,10 @@ public void update(Collection<MemcachedNode> toAttach,
*/
lock.lock();
try {
ArrayList<MemcachedNode> newAllNodes = new ArrayList<>(allNodes);
// Remove memcached nodes.
for (MemcachedNode node : toDelete) {
allNodes.remove(node);
newAllNodes.remove(node);
removeNodeFromGroup(node);
try {
node.closeChannel();
Expand All @@ -269,7 +271,7 @@ public void update(Collection<MemcachedNode> toAttach,

// Add memcached nodes.
for (MemcachedNode node : toAttach) {
allNodes.add(node);
newAllNodes.add(node);
insertNodeIntoGroup(node);
}

Expand All @@ -280,6 +282,7 @@ public void update(Collection<MemcachedNode> toAttach,
removeHash(group);
}
toDeleteGroups.clear();
allNodes = newAllNodes;
} finally {
/* ENABLE_MIGRATION if */
if (migrationInProgress && alterNodes.isEmpty()) {
Expand Down

0 comments on commit dfcd905

Please sign in to comment.