diff --git a/src/cluster_legacy.c b/src/cluster_legacy.c index b2e266d381..c01be5022b 100644 --- a/src/cluster_legacy.c +++ b/src/cluster_legacy.c @@ -1269,7 +1269,8 @@ void clusterAutoFailoverOnShutdown(void) { break; } if (replica->repl_data->repl_state == REPLICA_STATE_ONLINE && - replica->repl_data->repl_ack_off == server.primary_repl_offset) { + replica->repl_data->repl_ack_off == server.primary_repl_offset && + replica->repl_data->replica_nodeid && sdslen(replica->repl_data->replica_nodeid) == CLUSTER_NAMELEN) { best_replica = replica; } } @@ -1296,12 +1297,12 @@ void clusterAutoFailoverOnShutdown(void) { "$9\r\nREPLICAID\r\n" "$%d\r\n%s\r\n", CLUSTER_NAMELEN, - best_replica->repl_data->nodeid); + best_replica->repl_data->replica_nodeid); /* Must install write handler for all replicas first before feeding * replication stream. */ prepareReplicasToWrite(); feedReplicationBuffer(buf, buflen); - serverLog(LL_NOTICE, "Perform auto failover to replica %.40s on shutdown.", best_replica->repl_data->nodeid); + serverLog(LL_NOTICE, "Perform auto failover to replica %s on shutdown.", best_replica->repl_data->replica_nodeid); } /* Called when a cluster node receives SHUTDOWN. */ diff --git a/src/replication.c b/src/replication.c index 202b1d1064..8746a01c96 100644 --- a/src/replication.c +++ b/src/replication.c @@ -1296,6 +1296,7 @@ void freeClientReplicationData(client *c) { } if (c->flag.primary) replicationHandlePrimaryDisconnection(); sdsfree(c->repl_data->replica_addr); + sdsfree(c->repl_data->replica_nodeid); zfree(c->repl_data); c->repl_data = NULL; } @@ -1503,12 +1504,19 @@ void replconfCommand(client *c) { c->repl_data->associated_rdb_client_id = (uint64_t)client_id; } else if (!strcasecmp(c->argv[j]->ptr, "set-cluster-node-id")) { /* REPLCONF SET-CLUSTER-NODE-ID */ - if (!server.cluster_enabled) return; + if (!server.cluster_enabled) { + addReplyError(c, "This instance has cluster support disabled"); + return; + } - clusterNode *n = clusterLookupNode(c->argv[2]->ptr, sdslen(c->argv[2]->ptr)); - if (!n) return; + clusterNode *n = clusterLookupNode(c->argv[2]->ptr, sdslen(c->argv[j + 1]->ptr)); + if (!n) { + addReplyErrorFormat(c, "Unknown node %s", (char *)c->argv[j + 1]->ptr); + return; + } - memcpy(c->repl_data->nodeid, n->name, CLUSTER_NAMELEN); + if (c->repl_data->replica_nodeid) sdsfree(c->repl_data->replica_nodeid); + c->repl_data->replica_nodeid = sdsdup(c->argv[j + 1]->ptr); } else { addReplyErrorFormat(c, "Unrecognized REPLCONF option: %s", (char *)c->argv[j]->ptr); return; diff --git a/src/server.h b/src/server.h index 274bab61af..ff576a7b8e 100644 --- a/src/server.h +++ b/src/server.h @@ -151,7 +151,6 @@ struct hdr_histogram; #else #define CONFIG_ACTIVE_DEFRAG_DEFAULT 1 #endif -#define CLUSTER_NAMELEN 40 /* Bucket sizes for client eviction pools. Each bucket stores clients with * memory usage of up to twice the size of the bucket below it. */ @@ -1155,7 +1154,7 @@ typedef struct ClientReplicationData { see the definition of replBufBlock. */ size_t ref_block_pos; /* Access position of referenced buffer block, i.e. the next offset to send. */ - char nodeid[CLUSTER_NAMELEN]; /* Node id in cluster mode. */ + sds replica_nodeid; /* Node id in cluster mode. */ } ClientReplicationData; typedef struct ClientModuleData { diff --git a/valkey.conf b/valkey.conf index b2b7438ec8..e283df2cfb 100644 --- a/valkey.conf +++ b/valkey.conf @@ -1694,7 +1694,7 @@ aof-timestamp-enabled no # In cluster mode, if a primary node with replicas receives a SIGTERM or is shut down, # it can proactively initiate a manual failover. This promotes one of its replicas to - # primary before shutdown, resulting in a quicker and safer transition than relying on +# primary before shutdown, resulting in a quicker and safer transition than relying on # an automatic failover. Default is 'no'. # # auto-failover-on-shutdown no