/** * Zookeeper callback when an event occurs. */ static void process(zhandle_t *zk_handle, int type, int state, const char *path, void *context) { LOG_DEBUG(("Watcher %d state = %s", type, state2String(state))); if (type == ZOO_SESSION_EVENT) { if (state == ZOO_AUTH_FAILED_STATE) { // We don't do auth. LOG_WARN(("Got an auth request from zookeeper. Server config is not compatible to this client!")); } else if (state == ZOO_CONNECTED_STATE) { const clientid_t *id = zoo_client_id(zk_handle); if (myid.client_id == 0 || myid.client_id != id->client_id) { myid = *id; LOG_DEBUG(("Got a new session id: 0x%llx",(long long int) myid.client_id)); } connected = true; generation++; LOG_DEBUG(("Client now connected!")); } else if (state == ZOO_EXPIRED_SESSION_STATE) { LOG_DEBUG(("Session expired, closing zookeeper")); connected = false; close_zookeeper(); } else { LOG_DEBUG(("Disconnecting")); connected = false; } } else { generation++; LOG_DEBUG(("Ignoring non-session event")); } }
void ZkWatcher(zhandle_t *zzh, int type, int state, const char *path, void *watcherCtx) { MCE_WARN( "ZkWatcher type = "<<watcherEvent2String(type)<<" state = "<<state2String(state)<<" path = "<<path ); if( type == ZOO_SESSION_EVENT ) { if( state == ZOO_CONNECTED_STATE ) { boost::shared_lock<boost::shared_mutex> lock(managers_lock_); managers_[zzh]->notifyZkLock(); managers_[zzh]->notifyHandle(""); return; } if( state == ZOO_EXPIRED_SESSION_STATE ) { ZkManager* tempPtr = 0; { boost::shared_lock<boost::shared_mutex> lock(managers_lock_); tempPtr = managers_[zzh]; } if( tempPtr ) { while( tempPtr->reInitZk() == false ) { sleep(1000); } }else { MCE_WARN("ZkWatcher can not reinitzk!"); } { boost::upgrade_lock<boost::shared_mutex> lock(managers_lock_); boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock1(lock); std::map<zhandle_t*,ZkManager*>::iterator findIt = managers_.find( zzh ); if( findIt != managers_.end() ) { managers_.erase(managers_.find(zzh)); } } return; } } { boost::shared_lock<boost::shared_mutex> lock(managers_lock_); managers_[zzh]->notifyHandle(path); } }
void watcher(zhandle_t *zzh, int type, int state, const char *path, void* context) { /* Be careful using zh here rather than zzh - as this may be mt code * the client lib may call the watcher before zookeeper_init returns */ fprintf(stderr, "Watcher %s state = %s", type2String(type), state2String(state)); if (path && strlen(path) > 0) { fprintf(stderr, " for path %s", path); } fprintf(stderr, "\n"); if (type == ZOO_SESSION_EVENT) { if (state == ZOO_CONNECTED_STATE) { const clientid_t *id = zoo_client_id(zzh); if (myid.client_id == 0 || myid.client_id != id->client_id) { myid = *id; fprintf(stderr, "Got a new session id: 0x%llx\n", _LL_CAST_ myid.client_id); if (clientIdFile) { FILE *fh = fopen(clientIdFile, "w"); if (!fh) { perror(clientIdFile); } else { int rc = fwrite(&myid, sizeof(myid), 1, fh); if (rc != sizeof(myid)) { perror("writing client id"); } fclose(fh); } } } } else if (state == ZOO_AUTH_FAILED_STATE) { fprintf(stderr, "Authentication failure. Shutting down...\n"); zookeeper_close(zzh); shutdownThisThing=1; zh=0; } else if (state == ZOO_EXPIRED_SESSION_STATE) { fprintf(stderr, "Session expired. Shutting down...\n"); zookeeper_close(zzh); shutdownThisThing=1; zh=0; } } }
// Called when there is some state change wrt zk handle // Note:- This is called under the context of the zk thread // So be sure that anything that you do is threadsafe void ZKClient::GlobalWatcher(zhandle_t* _zh, sp_int32 _type, sp_int32 _state, const char* _path) { // Be careful using zk_handler_ here rather than _zzh; // the client lib may call the watcher before zookeeper_init returns LOG(INFO) << "ZKClient GlobalWatcher called with type " << type2String(_type) << " and state " << state2String(_state); if (_path && strlen(_path) > 0) { LOG(INFO) << " for path " << _path; } if (_type == ZOO_SESSION_EVENT) { if (_state == ZOO_CONNECTED_STATE) { const clientid_t* id = zoo_client_id(_zh); if (zk_clientid_.client_id == 0 || zk_clientid_.client_id != id->client_id) { zk_clientid_ = *id; LOG(INFO) << "Got a new session id: " << zk_clientid_.client_id << "\n"; } } if (_state == ZOO_AUTH_FAILED_STATE) { LOG(FATAL) << "ZKClient Authentication failure. Shutting down...\n"; } else if (_state == ZOO_EXPIRED_SESSION_STATE) { // If client watcher is set, notify it about the session expiry // instead of shutting down. if (client_global_watcher_cb_) { const ZkWatchEvent event = {_type, _state, _path}; SendWatchEvent(event); } else { // We need to close and re-establish // There are watches, etc that need to be set again. // So the simpler option here is to kill ourselves LOG(FATAL) << "Session expired. Shutting down...\n"; } } else if (_state == ZOO_CONNECTING_STATE) { // We are still in the process of connecting LOG(INFO) << "Re-connecting to the zookeeper\n"; } else if (_state == ZOO_ASSOCIATING_STATE) { // Connection process still ongoing } } }
void Cluster::OnInitWatcher(zhandle_t *zzh, int type, int state, const char *path) { INFO_LOG("Watcher %s state = %s for path %s", type2String(type), state2String(state), (path && strlen(path) > 0) ? path : ""); if (type == ZOO_SESSION_EVENT) { if (state == ZOO_CONNECTED_STATE) { const clientid_t *id = zoo_client_id(zzh); if (m_zk_clientid.client_id == 0 || m_zk_clientid.client_id != id->client_id) { m_zk_clientid = *id; INFO_LOG("Got a new session id: 0x%llx", (long long )m_zk_clientid.client_id); if (!g_db->GetConf().zk_clientid_file.empty()) { std::string content; content.assign((const char*) (&m_zk_clientid), sizeof(m_zk_clientid)); file_write_content(g_db->GetConf().zk_clientid_file, content); } } m_state = ZK_STATE_CONNECTED; } else if (state == ZOO_AUTH_FAILED_STATE) { WARN_LOG("Zookeeper server authentication failure. Shutting down..."); zookeeper_close(zzh); m_zk = NULL; m_state = ZK_STATE_DISCONNECT; } else if (state == ZOO_EXPIRED_SESSION_STATE) { WARN_LOG("Session expired. Shutting down..."); zookeeper_close(zzh); m_zk = NULL; m_state = ZK_STATE_DISCONNECT; } } }
void INetwork::print_network() { DBG("rat '%s', state '%s', name '%s', strength '%u', average '%u'", rat2String(m_rat), state2String(m_state), m_name, m_strength, m_strength_average); }