Skip to content

Commit

Permalink
Move the logic to clusterHandleServerShutdown
Browse files Browse the repository at this point in the history
Signed-off-by: Binbin <[email protected]>
  • Loading branch information
enjoy-binbin committed Oct 27, 2024
1 parent e7b33fa commit d6649e5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
26 changes: 26 additions & 0 deletions src/cluster_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,32 @@ void clusterInitLast(void) {

/* Called when a cluster node receives SHUTDOWN. */
void clusterHandleServerShutdown(void) {
if (server.auto_failover_on_shutdown) {
/* Find the first best replica, that is, the replica with the largest offset. */
client *best_replica = NULL;
listIter replicas_iter;
listNode *replicas_list_node;
listRewind(server.replicas, &replicas_iter);
while ((replicas_list_node = listNext(&replicas_iter)) != NULL) {
client *replica = listNodeValue(replicas_list_node);
if (replica->repl_state != REPLICA_STATE_ONLINE) continue;
if (best_replica == NULL || replica->repl_ack_off > best_replica->repl_ack_off) best_replica = replica;
if (best_replica->repl_ack_off == server.primary_repl_offset) break;
}
if (best_replica) {
/* Send a CLUSTER FAILOVER FORCE to the best replica. */
const char *buf = "*3\r\n$7\r\nCLUSTER\r\n$8\r\nFAILOVER\r\n$5\r\nFORCE\r\n";
if (connWrite(best_replica->conn, buf, strlen(buf)) == (int)strlen(buf)) {
serverLog(LL_NOTICE, "Sending CLUSTER FAILOVER FORCE to replica %s succeeded.",
replicationGetReplicaName(best_replica));
} else {
serverLog(LL_WARNING, "Failed to send CLUSTER FAILOVER FORCE to replica: %s", strerror(errno));
}
} else {
serverLog(LL_NOTICE, "Unable to find a replica to perform an auto failover on shutdown.");
}
}

/* The error logs have been logged in the save function if the save fails. */
serverLog(LL_NOTICE, "Saving the cluster configuration file before exiting.");
clusterSaveConfig(1);
Expand Down
24 changes: 0 additions & 24 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -4308,7 +4308,6 @@ int finishShutdown(void) {
int force = server.shutdown_flags & SHUTDOWN_FORCE;

/* Log a warning for each replica that is lagging. */
client *best_replica = NULL;
listIter replicas_iter;
listNode *replicas_list_node;
int num_replicas = 0, num_lagging_replicas = 0;
Expand All @@ -4323,14 +4322,6 @@ int finishShutdown(void) {
replicationGetReplicaName(replica), server.primary_repl_offset - replica->repl_ack_off, lag,
replstateToString(replica->repl_state));
}
/* Find the best replica, that is, the replica with the largest offset. */
if (replica->repl_state == REPLICA_STATE_ONLINE) {
if (best_replica == NULL) {
best_replica = replica;
} else if (replica->repl_ack_off > best_replica->repl_ack_off) {
best_replica = replica;
}
}
}
if (num_replicas > 0) {
serverLog(LL_NOTICE, "%d of %d replicas are in sync when shutting down.", num_replicas - num_lagging_replicas,
Expand Down Expand Up @@ -4430,21 +4421,6 @@ int finishShutdown(void) {
* send them pending writes. */
flushReplicasOutputBuffers();

if (server.auto_failover_on_shutdown && server.cluster_enabled && best_replica) {
/* Sending a CLUSTER FAILOVER FORCE to the best replica. */
const char *buf = "*3\r\n$7\r\nCLUSTER\r\n$8\r\nFAILOVER\r\n$5\r\nFORCE\r\n";
if (connWrite(best_replica->conn, buf, strlen(buf)) == (int)strlen(buf)) {
serverLog(LL_NOTICE, "Sending CLUSTER FAILOVER FORCE to replica %s succeeded.",
replicationGetReplicaName(best_replica));
} else {
serverLog(LL_WARNING, "Failed to send CLUSTER FAILOVER FORCE to replica: %s", strerror(errno));
}
}

if (server.auto_failover_on_shutdown && server.cluster_enabled && !best_replica) {
serverLog(LL_WARNING, "Unable to find a replica to perform an auto failover on shutdown.");
}

/* Close the listening sockets. Apparently this allows faster restarts. */
closeListeningSockets(1);

Expand Down

0 comments on commit d6649e5

Please sign in to comment.