From 511f01bd140a117938e4e7b6b78d96012af07a5e Mon Sep 17 00:00:00 2001 From: christianEQ Date: Thu, 4 Feb 2021 22:36:32 +0000 Subject: [PATCH] fixed up backup stuff Former-commit-id: 8a23162c24dc13a4268928c5defd85b0b5249d39 --- src/db.cpp | 63 ++++++--------------------------------------- src/replication.cpp | 8 +++--- src/server.h | 12 +++------ 3 files changed, 15 insertions(+), 68 deletions(-) diff --git a/src/db.cpp b/src/db.cpp index 6327a1e44..ff96e4674 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -516,12 +516,7 @@ long long emptyDbStructure(redisDb **dbarray, int dbnum, int async, for (int j = startdb; j <= enddb; j++) { removed += dbarray[j]->size(); - if (async) { - dbarray[j]->emptyDbAsync(); - } else { - dictEmpty(dbarray[j]->dictUnsafeKeyOnly(),callback); - dbarray[j]->setexpireUnsafe()->clear(); - } + dbarray[j]->clear(async, callback); /* Because all keys of database are removed, reset average ttl. */ dbarray[j]->avg_ttl = 0; dbarray[j]->last_expire_set = 0; @@ -585,47 +580,21 @@ long long emptyDb(int dbnum, int flags, void(callback)(void*)) { /* Store a backup of the database for later use, and put an empty one * instead of it. */ -dbBackup *backupDb(void) { - dbBackup *backup = (dbBackup*)zmalloc(sizeof(dbBackup)); - - /* Backup main DBs. */ - backup->dbarray = (redisDb**)zmalloc(sizeof(redisDb*)*cserver.dbnum); +const redisDbPersistentDataSnapshot **backupDb(void) { + const redisDbPersistentDataSnapshot **backup = (const redisDbPersistentDataSnapshot**)zmalloc(sizeof(redisDbPersistentDataSnapshot*)*cserver.dbnum); for (int i=0; idbarray[i] = g_pserver->db[i]; - g_pserver->db[i] = new (MALLOC_LOCAL) redisDb(); - g_pserver->db[i]->initialize(i); + backup[i] = g_pserver->db[i]->createSnapshot(LLONG_MAX, false); } - - /* Backup cluster slots to keys map if enable cluster. */ - if (g_pserver->cluster_enabled) { - backup->slots_to_keys = g_pserver->cluster->slots_to_keys; - memcpy(backup->slots_keys_count, g_pserver->cluster->slots_keys_count, - sizeof(g_pserver->cluster->slots_keys_count)); - g_pserver->cluster->slots_to_keys = raxNew(); - memset(g_pserver->cluster->slots_keys_count, 0, - sizeof(g_pserver->cluster->slots_keys_count)); - } - return backup; } /* Discard a previously created backup, this can be slow (similar to FLUSHALL) * Arguments are similar to the ones of emptyDb, see EMPTYDB_ flags. */ -void discardDbBackup(dbBackup *buckup, int flags, void(callback)(void*)) { - int async = (flags & EMPTYDB_ASYNC); - +void discardDbBackup(const redisDbPersistentDataSnapshot **buckup, int flags, void(callback)(void*)) { /* Release main DBs backup . */ - emptyDbStructure(buckup->dbarray, -1, async, callback); for (int i=0; idbarray[i]->dictUnsafeKeyOnly()); - buckup->dbarray[i]->delete_setexpire(); + g_pserver->db[i]->endSnapshot(buckup[i]); } - - /* Release slots to keys map backup if enable cluster. */ - if (g_pserver->cluster_enabled) freeSlotsToKeysMap(buckup->slots_to_keys, async); - - /* Release buckup. */ - zfree(buckup->dbarray); zfree(buckup); } @@ -633,27 +602,11 @@ void discardDbBackup(dbBackup *buckup, int flags, void(callback)(void*)) { * in the db). * This function should be called after the current contents of the database * was emptied with a previous call to emptyDb (possibly using the async mode). */ -void restoreDbBackup(dbBackup *buckup) { +void restoreDbBackup(const redisDbPersistentDataSnapshot **buckup) { /* Restore main DBs. */ for (int i=0; idb[i]->size() == 0); - serverAssert(g_pserver->db[i]->expireSize() == 0); - dictRelease(g_pserver->db[i]->dictUnsafeKeyOnly()); - g_pserver->db[i]->delete_setexpire(); - g_pserver->db[i] = buckup->dbarray[i]; + g_pserver->db[i]->restoreSnapshot(buckup[i]); } - - /* Restore slots to keys map backup if enable cluster. */ - if (g_pserver->cluster_enabled) { - serverAssert(g_pserver->cluster->slots_to_keys->numele == 0); - raxFree(g_pserver->cluster->slots_to_keys); - g_pserver->cluster->slots_to_keys = buckup->slots_to_keys; - memcpy(g_pserver->cluster->slots_keys_count, buckup->slots_keys_count, - sizeof(g_pserver->cluster->slots_keys_count)); - } - - /* Release buckup. */ - zfree(buckup->dbarray); zfree(buckup); } diff --git a/src/replication.cpp b/src/replication.cpp index 5e381ebc6..aac9c9e9b 100644 --- a/src/replication.cpp +++ b/src/replication.cpp @@ -1874,7 +1874,7 @@ static int useDisklessLoad() { /* Helper function for readSyncBulkPayload() to make backups of the current * databases before socket-loading the new ones. The backups may be restored * by disklessLoadRestoreBackup or freed by disklessLoadDiscardBackup later. */ -dbBackup *disklessLoadMakeBackup(void) { +const redisDbPersistentDataSnapshot **disklessLoadMakeBackup(void) { return backupDb(); } @@ -1884,13 +1884,13 @@ dbBackup *disklessLoadMakeBackup(void) { * * If the socket loading went wrong, we want to restore the old backups * into the server databases. */ -void disklessLoadRestoreBackup(dbBackup *buckup) { +void disklessLoadRestoreBackup(const redisDbPersistentDataSnapshot **buckup) { restoreDbBackup(buckup); } /* Helper function for readSyncBulkPayload() to discard our old backups * when the loading succeeded. */ -void disklessLoadDiscardBackup(dbBackup *buckup, int flag) { +void disklessLoadDiscardBackup(const redisDbPersistentDataSnapshot **buckup, int flag) { discardDbBackup(buckup, flag, replicationEmptyDbCallback); } @@ -1900,7 +1900,7 @@ void readSyncBulkPayload(connection *conn) { char buf[PROTO_IOBUF_LEN]; ssize_t nread, readlen, nwritten; int use_diskless_load = useDisklessLoad(); - dbBackup *diskless_load_backup = NULL; + const redisDbPersistentDataSnapshot **diskless_load_backup = NULL; rdbSaveInfo rsi = RDB_SAVE_INFO_INIT; int empty_db_flags = g_pserver->repl_slave_lazy_flush ? EMPTYDB_ASYNC : EMPTYDB_NO_FLAGS; diff --git a/src/server.h b/src/server.h index c33a8eb88..54bb515b9 100644 --- a/src/server.h +++ b/src/server.h @@ -1074,11 +1074,6 @@ public: return m_setexpire->random_value(); } - void delete_setexpire() - { - delete m_setexpire; - } - dict_iter end() { return dict_iter(nullptr, nullptr); } dict_const_iter end() const { return dict_const_iter(nullptr); } @@ -1291,7 +1286,6 @@ struct redisDb : public redisDbPersistentDataSnapshot using redisDbPersistentData::expireSize; using redisDbPersistentData::removeExpire; using redisDbPersistentData::removeSubkeyExpire; - using redisDbPersistentData::delete_setexpire; using redisDbPersistentData::clear; using redisDbPersistentData::emptyDbAsync; using redisDbPersistentData::iterate; @@ -3031,9 +3025,9 @@ long long emptyDb(int dbnum, int flags, void(callback)(void*)); long long emptyDbStructure(redisDb **dbarray, int dbnum, int flags, void(callback)(void*)); void flushAllDataAndResetRDB(int flags); long long dbTotalServerKeyCount(); -dbBackup *backupDb(void); -void restoreDbBackup(dbBackup *buckup); -void discardDbBackup(dbBackup *buckup, int flags, void(callback)(void*)); +const redisDbPersistentDataSnapshot **backupDb(void); +void restoreDbBackup(const redisDbPersistentDataSnapshot **buckup); +void discardDbBackup(const redisDbPersistentDataSnapshot **buckup, int flags, void(callback)(void*)); int selectDb(client *c, int id);