-
Notifications
You must be signed in to change notification settings - Fork 704
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
Trigger manual failover on SIGTERM / shutdown to cluster primary #1091
base: unstable
Are you sure you want to change the base?
Changes from 15 commits
6ab8888
4b49f03
f9ca731
df0ef8d
594fd5a
519eb2a
32043dd
e7b33fa
d6649e5
64831c9
b06a8c4
5f7b429
e56a360
0ccc4e4
c9bfd69
c8037a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1233,6 +1233,40 @@ void clusterInitLast(void) { | |
|
||
/* Called when a cluster node receives SHUTDOWN. */ | ||
void clusterHandleServerShutdown(void) { | ||
if (nodeIsPrimary(myself) && 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); | ||
/* This is done only when the replica offset is caught up, to avoid data loss. | ||
* And 0x800ff is 8.0.255, we only support new versions for this feature. */ | ||
if (replica->repl_data->repl_state == REPLICA_STATE_ONLINE && | ||
// replica->repl_data->replica_version > 0x800ff && | ||
replica->name && sdslen(replica->name->ptr) == CLUSTER_NAMELEN && | ||
replica->repl_data->repl_ack_off == server.primary_repl_offset) { | ||
best_replica = replica; | ||
break; | ||
} | ||
} | ||
|
||
if (best_replica) { | ||
/* Send a CLUSTER FAILOVER FORCE to the best replica. */ | ||
char buf[128]; | ||
size_t buflen = snprintf(buf, sizeof(buf), "*5\r\n$7\r\nCLUSTER\r\n$8\r\nFAILOVER\r\n$5\r\nFORCE\r\n$9\r\nreplicaid\r\n$%d\r\n%s\r\n", CLUSTER_NAMELEN, (char *)best_replica->name->ptr); | ||
enjoy-binbin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (connWrite(best_replica->conn, buf, buflen) == (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); | ||
|
@@ -6998,32 +7032,46 @@ int clusterCommandSpecial(client *c) { | |
} else { | ||
addReplyLongLong(c, clusterNodeFailureReportsCount(n)); | ||
} | ||
} else if (!strcasecmp(c->argv[1]->ptr, "failover") && (c->argc == 2 || c->argc == 3)) { | ||
/* CLUSTER FAILOVER [FORCE|TAKEOVER] */ | ||
} else if (!strcasecmp(c->argv[1]->ptr, "failover") && (c->argc >= 2)) { | ||
/* CLUSTER FAILOVER [FORCE|TAKEOVER] [replicaid <node id>] */ | ||
zuiderkwast marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int force = 0, takeover = 0; | ||
robj *replicaid = NULL; | ||
|
||
if (c->argc == 3) { | ||
if (!strcasecmp(c->argv[2]->ptr, "force")) { | ||
for (int j = 2; j < c->argc; j++) { | ||
int moreargs = (c->argc - 1) - j; | ||
if (!strcasecmp(c->argv[j]->ptr, "force")) { | ||
force = 1; | ||
} else if (!strcasecmp(c->argv[2]->ptr, "takeover")) { | ||
} else if (!strcasecmp(c->argv[j]->ptr, "takeover")) { | ||
takeover = 1; | ||
force = 1; /* Takeover also implies force. */ | ||
} else if (!strcasecmp(c->argv[j]->ptr, "replicaid") && moreargs) { | ||
j++; | ||
replicaid = c->argv[j]; | ||
} else { | ||
addReplyErrorObject(c, shared.syntaxerr); | ||
return 1; | ||
} | ||
} | ||
|
||
/* Check if it should be executed by myself. */ | ||
if (replicaid != NULL) { | ||
clusterNode *n = clusterLookupNode(replicaid->ptr, sdslen(replicaid->ptr)); | ||
if (n != myself) { | ||
enjoy-binbin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/* Ignore this command, including the sanity check and the process. */ | ||
addReply(c, shared.ok); | ||
return 1; | ||
} | ||
} | ||
|
||
/* Check preconditions. */ | ||
if (clusterNodeIsPrimary(myself)) { | ||
addReplyError(c, "You should send CLUSTER FAILOVER to a replica"); | ||
if (replicaid == NULL) addReplyError(c, "You should send CLUSTER FAILOVER to a replica"); | ||
return 1; | ||
} else if (myself->replicaof == NULL) { | ||
addReplyError(c, "I'm a replica but my master is unknown to me"); | ||
if (replicaid == NULL) addReplyError(c, "I'm a replica but my master is unknown to me"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why add this? If the primary is unknown, the failover can't succeed, so I think we need to return an error even if REPLICAID is sent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering if there would be some races, that a replica return an error to the priamry. Or maybe we should always return OK if replicaid is passed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replicas don't send the replies to primaries. Only problem is the confic to panic on repöocation errors. But i can't see any races. Can you? We can return ok if you want. Maybe we should check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we will write it to the backlog, i am worry that after some down and up, the psync will get the command and return an error. Though I haven't verified it specifically. |
||
return 1; | ||
} else if (!force && (nodeFailed(myself->replicaof) || myself->replicaof->link == NULL)) { | ||
addReplyError(c, "Master is down or failed, " | ||
"please use CLUSTER FAILOVER FORCE"); | ||
if (replicaid == NULL) addReplyError(c, "Master is down or failed, please use CLUSTER FAILOVER FORCE"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why add this? I think it should be an error even with REPLICAID. |
||
return 1; | ||
} | ||
resetManualFailover(); | ||
|
@@ -7042,7 +7090,11 @@ int clusterCommandSpecial(client *c) { | |
/* If this is a forced failover, we don't need to talk with our | ||
* primary to agree about the offset. We just failover taking over | ||
* it without coordination. */ | ||
serverLog(LL_NOTICE, "Forced failover user request accepted (user request from '%s').", client); | ||
if (c == server.primary) { | ||
serverLog(LL_NOTICE, "Forced failover primary request accepted (primary request from '%s').", client); | ||
} else { | ||
serverLog(LL_NOTICE, "Forced failover user request accepted (user request from '%s').", client); | ||
} | ||
manualFailoverCanStart(); | ||
/* We can start a manual failover as soon as possible, setting a flag | ||
* here so that we don't need to waiting for the cron to kick in. */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
proc shutdown_how {srv_id how} { | ||
if {$how == "shutdown"} { | ||
catch {R $srv_id shutdown nosave} | ||
} elseif {$how == "sigterm"} { | ||
exec kill -SIGTERM [s -$srv_id process_id] | ||
} | ||
} | ||
|
||
# We will start a cluster with 3 primary nodes and 4 replicas, the primary 1 will have 2 replicas. | ||
# We will pause the replica 1, and then shutdown the primary 1, and making replica 2 to become | ||
# the new primary. | ||
proc test_main {how shutdown_timeout} { | ||
test "auto-failover-on-shutdown will always pick a best replica and send CLUSTER FAILOVER - $how - shutdown-timeout: $shutdown_timeout" { | ||
set primary [srv 0 client] | ||
set replica1 [srv -3 client] | ||
set replica1_pid [s -3 process_id] | ||
set replica2 [srv -6 client] | ||
set replica2_ip [srv -6 host] | ||
set replica2_port [srv -6 port] | ||
|
||
$primary config set auto-failover-on-shutdown yes | ||
$primary config set shutdown-timeout $shutdown_timeout | ||
$primary config set repl-ping-replica-period 3600 | ||
|
||
# To avoid failover kick in. | ||
$replica2 config set cluster-replica-no-failover yes | ||
|
||
# Pause a replica so it has no chance to catch up with the offset. | ||
pause_process $replica1_pid | ||
|
||
# Primary write some data to increase the offset. | ||
for {set i 0} {$i < 10} {incr i} { | ||
$primary incr key_991803 | ||
} | ||
|
||
if {$shutdown_timeout == 0} { | ||
# Wait the replica2 catch up with the offset | ||
wait_for_ofs_sync $primary $replica2 | ||
wait_replica_acked_ofs $primary $replica2 $replica2_ip $replica2_port | ||
} else { | ||
# If shutdown-timeout is enable, we expect the primary to pause writing | ||
# and wait for the replica to catch up with the offset. | ||
} | ||
|
||
# Shutdown the primary. | ||
shutdown_how 0 $how | ||
|
||
# Wait for the replica2 to become a primary. | ||
wait_for_condition 1000 50 { | ||
[s -6 role] eq {master} | ||
zuiderkwast marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
puts "s -6 role: [s -6 role]" | ||
fail "Failover does not happened" | ||
} | ||
|
||
# Make sure that the expected logs are printed. | ||
verify_log_message 0 "*Sending CLUSTER FAILOVER FORCE to replica*" 0 | ||
verify_log_message -6 "*Forced failover primary request accepted*" 0 | ||
|
||
resume_process $replica1_pid | ||
} | ||
|
||
test "Unable to find a replica to perform an auto failover - $how" { | ||
set primary [srv -6 client] | ||
set replica1 [srv -3 client] | ||
set replica1_pid [s -3 process_id] | ||
|
||
pause_process $replica1_pid | ||
|
||
$primary config set auto-failover-on-shutdown yes | ||
$primary client kill type replica | ||
shutdown_how 6 $how | ||
wait_for_log_messages -6 {"*Unable to find a replica to perform an auto failover on shutdown*"} 0 1000 10 | ||
|
||
resume_process $replica1_pid | ||
} | ||
} | ||
|
||
start_cluster 3 4 {tags {external:skip cluster}} { | ||
test_main "shutdown" 0 | ||
} | ||
|
||
start_cluster 3 4 {tags {external:skip cluster}} { | ||
test_main "sigterm" 0 | ||
} | ||
|
||
start_cluster 3 4 {tags {external:skip cluster}} { | ||
test_main "shutdown" 10 | ||
} | ||
|
||
start_cluster 3 4 {tags {external:skip cluster}} { | ||
test_main "sigterm" 10 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot to uncomment the check?
In the comment, maybe we shall not say "new" because in a few years, this will not be new anymore.
Why not check
>= 0x80100
instead> 0x800ff
? It's the same but maybe easier to read?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i take this from
just a easy way that i can test in local.