Skip to content

Commit

Permalink
INTERNAL: Refactoring updateReplConnections.
Browse files Browse the repository at this point in the history
  • Loading branch information
brido4125 committed Feb 4, 2025
1 parent b3c0fbd commit d6ad65c
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src/main/java/net/spy/memcached/MemcachedConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,41 +405,36 @@ private void updateReplConnections(List<InetSocketAddress> addrs) throws IOExcep
List<MemcachedReplicaGroup> changeRoleGroups = new ArrayList<>();
List<Task> taskList = new ArrayList<>(); // tasks executed after locator update

/* In replication, after SWITCHOVER or REPL_SLAVE is received from a group
* and switchover is performed, but before the group's znode is changed,
* another group's znode can be changed.
*
* In this case, there is a problem that the switchover is restored
* because the state of the switchover group and the znode state are different.
*
* In order to remove the abnormal phenomenon,
* we find out the changed groups with the comparison of previous and current znode list,
* and update the state of groups based on them.
*/
Set<String> changedGroups = findChangedGroups(addrs, locator.getAll());

// Create new group list from the provided addresses
Map<String, List<ArcusReplNodeAddress>> newAllGroups =
ArcusReplNodeAddress.makeGroupAddrsList(findAddrsOfChangedGroups(addrs, changedGroups));
ArcusReplNodeAddress.makeGroupAddrsList(addrs);

// remove invalidated groups in changedGroups
for (Map.Entry<String, List<ArcusReplNodeAddress>> entry : newAllGroups.entrySet()) {
if (!ArcusReplNodeAddress.validateGroup(entry)) {
changedGroups.remove(entry.getKey());
}
}
Set<String> invalidGroups = new HashSet<>();

// Get the existing groups from the locator
Map<String, MemcachedReplicaGroup> oldAllGroups =
((ArcusReplKetamaNodeLocator) locator).getAllGroups();

for (String changedGroupName : changedGroups) {
MemcachedReplicaGroup oldGroup = oldAllGroups.get(changedGroupName);
List<ArcusReplNodeAddress> newGroupAddrs = newAllGroups.get(changedGroupName);

if (oldGroup == null) {
// Newly added group
for (ArcusReplNodeAddress newAddr : newGroupAddrs) {
for (Map.Entry<String, List<ArcusReplNodeAddress>> entry : newAllGroups.entrySet()) {
if (!ArcusReplNodeAddress.validateGroup(entry)) {
invalidGroups.add(entry.getKey());
continue;
}
// Handle newly added groups
if (!oldAllGroups.containsKey(entry.getKey())) {
for (ArcusReplNodeAddress newAddr : entry.getValue()) {
attachNodes.add(attachMemcachedNode(newAddr));
}
}
}

for (Map.Entry<String, MemcachedReplicaGroup> oldGroupEntry : oldAllGroups.entrySet()) {
String groupName = oldGroupEntry.getKey();
MemcachedReplicaGroup oldGroup = oldGroupEntry.getValue();
List<ArcusReplNodeAddress> newGroupAddrs = newAllGroups.get(groupName);

// If group name exists in old groups, invalid case is ignored.
if (invalidGroups.contains(groupName)) {
continue;
}

Expand Down

0 comments on commit d6ad65c

Please sign in to comment.