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

Function replicaofCommand

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

Source from the content-addressed store, hash-verified

2744}
2745
2746void replicaofCommand(client *c) {
2747 /* SLAVEOF is not allowed in cluster mode as replication is automatically
2748 * configured using the current address of the master node. */
2749 if (server.cluster_enabled) {
2750 addReplyError(c,"REPLICAOF not allowed in cluster mode.");
2751 return;
2752 }
2753
2754 if (server.failover_state != NO_FAILOVER) {
2755 addReplyError(c,"REPLICAOF not allowed while failing over.");
2756 return;
2757 }
2758
2759 /* The special host/port combination "NO" "ONE" turns the instance
2760 * into a master. Otherwise the new master address is set. */
2761 if (!strcasecmp(c->argv[1]->ptr,"no") &&
2762 !strcasecmp(c->argv[2]->ptr,"one")) {
2763 if (server.masterhost) {
2764 replicationUnsetMaster();
2765 sds client = catClientInfoString(sdsempty(),c);
2766 serverLog(LL_NOTICE,"MASTER MODE enabled (user request from '%s')",
2767 client);
2768 sdsfree(client);
2769 }
2770 } else {
2771 long port;
2772
2773 if (c->flags & CLIENT_SLAVE)
2774 {
2775 /* If a client is already a replica they cannot run this command,
2776 * because it involves flushing all replicas (including this
2777 * client) */
2778 addReplyError(c, "Command is not valid when client is a replica.");
2779 return;
2780 }
2781
2782 if ((getLongFromObjectOrReply(c, c->argv[2], &port, NULL) != C_OK))
2783 return;
2784
2785 /* Check if we are already attached to the specified master */
2786 if (server.masterhost && !strcasecmp(server.masterhost,c->argv[1]->ptr)
2787 && server.masterport == port) {
2788 serverLog(LL_NOTICE,"REPLICAOF would result into synchronization "
2789 "with the master we are already connected "
2790 "with. No operation performed.");
2791 addReplySds(c,sdsnew("+OK Already connected to specified "
2792 "master\r\n"));
2793 return;
2794 }
2795 /* There was no previous master or the user specified a different one,
2796 * we can continue. */
2797 replicationSetMaster(c->argv[1]->ptr, port);
2798 sds client = catClientInfoString(sdsempty(),c);
2799 serverLog(LL_NOTICE,"REPLICAOF %s:%d enabled (user request from '%s')",
2800 server.masterhost, server.masterport, client);
2801 sdsfree(client);
2802 }
2803 addReply(c,shared.ok);

Callers

nothing calls this directly

Calls 11

addReplyErrorFunction · 0.85
strcasecmpFunction · 0.85
replicationUnsetMasterFunction · 0.85
catClientInfoStringFunction · 0.85
sdsemptyFunction · 0.85
sdsfreeFunction · 0.85
getLongFromObjectOrReplyFunction · 0.85
addReplySdsFunction · 0.85
sdsnewFunction · 0.85
replicationSetMasterFunction · 0.85
addReplyFunction · 0.85

Tested by

no test coverage detected