diff --git a/src/cluster.c b/src/cluster.c index 35f3a76be..6bbb9e0cc 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -508,6 +508,7 @@ void deriveAnnouncedPorts(int *announced_port, int *announced_pport, * that may change at runtime via CONFIG SET. This function changes the * set of flags in myself->flags accordingly. */ void clusterUpdateMyselfFlags(void) { + if (!myself) return; int oldflags = myself->flags; int nofailover = server.cluster_slave_no_failover ? CLUSTER_NODE_NOFAILOVER : 0; @@ -519,6 +520,36 @@ void clusterUpdateMyselfFlags(void) { } } + +/* We want to take myself->ip in sync with the cluster-announce-ip option. +* The option can be set at runtime via CONFIG SET. */ +void clusterUpdateMyselfIp(void) { + if (!myself) return; + static char *prev_ip = NULL; + char *curr_ip = server.cluster_announce_ip; + int changed = 0; + + if (prev_ip == NULL && curr_ip != NULL) changed = 1; + else if (prev_ip != NULL && curr_ip == NULL) changed = 1; + else if (prev_ip && curr_ip && strcmp(prev_ip,curr_ip)) changed = 1; + + if (changed) { + if (prev_ip) zfree(prev_ip); + prev_ip = curr_ip; + + if (curr_ip) { + /* We always take a copy of the previous IP address, by + * duplicating the string. This way later we can check if + * the address really changed. */ + prev_ip = zstrdup(prev_ip); + strncpy(myself->ip,server.cluster_announce_ip,NET_IP_STR_LEN); + myself->ip[NET_IP_STR_LEN-1] = '\0'; + } else { + myself->ip[0] = '\0'; /* Force autodetection. */ + } + } +} + void clusterInit(void) { int saveconf = 0; @@ -603,6 +634,7 @@ void clusterInit(void) { server.cluster->mf_end = 0; resetManualFailover(); clusterUpdateMyselfFlags(); + clusterUpdateMyselfIp(); } /* Reset a node performing a soft or hard reset: @@ -3612,35 +3644,6 @@ void clusterCron(void) { iteration++; /* Number of times this function was called so far. */ - /* We want to take myself->ip in sync with the cluster-announce-ip option. - * The option can be set at runtime via CONFIG SET, so we periodically check - * if the option changed to reflect this into myself->ip. */ - { - static char *prev_ip = NULL; - char *curr_ip = server.cluster_announce_ip; - int changed = 0; - - if (prev_ip == NULL && curr_ip != NULL) changed = 1; - else if (prev_ip != NULL && curr_ip == NULL) changed = 1; - else if (prev_ip && curr_ip && strcmp(prev_ip,curr_ip)) changed = 1; - - if (changed) { - if (prev_ip) zfree(prev_ip); - prev_ip = curr_ip; - - if (curr_ip) { - /* We always take a copy of the previous IP address, by - * duplicating the string. This way later we can check if - * the address really changed. */ - prev_ip = zstrdup(prev_ip); - strncpy(myself->ip,server.cluster_announce_ip,NET_IP_STR_LEN); - myself->ip[NET_IP_STR_LEN-1] = '\0'; - } else { - myself->ip[0] = '\0'; /* Force autodetection. */ - } - } - } - /* The handshake timeout is the time after which a handshake node that was * not turned into a normal node is removed from the nodes. Usually it is * just the NODE_TIMEOUT value, but when NODE_TIMEOUT is too small we use @@ -3648,9 +3651,6 @@ void clusterCron(void) { handshake_timeout = server.cluster_node_timeout; if (handshake_timeout < 1000) handshake_timeout = 1000; - /* Update myself flags. */ - clusterUpdateMyselfFlags(); - /* Clear so clusterNodeCronHandleReconnect can count the number of nodes in PFAIL. */ server.cluster->stats_pfail_nodes = 0; /* Run through some of the operations we want to do on each cluster node. */ diff --git a/src/cluster.h b/src/cluster.h index e6cdc1897..e0cf5c4dd 100644 --- a/src/cluster.h +++ b/src/cluster.h @@ -323,5 +323,7 @@ void slotToKeyReplaceEntry(dictEntry *entry, redisDb *db); void slotToKeyInit(redisDb *db); void slotToKeyFlush(redisDb *db); void slotToKeyDestroy(redisDb *db); +void clusterUpdateMyselfFlags(void); +void clusterUpdateMyselfIp(void); #endif /* __CLUSTER_H */ diff --git a/src/config.c b/src/config.c index 19ad2926f..5d0bf664a 100644 --- a/src/config.c +++ b/src/config.c @@ -2213,6 +2213,23 @@ int updateRequirePass(sds val, sds prev, const char **err) { return 1; } + +int updateClusterFlags(int val, int prev, const char **err) { + UNUSED(val); + UNUSED(prev); + UNUSED(err); + clusterUpdateMyselfFlags(); + return 1; +} + +int updateClusterIp(char *val, char *prev, const char **err) { + UNUSED(val); + UNUSED(prev); + UNUSED(err); + clusterUpdateMyselfIp(); + return 1; +} + #ifdef USE_OPENSSL static int updateTlsCfg(char *val, char *prev, const char **err) { UNUSED(val); @@ -2527,7 +2544,7 @@ standardConfig configs[] = { createBoolConfig("aof-load-truncated", NULL, MODIFIABLE_CONFIG, server.aof_load_truncated, 1, NULL, NULL), createBoolConfig("aof-use-rdb-preamble", NULL, MODIFIABLE_CONFIG, server.aof_use_rdb_preamble, 1, NULL, NULL), createBoolConfig("aof-timestamp-enabled", NULL, MODIFIABLE_CONFIG, server.aof_timestamp_enabled, 0, NULL, NULL), - createBoolConfig("cluster-replica-no-failover", "cluster-slave-no-failover", MODIFIABLE_CONFIG, server.cluster_slave_no_failover, 0, NULL, NULL), /* Failover by default. */ + createBoolConfig("cluster-replica-no-failover", "cluster-slave-no-failover", MODIFIABLE_CONFIG, server.cluster_slave_no_failover, 0, NULL, updateClusterFlags), /* Failover by default. */ createBoolConfig("replica-lazy-flush", "slave-lazy-flush", MODIFIABLE_CONFIG, server.repl_slave_lazy_flush, 0, NULL, NULL), createBoolConfig("replica-serve-stale-data", "slave-serve-stale-data", MODIFIABLE_CONFIG, server.repl_serve_stale_data, 1, NULL, NULL), createBoolConfig("replica-read-only", "slave-read-only", DEBUG_CONFIG | MODIFIABLE_CONFIG, server.repl_slave_ro, 1, NULL, NULL), @@ -2551,7 +2568,7 @@ standardConfig configs[] = { createStringConfig("pidfile", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.pidfile, NULL, NULL, NULL), createStringConfig("replica-announce-ip", "slave-announce-ip", MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.slave_announce_ip, NULL, NULL, NULL), createStringConfig("masteruser", NULL, MODIFIABLE_CONFIG | SENSITIVE_CONFIG, EMPTY_STRING_IS_NULL, server.masteruser, NULL, NULL, NULL), - createStringConfig("cluster-announce-ip", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.cluster_announce_ip, NULL, NULL, NULL), + createStringConfig("cluster-announce-ip", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.cluster_announce_ip, NULL, NULL, updateClusterIp), createStringConfig("cluster-config-file", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, server.cluster_configfile, "nodes.conf", NULL, NULL), createStringConfig("syslog-ident", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, server.syslog_ident, "redis", NULL, NULL), createStringConfig("dbfilename", NULL, MODIFIABLE_CONFIG, ALLOW_EMPTY_STRING, server.rdb_filename, "dump.rdb", isValidDBfilename, NULL),