diff --git a/src/cluster.h b/src/cluster.h index 02c5f67f3..0cf71ad4d 100644 --- a/src/cluster.h +++ b/src/cluster.h @@ -80,6 +80,7 @@ int clusterNodeIsMyself(clusterNode *n); clusterNode *getMyClusterNode(void); char *getMyClusterId(void); int getClusterSize(void); +int getMyClusterSlotCount(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 1f957c99d..b77bbdab3 100644 --- a/src/cluster_legacy.c +++ b/src/cluster_legacy.c @@ -5773,6 +5773,10 @@ int getClusterSize(void) { return dictSize(server.cluster->nodes); } +int getMyClusterSlotCount(void) { + return server.cluster->myself->numslots; +} + char **getClusterNodesList(size_t *numnodes) { size_t count = dictSize(server.cluster->nodes); char **ids = zmalloc((count+1)*CLUSTER_NAMELEN); diff --git a/src/db.c b/src/db.c index a369c9a9a..690d23ea2 100644 --- a/src/db.c +++ b/src/db.c @@ -2196,9 +2196,14 @@ int expireIfNeeded(redisDb *db, robj *key, int flags) { int dbExpand(const redisDb *db, uint64_t db_size, dbKeyType keyType, int try_expand) { dict *d; 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(); + if (slots == 0) return C_OK; + db_size = db_size / slots; + for (int i = 0; i < CLUSTER_SLOTS; i++) { if (clusterNodeCoversSlot(getMyClusterNode(), i)) { - /* We don't know exact number of keys that would fall into each slot, but we can approximate it, assuming even distribution. */ if (keyType == DB_MAIN) { d = db->dict[i]; } else {