1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 Startup, and shutdown the memcached servers.
43 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
45 #include <libtest/common.h>
57 #include <libmemcached/memcached.h>
58 #include <libmemcached/util.h>
60 #include <libtest/server.h>
61 #include <libtest/killpid.h>
62 #include <libtest/wait.h>
64 #define SOCKET_FILE "/tmp/memcached.socket"
66 static pid_t
__getpid(server_st
& server
)
68 memcached_return_t rc
;
69 pid_t pid
= libmemcached_util_getpid(server
.hostname(), server
.port(), &rc
);
73 static bool __ping(server_st
& server
)
75 memcached_return_t rc
;
76 bool ret
= libmemcached_util_ping(server
.hostname(), server
.port(), &rc
);
80 static bool cycle_server(server_st
*server
)
84 if (libmemcached_util_ping(server
->hostname(), server
->port(), NULL
))
86 // First we try to kill it, and on fail of that we flush it.
87 pid_t pid
= libmemcached_util_getpid(server
->hostname(), server
->port(), NULL
);
89 if (pid
> 0 and kill_pid(pid
))
91 Error
<< "Killed existing server," << *server
<< " with pid:" << pid
;
94 else if (libmemcached_util_flush(server
->hostname(), server
->port(), NULL
)) // If we can flush it, we will just use it
96 Error
<< "Found server on port " << int(server
->port()) << ", flushed it!";
99 } // No idea what is wrong here, so we need to find a different port
112 bool server_startup(server_startup_st
*construct
)
116 if (getenv(((char *)"MEMCACHED_SERVERS")))
118 construct
->server_list
= getenv(((char *)"MEMCACHED_SERVERS"));
119 Log
<< "MEMCACHED_SERVERS " << construct
->server_list
;
124 std::string server_config_string
;
126 uint32_t port_base
= 0;
127 for (uint32_t x
= 0; x
< uint32_t(construct
->count
-1); x
++)
129 server_st
*server
= NULL
;
133 char variable_buffer
[1024];
135 snprintf(variable_buffer
, sizeof(variable_buffer
), "LIBMEMCACHED_PORT_%u", x
);
137 if ((var
= getenv(variable_buffer
)))
139 server
= new server_st((in_port_t
)atoi(var
), __getpid
, __ping
);
143 server
= new server_st(in_port_t(x
+TEST_PORT_BASE
+port_base
), __getpid
, __ping
);
145 while (not cycle_server(server
))
147 Error
<< "Found server " << *server
<< ", could not flush it, so trying next port.";
149 server
->set_port(in_port_t(x
+TEST_PORT_BASE
+port_base
));
154 if (server
->is_used())
156 Log
<< "Using server at : " << server
;
160 char buffer
[FILENAME_MAX
];
163 snprintf(buffer
, sizeof(buffer
), "%s -d -t 1 -p %u -U %u -m 128",
164 MEMCACHED_BINARY
, server
->port(), server
->port());
168 snprintf(buffer
, sizeof(buffer
), "%s -d -t 1 -p %u -U %u",
169 MEMCACHED_BINARY
, server
->port(), server
->port());
171 server
->set_command(buffer
);
173 if (not server
->start())
175 Error
<< "Failed system(" << buffer
<< ")";
179 Log
<< "STARTING SERVER: " << buffer
<< " pid:" << server
->pid();
181 construct
->push_server(server
);
185 assert(server
->has_port());
186 set_default_port(server
->port());
189 char port_str
[NI_MAXSERV
];
190 snprintf(port_str
, sizeof(port_str
), "%u", int(server
->port()));
192 server_config_string
+= "--server=";
193 server_config_string
+= server
->hostname();
194 server_config_string
+= ":";
195 server_config_string
+= port_str
;
196 server_config_string
+= " ";
202 std::string
socket_file(SOCKET_FILE
);
205 if ((var
= getenv("LIBMEMCACHED_SOCKET")))
210 server_st
*server
= new server_st(SOCKET_FILE
, __getpid
, __ping
);
212 if (not cycle_server(server
))
214 Error
<< "Found server " << server
<< ", could not flush it, failing since socket file is not available.";
218 if (server
->is_used())
220 Log
<< "Using server at : " << *server
;
224 char buffer
[FILENAME_MAX
];
225 snprintf(buffer
, sizeof(buffer
), "%s -d -t 1 -s %s", MEMCACHED_BINARY
, SOCKET_FILE
);
226 server
->set_command(buffer
);
228 if (not server
->start())
230 Error
<< "Failed system(" << buffer
<< ")";
234 Log
<< "STARTING SERVER: " << buffer
<< " pid:" << server
->pid();
236 set_default_socket(server
->hostname());
237 construct
->push_server(server
);
240 server_config_string
+= "--socket=\"";
241 server_config_string
+= server
->hostname();
242 server_config_string
+= "\" ";
246 server_config_string
.resize(server_config_string
.size() -1); // Remove final space
247 construct
->server_list
= server_config_string
;
252 srandom((unsigned int)time(NULL
));
257 void server_shutdown(server_startup_st
*construct
)
262 construct
->shutdown();