3a5a7a0e3eaf14eff0cb7da20c21384fb1b827ad
[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 "libtest/yatlcon.h"
38
39 #include "libtest/common.h"
40
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 libtest::vchar_t port_str;
79 port_str.resize(NI_MAXSERV);
80 snprintf(&port_str[0], port_str.size(), "%u", int(arg->port()));
81
82 server_config_string+= "--server=";
83 server_config_string+= arg->hostname();
84 server_config_string+= ":";
85 server_config_string+= &port_str[0];
86 server_config_string+= " ";
87 }
88
89 server_list+= server_config_string;
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 std::for_each(servers.begin(), servers.end(), DeleteFromVector());
120 servers.clear();
121 }
122
123 bool server_startup_st::check() const
124 {
125 bool success= true;
126 for (std::vector<Server *>::const_iterator iter= servers.begin(); iter != servers.end(); ++iter)
127 {
128 if ((*iter)->check() == false)
129 {
130 success= false;
131 }
132 }
133
134 return success;
135 }
136
137 bool server_startup_st::shutdown()
138 {
139 bool success= true;
140 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
141 {
142 if ((*iter)->has_pid() and (*iter)->kill() == false)
143 {
144 Error << "Unable to kill:" << *(*iter);
145 success= false;
146 }
147 }
148
149 return success;
150 }
151
152 void server_startup_st::restart()
153 {
154 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
155 {
156 (*iter)->start();
157 }
158 }
159
160 #define MAGIC_MEMORY 123575
161 server_startup_st::server_startup_st() :
162 _magic(MAGIC_MEMORY),
163 _socket(false),
164 _sasl(false),
165 udp(0),
166 _servers_to_run(5)
167 { }
168
169 server_startup_st::~server_startup_st()
170 {
171 clear();
172 }
173
174 bool server_startup_st::validate()
175 {
176 return _magic == MAGIC_MEMORY;
177 }
178
179 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, const char *argv[])
180 {
181 return construct.start_server(server_type, try_port, argv);
182 }
183
184 libtest::Server* server_startup_st::create(const std::string& server_type, in_port_t try_port, const bool is_socket)
185 {
186 libtest::Server *server= NULL;
187
188 if (is_socket == false)
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
196 if (is_socket)
197 {
198 if (server_type.compare("memcached") == 0)
199 {
200 server= build_memcached_socket("localhost", try_port);
201 }
202 else
203 {
204 Error << "Socket is not support for server: " << server_type;
205 return NULL;
206 }
207 }
208 else if (server_type.compare("gearmand") == 0)
209 {
210 server= build_gearmand("localhost", try_port);
211 }
212 else if (server_type.compare("hostile-gearmand") == 0)
213 {
214 server= build_gearmand("localhost", try_port, "gearmand/hostile_gearmand");
215 }
216 else if (server_type.compare("drizzled") == 0)
217 {
218 if (DRIZZLED_BINARY)
219 {
220 if (HAVE_LIBDRIZZLE)
221 {
222 server= build_drizzled("localhost", try_port);
223 }
224 }
225 }
226 else if (server_type.compare("blobslap_worker") == 0)
227 {
228 if (GEARMAND_BINARY)
229 {
230 if (GEARMAND_BLOBSLAP_WORKER)
231 {
232 if (HAVE_LIBGEARMAN)
233 {
234 server= build_blobslap_worker(try_port);
235 }
236 }
237 }
238 }
239 else if (server_type.compare("memcached") == 0)
240 {
241 if (HAVE_MEMCACHED_BINARY)
242 {
243 server= build_memcached("localhost", try_port);
244 }
245 }
246
247 return server;
248 }
249
250 class ServerPtr {
251 public:
252 ServerPtr(libtest::Server* server_):
253 _server(server_)
254 { }
255
256 ~ServerPtr()
257 {
258 delete _server;
259 }
260
261 void reset()
262 {
263 delete _server;
264 _server= NULL;
265 }
266
267 libtest::Server* release(libtest::Server* server_= NULL)
268 {
269 libtest::Server* tmp= _server;
270 _server= server_;
271 return tmp;
272 }
273
274 libtest::Server* operator->() const
275 {
276 return _server;
277 }
278
279 libtest::Server* operator&() const
280 {
281 return _server;
282 }
283
284 private:
285 libtest::Server* _server;
286 };
287
288 bool server_startup_st::_start_server(const bool is_socket,
289 const std::string& server_type,
290 in_port_t try_port,
291 const char *argv[])
292 {
293 try {
294 ServerPtr server(create(server_type, try_port, is_socket));
295
296 if (&server == NULL)
297 {
298 Error << "Could not allocate server: " << server_type;
299 return false;
300 }
301
302 /*
303 We will now cycle the server we have created.
304 */
305 if (server->cycle() == false)
306 {
307 Error << "Could not start up server " << &server;
308 return false;
309 }
310
311 server->init(argv);
312
313 #if 0
314 if (false)
315 {
316 Out << "Pausing for startup, hit return when ready.";
317 std::string gdb_command= server->base_command();
318 getchar();
319 }
320 else
321 #endif
322
323 if (server->start() == false)
324 {
325 return false;
326 }
327 else
328 {
329 {
330 #if defined(DEBUG)
331 if (DEBUG)
332 {
333 Outn();
334 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
335 Outn();
336 }
337 #endif
338 }
339 }
340
341 push_server(server.release());
342
343 if (is_socket and &server)
344 {
345 set_default_socket(server->socket().c_str());
346 }
347 }
348 catch (const libtest::disconnected& err)
349 {
350 if (fatal::is_disabled() == false and try_port != LIBTEST_FAIL_PORT)
351 {
352 stream::cerr(err.file(), err.line(), err.func()) << err.what();
353 return false;
354 }
355 }
356 catch (const libtest::__test_result& err)
357 {
358 stream::cerr(err.file(), err.line(), err.func()) << err.what();
359 return false;
360 }
361 catch (const std::exception& err)
362 {
363 Error << err.what();
364 return false;
365 }
366 catch (...)
367 {
368 Error << "error occured while creating server: " << server_type;
369 return false;
370 }
371
372 return true;
373 }
374
375 bool server_startup_st::start_server(const std::string& server_type, in_port_t try_port, const char *argv[])
376 {
377 return _start_server(false, server_type, try_port, argv);
378 }
379
380 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, const char *argv[])
381 {
382 return _start_server(true, server_type, try_port, argv);
383 }
384
385 std::string server_startup_st::option_string() const
386 {
387 std::string temp= server_list;
388 rtrim(temp);
389 return temp;
390 }
391
392
393 } // namespace libtest