From 49fecbe1d40e19353756fd5a8e2a0a8bc8e37092 Mon Sep 17 00:00:00 2001 From: John Sully Date: Tue, 9 Jun 2020 12:43:58 -0400 Subject: [PATCH] Implement replicaof remove as requested in issue #192 Former-commit-id: 70b80aa5fad6c2191c2142ce49626b81d0950fa8 --- src/replication.cpp | 37 ++++++++++++++++++++++++++++++++++--- src/server.cpp | 2 +- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/replication.cpp b/src/replication.cpp index dae37fc72..768a53f0c 100644 --- a/src/replication.cpp +++ b/src/replication.cpp @@ -3151,15 +3151,46 @@ void replicaofCommand(client *c) { return; } - /* The special host/port combination "NO" "ONE" turns the instance - * into a master. Otherwise the new master address is set. */ - if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),"no") && + if (c->argc > 3) { + if (c->argc != 4) { + addReplyError(c, "Invalid arguments"); + return; + } + if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),"remove")) { + listIter li; + listNode *ln; + bool fRemoved = false; + long port; + string2l(szFromObj(c->argv[3]), sdslen(szFromObj(c->argv[3])), &port); + LRestart: + listRewind(g_pserver->masters, &li); + while ((ln = listNext(&li))) { + redisMaster *mi = (redisMaster*)listNodeValue(ln); + if (mi->masterport != port) + continue; + if (sdscmp(szFromObj(c->argv[2]), mi->masterhost) == 0) { + replicationUnsetMaster(mi); + fRemoved = true; + goto LRestart; + } + } + if (!fRemoved) { + addReplyError(c, "Master not found"); + return; + } else if (listLength(g_pserver->masters) == 0) { + goto LLogNoMaster; + } + } + } else if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),"no") && !strcasecmp((const char*)ptrFromObj(c->argv[2]),"one")) { + /* The special host/port combination "NO" "ONE" turns the instance + * into a master. Otherwise the new master address is set. */ if (listLength(g_pserver->masters)) { while (listLength(g_pserver->masters)) { replicationUnsetMaster((redisMaster*)listNodeValue(listFirst(g_pserver->masters))); } + LLogNoMaster: sds client = catClientInfoString(sdsempty(),c); serverLog(LL_NOTICE,"MASTER MODE enabled (user request from '%s')", client); diff --git a/src/server.cpp b/src/server.cpp index 4fa6ce725..c197df741 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -768,7 +768,7 @@ struct redisCommand redisCommandTable[] = { "admin no-script ok-stale", 0,NULL,0,0,0,0,0,0}, - {"replicaof",replicaofCommand,3, + {"replicaof",replicaofCommand,-3, "admin no-script ok-stale", 0,NULL,0,0,0,0,0,0},