Merging bzr://gaz.tangent.org/libmemcached/build/ to Build branch
[awesomized/libmemcached] / libtest / server_container.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <config.h>
23 #include <libtest/common.h>
24
25 #include <cassert>
26 #include <cerrno>
27 #include <cstdlib>
28 #include <iostream>
29
30 #include <algorithm>
31 #include <functional>
32 #include <locale>
33
34 // trim from end
35 static inline std::string &rtrim(std::string &s)
36 {
37 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
38 return s;
39 }
40
41 namespace libtest {
42
43 void server_startup_st::push_server(Server *arg)
44 {
45 servers.push_back(arg);
46
47 char port_str[NI_MAXSERV];
48 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
49
50 std::string server_config_string;
51 if (arg->has_socket())
52 {
53 server_config_string+= "--socket=";
54 server_config_string+= '"';
55 server_config_string+= arg->socket();
56 server_config_string+= '"';
57 server_config_string+= " ";
58 }
59 else
60 {
61 server_config_string+= "--server=";
62 server_config_string+= arg->hostname();
63 server_config_string+= ":";
64 server_config_string+= port_str;
65 server_config_string+= " ";
66 }
67
68 server_list+= server_config_string;
69
70 }
71
72 Server* server_startup_st::pop_server()
73 {
74 Server *tmp= servers.back();
75 servers.pop_back();
76 return tmp;
77 }
78
79 bool server_startup_st::shutdown(uint32_t number_of_host)
80 {
81 if (servers.size() > number_of_host)
82 {
83 Server* tmp= servers[number_of_host];
84
85 if (tmp and tmp->has_pid() and tmp->kill() == false)
86 { }
87 else
88 {
89 return true;
90 }
91 }
92
93 return false;
94 }
95
96 void server_startup_st::shutdown_and_remove()
97 {
98 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
99 {
100 delete *iter;
101 }
102 servers.clear();
103 }
104
105 bool server_startup_st::check() const
106 {
107 bool success= true;
108 for (std::vector<Server *>::const_iterator iter= servers.begin(); iter != servers.end(); iter++)
109 {
110 if ((*iter)->check() == false)
111 {
112 success= false;
113 }
114 }
115
116 return success;
117 }
118
119 bool server_startup_st::shutdown()
120 {
121 bool success= true;
122 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
123 {
124 if ((*iter)->has_pid() and (*iter)->kill() == false)
125 {
126 Error << "Unable to kill:" << *(*iter);
127 success= false;
128 }
129 }
130
131 return success;
132 }
133
134 void server_startup_st::restart()
135 {
136 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
137 {
138 (*iter)->start();
139 }
140 }
141
142 #define MAGIC_MEMORY 123575
143 server_startup_st::server_startup_st() :
144 _magic(MAGIC_MEMORY),
145 _socket(false),
146 _sasl(false),
147 _count(0),
148 udp(0),
149 _servers_to_run(5)
150 { }
151
152 server_startup_st::~server_startup_st()
153 {
154 shutdown_and_remove();
155 }
156
157 bool server_startup_st::validate()
158 {
159 return _magic == MAGIC_MEMORY;
160 }
161
162 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)
163 {
164 if (try_port <= 0)
165 {
166 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
167 }
168
169 libtest::Server *server= NULL;
170 if (0)
171 { }
172 else if (server_type.compare("gearmand") == 0)
173 {
174 if (GEARMAND_BINARY)
175 {
176 if (HAVE_LIBGEARMAN)
177 {
178 server= build_gearmand("localhost", try_port);
179 }
180 }
181 }
182 else if (server_type.compare("blobslap_worker") == 0)
183 {
184 if (GEARMAND_BINARY)
185 {
186 if (GEARMAND_BLOBSLAP_WORKER)
187 {
188 if (HAVE_LIBGEARMAN)
189 {
190 server= build_blobslap_worker(try_port);
191 }
192 }
193 }
194 }
195 else if (server_type.compare("memcached-sasl") == 0)
196 {
197 if (MEMCACHED_SASL_BINARY)
198 {
199 if (HAVE_LIBMEMCACHED)
200 {
201 server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
202 }
203 }
204 }
205 else if (server_type.compare("memcached") == 0)
206 {
207 if (MEMCACHED_BINARY)
208 {
209 if (HAVE_LIBMEMCACHED)
210 {
211 server= build_memcached("localhost", try_port);
212 }
213 }
214 }
215 else if (server_type.compare("memcached-light") == 0)
216 {
217 if (MEMCACHED_LIGHT_BINARY)
218 {
219 if (HAVE_LIBMEMCACHED)
220 {
221 server= build_memcached_light("localhost", try_port);
222 }
223 }
224 }
225
226 if (server == NULL)
227 {
228 fatal_message("Launching of an unknown server was attempted");
229 }
230
231 /*
232 We will now cycle the server we have created.
233 */
234 if (server->cycle() == false)
235 {
236 Error << "Could not start up server " << *server;
237 delete server;
238 return false;
239 }
240
241 server->build(argc, argv);
242
243 if (gdb_is_caller() and false)
244 {
245 Out << "Pausing for startup, hit return when ready.";
246 std::string gdb_command= server->base_command();
247 std::string options;
248 #if 0
249 Out << "run " << server->args(options);
250 #endif
251 getchar();
252 }
253 else if (server->start() == false)
254 {
255 delete server;
256 return false;
257 }
258 else
259 {
260 if (opt_startup_message)
261 {
262 Outn();
263 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
264 Outn();
265 }
266 }
267
268 construct.push_server(server);
269
270 return true;
271 }
272
273 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
274 {
275 (void)try_port;
276 Outn();
277
278 Server *server= NULL;
279 if (0)
280 { }
281 else if (server_type.compare("gearmand") == 0)
282 {
283 Error << "Socket files are not supported for gearmand yet";
284 }
285 else if (server_type.compare("memcached-sasl") == 0)
286 {
287 if (MEMCACHED_SASL_BINARY)
288 {
289 if (HAVE_LIBMEMCACHED)
290 {
291 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
292 }
293 else
294 {
295 Error << "Libmemcached was not found";
296 }
297 }
298 else
299 {
300 Error << "No memcached binary is available";
301 }
302 }
303 else if (server_type.compare("memcached") == 0)
304 {
305 if (MEMCACHED_BINARY)
306 {
307 if (HAVE_LIBMEMCACHED)
308 {
309 server= build_memcached_socket("localhost", try_port);
310 }
311 else
312 {
313 Error << "Libmemcached was not found";
314 }
315 }
316 else
317 {
318 Error << "No memcached binary is available";
319 }
320 }
321 else
322 {
323 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
324 }
325
326 if (server == NULL)
327 {
328 Error << "Failure occured while creating server: " << server_type;
329 return false;
330 }
331
332 /*
333 We will now cycle the server we have created.
334 */
335 if (server->cycle() == false)
336 {
337 Error << "Could not start up server " << *server;
338 delete server;
339 return false;
340 }
341
342 server->build(argc, argv);
343
344 if (gdb_is_caller() and false)
345 {
346 Out << "Pausing for startup, hit return when ready.";
347 std::string gdb_command= server->base_command();
348 std::string options;
349 #if 0
350 Out << "run " << server->args(options);
351 #endif
352 getchar();
353 }
354 else if (server->start() == false)
355 {
356 Error << "Failed to start " << *server;
357 delete server;
358 return false;
359 }
360 else
361 {
362 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
363 }
364
365 push_server(server);
366
367 set_default_socket(server->socket().c_str());
368
369 Outn();
370
371 return true;
372 }
373
374 std::string server_startup_st::option_string() const
375 {
376 std::string temp= server_list;
377 rtrim(temp);
378 return temp;
379 }
380
381
382 } // namespace libtest