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