Sentinel: don't detect duplicated Sentinels, just address switch

Since with a previous commit Sentinels now persist their unique ID, we
no longer need to detect duplicated Sentinels and re-add them. We remove
and re-add back using different events only in the case of address
switch of the same Sentinel, without generating a new +sentinel event.
This commit is contained in:
antirez 2015-05-07 10:03:40 +02:00
parent 794fc4c9a8
commit a0cd75cd1b

View File

@ -1068,35 +1068,29 @@ const char *sentinelRedisInstanceTypeStr(sentinelRedisInstance *ri) {
else return "unknown"; else return "unknown";
} }
/* This function removes all the instances found in the dictionary of /* This function remove the Sentinel with the specified ID from the
* sentinels in the specified 'master', having either: * specified master.
* *
* 1) The same ip/port as specified. * If "runid" is NULL the function returns ASAP.
* 2) The same runid.
* *
* "1" and "2" don't need to verify at the same time, just one is enough. * This function is useful because on Sentinels address switch, we want to
* If "runid" is NULL it is not checked. * remove our old entry and add a new one for the same ID but with the new
* Similarly if "ip" is NULL it is not checked. * address.
* *
* This function is useful because every time we add a new Sentinel into * The function returns 1 if the matching Sentinel was removed, otherwise
* a master's Sentinels dictionary, we want to be very sure about not * 0 if there was no Sentinel with this ID. */
* having duplicated instances for any reason. This is important because int removeMatchingSentinelFromMaster(sentinelRedisInstance *master, char *runid) {
* other sentinels are needed to reach ODOWN quorum, and later to get
* voted for a given configuration epoch in order to perform the failover.
*
* The function returns the number of Sentinels removed. */
int removeMatchingSentinelsFromMaster(sentinelRedisInstance *master, char *ip, int port, char *runid) {
dictIterator *di; dictIterator *di;
dictEntry *de; dictEntry *de;
int removed = 0; int removed = 0;
if (runid == NULL) return 0;
di = dictGetSafeIterator(master->sentinels); di = dictGetSafeIterator(master->sentinels);
while((de = dictNext(di)) != NULL) { while((de = dictNext(di)) != NULL) {
sentinelRedisInstance *ri = dictGetVal(de); sentinelRedisInstance *ri = dictGetVal(de);
if ((ri->runid && runid && strcmp(ri->runid,runid) == 0) || if (ri->runid && strcmp(ri->runid,runid) == 0) {
(ip && strcmp(ri->addr->ip,ip) == 0 && port == ri->addr->port))
{
dictDelete(master->sentinels,ri->name); dictDelete(master->sentinels,ri->name);
removed++; removed++;
} }
@ -2161,21 +2155,18 @@ void sentinelProcessHelloMessage(char *hello, int hello_len) {
if (!si) { if (!si) {
/* If not, remove all the sentinels that have the same runid /* If not, remove all the sentinels that have the same runid
* OR the same ip/port, because it's either a restart or a * because there was an address change, and add the same Sentinel
* network topology change. */ * with the new address back. */
removed = removeMatchingSentinelsFromMaster(master,token[0],port, removed = removeMatchingSentinelFromMaster(master,token[2]);
token[2]);
if (removed) { if (removed) {
sentinelEvent(REDIS_NOTICE,"-dup-sentinel",master, sentinelEvent(REDIS_NOTICE,"+sentinel-address-switch",master,
"%@ #duplicate of %s:%d or %s", "%@ ip %s port %d for %s", token[0],port,token[2]);
token[0],port,token[2]);
} }
/* Add the new sentinel. */ /* Add the new sentinel. */
si = createSentinelRedisInstance(NULL,SRI_SENTINEL, si = createSentinelRedisInstance(NULL,SRI_SENTINEL,
token[0],port,master->quorum,master); token[0],port,master->quorum,master);
if (si) { if (si) {
sentinelEvent(REDIS_NOTICE,"+sentinel",si,"%@");
/* The runid is NULL after a new instance creation and /* The runid is NULL after a new instance creation and
* for Sentinels we don't have a later chance to fill it, * for Sentinels we don't have a later chance to fill it,
* so do it now. */ * so do it now. */