Skip to content

Commit

Permalink
sentinel: Decouple bind address from address sent to other sentinels
Browse files Browse the repository at this point in the history
There are instances such as EC2 where the bind address is private
(behind a NAT) and cannot be accessible from WAN.

https://groups.google.com/d/msg/redis-db/PVVvjO4nMd0/P3oWC036v3cJ
  • Loading branch information
dkong authored and antirez committed Sep 4, 2014
1 parent 67e414c commit 3d93926
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 7 additions & 0 deletions sentinel.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
# The port that this sentinel instance will run on
port 26379

# announce <ip>
# The IP that other sentinels use to connect to this sentinel instance
#
# Example:
#
# sentinel announce 1.2.3.4

# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
Expand Down
27 changes: 24 additions & 3 deletions src/sentinel.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ struct sentinelState {
mstime_t tilt_start_time; /* When TITL started. */
mstime_t previous_time; /* Last time we ran the time handler. */
list *scripts_queue; /* Queue of user scripts to execute. */
sentinelAddr *announce_addr; /* Address that is gossiped to other sentinels. */
} sentinel;

/* A script execution job. */
Expand Down Expand Up @@ -425,6 +426,7 @@ void initSentinel(void) {
sentinel.previous_time = mstime();
sentinel.running_scripts = 0;
sentinel.scripts_queue = listCreate();
sentinel.announce_addr = NULL;
}

/* This function gets called when the server is in Sentinel mode, started,
Expand Down Expand Up @@ -1425,6 +1427,12 @@ char *sentinelHandleConfiguration(char **argv, int argc) {
return "Wrong hostname or port for sentinel.";
}
if (argc == 5) si->runid = sdsnew(argv[4]);
} else if (!strcasecmp(argv[0],"announce") && argc == 2) {
/* announce <host> */
sentinel.announce_addr = createSentinelAddr(argv[1], server.port);
if (sentinel.announce_addr == NULL) {
return "Unable to resolve host.";
}
} else {
return "Unrecognized sentinel configuration statement.";
}
Expand Down Expand Up @@ -2213,19 +2221,30 @@ int sentinelSendHello(sentinelRedisInstance *ri) {
char ip[REDIS_IP_STR_LEN];
char payload[REDIS_IP_STR_LEN+1024];
int retval;
char *announceIP;
int port;
sentinelRedisInstance *master = (ri->flags & SRI_MASTER) ? ri : ri->master;
sentinelAddr *master_addr = sentinelGetCurrentMasterAddress(master);

if (ri->flags & SRI_DISCONNECTED) return REDIS_ERR;

/* Try to obtain our own IP address. */
if (anetSockName(ri->cc->c.fd,ip,sizeof(ip),NULL) == -1) return REDIS_ERR;
/* Use the specified announce address if specified, otherwise try to
* obtain our own IP address. */
if (sentinel.announce_addr) {
announceIP = sentinel.announce_addr->ip;
port = sentinel.announce_addr->port;
} else {
if (anetSockName(ri->cc->c.fd,ip,sizeof(ip),NULL) == -1)
return REDIS_ERR;
announceIP = ip;
port = server.port;
}

/* Format and send the Hello message. */
snprintf(payload,sizeof(payload),
"%s,%d,%s,%llu," /* Info about this sentinel. */
"%s,%s,%d,%llu", /* Info about current master. */
ip, server.port, server.runid,
announceIP, port, server.runid,
(unsigned long long) sentinel.current_epoch,
/* --- */
master->name,master_addr->ip,master_addr->port,
Expand All @@ -2234,6 +2253,8 @@ int sentinelSendHello(sentinelRedisInstance *ri) {
sentinelPublishReplyCallback, NULL, "PUBLISH %s %s",
SENTINEL_HELLO_CHANNEL,payload);
if (retval != REDIS_OK) return REDIS_ERR;
redisLog(REDIS_DEBUG, "Sentinel Send Hello ip=%s, port=%d, id=%s, epoch=%llu",
announceIP, port, server.runid, (unsigned long long) sentinel.current_epoch);
ri->pending_commands++;
return REDIS_OK;
}
Expand Down

0 comments on commit 3d93926

Please sign in to comment.