From bb3b5ddd1968d2715acc37b63124ccf461276160 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 20 Sep 2017 11:28:13 +0200 Subject: PSYNC2: More refinements related to #4316. --- src/rdb.c | 24 +++++++++++++----------- src/replication.c | 1 + 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/rdb.c b/src/rdb.c index c83a05a32..125df6071 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -2019,14 +2019,7 @@ void bgsaveCommand(client *c) { } /* Populate the rdbSaveInfo structure used to persist the replication - * information inside the RDB file. - * For master, if server.repl_backlog is not NULL, fill rdbSaveInfo with - * server.slaveseldb, otherwise just use init -1. - * Don't worry, master will send SELECT command to replication stream, - * because if server.repl_backlog is NULL, that will trigger full synchronization, - * function replicationSetupSlaveForFullResync() sets server.slaveseldb be -1, - * then replicationFeedSlaves() will send SELECT command when server.slaveseldb is -1. - * For slave, currently the structure explicitly + * information inside the RDB file. Currently the structure explicitly * contains just the currently selected DB from the master stream, however * if the rdbSave*() family functions receive a NULL rsi structure also * the Replication ID/offset is not saved. The function popultes 'rsi' @@ -2037,15 +2030,24 @@ void bgsaveCommand(client *c) { rdbSaveInfo *rdbPopulateSaveInfo(rdbSaveInfo *rsi) { rdbSaveInfo rsi_init = RDB_SAVE_INFO_INIT; *rsi = rsi_init; + + /* If the instance is a master, we can populate the replication info + * in all the cases, even if sometimes in incomplete (but safe) form. */ if (!server.masterhost) { if (server.repl_backlog) rsi->repl_stream_db = server.slaveseldb; + /* Note that if repl_backlog is NULL, it means that histories + * following from this point will trigger a full synchronization + * generating a SELECT statement, so we can leave the currently + * selected DB set to -1. This allows a restarted master to reload + * its replication ID/offset when there are no connected slaves. */ return rsi; } + + /* If the instance is a slave we need a connected master in order to + * fetch the currently selected DB. */ if (server.master) { rsi->repl_stream_db = server.master->db->id; return rsi; - } else { - serverLog(LL_WARNING,"As a slave there is no valid master, can not persist replication information"); - return NULL; } + return NULL; } diff --git a/src/replication.c b/src/replication.c index 4358d63e6..e0b3d910e 100644 --- a/src/replication.c +++ b/src/replication.c @@ -579,6 +579,7 @@ int startBgsaveForReplication(int mincapa) { else retval = rdbSaveBackground(server.rdb_filename,rsiptr); } else { + serverLog(LL_WARNING,"BGSAVE for replication: replication information not available, can't generate the RDB file right now. Try later."); retval = C_ERR; } -- cgit v1.2.1