mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-22 16:18:28 -05:00
SLAVEOF command refactored into a proper API.
We now have replicationSetMaster() and replicationUnsetMaster() that can be called in other contexts (for instance Redis Cluster).
This commit is contained in:
parent
0c01088b51
commit
7bead003e2
@ -1096,6 +1096,8 @@ void replicationCron(void);
|
|||||||
void replicationHandleMasterDisconnection(void);
|
void replicationHandleMasterDisconnection(void);
|
||||||
void replicationCacheMaster(redisClient *c);
|
void replicationCacheMaster(redisClient *c);
|
||||||
void resizeReplicationBacklog(long long newsize);
|
void resizeReplicationBacklog(long long newsize);
|
||||||
|
void replicationSetMaster(char *ip, int port);
|
||||||
|
void replicationUnsetMaster(void);
|
||||||
|
|
||||||
/* Generic persistence functions */
|
/* Generic persistence functions */
|
||||||
void startLoading(FILE *fp);
|
void startLoading(FILE *fp);
|
||||||
|
@ -1226,16 +1226,35 @@ int cancelReplicationHandshake(void) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Set replication to the specified master address and port. */
|
||||||
|
void replicationSetMaster(char *ip, int port) {
|
||||||
|
sdsfree(server.masterhost);
|
||||||
|
server.masterhost = sdsdup(ip);
|
||||||
|
server.masterport = port;
|
||||||
|
if (server.master) freeClient(server.master);
|
||||||
|
disconnectSlaves(); /* Force our slaves to resync with us as well. */
|
||||||
|
replicationDiscardCachedMaster(); /* Don't try a PSYNC. */
|
||||||
|
freeReplicationBacklog(); /* Don't allow our chained slaves to PSYNC. */
|
||||||
|
cancelReplicationHandshake();
|
||||||
|
server.repl_state = REDIS_REPL_CONNECT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Cancel replication, setting the instance as a master itself. */
|
||||||
|
void replicationUnsetMaster(void) {
|
||||||
|
if (server.masterhost == NULL) return; /* Nothing to do. */
|
||||||
|
sdsfree(server.masterhost);
|
||||||
|
server.masterhost = NULL;
|
||||||
|
if (server.master) freeClient(server.master);
|
||||||
|
replicationDiscardCachedMaster();
|
||||||
|
cancelReplicationHandshake();
|
||||||
|
server.repl_state = REDIS_REPL_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
void slaveofCommand(redisClient *c) {
|
void slaveofCommand(redisClient *c) {
|
||||||
if (!strcasecmp(c->argv[1]->ptr,"no") &&
|
if (!strcasecmp(c->argv[1]->ptr,"no") &&
|
||||||
!strcasecmp(c->argv[2]->ptr,"one")) {
|
!strcasecmp(c->argv[2]->ptr,"one")) {
|
||||||
if (server.masterhost) {
|
if (server.masterhost) {
|
||||||
sdsfree(server.masterhost);
|
replicationUnsetMaster();
|
||||||
server.masterhost = NULL;
|
|
||||||
if (server.master) freeClient(server.master);
|
|
||||||
replicationDiscardCachedMaster();
|
|
||||||
cancelReplicationHandshake();
|
|
||||||
server.repl_state = REDIS_REPL_NONE;
|
|
||||||
redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)");
|
redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -1253,15 +1272,7 @@ void slaveofCommand(redisClient *c) {
|
|||||||
}
|
}
|
||||||
/* There was no previous master or the user specified a different one,
|
/* There was no previous master or the user specified a different one,
|
||||||
* we can continue. */
|
* we can continue. */
|
||||||
sdsfree(server.masterhost);
|
replicationSetMaster(c->argv[1]->ptr, port);
|
||||||
server.masterhost = sdsdup(c->argv[1]->ptr);
|
|
||||||
server.masterport = port;
|
|
||||||
if (server.master) freeClient(server.master);
|
|
||||||
disconnectSlaves(); /* Force our slaves to resync with us as well. */
|
|
||||||
replicationDiscardCachedMaster(); /* Don't try a PSYNC. */
|
|
||||||
freeReplicationBacklog(); /* Don't allow our chained slaves to PSYNC. */
|
|
||||||
cancelReplicationHandshake();
|
|
||||||
server.repl_state = REDIS_REPL_CONNECT;
|
|
||||||
redisLog(REDIS_NOTICE,"SLAVE OF %s:%d enabled (user request)",
|
redisLog(REDIS_NOTICE,"SLAVE OF %s:%d enabled (user request)",
|
||||||
server.masterhost, server.masterport);
|
server.masterhost, server.masterport);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user