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

Function replicationSetMaster

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

Set replication to the specified master address and port. */

Source from the content-addressed store, hash-verified

2611
2612/* Set replication to the specified master address and port. */
2613void replicationSetMaster(char *ip, int port) {
2614 int was_master = server.masterhost == NULL;
2615
2616 sdsfree(server.masterhost);
2617 server.masterhost = NULL;
2618 if (server.master) {
2619 freeClient(server.master);
2620 }
2621 disconnectAllBlockedClients(); /* Clients blocked in master, now slave. */
2622
2623 /* Setting masterhost only after the call to freeClient since it calls
2624 * replicationHandleMasterDisconnection which can trigger a re-connect
2625 * directly from within that call. */
2626 server.masterhost = sdsnew(ip);
2627 server.masterport = port;
2628
2629 /* Update oom_score_adj */
2630 setOOMScoreAdj(-1);
2631
2632 /* Force our slaves to resync with us as well. They may hopefully be able
2633 * to partially resync with us, but we can notify the replid change. */
2634 disconnectSlaves();
2635 cancelReplicationHandshake(0);
2636 /* Before destroying our master state, create a cached master using
2637 * our own parameters, to later PSYNC with the new master. */
2638 if (was_master) {
2639 replicationDiscardCachedMaster();
2640 replicationCacheMasterUsingMyself();
2641 }
2642
2643 /* Fire the role change modules event. */
2644 moduleFireServerEvent(REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED,
2645 REDISMODULE_EVENT_REPLROLECHANGED_NOW_REPLICA,
2646 NULL);
2647
2648 /* Fire the master link modules event. */
2649 if (server.repl_state == REPL_STATE_CONNECTED)
2650 moduleFireServerEvent(REDISMODULE_EVENT_MASTER_LINK_CHANGE,
2651 REDISMODULE_SUBEVENT_MASTER_LINK_DOWN,
2652 NULL);
2653
2654 server.repl_state = REPL_STATE_CONNECT;
2655 serverLog(LL_NOTICE,"Connecting to MASTER %s:%d",
2656 server.masterhost, server.masterport);
2657 connectWithMaster();
2658}
2659
2660/* Cancel replication, setting the instance as a master itself. */
2661void replicationUnsetMaster(void) {

Callers 5

replicaofCommandFunction · 0.85
updateFailoverStatusFunction · 0.85
clusterCronFunction · 0.85
clusterSetMasterFunction · 0.85

Calls 11

sdsfreeFunction · 0.85
sdsnewFunction · 0.85
setOOMScoreAdjFunction · 0.85
disconnectSlavesFunction · 0.85
moduleFireServerEventFunction · 0.85
connectWithMasterFunction · 0.85
freeClientFunction · 0.70

Tested by

no test coverage detected