@@ -55,7 +55,7 @@ void NodeWatcher(zhandle_t* zh, int type, int state, const char* path, void* wat
5555}
5656
5757void ItemWatcher (zhandle_t * zh, int type, int state, const char * path, void * watcher_ctx) {
58- PDLOG (INFO, " node watcher with event type %d, state %d" , type, state);
58+ PDLOG (INFO, " item watcher with event type %d, state %d" , type, state);
5959 if (zoo_get_context (zh)) {
6060 ZkClient* client = const_cast <ZkClient*>(reinterpret_cast <const ZkClient*>(zoo_get_context (zh)));
6161 std::string path_str (path);
@@ -64,8 +64,8 @@ void ItemWatcher(zhandle_t* zh, int type, int state, const char* path, void* wat
6464}
6565
6666ZkClient::ZkClient (const std::string& hosts, const std::string& real_endpoint, int32_t session_timeout,
67- const std::string& endpoint, const std::string& zk_root_path,
68- const std::string& auth_schema, const std::string& cert)
67+ const std::string& endpoint, const std::string& zk_root_path, const std::string& auth_schema,
68+ const std::string& cert)
6969 : hosts_(hosts),
7070 session_timeout_ (session_timeout),
7171 endpoint_(endpoint),
@@ -92,8 +92,8 @@ ZkClient::ZkClient(const std::string& hosts, const std::string& real_endpoint, i
9292}
9393
9494ZkClient::ZkClient (const std::string& hosts, int32_t session_timeout, const std::string& endpoint,
95- const std::string& zk_root_path, const std::string& zone_path,
96- const std::string& auth_schema, const std::string& cert)
95+ const std::string& zk_root_path, const std::string& zone_path, const std::string& auth_schema,
96+ const std::string& cert)
9797 : hosts_(hosts),
9898 session_timeout_(session_timeout),
9999 endpoint_(endpoint),
@@ -296,8 +296,7 @@ bool ZkClient::CreateNode(const std::string& node, const std::string& value, int
296296 }
297297 uint32_t size = node.size () + 11 ;
298298 char path_buffer[size]; // NOLINT
299- int ret =
300- zoo_create (zk_, node.c_str (), value.c_str (), value.size (), &acl_vector_, flags, path_buffer, size);
299+ int ret = zoo_create (zk_, node.c_str (), value.c_str (), value.size (), &acl_vector_, flags, path_buffer, size);
301300 if (ret == ZOK) {
302301 assigned_path_name.assign (path_buffer, size - 1 );
303302 PDLOG (INFO, " create node %s ok and real node name %s" , node.c_str (), assigned_path_name.c_str ());
@@ -583,8 +582,13 @@ void ZkClient::LogEvent(int type, int state, const char* path) {
583582 if (type == ZOO_SESSION_EVENT) {
584583 if (state == ZOO_CONNECTED_STATE) {
585584 Connected ();
585+ } else if (state == ZOO_CONNECTING_STATE || state == ZOO_ASSOCIATING_STATE) {
586+ // just wait
586587 } else if (state == ZOO_EXPIRED_SESSION_STATE) {
587588 connected_ = false ;
589+ } else {
590+ // unknow state, should retry
591+ connected_ = false ;
588592 }
589593 }
590594}
@@ -630,5 +634,18 @@ bool ZkClient::Mkdir(const std::string& path) {
630634 return MkdirNoLock (path);
631635}
632636
637+ bool ZkClient::EnsureConnected () {
638+ if (!IsConnected ()) {
639+ LOG (WARNING) << " reconnect zk" ;
640+ if (Reconnect ()) {
641+ LOG (INFO) << " reconnect zk ok" ;
642+ } else {
643+ LOG (WARNING) << " reconnect zk failed" ;
644+ return false ;
645+ }
646+ }
647+ return true ;
648+ }
649+
633650} // namespace zk
634651} // namespace openmldb
0 commit comments