X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fdump.cc;h=4fcf7de2aa282ba7694dd61da33d14e53ff614d1;hb=9973d386ac8476cd09cc4d9cf7bd2234a42740c7;hp=899a4c3ef1317bf2036be32dd321e55375ab39a1;hpb=387c65c94bec4333da574771fc82a40deabbc3e2;p=awesomized%2Flibmemcached diff --git a/libmemcached/dump.cc b/libmemcached/dump.cc index 899a4c3e..4fcf7de2 100644 --- a/libmemcached/dump.cc +++ b/libmemcached/dump.cc @@ -44,101 +44,110 @@ #include -static memcached_return_t ascii_dump(memcached_st *memc, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks) +static memcached_return_t ascii_dump(Memcached *memc, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks) { - for (uint32_t server_key= 0; server_key < memcached_server_count(memc); server_key++) + /* MAX_NUMBER_OF_SLAB_CLASSES is defined to 200 in Memcached 1.4.10 */ + for (uint32_t x= 0; x < 200; x++) { - memcached_server_write_instance_st instance= memcached_server_instance_fetch(memc, server_key); + char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + int buffer_length= snprintf(buffer, sizeof(buffer), "%u", x); + if (size_t(buffer_length) >= sizeof(buffer) or buffer_length < 0) + { + return memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)")); + } + + // @NOTE the hard coded zero means "no limit" + libmemcached_io_vector_st vector[]= + { + { memcached_literal_param("stats cachedump ") }, + { buffer, size_t(buffer_length) }, + { memcached_literal_param(" 0\r\n") } + }; - bool exit_slab_loop= false; - /* MAX_NUMBER_OF_SLAB_CLASSESdefined to 200 in Memcached 1.4.10 */ - for (uint32_t x= 0; x < 200 and (exit_slab_loop == false); x++) + // Send message to all servers + for (uint32_t server_key= 0; server_key < memcached_server_count(memc); server_key++) { - char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - int buffer_length= snprintf(buffer, sizeof(buffer), "%u", x); - if (size_t(buffer_length) >= sizeof(buffer) or buffer_length < 0) + memcached_instance_st* instance= memcached_instance_fetch(memc, server_key); + + memcached_return_t vdo_rc; + if (memcached_failed((vdo_rc= memcached_vdo(instance, vector, 3, true)))) { - return memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, - memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)")); + return vdo_rc; } + } - libmemcached_io_vector_st vector[]= + // Collect the returned items + memcached_instance_st* instance; + memcached_return_t read_ret= MEMCACHED_SUCCESS; + while ((instance= memcached_io_get_readable_server(memc, read_ret))) + { + memcached_return_t response_rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); + if (response_rc == MEMCACHED_ITEM) { - { memcached_literal_param("stats cachedump ") }, - { buffer, buffer_length }, - { memcached_literal_param(" 0 0\r\n") } - }; + char *string_ptr, *end_ptr; - memcached_return_t vdo_rc; - if (memcached_success((vdo_rc= memcached_vdo(instance, vector, 3, true)))) - { - while (1) + string_ptr= buffer; + string_ptr+= 5; /* Move past ITEM */ + + for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++) {} ; + + char *key= string_ptr; + key[(size_t)(end_ptr-string_ptr)]= 0; + + for (uint32_t callback_counter= 0; callback_counter < number_of_callbacks; callback_counter++) { - memcached_return_t response_rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL); - fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, memcached_strerror(NULL, response_rc)); - if (response_rc == MEMCACHED_ITEM) + memcached_return_t callback_rc= (*callback[callback_counter])(memc, key, (size_t)(end_ptr-string_ptr), context); + if (callback_rc != MEMCACHED_SUCCESS) { - char *string_ptr, *end_ptr; - - string_ptr= buffer; - string_ptr+= 5; /* Move past ITEM */ - - for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++) {} ; - - char *key= string_ptr; - key[(size_t)(end_ptr-string_ptr)]= 0; - - for (uint32_t callback_counter= 0; callback_counter < number_of_callbacks; callback_counter++) - { - memcached_return_t callback_rc= (*callback[callback_counter])(memc, key, (size_t)(end_ptr-string_ptr), context); - if (callback_rc != MEMCACHED_SUCCESS) - { - // @todo build up a message for the error from the value - memcached_set_error(*instance, callback_rc, MEMCACHED_AT); - break; - } - } - } - else if (response_rc == MEMCACHED_END) - { - // No additional items were found - exit_slab_loop= true; - break; - } - else if (response_rc == MEMCACHED_SERVER_ERROR or response_rc == MEMCACHED_CLIENT_ERROR) - { - /* If we try to request stats cachedump for a slab class that is too big - * the server will return an incorrect error message: - * "MEMCACHED_SERVER_ERROR failed to allocate memory" - * This isn't really a fatal error, so let's just skip it. I want to - * fix the return value from the memcached server to a CLIENT_ERROR, - * so let's add support for that as well right now. - */ - exit_slab_loop= true; - break; - } - else - { - memcached_set_error(*instance, response_rc, MEMCACHED_AT); - exit_slab_loop= true; + // @todo build up a message for the error from the value + memcached_set_error(*instance, callback_rc, MEMCACHED_AT); break; } } } + else if (response_rc == MEMCACHED_END) + { + // All items have been returned + } + else if (response_rc == MEMCACHED_SERVER_ERROR) + { + /* If we try to request stats cachedump for a slab class that is too big + * the server will return an incorrect error message: + * "MEMCACHED_SERVER_ERROR failed to allocate memory" + * This isn't really a fatal error, so let's just skip it. I want to + * fix the return value from the memcached server to a CLIENT_ERROR, + * so let's add support for that as well right now. + */ + assert(response_rc == MEMCACHED_SUCCESS); // Just fail + return response_rc; + } + else if (response_rc == MEMCACHED_CLIENT_ERROR) + { + /* The maximum number of slabs has changed in the past (currently 1<<6-1), + * so ignore any client errors complaining about an illegal slab id. + */ + if (0 == strncmp(buffer, "CLIENT_ERROR Illegal slab id", sizeof("CLIENT_ERROR Illegal slab id") - 1)) { + memcached_error_free(*instance); + memcached_error_free(*memc); + } else { + return response_rc; + } + } else { - exit_slab_loop= true; - memcached_set_error(*instance, vdo_rc, MEMCACHED_AT); + // IO error of some sort must have occurred + return response_rc; } - fprintf(stderr, "Was able to request %u slab\n", x); } } return memcached_has_current_error(*memc) ? MEMCACHED_SOME_ERRORS : MEMCACHED_SUCCESS; } -memcached_return_t memcached_dump(memcached_st *ptr, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks) +memcached_return_t memcached_dump(memcached_st *shell, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks) { + Memcached* ptr= memcached2Memcached(shell); memcached_return_t rc; if (memcached_failed(rc= initialize_query(ptr, true))) {