void ccs_children_watcher(zhandle_t* zh, int type, int state, const char* path, void* watcherCtx) { printf("\n\n"); printf("children event happened: type[%d]\n", type); int ret = 0; struct watch_func_para_t* para= (struct watch_func_para_t*)watcherCtx; struct String_vector strings; struct Stat stat; ret = zoo_wget_children2(para->zkhandle, "/election", ccs_children_watcher, watcherCtx, &strings, &stat); if (ret) { fprintf(stderr, "child: zoo_wget_children2 error [%d]\n", ret); exit(EXIT_FAILURE); } if (is_leader(para->zkhandle, para->node)) { i_am_leader = true; printf("This is [%s], i am a leader\n", para->node); } else { i_am_leader = false; printf("This is [%s], i am a follower\n", para->node); } return; }
static int join_the_election(zhandle_t *zkhandle, char node[], struct watch_func_para_t *para) { char buf[512] = {0}; int ret; ret = zoo_create(zkhandle, "/election/ccs-member", "hello", 5, &ZOO_OPEN_ACL_UNSAFE, /* a completely open ACL */ ZOO_SEQUENCE|ZOO_EPHEMERAL, buf, sizeof(buf)-1); if (ret) { fprintf(stderr, "zoo_create error [%d]\n", ret); return ret; } get_node_name(buf, node); struct Stat stat; memset(&stat, 0, sizeof(stat)); struct String_vector strings; memset(para, 0, sizeof(*para)); para->zkhandle = zkhandle; strcpy(para->node, node); ret = zoo_wget_children2(zkhandle, "/election", ccs_children_watcher, para, &strings, &stat); if (ret) { fprintf(stderr, "zoo_wget_children2 error [%d]\n", ret); return ret; } return ret; }
void election_watcher(zhandle_t * zh, int type, int state, const char* path, void* watcherCtx) { watch_func_para_t* para= (watch_func_para_t*)watcherCtx; DEBUG_PRINT("something happened:",""); DEBUG_PRINT("type - %d", type); DEBUG_PRINT("state - %d", state); DEBUG_PRINT("path - %s", path); DEBUG_PRINT("data - %s", para->node); struct String_vector strings; struct Stat stat; int ret = zoo_wget_children2(zh, path, election_watcher, watcherCtx, &strings, &stat); if(ret) { printf("zoo_wget_children2 path %s error ret %d\n", path, ret);} deallocate_String_vector(&strings); /* if(para->zks->isLeader()) { printf("I'm leader now!\n"); } else { printf("I'm slave.\n"); } */ }
/* 监视league下的节点变化事件 */ int zkServer::watchChildren() { if(NULL == zkhandle) { ERROR_PRINT(" zkhandle is NULL ", ""); return -1; } int ret = 0; memset(¶, 0, sizeof(para)); para.zkhandle = zkhandle; strcpy(para.node, node_name.c_str()); DEBUG_PRINT("node_name %s para.node %s", node_name.c_str(), para.node); para.zks = this; struct String_vector strings; struct Stat stat; ret = zoo_wget_children2(zkhandle, (root+server_name).c_str(), election_watcher, ¶, &strings, &stat); if (ret) { ERROR_PRINT("error %d of %s", ret, "wget children2"); return -2; } deallocate_String_vector(&strings); return ret; }
static VALUE method_get_children(VALUE self, VALUE reqid, VALUE path, VALUE async, VALUE watch) { struct String_vector strings; struct Stat stat; int rc; VALUE output; STANDARD_PREAMBLE(self, zk, reqid, path, async, watch, data_ctx, watch_ctx, call_type); switch (call_type) { case SYNC: rc = zoo_get_children2(zk->zh, RSTRING_PTR(path), 0, &strings, &stat); break; case SYNC_WATCH: rc = zoo_wget_children2(zk->zh, RSTRING_PTR(path), zkrb_state_callback, watch_ctx, &strings, &stat); break; case ASYNC: rc = zoo_aget_children2(zk->zh, RSTRING_PTR(path), 0, zkrb_strings_stat_callback, data_ctx); break; case ASYNC_WATCH: rc = zoo_awget_children2(zk->zh, RSTRING_PTR(path), zkrb_state_callback, watch_ctx, zkrb_strings_stat_callback, data_ctx); break; } output = rb_ary_new(); rb_ary_push(output, INT2FIX(rc)); if (IS_SYNC(call_type) && rc == ZOK) { rb_ary_push(output, zkrb_string_vector_to_ruby(&strings)); rb_ary_push(output, zkrb_stat_to_rarray(&stat)); } return output; }
ZooKeeperStringVectorResult ZooKeeperNodeManager::GetNodeChildren( const std::string& path ) { String_vector stringVector; Stat stat; memset( &stat, 0, sizeof( stat ) ); // gets the children associated with a node synchronously int result = zoo_wget_children2( m_zooKeeperSessionManager->GetHandle(), path.c_str(), NULL, NULL, &stringVector, &stat ); if( result == ZOK ) { std::vector< std::string > children; for( int32_t i = 0; i < stringVector.count; ++i ) { children.push_back( stringVector.data[i] ); } return std::make_pair( children, true ); } else if( result == ZNONODE ) { return std::make_pair( std::vector< std::string >(), false ); } throw ZooKeeperException( result ); }
ZooKeeperStringVectorResult ZooKeeperNodeManager::GetNodeChildren( const std::string& path, IZooKeeperNodeEventHandler* eventHandler ) { // insert into cache before calling wget to avoid race condition Insert( path, eventHandler ); String_vector stringVector; Stat stat; memset( &stat, 0, sizeof( stat ) ); // gets the children associated with a node synchronously and set a watcher int result = zoo_wget_children2( m_zooKeeperSessionManager->GetHandle(), path.c_str(), ZooKeeperEvent::EventHandler, this, &stringVector, &stat ); if( result == ZOK ) { std::vector< std::string > children; for( int32_t i = 0; i < stringVector.count; ++i ) { children.push_back( stringVector.data[i] ); } return std::make_pair( children, true ); } else if( result == ZNONODE ) { return std::make_pair( std::vector< std::string >(), false ); } throw ZooKeeperException( result ); }
//同步方式获得子节点信息 void getChildren(zhandle_t *zkhandle,char *str) { struct String_vector strings; struct Stat stat; int flag = zoo_wget_children2(zkhandle,str, watcher_myself,(void *)"testgetChildren", &strings,&stat); if (flag==ZOK) { int32_t i=0; for (;i<strings.count;++i) printf("%s\n",strings.data[i]); } }
int zoo_wget_children2_int(zhandle_t *zh, const char *path, watcher_fn watcher, unsigned long watcherCtx, struct String_vector *strings, struct Stat *stat) { return zoo_wget_children2(zh, path, watcher, (void*)watcherCtx, strings, stat); }