Skip to content

Commit

Permalink
FIX: Handling abnormal swithover from zk.
Browse files Browse the repository at this point in the history
  • Loading branch information
brido4125 committed Jan 20, 2025
1 parent 99a2dda commit 542c8c4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/main/java/net/spy/memcached/MemcachedConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,34 @@ private void updateReplConnections(List<InetSocketAddress> addrs) throws IOExcep
// move operation slave -> master.
taskList.add(new MoveOperationTask(
oldSlaveNode, oldMasterNode, false));
// clear the masterCandidate if the removed slave is the masterCandidate.
if (oldGroup.getMasterCandidate() == oldSlaveNode) {
oldGroup.clearMasterCandidate();
}
}
}
} else if (oldSlaveAddrs.contains(newMasterAddr)) {
if (newSlaveAddrs.contains(oldMasterAddr)) {
// Switchover
if (oldGroup.getMasterCandidate() != null) {
MemcachedNode oldMasterCandidate = oldGroup.getMasterCandidate();
if (oldMasterCandidate != null) {
ArcusReplNodeAddress masterFromZk = (ArcusReplNodeAddress) oldGroup
.getSlaveNodeBy(newMasterAddr.getIPPort()).getSocketAddress();
changeRoleGroups.add(oldGroup);
if (!masterFromZk.isSameAddress(
((ArcusReplNodeAddress) oldMasterCandidate.getSocketAddress()))) {
/**
* Moves ops from oldMasterCandidate set by cache server to newMasterCandidate.
* Handling the below case.
* old group : [oldMaster, oldSlave1, oldSlave2]
* old group after switchover response :
* [oldMaster, oldSlave1-masterCandidate, oldSlave2]
* new group from zk cache list: [slave1, X, newMaster]
*/
oldGroup.setMasterCandidateByAddr(newMasterAddr.getIPPort());
taskList.add(new MoveOperationTask(
oldMasterCandidate, oldGroup.getMasterCandidate(), false));
}
} else {
// ZK event occurs before cache server response.
oldGroup.setMasterCandidateByAddr(newMasterAddr.getIPPort());
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/spy/memcached/MemcachedReplicaGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public void setMasterCandidate() {
}
}

public void clearMasterCandidate() {
this.masterCandidate = null;
}

public void setMasterCandidateByAddr(String address) {
for (MemcachedNode node : this.getSlaveNodes()) {
if (address.equals(((ArcusReplNodeAddress) node.getSocketAddress()).getIPPort())) {
Expand Down Expand Up @@ -181,5 +185,14 @@ private MemcachedNode getNextActiveSlaveNodeNoRotate() {
public static String getGroupNameFromNode(final MemcachedNode node) {
return ((ArcusReplNodeAddress) node.getSocketAddress()).getGroupName();
}

public MemcachedNode getSlaveNodeBy(String address) {
for (MemcachedNode node : this.getSlaveNodes()) {
if (address.equals(((ArcusReplNodeAddress) node.getSocketAddress()).getIPPort())) {
return node;
}
}
return null;
}
}
/* ENABLE_REPLICATION end */

0 comments on commit 542c8c4

Please sign in to comment.