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