MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / replicationUnsetMaster

Function replicationUnsetMaster

src/replication.cpp:4017–4083  ·  view source on GitHub ↗

Cancel replication, setting the instance as a master itself. */

Source from the content-addressed store, hash-verified

4015
4016/* Cancel replication, setting the instance as a master itself. */
4017void replicationUnsetMaster(redisMaster *mi) {
4018 if (mi->masterhost == NULL) return; /* Nothing to do. */
4019
4020 /* Fire the master link modules event. */
4021 if (mi->repl_state == REPL_STATE_CONNECTED)
4022 moduleFireServerEvent(REDISMODULE_EVENT_MASTER_LINK_CHANGE,
4023 REDISMODULE_SUBEVENT_MASTER_LINK_DOWN,
4024 NULL);
4025
4026 /* Clear masterhost first, since the freeClient calls
4027 * replicationHandleMasterDisconnection which can attempt to re-connect. */
4028 sdsfree(mi->masterhost);
4029 mi->masterhost = NULL;
4030 disconnectMaster(mi);
4031 replicationDiscardCachedMaster(mi);
4032 cancelReplicationHandshake(mi,false);
4033 /* When a slave is turned into a master, the current replication ID
4034 * (that was inherited from the master at synchronization time) is
4035 * used as secondary ID up to the current offset, and a new replication
4036 * ID is created to continue with a new replication history.
4037 *
4038 * NOTE: this function MUST be called after we call
4039 * freeClient(g_pserver->master), since there we adjust the replication
4040 * offset trimming the final PINGs. See Github issue #7320. */
4041 shiftReplicationId();
4042 /* Disconnecting all the slaves is required: we need to inform slaves
4043 * of the replication ID change (see shiftReplicationId() call). However
4044 * the slaves will be able to partially resync with us, so it will be
4045 * a very fast reconnection. */
4046 if (!g_pserver->fActiveReplica)
4047 disconnectSlaves();
4048 mi->repl_state = REPL_STATE_NONE;
4049
4050 /* We need to make sure the new master will start the replication stream
4051 * with a SELECT statement. This is forced after a full resync, but
4052 * with PSYNC version 2, there is no need for full resync after a
4053 * master switch. */
4054 g_pserver->replicaseldb = -1;
4055
4056 /* Once we turn from replica to master, we consider the starting time without
4057 * slaves (that is used to count the replication backlog time to live) as
4058 * starting from now. Otherwise the backlog will be freed after a
4059 * failover if slaves do not connect immediately. */
4060 g_pserver->repl_no_slaves_since = g_pserver->unixtime;
4061
4062 /* Reset down time so it'll be ready for when we turn into replica again. */
4063 mi->repl_down_since = 0;
4064
4065 listNode *ln = listSearchKey(g_pserver->masters, mi);
4066 serverAssert(ln != nullptr);
4067 listDelNode(g_pserver->masters, ln);
4068 freeMasterInfo(mi);
4069
4070 /* Update oom_score_adj */
4071 setOOMScoreAdj(-1);
4072
4073 /* Fire the role change modules event. */
4074 moduleFireServerEvent(REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED,

Callers 6

clusterResetFunction · 0.85
clusterCommandFunction · 0.85
syncCommandFunction · 0.85
replicaofCommandFunction · 0.85
abortFailoverFunction · 0.85

Calls 13

moduleFireServerEventFunction · 0.85
sdsfreeFunction · 0.85
disconnectMasterFunction · 0.85
shiftReplicationIdFunction · 0.85
disconnectSlavesFunction · 0.85
listSearchKeyFunction · 0.85
listDelNodeFunction · 0.85
freeMasterInfoFunction · 0.85
setOOMScoreAdjFunction · 0.85
restartAOFAfterSYNCFunction · 0.85

Tested by

no test coverage detected