attempt to fix cachedump test
[awesomized/libmemcached] / libmemcached / dump.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 /*
38 We use this to dump all keys.
39
40 At this point we only support a callback method. This could be optimized by first
41 calling items and finding active slabs. For the moment though we just loop through
42 all slabs on servers and "grab" the keys.
43 */
44
45 #include <libmemcached/common.h>
46
47 static memcached_return_t ascii_dump(Memcached *memc, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks)
48 {
49 memcached_version(memc);
50 /* MAX_NUMBER_OF_SLAB_CLASSES is defined to 200 in Memcached 1.4.10 */
51 for (uint32_t x= 0; x < 200; x++)
52 {
53 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
54 int buffer_length= snprintf(buffer, sizeof(buffer), "%u", x);
55 if (size_t(buffer_length) >= sizeof(buffer) or buffer_length < 0)
56 {
57 return memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
58 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
59 }
60
61 // @NOTE the hard coded zero means "no limit"
62 libmemcached_io_vector_st vector[]=
63 {
64 { memcached_literal_param("stats cachedump ") },
65 { buffer, size_t(buffer_length) },
66 { memcached_literal_param(" 0\r\n") }
67 };
68
69 // Send message to all servers
70 for (uint32_t server_key= 0; server_key < memcached_server_count(memc); server_key++)
71 {
72 memcached_instance_st* instance= memcached_instance_fetch(memc, server_key);
73
74 // skip slabs >63 for server versions >= 1.4.23
75 if (x < 64 || memcached_version_instance_cmp(instance, 1, 4, 23) < 0) {
76 memcached_return_t vdo_rc;
77 if (memcached_failed((vdo_rc= memcached_vdo(instance, vector, 3, true))))
78 {
79 return vdo_rc;
80 }
81 }
82 }
83
84 // Collect the returned items
85 memcached_instance_st* instance;
86 memcached_return_t read_ret= MEMCACHED_SUCCESS;
87 while ((instance= memcached_io_get_readable_server(memc, read_ret)))
88 {
89 memcached_return_t response_rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
90 if (response_rc == MEMCACHED_ITEM)
91 {
92 char *string_ptr, *end_ptr;
93
94 string_ptr= buffer;
95 string_ptr+= 5; /* Move past ITEM */
96
97 for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++) {} ;
98
99 char *key= string_ptr;
100 key[(size_t)(end_ptr-string_ptr)]= 0;
101
102 for (uint32_t callback_counter= 0; callback_counter < number_of_callbacks; callback_counter++)
103 {
104 memcached_return_t callback_rc= (*callback[callback_counter])(memc, key, (size_t)(end_ptr-string_ptr), context);
105 if (callback_rc != MEMCACHED_SUCCESS)
106 {
107 // @todo build up a message for the error from the value
108 memcached_set_error(*instance, callback_rc, MEMCACHED_AT);
109 break;
110 }
111 }
112 }
113 else if (response_rc == MEMCACHED_END)
114 {
115 // All items have been returned
116 }
117 else if (response_rc == MEMCACHED_SERVER_ERROR)
118 {
119 /* If we try to request stats cachedump for a slab class that is too big
120 * the server will return an incorrect error message:
121 * "MEMCACHED_SERVER_ERROR failed to allocate memory"
122 * This isn't really a fatal error, so let's just skip it. I want to
123 * fix the return value from the memcached server to a CLIENT_ERROR,
124 * so let's add support for that as well right now.
125 */
126 assert(response_rc == MEMCACHED_SUCCESS); // Just fail
127 return response_rc;
128 }
129 else if (response_rc == MEMCACHED_CLIENT_ERROR)
130 {
131 /* The maximum number of slabs has changed in the past (currently 1<<6-1),
132 * so ignore any client errors complaining about an illegal slab id.
133 */
134 if (0 == strncmp(buffer, "CLIENT_ERROR Illegal slab id", sizeof("CLIENT_ERROR Illegal slab id") - 1)) {
135 memcached_error_free(*instance);
136 memcached_error_free(*memc);
137 } else {
138 return response_rc;
139 }
140 }
141 else
142 {
143 // IO error of some sort must have occurred
144 return response_rc;
145 }
146 }
147 }
148
149 return memcached_has_current_error(*memc) ? MEMCACHED_SOME_ERRORS : MEMCACHED_SUCCESS;
150 }
151
152 memcached_return_t memcached_dump(memcached_st *shell, memcached_dump_fn *callback, void *context, uint32_t number_of_callbacks)
153 {
154 Memcached* ptr= memcached2Memcached(shell);
155 memcached_return_t rc;
156 if (memcached_failed(rc= initialize_query(ptr, true)))
157 {
158 return rc;
159 }
160
161 /*
162 No support for Binary protocol yet
163 @todo Fix this so that we just flush, switch to ascii, and then go back to binary.
164 */
165 if (memcached_is_binary(ptr))
166 {
167 return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, memcached_literal_param("Binary protocol is not supported for memcached_dump()"));
168 }
169
170 return ascii_dump(ptr, callback, context, number_of_callbacks);
171 }