From 8e11f84dedf87a151059eacb91782d937afeeefd Mon Sep 17 00:00:00 2001 From: "zhaozhao.zz" Date: Thu, 7 Dec 2023 14:30:48 +0800 Subject: [PATCH] Fix replica node cannot expand dicts when loading legacy RDB (#12839) When loading RDB on cluster nodes, it is necessary to consider the scenario where a node is a replica. For example, during a rolling upgrade, new version instances are often mounted as replicas on old version instances. In this case, the full synchronization legacy RDB does not contain slot information, and the new version instance, acting as a replica, should be able to handle the legacy RDB correctly for `dbExpand`. Additionally, renaming `getMyClusterSlotCount` to `getMyShardSlotCount` would be appropriate. Introduced in #11695 --- src/cluster.h | 2 +- src/cluster_legacy.c | 10 ++++++++-- src/db.c | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/cluster.h b/src/cluster.h index 0cf71ad4d..97a4febd5 100644 --- a/src/cluster.h +++ b/src/cluster.h @@ -80,7 +80,7 @@ int clusterNodeIsMyself(clusterNode *n); clusterNode *getMyClusterNode(void); char *getMyClusterId(void); int getClusterSize(void); -int getMyClusterSlotCount(void); +int getMyShardSlotCount(void); int handleDebugClusterCommand(client *c); int clusterNodePending(clusterNode *node); int clusterNodeIsMaster(clusterNode *n); diff --git a/src/cluster_legacy.c b/src/cluster_legacy.c index 329d2d098..340b2dfe9 100644 --- a/src/cluster_legacy.c +++ b/src/cluster_legacy.c @@ -5781,8 +5781,14 @@ int getClusterSize(void) { return dictSize(server.cluster->nodes); } -int getMyClusterSlotCount(void) { - return server.cluster->myself->numslots; +int getMyShardSlotCount(void) { + if (!nodeIsSlave(server.cluster->myself)) { + return server.cluster->myself->numslots; + } else if (server.cluster->myself->slaveof) { + return server.cluster->myself->slaveof->numslots; + } else { + return 0; + } } char **getClusterNodesList(size_t *numnodes) { diff --git a/src/db.c b/src/db.c index 1de2f2601..f82e090ed 100644 --- a/src/db.c +++ b/src/db.c @@ -2215,7 +2215,7 @@ int dbExpand(const redisDb *db, uint64_t db_size, dbKeyType keyType, int try_exp if (server.cluster_enabled) { /* We don't know exact number of keys that would fall into each slot, but we can * approximate it, assuming even distribution, divide it by the number of slots. */ - int slots = getMyClusterSlotCount(); + int slots = getMyShardSlotCount(); if (slots == 0) return C_OK; db_size = db_size / slots;