Skip to content

Commit

Permalink
Change nodeid to sds
Browse files Browse the repository at this point in the history
Signed-off-by: Binbin <[email protected]>
  • Loading branch information
enjoy-binbin committed Feb 25, 2025
1 parent 8cba555 commit ade48cb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/cluster_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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. */
Expand Down
16 changes: 12 additions & 4 deletions src/replication.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 <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;
Expand Down
3 changes: 1 addition & 2 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion valkey.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ade48cb

Please sign in to comment.