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

Function replicationCacheMaster

src/replication.cpp:4322–4370  ·  view source on GitHub ↗

This function is called by freeClient() in order to cache the master * client structure instead of destroying it. freeClient() will return * ASAP after this function returns, so every action needed to avoid problems * with a client that is really "suspended" has to be done by this function. * * The other functions that will deal with the cached master are: * * replicationDiscardCachedMaster

Source from the content-addressed store, hash-verified

4320 * handshake in order to reactivate the cached master.
4321 */
4322void replicationCacheMaster(redisMaster *mi, client *c) {
4323 serverAssert(mi->master == c);
4324 serverAssert(mi->master != NULL && mi->cached_master == NULL);
4325 serverLog(LL_NOTICE,"Caching the disconnected master state.");
4326 AssertCorrectThread(c);
4327 std::lock_guard<decltype(c->lock)> clientlock(c->lock);
4328
4329 /* Unlink the client from the server structures. */
4330 unlinkClient(c);
4331
4332 /* Reset the master client so that's ready to accept new commands:
4333 * we want to discard te non processed query buffers and non processed
4334 * offsets, including pending transactions, already populated arguments,
4335 * pending outputs to the master. */
4336 sdsclear(mi->master->querybuf);
4337 if (!mi->master->vecqueuedcmd.empty()) {
4338 mi->master->vecqueuedcmd.clear();
4339 }
4340 mi->master->multibulklen = 0;
4341 sdsclear(mi->master->pending_querybuf);
4342 mi->master->read_reploff = mi->master->reploff;
4343 if (c->flags & CLIENT_MULTI) discardTransaction(c);
4344 listEmpty(c->reply);
4345 c->sentlen = 0;
4346 c->reply_bytes = 0;
4347 c->bufpos = 0;
4348 resetClient(c);
4349
4350 /* Save the master. g_pserver->master will be set to null later by
4351 * replicationHandleMasterDisconnection(). */
4352 mi->cached_master = mi->master;
4353
4354 /* Invalidate the Peer ID cache. */
4355 if (c->peerid) {
4356 sdsfree(c->peerid);
4357 c->peerid = NULL;
4358 }
4359 /* Invalidate the Sock Name cache. */
4360 if (c->sockname) {
4361 sdsfree(c->sockname);
4362 c->sockname = NULL;
4363 }
4364
4365 /* Caching the master happens instead of the actual freeClient() call,
4366 * so make sure to adjust the replication state. This function will
4367 * also set g_pserver->master to NULL. */
4368 replicationHandleMasterDisconnection(mi);
4369 serverAssert(mi->master == nullptr);
4370}
4371
4372/* This function is called when a master is turend into a slave, in order to
4373 * create from scratch a cached master for the new client, that will allow

Callers 2

freeClientFunction · 0.85

Calls 10

serverLogFunction · 0.85
unlinkClientFunction · 0.85
sdsclearFunction · 0.85
discardTransactionFunction · 0.85
listEmptyFunction · 0.85
sdsfreeFunction · 0.85
resetClientFunction · 0.70
emptyMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected