Skip to content

Commit

Permalink
Have reload copy handlers over from the previous server config if the…
Browse files Browse the repository at this point in the history
…y haven't changed. Debugging output on temporarily.
  • Loading branch information
zedshaw committed Jun 15, 2011
1 parent 63c332f commit 645aabd
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/connection.c
Expand Up @@ -625,7 +625,7 @@ void Connection_task(void *v)
error: // fallthrough
State_exec(&conn->state, CLOSE, (void *)conn);
Connection_destroy(conn);
return;
taskexit(0);
}

int Connection_deliver_raw(Connection *conn, bstring buf)
Expand Down
13 changes: 10 additions & 3 deletions src/handler.c
@@ -1,3 +1,5 @@
#undef NDEBUG

/**
*
* Copyright (c) 2010, Zed A. Shaw and Mongrel2 Project Contributors.
Expand Down Expand Up @@ -178,7 +180,7 @@ static inline int handler_recv_parse(Handler *handler, HandlerParser *parser)

rc = mqrecv(handler->recv_socket, inmsg, 0);
check(rc == 0, "Receive on handler socket failed.");
check(handler->running, "Received shutdown notification, goodbye.");
check(handler->running, "Handler marked as not running.");

rc = HandlerParser_execute(parser, zmq_msg_data(inmsg), zmq_msg_size(inmsg));
check(rc == 1, "Failed to parse message from handler.");
Expand Down Expand Up @@ -236,13 +238,18 @@ void Handler_task(void *v)
HandlerParser_reset(parser);
}

debug("############################### HANDLER EXITED: %p, running: %d, task: %p",
handler, handler->running, handler->task);
handler->running = 0;
handler->task = NULL;
HandlerParser_destroy(parser);
debug("HANDLER EXITED.");
taskexit(0);

error:
handler->running = 0;
handler->task = NULL;
HandlerParser_destroy(parser);
log_err("HANDLER TASK DIED");
log_err("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! HANDLER TASK DIED");
taskexit(1);
}

Expand Down
19 changes: 10 additions & 9 deletions src/mongrel2.c
Expand Up @@ -112,7 +112,7 @@ void start_terminator()
}


Server *load_server(const char *db_file, const char *server_uuid, int reuse_fd)
Server *load_server(const char *db_file, const char *server_uuid, Server *old_srv)
{
int rc = 0;
Server *srv = NULL;
Expand All @@ -130,17 +130,17 @@ Server *load_server(const char *db_file, const char *server_uuid, int reuse_fd)
check(srv, "Failed to load server %s from %s", server_uuid, db_file);
check(srv->default_host, "No default_host set for server: %s, you need one host named: %s", server_uuid, bdata(srv->default_hostname));

if(reuse_fd == -1) {
if(old_srv == NULL || old_srv->listen_fd == -1) {
srv->listen_fd = netannounce(TCP, bdata(srv->bind_addr), srv->port);
check(srv->listen_fd >= 0, "Can't announce on TCP port %d", srv->port);
check(fdnoblock(srv->listen_fd) == 0, "Failed to set listening port %d nonblocking.", srv->port);
} else {
srv->listen_fd = dup(reuse_fd);
srv->listen_fd = dup(old_srv->listen_fd);
check(srv->listen_fd != -1, "Failed to dup the socket from the running server.");
fdclose(reuse_fd);
fdclose(old_srv->listen_fd);
}

check(Server_start_handlers(srv) == 0, "Failed to start handlers.");
check(Server_start_handlers(srv, old_srv) == 0, "Failed to start handlers.");

Config_close_db();
return srv;
Expand Down Expand Up @@ -253,14 +253,15 @@ Server *reload_server(Server *old_srv, const char *db_file, const char *server_u
{
RUNNING = 1;

Server_stop_handlers(old_srv);

log_info("------------------------ RELOAD %s -----------------------------------", server_uuid);
MIME_destroy();
Setting_destroy();

Server *srv = load_server(db_file, server_uuid, old_srv->listen_fd);
Server *srv = load_server(db_file, server_uuid, old_srv);
check(srv != NULL, "Failed to load new server config.");

Server_stop_handlers(old_srv);

RELOAD = 0;
return srv;

Expand Down Expand Up @@ -322,7 +323,7 @@ void taskmain(int argc, char **argv)
check(rc != -1, "Failed to load the config module: %s", argv[3]);
}

SERVER = load_server(argv[1], argv[2], -1);
SERVER = load_server(argv[1], argv[2], NULL);
check(SERVER, "Aborting since can't load server.");

SuperPoll_get_max_fd();
Expand Down
67 changes: 60 additions & 7 deletions src/server.c
@@ -1,3 +1,4 @@
#undef NDEBUG
/**
*
* Copyright (c) 2010, Zed A. Shaw and Mongrel2 Project Contributors.
Expand Down Expand Up @@ -339,11 +340,57 @@ Host *Server_match_backend(Server *srv, bstring target)
return NULL;
}

int Server_start_handlers(Server *srv)
static inline int Server_copy_active_handlers(Server *srv, Server *copy_from)
{
debug("Copying handlers from %p to %p.", copy_from, srv);
int i = 0;

for(i = 0; i < darray_end(copy_from->handlers); i++) {
Handler *from = darray_get(copy_from->handlers, i);

int j = 0;
for(j = 0; j < darray_end(srv->handlers); j++) {
Handler *to = darray_get(srv->handlers, j);

debug("Comparing %s=%s, %s=%s, %s=%s, %s=%s",
bdata(from->send_ident),
bdata(to->send_ident),
bdata(from->recv_ident),
bdata(to->recv_ident),
bdata(from->recv_spec),
bdata(to->recv_spec),
bdata(from->send_spec),
bdata(to->send_spec));

if(biseq(from->send_ident, to->send_ident) &&
biseq(from->recv_ident, to->recv_ident) &&
biseq(from->recv_spec, to->recv_spec) &&
biseq(from->send_spec, to->send_spec)
)
{
debug("Handler is the same, copying over: %p to %p", from, to);
darray_set(srv->handlers, j, from);
// swap them around so that the darrays stay full
darray_set(copy_from->handlers, i, to);
to->running = 0;
break;
}
}
}

return 0;
}

int Server_start_handlers(Server *srv, Server *copy_from)
{
int i = 0;
int rc = 0;

if(copy_from != NULL) {
rc = Server_copy_active_handlers(srv, copy_from);
check(rc != -1, "Failed to copy old handlers to new server config.");
}

for(i = 0; i < darray_end(srv->handlers); i++) {
Handler *handler = darray_get(srv->handlers, i);
check(handler != NULL, "Invalid handler, can't be NULL.");
Expand All @@ -369,15 +416,21 @@ int Server_stop_handlers(Server *srv)
Handler *handler = darray_get(srv->handlers, i);
check(handler != NULL, "Invalid handler, can't be NULL.");

debug("############################### HANDLER SHUTDOWN: %p, running: %d, task: %p",
handler, handler->running, handler->task);
if(handler->running) {
log_info("STOPPING HANDLER %s", bdata(handler->send_spec));
tasksignal(handler->task, SIGINT);
handler->running = 0;
taskdelay(1);

if(handler->recv_socket) zmq_close(handler->recv_socket);
if(handler->send_socket) zmq_close(handler->send_socket);
if(handler->task != NULL) {
tasksignal(handler->task, SIGINT);
handler->running = 0;
taskdelay(1);
}
}

if(handler->recv_socket) zmq_close(handler->recv_socket);
if(handler->send_socket) zmq_close(handler->send_socket);
handler->recv_socket = NULL;
handler->send_socket = NULL;
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/server.h
Expand Up @@ -85,7 +85,7 @@ void Server_set_default_host(Server *srv, Host *host);

Host *Server_match_backend(Server *srv, bstring target);

int Server_start_handlers(Server *srv);
int Server_start_handlers(Server *srv, Server *copy_from);

int Server_stop_handlers(Server *srv);

Expand Down
1 change: 1 addition & 0 deletions src/task/task.c
Expand Up @@ -224,6 +224,7 @@ static void taskscheduler(void)
i = t->alltaskslot;
alltask[i] = alltask[--nalltask];
alltask[i]->alltaskslot = i;
debug("FREEING TASK: %p", t);
free(t);
}
}
Expand Down

0 comments on commit 645aabd

Please sign in to comment.