redis关于sentinel配置bind的ip先后顺序问题


redis关于sentinel配置bind的ip先后顺序问题

准备开始看sentinel相关的代码,首先先启动起来看看情况,
于是准备了3台机器
10.100.13.81
10.100.13.88
10.100.13.160 

10.100.13.81作为主 88和160 作为从机,

启动单一的sentinel,遇到一个奇怪的问题
如果启动的sentinel是在主机本地,那么可以找到主机的从机,
结果显示如下:
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=1
slaves的数量是2,正确

但是如果是远程的sentinal监控redis主机时候,
就发现找不到主机的从机,状态失败
结果显示如下:
master0:name=mymaster,status=odown,address=10.100.13.81:6379,slaves=0,sentinels=1
slaves的数量是0,有问题

于是我们开始了debug
找到了函数
/* Send periodic PING, INFO, and PUBLISH to the Hello channel to
 * the specified master or slave instance. */
void sentinelSendPeriodicCommands(sentinelRedisInstance *ri) {
    mstime_t now = mstime();
    mstime_t info_period, ping_period;
    int retval;

    /* Return ASAP if we have already a PING or INFO already pending, or
     * in the case the instance is not properly connected. */
    if (ri->link->disconnected) return;  发现到达这里就返回了,查看了下,这里的标志是1,即断开
。。。。。。
}

然后我们开始查找为什么这个连接会断开? 
void sentinelHandleRedisInstance(sentinelRedisInstance *ri) {
    /* ========== MONITORING HALF ============ */
    /* Every kind of instance */
    sentinelReconnectInstance(ri); ********这里进行了反复连接请求
    sentinelSendPeriodicCommands(ri);

。。。。。。
进入函数sentinelReconnectInstance
void sentinelReconnectInstance(sentinelRedisInstance *ri) {
    if (ri->link->disconnected == 0) return;
    if (ri->addr->port == 0) return; /* port == 0 means invalid address. */
    instanceLink *link = ri->link;
    mstime_t now = mstime();

    if (now - ri->link->last_reconn_time < SENTINEL_PING_PERIOD) return;
    ri->link->last_reconn_time = now;

    /* Commands connection. */
    if (link->cc == NULL) {   
        link->cc = redisAsyncConnectBind(ri->addr->ip,ri->addr->port,NET_FIRST_BIND_ADDR); debug到这里,发现这个宏定义的取值问题
       。。。。。。。
该宏定义如下:
/* Get the first bind addr or NULL */
#define NET_FIRST_BIND_ADDR (server.bindaddr_count ? server.bindaddr[0] : NULL)
可以看到判断bind的配置时候有ip,有的话取第一个
而我的配置如下:
bind 127.0.0.1 10.100.13.160
bind 127.0.0.1 10.100.13.88
第一个是127.0.0.1,所以远程连接的时候就有问题,

解决方案: 只需要调换地址即可
bind 10.100.13.160 127.0.0.1 
bind 10.100.13.88 127.0.0.1 

如果更好的理解上来说,这里应该进行多一些判断,将后面的IP也进行测试一下,
这里只测试第一个,显得不是很友好