Lose data when testing with multiple thread
I have setup a cluster with 1 master and 2 slaves. and wrote a sample running on fourth computer to send the key/value to master. this sample runs two threads for writing 10000 records to master simulately. the key for one thread is from '0key0' to '0key9999', for another thread is from '1key0' to '1key9999'. when the test finished, I read the data which two threads wrote with one thread and found some data lost:
1. some key is duplicate.
2. key is correct but value is null
3. key non exists, means this key value not written.
following is the test code, any problems exists? please help me !!!
Compile common is : gcc main.c -lcouchbase -pthread -w -o main
Run common is : ./main 0 10000
after finished,there is only 18224 item in the cluster.
mainThread:
void *myThread(int id)
{
int i,mynum,mytotalnum;
mynum = num;
mytotalnum = totalnum;
i = mynum;
libcouchbase_t instance; /* our libcouchbase instance */
libcouchbase_error_t oprc; /* for checking various responses */
instance = libcouchbase_create(host, username,
passwd, bucket, NULL);
if (instance == NULL) {
fprintf(stderr, "Failed to create libcouchbase instance\n");
return 1;
}
if (libcouchbase_connect(instance) != LIBCOUCHBASE_SUCCESS) {
fprintf(stderr, "Failed to connect libcouchbase instance to server\n");
return 1;
}
libcouchbase_wait(instance);
memset(doc,'-',sizeof(doc));
for(;i
{
sprintf(key,"%dkey%d\0",id,i);
printf("key=%s\n",key);
/* Store doc to in the system */
oprc = libcouchbase_store(instance,NULL,LIBCOUCHBASE_SET,key, /* the key or _id of the document */strlen(key), /* the key length */doc,strlen(doc), /* length of */0, /* flags, */0, /* expiration */0); /* and CAS values, see API reference */
if (oprc != LIBCOUCHBASE_SUCCESS) {
printf("Failed to create hello store operation.\n");
return 1;
}
/* Wait for the operation to compelete */
libcouchbase_wait(instance);
oprc = libcouchbase_get_last_error(instance);
if (oprc == LIBCOUCHBASE_SUCCESS) {
// printf("key=%s\n",key);
} else {
printf("Could not set hello. Error received is %d\n", oprc);
printf("server---i:%d\n",i);
return 1;
}
}