Remove how use instance (keep API intact)
[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 memcached_server_instance_st instance=
128 memcached_server_instance_by_position(memc_clone, host);
129
130 ((memcached_server_write_instance_st)instance)->port= 0;
131
132 for (int x= 'a'; x <= 'z'; ++x)
133 {
134 const char key[2]= { (char)x, 0 };
135 size_t len;
136 uint32_t flags;
137 memcached_return_t rc;
138 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
139 &len, &flags, &rc);
140 test_compare(MEMCACHED_SUCCESS, rc);
141 test_true(val);
142 free(val);
143 }
144
145 memcached_free(memc_clone);
146 }
147
148 return TEST_SUCCESS;
149 }
150
151 test_return_t replication_mget_test(memcached_st *memc)
152 {
153 memcached_st *memc_clone= memcached_clone(NULL, memc);
154 test_true(memc_clone);
155 test_compare(MEMCACHED_SUCCESS,
156 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0));
157
158 const char *keys[]= { "bubba", "key1", "key2", "key3" };
159 size_t len[]= { 5, 4, 4, 4 };
160
161 for (size_t x= 0; x< 4; ++x)
162 {
163 test_compare(MEMCACHED_SUCCESS, memcached_set(memc, keys[x], len[x], "0", 1, 0, 0));
164 }
165
166 /*
167 ** We are using the quiet commands to store the replicas, so we need
168 ** to ensure that all of them are processed before we can continue.
169 ** In the test we go directly from storing the object to trying to
170 ** receive the object from all of the different servers, so we
171 ** could end up in a race condition (the memcached server hasn't yet
172 ** processed the quiet command from the replication set when it process
173 ** the request from the other client (created by the clone)). As a
174 ** workaround for that we call memcached_quit to send the quit command
175 ** to the server and wait for the response ;-) If you use the test code
176 ** as an example for your own code, please note that you shouldn't need
177 ** to do this ;-)
178 */
179 memcached_quit(memc);
180
181 /*
182 * Don't do the following in your code. I am abusing the internal details
183 * within the library, and this is not a supported interface.
184 * This is to verify correct behavior in the library
185 */
186 memcached_result_st result_obj;
187 for (uint32_t host= 0; host < memcached_server_count(memc_clone); host++)
188 {
189 memcached_st *new_clone= memcached_clone(NULL, memc);
190 memcached_server_instance_st instance=
191 memcached_server_instance_by_position(new_clone, host);
192 ((memcached_server_write_instance_st)instance)->port= 0;
193
194 for (int x= 'a'; x <= 'z'; ++x)
195 {
196 char key[2]= { (char)x, 0 };
197
198 test_compare(MEMCACHED_SUCCESS,
199 memcached_mget_by_key(new_clone, key, 1, keys, len, 4));
200
201 memcached_result_st *results= memcached_result_create(new_clone, &result_obj);
202 test_true(results);
203
204 int hits= 0;
205 memcached_return_t rc;
206 while ((results= memcached_fetch_result(new_clone, &result_obj, &rc)) != NULL)
207 {
208 hits++;
209 }
210 test_compare(4, hits);
211 memcached_result_free(&result_obj);
212 }
213
214 memcached_free(new_clone);
215 }
216
217 memcached_free(memc_clone);
218
219 return TEST_SUCCESS;
220 }
221
222 test_return_t replication_randomize_mget_test(memcached_st *memc)
223 {
224 memcached_result_st result_obj;
225 memcached_st *memc_clone= memcached_clone(NULL, memc);
226 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 3);
227 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, 1);
228
229 const char *keys[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
230 size_t len[]= { 4, 4, 4, 4, 4, 4, 4 };
231
232 for (size_t x= 0; x< 7; ++x)
233 {
234 test_compare(MEMCACHED_SUCCESS,
235 memcached_set(memc, keys[x], len[x], "1", 1, 0, 0));
236 }
237
238 memcached_quit(memc);
239
240 for (size_t x= 0; x< 7; ++x)
241 {
242 const char key[2]= { (char)x, 0 };
243
244 test_compare(MEMCACHED_SUCCESS,
245 memcached_mget_by_key(memc_clone, key, 1, keys, len, 7));
246
247 memcached_result_st *results= memcached_result_create(memc_clone, &result_obj);
248 test_true(results);
249
250 int hits= 0;
251 memcached_return_t rc;
252 while ((results= memcached_fetch_result(memc_clone, &result_obj, &rc)) != NULL)
253 {
254 ++hits;
255 }
256 test_compare(hits, 7);
257 memcached_result_free(&result_obj);
258 }
259 memcached_free(memc_clone);
260
261 return TEST_SUCCESS;
262 }
263
264 test_return_t replication_delete_test(memcached_st *memc_just_cloned)
265 {
266 memcached_flush(memc_just_cloned, 0);
267 memcached_st *memc_not_replicate= memcached_clone(NULL, memc_just_cloned);
268 memcached_st *memc_replicated= memcached_clone(NULL, memc_just_cloned);
269 const char *keys[]= { "bubba", "key1", "key2", "key3", "key4" };
270
271 test_compare(uint64_t(1), memcached_behavior_get(memc_replicated, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL));
272 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc_replicated, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, false));
273
274 // Make one copy
275 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc_replicated, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 1UL));
276 test_compare(uint64_t(1), memcached_behavior_get(memc_replicated, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS));
277
278 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc_not_replicate, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0UL));
279 test_compare(uint64_t(0), memcached_behavior_get(memc_not_replicate, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS));
280
281 for (size_t x= 0; x < test_array_length(keys); ++x)
282 {
283 memcached_set(memc_replicated,
284 test_string_make_from_cstr(keys[x]), // Keys
285 test_string_make_from_cstr(keys[x]), // We use the keys as values
286 0, 0);
287 }
288
289 memcached_flush_buffers(memc_replicated);
290
291 // Confirm keys with replication read
292 test_compare(TEST_SUCCESS, confirm_keys_exist(memc_replicated, keys, test_array_length(keys), true, true));
293 test_compare(TEST_SUCCESS, confirm_keys_exist(memc_not_replicate, keys, test_array_length(keys), true, true));
294
295 /* Delete the items from all of the servers except 1, we use the non replicated memc so that we know we deleted the keys */
296 for (size_t x= 0; x < test_array_length(keys); ++x)
297 {
298 test_compare(MEMCACHED_SUCCESS,
299 memcached_delete(memc_replicated,
300 test_string_make_from_cstr(keys[x]), // Keys
301 0));
302 }
303
304 test_compare(TEST_SUCCESS, confirm_keys_dont_exist(memc_replicated, keys, test_array_length(keys)));
305 test_compare(TEST_SUCCESS, confirm_keys_dont_exist(memc_not_replicate, keys, test_array_length(keys)));
306 #if 0
307 test_zero(confirm_key_count(memc_not_replicate));
308 #endif
309
310 memcached_free(memc_not_replicate);
311 memcached_free(memc_replicated);
312
313 return TEST_SUCCESS;
314 }
315
316 test_return_t replication_randomize_mget_fail_test(memcached_st *memc)
317 {
318 memcached_st *memc_clone= memcached_clone(NULL, memc);
319 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 3);
320
321 for (int x= int(MEMCACHED_SUCCESS); x < int(MEMCACHED_MAXIMUM_RETURN); ++x)
322 {
323 const char *key= memcached_strerror(NULL, memcached_return_t(x));
324 test_compare(MEMCACHED_SUCCESS,
325 memcached_set(memc,
326 key, strlen(key),
327 key, strlen(key), 0, 0));
328 }
329
330 memcached_flush_buffers(memc);
331
332 // We need to now cause a failure in one server, never do this in your own
333 // code.
334 close(memc_clone->servers[1].fd);
335 memc_clone->servers[1].port= 1;
336 memc_clone->servers[1].address_info_next= NULL;
337
338 for (int x= int(MEMCACHED_SUCCESS); x < int(MEMCACHED_MAXIMUM_RETURN); ++x)
339 {
340 const char *key= memcached_strerror(NULL, memcached_return_t(x));
341 uint32_t flags;
342 size_t value_length;
343 memcached_return_t rc;
344 char *value= memcached_get(memc_clone, key, strlen(key), &value_length, &flags, &rc);
345 test_compare(MEMCACHED_SUCCESS, rc);
346 test_compare(strlen(key), value_length);
347 test_strcmp(key, value);
348 free(value);
349 }
350 memcached_free(memc_clone);
351 return TEST_SUCCESS;
352 }
353
354 /* Test that single miss does not cause replica reads to fail */
355 test_return_t replication_miss_test(memcached_st *memc)
356 {
357 test_skip(true, false);
358
359 memcached_st *memc_repl= memcached_clone(NULL, memc);
360 test_true(memc_repl);
361 memcached_st *memc_single= memcached_clone(NULL, memc);
362 test_true(memc_single);
363
364 const char *value = "my_value";
365 size_t vlen;
366 uint32_t flags;
367
368 /* this test makes sense only with 2 or more servers */
369 test_true(memcached_server_count(memc_repl) > 1);
370
371 /* Consistent hash */
372 test_compare(MEMCACHED_SUCCESS,
373 memcached_behavior_set_distribution(memc_repl, MEMCACHED_DISTRIBUTION_CONSISTENT));
374 test_compare(MEMCACHED_SUCCESS,
375 memcached_behavior_set_distribution(memc_single, MEMCACHED_DISTRIBUTION_CONSISTENT));
376
377 /* The first clone writes to all servers */
378 test_compare(MEMCACHED_SUCCESS,
379 memcached_behavior_set(memc_repl, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, true));
380 test_compare(MEMCACHED_SUCCESS,
381 memcached_behavior_set(memc_repl, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS,
382 memcached_server_count(memc_repl)));
383
384 /* Write to servers */
385 {
386 memcached_return_t rc= memcached_set(memc_repl,
387 test_literal_param(__func__),
388 value, strlen(value),
389 time_t(1200), uint32_t(0));
390 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
391 }
392
393 /* Use the second clone to remove the key from primary server.
394 This should remove the key from only one server */
395 {
396 memcached_return_t rc= memcached_delete(memc_single,
397 test_literal_param(__func__),
398 0);
399 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
400 }
401
402 /* Remove the server where the key was deleted */
403 {
404 #if 0
405 memcached_return_t rc;
406 const memcached_server_st *instance= memcached_server_by_key(memc_single,
407 test_literal_param(__func__),
408 &rc);
409 test_compare(MEMCACHED_SUCCESS, rc);
410 test_compare(MEMCACHED_SUCCESS,
411 memcached_server_remove(instance));
412 #endif
413 }
414
415 /* Test that others still have it */
416 {
417 memcached_return_t rc;
418 char *get_value= memcached_get(memc_single,
419 test_literal_param(__func__),
420 &vlen, &flags, &rc);
421 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
422 test_true(get_value and strcmp(get_value, value) == 0);
423 free(get_value);
424 }
425
426 /* This read should still return the value as we have it on other servers */
427 {
428 memcached_return_t rc;
429 char *get_value= memcached_get(memc_repl,
430 test_literal_param(__func__),
431 &vlen, &flags, &rc);
432 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
433 test_true(get_value and strcmp(get_value, value) == 0);
434 free(get_value);
435 }
436
437 memcached_free(memc_repl);
438 memcached_free(memc_single);
439
440 return TEST_SUCCESS;
441 }