MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / roleCommand

Function roleCommand

src/replication.cpp:4215–4282  ·  view source on GitHub ↗

ROLE command: provide information about the role of the instance * (master or replica) and additional information related to replication * in an easy to process format. */

Source from the content-addressed store, hash-verified

4213 * (master or replica) and additional information related to replication
4214 * in an easy to process format. */
4215void roleCommand(client *c) {
4216 if (listLength(g_pserver->masters) == 0) {
4217 listIter li;
4218 listNode *ln;
4219 void *mbcount;
4220 int slaves = 0;
4221
4222 addReplyArrayLen(c,3);
4223 addReplyBulkCBuffer(c,"master",6);
4224 addReplyLongLong(c,g_pserver->master_repl_offset);
4225 mbcount = addReplyDeferredLen(c);
4226 listRewind(g_pserver->slaves,&li);
4227 while((ln = listNext(&li))) {
4228 client *replica = (client*)ln->value;
4229 char ip[NET_IP_STR_LEN], *slaveaddr = replica->slave_addr;
4230
4231 if (!slaveaddr) {
4232 if (connPeerToString(replica->conn,ip,sizeof(ip),NULL) == -1)
4233 continue;
4234 slaveaddr = ip;
4235 }
4236 if (replica->replstate != SLAVE_STATE_ONLINE) continue;
4237 addReplyArrayLen(c,3);
4238 addReplyBulkCString(c,slaveaddr);
4239 addReplyBulkLongLong(c,replica->slave_listening_port);
4240 addReplyBulkLongLong(c,replica->repl_ack_off);
4241 slaves++;
4242 }
4243 setDeferredArrayLen(c,mbcount,slaves);
4244 } else {
4245 listIter li;
4246 listNode *ln;
4247 listRewind(g_pserver->masters, &li);
4248
4249 if (listLength(g_pserver->masters) > 1)
4250 addReplyArrayLen(c,listLength(g_pserver->masters));
4251 while ((ln = listNext(&li)))
4252 {
4253 redisMaster *mi = (redisMaster*)listNodeValue(ln);
4254 std::unique_lock<fastlock> lock;
4255 if (mi->master != nullptr)
4256 lock = std::unique_lock<fastlock>(mi->master->lock);
4257
4258 const char *slavestate = NULL;
4259 addReplyArrayLen(c,5);
4260 if (g_pserver->fActiveReplica)
4261 addReplyBulkCBuffer(c,"active-replica",14);
4262 else
4263 addReplyBulkCBuffer(c,"slave",5);
4264 addReplyBulkCString(c,mi->masterhost);
4265 addReplyLongLong(c,mi->masterport);
4266 if (slaveIsInHandshakeState(mi)) {
4267 slavestate = "handshake";
4268 } else {
4269 switch(mi->repl_state) {
4270 case REPL_STATE_NONE: slavestate = "none"; break;
4271 case REPL_STATE_CONNECT: slavestate = "connect"; break;
4272 case REPL_STATE_CONNECTING: slavestate = "connecting"; break;

Callers

nothing calls this directly

Calls 11

addReplyArrayLenFunction · 0.85
addReplyBulkCBufferFunction · 0.85
addReplyLongLongFunction · 0.85
addReplyDeferredLenFunction · 0.85
listRewindFunction · 0.85
listNextFunction · 0.85
connPeerToStringFunction · 0.85
addReplyBulkCStringFunction · 0.85
addReplyBulkLongLongFunction · 0.85
setDeferredArrayLenFunction · 0.85
slaveIsInHandshakeStateFunction · 0.85

Tested by

no test coverage detected