行杂记之Zookeeper SessionTimeOut分析

来源:互联网 发布:oracle数据库字段类型 编辑:程序博客网 时间:2024/06/08 04:56

摘要: 近期的一个C++项目里使用了Zookeeper做服务发现,期间遇到了SessionTimeOut问题的困扰,明明通过zookeeper c client设置了超时时间,但无效。 请原谅我一开始对zookeeper不熟悉。

0.前言

本文为笃行日常工作记录,烂笔头系列。

源码前面,了无秘密 — by 侯杰

近期的一个C++项目里使用了Zookeeper做服务发现,期间遇到了SessionTimeOut问题的困扰,明明通过zookeeper c client设置了超时时间,但无效。

请原谅我一开始对zookeeper不熟悉。最终通过分析源码了解到SessionTimeOut最终的确定是一个协商的过程,而不是简单的配置生效。

在这里记录下Session超时时间的有关分析,基于zookeeper 3.4.8

1.zookeeper client SessionTimeOut

项目中使用的是 C client,通过zookeer_init 创建zk session,调用了zookeeper_init其实就开始了建立链接到ZK集群的过程,这里设置的recv_timeout 为客户端所期望的session超时时间,单位为毫秒。

ZOOAPI zhandle_t *zookeeper_init(const char *host, watcher_fn fn,  int recv_timeout, const clientid_t *clientid, void *context, int flags);

连接成功之后客户端发起握手协议,可以看到之前设置的recv_timeout随握手协议一起发送给服务端,zookeeper.c#L1485。

static int prime_connection(zhandle_t *zh){    int rc;    /*this is the size of buffer to serialize req into*/    char buffer_req[HANDSHAKE_REQ_SIZE];    int len = sizeof(buffer_req);    int hlen = 0;    struct connect_req req;    req.protocolVersion = 0;    req.sessionId = zh->seen_rw_server_before ? zh->client_id.client_id : 0;    req.passwd_len = sizeof(req.passwd);    memcpy(req.passwd, zh->client_id.passwd, sizeof(zh->client_id.passwd));    req.timeOut = zh->recv_timeout; <-这里设置timeOut    req.lastZxidSeen = zh->last_zxid;    req.readOnly = zh->allow_read_only;    hlen = htonl(len);    /* We are running fast and loose here, but this string should fit in the initial buffer! */    rc=zookeeper_send(zh->fd, &hlen, sizeof(len));    serialize_prime_connect(&req, buffer_req);    rc=rc<0 ? rc : zookeeper_send(zh->fd, buffer_req, len);    if (rc<0) {        return handle_socket_error_msg(zh, __LINE__, ZCONNECTIONLOSS,                "failed to send a handshake packet: %s", strerror(errno));    }

再来看看处理握手协议Resp的逻辑 zookeeper.c L1767

static int check_events(zhandle_t *zh, int events){    if (zh->fd == -1)        return ZINVALIDSTATE;  ……  ……  ……   deserialize_prime_response(&zh->primer_storage, zh->primer_buffer.buffer);                /* We are processing the primer_buffer, so we need to finish                 * the connection handshake */                oldid = zh->client_id.client_id;                newid = zh->primer_storage.sessionId;                if (oldid != 0 && oldid != newid) {                    zh->state = ZOO_EXPIRED_SESSION_STATE;                    errno = ESTALE;                    return handle_socket_error_msg(zh,__LINE__,ZSESSIONEXPIRED,                            "sessionId=%#llx has expired.",oldid);                } else {                    zh->recv_timeout = zh->primer_storage.timeOut; //设置为Resp的Timeout                    zh->client_id.client_id = newid;}

至此可以发现,最终客户端的SessionTimeOut时间实际是经过服务端下发之后的,并不一定是最先设置的。

2.Zookeeper Server SessionTimeOut

2.1协商客户端上报的SessionTimeOut

来看看服务端握手的处理逻辑ZooKeeperServer.java#L876。

public void processConnectRequest(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOException {        BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(incomingBuffer));        ConnectRequest connReq = new ConnectRequest();        connReq.deserialize(bia, "connect");………………  //根据客户端上报的timeout和服务端自身的minSessionTimeOut。  //如果上报的timeout小于minSessionTimeOut则 设置timeout为minSessionTimeOut.  //如果上报的timeout大于maxSessionTimeOut则 设置timeout为maxSessionTimeOut.  //如果介于两则之间,则以上报的时间为准。        int sessionTimeout = connReq.getTimeOut();        byte passwd[] = connReq.getPasswd();        int minSessionTimeout = getMinSessionTimeout();        if (sessionTimeout < minSessionTimeout) {            sessionTimeout = minSessionTimeout;        }        int maxSessionTimeout = getMaxSessionTimeout();        if (sessionTimeout > maxSessionTimeout) {            sessionTimeout = maxSessionTimeout;        }        cnxn.setSessionTimeout(sessionTimeout);………………    }

可以一句话概括,客户端上报的期望timeout一定要在服务端设置的上下界之间,如果越过边界,则以边界为准。

2.2 服务端MinSessionTimeOut和MaxSessionTimeOut的确定

​​​​​​​

原文链接

原创粉丝点击