Minor changes to PR #2813.

* Function to test for slave handshake renamed slaveIsInHandshakeState.
* Function no longer accepts arguments since it always tests the
  same global state.
* Test for state translated to a range test since defines are guaranteed
  to stay in order in the future.
* Use the new function in the ROLE command implementation as well.
This commit is contained in:
antirez 2015-10-15 09:59:07 +02:00
parent dc03e4c51b
commit 6ef80f4ed2

View File

@ -41,7 +41,6 @@ void replicationDiscardCachedMaster(void);
void replicationResurrectCachedMaster(int newfd);
void replicationSendAck(void);
void putSlaveOnline(redisClient *slave);
int serverInHandshakeState(int repl_state);
/* --------------------------- Utility functions ---------------------------- */
@ -910,6 +909,13 @@ void updateSlavesWaitingBgsave(int bgsaveerr, int type) {
/* ----------------------------------- SLAVE -------------------------------- */
/* Returns 1 if the given replication state is a handshake state,
* 0 otherwise. */
int slaveIsInHandshakeState(void) {
return server.repl_state >= REDIS_REPL_RECEIVE_PONG &&
server.repl_state <= REDIS_REPL_RECEIVE_PSYNC;
}
/* Abort the async download of the bulk dataset while SYNC-ing with master */
void replicationAbortSyncTransfer(void) {
redisAssert(server.repl_state == REDIS_REPL_TRANSFER);
@ -1628,7 +1634,7 @@ void undoConnectWithMaster(void) {
int fd = server.repl_transfer_s;
redisAssert(server.repl_state == REDIS_REPL_CONNECTING ||
serverInHandshakeState(server.repl_state));
slaveIsInHandshakeState());
aeDeleteFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE);
close(fd);
server.repl_transfer_s = -1;
@ -1647,7 +1653,7 @@ int cancelReplicationHandshake(void) {
if (server.repl_state == REDIS_REPL_TRANSFER) {
replicationAbortSyncTransfer();
} else if (server.repl_state == REDIS_REPL_CONNECTING ||
serverInHandshakeState(server.repl_state))
slaveIsInHandshakeState())
{
undoConnectWithMaster();
} else {
@ -1782,42 +1788,23 @@ void roleCommand(redisClient *c) {
addReplyBulkCBuffer(c,"slave",5);
addReplyBulkCString(c,server.masterhost);
addReplyLongLong(c,server.masterport);
switch(server.repl_state) {
case REDIS_REPL_NONE: slavestate = "none"; break;
case REDIS_REPL_CONNECT: slavestate = "connect"; break;
case REDIS_REPL_CONNECTING: slavestate = "connecting"; break;
case REDIS_REPL_RECEIVE_PONG:
case REDIS_REPL_SEND_AUTH:
case REDIS_REPL_RECEIVE_AUTH:
case REDIS_REPL_SEND_PORT:
case REDIS_REPL_RECEIVE_PORT:
case REDIS_REPL_SEND_CAPA:
case REDIS_REPL_RECEIVE_CAPA:
case REDIS_REPL_SEND_PSYNC:
case REDIS_REPL_RECEIVE_PSYNC: slavestate = "handshake"; break;
case REDIS_REPL_TRANSFER: slavestate = "sync"; break;
case REDIS_REPL_CONNECTED: slavestate = "connected"; break;
default: slavestate = "unknown"; break;
if (slaveIsInHandshakeState()) {
slavestate = "handshake";
} else {
switch(server.repl_state) {
case REDIS_REPL_NONE: slavestate = "none"; break;
case REDIS_REPL_CONNECT: slavestate = "connect"; break;
case REDIS_REPL_CONNECTING: slavestate = "connecting"; break;
case REDIS_REPL_TRANSFER: slavestate = "sync"; break;
case REDIS_REPL_CONNECTED: slavestate = "connected"; break;
default: slavestate = "unknown"; break;
}
}
addReplyBulkCString(c,slavestate);
addReplyLongLong(c,server.master ? server.master->reploff : -1);
}
}
/* Returns 1 if the given replication state is a handshake state,
* 0 otherwise. */
int serverInHandshakeState(int repl_state) {
return repl_state == REDIS_REPL_RECEIVE_PONG ||
repl_state == REDIS_REPL_SEND_AUTH ||
repl_state == REDIS_REPL_RECEIVE_AUTH ||
repl_state == REDIS_REPL_SEND_PORT ||
repl_state == REDIS_REPL_RECEIVE_PORT ||
repl_state == REDIS_REPL_SEND_CAPA ||
repl_state == REDIS_REPL_RECEIVE_CAPA ||
repl_state == REDIS_REPL_SEND_PSYNC ||
repl_state == REDIS_REPL_RECEIVE_PSYNC;
}
/* Send a REPLCONF ACK command to the master to inform it about the current
* processed offset. If we are not connected with a master, the command has
* no effects. */
@ -2202,8 +2189,8 @@ void replicationCron(void) {
/* Non blocking connection timeout? */
if (server.masterhost &&
(server.repl_state == REDIS_REPL_CONNECTING ||
serverInHandshakeState(server.repl_state)) &&
(time(NULL)-server.repl_transfer_lastio) > server.repl_timeout)
slaveIsInHandshakeState()) &&
(time(NULL)-server.repl_transfer_lastio) > server.repl_timeout)
{
redisLog(REDIS_WARNING,"Timeout connecting to the MASTER...");
undoConnectWithMaster();