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

Function sentinelSendSlaveOf

src/sentinel.cpp:4521–4586  ·  view source on GitHub ↗

Send SLAVEOF to the specified instance, always followed by a * CONFIG REWRITE command in order to store the new configuration on disk * when possible (that is, if the Redis instance is recent enough to support * config rewriting, and if the server was started with a configuration file). * * If Host is NULL the function sends "SLAVEOF NO ONE". * * The command returns C_OK if the SLAVEOF comm

Source from the content-addressed store, hash-verified

4519 * (later) delivery otherwise C_ERR. The command replies are just
4520 * discarded. */
4521int sentinelSendSlaveOf(sentinelRedisInstance *ri, const sentinelAddr *addr) {
4522 char portstr[32];
4523 const char *host;
4524 int retval;
4525
4526 /* If host is NULL we send SLAVEOF NO ONE that will turn the instance
4527 * into a master. */
4528 if (!addr) {
4529 host = "NO";
4530 memcpy(portstr,"ONE",4);
4531 } else {
4532 host = announceSentinelAddr(addr);
4533 ll2string(portstr,sizeof(portstr),addr->port);
4534 }
4535
4536 /* In order to send SLAVEOF in a safe way, we send a transaction performing
4537 * the following tasks:
4538 * 1) Reconfigure the instance according to the specified host/port params.
4539 * 2) Rewrite the configuration.
4540 * 3) Disconnect all clients (but this one sending the command) in order
4541 * to trigger the ask-master-on-reconnection protocol for connected
4542 * clients.
4543 *
4544 * Note that we don't check the replies returned by commands, since we
4545 * will observe instead the effects in the next INFO output. */
4546 retval = redisAsyncCommand(ri->link->cc,
4547 sentinelDiscardReplyCallback, ri, "%s",
4548 sentinelInstanceMapCommand(ri,"MULTI"));
4549 if (retval == C_ERR) return retval;
4550 ri->link->pending_commands++;
4551
4552 retval = redisAsyncCommand(ri->link->cc,
4553 sentinelDiscardReplyCallback, ri, "%s %s %s",
4554 sentinelInstanceMapCommand(ri,"SLAVEOF"),
4555 host, portstr);
4556 if (retval == C_ERR) return retval;
4557 ri->link->pending_commands++;
4558
4559 retval = redisAsyncCommand(ri->link->cc,
4560 sentinelDiscardReplyCallback, ri, "%s REWRITE",
4561 sentinelInstanceMapCommand(ri,"CONFIG"));
4562 if (retval == C_ERR) return retval;
4563 ri->link->pending_commands++;
4564
4565 /* CLIENT KILL TYPE <type> is only supported starting from Redis 2.8.12,
4566 * however sending it to an instance not understanding this command is not
4567 * an issue because CLIENT is variadic command, so Redis will not
4568 * recognized as a syntax error, and the transaction will not fail (but
4569 * only the unsupported command will fail). */
4570 for (int type = 0; type < 2; type++) {
4571 retval = redisAsyncCommand(ri->link->cc,
4572 sentinelDiscardReplyCallback, ri, "%s KILL TYPE %s",
4573 sentinelInstanceMapCommand(ri,"CLIENT"),
4574 type == 0 ? "normal" : "pubsub");
4575 if (retval == C_ERR) return retval;
4576 ri->link->pending_commands++;
4577 }
4578

Calls 4

announceSentinelAddrFunction · 0.85
ll2stringFunction · 0.85
redisAsyncCommandFunction · 0.85

Tested by

no test coverage detected