Add ability to have version "just requested" when you initially connect.
[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 memcached_return_t del_rc= memcached_delete(memc_replicated,
297 test_string_make_from_cstr(keys[x]), // Keys
298 0);
299 if (del_rc == MEMCACHED_SUCCESS or del_rc == MEMCACHED_NOTFOUND)
300 { }
301 else
302 {
303 test_compare(MEMCACHED_SUCCESS, del_rc);
304 }
305 }
306
307 test_compare(TEST_SUCCESS, confirm_keys_dont_exist(memc_replicated, keys, test_array_length(keys)));
308 test_compare(TEST_SUCCESS, confirm_keys_dont_exist(memc_not_replicate, keys, test_array_length(keys)));
309 #if 0
310 test_zero(confirm_key_count(memc_not_replicate));
311 #endif
312
313 memcached_free(memc_not_replicate);
314 memcached_free(memc_replicated);
315
316 return TEST_SUCCESS;
317 }
318
319 test_return_t replication_randomize_mget_fail_test(memcached_st *memc)
320 {
321 memcached_st *memc_clone= memcached_clone(NULL, memc);
322 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 3);
323
324 for (int x= int(MEMCACHED_SUCCESS); x < int(MEMCACHED_MAXIMUM_RETURN); ++x)
325 {
326 const char *key= memcached_strerror(NULL, memcached_return_t(x));
327 test_compare(MEMCACHED_SUCCESS,
328 memcached_set(memc,
329 key, strlen(key),
330 key, strlen(key), 0, 0));
331 }
332
333 memcached_flush_buffers(memc);
334
335 // We need to now cause a failure in one server, never do this in your own
336 // code.
337 close(memc_clone->servers[1].fd);
338 memc_clone->servers[1].port(1);
339 memc_clone->servers[1].address_info_next= NULL;
340
341 for (int x= int(MEMCACHED_SUCCESS); x < int(MEMCACHED_MAXIMUM_RETURN); ++x)
342 {
343 const char *key= memcached_strerror(NULL, memcached_return_t(x));
344 uint32_t flags;
345 size_t value_length;
346 memcached_return_t rc;
347 char *value= memcached_get(memc_clone, key, strlen(key), &value_length, &flags, &rc);
348 test_compare(MEMCACHED_SUCCESS, rc);
349 test_compare(strlen(key), value_length);
350 test_strcmp(key, value);
351 free(value);
352 }
353 memcached_free(memc_clone);
354 return TEST_SUCCESS;
355 }
356
357 /* Test that single miss does not cause replica reads to fail */
358 test_return_t replication_miss_test(memcached_st *memc)
359 {
360 test_skip(true, false);
361
362 memcached_st *memc_repl= memcached_clone(NULL, memc);
363 test_true(memc_repl);
364 memcached_st *memc_single= memcached_clone(NULL, memc);
365 test_true(memc_single);
366
367 const char *value = "my_value";
368 size_t vlen;
369 uint32_t flags;
370
371 /* this test makes sense only with 2 or more servers */
372 test_true(memcached_server_count(memc_repl) > 1);
373
374 /* Consistent hash */
375 test_compare(MEMCACHED_SUCCESS,
376 memcached_behavior_set_distribution(memc_repl, MEMCACHED_DISTRIBUTION_CONSISTENT));
377 test_compare(MEMCACHED_SUCCESS,
378 memcached_behavior_set_distribution(memc_single, MEMCACHED_DISTRIBUTION_CONSISTENT));
379
380 /* The first clone writes to all servers */
381 test_compare(MEMCACHED_SUCCESS,
382 memcached_behavior_set(memc_repl, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, true));
383 test_compare(MEMCACHED_SUCCESS,
384 memcached_behavior_set(memc_repl, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS,
385 memcached_server_count(memc_repl)));
386
387 /* Write to servers */
388 {
389 memcached_return_t rc= memcached_set(memc_repl,
390 test_literal_param(__func__),
391 value, strlen(value),
392 time_t(1200), uint32_t(0));
393 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
394 }
395
396 /* Use the second clone to remove the key from primary server.
397 This should remove the key from only one server */
398 {
399 memcached_return_t rc= memcached_delete(memc_single,
400 test_literal_param(__func__),
401 0);
402 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
403 }
404
405 /* Remove the server where the key was deleted */
406 {
407 #if 0
408 memcached_return_t rc;
409 const memcached_server_st *instance= memcached_server_by_key(memc_single,
410 test_literal_param(__func__),
411 &rc);
412 test_compare(MEMCACHED_SUCCESS, rc);
413 test_compare(MEMCACHED_SUCCESS,
414 memcached_server_remove(instance));
415 #endif
416 }
417
418 /* Test that others still have it */
419 {
420 memcached_return_t rc;
421 char *get_value= memcached_get(memc_single,
422 test_literal_param(__func__),
423 &vlen, &flags, &rc);
424 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
425 test_true(get_value and strcmp(get_value, value) == 0);
426 free(get_value);
427 }
428
429 /* This read should still return the value as we have it on other servers */
430 {
431 memcached_return_t rc;
432 char *get_value= memcached_get(memc_repl,
433 test_literal_param(__func__),
434 &vlen, &flags, &rc);
435 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
436 test_true(get_value and strcmp(get_value, value) == 0);
437 free(get_value);
438 }
439
440 memcached_free(memc_repl);
441 memcached_free(memc_single);
442
443 return TEST_SUCCESS;
444 }