From 1f9728cb20f7eaed3f613e7b8c49292edd1edc34 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 19 Nov 2013 12:34:39 +0100 Subject: [PATCH] Sentinel: failover script execution fixed. --- src/sentinel.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/sentinel.c b/src/sentinel.c index 7319e731e..4b40e7a20 100644 --- a/src/sentinel.c +++ b/src/sentinel.c @@ -437,6 +437,16 @@ sentinelAddr *createSentinelAddr(char *hostname, int port) { return sa; } +/* Return a duplicate of the source address. */ +sentinelAddr *dupSentinelAddr(sentinelAddr *src) { + sentinelAddr *sa; + + sa = zmalloc(sizeof(*sa)); + sa->ip = sdsnew(src->ip); + sa->port = src->port; + return sa; +} + /* Free a Sentinel address. Can't fail. */ void releaseSentinelAddr(sentinelAddr *sa) { sdsfree(sa->ip); @@ -783,15 +793,13 @@ void sentinelPendingScriptsCommand(redisClient *c) { * * * - * It is called every time a failover starts, ends, or is aborted. + * It is called every time a failover is performed. * - * is "start", "end" or "abort". + * is currently always "failover". * is either "leader" or "observer". * * from/to fields are respectively master -> promoted slave addresses for - * "start" and "end", or the reverse (promoted slave -> master) in case of - * "abort". - */ + * "start" and "end". */ void sentinelCallClientReconfScript(sentinelRedisInstance *master, int role, char *state, sentinelAddr *from, sentinelAddr *to) { char fromport[32], toport[32]; @@ -2009,13 +2017,21 @@ void sentinelReceiveHelloMessages(redisAsyncContext *c, void *reply, void *privd if (master_port != msgmaster->addr->port || !strcmp(msgmaster->addr->ip, token[5])) { + sentinelAddr *old_addr; + sentinelEvent(REDIS_WARNING,"+switch-master", msgmaster,"%s %s %d %s %d", msgmaster->name, msgmaster->addr->ip, msgmaster->addr->port, token[5], master_port); + + old_addr = dupSentinelAddr(msgmaster->addr); sentinelResetMasterAndChangeAddress(msgmaster, token[5], master_port); + sentinelCallClientReconfScript(msgmaster, + SENTINEL_OBSERVER,"start", + old_addr,msgmaster->addr); + releaseSentinelAddr(old_addr); } } } @@ -3038,13 +3054,9 @@ void sentinelFailoverDetectEnd(sentinelRedisInstance *master) { } if (not_reconfigured == 0) { - int role = SENTINEL_LEADER; - sentinelEvent(REDIS_WARNING,"+failover-end",master,"%@"); master->failover_state = SENTINEL_FAILOVER_STATE_UPDATE_CONFIG; master->failover_state_change_time = mstime(); - sentinelCallClientReconfScript(master,role,"end",master->addr, - master->promoted_slave->addr); } /* If I'm the leader it is a good idea to send a best effort SLAVEOF