Пример #1
0
std::pair<bool, std::string> get_node_value(zhandle_t* handle, 
                                            const std::string& node,
                                            const std::string& stat_message) {
  char buffer[1024];
  int length = 1024;
  int stat = zoo_get(handle, node.c_str(), 0, buffer, &length, NULL);
  if (stat != ZOK) print_stat(stat, stat_message + " get_node_value", node);
  if (stat != ZOK) return std::pair<bool, std::string>(false, "");

  // we are good here
  if (length <= 1024) {
    // ok. it fit inside the buffer
    // we can return
    if (length < 0) return std::pair<bool, std::string>(true, "");
    else return std::pair<bool, std::string>(true, std::string(buffer, length));
  }
  else {
    while(1) {
      // buffer not long enough. The length parameter constains the actual length 
      // try again. keep looping until we succeed
      char* newbuffer = new char[length]; 
      int stat = zoo_get(handle, node.c_str(), 0, newbuffer, &length, NULL);
      if (stat != ZOK) print_stat(stat, stat_message + " get_node_value", node);
      std::string retval(newbuffer, length);
      delete newbuffer;

      if (stat != ZOK) print_stat(stat, stat_message + " get_node_value", node);
      if (stat != ZOK) return std::pair<bool, std::string>(false, "");
      if (length < 0) return std::pair<bool, std::string>(true, "");
      else return std::pair<bool, std::string>(true, retval);
    }   
  }
}
Пример #2
0
void do_work(zhandle_t *zkhandle)
{
    char group_path[512]={0};
    char breaker_topic_path[512]={0};

    sprintf(group_path,"/Consumer/%s/group-%d",g_topic,g_groupid);
    sprintf(breaker_topic_path,"/Breaker/%s",g_topic);

    if(g_mode == MASTER_MODE){
        set_balancing_strategy(zkhandle,group_path,breaker_topic_path);
    }else{
        //todo
        char str_partitions[512]={0};
        int  len = sizeof(str_partitions);
        int ret = zoo_get(zkhandle,g_my_path,zkget_watcher_g,"",str_partitions,&len,NULL);

        if(ret != ZOK){
            fprintf(stderr,"failed to get data of the path %s.\n",g_my_path);
        }else{
            struct String_vector partitions;
            
            convert_str_to_vector(str_partitions,",",&partitions);
            int i = 0;
            for(i = 0; i < partitions.count; ++i){
                sprintf(msg_path,"/Breaker/%s/Partition-%s",g_topic,partitions.data[i]);
                
            }
        }
        
    }

} 
Пример #3
0
	int getServers(zhandle_t* zh, const string& path, int watcher, vector<ServerNode>& serverNodes){
		struct String_vector children;
		int result = zoo_get_children(zh, path.c_str(), watcher, &children);
		if(result != ZOK){
			return result;
		}

		char buffer[512];
		int bufferLen = 512;
		struct Stat stat;

		for(int i = 0; i < children.count; i++){
			bufferLen = 512;
			string serverName(children.data[i]);
			size_t p = serverName.find('-');
			if(p == string::npos){
				continue;
			}
			ServerNode node;
			node.index = boost::lexical_cast<int>(serverName.substr(p + 1));
			
			string nodePath = path + '/' + serverName;
			result = zoo_get(zh, nodePath.c_str(), 0, buffer, &bufferLen, &stat);
			if(result != ZOK){
				return result;
			}

			node.address.append(buffer, bufferLen);
			serverNodes.push_back(node);
		}

		sort(serverNodes.begin(), serverNodes.end());
		return ZOK;
	}
bool ZKOP::zkGetNodeData(const std::string& node, std::string& data, int size, int watch) 
{/*{{{*/

	data.resize(size);

	int rc = 0;
	if ((rc = zoo_get(zkhandle, node.data(), watch, &data[0], &size, NULL)) != ZOK) {
		LOG(ERROR) << "get node data failed, node = ["
			<< node << "], error = [" << zerror(rc) << "]" << std::endl;
		return false;
	}

	do {
		if (size < 0) {
			data.resize(0);
			break;
		}   
		data.resize(size);
	} while (0);

/*
#ifndef NDEBUG
	LOG(INFO) << "get node data successful, node = [" << node
		<< "], data = [" << data << "], size = [" << size << "]" << std::endl;
#endif
*/

	return true;
}/*}}}*/
Пример #5
0
 int Cluster::FetchClusterTopo()
 {
     std::string partitions_path = "/" + g_db->GetConf().cluster_name + "/partitions";
     std::string nodes_path = "/" + g_db->GetConf().cluster_name + "/nodes";
     char bufer[2028];
     int err = zoo_wget(m_zk, nodes_path.c_str(), ZKGetWatchCallback, this, bufer, 0, NULL);
     err = zoo_get(m_zk, partitions_path.c_str(), 0, bufer, 0, NULL);
     return -1;
 }
Пример #6
0
Error_Info HBAgent::readZookeeper(){
          Error_Info err; 
	  string zk_snap;
	  const string ZK_SNAP="/wde/gbase/data/snapshot";
          zhandle_t * zh;
	  bool pingpong=false;
          zh=zookeeper_init(zookeeperAddress.c_str(),watcher, 10000, 0, &pingpong, 0);                              
          char buffer[8192];
          int buflen=sizeof(buffer);
          struct Stat stat;
 	  if(zh){
 	  	int rc;
		int sleeptimes=0;
		while(!pingpong){
			sleep(1);
			sleeptimes++;
			if(sleeptimes==10){
				err.code=-2;
				err.description="Can't Connected to Zookeeper: "+zookeeperAddress;
				return err;
			}
		}
		if(zoo_state(zh)!=ZOO_CONNECTED_STATE){
			err.code=HB_READZOOKEEPER_FAIL; 			err.description="Can't Connected to Zookeeper: "+zookeeperAddress;


			return err;
		}
          	rc=zoo_get(zh,ZK_SNAP.c_str(),0,buffer,&buflen,&stat);
		if(rc==ZOK){
          		zk_snap=string(buffer,buflen);
		}else{
			err.code=HB_BADZOOKEEPER_CONFIG;
			err.description="get snapshot from zookeeper fail";
			return err;
		}
          }else{
	      err.code=HB_READZOOKEEPER_FAIL;
              err.description="zookeeper init fail";
	      return err;
          }
          zookeeper_close(zh);
	  boost::replace_all(zk_snap,"\'","\"");
	  Json::Value root;
	  Json::Reader reader;
	  bool parseSuccess=reader.parse(zk_snap,root);
	  if(!parseSuccess){
		err.code=HB_READZOOKEEPER_FAIL;
		err.description="Bad Zookpeer Value for snapshot in Zookeeper";
	  }
	  collection=root["name"].asString();
	  columnfamily="contents";
	  err.code=HB_OK;
	  err.description="zookeeper init success";
	  return err;
}           
Пример #7
0
void test_watcher(zhandle_t *zh, int type,
        int state, const char *path,void *watcherCtx) {
	char buf[512] = {0};
	int buflen = 512;
	zhandle_t *h = (zhandle_t *)watcherCtx;
	printf("triggered watcher type[%d] stat[%d] path[%s]\n",
			type, state, path);
	if (ZOO_CONNECTED_STATE == state) {
		zoo_get(zh, "/root", 1, buf, &buflen, NULL);
		printf("/root: %s\n", buf);
	}
}
Пример #8
0
static void*
item_expire_thread(void *arg)
{
    zhandle_t* zkhandle = (zhandle_t*)arg;
    int timeout = 30;
    struct String_vector strings;
    int ret;
    time_t now;

    fprintf(stderr, "item expire thread start up!\n");

    while(1) {
        if (i_am_leader) {
            /* clear the expired items */
            ret = zoo_get_children(zkhandle, "/ccs/employee_info_tab", 0,
                                   &strings);
            if (ret != 0) {
                fprintf(stderr, "zoo_get_children error [%d]\n", ret);
            }
            time(&now);

            for (int i = 0; i < strings.count; i++) {
                char path[128];
                memset(path, 0, sizeof(path));
                char value[128] = {0};
                int value_len = sizeof(value);
                struct Stat item_stat;

                sprintf(path, "/ccs/employee_info_tab/%s", strings.data[i]);
                ret = zoo_get(zkhandle, path, 0, value, &value_len, &item_stat);
                if (ret != 0) {
                    fprintf(stderr, "zoo_get error [%d]\n", ret);
                    continue;
                }

                if ((item_stat.ctime/1000) < now - 30) {
                    ret = zoo_delete(zkhandle, path, -1);
                    if (ret != 0) {
                        fprintf(stderr, "zoo_delete error [%d]\n", ret);
                        continue;
                    }
                    printf("[expire]: employee_info_tab: expire [%s]\n", strings.data[i]);
                } else {
                    printf("[expire]: employee_info_tab: skip [%s]\n", strings.data[i]);
                }

            }
        } 
        sleep(timeout);
    }

}
Пример #9
0
bool zk::read(const string& path, string& out) {
  scoped_lock lk(m_);
  char buf[1024];
  int buflen = 1024;
  int rc = zoo_get(zh_, path.c_str(), 0, buf, &buflen, NULL);
  if (rc == ZOK) {
    out = string(buf, buflen);
    return buflen <= 1024;
  } else {
    LOG(ERROR) << "failed to get data: " << path << " - " << zerror(rc);
    return false;
  }
}
Пример #10
0
//同步方式获取节点数据
void get(zhandle_t* zkhandle)
{
    printf("同步方式获取节点数据-----------------------\n");
    char buffer1[64];
    int bufferlen1=sizeof(buffer1);

    int flag1=zoo_get(zkhandle,"/xyz3",0,
                      buffer1,&bufferlen1,NULL);
    if (flag1 ==ZOK)
    {
        printf("节点/xyz3的数据为: %s\n",buffer1);
    }
}
Пример #11
0
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);
	}
}
Пример #12
0
int main(int argc, char **argv) {
  char buffer[512];
  char p[2048];
  char *cert=0;
  char appId[64];

  strcpy(appId, "example.foo_test");
  cert = foo_get_cert_once(appId);
  if(cert!=0) {
    fprintf(stderr, "Certificate for appid [%s] is [%s]\n",appId,cert);
    strncpy(p,cert, sizeof(p)-1);
    free(cert);
  } else {
    fprintf(stderr, "Certificate for appid [%s] not found\n",appId);
    strcpy(p, "dummy");
  }

  zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);

  zh = zookeeper_init("localhost:2181", watcher, 10000, 0, 0, 0);
  if (!zh) {
	fprintf(stderr, "Error1: %s\n", strerror(errno));
    return errno;
  }
  if (zoo_add_auth(zh, "foo", p, strlen(p), 0, 0) != ZOK) {
    fprintf(stderr, "Error2: %s\n", strerror(errno));
    return 2;
  }

  struct ACL CREATE_ONLY_ACL[] = {{ZOO_PERM_CREATE, ZOO_AUTH_IDS}};
  struct ACL_vector CREATE_ONLY = {1, CREATE_ONLY_ACL};
  int rc = zoo_create(zh, "/xyz", "value", 5, &CREATE_ONLY, ZOO_EPHEMERAL, (char*)&buffer, sizeof(buffer) - 1);
  if (!rc) {
    fprintf(stderr, "Error3: %s\n", strerror(errno));
    return errno;
  }

  /** this operation will fail with a ZNOAUTH error */
  int buflen= sizeof(buffer);
  struct Stat stat;
  rc = zoo_get(zh, "/xyz", 0, buffer, &buflen, &stat);
  if (rc) {
    fprintf(stderr, "Error4: %d for %s\n", rc, __LINE__);
  } else {
	fprintf(stdout, "Got %s\n", (char*)&buffer);
  }

  zookeeper_close(zh);
  return 0;
}
Пример #13
0
enum zoo_res zu_create_ephemeral(struct zoodis *z)
{
    int res;
    int bufsize = 512;
    char buffer[bufsize]; // no reason, added due to some of example, need to be done.
    int buffer_len = bufsize;
    memset(buffer, 0x00, bufsize);

    res = zoo_get(z->zh, z->zoo_nodepath->data, 0, buffer, &buffer_len, 0);

    if(res == ZOK)
    {
        if(buffer_len == z->zoo_nodedata->len && strncmp(z->zoo_nodedata->data, buffer, z->zoo_nodedata->len) == 0)
        {
            return ZOO_RES_OK;
        }else
        {
            zu_remove_ephemeral(z);
        }
    }else if(res != ZOK && res != ZNONODE)
    {
        ZU_RETURN_PRINT(res);
        if(res == ZINVALIDSTATE)
        {
            zookeeper_close(z->zh);
            zoodis.zoo_stat = ZOO_STAT_NOT_CONNECTED;
            z->zid = NULL;
            log_warn("Zookeeper: error, trying to re-connect.");
            zu_connect(z);
        }
        // exit_proc(-1);
    }

    res = zoo_create(z->zh, z->zoo_nodepath->data, z->zoo_nodedata->data, strlen(z->zoo_nodedata->data), &ZOO_READ_ACL_UNSAFE, ZOO_EPHEMERAL, buffer, sizeof(buffer)-1);
    if(res != ZOK)
    {
        ZU_RETURN_PRINT(res);
        if(res == ZINVALIDSTATE)
        {
            zookeeper_close(z->zh);
            zoodis.zoo_stat = ZOO_STAT_NOT_CONNECTED;
            z->zid = NULL;
            log_warn("Zookeeper: error, trying to re-connect.");
            zu_connect(z);
        }
    }

    return ZOO_RES_OK;
}
Пример #14
0
vector<BackendItem> MasterHandler::getExistingAssignments() {
	vector<BackendItem> result;
	if (ZooHandler::getInstance().sessionState != ZOO_CONNECTED_STATE) {
		LOG(ERROR)<<"Invalid sessionstate";
		return result;
	}

	//1)get list of (assignmentznode)/BFSElection children and set a watch for changes in these folder
	String_vector children;
	int callResult = zoo_get_children(ZooHandler::getInstance().zh, ZooHandler::getInstance().assignmentZNode.c_str(),0, &children);
	if (callResult != ZOK) {
		LOG(ERROR)<<"zoo_get_children failed:"<<zerror(callResult);
		return result;
	}
	//2)get content of each node
	for (int i = 0; i < children.count; i++) {
		string node(children.data[i]);
		//Allocate 1MB data
		const int length = 1024 * 1024;
		char *buffer = new char[length];
		int len = length;
		int callResult =
					zoo_get(ZooHandler::getInstance().zh,
						(ZooHandler::getInstance().assignmentZNode + "/" + node).c_str(),
						0, buffer, &len, nullptr);
		if (callResult != ZOK) {
			LOG(ERROR)<<"zoo_get failed:"<<zerror(callResult);
			delete[] buffer;
			buffer = nullptr;
			continue;
		}
		if(len >= 0 && len <= length-1)
			buffer[len] = '\0';

		//3)parse node content to a znode
		char *tok = strtok(buffer, "\n");
		while (tok != NULL) {
			string file(tok);
			result.push_back(BackendItem(file,-1l,"",""));
			tok = strtok(NULL, "\n");
		}
		//Release memory
		delete[] buffer;
		buffer = nullptr;
	}

	return result;
}
Пример #15
0
static VALUE method_get(VALUE self, VALUE path) {
  char data[1024];
  int data_len = sizeof(data);

  struct Stat stat;
  memset(data, 0, sizeof(data));

  Check_Type(path, T_STRING);
  FETCH_DATA_PTR(self, zk);
  
  check_errors(zoo_get(zk->zh, RSTRING(path)->ptr, 0, data, &data_len, &stat));

  return rb_ary_new3(2,
		     rb_str_new(data, data_len),
		     array_from_stat(&stat));
}
Пример #16
0
static VALUE method_get(VALUE self, VALUE path) {
  struct zk_rb_data* zk;
  char data[1024];
  int data_len = 1024;
  struct Stat stat;

  Check_Type(path, T_STRING);
  Data_Get_Struct(rb_iv_get(self, "@data"), struct zk_rb_data, zk);
  
  check_errors(zoo_get(zk->zh, RSTRING(path)->ptr, 0, data, &data_len, &stat));
  /*printf("got some data; version=%d\n", stat.version);*/

  return rb_ary_new3(2,
		     rb_str_new(data, data_len),
		     array_from_stat(&stat));
}
Пример #17
0
static VALUE method_get(VALUE self, VALUE reqid, VALUE path, VALUE async, VALUE watch) {
  char * data ;
  int data_len = MAX_ZNODE_SIZE;
  struct Stat stat;
  int rc;
  VALUE output ;
  STANDARD_PREAMBLE(self, zk, reqid, path, async, watch, data_ctx, watch_ctx, call_type);

  /* ugh */
  data = malloc(MAX_ZNODE_SIZE);


  switch (call_type) {
    case SYNC:
      rc = zoo_get(zk->zh, RSTRING_PTR(path), 0, data, &data_len, &stat);
      break;

    case SYNC_WATCH:
      rc = zoo_wget(zk->zh, RSTRING_PTR(path), zkrb_state_callback, watch_ctx, data, &data_len, &stat);
      break;

    case ASYNC:
      rc = zoo_aget(zk->zh, RSTRING_PTR(path), 0, zkrb_data_callback, data_ctx);
      break;

    case ASYNC_WATCH:
      rc = zoo_awget(zk->zh, RSTRING_PTR(path), zkrb_state_callback, watch_ctx, zkrb_data_callback, data_ctx);
      break;
  }

  output = rb_ary_new();
  rb_ary_push(output, INT2FIX(rc));
  if (IS_SYNC(call_type) && rc == ZOK) {
    if (data_len == -1)
      rb_ary_push(output, Qnil);        /* No data associated with path */
    else
      rb_ary_push(output, rb_str_new(data, data_len));
    rb_ary_push(output, zkrb_stat_to_rarray(&stat));
  }
  free(data);

  return output;
}
Пример #18
0
string GetDataBuilderImpl::forPath(string path, int length) {
    struct Stat *s = NULL;
    if (this->stat != NULL) {
        s = this->stat;
    }
    char buff[length];
    if (watcherFn == NULL) {
        zoo_get(zk, path.c_str(), 0, buff, &length, s);
    } else {
        zoo_wget(zk, path.c_str(), watcherFn, watcherCtx, buff, &length, s);
    }


    if (length == -1) {
        return string();
    } else {
        return string(buff, length);
    }
}
void ZKWrapper::getDataByPath(const std::string& strPath, std::string& strData)
{
	std::cout << "[Get data in sync mode...]\n";
	char buffer[64];
	int bufferLen = sizeof(buffer);
	memset(buffer, 0, bufferLen);

	//1 implies that watcher is enabled.
	int flag = zoo_get(m_zkHandler, 
			strPath.c_str(), 1,
			buffer, &bufferLen, NULL
			);
	if (flag == ZOK)
	{
		strData.assign(buffer, bufferLen);
	} 
	else 
	{
		fprintf(stderr, "ERROR when getting data of node '%s'\n", strPath.c_str());
	}
}
Пример #20
0
void printChildren() {
        String_vector children;
        zoo_get_children(zh, "/test", 1, &children);

        char fullPath[1024];
        int result = -1;
        printf("==================== children of /test ==================\n");
        for (int i = 0; i < children.count; i++) {
                sprintf(fullPath, "/test/%s", children.data[i]);
                Stat stat;
                char *resultData;
                int resultLenth;
                result = zoo_get(zh, fullPath, 0, resultData, &resultLenth, &stat);

                if (result != ZOK) {
                        printf("Get Error:%s,%s", fullPath, zerror(result));
                        continue;
                }

                printf("%s\n", children.data[i]);
        }
}
Пример #21
0
int main(int argc, char** argv)
{
	char buffer[512];
	char p[2048];
	char *cert=0;
	char appId[64];
	strcpy(p, "dummy");
	zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
	zh = zookeeper_init("localhost:2181", watcher, 10000,0,0,0);
	while(zoo_state(zh) == 0);
	if(zoo_add_auth(zh,"digest",p,strlen(p),0,0) != ZOK)
	{
		printf("auto failed\n");
		return 2;
	}

	printf("auto success\n");
	struct ACL CREATE_ONLY_ACL[] = {{ZOO_PERM_CREATE, ZOO_AUTH_IDS}};
	struct ACL_vector CREATE_ONLY = {1, CREATE_ONLY_ACL};
	int rc = zoo_create(zh, "/zookeeper/xyz","value",5,&ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL,buffer, sizeof(buffer) -1);

	int buflen = sizeof(buffer);
	struct Stat stat;
	while(true)
	{
		rc = zoo_get(zh, "/zookeeper/xyz", 0, buffer, &buflen, &stat);
		if(rc)
		{
			fprintf(stderr, "Error %d, for %d\n",rc, __LINE__);
		}

		printf("%s", buffer);
		sleep(5);
	}
	zookeeper_close(zh);
	return 0;
}
Пример #22
0
int zkServer::zkCreate(const char *data, char *path_buffer, int path_buffer_len)
{
    int ret = 0;
    if(NULL == zkhandle)
    {
        ERROR_PRINT(" zkhandle is NULL ", "");
        return -1;
    }

    DEBUG_PRINT("root:%s server_name:%s prefix:%s ", root.c_str(), server_name.c_str(),prefix.c_str());
    ret = zoo_create(zkhandle, std::string(root + server_name + prefix).c_str(), data, strlen(data), 
            &ZOO_OPEN_ACL_UNSAFE, 
            ZOO_EPHEMERAL|ZOO_SEQUENCE, 
            path_buffer, 
            path_buffer_len);

    if (ret) {
        ERROR_PRINT(" error %d of %s ", ret, "create node");
	return -2;
    }
    else 
    {
        char node[PATH_BUFFER_LEN] = {0};
        get_node_name(path_buffer, node);
        node_name = std::string(node);
        DEBUG_PRINT("path_buffer:%s, node_name:%s", path_buffer, node_name.c_str());
        
        /* get data from znode */
        char node_value[PATH_BUFFER_LEN] = {0};
        int value_len = PATH_BUFFER_LEN;
        ret = zoo_get(zkhandle, path_buffer, 0, node_value, &value_len, NULL);
        DEBUG_PRINT("node data:%s node_name:%s", node_value, node_name.c_str());
    }

    return ret;
}
Пример #23
0
static int check_leader(view* cur_view, char *znode_path)
{
    int rc, i, zoo_data_len = ZDATALEN;
    char str[64];

    sprintf(str, "%"PRIu32",%"PRIu64"", myid, SRV_DATA->log->tail);
    rc = zoo_set(zh, znode_path, str, strlen(str), -1);
    if (rc)
    {
        fprintf(stderr, "Error %d for zoo_set\n", rc);
    }
    struct String_vector *children_list = (struct String_vector *)malloc(sizeof(struct String_vector));
    rc = zoo_get_children(zh, "/election", 0, children_list);
    if (rc)
    {
        fprintf(stderr, "Error %d for zoo_get_children\n", rc);
    }
    char *p;
    struct znodes_data znodes[MAX_SERVER_COUNT];

    for (i = 0; i < children_list->count; ++i)
    {
        char *zoo_data = malloc(ZDATALEN * sizeof(char));
        char zpath[64];
        get_znode_path(children_list->data[i], zpath);
        rc = zoo_get(zh, zpath, 0, zoo_data, &zoo_data_len, NULL);
        if (rc)
        {
            fprintf(stderr, "Error %d for zoo_get\n", rc);
        }
        if (*zoo_data == 'n')
        {
            znodes[i].node_id = 9999;
            znodes[i].tail = SRV_DATA->log->len;
        } else{
            p = strtok(zoo_data, ",");
            znodes[i].node_id = atoi(p);
            p = strtok(NULL, ",");
            znodes[i].tail = atoi(p);
        }
        strcpy(znodes[i].znode_path, zpath);
        free(zoo_data);
    }
    qsort((void*)&znodes, children_list->count, sizeof(struct znodes_data), (compfn)compare_tail);

    for (i = 1; i < children_list->count; i++)
    {
        if (znodes[i].tail != znodes[0].tail)
            continue;
    }
    int num_max_tail = i;
    qsort((void*)&znodes, num_max_tail, sizeof(struct znodes_data), (compfn)compare_path);    

    cur_view->leader_id = znodes[0].node_id;

    if (cur_view->leader_id == myid)
    {
        fprintf(stderr, "I am the leader\n");
        //recheck
    }else{
        fprintf(stderr, "I am a follower\n");
        // RDMA read
        // update view
        // zoo_set
    }
    free(children_list);
    return 0;
}
Пример #24
0
int main(int argc, char *argv[], char *envp[])
{
	INITSRVRTRC

	CEE_status				sts = CEE_SUCCESS;
	SRVR_INIT_PARAM_Def		initParam;
	DWORD					processId;
	char					tmpString[128];
	char					tmpString2[32];
	char					tmpString3[512];
	CEECFG_Transport		transport;
	CEECFG_TcpPortNumber	portNumber;
	BOOL					retcode;
	IDL_OBJECT_def			srvrObjRef;
	CEECFG_TcpProcessName	TcpProcessName;
	int						TransportTrace = 0;

	CALL_COMP_DOVERS(ndcs,argc,argv);

try
{
	regZnodeName[0] = '\x0';
	zkHost[0] = '\x0';
	zkRootNode[0] = '\x0';

	// Initialize seabed
	int	sbResult;
	char buffer[FILENAME_MAX] = {0};
	bzero(buffer, sizeof(buffer));

	sbResult = file_init_attach(&argc, &argv, true, buffer);
	if(sbResult != XZFIL_ERR_OK){
		exit(3);
	}
	sbResult = file_mon_process_startup(true);
	if(sbResult != XZFIL_ERR_OK){
		exit(3);
	}
	msg_mon_enable_mon_messages(true);
}
catch(SB_Fatal_Excep sbfe)
{
	exit(3);
}

	sigset_t newset, oldset;
	sigemptyset(&newset);
	sigaddset(&newset,SIGQUIT);
	sigaddset(&newset,SIGTERM);
	sigprocmask(SIG_BLOCK,&newset,&oldset);

	processId = GetCurrentProcessId();

	 retcode = getInitParamSrvr(argc, argv, initParam, tmpString, tmpString3);
	retcode = TRUE;

	mxosrvr_init_seabed_trace_dll();
	atexit(mxosrvr_atexit_function);

	// +++ Todo: Duplicating calls here. Should try to persist in srvrGlobal
	MS_Mon_Process_Info_Type  proc_info;
	msg_mon_get_process_info_detail(NULL, &proc_info);
	myNid = proc_info.nid;
	myPid = proc_info.pid;
	myProcName = proc_info.process_name;

	char logNameSuffix[32];
	sprintf( logNameSuffix, "_%d_%d.log", myNid, myPid );
	CommonLogger::instance().initLog4cxx("log4cxx.trafodion.masterexe.config", logNameSuffix);

    if(retcode == FALSE )
   {
//LCOV_EXCL_START
      SendEventMsg(  MSG_SET_SRVR_CONTEXT_FAILED,
                           EVENTLOG_ERROR_TYPE,
                           processId,
                           ODBCMX_SERVER,
                           srvrObjRef,
                           2,
                           tmpString,
                           tmpString3);
      exit(0);
//LCOV_EXCL_STOP
   }

   GTransport.initialize();
   if(GTransport.error != 0 )
   {
//LCOV_EXCL_START
      SendEventMsg(  MSG_SET_SRVR_CONTEXT_FAILED,
                           EVENTLOG_ERROR_TYPE,
                           processId,
                           ODBCMX_SERVER,
                           srvrObjRef,
                           1,
                           GTransport.error_message);
      exit(0);
//LCOV_EXCL_STOP
   }
   chdir(GTransport.myPathname);

   initParam.srvrType = CORE_SRVR;

//LCOV_EXCL_START
   if (initParam.debugFlag & SRVR_DEBUG_BREAK)
   {
        volatile int done = 0;
        while (!done) {
          sleep(10);
        }
   }
//LCOV_EXCL_STOP

	char zkErrStr[2048];
	stringstream zk_ip_port;
//	zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
	if( zkHost[0] == '\x0' && regZnodeName[0] == '\x0' )
	{
		sprintf(zkErrStr, "***** Cannot get Zookeeper properties or registered znode info from startup params");
		SendEventMsg(  MSG_SET_SRVR_CONTEXT_FAILED,
						   EVENTLOG_ERROR_TYPE,
						   processId,
						   ODBCMX_SERVER,
						   srvrObjRef,
						   1,
						   zkErrStr);
		// exit(1);

	}
	else
	{
		zk_ip_port << zkHost;
		sprintf(zkErrStr, "zk_ip_port is: %s", zk_ip_port.str().c_str());
		SendEventMsg(MSG_SERVER_TRACE_INFO, EVENTLOG_INFORMATION_TYPE,
					processId, ODBCMX_SERVER,
					srvrObjRef, 1, zkErrStr);
	}

	if (initParam.debugFlag & SRVR_DEBUG_BREAK)
		zkSessionTimeout = 600;

	zoo_deterministic_conn_order(1); // enable deterministic order
	zh = zookeeper_init(zk_ip_port.str().c_str(), watcher, zkSessionTimeout * 1000, &myid, 0, 0);
	if (zh == 0){
		sprintf(zkErrStr, "***** zookeeper_init() failed for host:port %s",zk_ip_port.str().c_str());
		SendEventMsg(  MSG_SET_SRVR_CONTEXT_FAILED,
						   EVENTLOG_ERROR_TYPE,
						   processId,
						   ODBCMX_SERVER,
						   srvrObjRef,
						   1,
						   zkErrStr);
		// exit(1);
	}

	bool found = false;
	int rc;
	stringstream ss;
	ss.str("");
	ss << zkRootNode << "/dcs/master";
	string dcsMaster(ss.str());
	Stat stat;
	int startPortNum = 0, portRangeNum;
	char masterHostName[MAX_HOST_NAME_LEN];
	char startPort[12], portRange[12], masterTS[24];
	struct String_vector children;
	children.count = 0;
	children.data = NULL;

	// Get the instance ID from registered node
	char *tkn;
	char tmpStr[256];
	strcpy( tmpStr,  regZnodeName );
	tkn = strtok(tmpStr, ":" );			
	if(tkn!=NULL)
		strcpy(hostname,tkn);
	tkn = strtok(NULL, ":" );
	if( tkn != NULL )
		strcpy( instanceId, tkn );
	tkn = strtok(NULL, ":" );
	if( tkn != NULL )
		strcpy( childId, tkn );
	else
		;	// +++ Todo handle error

	while(!found)
	{
		rc = zoo_exists(zh, dcsMaster.c_str(), 0, &stat);
		if( rc == ZNONODE )
			continue;
		else
		if( rc == ZOK )
		{
			rc = zoo_get_children(zh, dcsMaster.c_str(), 0, &children);
			if( children.count > 0 )
			{
				char zknodeName[2048];
				strcpy(zknodeName, children.data[0]);
				tkn = strtok(zknodeName, ":" );
				if( tkn != NULL )
					strcpy( masterHostName, tkn );

				tkn = strtok(NULL, ":" );
				if( tkn != NULL ) {
					strcpy( startPort, tkn );
					startPortNum = atoi(tkn);
				}

				tkn = strtok(NULL, ":" );
				if( tkn != NULL ) {
					strcpy( portRange, tkn );
					portRangeNum = atoi(tkn);
				}

				tkn = strtok(NULL, ":" );
				if( tkn != NULL )
					strcpy( masterTS, tkn );

				free_String_vector(&children);
				found = true;
			}
			else
				continue;
		}
		else	// error
		{
			sprintf(zkErrStr, "***** zoo_exists() for %s failed with error %d",dcsMaster.c_str(), rc);
			SendEventMsg(  MSG_SET_SRVR_CONTEXT_FAILED,
							   EVENTLOG_ERROR_TYPE,
							   processId,
							   ODBCMX_SERVER,
							   srvrObjRef,
							   1,
							   zkErrStr);
			break;
		}
	}

	// Initialize initparam to defaults
	initParam.transport = CEE_TRANSPORT_TCP;	// -T 3
	initParam.majorVersion = 3; 				// -V 3
	// Will need to remove $ZTC0 and NonStopODBC from below
	sprintf( initParam.asSrvrObjRef, "TCP:$ZTC0/%s:NonStopODBC", startPort);	// -A TCP:$ZTC0/52500:NonStopODBC
	// Will need to remove this after we get rid off all existing AS related processing
	sprintf( initParam.ASProcessName, "$MXOAS" );	// -AS $MXOAS
	// Will need to remove this after we get rid off all existing WMS related processing
	sprintf( initParam.QSProcessName, "$ZWMGR" );	// -QS $ZWMGR

	// moved this here from begining of the function
	BUILD_OBJECTREF(initParam.asSrvrObjRef, srvrObjRef, "NonStopODBC", initParam.portNumber);

	ss.str("");
	ss << zkRootNode << "/dcs/servers/registered";
	string dcsRegistered(ss.str());

	char realpath[1024];
	bool zk_error = false;

 	if( found )
	{
		sprintf(zkErrStr, "Found master node in Zookeeper");
		SendEventMsg(MSG_SERVER_TRACE_INFO, EVENTLOG_INFORMATION_TYPE,
					processId, ODBCMX_SERVER,
					srvrObjRef, 1, zkErrStr);

		found = false;
		while(!found)
		{
			rc = zoo_exists(zh, dcsRegistered.c_str(), 0, &stat);
			if( rc == ZNONODE )
				continue;
			else
			if( rc == ZOK )
			{
				int i;
				//This section is the original port finding mechanism.
				//All servers (the herd) start looking for any available port
				//between starting port number+2 through port range max.
				//This is mainly for backward compatability for DcsServers
				//that don't pass PORTMAPTOSECS and PORTBINDTOSECS param
				if(portMapToSecs == -1 && portBindToSecs == -1) {
					for(i = startPortNum+2; i < startPortNum+portRangeNum; i++) {
						if (GTransport.m_listener->verifyPortAvailable("SRVR", i))
							break;
					}

					if( i == startPortNum+portRangeNum )
					{
						zk_error = true;
						sprintf(zkErrStr, "***** No ports free");
						break;
					}
				} else {
					//This section is for new port map params, PORTMAPTOSECS and PORTBINDTOSECS,
					//passed in by DcsServer. DcsMaster writes the port map to data portion of
					//<username>/dcs/servers/registered znode. Wait PORTMAPTOSECS for port map
					//to appear in registered znode. When it appears read it and scan looking for
					//match of instance and child Id.
					long retryTimeout = 500;//.5 second
					long long timeout = JULIANTIMESTAMP();
					bool isPortsMapped = false;
					char *zkData = new char[1000000];
					int zkDataLen = 1000000;
					while(! isPortsMapped) {
						memset(zkData,0,1000000);
						rc = zoo_get(zh, dcsRegistered.c_str(), false, zkData, &zkDataLen, &stat);
						if( rc == ZOK && zkDataLen > 0 ) {
							sprintf(zkErrStr, "DCS port map = %s", zkData);
							SendEventMsg(MSG_SERVER_TRACE_INFO, EVENTLOG_INFORMATION_TYPE,
									processId, ODBCMX_SERVER,
									srvrObjRef, 1, zkErrStr);

							int myInstanceId = atoi(instanceId);
							int myChildId = atoi(childId);

							sprintf(zkErrStr, "Searching for my id (%d:%d) in port map",myInstanceId,myChildId);
							SendEventMsg(MSG_SERVER_TRACE_INFO, EVENTLOG_INFORMATION_TYPE,
									processId, ODBCMX_SERVER,
									srvrObjRef, 1, zkErrStr);

							char portMapInstanceId[8];
							char portMapChildId[8];
							char portMapPortNum[8];
							char* saveptr;
							char* token = strtok_r (zkData,":",&saveptr);
							while (token != NULL)
							{
								if( token != NULL )//instance Id
									strcpy( portMapInstanceId, token );
								token = strtok_r(NULL, ":",&saveptr);
								if( token != NULL )//child id
									strcpy( portMapChildId, token );
								token = strtok_r(NULL, ":",&saveptr);
								if( token != NULL )//port number
									strcpy( portMapPortNum, token );

								int currPortMapInstanceId = atoi(portMapInstanceId);
								int currPortMapChildId = atoi(portMapChildId);
								int currPortMapPortNum = atoi(portMapPortNum);

								if(myInstanceId == currPortMapInstanceId && myChildId == currPortMapChildId) {
									i = currPortMapPortNum;
									sprintf(zkErrStr, "Found my port number = %d in port map", i);
									SendEventMsg(MSG_SERVER_TRACE_INFO, EVENTLOG_INFORMATION_TYPE,
											processId, ODBCMX_SERVER,
											srvrObjRef, 1, zkErrStr);
									break;
								} else {
									token = strtok_r (NULL, ":",&saveptr);
								}
							}

							timeout = JULIANTIMESTAMP();
							bool isAvailable = false;
							while ( isAvailable == false ) {
								if (GTransport.m_listener->verifyPortAvailable("SRVR", i)) {
									isAvailable = true;
								} else {
									if((JULIANTIMESTAMP() - timeout) > (portBindToSecs * 1000000)) {
										sprintf(zkErrStr, "Port bind timeout...exiting");
										zk_error = true;
										break;
									} else {
										sprintf(zkErrStr, "Port = %d is already in use...retrying", i);
										SendEventMsg(MSG_SERVER_TRACE_INFO, EVENTLOG_INFORMATION_TYPE,
												processId, ODBCMX_SERVER,
												srvrObjRef, 1, zkErrStr);
										DELAY(retryTimeout);
									}
								}
							}

							isPortsMapped = true;

						} else {
							if((JULIANTIMESTAMP() - timeout) > (portMapToSecs * 1000000)) {
								sprintf(zkErrStr, "Port map read timeout...exiting");
								zk_error = true;
								break;
							} else {
								sprintf(zkErrStr, "Waiting for port map");
								SendEventMsg(MSG_SERVER_TRACE_INFO, EVENTLOG_INFORMATION_TYPE,
										processId, ODBCMX_SERVER,
										srvrObjRef, 1, zkErrStr);
								DELAY(retryTimeout);
								rc = zoo_exists(zh, dcsRegistered.c_str(), 0, &stat);
							}
						}
					}

					delete[] zkData;
				}

				initParam.portNumber = i;

				stringstream newpath;
				newpath.str("");
				newpath << dcsRegistered.c_str() << "/" << regZnodeName;
//				dcsRegisteredNode.str("");
//				dcsRegisteredNode << dcsRegistered.c_str() << "/" << regZnodeName;
				dcsRegisteredNode = newpath.str();

				ss.str("");
				ss << myPid;
				string pid(ss.str());

				ss.str("");
				ss << "STARTING"
				   << ":"
				   << JULIANTIMESTAMP()
				   << ":"
				   << ":"				// Dialogue ID
				   << myNid
				   << ":"
				   << myPid
				   << ":"
				   << myProcName.c_str()
				   << ":"			   		// Server IP address
				   << ":"					// Server Port
				   << ":"					// Client computer name
				   << ":"					// Client address
				   << ":"					// Client port
				   << ":"					// Client Appl name
				   << ":";

				regSrvrData = ss.str();

				rc = zoo_create(zh, dcsRegisteredNode.c_str(), regSrvrData.c_str(), regSrvrData.length(), &ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL, realpath, sizeof(realpath)-1);
				if( rc != ZOK )
				{
					zk_error = true;
					sprintf(zkErrStr, "***** zoo_create() failed with error %d", rc);
					break;
				}
				found = true;
			}
			else	// error
			{
				zk_error = true;
				sprintf(zkErrStr, "***** zoo_exists() for %s failed with error %d",dcsRegistered.c_str(), rc);
				break;
			}
		}
	}

	if( zk_error ) {
		SendEventMsg(  MSG_SET_SRVR_CONTEXT_FAILED,
					   EVENTLOG_ERROR_TYPE,
					   processId,
					   ODBCMX_SERVER,
					   srvrObjRef,
					   1,
					   zkErrStr);
		exit(1);
	}

//LCOV_EXCL_START
// when a server dies, the MXOAS sends message to CFG. CFG creates the MXOSRVR process
// and passess only one command line atribute: -SQL CLEANUP OBSOLETE VOLATILE TABLES
// It is for cleanup resources (volatile tables).
// Newly created MXOSRVR process executes CLEANUP OBSOLETE VOLATILE TABLES and exits.
   // (This process is not managed by AS!. It is only a helper.

	if (initParam.sql != NULL)
	{
      if (strncmp(initParam.sql, "SELECT COUNT", 12) == 0)
      {
         //You can specify a completion code with any positive value in a PROCESS_STOP_.
         //Negative completion codes are reserved for HP use.
         //Therefore negative codes will return as 1000 + abs(completionCode)
         short completionCode = -1;
         completionCode = SQL_EXECDIRECT_FETCH(&initParam);

         if (completionCode < 0)
            completionCode = 1000 + abs(completionCode);

#ifdef NSK_PLATFORM
         PROCESS_STOP_(,,,completionCode,,,,);
#else
		 /*
		  * TODO:
		  * need to revisit this logic to return a value via exit code
		  *
		  */
#endif
      }
      else
      {
Пример #25
0
int main()
{
  char buffer[512];
  char p[2048];
  char *cert=0;
  char appId[64];
 
  strcpy(appId, "example.foo_test");
  cert = foo_get_cert_once(appId);
  if(cert!=0) {
    fprintf(stderr,
            "Certificate for appid [%s] is [%s]\n",appId,cert);
    strncpy(p,cert, sizeof(p)-1);
    free(cert);
  } else {
    fprintf(stderr, "Certificate for appid [%s] not found\n",appId);
    strcpy(p, "dummy");
  }
 
  zoo_set_debug_level(ZOO_LOG_LEVEL_ERROR);
 
  zh = zookeeper_init("127.0.0.1:2181", watcher, 10000, 0, 0, 0);
  zh2 = zookeeper_init("127.0.0.1:2181", watcher, 10000, 0, 0, 0);
  // zookeeper_init是一个异步函数需要等待zh->state非0
  while(zoo_state(zh) == 0) {}
  while(zoo_state(zh2) == 0) {}

// struct Id id_tmp = {(char*)"digest", (char*)"user:tpUq/4Pn5A64fVZyQ0gOJ8ZWqkY="};

  int rc = zoo_add_auth(zh,"digest", "dbscale:dbscale",strlen("dbscale:dbscale"),0,0);
  if (rc == ZOK){
    printf("zoo add auth sucess\n");
  } else {
    printf("zoo add auth failed\n");
  }
 // struct ACL CREATE_ONLY_ACL[] = {{ZOO_PERM_ALL, id_tmp}};//, {ZOO_PERM_READ, ZOO_AUTH_IDS}, {ZOO_PERM_WRITE, ZOO_AUTH_IDS}};
//  struct ACL_vector CREATE_ONLY = {1, CREATE_ONLY_ACL};
  rc = zoo_create(zh,"/xyz","value", 5, &ZOO_CREATOR_ALL_ACL/*&CREATE_ONLY*/, ZOO_EPHEMERAL,
                      buffer, sizeof(buffer)-1);
 
  if (rc == ZOK) {
    printf("create ok \n");
  } else {
    printf("create faild%d\n" ,rc);
  }

  struct ACL_vector ret;
  printf("zoo set acl %d\n", zoo_get_acl(zh, "/xyz", &ret, 0));
  rc = zoo_exists(zh, "/xyz", 0, 0);
  if (rc != ZOK) {
    printf("%d exist fail\n", rc);
  } else {
    printf("/xyz is exist\n");
  }
  /** this operation will fail with a ZNOAUTH error */
  int buflen= sizeof(buffer);
  struct Stat stat;
  rc = zoo_exists(zh2, "/xyz", 0, 0);
if (rc == ZNOAUTH) {
    fprintf(stderr, "Error %d for %d\n", rc, __LINE__);
  }
  else {
    printf("zh2 exist %s\n", buffer);
  }

  rc = zoo_get(zh2, "/xyz", 0, buffer, &buflen, &stat);
  if (rc == ZNOAUTH) {
    fprintf(stderr, "Error %d for %d\n", rc, __LINE__);
  }
  else {
    printf("get data %s\n", buffer);
  }
   if (rc == ZOK){
    printf("zoo create xyz  sucess\n");
  } else {
    printf("zoo create xyz  failed %s\n", zerror(rc));
  }

  rc = zoo_add_auth(zh2,"digest", "dbscale:dbscale",strlen("dbscale:dbscale"),0,0);
  if (rc == ZOK){
    printf("zoo add auth sucess\n");
  } else {
    printf("zoo add auth failed\n");
  }
  rc = zoo_get(zh2, "/xyz", 0, buffer, &buflen, &stat);
  if (rc == ZNOAUTH) {
    fprintf(stderr, "Error %d for %d\n", rc, __LINE__);
  }
  else {
    printf("get data %s\n", buffer);
  }

  rc = zoo_wget(zh2, "/xyz", watcher, NULL, buffer, &buflen, &stat);
  if (rc == ZNOAUTH) {
    fprintf(stderr, "Error %d for %d\n", rc, __LINE__);
  }
  else {
    printf("wget sucess %s\n", buffer);
  }

  zookeeper_close(zh);
  return 0;
}