330f7bf753c2fb4049ad0b4f87d70714bec5c4a8
[awesomized/libmemcached] / tests / replication.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Gearmand client and server library.
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <libtest/common.h>
39
40 #include <libmemcached/common.h>
41 #include <tests/replication.h>
42
43 test_return_t replication_set_test(memcached_st *memc)
44 {
45 memcached_return_t rc;
46 memcached_st *memc_clone= memcached_clone(NULL, memc);
47 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
48
49 rc= memcached_set(memc, "bubba", 5, "0", 1, 0, 0);
50 test_true(rc == MEMCACHED_SUCCESS);
51
52 /*
53 ** We are using the quiet commands to store the replicas, so we need
54 ** to ensure that all of them are processed before we can continue.
55 ** In the test we go directly from storing the object to trying to
56 ** receive the object from all of the different servers, so we
57 ** could end up in a race condition (the memcached server hasn't yet
58 ** processed the quiet command from the replication set when it process
59 ** the request from the other client (created by the clone)). As a
60 ** workaround for that we call memcached_quit to send the quit command
61 ** to the server and wait for the response ;-) If you use the test code
62 ** as an example for your own code, please note that you shouldn't need
63 ** to do this ;-)
64 */
65 memcached_quit(memc);
66
67 /*
68 ** "bubba" should now be stored on all of our servers. We don't have an
69 ** easy to use API to address each individual server, so I'll just iterate
70 ** through a bunch of "master keys" and I should most likely hit all of the
71 ** servers...
72 */
73 for (int x= 'a'; x <= 'z'; ++x)
74 {
75 const char key[2]= { (char)x, 0 };
76 size_t len;
77 uint32_t flags;
78 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
79 &len, &flags, &rc);
80 test_true(rc == MEMCACHED_SUCCESS);
81 test_true(val != NULL);
82 free(val);
83 }
84
85 memcached_free(memc_clone);
86
87 return TEST_SUCCESS;
88 }
89
90 test_return_t replication_get_test(memcached_st *memc)
91 {
92 memcached_return_t rc;
93
94 /*
95 * Don't do the following in your code. I am abusing the internal details
96 * within the library, and this is not a supported interface.
97 * This is to verify correct behavior in the library
98 */
99 for (uint32_t host= 0; host < memcached_server_count(memc); ++host)
100 {
101 memcached_st *memc_clone= memcached_clone(NULL, memc);
102 memcached_server_instance_st instance=
103 memcached_server_instance_by_position(memc_clone, host);
104
105 ((memcached_server_write_instance_st)instance)->port= 0;
106
107 for (int x= 'a'; x <= 'z'; ++x)
108 {
109 const char key[2]= { (char)x, 0 };
110 size_t len;
111 uint32_t flags;
112 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
113 &len, &flags, &rc);
114 test_true(rc == MEMCACHED_SUCCESS);
115 test_true(val != NULL);
116 free(val);
117 }
118
119 memcached_free(memc_clone);
120 }
121
122 return TEST_SUCCESS;
123 }
124
125 test_return_t replication_mget_test(memcached_st *memc)
126 {
127 memcached_return_t rc;
128 memcached_st *memc_clone= memcached_clone(NULL, memc);
129 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
130
131 const char *keys[]= { "bubba", "key1", "key2", "key3" };
132 size_t len[]= { 5, 4, 4, 4 };
133
134 for (size_t x= 0; x< 4; ++x)
135 {
136 rc= memcached_set(memc, keys[x], len[x], "0", 1, 0, 0);
137 test_true(rc == MEMCACHED_SUCCESS);
138 }
139
140 /*
141 ** We are using the quiet commands to store the replicas, so we need
142 ** to ensure that all of them are processed before we can continue.
143 ** In the test we go directly from storing the object to trying to
144 ** receive the object from all of the different servers, so we
145 ** could end up in a race condition (the memcached server hasn't yet
146 ** processed the quiet command from the replication set when it process
147 ** the request from the other client (created by the clone)). As a
148 ** workaround for that we call memcached_quit to send the quit command
149 ** to the server and wait for the response ;-) If you use the test code
150 ** as an example for your own code, please note that you shouldn't need
151 ** to do this ;-)
152 */
153 memcached_quit(memc);
154
155 /*
156 * Don't do the following in your code. I am abusing the internal details
157 * within the library, and this is not a supported interface.
158 * This is to verify correct behavior in the library
159 */
160 memcached_result_st result_obj;
161 for (uint32_t host= 0; host < memc_clone->number_of_hosts; host++)
162 {
163 memcached_st *new_clone= memcached_clone(NULL, memc);
164 memcached_server_instance_st instance=
165 memcached_server_instance_by_position(new_clone, host);
166 ((memcached_server_write_instance_st)instance)->port= 0;
167
168 for (int x= 'a'; x <= 'z'; ++x)
169 {
170 char key[2]= { (char)x, 0 };
171
172 rc= memcached_mget_by_key(new_clone, key, 1, keys, len, 4);
173 test_true(rc == MEMCACHED_SUCCESS);
174
175 memcached_result_st *results= memcached_result_create(new_clone, &result_obj);
176 test_true(results);
177
178 int hits= 0;
179 while ((results= memcached_fetch_result(new_clone, &result_obj, &rc)) != NULL)
180 {
181 hits++;
182 }
183 test_true(hits == 4);
184 memcached_result_free(&result_obj);
185 }
186
187 memcached_free(new_clone);
188 }
189
190 memcached_free(memc_clone);
191
192 return TEST_SUCCESS;
193 }
194
195 test_return_t replication_randomize_mget_test(memcached_st *memc)
196 {
197 memcached_result_st result_obj;
198 memcached_return_t rc;
199 memcached_st *memc_clone= memcached_clone(NULL, memc);
200 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 3);
201 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, 1);
202
203 const char *keys[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
204 size_t len[]= { 4, 4, 4, 4, 4, 4, 4 };
205
206 for (size_t x= 0; x< 7; ++x)
207 {
208 rc= memcached_set(memc, keys[x], len[x], "1", 1, 0, 0);
209 test_true(rc == MEMCACHED_SUCCESS);
210 }
211
212 memcached_quit(memc);
213
214 for (size_t x= 0; x< 7; ++x)
215 {
216 const char key[2]= { (char)x, 0 };
217
218 rc= memcached_mget_by_key(memc_clone, key, 1, keys, len, 7);
219 test_true(rc == MEMCACHED_SUCCESS);
220
221 memcached_result_st *results= memcached_result_create(memc_clone, &result_obj);
222 test_true(results);
223
224 int hits= 0;
225 while ((results= memcached_fetch_result(memc_clone, &result_obj, &rc)) != NULL)
226 {
227 ++hits;
228 }
229 test_true(hits == 7);
230 memcached_result_free(&result_obj);
231 }
232 memcached_free(memc_clone);
233 return TEST_SUCCESS;
234 }
235
236 test_return_t replication_delete_test(memcached_st *memc)
237 {
238 memcached_return_t rc;
239 memcached_st *memc_clone= memcached_clone(NULL, memc);
240 /* Delete the items from all of the servers except 1 */
241 uint64_t repl= memcached_behavior_get(memc,
242 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS);
243 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, --repl);
244
245 const char *keys[]= { "bubba", "key1", "key2", "key3" };
246 size_t len[]= { 5, 4, 4, 4 };
247
248 for (size_t x= 0; x< 4; ++x)
249 {
250 rc= memcached_delete_by_key(memc, keys[0], len[0], keys[x], len[x], 0);
251 test_true(rc == MEMCACHED_SUCCESS);
252 }
253
254 /*
255 * Don't do the following in your code. I am abusing the internal details
256 * within the library, and this is not a supported interface.
257 * This is to verify correct behavior in the library
258 */
259 uint32_t hash= memcached_generate_hash(memc, keys[0], len[0]);
260 for (uint32_t x= 0; x < (repl + 1); ++x)
261 {
262 memcached_server_instance_st instance=
263 memcached_server_instance_by_position(memc_clone, x);
264
265 ((memcached_server_write_instance_st)instance)->port= 0;
266 if (++hash == memc_clone->number_of_hosts)
267 hash= 0;
268 }
269
270 memcached_result_st result_obj;
271 for (uint32_t host= 0; host < memc_clone->number_of_hosts; ++host)
272 {
273 for (size_t x= 'a'; x <= 'z'; ++x)
274 {
275 const char key[2]= { (char)x, 0 };
276
277 rc= memcached_mget_by_key(memc_clone, key, 1, keys, len, 4);
278 test_true(rc == MEMCACHED_SUCCESS);
279
280 memcached_result_st *results= memcached_result_create(memc_clone, &result_obj);
281 test_true(results);
282
283 int hits= 0;
284 while ((results= memcached_fetch_result(memc_clone, &result_obj, &rc)) != NULL)
285 {
286 ++hits;
287 }
288 test_true(hits == 4);
289 memcached_result_free(&result_obj);
290 }
291 }
292 memcached_free(memc_clone);
293
294 return TEST_SUCCESS;
295 }
296
297 test_return_t replication_randomize_mget_fail_test(memcached_st *memc)
298 {
299 memcached_st *memc_clone= memcached_clone(NULL, memc);
300 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 3);
301
302 for (int x= int(MEMCACHED_SUCCESS); x < int(MEMCACHED_MAXIMUM_RETURN); ++x)
303 {
304 const char *key= memcached_strerror(NULL, memcached_return_t(x));
305 memcached_return_t rc= memcached_set(memc,
306 key, strlen(key),
307 key, strlen(key), 0, 0);
308 test_true(rc == MEMCACHED_SUCCESS);
309 }
310
311 memcached_flush_buffers(memc);
312
313 // We need to now cause a failure in one server, never do this in your own
314 // code.
315 close(memc_clone->servers[1].fd);
316 memc_clone->servers[1].port= 1;
317 memc_clone->servers[1].address_info_next= NULL;
318
319 for (int x= int(MEMCACHED_SUCCESS); x < int(MEMCACHED_MAXIMUM_RETURN); ++x)
320 {
321 const char *key= memcached_strerror(NULL, memcached_return_t(x));
322 memcached_return_t rc;
323 uint32_t flags;
324 size_t value_length;
325 char *value= memcached_get(memc_clone, key, strlen(key), &value_length, &flags, &rc);
326 test_true(rc == MEMCACHED_SUCCESS);
327 test_compare(strlen(key), value_length);
328 test_strcmp(key, value);
329 free(value);
330 }
331 memcached_free(memc_clone);
332 return TEST_SUCCESS;
333 }