Populate the rdbSaveInfo structure used to persist the replication * 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' * that is normally stack-allocated in the c
| 4017 | * is returned, and the RDB saving will not persist any replication related |
| 4018 | * information. */ |
| 4019 | rdbSaveInfo *rdbPopulateSaveInfo(rdbSaveInfo *rsi) { |
| 4020 | rdbSaveInfo rsi_init; |
| 4021 | *rsi = rsi_init; |
| 4022 | |
| 4023 | memcpy(rsi->repl_id, g_pserver->replid, sizeof(g_pserver->replid)); |
| 4024 | rsi->master_repl_offset = g_pserver->master_repl_offset; |
| 4025 | |
| 4026 | if (g_pserver->fActiveReplica) { |
| 4027 | listIter li; |
| 4028 | listNode *ln = nullptr; |
| 4029 | listRewind(g_pserver->masters, &li); |
| 4030 | while ((ln = listNext(&li))) { |
| 4031 | redisMaster *mi = (redisMaster*)listNodeValue(ln); |
| 4032 | MasterSaveInfo msi(*mi); |
| 4033 | rsi->addMaster(msi); |
| 4034 | } |
| 4035 | } |
| 4036 | |
| 4037 | /* If the instance is a master, we can populate the replication info |
| 4038 | * only when repl_backlog is not NULL. If the repl_backlog is NULL, |
| 4039 | * it means that the instance isn't in any replication chains. In this |
| 4040 | * scenario the replication info is useless, because when a replica |
| 4041 | * connects to us, the NULL repl_backlog will trigger a full |
| 4042 | * synchronization, at the same time we will use a new replid and clear |
| 4043 | * replid2. */ |
| 4044 | if (g_pserver->fActiveReplica || (!listLength(g_pserver->masters) && g_pserver->repl_backlog)) { |
| 4045 | /* Note that when g_pserver->replicaseldb is -1, it means that this master |
| 4046 | * didn't apply any write commands after a full synchronization. |
| 4047 | * So we can let repl_stream_db be 0, this allows a restarted replica |
| 4048 | * to reload replication ID/offset, it's safe because the next write |
| 4049 | * command must generate a SELECT statement. */ |
| 4050 | rsi->repl_stream_db = g_pserver->replicaseldb == -1 ? 0 : g_pserver->replicaseldb; |
| 4051 | return rsi; |
| 4052 | } |
| 4053 | |
| 4054 | struct redisMaster *miFirst = (redisMaster*)(listLength(g_pserver->masters) ? listNodeValue(listFirst(g_pserver->masters)) : NULL); |
| 4055 | |
| 4056 | /* If the instance is a replica we need a connected master |
| 4057 | * in order to fetch the currently selected DB. */ |
| 4058 | if (miFirst && miFirst->master) { |
| 4059 | rsi->repl_stream_db = miFirst->master->db->id; |
| 4060 | return rsi; |
| 4061 | } |
| 4062 | |
| 4063 | /* If we have a cached master we can use it in order to populate the |
| 4064 | * replication selected DB info inside the RDB file: the replica can |
| 4065 | * increment the master_repl_offset only from data arriving from the |
| 4066 | * master, so if we are disconnected the offset in the cached master |
| 4067 | * is valid. */ |
| 4068 | if (miFirst && miFirst->cached_master) { |
| 4069 | rsi->repl_stream_db = miFirst->cached_master->db->id; |
| 4070 | return rsi; |
| 4071 | } |
| 4072 | return NULL; |
| 4073 | } |
no test coverage detected