zoukankan      html  css  js  c++  java
  • Redis源码分析Sentinel(3)主观下线与客观下线

    1、检测主观下线状态:

    void sentinelCheckSubjectivelyDown(sentinelRedisInstance *ri) {
        mstime_t elapsed = mstime() - ri->last_avail_time;
    
        /* Check if we are in need for a reconnection of one of the 
         * links, because we are detecting low activity.
         *
         * 1) Check if the command link seems connected, was connected not less
         *    than SENTINEL_MIN_LINK_RECONNECT_PERIOD, but still we have an
         *    idle time that is greater than down_after_period / 2 seconds. */
        if (ri->cc &&
            (mstime() - ri->cc_conn_time) > SENTINEL_MIN_LINK_RECONNECT_PERIOD &&
            (mstime() - ri->last_pong_time) > (ri->down_after_period/2))
        {
            sentinelKillLink(ri,ri->cc);
        }
    
        /* 2) Check if the pubsub link seems connected, was connected not less
         *    than SENTINEL_MIN_LINK_RECONNECT_PERIOD, but still we have no
         *    activity in the Pub/Sub channel for more than
         *    SENTINEL_PUBLISH_PERIOD * 3.
         */
        if (ri->pc &&
            (mstime() - ri->pc_conn_time) > SENTINEL_MIN_LINK_RECONNECT_PERIOD &&
            (mstime() - ri->pc_last_activity) > (SENTINEL_PUBLISH_PERIOD*3))
        {
            sentinelKillLink(ri,ri->pc);
        }
    
        /* Update the SDOWN flag. We believe the instance is SDOWN if:
         *
         * 1) It is not replying.
         * 2) We believe it is a master, it reports to be a slave for enough time
         *    to meet the down_after_period, plus enough time to get two times
         *    INFO report from the instance. */
        if (elapsed > ri->down_after_period ||
            (ri->flags & SRI_MASTER &&
             ri->role_reported == SRI_SLAVE &&
             mstime() - ri->role_reported_time >
              (ri->down_after_period+SENTINEL_INFO_PERIOD*2)))
        {
            /* Is subjectively down */
            if ((ri->flags & SRI_S_DOWN) == 0) {
                sentinelEvent(REDIS_WARNING,"+sdown",ri,"%@");
                ri->s_down_since_time = mstime();
                /* 打开主观下线标志 */
                ri->flags |= SRI_S_DOWN;
            }
        } else {
            /* Is subjectively up */
            if (ri->flags & SRI_S_DOWN) {
                sentinelEvent(REDIS_WARNING,"-sdown",ri,"%@");
                ri->flags &= ~(SRI_S_DOWN|SRI_SCRIPT_KILL_SENT);
            }
        }
    }
    
    • 主要是通过判断时间来判断主观下线的;
    • L46:打开主观下线标志;

    2、判断客观下线状态:

    void sentinelCheckObjectivelyDown(sentinelRedisInstance *master) {
        dictIterator *di;
        dictEntry *de;
        int quorum = 0, odown = 0;
    
        if (master->flags & SRI_S_DOWN) {
            /* Is down for enough sentinels? */
            quorum = 1; /* the current sentinel. */
            /* Count all the other sentinels. */
            /* 遍历master的sentinels字典,判断他们是否认为该master主观下线 */
            di = dictGetIterator(master->sentinels);
            while((de = dictNext(di)) != NULL) {
                sentinelRedisInstance *ri = dictGetVal(de);
    
                if (ri->flags & SRI_MASTER_DOWN) quorum++;
            }
            dictReleaseIterator(di);
            /* 可以判断为客观下线 */
            if (quorum >= master->quorum) odown = 1;
        }
    
        /* Set the flag accordingly to the outcome. */
        if (odown) {
            if ((master->flags & SRI_O_DOWN) == 0) {
                sentinelEvent(REDIS_WARNING,"+odown",master,"%@ #quorum %d/%d",
                    quorum, master->quorum);
                master->flags |= SRI_O_DOWN;
                master->o_down_since_time = mstime();
            }
        } else {
            if (master->flags & SRI_O_DOWN) {
                sentinelEvent(REDIS_WARNING,"-odown",master,"%@");
                master->flags &= ~SRI_O_DOWN;
            }
        }
    }
    
    • L1:参数是master,记住只有主服务器才需要被检查客观下线状态

    • L10~L17:通过监视该master的所有Sentinel投票来决定 本机sentinel认定该master 客观下线

  • 相关阅读:
    关于sizeof,对空指针sizeof(*p)可以吗?
    Mysql之锁(一)
    Mysql之事务
    Mysql之Explain关键字及常见的优化手段
    Mysql查询优化器之关于子查询的优化
    Mysql查询优化器之关于JOIN的优化
    Mysql查询优化器之基本优化
    Mysql之B+树索引实战
    Mysql之索引
    Mysql之InnoDB行格式、数据页结构
  • 原文地址:https://www.cnblogs.com/macguz/p/15865713.html
Copyright © 2011-2022 走看看