示例#1
0
void choose_mater(zhandle_t *zkhandle,const char *group_path,const char *self_name)
{
    struct String_vector procs;
    int i = 0;
    int ret = zoo_wget_children(zkhandle,group_path,zkbalance_watcher_g,NULL,&procs);
        
    if(ret != ZOK || procs.count == 0){
        fprintf(stderr,"failed to get the children of path %s!\n",group_path);
    }else{
        char master_name[512]={0};
        
        strcpy(master_name,procs.data[0]);

        for(i = 1; i < procs.count; ++i){
            if(strcmp(master_name,procs.data[i])>0){
                strcpy(master_name,procs.data[i]);
            }
        }

        if(strcmp(master_name,self_name) == 0){
            g_mode = MASTER_MODE;
        }else{
            g_mode = SLAVE_MODE;
        }
        
    }

    for(i = 0; i < procs.count; ++i){
        free(procs.data[i]);
        procs.data[i] = NULL;
    }

}
示例#2
0
int start_zookeeper(view* cur_view, int *zfd, pthread_mutex_t *lock)
{
	int rc;
	char zoo_host_port[32];
	sprintf(zoo_host_port, "localhost:%d", zoo_port);
	zh = zookeeper_init(zoo_host_port, zookeeper_init_watcher, 15000, 0, 0, 0);

    while(is_connected != 1);
    int interest, fd;
    struct timeval tv;
    zookeeper_interest(zh, &fd, &interest, &tv);
    *zfd = fd;

    char path_buffer[512];
    rc = zoo_create(zh, "/election/guid-n_", NULL, -1, &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE|ZOO_EPHEMERAL, path_buffer, 512);
    if (rc)
    {
        fprintf(stderr, "Error %d for zoo_create\n", rc);
    }
    char znode_path[512];
    get_znode_path(path_buffer, znode_path);

    check_leader(cur_view, path_buffer);
    struct watcherContext *watcherPara = (struct watcherContext *)malloc(sizeof(struct watcherContext));
    strcpy(watcherPara->znode_path, znode_path);
    watcherPara->lock = lock;
    watcherPara->cur_view = cur_view;

    rc = zoo_wget_children(zh, "/election", zoo_wget_children_watcher, (void*)watcherPara, NULL);
    if (rc)
    {
        fprintf(stderr, "Error %d for zoo_wget_children\n", rc);
    }
    return 0;
}
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);
        }
    }
}
示例#4
0
void completion_fn_enter(int rc, const struct String_vector *strings, const void* data)
{
	int ret = 0;
	int i = 0;
	struct String_vector *strNew;
	printf("completion_fn_enter count: %d\n", strings->count);
	if(strings->count < 3)
	{
		//ret = zoo_aget_children(g_zhdl, g_root, false, completion_fn_enter, 
		//	"root child node");
		printf("completion_fn_enter set watcher\n");
		ret = zoo_wget_children(g_zhdl, g_root, watcher_fn_znode, NULL, strNew);
		if(ret != ZOK)
		{
			printf("completion_fn_enter set watcher error\n");
		}
	}
	else
	{
		for(i = 0; i < strings->count; i++)
		{
			printf("completion_fn_enter node: %s\n", strings->data[i]);
		}
		g_enterFlag = 0;
	}
}
示例#5
0
void cached_zk::list_(const string& path, std::set<std::string>& out)
{
  out.clear();
  struct String_vector s;
  int rc = zoo_wget_children(zh_, path.c_str(), cached_zk::update_cache, this, &s);

  if (rc == ZOK) {
    std::set<string> cache;
    for (int i=0; i<s.count; ++i) {
      out.insert(s.data[i]);
    }
  } else {
    LOG(ERROR) << zerror(rc) << " (" << path << ")";
  }
}
示例#6
0
void zoo_wget_children_watcher(zhandle_t *wzh, int type, int state, const char *path, void *watcherCtx) {
    if (type == ZOO_CHILD_EVENT)
    {
        int rc;
        struct watcherContext *watcherPara = (struct watcherContext*)watcherCtx;
        // block the threads
        pthread_mutex_lock(watcherPara->lock);
        rc = zoo_wget_children(zh, "/election", zoo_wget_children_watcher, watcherCtx, NULL);
        if (rc)
        {
            fprintf(stderr, "Error %d for zoo_wget_children\n", rc);
        }
        check_leader(watcherPara->cur_view, watcherPara->znode_path);
        pthread_mutex_unlock(watcherPara->lock); 
    }
}
示例#7
0
bool cached_zk::list_(const string& path, std::set<std::string>& out) {
  out.clear();
  struct String_vector s;
  int rc = zoo_wget_children(zh_, path.c_str(), cached_zk::update_cache, this,
                             &s);

  if (rc == ZOK) {
    for (int i = 0; i < s.count; ++i) {
      out.insert(s.data[i]);
    }
    return true;
  } else {
    LOG(ERROR) << "failed to get children: " << path << " - " << zerror(rc);
    return false;
  }
}
示例#8
0
void completion_fn_leave(int rc, const struct String_vector *strings, const void* data)
{
	int ret = 0;
	struct String_vector *strNew;
	printf("completion_fn_leave count: %d\n", strings->count);
	if(strings->count > 0)
	{
		//ret = zoo_aget_children(g_zhdl, g_root, false, completion_fn_leave, 
		//	"root child node");
		ret = zoo_wget_children(g_zhdl, g_root, watcher_fn_znode_leave, NULL, strNew);
		if(ret != ZOK)
		{
			printf("completion_fn_leave set watcher error\n");
		}
	}
	else
	{
		g_leaveFlag = 0;
	}
}
示例#9
0
文件: MasterHandler.cpp 项目: 40a/BFS
bool MasterHandler::cleanAssignmentFolder() {
  String_vector children;
  int callResult = zoo_wget_children(ZooHandler::getInstance().zh,
      ZooHandler::getInstance().assignmentZNode.c_str(),
      nullptr,nullptr, &children);
  if (callResult != ZOK) {
    LOG(ERROR)<<"zoo_wget_children failed:"<<zerror(callResult);
    return false;
  }

  zoo_op_t *opArr = new zoo_op[children.count];
  for(int i = 0;i<children.count;i++) {
    zoo_op_t op;
    string path = ZooHandler::getInstance().assignmentZNode + "/" +string(children.data[i]);
    zoo_delete_op_init(&op,path.c_str(),-1);//No versioning
    opArr[i] = op;
  }

/*  for(int i=0;i<children.count;i++)
    fprintf(stderr, "removing:%s\n",opArr[i].delete_op.path);*/

  zoo_op_result_t *results = new zoo_op_result_t[children.count];

  //int res = zoo_multi(ZooHandler::getInstance().zh,children.count,opArr,results);
  for(int i = 0;i<children.count;i++) {
    string path = ZooHandler::getInstance().assignmentZNode + "/" +string(children.data[i]);
    int res = zoo_delete(ZooHandler::getInstance().zh,path.c_str(),-1);
    if(res != ZOK)
      return false;
  }

  delete []opArr;
  delete []results;
  //return res == ZOK;
  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;
}
示例#11
0
int set_balancing_strategy(zhandle_t *zkhandle,char *group_path,char *breaker_topic_path)
{   
    if(g_mode != MASTER_MODE){
       return 0;
    }
    struct String_vector partitions;
    struct String_vector clients;
    
    int ret = zoo_wget_children(zkhandle,breaker_topic_path,zkbalance_watcher_g,NULL,&partitions);
    if(ret != ZOK){
        fprintf(stderr,"error when get children of path %s.\n",breaker_topic_path); 
        return ret;
    }
    
    ret = zoo_wget_children(zkhandle,group_path,zkbalance_watcher_g,NULL,&clients);
    if(ret != ZOK){
        fprintf(stderr,"error when get children of path %s.\n",group_path); 
        return ret;
    }

    int i = 0;
    char client_path[512]={0};
    char buffer[1024] = {0};

    struct String_vector values;
    if(clients.count){
        values.count = clients.count < partitions.count ? clients.count : partitions.count;

        values.data = calloc(sizeof(*values.data),values.count);
        memset(values.data,0,sizeof(*values.data)*values.count);
    }else{
        values.count = 0;
        values.data = NULL;
    }
    printf("values.count = %d\n",values.count);

    for(i = 0; i < partitions.count; ++i){
        int k = i % clients.count;
        if (values.data[k] == NULL){
            sprintf(buffer,"%d",i); 
            values.data[k] = strdup(buffer);

        }else {
            sprintf(buffer,"%s,%d",values.data[k],i);
            free(values.data[k]);
            values.data[k] = strdup(buffer);
        }
        printf("%d = %s\n",k,values.data[k]);
    }
    for(i = 0; i < values.count; ++i){    
        sprintf(client_path,"/Consumer/%s/group-%d/%s",g_topic,g_groupid,clients.data[i%clients.count]);
        
        ret = zoo_set(zkhandle,client_path,values.data[i],strlen(values.data[i]),-1);
        if(ret != ZOK){
            continue;
        }
        printf("%s = %s\n",client_path,values.data[i]);
        
        free(values.data[i]);
    }
    //多余的consumer将收不到分区消息
    for(i = values.count; i < clients.count; ++i){
        sprintf(client_path,"/Consumer/%s/group-%d/%s",g_topic,g_groupid,clients.data[i%clients.count]);
        
        ret = zoo_set(zkhandle,client_path,"",0,-1);
        if(ret != ZOK){
            continue;
        }
        printf("%s = %s\n",client_path,"");

    }
}