Skip to content

Commit 70b80aa

Browse files
committed
Implement replicaof remove as requested in issue Snapchat#192
1 parent e8d4d09 commit 70b80aa

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/replication.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3151,15 +3151,46 @@ void replicaofCommand(client *c) {
31513151
return;
31523152
}
31533153

3154-
/* The special host/port combination "NO" "ONE" turns the instance
3155-
* into a master. Otherwise the new master address is set. */
3156-
if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),"no") &&
3154+
if (c->argc > 3) {
3155+
if (c->argc != 4) {
3156+
addReplyError(c, "Invalid arguments");
3157+
return;
3158+
}
3159+
if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),"remove")) {
3160+
listIter li;
3161+
listNode *ln;
3162+
bool fRemoved = false;
3163+
long port;
3164+
string2l(szFromObj(c->argv[3]), sdslen(szFromObj(c->argv[3])), &port);
3165+
LRestart:
3166+
listRewind(g_pserver->masters, &li);
3167+
while ((ln = listNext(&li))) {
3168+
redisMaster *mi = (redisMaster*)listNodeValue(ln);
3169+
if (mi->masterport != port)
3170+
continue;
3171+
if (sdscmp(szFromObj(c->argv[2]), mi->masterhost) == 0) {
3172+
replicationUnsetMaster(mi);
3173+
fRemoved = true;
3174+
goto LRestart;
3175+
}
3176+
}
3177+
if (!fRemoved) {
3178+
addReplyError(c, "Master not found");
3179+
return;
3180+
} else if (listLength(g_pserver->masters) == 0) {
3181+
goto LLogNoMaster;
3182+
}
3183+
}
3184+
} else if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),"no") &&
31573185
!strcasecmp((const char*)ptrFromObj(c->argv[2]),"one")) {
3186+
/* The special host/port combination "NO" "ONE" turns the instance
3187+
* into a master. Otherwise the new master address is set. */
31583188
if (listLength(g_pserver->masters)) {
31593189
while (listLength(g_pserver->masters))
31603190
{
31613191
replicationUnsetMaster((redisMaster*)listNodeValue(listFirst(g_pserver->masters)));
31623192
}
3193+
LLogNoMaster:
31633194
sds client = catClientInfoString(sdsempty(),c);
31643195
serverLog(LL_NOTICE,"MASTER MODE enabled (user request from '%s')",
31653196
client);

src/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ struct redisCommand redisCommandTable[] = {
768768
"admin no-script ok-stale",
769769
0,NULL,0,0,0,0,0,0},
770770

771-
{"replicaof",replicaofCommand,3,
771+
{"replicaof",replicaofCommand,-3,
772772
"admin no-script ok-stale",
773773
0,NULL,0,0,0,0,0,0},
774774

0 commit comments

Comments
 (0)