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

Function replicationFeedSlave

src/replication.cpp:470–525  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

468sds catCommandForAofAndActiveReplication(sds buf, struct redisCommand *cmd, robj **argv, int argc);
469
470void replicationFeedSlave(client *replica, int dictid, robj **argv, int argc, bool fSendRaw)
471{
472 char llstr[LONG_STR_SIZE];
473 std::unique_lock<decltype(replica->lock)> lock(replica->lock);
474
475 /* Send SELECT command to every replica if needed. */
476 if (g_pserver->replicaseldb != dictid) {
477 robj *selectcmd;
478
479 /* For a few DBs we have pre-computed SELECT command. */
480 if (dictid >= 0 && dictid < PROTO_SHARED_SELECT_CMDS) {
481 selectcmd = shared.select[dictid];
482 } else {
483 int dictid_len;
484
485 dictid_len = ll2string(llstr,sizeof(llstr),dictid);
486 selectcmd = createObject(OBJ_STRING,
487 sdscatprintf(sdsempty(),
488 "*2\r\n$6\r\nSELECT\r\n$%d\r\n%s\r\n",
489 dictid_len, llstr));
490 }
491
492 /* Add the SELECT command into the backlog. */
493 /* We don't do this for advanced replication because this will be done later when it adds the whole RREPLAY command */
494 if (g_pserver->repl_backlog && fSendRaw) feedReplicationBacklogWithObject(selectcmd);
495
496 /* Send it to slaves */
497 addReply(replica,selectcmd);
498
499 if (dictid < 0 || dictid >= PROTO_SHARED_SELECT_CMDS)
500 decrRefCount(selectcmd);
501 }
502 g_pserver->replicaseldb = dictid;
503
504 /* Feed slaves that are waiting for the initial SYNC (so these commands
505 * are queued in the output buffer until the initial SYNC completes),
506 * or are already in sync with the master. */
507
508 if (fSendRaw)
509 {
510 /* Add the multi bulk length. */
511 addReplyArrayLen(replica,argc);
512
513 /* Finally any additional argument that was not stored inside the
514 * static buffer if any (from j to argc). */
515 for (int j = 0; j < argc; j++)
516 addReplyBulk(replica,argv[j]);
517 }
518 else
519 {
520 struct redisCommand *cmd = lookupCommand(szFromObj(argv[0]));
521 sds buf = catCommandForAofAndActiveReplication(sdsempty(), cmd, argv, argc);
522 addReplyProto(replica, buf, sdslen(buf));
523 sdsfree(buf);
524 }
525}
526
527static int writeProtoNum(char *dst, const size_t cchdst, long long num)

Callers 1

propagateMasterStaleKeysFunction · 0.85

Calls 15

ll2stringFunction · 0.85
createObjectFunction · 0.85
sdscatprintfFunction · 0.85
sdsemptyFunction · 0.85
addReplyFunction · 0.85
decrRefCountFunction · 0.85
addReplyArrayLenFunction · 0.85
addReplyBulkFunction · 0.85
lookupCommandFunction · 0.85
szFromObjFunction · 0.85

Tested by

no test coverage detected