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

Function clusterGetSlaveRank

src/cluster.cpp:3067–3082  ·  view source on GitHub ↗

This function returns the "rank" of this instance, a slave, in the context * of its master-slaves ring. The rank of the slave is given by the number of * other slaves for the same master that have a better replication offset * compared to the local one (better means, greater, so they claim more data). * * A slave with rank 0 is the one with the greatest (most up to date) * replication offset

Source from the content-addressed store, hash-verified

3065 * get voted and replace a failing master. Slaves with better replication
3066 * offsets are more likely to win. */
3067int clusterGetSlaveRank(void) {
3068 long long myoffset;
3069 int j, rank = 0;
3070 clusterNode *master;
3071
3072 serverAssert(nodeIsSlave(myself));
3073 master = myself->slaveof;
3074 if (master == NULL) return 0; /* Never called by slaves without master. */
3075
3076 myoffset = replicationGetSlaveOffset(getFirstMaster());
3077 for (j = 0; j < master->numslaves; j++)
3078 if (master->slaves[j] != myself &&
3079 !nodeCantFailover(master->slaves[j]) &&
3080 master->slaves[j]->repl_offset > myoffset) rank++;
3081 return rank;
3082}
3083
3084/* This function is called by clusterHandleSlaveFailover() in order to
3085 * let the slave log why it is not able to failover. Sometimes there are

Callers 1

Calls 2

getFirstMasterFunction · 0.85

Tested by

no test coverage detected