void watchchildren(zhandle_t *zzh, int type, int state, const char *path, void* context) { struct String_vector str; int rc; printf("The event path %s, event type %d\n", path, type); if (type == ZOO_SESSION_EVENT) { if (state == ZOO_CONNECTED_STATE) { return; } else if (state == ZOO_AUTH_FAILED_STATE) { zookeeper_close(zzh); exit(1); } else if (state == ZOO_EXPIRED_SESSION_STATE) { zookeeper_close(zzh); exit(1); } } // Put the watch again rc = zoo_wget_children(zh, "/testpath1", watchchildren , mycontext, &str); if (ZOK != rc){ printf("Problems %d\n", rc); } else { int i = 0; while (i < str.count) { printf("Children %s\n", str.data[i++]); } if (str.count) { deallocate_String_vector(&str); } } }
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; }
int zkServer::isLeader() { int ret = 0; int flag = 1; if(NULL == zkhandle) { ERROR_PRINT(" zkhandle is NULL ", ""); return -1; } struct String_vector strings; DEBUG_PRINT("root %s server_name %s count %u", root.c_str(), server_name.c_str(), strings.count); ret = zoo_get_children(zkhandle, (root+server_name).c_str(), 0, &strings); if (ret) { ERROR_PRINT("Error %d for %s", ret, "get_children"); return -1; } int i; for ( i=0; i<strings.count; i++ ) { DEBUG_PRINT("node_name %s string[%d].data %s", node_name.c_str(), i, strings.data[i]); if ( strcmp(node_name.c_str(), strings.data[i])>0 ) { /* 如果我自己不是最小的节点 */ flag = 0; break; } } deallocate_String_vector(&strings); return flag; }
static void set_brokerlist_from_zookeeper(zhandle_t *zzh, char *brokers) { if (zzh) { struct String_vector brokerlist; if (zoo_get_children(zzh, BROKER_PATH, 1, &brokerlist) != ZOK) { fprintf(stderr, "No brokers found on path %s\n", BROKER_PATH); return; } int i; char *brokerptr = brokers; for (i = 0; i < brokerlist.count; i++) { char path[255], cfg[1024]; sprintf(path, "/brokers/ids/%s", brokerlist.data[i]); int len = sizeof(cfg); zoo_get(zzh, path, 0, cfg, &len, NULL); if (len > 0) { cfg[len] = '\0'; json_error_t jerror; json_t *jobj = json_loads(cfg, 0, &jerror); if (jobj) { json_t *jhost = json_object_get(jobj, "host"); json_t *jport = json_object_get(jobj, "port"); if (jhost && jport) { const char *host = json_string_value(jhost); const int port = json_integer_value(jport); sprintf(brokerptr, "%s:%d", host, port); brokerptr += strlen(brokerptr); if (i < brokerlist.count - 1) { *brokerptr++ = ','; } } json_decref(jobj); } } } deallocate_String_vector(&brokerlist); printf("Found brokers %s\n", brokers); } }
bool ZKOP::zkFreezkVector(zkVector& zkvector) {/*{{{*/ int rc = 0; if ((rc = deallocate_String_vector(&zkvector)) != ZOK) { LOG(ERROR) << "rree node children failed, error = [" << zerror(rc) << "]" << std::endl; return false; } /* #ifndef NDEBUG LOG(INFO) << "free node children successful, count = [" << zkvector.count << "]" << std::endl; #endif */ return true; }/*}}}*/
int main(int argc, char *argv[]) { int rc; int fd; int interest; int events; struct timeval tv; fd_set rfds, wfds, efds; if (argc != 2) { fprintf(stderr, "USAGE: %s host:port\n", argv[0]); exit(1); } FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds); zoo_set_debug_level(ZOO_LOG_LEVEL_INFO); zoo_deterministic_conn_order(1); hostPort = argv[1]; zh = zookeeper_init(hostPort, watcher, 30000, &myid, 0, 0); if (!zh) { return errno; } while (1) { zookeeper_interest(zh, &fd, &interest, &tv); usleep(10); if (connected == 1) { struct String_vector str; usleep(10); // watch existence of the node rc = zoo_wget_children(zh, "/testpath1", watchchildren , mycontext, &str); if (ZOK != rc){ printf("Problems %d\n", rc); } else { int i = 0; while (i < str.count) { printf("Children %s\n", str.data[i++]); } if (str.count) { deallocate_String_vector(&str); } } connected++; } if (fd != -1) { if (interest & ZOOKEEPER_READ) { FD_SET(fd, &rfds); } else { FD_CLR(fd, &rfds); } if (interest & ZOOKEEPER_WRITE) { FD_SET(fd, &wfds); } else { FD_CLR(fd, &wfds); } } else { fd = 0; } FD_SET(0, &rfds); rc = select(fd+1, &rfds, &wfds, &efds, &tv); events = 0; if (rc > 0) { if (FD_ISSET(fd, &rfds)) { events |= ZOOKEEPER_READ; } if (FD_ISSET(fd, &wfds)) { events |= ZOOKEEPER_WRITE; } } zookeeper_process(zh, events); } return 0; }
void deallocate_GetChildren2Response(struct GetChildren2Response*v){ deallocate_String_vector(&v->children); deallocate_Stat(&v->stat); }
void deallocate_SetWatches(struct SetWatches*v){ deallocate_String_vector(&v->dataWatches); deallocate_String_vector(&v->existWatches); deallocate_String_vector(&v->childWatches); }
void workers_init (workers_t ** workers, ozookeeper_t * ozookeeper) { int result; char path[1000]; char octopus[1000]; char comp_name[1000]; oconfig_octopus (ozookeeper->config, octopus); oconfig_comp_name (ozookeeper->config, comp_name); sprintf (path, "/%s/computers/%s/worker_nodes", octopus, comp_name); struct String_vector worker_children; result = zoo_get_children (ozookeeper->zh, path, 0, &worker_children); if (ZOK == result) { //mallocing *workers = malloc (sizeof (workers_t)); (*workers)->size = worker_children.count; (*workers)->id = malloc (sizeof (char *) * (worker_children.count)); (*workers)->pthread = malloc (sizeof (pthread_t) * worker_children.count); //create the threads //localdb object //used to save the counter used to create new vertices localdb_t *localdb; localdb_init (&localdb); int iter; if (worker_children.count < 1000) { worker_t *worker; for (iter = 0; iter < worker_children.count; iter++) { (*workers)->id[iter] = malloc (strlen (worker_children.data[iter]) + 1 + 1 + strlen (comp_name)); sprintf ((*workers)->id[iter], "%s/%s", comp_name, worker_children.data[iter]); worker_init (&worker, ozookeeper->zh, ozookeeper->config, comp_name, worker_children.data[iter] , localdb); pthread_create (&((*workers)->pthread[iter]), NULL, worker_fn, worker); } } else { fprintf (stderr, "\n More workers than allowed.. error exiting"); exit (1); } if (ZOK != result && ZNONODE != result) { fprintf (stderr, "\n Couldnt get the children.. error exiting"); exit (1); } deallocate_String_vector (&worker_children); } }