Cluster refactor: Add failover cmd support to cluster api

The failover command is up until now not supported
in cluster mode. This commit allows a cluster
implementation to support the command. The legacy
clustering implementation still does not support
this command.

Signed-off-by: Josh Hershberg <yehoshua@redis.com>
This commit is contained in:
Josh Hershberg 2023-11-05 10:47:57 +02:00
parent c6157b3510
commit 2e5181ef28
3 changed files with 30 additions and 10 deletions

View File

@ -96,6 +96,8 @@ char* clusterNodeHostname(clusterNode *node);
const char *getPreferredEndpoint(clusterNode *n);
void migrateCommand(client *c);
long long getReplOffset(clusterNode *node);
int clusterAllowFailoverCmd(client *c);
void clusterPromoteSelfToMaster(void);
char **clusterDebugCommandHelp(void);
ConnectionType *connTypeOfCluster(void);

View File

@ -6400,11 +6400,27 @@ long long getReplOffset(clusterNode *node) {
}
const char *getPreferredEndpoint(clusterNode *n) {
char* hostname = clusterNodeHostname(n);
switch(server.cluster_preferred_endpoint_type) {
case CLUSTER_ENDPOINT_TYPE_IP: return clusterNodeIp(n);
case CLUSTER_ENDPOINT_TYPE_HOSTNAME: return (hostname != NULL && hostname[0] != '\0') ? hostname : "?";
case CLUSTER_ENDPOINT_TYPE_UNKNOWN_ENDPOINT: return "";
char *hostname = clusterNodeHostname(n);
switch (server.cluster_preferred_endpoint_type) {
case CLUSTER_ENDPOINT_TYPE_IP:
return clusterNodeIp(n);
case CLUSTER_ENDPOINT_TYPE_HOSTNAME:
return (hostname != NULL && hostname[0] != '\0') ? hostname : "?";
case CLUSTER_ENDPOINT_TYPE_UNKNOWN_ENDPOINT:
return "";
}
return "unknown";
}
int clusterAllowFailoverCmd(client *c) {
if (!server.cluster_enabled) {
return 1;
}
addReplyError(c,"FAILOVER not allowed in cluster mode. "
"Use CLUSTER FAILOVER command instead.");
return 0;
}
void clusterPromoteSelfToMaster(void) {
replicationUnsetMaster();
}

View File

@ -951,7 +951,11 @@ void syncCommand(client *c) {
}
if (!strcasecmp(c->argv[1]->ptr,server.replid)) {
replicationUnsetMaster();
if (server.cluster_enabled) {
clusterPromoteSelfToMaster();
} else {
replicationUnsetMaster();
}
sds client = catClientInfoString(sdsempty(),c);
serverLog(LL_NOTICE,
"MASTER MODE enabled (failover request from '%s')",client);
@ -4061,12 +4065,10 @@ void abortFailover(const char *err) {
* will attempt forever and must be manually aborted.
*/
void failoverCommand(client *c) {
if (server.cluster_enabled) {
addReplyError(c,"FAILOVER not allowed in cluster mode. "
"Use CLUSTER FAILOVER command instead.");
if (!clusterAllowFailoverCmd(c)) {
return;
}
/* Handle special case for abort */
if ((c->argc == 2) && !strcasecmp(c->argv[1]->ptr,"abort")) {
if (server.failover_state == NO_FAILOVER) {