Merge in fix for reusing active memcached servers.
[awesomized/libmemcached] / libtest / server.c
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker 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
39 /*
40 Startup, and shutdown the memcached servers.
41 */
42
43 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
44
45 #include <config.h>
46
47 #include <iso646.h>
48
49 #include <assert.h>
50 #include <limits.h>
51 #include <signal.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <sys/time.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include <errno.h>
59
60 #include <libmemcached/memcached.h>
61 #include <libmemcached/util.h>
62
63 #include <libtest/server.h>
64
65 static struct timespec global_sleep_value= { .tv_sec= 0, .tv_nsec= 50000 };
66
67 static void global_sleep(void)
68 {
69 #ifdef WIN32
70 sleep(1);
71 #else
72 nanosleep(&global_sleep_value, NULL);
73 #endif
74 }
75
76 static bool wait_for_file(const char *filename)
77 {
78 uint32_t timeout= 6;
79 uint32_t waited;
80 uint32_t this_wait;
81 uint32_t retry;
82
83 for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
84 {
85 if ((! access(filename, R_OK)) || (waited >= timeout))
86 {
87 return true;
88 }
89
90 this_wait= retry * retry / 3 + 1;
91 sleep(this_wait);
92 }
93
94 return false;
95 }
96
97 static void kill_file(const char *file_buffer)
98 {
99 FILE *fp;
100
101 while ((fp= fopen(file_buffer, "r")))
102 {
103 char pid_buffer[1024];
104
105 if (fgets(pid_buffer, sizeof(pid_buffer), fp) != NULL)
106 {
107 pid_t pid= (pid_t)atoi(pid_buffer);
108 if (pid != 0)
109 {
110 if (kill(pid, SIGTERM) == -1)
111 {
112 remove(file_buffer); // If this happens we may be dealing with a dead server that left its pid file.
113 }
114 else
115 {
116 uint32_t counter= 3;
117 while ((kill(pid, 0) == 0) && --counter)
118 {
119 global_sleep();
120 }
121 }
122 }
123 }
124
125 global_sleep();
126
127 fclose(fp);
128 }
129 }
130
131 void server_startup(server_startup_st *construct)
132 {
133 if ((construct->server_list= getenv("MEMCACHED_SERVERS")))
134 {
135 printf("servers %s\n", construct->server_list);
136 construct->servers= memcached_servers_parse(construct->server_list);
137 construct->server_list= NULL;
138 construct->count= 0;
139 }
140 else
141 {
142 {
143 char server_string_buffer[8096];
144 char *end_ptr;
145 end_ptr= server_string_buffer;
146
147 uint32_t port_base= 0;
148 for (uint32_t x= 0; x < construct->count; x++)
149 {
150 int status;
151
152 snprintf(construct->pid_file[x], FILENAME_MAX, "/tmp/memcached.pidXXXXXX");
153 int fd;
154 if ((fd= mkstemp(construct->pid_file[x])) == -1)
155 {
156 perror("mkstemp");
157 return;
158 }
159 close(fd);
160
161 {
162 char *var;
163 char variable_buffer[1024];
164
165 snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
166
167 if ((var= getenv(variable_buffer)))
168 {
169 construct->port[x]= (in_port_t)atoi(var);
170 }
171 else
172 {
173 do {
174 construct->port[x]= (in_port_t)(x + TEST_PORT_BASE + port_base);
175
176 if (libmemcached_util_ping("localhost", construct->port[x], NULL))
177 {
178 if (libmemcached_util_flush("localhost", construct->port[x], NULL))
179 {
180 fprintf(stderr, "Found server on port %d, flushed it!\n", (int)construct->port[x]);
181 } // If we can flush it, we will just use it
182 else
183 {
184 fprintf(stderr, "Found server on port %d, could not flush it, so trying next port.\n", (int)construct->port[x]);
185 port_base++;
186 construct->port[x]= 0;
187 }
188 }
189 } while (construct->port[x] == 0);
190 }
191 }
192
193 char buffer[FILENAME_MAX];
194 if (x == 0)
195 {
196 snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u -m 128",
197 MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
198 }
199 else
200 {
201 snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u",
202 MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
203 }
204
205 if (libmemcached_util_ping("localhost", construct->port[x], NULL))
206 {
207 fprintf(stderr, "Server on port %u already exists\n", construct->port[x]);
208 }
209 else
210 {
211 status= system(buffer);
212 fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status);
213 }
214
215 int count;
216 size_t remaining_length= sizeof(server_string_buffer) - (size_t)(end_ptr -server_string_buffer);
217 count= snprintf(end_ptr, remaining_length, "localhost:%u,", construct->port[x]);
218
219 if ((size_t)count >= remaining_length || count < 0)
220 {
221 fprintf(stderr, "server names grew to be larger then buffer allowed\n");
222 abort();
223 }
224 end_ptr+= count;
225 }
226 *end_ptr= 0;
227
228
229 for (uint32_t x= 0; x < construct->count; x++)
230 {
231 if (! wait_for_file(construct->pid_file[x]))
232 {
233 abort();
234 }
235 }
236
237 for (uint32_t x= 0; x < construct->count; x++)
238 {
239 uint32_t counter= 3000; // Absurd, just to catch run away process
240 while (construct->pids[x] <= 0 && --counter)
241 {
242 FILE *file= fopen(construct->pid_file[x], "r");
243 if (file)
244 {
245 char pid_buffer[1024];
246 char *found= fgets(pid_buffer, sizeof(pid_buffer), file);
247
248 if (found)
249 {
250 construct->pids[x]= atoi(pid_buffer);
251 fclose(file);
252
253 if (construct->pids[x] > 0)
254 break;
255 }
256 fclose(file);
257 }
258 switch (errno)
259 {
260 default:
261 fprintf(stderr, "%s -> fopen(%s)\n", construct->pid_file[x], strerror(errno));
262 abort();
263 case ENOENT:
264 case EINTR:
265 case EACCES:
266 break;
267 case ENOTCONN:
268 continue;
269 }
270
271 // Safety 3rd, check to see if the file has gone away
272 if (! wait_for_file(construct->pid_file[x]))
273 {
274 abort();
275 }
276 }
277
278 bool was_started= false;
279 if (construct->pids[x] > 0)
280 {
281 counter= 30;
282 while (--counter)
283 {
284 if (kill(construct->pids[x], 0) == 0)
285 {
286 was_started= true;
287 break;
288 }
289 global_sleep();
290 }
291 }
292
293 if (was_started == false)
294 {
295 fprintf(stderr, "Failed to open buffer %s(%d)\n", construct->pid_file[x], construct->pids[x]);
296 for (uint32_t y= 0; y < construct->count; y++)
297 {
298 if (construct->pids[y] > 0)
299 kill(construct->pids[y], SIGTERM);
300 }
301 abort();
302 }
303 }
304
305 construct->server_list= strdup(server_string_buffer);
306 }
307 printf("servers %s\n", construct->server_list);
308 construct->servers= memcached_servers_parse(construct->server_list);
309 }
310
311 assert(construct->servers);
312
313 srandom((unsigned int)time(NULL));
314
315 for (uint32_t x= 0; x < memcached_server_list_count(construct->servers); x++)
316 {
317 printf("\t%s : %d\n", memcached_server_name(&construct->servers[x]), memcached_server_port(&construct->servers[x]));
318 assert(construct->servers[x].fd == -1);
319 assert(construct->servers[x].cursor_active == 0);
320 }
321
322 printf("\n");
323 }
324
325 void server_shutdown(server_startup_st *construct)
326 {
327 if (construct->server_list)
328 {
329 for (uint32_t x= 0; x < construct->count; x++)
330 {
331 kill_file(construct->pid_file[x]);
332 }
333
334 free(construct->server_list);
335 }
336 }