行杂记之Zookeeper SessionTimeOut分析

摘要:近期的一个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

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

ZOOAPI zhandle_t *zookeeper_init(constchar*host, watcher_fn fn,intrecv_timeout,constclientid_t*clientid,void*context,intflags);

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

staticintprime_connection(zhandle_t *zh){intrc;/*this is the size of buffer to serialize req into*/charbuffer_req[HANDSHAKE_REQ_SIZE];intlen =sizeof(buffer_req);inthlen =0;structconnect_reqreq;    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) {returnhandle_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的Timeoutzh->client_id.client_id = newid;}

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

2.Zookeeper Server SessionTimeOut

2.1协商客户端上报的SessionTimeOut

《行杂记之Zookeeper SessionTimeOut分析》

原文链接

    原文作者:乱蓬头199302
    原文地址: https://www.jianshu.com/p/14aec2f1a65d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞