0xsky / xredis

Redis C++ client, support the data slice storage, support redis cluster, thread-safe,multi-platform,connection pool, read/write separation.
GNU General Public License v3.0
337 stars 153 forks source link

redis cluster模式下主从切换后崩溃在集群连接处 #44

Closed bulingma closed 5 years ago

bulingma commented 5 years ago

redis cluster模式下,宕机一台主节点,发生主从切换后,宕机的主节点显示disconnected状态,则xRedisClusterClient::ConnectRedis函数内的逻辑会导致连接集群崩溃(访问非法内存nodeinfo[8]注:master fail状态时没有nodeinfo[8]),但是当前集群是可用的,该段代码建议增加如下代码: bool xRedisClusterClient::ConnectRedis(const char *host, uint32_t port, uint32_t poolsize) {

...

for (size_t i= 0; i < vlines.size(); ++i) {
    NodeInfo node;
    node.strinfo = vlines[i];

    vector<string> nodeinfo;
    str2Vect(node.strinfo.c_str(), nodeinfo, " ");
    for (size_t k = 0; k < nodeinfo.size(); ++k) {
        //printf("%lu : %s \r\n", k, nodeinfo[k].c_str());
    }

    if(NULL != strstr(nodeinfo[7].c_str(), "disconnected")){
        //printf("%s \r\n", nodeinfo[7].c_str());
        continue;
    }

    if (NULL == strstr(nodeinfo[2].c_str(), "master")) {
        //printf("%s \r\n", nodeinfo[2].c_str());
        continue;
    }
    node.id = nodeinfo[0];
    node.ParseNodeString(nodeinfo[1]);
    node.ParseSlotString(nodeinfo[8]);
    //printf("------------------------\r\n");
    vNodes.push_back(node);
}

....

}

0xsky commented 5 years ago

你能修改下,提交fix 上来么?

0xsky commented 5 years ago

have fixed