First pass through turning instance into ++
[m6w6/libmemcached] / tests / libmemcached-1.0 / replication.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached 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 <config.h>
39 #include <libtest/test.hpp>
40
41 using namespace libtest;
42
43 #include <libmemcached/memcached.h>
44 #include <libmemcached/server_instance.h>
45 #include <tests/replication.h>
46 #include <tests/debug.h>
47
48 #include "tests/libmemcached-1.0/setup_and_teardowns.h"
49
50 test_return_t check_replication_sanity_TEST(memcached_st *memc)
51 {
52 test_true(memc);
53 test_compare(uint64_t(1),
54 memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL));
55
56 /*
57 * Make sure that we store the item on all servers
58 * (master + replicas == number of servers)
59 */
60 test_compare(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS), uint64_t(memcached_server_count(memc) - 1));
61
62 return TEST_SUCCESS;
63 }
64
65 test_return_t replication_set_test(memcached_st *memc)
66 {
67 memcached_st *memc_clone= memcached_clone(NULL, memc);
68 test_true(memc_clone);
69 test_compare(MEMCACHED_SUCCESS,
70 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0));
71
72 test_compare(MEMCACHED_SUCCESS,
73 memcached_set(memc, "bubba", 5, "0", 1, 0, 0));
74
75 /*
76 ** We are using the quiet commands to store the replicas, so we need
77 ** to ensure that all of them are processed before we can continue.
78 ** In the test we go directly from storing the object to trying to
79 ** receive the object from all of the different servers, so we
80 ** could end up in a race condition (the memcached server hasn't yet
81 ** processed the quiet command from the replication set when it process
82 ** the request from the other client (created by the clone)). As a
83 ** workaround for that we call memcached_quit to send the quit command
84 ** to the server and wait for the response ;-) If you use the test code
85 ** as an example for your own code, please note that you shouldn't need
86 ** to do this ;-)
87 */
88 memcached_quit(memc);
89
90 /*
91 ** "bubba" should now be stored on all of our servers. We don't have an
92 ** easy to use API to address each individual server, so I'll just iterate
93 ** through a bunch of "master keys" and I should most likely hit all of the
94 ** servers...
95 */
96 for (int x= 'a'; x <= 'z'; ++x)
97 {
98 const char key[2]= { (char)x, 0 };
99 size_t len;
100 uint32_t flags;
101 memcached_return_t rc;
102 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
103 &len, &flags, &rc);
104 test_compare(MEMCACHED_SUCCESS, rc);
105 test_true(val);
106 free(val);
107 }
108
109 memcached_free(memc_clone);
110
111 return TEST_SUCCESS;
112 }
113
114 #include "libmemcached/instance.h"
115
116 test_return_t replication_get_test(memcached_st *memc)
117 {
118
119 /*
120 * Don't do the following in your code. I am abusing the internal details
121 * within the library, and this is not a supported interface.
122 * This is to verify correct behavior in the library
123 */
124 for (uint32_t host= 0; host < memcached_server_count(memc); ++host)
125 {
126 memcached_st *memc_clone= memcached_clone(NULL, memc);
127 org::libmemcached::Instance* instance= (org::libmemcached::Instance*)memcached_server_instance_by_position(memc_clone, host);
128
129 instance->port(0);
130
131 for (int x= 'a'; x <= 'z'; ++x)
132 {
133 const char key[2]= { (char)x, 0 };
134 size_t len;
135 uint32_t flags;
136 memcached_return_t rc;
137 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
138 &len, &flags, &rc);
139 test_compare(MEMCACHED_SUCCESS, rc);
140 test_true(val);
141 free(val);
142 }
143
144 memcached_free(memc_clone);
145 }
146
147 return TEST_SUCCESS;
148 }
149
150 test_return_t replication_mget_test(memcached_st *memc)
151 {
152 memcached_st *memc_clone= memcached_clone(NULL, memc);
153 test_true(memc_clone);
154 test_compare(MEMCACHED_SUCCESS,
155 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0));
156
157 const char *keys[]= { "bubba", "key1", "key2", "key3" };
158 size_t len[]= { 5, 4, 4, 4 };
159
160 for (size_t x= 0; x< 4; ++x)
161 {
162 test_compare(MEMCACHED_SUCCESS, memcached_set(memc, keys[x], len[x], "0", 1, 0, 0));
163 }
164
165 /*
166 ** We are using the quiet commands to store the replicas, so we need
167 ** to ensure that all of them are processed before we can continue.
168 ** In the test we go directly from storing the object to trying to
169 ** receive the object from all of the different servers, so we
170 ** could end up in a race condition (the memcached server hasn't yet
171 ** processed the quiet command from the replication set when it process
172 ** the request from the other client (created by the clone)). As a
173 ** workaround for that we call memcached_quit to send the quit command
174 ** to the server and wait for the response ;-) If you use the test code
175 ** as an example for your own code, please note that you shouldn't need
176 ** to do this ;-)
177 */
178 memcached_quit(memc);
179
180 /*
181 * Don't do the following in your code. I am abusing the internal details
182 * within the library, and this is not a supported interface.
183 * This is to verify correct behavior in the library
184 */
185 memcached_result_st result_obj;
186 for (uint32_t host= 0; host < memcached_server_count(memc_clone); host++)
187 {
188 memcached_st *new_clone= memcached_clone(NULL, memc);
189 memcached_server_instance_st instance= memcached_server_instance_by_position(new_clone, host);
190 ((memcached_server_write_instance_st)instance)->port(0);
191
192 for (int x= 'a'; x <= 'z'; ++x)
193 {
194 char key[2]= { (char)x, 0 };
195
196 test_compare(MEMCACHED_SUCCESS,
197 memcached_mget_by_key(new_clone, key, 1, keys, len, 4));
198
199 memcached_result_st *results= memcached_result_create(new_clone, &result_obj);
200 test_true(results);
201
202 int hits= 0;
203 memcached_return_t rc;
204 while ((results= memcached_fetch_result(new_clone, &result_obj, &rc)) != NULL)
205 {
206 hits++;
207 }
208 test_compare(4, hits);
209 memcached_result_free(&result_obj);
210 }
211
212 memcached_free(new_clone);
213 }
214
215 memcached_free(memc_clone);
216
217 return TEST_SUCCESS;
218 }
219
220 test_return_t replication_randomize_mget_test(memcached_st *memc)
221 {
222 memcached_result_st result_obj;
223 memcached_st *memc_clone= memcached_clone(NULL, memc);
224 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 3);
225 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, 1);
226
227 const char *keys[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
228 size_t len[]= { 4, 4, 4, 4, 4, 4, 4 };
229
230 for (size_t x= 0; x< 7; ++x)
231 {
232 test_compare(MEMCACHED_SUCCESS,
233 memcached_set(memc, keys[x], len[x], "1", 1, 0, 0));
234 }
235
236 memcached_quit(memc);
237
238 for (size_t x= 0; x< 7; ++x)
239 {
240 const char key[2]= { (char)x, 0 };
241
242 test_compare(MEMCACHED_SUCCESS,
243 memcached_mget_by_key(memc_clone, key, 1, keys, len, 7));
244
245 memcached_result_st *results= memcached_result_create(memc_clone, &result_obj);
246 test_true(results);
247
248 int hits= 0;
249 memcached_return_t rc;
250 while ((results= memcached_fetch_result(memc_clone, &result_obj, &rc)) != NULL)
251 {
252 ++hits;
253 }
254 test_compare(hits, 7);
255 memcached_result_free(&result_obj);
256 }
257 memcached_free(memc_clone);
258
259 return TEST_SUCCESS;
260 }
261
262 test_return_t replication_delete_test(memcached_st *memc_just_cloned)
263 {
264 memcached_flush(memc_just_cloned, 0);
265 memcached_st *memc_not_replicate= memcached_clone(NULL, memc_just_cloned);
266 memcached_st *memc_replicated= memcached_clone(NULL, memc_just_cloned);
267 const char *keys[]= { "bubba", "key1", "key2", "key3", "key4" };
268
269 test_compare(uint64_t(1), memcached_behavior_get(memc_replicated, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL));
270 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc_replicated, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, false));
271
272 // Make one copy
273 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc_replicated, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 1UL));
274 test_compare(uint64_t(1), memcached_behavior_get(memc_replicated, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS));
275
276 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc_not_replicate, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0UL));
277 test_compare(uint64_t(0), memcached_behavior_get(memc_not_replicate, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS));
278
279 for (size_t x= 0; x < test_array_length(keys); ++x)
280 {
281 memcached_set(memc_replicated,
282 test_string_make_from_cstr(keys[x]), // Keys
283 test_string_make_from_cstr(keys[x]), // We use the keys as values
284 0, 0);
285 }
286
287 memcached_flush_buffers(memc_replicated);
288
289 // Confirm keys with replication read
290 test_compare(TEST_SUCCESS, confirm_keys_exist(memc_replicated, keys, test_array_length(keys), true, true));
291 test_compare(TEST_SUCCESS, confirm_keys_exist(memc_not_replicate, keys, test_array_length(keys), true, true));
292
293 /* Delete the items from all of the servers except 1, we use the non replicated memc so that we know we deleted the keys */
294 for (size_t x= 0; x < test_array_length(keys); ++x)
295 {
296 test_compare(MEMCACHED_SUCCESS,
297 memcached_delete(memc_replicated,
298 test_string_make_from_cstr(keys[x]), // Keys
299 0));
300 }
301
302 test_compare(TEST_SUCCESS, confirm_keys_dont_exist(memc_replicated, keys, test_array_length(keys)));
303 test_compare(TEST_SUCCESS, confirm_keys_dont_exist(memc_not_replicate, keys, test_array_length(keys)));
304 #if 0
305 test_zero(confirm_key_count(memc_not_replicate));
306 #endif
307
308 memcached_free(memc_not_replicate);
309 memcached_free(memc_replicated);
310
311 return TEST_SUCCESS;
312 }
313
314 test_return_t replication_randomize_mget_fail_test(memcached_st *memc)
315 {
316 memcached_st *memc_clone= memcached_clone(NULL, memc);
317 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 3);
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 test_compare(MEMCACHED_SUCCESS,
323 memcached_set(memc,
324 key, strlen(key),
325 key, strlen(key), 0, 0));
326 }
327
328 memcached_flush_buffers(memc);
329
330 // We need to now cause a failure in one server, never do this in your own
331 // code.
332 close(memc_clone->servers[1].fd);
333 memc_clone->servers[1].port(1);
334 memc_clone->servers[1].address_info_next= NULL;
335
336 for (int x= int(MEMCACHED_SUCCESS); x < int(MEMCACHED_MAXIMUM_RETURN); ++x)
337 {
338 const char *key= memcached_strerror(NULL, memcached_return_t(x));
339 uint32_t flags;
340 size_t value_length;
341 memcached_return_t rc;
342 char *value= memcached_get(memc_clone, key, strlen(key), &value_length, &flags, &rc);
343 test_compare(MEMCACHED_SUCCESS, rc);
344 test_compare(strlen(key), value_length);
345 test_strcmp(key, value);
346 free(value);
347 }
348 memcached_free(memc_clone);
349 return TEST_SUCCESS;
350 }
351
352 /* Test that single miss does not cause replica reads to fail */
353 test_return_t replication_miss_test(memcached_st *memc)
354 {
355 test_skip(true, false);
356
357 memcached_st *memc_repl= memcached_clone(NULL, memc);
358 test_true(memc_repl);
359 memcached_st *memc_single= memcached_clone(NULL, memc);
360 test_true(memc_single);
361
362 const char *value = "my_value";
363 size_t vlen;
364 uint32_t flags;
365
366 /* this test makes sense only with 2 or more servers */
367 test_true(memcached_server_count(memc_repl) > 1);
368
369 /* Consistent hash */
370 test_compare(MEMCACHED_SUCCESS,
371 memcached_behavior_set_distribution(memc_repl, MEMCACHED_DISTRIBUTION_CONSISTENT));
372 test_compare(MEMCACHED_SUCCESS,
373 memcached_behavior_set_distribution(memc_single, MEMCACHED_DISTRIBUTION_CONSISTENT));
374
375 /* The first clone writes to all servers */
376 test_compare(MEMCACHED_SUCCESS,
377 memcached_behavior_set(memc_repl, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, true));
378 test_compare(MEMCACHED_SUCCESS,
379 memcached_behavior_set(memc_repl, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS,
380 memcached_server_count(memc_repl)));
381
382 /* Write to servers */
383 {
384 memcached_return_t rc= memcached_set(memc_repl,
385 test_literal_param(__func__),
386 value, strlen(value),
387 time_t(1200), uint32_t(0));
388 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
389 }
390
391 /* Use the second clone to remove the key from primary server.
392 This should remove the key from only one server */
393 {
394 memcached_return_t rc= memcached_delete(memc_single,
395 test_literal_param(__func__),
396 0);
397 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
398 }
399
400 /* Remove the server where the key was deleted */
401 {
402 #if 0
403 memcached_return_t rc;
404 const memcached_server_st *instance= memcached_server_by_key(memc_single,
405 test_literal_param(__func__),
406 &rc);
407 test_compare(MEMCACHED_SUCCESS, rc);
408 test_compare(MEMCACHED_SUCCESS,
409 memcached_server_remove(instance));
410 #endif
411 }
412
413 /* Test that others still have it */
414 {
415 memcached_return_t rc;
416 char *get_value= memcached_get(memc_single,
417 test_literal_param(__func__),
418 &vlen, &flags, &rc);
419 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
420 test_true(get_value and strcmp(get_value, value) == 0);
421 free(get_value);
422 }
423
424 /* This read should still return the value as we have it on other servers */
425 {
426 memcached_return_t rc;
427 char *get_value= memcached_get(memc_repl,
428 test_literal_param(__func__),
429 &vlen, &flags, &rc);
430 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
431 test_true(get_value and strcmp(get_value, value) == 0);
432 free(get_value);
433 }
434
435 memcached_free(memc_repl);
436 memcached_free(memc_single);
437
438 return TEST_SUCCESS;
439 }