05997238bb2379624f048217d9d4b488639f22f1
[m6w6/libmemcached] / libtest / server_container.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include "mem_config.h"
38 #include <libtest/common.h>
39
40 #include <cassert>
41 #include <cerrno>
42 #include <cstdlib>
43 #include <iostream>
44
45 #include <algorithm>
46 #include <functional>
47 #include <locale>
48
49 // trim from end
50 static inline std::string &rtrim(std::string &s)
51 {
52 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
53 return s;
54 }
55
56 namespace libtest {
57
58 Server* server_startup_st::last()
59 {
60 return servers.back();
61 }
62
63 void server_startup_st::push_server(Server *arg)
64 {
65 servers.push_back(arg);
66
67 std::string server_config_string;
68 if (arg->has_socket())
69 {
70 server_config_string+= "--socket=";
71 server_config_string+= '"';
72 server_config_string+= arg->socket();
73 server_config_string+= '"';
74 server_config_string+= " ";
75 }
76 else
77 {
78 char port_str[NI_MAXSERV]= { 0 };
79 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
80
81 server_config_string+= "--server=";
82 server_config_string+= arg->hostname();
83 server_config_string+= ":";
84 server_config_string+= port_str;
85 server_config_string+= " ";
86 }
87
88 server_list+= server_config_string;
89
90 }
91
92 Server* server_startup_st::pop_server()
93 {
94 Server *tmp= servers.back();
95 servers.pop_back();
96 return tmp;
97 }
98
99 // host_to_shutdown => host number to shutdown in array
100 bool server_startup_st::shutdown(uint32_t host_to_shutdown)
101 {
102 if (servers.size() > host_to_shutdown)
103 {
104 Server* tmp= servers[host_to_shutdown];
105
106 if (tmp and tmp->kill() == false)
107 { }
108 else
109 {
110 return true;
111 }
112 }
113
114 return false;
115 }
116
117 void server_startup_st::clear()
118 {
119 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
120 {
121 delete *iter;
122 }
123 servers.clear();
124 }
125
126 bool server_startup_st::check() const
127 {
128 bool success= true;
129 for (std::vector<Server *>::const_iterator iter= servers.begin(); iter != servers.end(); ++iter)
130 {
131 if ((*iter)->check() == false)
132 {
133 success= false;
134 }
135 }
136
137 return success;
138 }
139
140 bool server_startup_st::shutdown()
141 {
142 bool success= true;
143 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
144 {
145 if ((*iter)->has_pid() and (*iter)->kill() == false)
146 {
147 Error << "Unable to kill:" << *(*iter);
148 success= false;
149 }
150 }
151
152 return success;
153 }
154
155 void server_startup_st::restart()
156 {
157 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
158 {
159 (*iter)->start();
160 }
161 }
162
163 #define MAGIC_MEMORY 123575
164 server_startup_st::server_startup_st() :
165 _magic(MAGIC_MEMORY),
166 _socket(false),
167 _sasl(false),
168 _count(0),
169 udp(0),
170 _servers_to_run(5)
171 { }
172
173 server_startup_st::~server_startup_st()
174 {
175 clear();
176 }
177
178 bool server_startup_st::validate()
179 {
180 return _magic == MAGIC_MEMORY;
181 }
182
183 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[], const bool opt_startup_message)
184 {
185 return construct.start_server(server_type, try_port, argc, argv, opt_startup_message);
186 }
187
188 bool server_startup_st::start_server(const std::string& server_type, in_port_t try_port, int argc, const char *argv[], const bool opt_startup_message)
189 {
190 if (try_port <= 0)
191 {
192 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
193 }
194
195 libtest::Server *server= NULL;
196 try {
197 if (0)
198 { }
199 else if (server_type.compare("gearmand") == 0)
200 {
201 if (GEARMAND_BINARY)
202 {
203 if (HAVE_LIBGEARMAN)
204 {
205 server= build_gearmand("localhost", try_port);
206 }
207 }
208 }
209 else if (server_type.compare("hostile-gearmand") == 0)
210 {
211 if (GEARMAND_BINARY)
212 {
213 if (HAVE_LIBGEARMAN)
214 {
215 server= build_gearmand("localhost", try_port, "gearmand/hostile_gearmand");
216 }
217 }
218 }
219 else if (server_type.compare("drizzled") == 0)
220 {
221 if (DRIZZLED_BINARY)
222 {
223 if (HAVE_LIBDRIZZLE)
224 {
225 server= build_drizzled("localhost", try_port);
226 }
227 }
228 }
229 else if (server_type.compare("blobslap_worker") == 0)
230 {
231 if (GEARMAND_BINARY)
232 {
233 if (GEARMAND_BLOBSLAP_WORKER)
234 {
235 if (HAVE_LIBGEARMAN)
236 {
237 server= build_blobslap_worker(try_port);
238 }
239 }
240 }
241 }
242 else if (server_type.compare("memcached-sasl") == 0)
243 {
244 if (MEMCACHED_BINARY)
245 {
246 if (HAVE_LIBMEMCACHED)
247 {
248 server= build_memcached_sasl("localhost", try_port, username(), password());
249 }
250 }
251 }
252 else if (server_type.compare("memcached") == 0)
253 {
254 if (HAVE_MEMCACHED_BINARY)
255 {
256 if (HAVE_LIBMEMCACHED)
257 {
258 server= build_memcached("localhost", try_port);
259 }
260 }
261 }
262
263 if (server == NULL)
264 {
265 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Launching of an unknown server was attempted: %s", server_type.c_str());
266 }
267 }
268 catch (...)
269 {
270 throw;
271 }
272
273 try {
274 /*
275 We will now cycle the server we have created.
276 */
277 if (server->cycle() == false)
278 {
279 Error << "Could not start up server " << *server;
280 delete server;
281 return false;
282 }
283
284 server->build(argc, argv);
285
286 #if 0
287 if (false)
288 {
289 Out << "Pausing for startup, hit return when ready.";
290 std::string gdb_command= server->base_command();
291 getchar();
292 }
293 else
294 #endif
295
296 if (server->start() == false)
297 {
298 delete server;
299 return false;
300 }
301 else
302 {
303 if (opt_startup_message)
304 {
305 Outn();
306 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
307 Outn();
308 }
309 }
310 }
311 catch (libtest::disconnected& err)
312 {
313 if (fatal::is_disabled() == false and try_port != LIBTEST_FAIL_PORT)
314 {
315 stream::cerr(err.file(), err.line(), err.func()) << err.what();
316 delete server;
317 return false;
318 }
319 }
320 catch (...)
321 {
322 delete server;
323 throw;
324 }
325
326 push_server(server);
327
328 return true;
329 }
330
331 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc,
332 const char *argv[],
333 const bool opt_startup_message)
334 {
335 (void)try_port;
336 Outn();
337
338 Server *server= NULL;
339 try {
340 if (0)
341 { }
342 else if (server_type.compare("gearmand") == 0)
343 {
344 Error << "Socket files are not supported for gearmand yet";
345 }
346 else if (server_type.compare("memcached") == 0)
347 {
348 if (MEMCACHED_BINARY)
349 {
350 if (HAVE_LIBMEMCACHED)
351 {
352 server= build_memcached_socket("localhost", try_port);
353 }
354 else
355 {
356 Error << "Libmemcached was not found";
357 }
358 }
359 else
360 {
361 Error << "No memcached binary is available";
362 }
363 }
364 else
365 {
366 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
367 }
368
369 if (server == NULL)
370 {
371 Error << "Failure occured while creating server: " << server_type;
372 return false;
373 }
374
375 /*
376 We will now cycle the server we have created.
377 */
378 if (server->cycle() == false)
379 {
380 Error << "Could not start up server " << *server;
381 delete server;
382 return false;
383 }
384
385 server->build(argc, argv);
386
387 #if 0
388 if (false)
389 {
390 Out << "Pausing for startup, hit return when ready.";
391 std::string gdb_command= server->base_command();
392 std::string options;
393 Out << "run " << server->args(options);
394 getchar();
395 }
396 else
397 #endif
398 if (server->start() == false)
399 {
400 Error << "Failed to start " << *server;
401 delete server;
402 return false;
403 }
404 else
405 {
406 if (opt_startup_message)
407 {
408 Outn();
409 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
410 Outn();
411 }
412 }
413 }
414 catch (...)
415 {
416 delete server;
417 throw;
418 }
419
420 push_server(server);
421
422 set_default_socket(server->socket().c_str());
423
424 Outn();
425
426 return true;
427 }
428
429 std::string server_startup_st::option_string() const
430 {
431 std::string temp= server_list;
432 rtrim(temp);
433 return temp;
434 }
435
436
437 } // namespace libtest