From 3bab69e921039ad2e4f56c5842c4cf3e73a598dd Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 4 Mar 2020 17:44:21 +0100 Subject: [PATCH] Make sync RDB deletion configurable. Default to no. --- src/config.c | 1 + src/replication.c | 23 +++++++++++++++++++---- src/server.h | 2 ++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/config.c b/src/config.c index fd04b7c87..211b6d003 100644 --- a/src/config.c +++ b/src/config.c @@ -2084,6 +2084,7 @@ standardConfig configs[] = { createBoolConfig("always-show-logo", NULL, IMMUTABLE_CONFIG, server.always_show_logo, 0, NULL, NULL), createBoolConfig("protected-mode", NULL, MODIFIABLE_CONFIG, server.protected_mode, 1, NULL, NULL), createBoolConfig("rdbcompression", NULL, MODIFIABLE_CONFIG, server.rdb_compression, 1, NULL, NULL), + createBoolConfig("rdb-del-sync-files", NULL, MODIFIABLE_CONFIG, server.rdb_del_sync_files, 0, NULL, NULL), createBoolConfig("activerehashing", NULL, MODIFIABLE_CONFIG, server.activerehashing, 1, NULL, NULL), createBoolConfig("stop-writes-on-bgsave-error", NULL, MODIFIABLE_CONFIG, server.stop_writes_on_bgsave_err, 1, NULL, NULL), createBoolConfig("dynamic-hz", NULL, MODIFIABLE_CONFIG, server.dynamic_hz, 1, NULL, NULL), /* Adapt hz to # of clients.*/ diff --git a/src/replication.c b/src/replication.c index 20666bd20..429e1d8a8 100644 --- a/src/replication.c +++ b/src/replication.c @@ -625,8 +625,12 @@ int startBgsaveForReplication(int mincapa) { } /* If we succeeded to start a BGSAVE with disk target, let's remember - * this fact, so that we can later delete the file if needed. */ - if (retval == C_OK && !socket_target) RDBGeneratedByReplication = 1; + * this fact, so that we can later delete the file if needed. Note + * that we don't set the flag to 1 if the feature is disabled, otherwise + * it would never be cleared: the file is not deleted. This way if + * the user enables it later with CONFIG SET, we are fine. */ + if (retval == C_OK && !socket_target && server.rdb_del_sync_files) + RDBGeneratedByReplication = 1; /* If we failed to BGSAVE, remove the slaves waiting for a full * resynchronization from the list of slaves, inform them with @@ -926,6 +930,17 @@ void putSlaveOnline(client *slave) { * to take RDB files around, this violates certain policies in certain * environments. */ void removeRDBUsedToSyncReplicas(void) { + /* If the feature is disabled, return ASAP but also clear the + * RDBGeneratedByReplication flag in case it was set. Otherwise if the + * feature was enabled, but gets disabled later with CONFIG SET, the + * flag may remain set to one: then next time the feature is re-enabled + * via CONFIG SET we have have it set even if no RDB was generated + * because of replication recently. */ + if (!server.rdb_del_sync_files) { + RDBGeneratedByReplication = 0; + return; + } + if (allPersistenceDisabled() && RDBGeneratedByReplication) { client *slave; listNode *ln; @@ -1713,7 +1728,7 @@ void readSyncBulkPayload(connection *conn) { "Failed trying to load the MASTER synchronization " "DB from disk"); cancelReplicationHandshake(); - if (allPersistenceDisabled()) { + if (server.rdb_del_sync_files && allPersistenceDisabled()) { serverLog(LL_NOTICE,"Removing the RDB file obtained from " "the master. This replica has persistence " "disabled"); @@ -1725,7 +1740,7 @@ void readSyncBulkPayload(connection *conn) { } /* Cleanup. */ - if (allPersistenceDisabled()) { + if (server.rdb_del_sync_files && allPersistenceDisabled()) { serverLog(LL_NOTICE,"Removing the RDB file obtained from " "the master. This replica has persistence " "disabled"); diff --git a/src/server.h b/src/server.h index 5763671e4..3c19a17ea 100644 --- a/src/server.h +++ b/src/server.h @@ -1202,6 +1202,8 @@ struct redisServer { char *rdb_filename; /* Name of RDB file */ int rdb_compression; /* Use compression in RDB? */ int rdb_checksum; /* Use RDB checksum? */ + int rdb_del_sync_files; /* Remove RDB files used only for SYNC if + the instance does not use persistence. */ time_t lastsave; /* Unix time of last successful save */ time_t lastbgsave_try; /* Unix time of last attempted bgsave */ time_t rdb_save_time_last; /* Time used by last RDB save run. */