static void testWrongServer(const char *fname) { VBUCKET_CONFIG_HANDLE vb = vbucket_config_parse_file(configPath(fname)); if (vb == NULL) { fprintf(stderr, "vbucket_config_parse_file error: %s\n", vbucket_get_error()); abort(); } /* Starts at 0 */ assert(vbucket_get_master(vb, 0) == 0); /* Does not change when I told it I found the wrong thing */ assert(vbucket_found_incorrect_master(vb, 0, 1) == 0); assert(vbucket_get_master(vb, 0) == 0); /* Does change if I tell it I got the right thing and it was wrong. */ assert(vbucket_found_incorrect_master(vb, 0, 0) == 1); assert(vbucket_get_master(vb, 0) == 1); /* ...and again */ assert(vbucket_found_incorrect_master(vb, 0, 1) == 2); assert(vbucket_get_master(vb, 0) == 2); /* ...and then wraps */ assert(vbucket_found_incorrect_master(vb, 0, 2) == 0); assert(vbucket_get_master(vb, 0) == 0); vbucket_config_destroy(vb); }
static void testConfig(const char *fname) { int whoops = 0; const struct key_st *k; int i = 0; VBUCKET_CONFIG_HANDLE vb = vbucket_config_parse_file(configPath(fname)); if (vb == NULL) { fprintf(stderr, "vbucket_config_parse_file error: %s\n", vbucket_get_error()); abort(); } while ((k = &keys[i++])->key != NULL) { int id = vbucket_get_vbucket_by_key(vb, k->key, strlen(k->key)); if (id != k->vbucket) { fprintf(stderr, "Expected vbucket %d for key '%s' but got %d\n", k->vbucket, k->key, id); whoops = 1; } } if (whoops) { abort(); } assert(vbucket_config_get_num_servers(vb) == 3 || vbucket_config_get_num_servers(vb) == 4); assert(vbucket_config_get_num_replicas(vb) == 2); for (i = 0; i < 3; ++i) { assert(strcmp(vbucket_config_get_server(vb, i), servers[i]) == 0); } for (i = 0; i < 4; ++i) { assert(vbucket_get_master(vb, i) == vbuckets[i].master); assert(vbucket_get_replica(vb, i, 0) == vbuckets[i].replicas[0]); assert(vbucket_get_replica(vb, i, 1) == vbuckets[i].replicas[1]); } assert(vbucket_config_get_user(vb) == NULL); assert(vbucket_config_get_password(vb) == NULL); vbucket_config_destroy(vb); }
static void testWrongServerFFT(const char *fname) { VBUCKET_CONFIG_HANDLE vb = vbucket_config_parse_file(configPath(fname)); int rv = 0; int nvb = 0; int i = 0; if (vb == NULL) { fprintf(stderr, "vbucket_config_parse_file error: %s\n", vbucket_get_error()); abort(); } /* found incorrect master should not be the same as get master now */ nvb = vbucket_config_get_num_vbuckets(vb); for (i = 0; i < nvb; i++) { rv = vbucket_get_master(vb, i); assert(rv != vbucket_found_incorrect_master(vb, i, rv)); } /* the ideal test case should be that we check that the vbucket */ /* and the fvbucket map are identical at this point. TODO untill */ /* we have a vbucketlib function that diffs vbuckets and fvbuckets */ vbucket_config_destroy(vb); }
int main(int argc, char **argv) { VBUCKET_CONFIG_HANDLE vb = NULL; int rval; int num_keys_per_vbucket; int num_keys_to_generate; int num_vbuckets; char *** keys; int i, j, v, k, total; char *key; if (argc < 4) { printf("vbucketkeygen mapfile <keys per vbucket> <keys to generate>\n\n"); printf(" vbucketkeygen will output a list of keys that equally\n"); printf(" distribute amongst every vbucket.\n\n"); printf(" vbucketkeygen expects a vBucketServerMap JSON mapfile, and\n"); printf(" will print the keyname and vBucketId.\n"); printf(" You may use '-' instead for the filename to specify stdin.\n\n"); printf(" Examples:\n"); printf(" ./vbucketkeygen file.json 10 10000\n\n"); printf(" curl http://HOST:8091/pools/default/buckets/default | \\\n"); printf(" ./vbucketkeygen - 5 10000\n"); exit(1); } if (strcmp("-", argv[1]) == 0) { char buf[50000]; if (fgets(buf, sizeof(buf) - 1, stdin) == NULL) { fprintf(stderr, "ERROR: vbucketkeygen found no input on stdin\n"); exit(1); } buf[sizeof(buf) - 1] = '\0'; vb = vbucket_config_parse_string(buf); } else { vb = vbucket_config_parse_file(argv[1]); } if (vb == NULL) { fprintf(stderr, "ERROR: vbucket_config_parse_string error: %s\n", vbucket_get_error()); exit(1); } rval = 0; num_keys_per_vbucket = atoi(argv[2]); num_keys_to_generate = atoi(argv[3]); num_vbuckets = vbucket_config_get_num_vbuckets(vb); /* allocate memory and set each key to null since strdup will allocate that */ keys = malloc(sizeof(char***) * num_vbuckets); for (i = 0; i < num_vbuckets; i++) { keys[i] = malloc(sizeof(char**) * num_keys_per_vbucket); } for (i = 0; i < num_vbuckets; i++) { for (j = 0 ; j < num_keys_per_vbucket ; j++) { keys[i][j] = 0; } } /* generate keys and copy them to the keys structure */ key = malloc(sizeof(char) * (MAX_KEY_SIZE+1)); for (i = 0; i < num_keys_to_generate; i++) { snprintf(key, MAX_KEY_SIZE + 1, "key_%010d", i); v = vbucket_get_vbucket_by_key(vb, key, strlen(key)); for (k = 0; k < num_keys_per_vbucket; k++) { if (keys[v][k] == 0) { keys[v][k] = strdup(key); break; } } } /* print out <key> <vbucket> and count up total keys so we can check that every vbucket has the correct number of keys */ total = 0; for (i = 0; i < num_vbuckets; i++) { for (j = 0 ; j < num_keys_per_vbucket ; j++) { if (keys[i][j] != 0) { printf("%s %d\n", keys[i][j], i); total++; } } } if (total < (num_vbuckets * num_keys_per_vbucket)) { fprintf(stderr, "some vbuckets don't have enough keys\n"); rval = 1; } vbucket_config_destroy(vb); return rval; }