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