MCPcopy Create free account
hub / github.com/F-Stack/f-stack / replicationUnsetMaster

Function replicationUnsetMaster

app/redis-6.2.6/src/replication.c:2661–2719  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

2659
2660/* Cancel replication, setting the instance as a master itself. */
2661void replicationUnsetMaster(void) {
2662 if (server.masterhost == NULL) return; /* Nothing to do. */
2663
2664 /* Fire the master link modules event. */
2665 if (server.repl_state == REPL_STATE_CONNECTED)
2666 moduleFireServerEvent(REDISMODULE_EVENT_MASTER_LINK_CHANGE,
2667 REDISMODULE_SUBEVENT_MASTER_LINK_DOWN,
2668 NULL);
2669
2670 /* Clear masterhost first, since the freeClient calls
2671 * replicationHandleMasterDisconnection which can attempt to re-connect. */
2672 sdsfree(server.masterhost);
2673 server.masterhost = NULL;
2674 if (server.master) freeClient(server.master);
2675 replicationDiscardCachedMaster();
2676 cancelReplicationHandshake(0);
2677 /* When a slave is turned into a master, the current replication ID
2678 * (that was inherited from the master at synchronization time) is
2679 * used as secondary ID up to the current offset, and a new replication
2680 * ID is created to continue with a new replication history.
2681 *
2682 * NOTE: this function MUST be called after we call
2683 * freeClient(server.master), since there we adjust the replication
2684 * offset trimming the final PINGs. See Github issue #7320. */
2685 shiftReplicationId();
2686 /* Disconnecting all the slaves is required: we need to inform slaves
2687 * of the replication ID change (see shiftReplicationId() call). However
2688 * the slaves will be able to partially resync with us, so it will be
2689 * a very fast reconnection. */
2690 disconnectSlaves();
2691 server.repl_state = REPL_STATE_NONE;
2692
2693 /* We need to make sure the new master will start the replication stream
2694 * with a SELECT statement. This is forced after a full resync, but
2695 * with PSYNC version 2, there is no need for full resync after a
2696 * master switch. */
2697 server.slaveseldb = -1;
2698
2699 /* Update oom_score_adj */
2700 setOOMScoreAdj(-1);
2701
2702 /* Once we turn from slave to master, we consider the starting time without
2703 * slaves (that is used to count the replication backlog time to live) as
2704 * starting from now. Otherwise the backlog will be freed after a
2705 * failover if slaves do not connect immediately. */
2706 server.repl_no_slaves_since = server.unixtime;
2707
2708 /* Reset down time so it'll be ready for when we turn into replica again. */
2709 server.repl_down_since = 0;
2710
2711 /* Fire the role change modules event. */
2712 moduleFireServerEvent(REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED,
2713 REDISMODULE_EVENT_REPLROLECHANGED_NOW_MASTER,
2714 NULL);
2715
2716 /* Restart the AOF subsystem in case we shut it down during a sync when
2717 * we were still a slave. */
2718 if (server.aof_enabled && server.aof_state == AOF_OFF) restartAOFAfterSYNC();

Callers 5

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

Calls 9

moduleFireServerEventFunction · 0.85
sdsfreeFunction · 0.85
shiftReplicationIdFunction · 0.85
disconnectSlavesFunction · 0.85
setOOMScoreAdjFunction · 0.85
restartAOFAfterSYNCFunction · 0.85
freeClientFunction · 0.70

Tested by

no test coverage detected