Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Concurrency problem in locator allNodes. #769

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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 Down Expand Up @@ -251,9 +251,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 +270,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 +281,7 @@ public void update(Collection<MemcachedNode> toAttach,
removeHash(group);
}
toDeleteGroups.clear();
allNodes = newAllNodes;
} finally {
/* ENABLE_MIGRATION if */
if (migrationInProgress && alterNodes.isEmpty()) {
Expand Down