summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2012-08-30 17:57:02 +0200
committerantirez <antirez@gmail.com>2012-08-30 18:01:52 +0200
commit6276434ad22ded9f5bf9997137c311e447154908 (patch)
treeafeb87bf5e32e56ee4e124611c4588386c59afba
parent58186b9dcf88cd2132ac0ff4bd3a12304819cc4e (diff)
downloadredis-6276434ad22ded9f5bf9997137c311e447154908.tar.gz
Sentinel: do not crash against slaves not publishing the runid.
Older versions of Redis (before 2.4.17) don't publish the runid field in INFO. This commit makes Sentinel able to handle that without crashing.
-rw-r--r--src/sentinel.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/sentinel.c b/src/sentinel.c
index 11df7335c..659820f67 100644
--- a/src/sentinel.c
+++ b/src/sentinel.c
@@ -2521,9 +2521,21 @@ void sentinelStartFailoverIfNeeded(sentinelRedisInstance *master) {
int compareSlavesForPromotion(const void *a, const void *b) {
sentinelRedisInstance **sa = (sentinelRedisInstance **)a,
**sb = (sentinelRedisInstance **)b;
+ char *sa_runid, *sb_runid;
+
if ((*sa)->slave_priority != (*sb)->slave_priority)
return (*sa)->slave_priority - (*sb)->slave_priority;
- return strcasecmp((*sa)->runid,(*sb)->runid);
+
+ /* If priority is the same, select the slave with that has the
+ * lexicographically smaller runid. Note that we try to handle runid
+ * == NULL as there are old Redis versions that don't publish runid in
+ * INFO. A NULL runid is considered bigger than any other runid. */
+ sa_runid = (*sa)->runid;
+ sb_runid = (*sb)->runid;
+ if (sa_runid == NULL && sb_runid == NULL) return 0;
+ else if (sa_runid == NULL) return 1; /* a > b */
+ else if (sb_runid == NULL) return -1; /* a < b */
+ return strcasecmp(sa_runid, sb_runid);
}
sentinelRedisInstance *sentinelSelectSlave(sentinelRedisInstance *master) {