Skip to content
Newer
Older
100644 259 lines (215 sloc) 7.07 KB
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3 * Copyright 2011 Couchbase, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
18 #include "internal.h" /* lcb_t definition */
57dc552 @trondn Add support for multiple hosts for the bootstrap URL
trondn authored Feb 14, 2012
19
20
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
21 #include <string.h>
22 #include <assert.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <stdlib.h>
26
27 #include "server.h"
28 #include "test.h"
ac51b22 @trondn Tests should finish within 5 minutes
trondn authored Sep 13, 2012
29 #include "testutil.h"
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
30
31 int config_cnt;
32 int store_cnt;
33
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
34 static void error_callback(lcb_t instance,
35 lcb_error_t err,
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
36 const char *errinfo)
37 {
38 (void)instance;
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
39 err_exit("Error %s: %s", lcb_strerror(instance, err), errinfo);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
40 }
41
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
42 static void vbucket_state_callback(lcb_server_t *server)
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
43 {
44 config_cnt++;
45 server->instance->io->stop_event_loop(server->instance->io);
46 }
47
be2e201 @avsej Reformat tests
avsej authored Jun 2, 2012
48 struct rvbuf {
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
49 lcb_error_t error;
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
50 const char *bytes;
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
51 lcb_size_t nbytes;
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
52 };
53
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
54 static void store_callback(lcb_t instance,
55 const void *cookie,
56 lcb_storage_t operation,
57 lcb_error_t error,
58 const lcb_store_resp_t *resp)
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
59 {
60 struct rvbuf *rv = (struct rvbuf *)cookie;
61 rv->error = error;
62 store_cnt++;
63 instance->io->stop_event_loop(instance->io);
64
65 (void)operation;
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
66 (void)resp;
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
67 }
68
69
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
70 static void get_callback(lcb_t instance,
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
71 const void *cookie,
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
72 lcb_error_t error,
73 const lcb_get_resp_t *resp)
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
74 {
75 struct rvbuf *rv = (struct rvbuf *)cookie;
76 rv->error = error;
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
77 rv->bytes = malloc(resp->v.v0.nbytes);
78 memcpy((void *)rv->bytes, resp->v.v0.bytes, resp->v.v0.nbytes);
79 rv->nbytes = resp->v.v0.nbytes;
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
80 instance->io->stop_event_loop(instance->io);
81
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
82 (void)resp;
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
83 }
84
85 static void smoke_test(void)
86 {
ed20aeb @trondn Refactor: lcb_create_io_ops should take struct
trondn authored Sep 3, 2012
87 lcb_io_opt_t io;
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
88 const void *mock;
89 const char *endpoint;
90 const char *argv[] = {"--nodes", "20", NULL};
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
91 lcb_t instance;
0c923bd @trondn Refactor: lcb_create should use struct
trondn authored Sep 3, 2012
92 struct lcb_create_st options;
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
93
aadf8e0 @mnunberg Allow tests to be run against a real cluster
mnunberg authored Sep 3, 2012
94 if (is_using_real_cluster()) {
95 fprintf(stderr, "Skipping \"%s\" while testing towards real cluster\n",
96 __func__);
97 return;
98 }
99
100 mock = start_test_server((char **)argv);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
101 if (mock == NULL) {
102 err_exit("Failed to start mock server");
103 }
104
9e34abc @avsej Remove tests/loopfactory
avsej authored Sep 26, 2012
105 if (lcb_create_io_ops(&io, NULL) != LCB_SUCCESS) {
106 fprintf(stderr, "Failed to create IO instance\n");
107 exit(1);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
108 }
109
110 endpoint = get_mock_http_server(mock);
0c923bd @trondn Refactor: lcb_create should use struct
trondn authored Sep 3, 2012
111 memset(&options, 0, sizeof(options));
112 options.v.v0.host = endpoint;
113 options.v.v0.user = "Administrator";
114 options.v.v0.passwd = "password";
115 options.v.v0.io = io;
116
117 if (lcb_create(&instance, &options) != LCB_SUCCESS) {
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
118 err_exit("Failed to create libcouchbase instance");
119 }
120
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
121 (void)lcb_set_error_callback(instance, error_callback);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
122 instance->vbucket_state_listener = vbucket_state_callback;
123
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
124 if (lcb_connect(instance) != LCB_SUCCESS) {
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
125 err_exit("Failed to connect libcouchbase instance to server");
126 }
127 config_cnt = 0;
128 io->run_event_loop(io);
129 assert(config_cnt == 20);
130
131 config_cnt = 0;
300bcd5 @avsej Allow to select bucket for node failover/respawn
avsej authored Dec 16, 2011
132 failover_node(mock, 0, NULL);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
133 io->run_event_loop(io);
134 assert(config_cnt == 19);
135
136 config_cnt = 0;
300bcd5 @avsej Allow to select bucket for node failover/respawn
avsej authored Dec 16, 2011
137 respawn_node(mock, 0, NULL);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
138 io->run_event_loop(io);
139 assert(config_cnt == 20);
140
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
141 lcb_destroy(instance);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
142 shutdown_mock_server(mock);
143 }
144
145 static void buffer_relocation_test(void)
146 {
ed20aeb @trondn Refactor: lcb_create_io_ops should take struct
trondn authored Sep 3, 2012
147 lcb_io_opt_t io;
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
148 const void *mock;
149 const char *endpoint;
150 const char *argv[] = {"--nodes", "2", NULL};
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
151 lcb_t instance;
152 lcb_error_t err;
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
153 struct rvbuf rv;
154 const char *key = "foo", *val = "bar";
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
155 lcb_size_t nkey = strlen(key), nval = strlen(val);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
156 int vb, idx;
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
157 lcb_store_cmd_t storecmd;
158 const lcb_store_cmd_t *storecmds[] = { &storecmd };
159 lcb_get_cmd_t getcmd;
160 const lcb_get_cmd_t *getcmds[] = { &getcmd };
0c923bd @trondn Refactor: lcb_create should use struct
trondn authored Sep 3, 2012
161 struct lcb_create_st options;
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
162
aadf8e0 @mnunberg Allow tests to be run against a real cluster
mnunberg authored Sep 4, 2012
163 if (is_using_real_cluster()) {
164 fprintf(stderr, "Skipping \"%s\" while testing towards real cluster\n",
165 __func__);
166 return;
167 }
168
169 mock = start_test_server((char **)argv);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
170 if (mock == NULL) {
171 err_exit("Failed to start mock server");
172 }
173
9e34abc @avsej Remove tests/loopfactory
avsej authored Sep 26, 2012
174 if (lcb_create_io_ops(&io, NULL) != LCB_SUCCESS) {
175 fprintf(stderr, "Failed to create IO instance\n");
176 exit(1);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
177 }
178
179 endpoint = get_mock_http_server(mock);
0c923bd @trondn Refactor: lcb_create should use struct
trondn authored Sep 3, 2012
180 memset(&options, 0, sizeof(options));
181 options.v.v0.host = endpoint;
182 options.v.v0.user = "Administrator";
183 options.v.v0.passwd = "password";
184 options.v.v0.io = io;
185
186 if (lcb_create(&instance, &options) != LCB_SUCCESS) {
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
187 err_exit("Failed to create libcouchbase instance");
188 }
189
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
190 (void)lcb_set_error_callback(instance, error_callback);
191 (void)lcb_set_store_callback(instance, store_callback);
192 (void)lcb_set_get_callback(instance, get_callback);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
193 instance->vbucket_state_listener = vbucket_state_callback;
194
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
195 if (lcb_connect(instance) != LCB_SUCCESS) {
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
196 err_exit("Failed to connect libcouchbase instance to server");
197 }
198 io->run_event_loop(io);
199
200 /* schedule SET operation */
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
201
202 memset(&storecmd, 0, sizeof(storecmd));
203 storecmd.v.v0.key = key;
204 storecmd.v.v0.nkey = nkey;
205 storecmd.v.v0.bytes = val;
206 storecmd.v.v0.nbytes = nval;
207 storecmd.v.v0.operation = LCB_SET;
208 err = lcb_store(instance, &rv, 1, storecmds);
209 assert(err == LCB_SUCCESS);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
210
211 /* determine what server should receive that operation */
212 vb = vbucket_get_vbucket_by_key(instance->vbucket_config, key, nkey);
213 idx = instance->vb_server_map[vb];
214
215 /* switch off that server */
300bcd5 @avsej Allow to select bucket for node failover/respawn
avsej authored Dec 16, 2011
216 failover_node(mock, idx, NULL);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
217
218 /* execute event loop to reconfigure client and execute operation */
219 config_cnt = 0;
220 store_cnt = 0;
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
221 /* it should never return LCB_NOT_MY_VBUCKET */
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
222 while (config_cnt == 0 || store_cnt == 0) {
223 memset(&rv, 0, sizeof(rv));
224 io->run_event_loop(io);
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
225 assert(err != LCB_NOT_MY_VBUCKET);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
226 }
227
228 /* check that value was actually set */
229 memset(&rv, 0, sizeof(rv));
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
230 memset(&getcmd, 0, sizeof(getcmd));
231 getcmd.v.v0.key = key;
232 getcmd.v.v0.nkey = nkey;
233 err = lcb_get(instance, &rv, 1, getcmds);
234 assert(err == LCB_SUCCESS);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
235 io->run_event_loop(io);
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
236 assert(rv.error == LCB_SUCCESS);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
237 assert(memcmp(rv.bytes, "bar", 3) == 0);
19a3e34 @trondn Refactor the API
trondn authored Aug 23, 2012
238 free((void *)rv.bytes);
239 lcb_destroy(instance);
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
240 shutdown_mock_server(mock);
241 }
242
243 int main(int argc, char **argv)
244 {
be2e201 @avsej Reformat tests
avsej authored Jun 2, 2012
245 (void)argc;
246 (void)argv;
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
247
ac51b22 @trondn Tests should finish within 5 minutes
trondn authored Sep 13, 2012
248 setup_test_timeout_handler();
249
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
250 if (getenv("LIBCOUCHBASE_VERBOSE_TESTS") == NULL) {
251 freopen("/dev/null", "w", stdout);
252 }
253
4a9e55b @avsej Uncomment smoke test
avsej authored Jan 6, 2012
254 smoke_test();
8891685 @avsej Gracefully update vbucket configuration
avsej authored Nov 9, 2011
255 buffer_relocation_test();
256
257 return EXIT_SUCCESS;
258 }
Something went wrong with that request. Please try again.