X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=tests%2Ffunction.c;h=0d7b2d1ab1a51cbe6d172c18cbf28cad3b022e3a;hb=90a467967e3bc3a0508044e74ef9cf7b811ab095;hp=3cf729ba467a9dc59154a8626ca07844d20f73b2;hpb=07999ea4e52b8a80ea66376ec22768c32add7f29;p=m6w6%2Flibmemcached diff --git a/tests/function.c b/tests/function.c index 3cf729ba..0d7b2d1a 100644 --- a/tests/function.c +++ b/tests/function.c @@ -1449,6 +1449,70 @@ static test_return_t mget_test(memcached_st *memc) return TEST_SUCCESS; } +static test_return_t mget_execute(memcached_st *memc) +{ + bool binary= false; + if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0) + binary= true; + + /* + * I only want to hit _one_ server so I know the number of requests I'm + * sending in the pipeline. + */ + uint32_t number_of_hosts= memc->number_of_hosts; + memc->number_of_hosts= 1; + + int max_keys= binary ? 20480 : 1; + + + char **keys= calloc((size_t)max_keys, sizeof(char*)); + size_t *key_length=calloc((size_t)max_keys, sizeof(size_t)); + + /* First add all of the items.. */ + char blob[1024] = {0}; + memcached_return rc; + for (int x= 0; x < max_keys; ++x) + { + char k[251]; + key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%u", x); + keys[x]= strdup(k); + assert(keys[x] != NULL); + rc= memcached_add(memc, keys[x], key_length[x], blob, sizeof(blob), 0, 0); + assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED); + } + + /* Try to get all of them with a large multiget */ + unsigned int counter= 0; + memcached_execute_function callbacks[1]= { [0]= &callback_counter }; + rc= memcached_mget_execute(memc, (const char**)keys, key_length, + (size_t)max_keys, callbacks, &counter, 1); + + if (binary) + { + assert(rc == MEMCACHED_SUCCESS); + + rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1); + assert(rc == MEMCACHED_END); + + /* Verify that we got all of the items */ + assert(counter == (unsigned int)max_keys); + } + else + { + assert(rc == MEMCACHED_NOT_SUPPORTED); + assert(counter == 0); + } + + /* Release all allocated resources */ + for (int x= 0; x < max_keys; ++x) + free(keys[x]); + free(keys); + free(key_length); + + memc->number_of_hosts= number_of_hosts; + return TEST_SUCCESS; +} + static test_return_t get_stats_keys(memcached_st *memc) { char **list; @@ -2526,9 +2590,9 @@ static test_return_t user_supplied_bug18(memcached_st *trash) /* verify the standard ketama set. */ for (x= 0; x < 99; x++) { - uint32_t server_idx = memcached_generate_hash(memc, test_cases[x].key, strlen(test_cases[x].key)); + uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key)); char *hostname = memc->hosts[server_idx].hostname; - assert(strcmp(hostname, test_cases[x].server) == 0); + assert(strcmp(hostname, ketama_test_cases[x].server) == 0); } memcached_server_list_free(server_pool); @@ -2671,7 +2735,7 @@ static test_return_t auto_eject_hosts(memcached_st *trash) for (int x= 0; x < 99; x++) { - uint32_t server_idx = memcached_generate_hash(memc, test_cases[x].key, strlen(test_cases[x].key)); + uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key)); assert(server_idx != 2); } @@ -2681,17 +2745,73 @@ static test_return_t auto_eject_hosts(memcached_st *trash) run_distribution(memc); for (int x= 0; x < 99; x++) { - uint32_t server_idx = memcached_generate_hash(memc, test_cases[x].key, strlen(test_cases[x].key)); + uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key)); char *hostname = memc->hosts[server_idx].hostname; - assert(strcmp(hostname, test_cases[x].server) == 0); + assert(strcmp(hostname, ketama_test_cases[x].server) == 0); + } + + memcached_server_list_free(server_pool); + memcached_free(memc); + + return TEST_SUCCESS; +} + +static test_return_t output_ketama_weighted_keys(memcached_st *trash) +{ + (void) trash; + + memcached_return rc; + memcached_st *memc= memcached_create(NULL); + assert(memc); + + + rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1); + assert(rc == MEMCACHED_SUCCESS); + + uint64_t value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED); + assert(value == 1); + + rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5); + assert(rc == MEMCACHED_SUCCESS); + + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH); + assert(value == MEMCACHED_HASH_MD5); + + + assert(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE, + MEMCACHED_KETAMA_COMPAT_SPY) == MEMCACHED_SUCCESS); + + memcached_server_st *server_pool; + server_pool = memcached_servers_parse("10.0.1.1:11211,10.0.1.2:11211,10.0.1.3:11211,10.0.1.4:11211,10.0.1.5:11211,10.0.1.6:11211,10.0.1.7:11211,10.0.1.8:11211,192.168.1.1:11211,192.168.100.1:11211"); + memcached_server_push(memc, server_pool); + + FILE *fp; + if ((fp = fopen("ketama_keys.txt", "w"))) + { + // noop + } else { + printf("cannot write to file ketama_keys.txt"); + return TEST_FAILURE; } + for (int x= 0; x < 10000; x++) + { + char key[10]; + sprintf(key, "%d", x); + + uint32_t server_idx = memcached_generate_hash(memc, key, strlen(key)); + char *hostname = memc->hosts[server_idx].hostname; + unsigned int port = memc->hosts[server_idx].port; + fprintf(fp, "key %s is on host /%s:%u\n", key, hostname, port); + } + fclose(fp); memcached_server_list_free(server_pool); memcached_free(memc); return TEST_SUCCESS; } + static test_return_t result_static(memcached_st *memc) { memcached_result_st result; @@ -4559,6 +4679,125 @@ static test_return_t jenkins_run (memcached_st *memc __attribute__((unused))) return TEST_SUCCESS; } + +static test_return_t ketama_compatibility_libmemcached(memcached_st *trash) +{ + memcached_return rc; + uint64_t value; + int x; + memcached_server_st *server_pool; + memcached_st *memc; + + (void)trash; + + memc= memcached_create(NULL); + assert(memc); + + rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1); + assert(rc == MEMCACHED_SUCCESS); + + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED); + assert(value == 1); + + assert(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE, + MEMCACHED_KETAMA_COMPAT_LIBMEMCACHED) == MEMCACHED_SUCCESS); + + assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE) == + MEMCACHED_KETAMA_COMPAT_LIBMEMCACHED); + + server_pool = memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100"); + memcached_server_push(memc, server_pool); + + /* verify that the server list was parsed okay. */ + assert(memc->number_of_hosts == 8); + assert(strcmp(server_pool[0].hostname, "10.0.1.1") == 0); + assert(server_pool[0].port == 11211); + assert(server_pool[0].weight == 600); + assert(strcmp(server_pool[2].hostname, "10.0.1.3") == 0); + assert(server_pool[2].port == 11211); + assert(server_pool[2].weight == 200); + assert(strcmp(server_pool[7].hostname, "10.0.1.8") == 0); + assert(server_pool[7].port == 11211); + assert(server_pool[7].weight == 100); + + /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets + * us test the boundary wraparound. + */ + assert(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index); + + /* verify the standard ketama set. */ + for (x= 0; x < 99; x++) + { + uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key)); + char *hostname = memc->hosts[server_idx].hostname; + assert(strcmp(hostname, ketama_test_cases[x].server) == 0); + } + + memcached_server_list_free(server_pool); + memcached_free(memc); + + return TEST_SUCCESS; +} + +static test_return_t ketama_compatibility_spymemcached(memcached_st *trash) +{ + memcached_return rc; + uint64_t value; + int x; + memcached_server_st *server_pool; + memcached_st *memc; + + (void)trash; + + memc= memcached_create(NULL); + assert(memc); + + rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1); + assert(rc == MEMCACHED_SUCCESS); + + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED); + assert(value == 1); + + assert(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE, + MEMCACHED_KETAMA_COMPAT_SPY) == MEMCACHED_SUCCESS); + + assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE) == + MEMCACHED_KETAMA_COMPAT_SPY); + + server_pool = memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100"); + memcached_server_push(memc, server_pool); + + /* verify that the server list was parsed okay. */ + assert(memc->number_of_hosts == 8); + assert(strcmp(server_pool[0].hostname, "10.0.1.1") == 0); + assert(server_pool[0].port == 11211); + assert(server_pool[0].weight == 600); + assert(strcmp(server_pool[2].hostname, "10.0.1.3") == 0); + assert(server_pool[2].port == 11211); + assert(server_pool[2].weight == 200); + assert(strcmp(server_pool[7].hostname, "10.0.1.8") == 0); + assert(server_pool[7].port == 11211); + assert(server_pool[7].weight == 100); + + /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets + * us test the boundary wraparound. + */ + assert(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index); + + /* verify the standard ketama set. */ + for (x= 0; x < 99; x++) + { + uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases_spy[x].key, strlen(ketama_test_cases_spy[x].key)); + char *hostname = memc->hosts[server_idx].hostname; + assert(strcmp(hostname, ketama_test_cases_spy[x].server) == 0); + } + + memcached_server_list_free(server_pool); + memcached_free(memc); + + return TEST_SUCCESS; +} + static test_return_t regression_bug_434484(memcached_st *memc) { if (pre_binary(memc) != MEMCACHED_SUCCESS) @@ -4732,7 +4971,113 @@ static test_return_t regression_bug_442914(memcached_st *memc) return TEST_SUCCESS; } +static test_return_t regression_bug_447342(memcached_st *memc) +{ + if (memc->number_of_hosts < 3 || pre_replication(memc) != MEMCACHED_SUCCESS) + return TEST_SKIPPED; + + memcached_return rc; + + rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 2); + assert(rc == MEMCACHED_SUCCESS); + + const size_t max_keys= 100; + char **keys= calloc(max_keys, sizeof(char*)); + size_t *key_length=calloc(max_keys, sizeof(size_t)); + + for (int x= 0; x < (int)max_keys; ++x) + { + char k[251]; + key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%u", x); + keys[x]= strdup(k); + assert(keys[x] != NULL); + rc= memcached_set(memc, k, key_length[x], k, key_length[x], 0, 0); + assert(rc == MEMCACHED_SUCCESS); + } + + /* + ** We are using the quiet commands to store the replicas, so we need + ** to ensure that all of them are processed before we can continue. + ** In the test we go directly from storing the object to trying to + ** receive the object from all of the different servers, so we + ** could end up in a race condition (the memcached server hasn't yet + ** processed the quiet command from the replication set when it process + ** the request from the other client (created by the clone)). As a + ** workaround for that we call memcached_quit to send the quit command + ** to the server and wait for the response ;-) If you use the test code + ** as an example for your own code, please note that you shouldn't need + ** to do this ;-) + */ + memcached_quit(memc); + + /* Verify that all messages are stored, and we didn't stuff too much + * into the servers + */ + rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys); + assert(rc == MEMCACHED_SUCCESS); + + unsigned int counter= 0; + memcached_execute_function callbacks[1]= { [0]= &callback_counter }; + rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1); + /* Verify that we received all of the key/value pairs */ + assert(counter == (unsigned int)max_keys); + + memcached_quit(memc); + /* + * Don't do the following in your code. I am abusing the internal details + * within the library, and this is not a supported interface. + * This is to verify correct behavior in the library. Fake that two servers + * are dead.. + */ + unsigned int port0= memc->hosts[0].port; + unsigned int port2= memc->hosts[2].port; + memc->hosts[0].port= 0; + memc->hosts[2].port= 0; + + rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys); + assert(rc == MEMCACHED_SUCCESS); + + counter= 0; + rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1); + assert(counter == (unsigned int)max_keys); + + /* restore the memc handle */ + memc->hosts[0].port= port0; + memc->hosts[2].port= port2; + + memcached_quit(memc); + + /* Remove half of the objects */ + for (int x= 0; x < (int)max_keys; ++x) + if (x & 1) + { + rc= memcached_delete(memc, keys[x], key_length[x], 0); + assert(rc == MEMCACHED_SUCCESS); + } + + memcached_quit(memc); + memc->hosts[0].port= 0; + memc->hosts[2].port= 0; + + /* now retry the command, this time we should have cache misses */ + rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys); + assert(rc == MEMCACHED_SUCCESS); + counter= 0; + rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1); + assert(counter == (unsigned int)(max_keys >> 1)); + + /* Release allocated resources */ + for (size_t x= 0; x < max_keys; ++x) + free(keys[x]); + free(keys); + free(key_length); + + /* restore the memc handle */ + memc->hosts[0].port= port0; + memc->hosts[2].port= port2; + return TEST_SUCCESS; +} /* Test memcached_server_get_last_disconnect * For a working server set, shall be NULL @@ -4785,6 +5130,52 @@ static test_return_t test_get_last_disconnect(memcached_st *memc) return TEST_SUCCESS; } +/* + * This test ensures that the failure counter isn't incremented during + * normal termination of the memcached instance. + */ +static test_return_t wrong_failure_counter_test(memcached_st *memc) +{ + memcached_return rc; + + /* Set value to force connection to the server */ + const char *key= "marmotte"; + const char *value= "milka"; + + /* + * Please note that I'm abusing the internal structures in libmemcached + * in a non-portable way and you shouldn't be doing this. I'm only + * doing this in order to verify that the library works the way it should + */ + uint32_t number_of_hosts= memc->number_of_hosts; + memc->number_of_hosts= 1; + + /* Ensure that we are connected to the server by setting a value */ + rc= memcached_set(memc, key, strlen(key), + value, strlen(value), + (time_t)0, (uint32_t)0); + assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED); + + + /* The test is to see that the memcached_quit doesn't increase the + * the server failure conter, so let's ensure that it is zero + * before sending quit + */ + memc->hosts[0].server_failure_counter= 0; + + memcached_quit(memc); + + /* Verify that it memcached_quit didn't increment the failure counter + * Please note that this isn't bullet proof, because an error could + * occur... + */ + assert(memc->hosts[0].server_failure_counter == 0); + + /* restore the instance */ + memc->number_of_hosts= number_of_hosts; + + return TEST_SUCCESS; +} test_st udp_setup_server_tests[] ={ {"set_udp_behavior_test", 0, set_udp_behavior_test}, @@ -4848,6 +5239,7 @@ test_st tests[] ={ {"mget_result", 1, mget_result_test }, {"mget_result_alloc", 1, mget_result_alloc_test }, {"mget_result_function", 1, mget_result_function }, + {"mget_execute", 1, mget_execute }, {"mget_end", 0, mget_end }, {"get_stats", 0, get_stats }, {"add_host_test", 0, add_host_test }, @@ -4930,6 +5322,7 @@ test_st user_tests[] ={ {"user_supplied_bug19", 1, user_supplied_bug19 }, {"user_supplied_bug20", 1, user_supplied_bug20 }, {"user_supplied_bug21", 1, user_supplied_bug21 }, + {"wrong_failure_counter_test", 1, wrong_failure_counter_test}, {0, 0, 0} }; @@ -4953,6 +5346,13 @@ test_st regression_tests[]= { {"lp:434843 buffered", 1, regression_bug_434843_buffered }, {"lp:421108", 1, regression_bug_421108 }, {"lp:442914", 1, regression_bug_442914 }, + {"lp:447342", 1, regression_bug_447342 }, + {0, 0, 0} +}; + +test_st ketama_compatibility[]= { + {"libmemcached", 1, ketama_compatibility_libmemcached }, + {"spymemcached", 1, ketama_compatibility_spymemcached }, {0, 0, 0} }; @@ -4998,6 +5398,7 @@ test_st hsieh_availability[] ={ test_st ketama_auto_eject_hosts[] ={ {"auto_eject_hosts", 1, auto_eject_hosts }, + {"output_ketama_weighted_keys", 1, output_ketama_weighted_keys }, {0, 0, 0} }; @@ -5061,6 +5462,7 @@ collection_st collection[] ={ {"consistent_not", 0, 0, consistent_tests}, {"consistent_ketama", pre_behavior_ketama, 0, consistent_tests}, {"consistent_ketama_weighted", pre_behavior_ketama_weighted, 0, consistent_weighted_tests}, + {"ketama_compat", 0, 0, ketama_compatibility}, {"test_hashes", 0, 0, hash_tests}, {"replication", pre_replication, 0, replication_tests}, {"replication_noblock", pre_replication_noblock, 0, replication_tests},