1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Libmemcached client and server library.
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
39 #include <libtest/test.hpp>
41 using namespace libtest
;
43 #include <libmemcached/memcached.h>
44 #include <libmemcached/server_instance.h>
45 #include <tests/replication.h>
46 #include <tests/debug.h>
48 #include "tests/libmemcached-1.0/setup_and_teardowns.h"
50 test_return_t
check_replication_sanity_TEST(memcached_st
*memc
)
53 test_compare(uint64_t(1),
54 memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
57 * Make sure that we store the item on all servers
58 * (master + replicas == number of servers)
60 test_compare(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
), uint64_t(memcached_server_count(memc
) - 1));
65 test_return_t
replication_set_test(memcached_st
*memc
)
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));
72 test_compare(MEMCACHED_SUCCESS
,
73 memcached_set(memc
, "bubba", 5, "0", 1, 0, 0));
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
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
96 for (int x
= 'a'; x
<= 'z'; ++x
)
98 const char key
[2]= { (char)x
, 0 };
101 memcached_return_t rc
;
102 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
104 test_compare(MEMCACHED_SUCCESS
, rc
);
109 memcached_free(memc_clone
);
114 #include "libmemcached/instance.h"
116 test_return_t
replication_get_test(memcached_st
*memc
)
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
124 for (uint32_t host
= 0; host
< memcached_server_count(memc
); ++host
)
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
);
131 for (int x
= 'a'; x
<= 'z'; ++x
)
133 const char key
[2]= { (char)x
, 0 };
136 memcached_return_t rc
;
137 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
139 test_compare(MEMCACHED_SUCCESS
, rc
);
144 memcached_free(memc_clone
);
150 test_return_t
replication_mget_test(memcached_st
*memc
)
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));
157 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
158 size_t len
[]= { 5, 4, 4, 4 };
160 for (size_t x
= 0; x
< 4; ++x
)
162 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0));
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
178 memcached_quit(memc
);
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
185 memcached_result_st result_obj
;
186 for (uint32_t host
= 0; host
< memcached_server_count(memc_clone
); host
++)
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);
192 for (int x
= 'a'; x
<= 'z'; ++x
)
194 char key
[2]= { (char)x
, 0 };
196 test_compare(MEMCACHED_SUCCESS
,
197 memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4));
199 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
203 memcached_return_t rc
;
204 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
208 test_compare(4, hits
);
209 memcached_result_free(&result_obj
);
212 memcached_free(new_clone
);
215 memcached_free(memc_clone
);
220 test_return_t
replication_randomize_mget_test(memcached_st
*memc
)
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);
227 const char *keys
[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
228 size_t len
[]= { 4, 4, 4, 4, 4, 4, 4 };
230 for (size_t x
= 0; x
< 7; ++x
)
232 test_compare(MEMCACHED_SUCCESS
,
233 memcached_set(memc
, keys
[x
], len
[x
], "1", 1, 0, 0));
236 memcached_quit(memc
);
238 for (size_t x
= 0; x
< 7; ++x
)
240 const char key
[2]= { (char)x
, 0 };
242 test_compare(MEMCACHED_SUCCESS
,
243 memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 7));
245 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
249 memcached_return_t rc
;
250 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
254 test_compare(hits
, 7);
255 memcached_result_free(&result_obj
);
257 memcached_free(memc_clone
);
262 test_return_t
replication_delete_test(memcached_st
*memc_just_cloned
)
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" };
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));
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
));
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
));
279 for (size_t x
= 0; x
< test_array_length(keys
); ++x
)
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
287 memcached_flush_buffers(memc_replicated
);
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));
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
)
296 memcached_return_t del_rc
= memcached_delete(memc_replicated
,
297 test_string_make_from_cstr(keys
[x
]), // Keys
299 if (del_rc
== MEMCACHED_SUCCESS
or del_rc
== MEMCACHED_NOTFOUND
)
303 test_compare(MEMCACHED_SUCCESS
, del_rc
);
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
)));
310 test_zero(confirm_key_count(memc_not_replicate
));
313 memcached_free(memc_not_replicate
);
314 memcached_free(memc_replicated
);
319 test_return_t
replication_randomize_mget_fail_test(memcached_st
*memc
)
321 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
322 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 3);
324 for (int x
= int(MEMCACHED_SUCCESS
); x
< int(MEMCACHED_MAXIMUM_RETURN
); ++x
)
326 const char *key
= memcached_strerror(NULL
, memcached_return_t(x
));
327 test_compare(MEMCACHED_SUCCESS
,
330 key
, strlen(key
), 0, 0));
333 memcached_flush_buffers(memc
);
335 // We need to now cause a failure in one server, never do this in your own
337 close(memc_clone
->servers
[1].fd
);
338 memc_clone
->servers
[1].port(1);
339 memc_clone
->servers
[1].address_info_next
= NULL
;
341 for (int x
= int(MEMCACHED_SUCCESS
); x
< int(MEMCACHED_MAXIMUM_RETURN
); ++x
)
343 const char *key
= memcached_strerror(NULL
, memcached_return_t(x
));
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
);
353 memcached_free(memc_clone
);
357 /* Test that single miss does not cause replica reads to fail */
358 test_return_t
replication_miss_test(memcached_st
*memc
)
360 test_skip(true, false);
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
);
367 const char *value
= "my_value";
371 /* this test makes sense only with 2 or more servers */
372 test_true(memcached_server_count(memc_repl
) > 1);
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
));
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
)));
387 /* Write to servers */
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
);
396 /* Use the second clone to remove the key from primary server.
397 This should remove the key from only one server */
399 memcached_return_t rc
= memcached_delete(memc_single
,
400 test_literal_param(__func__
),
402 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
405 /* Remove the server where the key was deleted */
408 memcached_return_t rc
;
409 const memcached_server_st
*instance
= memcached_server_by_key(memc_single
,
410 test_literal_param(__func__
),
412 test_compare(MEMCACHED_SUCCESS
, rc
);
413 test_compare(MEMCACHED_SUCCESS
,
414 memcached_server_remove(instance
));
418 /* Test that others still have it */
420 memcached_return_t rc
;
421 char *get_value
= memcached_get(memc_single
,
422 test_literal_param(__func__
),
424 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
425 test_true(get_value
and strcmp(get_value
, value
) == 0);
429 /* This read should still return the value as we have it on other servers */
431 memcached_return_t rc
;
432 char *get_value
= memcached_get(memc_repl
,
433 test_literal_param(__func__
),
435 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
436 test_true(get_value
and strcmp(get_value
, value
) == 0);
440 memcached_free(memc_repl
);
441 memcached_free(memc_single
);