int c_zht_compare_swap_std(ZHTClient_c zhtClient, const char *key,
		const char *seen_value, const char *new_value, char *value_queried) {

#ifdef IND_MUTEX
	LockGuard lock(&c_zht_compare_swap_mutex);
#elif SHARED_MUTEX
	LockGuard lock(&c_zht_client_mutex);
#else
#endif

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	string skey(key);
	string sseenValue(seen_value);
	string snewValue(new_value);

	string resultStr;
	int rc = zhtcppClient->compare_swap(skey, sseenValue, snewValue, resultStr);

	memset(value_queried, 0, strlen(value_queried));

	strncpy(value_queried, resultStr.c_str(), resultStr.size());

	return rc;
}
int c_zht_init_std(ZHTClient_c *zhtClient, const char *zhtConfig,
		const char *neighborConf) {

#ifdef IND_MUTEX
	pthread_mutex_init(&c_zht_lookup_mutex, NULL);
	pthread_mutex_init(&c_zht_remove_mutex, NULL);
	pthread_mutex_init(&c_zht_insert_mutex, NULL);
	pthread_mutex_init(&c_zht_append_mutex, NULL);
	pthread_mutex_init(&c_zht_compare_swap_mutex, NULL);
	pthread_mutex_init(&c_zht_state_change_callback_mutex, NULL);
#elif SHARED_MUTEX
	pthread_mutex_init(&c_zht_client_mutex, NULL);
#else
#endif

	ZHTClient *zhtcppClient = new ZHTClient();

	string zhtConfigStr(zhtConfig);
	string neighborConfStr(neighborConf);

	if (zhtcppClient->init(zhtConfigStr, neighborConfStr) != 0) {

		printf("ZHTClient initialization failed, program exits.");
		return -1;
	}

	*zhtClient = (ZHTClient_c) zhtcppClient;

	return 0;
}
int benchmark(string &zhtConf, string &neighborConf) {

    srand(getpid() + TimeUtil::getTime_usec());

    if (zc.init(zhtConf, neighborConf) != 0) {

        cout << "ZHTClient initialization failed, program exits." << endl;
        return -1;
    }

    init_packages();

    benchmarkInsert();

    benchmarkLookup();

    benchmarkAppend();

    benchmarkRemove();

    zc.teardown();

    return 0;

}
int c_zht_compare_and_swap_std(ZHTClient_c zhtClient, const char *key,
		const char *seen_value, const char *new_value, char **value_queried) {

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;
	string sKey(key);
	string sSeenValue(seen_value);
	string sNewValue(new_value);

	Package package;
	package.set_virtualpath(sKey);
	package.set_isdir(true);
	package.set_replicano(5);
	package.set_operation(5);  // 5 for comapre and swap
	if (!sSeenValue.empty()) {
		package.set_realfullpath(sSeenValue);
	}
	if (!sNewValue.empty()) {
		package.set_newfullpath(sNewValue);
	}

	string return_str;
	int rc = zhtcppClient->compare_and_swap(package.SerializeAsString(),
			return_str);

	Package ret_pack;
	ret_pack.ParseFromString(return_str);

	strcpy(*value_queried, ret_pack.realfullpath().c_str());

	return rc;
}
int c_zht_remove_std(ZHTClient_c zhtClient, const char *pair) {

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	string str(pair);

	return zhtcppClient->remove(str);
}
int c_zht_insert_std(ZHTClient_c zhtClient, const char *pair) {

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	string str(pair);

	return zhtcppClient->insert(str);
}
int c_zht_append_std(ZHTClient_c zhtClient, const char *pair) {

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	string str(pair);

	return zhtcppClient->append(str);
}
Exemple #8
0
int zht_insert_meta(ZHTClient_c zhtClient, struct metadata * meta){

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	Package package;
	string keyStr(meta->key);
	
	//1. General infos on the file/storage
	package.set_virtualpath(keyStr); //as key
	package.set_isdir(false);
	package.set_replicano(1);
	package.set_operation(3); //1 for look up, 2 for remove, 3 for insert
	package.set_realfullpath(keyStr);
	
	//2. General infos on the encoding
	package.set_k(meta->k);
	package.set_m(meta->m);
	package.set_encodinglib(meta->encodingLib);
	package.set_filesize(meta->fileSize);
	package.set_bufsize(meta->bufsize);
	
	//3. Chunks/Replicas locations
	struct comTransfer * current = meta->loc->transfers;
	Package_Location*  location;

	while(current != NULL){
		location = package.add_location();
		
		location->set_hostname(string(current->hostName));
		location->set_port(current->port);
		location->set_distantchunkname(string(current->distantChunkName));
		
		current=current->next;
	}
	//4. Insertion
	std::string serialized = package.SerializeAsString();
	
	dbgprintf("Package Length:%i\n",serialized.size());
	
	//TEST
	Package package4;
	package4.ParseFromString(serialized);
	dbgprintf("Serialized: %s \n",serialized.c_str());
	dbgprintf("key: %s \n",(package4.virtualpath()).c_str());
	dbgprintf("k:%i,m:%i,encodinglib:%i,filesize:%i \n",package4.k(),package4.m(),package4.encodinglib(),package4.filesize());
	
	for (int j = 0; j < package4.location_size(); j++) {
		const Package_Location& location = package4.location(j);
		
		dbgprintf("chunk:%s, port:%i\n",location.distantchunkname().c_str(), location.port());
	}	
	//END TEST
	
	
	int res = zhtcppClient->insert(serialized);
	
	return res;
}
Exemple #9
0
int benchmarkInsert(string cfgFile, string memberList, vector<string> &pkgList,
		ZHTClient &clientRet, int numTest, int lenString) {

//	if (clientRet.initialize(cfgFile, memberList) != 0) { //zhouxb
	if (clientRet.initialize(cfgFile, memberList, TCP) != 0) {
		cout << "Crap! ZHTClient initialization failed, program exits." << endl;
		return -1;
	}

	for (int i = 0; i < numTest; i++) {
		Package package, package_ret;
		package.set_virtualpath(randomString(lenString)); //as key
		package.set_isdir(true);
		package.set_replicano(5); //orginal--Note: never let it be nagative!!!
		package.set_operation(3); // 3 for insert, 1 for look up, 2 for remove
		package.set_realfullpath(
				"Some-Real-longer-longer-and-longer-Paths--------");
		package.add_listitem("item-----2");
		package.add_listitem("item-----3");
		package.add_listitem("item-----4");
		package.add_listitem("item-----5");
		package.add_listitem("item-----6");
		string str = package.SerializeAsString();
//		cout << "package size = " << str.size() << endl;
//		cout<<"Client.cpp:benchmarkInsert: "<<endl;
//		cout<<"string: "<<str<<endl;
//		cout<<"Insert str: "<<str.c_str()<<endl;
//		cout<<"data(): "<< str.data()<<endl;
		pkgList.push_back(str);
	}
	double start = 0;
	double end = 0;
	start = getTime_msec();
	int errCount = 0;
	vector<string>::iterator it;
	int c = 0;
//	cout << "-----2" << endl;
	for (it = pkgList.begin(); it != pkgList.end(); it++) {
		//	cerr <<"insert count "<< c << endl;
		c++;
		string str_ins = *it;
//cout << "-----1" << endl;
		int ret = clientRet.insert(str_ins);
//cout << "-----2" << endl;
		if (ret < 0) {
			errCount++;
		}
	}
//close(sock);
	end = getTime_msec();
	cout << "Inserted " << numTest - errCount << " packages out of " << numTest
			<< ", cost " << end - start << " ms" << endl;
	return 0;
}
int c_zht_remove2_std(ZHTClient_c zhtClient, const char *key) {

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	string sKey(key);

	Package package;
	package.set_virtualpath(sKey);
	package.set_operation(2); //1 for look up, 2 for remove, 3 for insert, 4 append

	return zhtcppClient->remove(package.SerializeAsString());
}
int c_zht_remove_std(ZHTClient_c zhtClient, const char *key) {

#ifdef IND_MUTEX
	LockGuard lock(&c_zht_remove_mutex);
#elif SHARED_MUTEX
	LockGuard lock(&c_zht_client_mutex);
#else
#endif

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	string skey(key);

	return zhtcppClient->remove(skey);
}
int benchmarkInsert() {

    double start = 0;
    double end = 0;
    start = TimeUtil::getTime_msec();
    int errCount = 0;

    int c = 0;
    vector<string>::iterator it;
    for (it = pkgList.begin(); it != pkgList.end(); it++) {

        c++;

        string pkg_str = *it;
        Package pkg;
        pkg.ParseFromString(pkg_str);

        int ret = zc.insert(pkg.virtualpath(), pkg_str);

        if (ret < 0) {
            errCount++;
        }
    }

    end = TimeUtil::getTime_msec();

    char buf[200];
    sprintf(buf, "Inserted packages, %d, %d, cost(ms), %f", numOfOps - errCount,
            numOfOps, end - start);
    cout << buf << endl;

    return 0;
}
Exemple #13
0
float benchmarkLookup(vector<string> strList, ZHTClient &client) {
	vector<string>::iterator it;

	double start = 0;
	double end = 0;
	start = getTime_msec();
	int errCount = 0;
//cout << "Client: benchmarkLookup: start lookup \n";
	int c = 0;
	for (it = strList.begin(); it != strList.end(); it++) {
		string result;
//		cout << c << endl;
		c++;
//		cout<<"lookup: "<<c<<endl;
//sleep(1);
//
//		cout << "Client: What I want to find: \n";
//		cout <<"Lookup: "<< (*it).c_str() << endl;
		if (client.lookup((*it), result) < 0) {

			errCount++;
		} else if (result.empty()) { //empty string
			errCount++;
		}
//		cout << "Client: What I get: ";
//		cout << result.c_str() << endl;
	}

	end = getTime_msec();

	cout << "Lookup " << strList.size() - errCount << " packages out of "
			<< strList.size() << ", cost " << end - start << " ms" << endl;
	return 0;
}
Exemple #14
0
void test_pop() 
{
	int i = 11111;
	char intstr[10];
	sprintf(intstr, "%d", i);
	string uuid = string(intstr);

	//string uuid = getIp();
	string result = "y";
	int rc;

	while(result != "q")
	{
		cout << "UUID" << uuid << endl;
		try
		{
			rc = zc.pop(uuid, "q1", result);

			if (rc == 0)
			{
				i = i + 1;
				sprintf(intstr, "%d", i);
				uuid = string(intstr);

				printf("POP OK, rc(%d), value={%s}\n", rc, result.c_str());
			}
			else
				sleep(10);
		}
		catch(...)
		{
			sleep(10);
		}
	}	
}
int initconfig(ZHTClient &clientRet, string cfgFile, string memberList) {
	if (clientRet.initialize(cfgFile, memberList, TCP) != 0) {
		cout << "Crap! ZHTClient initialization failed, program exits." << endl;
		return -1;
	}

	return 0;
}
int main(int argc, char* argv[]) {

    //cout << "Usage: ./client <num_operations> <memberList> <configFile>"<< endl;
    /*	For BG/P:
    const string cmd = "cat /proc/personality.sh | grep BG_PSETORG";
    string torusID = executeShell(cmd);
    torusID.resize(torusID.size() - 1);
    int pid = getpid();
    unsigned int v = myhash(torusID.c_str(), 1000000) + pid;
    //cout<<"client: pid = "<<pid<<endl;

    //cout<<"Client random n = "<<v<<endl;
    srand(v);
    */

    char* isTCP = argv[4];
    //cout<<"Protocol = "<<isTCP<<endl;
    if (!strcmp("TCP", isTCP)) {
        TCP = true;
    } else {
        TCP = false;
    }

    int numOper = atoi(argv[1]);
    //cout<<"numOper = "<<numOper<<endl;
    string cfgFile(argv[3]);
    //cout<<"cfgFile = "<<cfgFile<<endl;
    string memberList(argv[2]);
    //cout<<"memberList: "<<memberList<<endl;
    vector<string> pkgList;
    ZHTClient testClient;
    //	int pid = getpid();
    char* tmpStr;
    stringstream ss; //create a stringstream
    //	ss << pid;

    //	string recordFile = "record." + ss.str();
    //	benchmarkTimeAnalize(cfgFile, memberList, pkgList, testClient, numOper, 15, recordFile);
    //cout<<"start to insert..."<<endl;
    insertMetadata(cfgFile, memberList, pkgList, testClient, numOper, 15); //25fro 128bytes.
    //cout << "Client:main, start lookup \n";
    benchmarkLookup(pkgList, testClient);
    benchmarkRemove(pkgList, testClient);
    testClient.tearDownTCP(TCP);
    return 0;
}
int c_zht_insert_std(ZHTClient_c zhtClient, const char *key,
		const char *value) {

#ifdef IND_MUTEX
	LockGuard lock(&c_zht_insert_mutex);
#elif SHARED_MUTEX
	LockGuard lock(&c_zht_client_mutex);
#else
#endif

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	string skey(key);
	string sval(value);

	return zhtcppClient->insert(skey, sval);
}
int c_zht_insert2_std(ZHTClient_c zhtClient, const char *key,
		const char *value) {

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	string sKey(key);
	string sValue(value);

	Package package;
	package.set_virtualpath(sKey); //as key
	package.set_isdir(true);
	package.set_replicano(5);
	package.set_operation(3); //1 for look up, 2 for remove, 3 for insert, 4 append
	if (!sValue.empty())
		package.set_realfullpath(sValue);

	return zhtcppClient->insert(package.SerializeAsString());
}
int c_zht_init_std(ZHTClient_c * zhtClient, const char *memberConfig,
		const char *zhtConfig, bool tcp) {

	ZHTClient * zhtcppClient = new ZHTClient();

	string zhtStr(zhtConfig);
	string memberStr(memberConfig);

	if (zhtcppClient->initialize(zhtStr, memberStr, tcp) != 0) {
		printf("Crap! ZHTClient initialization failed, program exits.");

		return -1;
	}

	*zhtClient = (ZHTClient_c) zhtcppClient;

	return 0;
}
Exemple #20
0
void submittaskszht(ZHTClient &clientRet, string client_id)
{
	int ret = 0;
	Package package;
	package.set_virtualpath(client_id);
	package.set_operation(21);
	cout << "Ok, what is the hell!\n" << endl;

	for (map<uint32_t, NodeList>::iterator map_it = update_map_zht.begin(); map_it != update_map_zht.end(); ++map_it)
	{
		uint32_t index = map_it->first;
		NodeList &update_list = map_it->second;
		while (!update_list.empty())
		{
			int num_vector_count, per_vector_count;
			string alltasks;
			vector< vector<string> > tokenize_string = tokenize_1(update_list.front(), '$', '#', num_vector_count, per_vector_count);
			//cout << " num_vector_count = " << num_vector_count << " per_vector_count = " << per_vector_count << endl;
			for(int i = 0; i < per_vector_count; i++)
			{
				Package tmpPackage;
				tmpPackage.ParseFromString(tokenize_string.at(0).at(i));
				string key = tmpPackage.virtualpath();
				alltasks.append(key); alltasks.append("\'\"");
			} //cout << "Serve
			package.set_realfullpath(alltasks);
			string str = package.SerializeAsString();
			ret += clientRet.svrtosvr(str, str.length(), index);
			update_list.pop_front();
		}
	}

//	Package package; string alltasks;
//			package.set_virtualpath(client_id); // Here key is just the client ID
//			package.set_operation(21);
//			//package.set_operation(22);
//			num_packages++;
//
//			int num_tasks_this_package = max_tasks_per_package;
//			int num_tasks_left = num_tasks - total_submitted1;
//			if (num_tasks_left < max_tasks_per_package) {
//				num_tasks_this_package = num_tasks_left;
//			}
//			for(int j = 0; j < num_tasks_this_package; j++) {
//				int task_id = it->first; ++it;
//
//	        	        stringstream task_id_ss;
//	                	task_id_ss << task_id << client_id; // Task ID + Client ID
//				alltasks.append(task_id_ss.str()); alltasks.append("\'\""); // Task ID
//			}
//			total_submitted1 = total_submitted1 + num_tasks_this_package;
//			package.set_realfullpath(alltasks);
//			string str = package.SerializeAsString();
//			//cout << "String size = " << str.size() << " str length = " << strlen(str.c_str());
//			int32_t ret = clientRet.svrtosvr(str, str.length(), toserver);
}
int c_zht_lookup2_std(ZHTClient_c zhtClient, const char *key, char *result,
		size_t *n) {

	ZHTClient *zhtcppClient = (ZHTClient *) zhtClient;

	string sKey(key);

	Package package;
	package.set_virtualpath(sKey); //as key
	package.set_isdir(true);
	package.set_replicano(5);
	package.set_operation(1); //1 for look up, 2 for remove, 3 for insert, 4 append

	string resultStr;
	int ret = zhtcppClient->lookup(package.SerializeAsString(), resultStr);

	/*
	 * hello,zht:hello,zht ==> hello,zht:zht
	 * */
	string store;
	char * pch, *sp;
	pch = strtok_r((char*) resultStr.c_str(), ":", &sp);
	Package package2;

	while (pch != NULL) {

		package2.ParseFromString(pch);
		string strRealfullpath = package2.realfullpath();

		store.append(strRealfullpath);
		store.append(":");

		pch = strtok_r(NULL, ":", &sp);
	}

	size_t found = store.find_last_of(":");
	store = store.substr(0, found);

	strncpy(result, store.c_str(), strlen(store.c_str()));
	*n = store.size();

	return ret;
}
int c_zht_state_change_callback_std(ZHTClient_c zhtClient, const char *key,
		const char *expected_val, int lease) {

#ifdef IND_MUTEX
	LockGuard lock(&c_zht_state_change_callback_mutex);
#elif SHARED_MUTEX
	LockGuard lock(&c_zht_client_mutex);
#else
#endif

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	string skey(key);
	string expectedval(expected_val);

	int rc = zhtcppClient->state_change_callback(skey, expectedval, lease);

	return rc;
}
int c_zht_lookup_std(ZHTClient_c zhtClient, const char *pair, char *result,
		size_t *n) {

	ZHTClient *zhtcppClient = (ZHTClient *) zhtClient;

	string sPair(pair);

	string resultStr;
	int ret = zhtcppClient->lookup(sPair, resultStr);

	/*
	 * hello,zht:hello,zht ==> hello,zht:zht
	 * */
	string store;
	char * pch, *sp;
	pch = strtok_r((char*) resultStr.c_str(), ":", &sp);
	Package package2;

	while (pch != NULL) {

		package2.ParseFromString(pch);
		string strRealfullpath = package2.realfullpath();

		store.append(strRealfullpath);
		store.append(":");

		pch = strtok_r(NULL, ":", &sp);
	}

	size_t found = store.find_last_of(":");
	store = store.substr(0, found);
	package2.set_realfullpath(store);

	store = package2.SerializeAsString();
	strncpy(result, store.c_str(), strlen(store.c_str()));
	*n = store.size();

	return ret;
}
int c_zht_init(const char *memberConfig, const char *zhtConfig, bool tcp) {

	string zhtStr(zhtConfig);
	string memberStr(memberConfig);

	if (zhtClient.initialize(zhtStr, memberStr, tcp) != 0) {
		printf("Crap! ZHTClient initialization failed, program exits.");

		return -1;
	}

	return 0;
}
int c_zht_remove2(const char *key) {

	string keyStr(key);

	if (keyStr.empty()) //empty key not allowed.
		return -1;

	Package package;
	package.set_virtualpath(keyStr);
	package.set_operation(2); //1 for look up, 2 for remove, 3 for insert

	return zhtClient.remove(package.SerializeAsString());
}
int c_zht_lookup_std(ZHTClient_c zhtClient, const char *key, char *result) {

#ifdef IND_MUTEX
	LockGuard lock(&c_zht_lookup_mutex);
#elif SHARED_MUTEX
	LockGuard lock(&c_zht_client_mutex);
#else
#endif

	ZHTClient *zhtcppClient = (ZHTClient *) zhtClient;

	string skey(key);

	string resultStr;
	int ret = zhtcppClient->lookup(skey, resultStr);

	memset(result, 0, strlen(result));

	strncpy(result, resultStr.c_str(), resultStr.size());

	return ret;
}
int c_zht_lookup(const char *pair, char *result) {

	string pkg(pair);
	string returnStr;

	int ret = zhtClient.lookup(pkg, returnStr);

	char *chars = new char[returnStr.size() + 1];
	strcpy(chars, returnStr.c_str());

	result = chars;

	return ret;
}
int c_zht_teardown_std(ZHTClient_c zhtClient) {

#ifdef IND_MUTEX
	pthread_mutex_destroy (&c_zht_lookup_mutex);
	pthread_mutex_destroy (&c_zht_remove_mutex);
	pthread_mutex_destroy (&c_zht_insert_mutex);
	pthread_mutex_destroy (&c_zht_append_mutex);
	pthread_mutex_destroy (&c_zht_compare_swap_mutex);
	pthread_mutex_destroy (&c_zht_state_change_callback_mutex);
#elif SHARED_MUTEX
	pthread_mutex_destroy(&c_zht_client_mutex);
#else
#endif

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	int rc = zhtcppClient->teardown();

	delete zhtcppClient;
	zhtcppClient = NULL;

	return rc;
}
int benchmarkAppend() {

    vector<string> pkgList_append;

    vector<string>::iterator it;
    for (it = pkgList.begin(); it != pkgList.end(); it++) {

        Package package;
        package.ParseFromString((*it));

        package.add_listitem("item-----6-append");

        pkgList_append.push_back(package.SerializeAsString());
    }

    double start = 0;
    double end = 0;
    start = TimeUtil::getTime_msec();
    int errCount = 0;

    int c = 0;
    for (it = pkgList_append.begin(); it != pkgList_append.end(); it++) {

        c++;

        string pkg_str = *it;
        Package pkg;
        pkg.ParseFromString(pkg_str);

        int ret = zc.append(pkg.virtualpath(), pkg_str);

        if (ret < 0) {
            errCount++;
        }
    }

    end = TimeUtil::getTime_msec();

    char buf[200];
    sprintf(buf, "Appended packages, %d, %d, cost(ms), %f", numOfOps - errCount,
            numOfOps, end - start);
    cout << buf << endl;

    return 0;
}
float benchmarkLookup(vector<string> strList, ZHTClient &client) {
    vector<string>::iterator it;

    /*
     for (it = strList.begin(); it != strList.end(); it++) {
     Package package;
     package.ParseFromString((*it));
     package.set_operation(1); // 3 for insert, 1 for look up, 2 for remove
     package.set_replicano(3); //5: original, 3 not original

     strList.erase(it);
     string newStr = package.SerializeAsString();
     strList.push_back(newStr);
     }
    */
    double start = 0;
    double end = 0;
    start = getTime_msec();
    int errCount = 0;
    //cout << "Client: benchmarkLookup: start lookup \n";
    int c=0;
    for (it = strList.begin(); it != strList.end(); it++) {
        string result;
        //cout <<"insert count "<< c << endl;
        c++;

        //cout<<"lookup: "<<i<<endl;
        //cout << "Client: What I want to find: \n";
        //cout <<"Lookup: "<< (*it).c_str() << endl;
        if (client.lookup((*it), result) < 0) {

            errCount++;
        } else if (result.empty()) { //empty string
            errCount++;
        }
        //cout << "Client: What I get: ";
        //cout << result.c_str() << endl;
    }

    end = getTime_msec();

    cout << "Lookup " << strList.size() - errCount << " packages out of "
         << strList.size() << ", cost " << end - start << " ms" << endl;
    return 0;
}