94f0eba0b692a73896cfaca7deac3d39a0545db5
[m6w6/libmemcached] / tests / 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 test_return_t replication_set_test(memcached_st *memc)
49 {
50 memcached_return_t rc;
51 memcached_st *memc_clone= memcached_clone(NULL, memc);
52 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
53
54 test_compare(MEMCACHED_SUCCESS,
55 memcached_set(memc, "bubba", 5, "0", 1, 0, 0));
56
57 /*
58 ** We are using the quiet commands to store the replicas, so we need
59 ** to ensure that all of them are processed before we can continue.
60 ** In the test we go directly from storing the object to trying to
61 ** receive the object from all of the different servers, so we
62 ** could end up in a race condition (the memcached server hasn't yet
63 ** processed the quiet command from the replication set when it process
64 ** the request from the other client (created by the clone)). As a
65 ** workaround for that we call memcached_quit to send the quit command
66 ** to the server and wait for the response ;-) If you use the test code
67 ** as an example for your own code, please note that you shouldn't need
68 ** to do this ;-)
69 */
70 memcached_quit(memc);
71
72 /*
73 ** "bubba" should now be stored on all of our servers. We don't have an
74 ** easy to use API to address each individual server, so I'll just iterate
75 ** through a bunch of "master keys" and I should most likely hit all of the
76 ** servers...
77 */
78 for (int x= 'a'; x <= 'z'; ++x)
79 {
80 const char key[2]= { (char)x, 0 };
81 size_t len;
82 uint32_t flags;
83 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
84 &len, &flags, &rc);
85 test_compare(MEMCACHED_SUCCESS, rc);
86 test_true(val);
87 free(val);
88 }
89
90 memcached_free(memc_clone);
91
92 return TEST_SUCCESS;
93 }
94
95 test_return_t replication_get_test(memcached_st *memc)
96 {
97 memcached_return_t rc;
98
99 /*
100 * Don't do the following in your code. I am abusing the internal details
101 * within the library, and this is not a supported interface.
102 * This is to verify correct behavior in the library
103 */
104 for (uint32_t host= 0; host < memcached_server_count(memc); ++host)
105 {
106 memcached_st *memc_clone= memcached_clone(NULL, memc);
107 memcached_server_instance_st instance=
108 memcached_server_instance_by_position(memc_clone, host);
109
110 ((memcached_server_write_instance_st)instance)->port= 0;
111
112 for (int x= 'a'; x <= 'z'; ++x)
113 {
114 const char key[2]= { (char)x, 0 };
115 size_t len;
116 uint32_t flags;
117 char *val= memcached_get_by_key(memc_clone, key, 1, "bubba", 5,
118 &len, &flags, &rc);
119 test_true(rc == MEMCACHED_SUCCESS);
120 test_true(val != NULL);
121 free(val);
122 }
123
124 memcached_free(memc_clone);
125 }
126
127 return TEST_SUCCESS;
128 }
129
130 test_return_t replication_mget_test(memcached_st *memc)
131 {
132 memcached_return_t rc;
133 memcached_st *memc_clone= memcached_clone(NULL, memc);
134 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0);
135
136 const char *keys[]= { "bubba", "key1", "key2", "key3" };
137 size_t len[]= { 5, 4, 4, 4 };
138
139 for (size_t x= 0; x< 4; ++x)
140 {
141 rc= memcached_set(memc, keys[x], len[x], "0", 1, 0, 0);
142 test_true(rc == MEMCACHED_SUCCESS);
143 }
144
145 /*
146 ** We are using the quiet commands to store the replicas, so we need
147 ** to ensure that all of them are processed before we can continue.
148 ** In the test we go directly from storing the object to trying to
149 ** receive the object from all of the different servers, so we
150 ** could end up in a race condition (the memcached server hasn't yet
151 ** processed the quiet command from the replication set when it process
152 ** the request from the other client (created by the clone)). As a
153 ** workaround for that we call memcached_quit to send the quit command
154 ** to the server and wait for the response ;-) If you use the test code
155 ** as an example for your own code, please note that you shouldn't need
156 ** to do this ;-)
157 */
158 memcached_quit(memc);
159
160 /*
161 * Don't do the following in your code. I am abusing the internal details
162 * within the library, and this is not a supported interface.
163 * This is to verify correct behavior in the library
164 */
165 memcached_result_st result_obj;
166 for (uint32_t host= 0; host < memcached_server_count(memc_clone); host++)
167 {
168 memcached_st *new_clone= memcached_clone(NULL, memc);
169 memcached_server_instance_st instance=
170 memcached_server_instance_by_position(new_clone, host);
171 ((memcached_server_write_instance_st)instance)->port= 0;
172
173 for (int x= 'a'; x <= 'z'; ++x)
174 {
175 char key[2]= { (char)x, 0 };
176
177 rc= memcached_mget_by_key(new_clone, key, 1, keys, len, 4);
178 test_true(rc == MEMCACHED_SUCCESS);
179
180 memcached_result_st *results= memcached_result_create(new_clone, &result_obj);
181 test_true(results);
182
183 int hits= 0;
184 while ((results= memcached_fetch_result(new_clone, &result_obj, &rc)) != NULL)
185 {
186 hits++;
187 }
188 test_true(hits == 4);
189 memcached_result_free(&result_obj);
190 }
191
192 memcached_free(new_clone);
193 }
194
195 memcached_free(memc_clone);
196
197 return TEST_SUCCESS;
198 }
199
200 test_return_t replication_randomize_mget_test(memcached_st *memc)
201 {
202 memcached_result_st result_obj;
203 memcached_return_t rc;
204 memcached_st *memc_clone= memcached_clone(NULL, memc);
205 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 3);
206 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, 1);
207
208 const char *keys[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
209 size_t len[]= { 4, 4, 4, 4, 4, 4, 4 };
210
211 for (size_t x= 0; x< 7; ++x)
212 {
213 rc= memcached_set(memc, keys[x], len[x], "1", 1, 0, 0);
214 test_true(rc == MEMCACHED_SUCCESS);
215 }
216
217 memcached_quit(memc);
218
219 for (size_t x= 0; x< 7; ++x)
220 {
221 const char key[2]= { (char)x, 0 };
222
223 test_compare(MEMCACHED_SUCCESS,
224 memcached_mget_by_key(memc_clone, key, 1, keys, len, 7));
225
226 memcached_result_st *results= memcached_result_create(memc_clone, &result_obj);
227 test_true(results);
228
229 int hits= 0;
230 while ((results= memcached_fetch_result(memc_clone, &result_obj, &rc)) != NULL)
231 {
232 ++hits;
233 }
234 test_compare(hits, 7);
235 memcached_result_free(&result_obj);
236 }
237 memcached_free(memc_clone);
238 return TEST_SUCCESS;
239 }
240
241 test_return_t replication_delete_test(memcached_st *memc_just_cloned)
242 {
243 memcached_flush(memc_just_cloned, 0);
244 memcached_st *memc_not_replicate= memcached_clone(NULL, memc_just_cloned);
245 memcached_st *memc_replicated= memcached_clone(NULL, memc_just_cloned);
246 const char *keys[]= { "bubba", "key1", "key2", "key3", "key4" };
247
248 test_true(memcached_behavior_get(memc_replicated, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL));
249 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc_replicated, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, false));
250
251 // Make one copy
252 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc_replicated, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 1UL));
253 test_compare(uint64_t(1), memcached_behavior_get(memc_replicated, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS));
254
255 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc_not_replicate, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 0UL));
256 test_compare(uint64_t(0), memcached_behavior_get(memc_not_replicate, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS));
257
258 for (size_t x= 0; x < test_array_length(keys); ++x)
259 {
260 memcached_set(memc_replicated,
261 test_string_make_from_cstr(keys[x]), // Keys
262 test_string_make_from_cstr(keys[x]), // We use the keys as values
263 0, 0);
264 }
265
266 memcached_flush_buffers(memc_replicated);
267
268 // Confirm keys with replication read
269 test_compare(TEST_SUCCESS, confirm_keys_exist(memc_replicated, keys, test_array_length(keys), true));
270 test_compare(TEST_SUCCESS, confirm_keys_exist(memc_not_replicate, keys, test_array_length(keys), true));
271
272 /* Delete the items from all of the servers except 1, we use the non replicated memc so that we know we deleted the keys */
273 for (size_t x= 0; x < test_array_length(keys); ++x)
274 {
275 test_compare(MEMCACHED_SUCCESS,
276 memcached_delete(memc_replicated,
277 test_string_make_from_cstr(keys[x]), // Keys
278 0));
279 }
280
281 test_compare(TEST_SUCCESS, confirm_keys_dont_exist(memc_replicated, keys, test_array_length(keys)));
282 test_compare(TEST_SUCCESS, confirm_keys_dont_exist(memc_not_replicate, keys, test_array_length(keys)));
283 #if 0
284 test_zero(confirm_key_count(memc_not_replicate));
285 #endif
286
287 memcached_free(memc_not_replicate);
288 memcached_free(memc_replicated);
289
290 return TEST_SUCCESS;
291 }
292
293 test_return_t replication_randomize_mget_fail_test(memcached_st *memc)
294 {
295 memcached_st *memc_clone= memcached_clone(NULL, memc);
296 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 3);
297
298 for (int x= int(MEMCACHED_SUCCESS); x < int(MEMCACHED_MAXIMUM_RETURN); ++x)
299 {
300 const char *key= memcached_strerror(NULL, memcached_return_t(x));
301 test_compare(MEMCACHED_SUCCESS,
302 memcached_set(memc,
303 key, strlen(key),
304 key, strlen(key), 0, 0));
305 }
306
307 memcached_flush_buffers(memc);
308
309 // We need to now cause a failure in one server, never do this in your own
310 // code.
311 close(memc_clone->servers[1].fd);
312 memc_clone->servers[1].port= 1;
313 memc_clone->servers[1].address_info_next= NULL;
314
315 for (int x= int(MEMCACHED_SUCCESS); x < int(MEMCACHED_MAXIMUM_RETURN); ++x)
316 {
317 const char *key= memcached_strerror(NULL, memcached_return_t(x));
318 memcached_return_t rc;
319 uint32_t flags;
320 size_t value_length;
321 char *value= memcached_get(memc_clone, key, strlen(key), &value_length, &flags, &rc);
322 test_true(rc == MEMCACHED_SUCCESS);
323 test_compare(strlen(key), value_length);
324 test_strcmp(key, value);
325 free(value);
326 }
327 memcached_free(memc_clone);
328 return TEST_SUCCESS;
329 }