Commit 0914e89a authored by Vitaly Arbuzov's avatar Vitaly Arbuzov
Browse files

Resize DB if there is no slot size info

parent 1f9c280a
......@@ -1854,12 +1854,11 @@ int expireIfNeeded(redisDb *db, robj *key, int flags) {
* this node owns. */
void expandDb(const redisDb *db, uint64_t db_size) {
if (server.cluster_enabled) {
dict *d;
dbIterator dbit;
dbIteratorInit(&dbit, (redisDb *) db);
while ((d = dbIteratorNextDict(&dbit))) {
/* We don't exact number of keys that would fall into each slot, but we can approximate it, assuming even distribution. */
dictExpand(d, (db_size / server.cluster->myself->numslots));
for (int i = 0; i < CLUSTER_SLOTS; i++) {
if (clusterNodeGetSlotBit(server.cluster->myself, i)) {
/* We don't know exact number of keys that would fall into each slot, but we can approximate it, assuming even distribution. */
dictExpand(db->dict[i], (db_size / server.cluster->myself->numslots));
}
}
} else {
dictExpand(db->dict[0], db_size);
......
......@@ -3002,6 +3002,8 @@ int rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi) {
int rdbLoadRioWithLoadingCtx(rio *rdb, int rdbflags, rdbSaveInfo *rsi, rdbLoadingCtx *rdb_loading_ctx) {
uint64_t dbid = 0;
int type, rdbver;
uint64_t db_size = 0;
int no_slot_info = 1;
redisDb *db = rdb_loading_ctx->dbarray+0;
char buf[1024];
int error;
......@@ -3077,15 +3079,12 @@ int rdbLoadRioWithLoadingCtx(rio *rdb, int rdbflags, rdbSaveInfo *rsi, rdbLoadin
} else if (type == RDB_OPCODE_RESIZEDB) {
/* RESIZEDB: Hint about the size of the keys in the currently
* selected data base, in order to avoid useless rehashing. */
uint64_t db_size, expires_size;
uint64_t expires_size;
if ((db_size = rdbLoadLen(rdb,NULL)) == RDB_LENERR)
goto eoferr;
if ((expires_size = rdbLoadLen(rdb,NULL)) == RDB_LENERR)
goto eoferr;
/* In cluster mode we don't resize whole DB, instead we rely on RDB_OPCODE_SLOT_INFO for resizing slots. */
if (!server.cluster_enabled) {
expandDb(db, db_size);
}
/* Main dictionary will be resized after reading OPCODE set. Resizing only expires here. */
dictExpand(db->expires,expires_size);
continue; /* Read next opcode. */
} else if (type == RDB_OPCODE_SLOT_INFO) {
......@@ -3099,6 +3098,7 @@ int rdbLoadRioWithLoadingCtx(rio *rdb, int rdbflags, rdbSaveInfo *rsi, rdbLoadin
goto eoferr;
/* In cluster mode we resize individual slot specific dictionaries based on the number of keys that slot holds. */
dictExpand(db->dict[slot_id], slot_size);
no_slot_info = 0;
continue; /* Read next opcode. */
} else if (type == RDB_OPCODE_AUX) {
/* AUX: generic string-string fields. Use to add state to RDB
......@@ -3228,6 +3228,12 @@ int rdbLoadRioWithLoadingCtx(rio *rdb, int rdbflags, rdbSaveInfo *rsi, rdbLoadin
continue;
}
/* If there is no slot info, it means that it's either not cluster mode or we are trying to load legacy RDB file.
* In this case we want to estimate number of keys per slot and resize accordingly. */
if (no_slot_info) {
expandDb(db, db_size);
}
/* Read key */
if ((key = rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS,NULL)) == NULL)
goto eoferr;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment