Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-157
[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 "libtest/yatlcon.h"
38
39 #include "libtest/common.h"
40
41 #include <cassert>
42 #include <cerrno>
43 #include <cstdlib>
44 #include <iostream>
45
46 #include <algorithm>
47 #include <functional>
48 #include <locale>
49
50 // trim from end
51 static inline std::string &rtrim(std::string &s)
52 {
53 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
54 return s;
55 }
56
57 namespace libtest {
58
59 Server* server_startup_st::last()
60 {
61 return servers.back();
62 }
63
64 void server_startup_st::push_server(Server *arg)
65 {
66 servers.push_back(arg);
67
68 std::string server_config_string;
69 if (arg->has_socket())
70 {
71 server_config_string+= "--socket=";
72 server_config_string+= '"';
73 server_config_string+= arg->socket();
74 server_config_string+= '"';
75 server_config_string+= " ";
76 }
77 else
78 {
79 libtest::vchar_t port_str;
80 port_str.resize(NI_MAXSERV);
81 snprintf(&port_str[0], port_str.size(), "%u", int(arg->port()));
82
83 server_config_string+= "--server=";
84 server_config_string+= arg->hostname();
85 server_config_string+= ":";
86 server_config_string+= &port_str[0];
87 server_config_string+= " ";
88 }
89
90 server_list+= server_config_string;
91
92 }
93
94 Server* server_startup_st::pop_server()
95 {
96 Server *tmp= servers.back();
97 servers.pop_back();
98 return tmp;
99 }
100
101 // host_to_shutdown => host number to shutdown in array
102 bool server_startup_st::shutdown(uint32_t host_to_shutdown)
103 {
104 if (servers.size() > host_to_shutdown)
105 {
106 Server* tmp= servers[host_to_shutdown];
107
108 if (tmp and tmp->kill() == false)
109 { }
110 else
111 {
112 return true;
113 }
114 }
115
116 return false;
117 }
118
119 void server_startup_st::clear()
120 {
121 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
122 {
123 delete *iter;
124 }
125 servers.clear();
126 }
127
128 bool server_startup_st::check() const
129 {
130 bool success= true;
131 for (std::vector<Server *>::const_iterator iter= servers.begin(); iter != servers.end(); ++iter)
132 {
133 if ((*iter)->check() == false)
134 {
135 success= false;
136 }
137 }
138
139 return success;
140 }
141
142 bool server_startup_st::shutdown()
143 {
144 bool success= true;
145 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
146 {
147 if ((*iter)->has_pid() and (*iter)->kill() == false)
148 {
149 Error << "Unable to kill:" << *(*iter);
150 success= false;
151 }
152 }
153
154 return success;
155 }
156
157 void server_startup_st::restart()
158 {
159 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
160 {
161 (*iter)->start();
162 }
163 }
164
165 #define MAGIC_MEMORY 123575
166 server_startup_st::server_startup_st() :
167 _magic(MAGIC_MEMORY),
168 _socket(false),
169 _sasl(false),
170 _count(0),
171 udp(0),
172 _servers_to_run(5)
173 { }
174
175 server_startup_st::~server_startup_st()
176 {
177 clear();
178 }
179
180 bool server_startup_st::validate()
181 {
182 return _magic == MAGIC_MEMORY;
183 }
184
185 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)
186 {
187 return construct.start_server(server_type, try_port, argc, argv, opt_startup_message);
188 }
189
190 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)
191 {
192 if (try_port <= 0)
193 {
194 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
195 }
196
197 libtest::Server *server= NULL;
198 try {
199 if (0)
200 { }
201 else if (server_type.compare("gearmand") == 0)
202 {
203 if (GEARMAND_BINARY)
204 {
205 if (HAVE_LIBGEARMAN)
206 {
207 server= build_gearmand("localhost", try_port);
208 }
209 }
210 }
211 else if (server_type.compare("hostile-gearmand") == 0)
212 {
213 if (GEARMAND_BINARY)
214 {
215 if (HAVE_LIBGEARMAN)
216 {
217 server= build_gearmand("localhost", try_port, "gearmand/hostile_gearmand");
218 }
219 }
220 }
221 else if (server_type.compare("drizzled") == 0)
222 {
223 if (DRIZZLED_BINARY)
224 {
225 if (HAVE_LIBDRIZZLE)
226 {
227 server= build_drizzled("localhost", try_port);
228 }
229 }
230 }
231 else if (server_type.compare("blobslap_worker") == 0)
232 {
233 if (GEARMAND_BINARY)
234 {
235 if (GEARMAND_BLOBSLAP_WORKER)
236 {
237 if (HAVE_LIBGEARMAN)
238 {
239 server= build_blobslap_worker(try_port);
240 }
241 }
242 }
243 }
244 else if (server_type.compare("memcached") == 0)
245 {
246 if (HAVE_MEMCACHED_BINARY)
247 {
248 if (HAVE_LIBMEMCACHED)
249 {
250 server= build_memcached("localhost", try_port);
251 }
252 }
253 }
254
255 if (server == NULL)
256 {
257 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Launching of an unknown server was attempted: %s", server_type.c_str());
258 }
259 }
260 catch (...)
261 {
262 throw;
263 }
264
265 try {
266 /*
267 We will now cycle the server we have created.
268 */
269 if (server->cycle() == false)
270 {
271 Error << "Could not start up server " << *server;
272 delete server;
273 return false;
274 }
275
276 server->build(argc, argv);
277
278 #if 0
279 if (false)
280 {
281 Out << "Pausing for startup, hit return when ready.";
282 std::string gdb_command= server->base_command();
283 getchar();
284 }
285 else
286 #endif
287
288 if (server->start() == false)
289 {
290 delete server;
291 return false;
292 }
293 else
294 {
295 if (opt_startup_message)
296 {
297 #if defined(DEBUG)
298 if (DEBUG)
299 {
300 Outn();
301 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
302 Outn();
303 }
304 #endif
305 }
306 }
307 }
308 catch (libtest::disconnected& err)
309 {
310 if (fatal::is_disabled() == false and try_port != LIBTEST_FAIL_PORT)
311 {
312 stream::cerr(err.file(), err.line(), err.func()) << err.what();
313 delete server;
314 return false;
315 }
316 }
317 catch (...)
318 {
319 delete server;
320 throw;
321 }
322
323 push_server(server);
324
325 return true;
326 }
327
328 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc,
329 const char *argv[],
330 const bool opt_startup_message)
331 {
332 (void)try_port;
333 Outn();
334
335 Server *server= NULL;
336 try {
337 if (0)
338 { }
339 else if (server_type.compare("gearmand") == 0)
340 {
341 Error << "Socket files are not supported for gearmand yet";
342 }
343 else if (server_type.compare("memcached") == 0)
344 {
345 if (MEMCACHED_BINARY)
346 {
347 if (HAVE_LIBMEMCACHED)
348 {
349 server= build_memcached_socket("localhost", try_port);
350 }
351 else
352 {
353 Error << "Libmemcached was not found";
354 }
355 }
356 else
357 {
358 Error << "No memcached binary is available";
359 }
360 }
361 else
362 {
363 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
364 }
365
366 if (server == NULL)
367 {
368 Error << "Failure occured while creating server: " << server_type;
369 return false;
370 }
371
372 /*
373 We will now cycle the server we have created.
374 */
375 if (server->cycle() == false)
376 {
377 Error << "Could not start up server " << *server;
378 delete server;
379 return false;
380 }
381
382 server->build(argc, argv);
383
384 #if 0
385 if (false)
386 {
387 Out << "Pausing for startup, hit return when ready.";
388 std::string gdb_command= server->base_command();
389 std::string options;
390 Out << "run " << server->args(options);
391 getchar();
392 }
393 else
394 #endif
395 if (server->start() == false)
396 {
397 Error << "Failed to start " << *server;
398 delete server;
399 return false;
400 }
401 else
402 {
403 if (opt_startup_message)
404 {
405 #if defined(DEBUG)
406 if (DEBUG)
407 {
408 Outn();
409 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
410 Outn();
411 }
412 #endif
413 }
414 }
415 }
416 catch (...)
417 {
418 delete server;
419 throw;
420 }
421
422 push_server(server);
423
424 set_default_socket(server->socket().c_str());
425
426 Outn();
427
428 return true;
429 }
430
431 std::string server_startup_st::option_string() const
432 {
433 std::string temp= server_list;
434 rtrim(temp);
435 return temp;
436 }
437
438
439 } // namespace libtest