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

Function syncCommand

src/replication.cpp:1370–1555  ·  view source on GitHub ↗

SYNC and PSYNC command implementation. */

Source from the content-addressed store, hash-verified

1368
1369/* SYNC and PSYNC command implementation. */
1370void syncCommand(client *c) {
1371 /* ignore SYNC if already replica or in monitor mode */
1372 if (c->flags & CLIENT_SLAVE) return;
1373
1374 /* Check if this is a failover request to a replica with the same replid and
1375 * become a master if so. */
1376 if (c->argc > 3 && !strcasecmp(szFromObj(c->argv[0]),"psync") &&
1377 !strcasecmp(szFromObj(c->argv[3]),"failover"))
1378 {
1379 serverLog(LL_WARNING, "Failover request received for replid %s.",
1380 (unsigned char *)szFromObj(c->argv[1]));
1381 if (!listLength(g_pserver->masters)) {
1382 addReplyError(c, "PSYNC FAILOVER can't be sent to a master.");
1383 return;
1384 }
1385 if (listLength(g_pserver->masters) > 1) {
1386 addReplyError(c, "PSYNC FAILOVER can't be used with multi-master.");
1387 return;
1388 }
1389
1390 if (!strcasecmp(szFromObj(c->argv[1]),g_pserver->replid)) {
1391 replicationUnsetMaster((redisMaster*)listNodeValue(listFirst(g_pserver->masters)));
1392 sds client = catClientInfoString(sdsempty(),c);
1393 serverLog(LL_NOTICE,
1394 "MASTER MODE enabled (failover request from '%s')",client);
1395 sdsfree(client);
1396 } else {
1397 addReplyError(c, "PSYNC FAILOVER replid must match my replid.");
1398 return;
1399 }
1400 }
1401
1402 /* Don't let replicas sync with us while we're failing over */
1403 if (g_pserver->failover_state != NO_FAILOVER) {
1404 addReplyError(c,"-NOMASTERLINK Can't SYNC while failing over");
1405 return;
1406 }
1407
1408 /* Refuse SYNC requests if we are a slave but the link with our master
1409 * is not ok... */
1410 if (!g_pserver->fActiveReplica) {
1411 if (FAnyDisconnectedMasters()) {
1412 addReplyError(c,"-NOMASTERLINK Can't SYNC while not connected with my master");
1413 return;
1414 }
1415 }
1416
1417 /* SYNC can't be issued when the server has pending data to send to
1418 * the client about already issued commands. We need a fresh reply
1419 * buffer registering the differences between the BGSAVE and the current
1420 * dataset, so that we can copy to other slaves if needed. */
1421 if (clientHasPendingReplies(c)) {
1422 addReplyError(c,"SYNC and PSYNC are invalid with pending output");
1423 return;
1424 }
1425
1426 serverLog(LL_NOTICE,"Replica %s asks for synchronization",
1427 replicationGetSlaveName(c));

Callers

nothing calls this directly

Calls 15

szFromObjFunction · 0.85
serverLogFunction · 0.85
addReplyErrorFunction · 0.85
replicationUnsetMasterFunction · 0.85
catClientInfoStringFunction · 0.85
sdsemptyFunction · 0.85
sdsfreeFunction · 0.85
FAnyDisconnectedMastersFunction · 0.85
clientHasPendingRepliesFunction · 0.85
replicationGetSlaveNameFunction · 0.85
ptrFromObjFunction · 0.85

Tested by

no test coverage detected