Server Starter - a superdaemon to hot-deploy server programs

18
Server::Starter a superdaemon to hot-deploy server programs Cybozu Labs, Inc. Kazuho Oku

description

Explains techniques used to write hot-deployable TCP servers, introduces a superdaemon to handle the problems.

Transcript of Server Starter - a superdaemon to hot-deploy server programs

Page 1: Server Starter - a superdaemon to hot-deploy server programs

Server::Starter a superdaemon to hot-deploy server programs

Cybozu Labs, Inc. Kazuho Oku

Page 2: Server Starter - a superdaemon to hot-deploy server programs

Hot deployment

 what is it?  upgrading web application without restarting the

application server

 the goals  no downtime  no resource leaks  fail-safe

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 2

Page 3: Server Starter - a superdaemon to hot-deploy server programs

Current techniques

 restart the interpreter (mod_perl)  pros: graceful  cons: XS may cause resource leaks, service-down

on deployment failure, cannot implement in pure-perl

 bind to unix socket (FastCGI)  pros: graceful, fail-safe  cons: only useful for local-machine

communication

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 3

Page 4: Server Starter - a superdaemon to hot-deploy server programs

Current techniques (cont'd)

 exec(myself) (Net::Server)  pros: graceful, pure-perl  cons: file descriptor leaks, service-down on

deployment failure

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 4

Page 5: Server Starter - a superdaemon to hot-deploy server programs

listen

spawn app. servers

Server::Starter

 a superdaemon for hot-deploying TCP servers  superdaemon binds to TCP ports, then spawns

the application server

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 5

SIGHUP

accept

app. logic

fork & exec accept

app. logic

SIGTERM

accept

app. logic fork & exec

Page 6: Server Starter - a superdaemon to hot-deploy server programs

Reaching the Goals

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 6

Page 7: Server Starter - a superdaemon to hot-deploy server programs

No downtime

 listening socket shared by old and new generation app. servers

 old app. servers receive SIGTERM after new servers start

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 7

listen

spawn app. servers

SIGHUP

accept

app. logic

fork & exec accept

app. logic

SIGTERM

accept

app. logic fork & exec

Page 8: Server Starter - a superdaemon to hot-deploy server programs

No resource leaks

 no chance of resource leaks  every generation of app. servers spawned from

superdaemon

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 8

listen

spawn app. servers

SIGHUP

accept

app. logic

fork & exec accept

app. logic

SIGTERM

accept

app. logic fork & exec

Page 9: Server Starter - a superdaemon to hot-deploy server programs

Fail-safe

 old app. server retired if and only if the new app. server starts up successfully  service continues even if the updated app. server

fails to start, in cases like missing modules, etc.  a good practice is to do self-testing in the app.

server before starting to serve client connections  is also an efficient way to preload modules

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 9

listen

spawn app. servers

SIGHUP

accept

app. logic

fork & exec accept

app. logic

SIGTERM

accept

app. logic fork & exec

Page 10: Server Starter - a superdaemon to hot-deploy server programs

Demo

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 10

Page 11: Server Starter - a superdaemon to hot-deploy server programs

Using Server::Starter

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 11

Page 12: Server Starter - a superdaemon to hot-deploy server programs

The Low-level Code

# from command line % start_server --port=80 my_httpd

# in my_httpd use Server::Starter qw(server_ports);

my $listen_sock = IO::Socket::INET->new( Proto => 'tcp', ); $listen_sock->fdopen((values %{server_ports()})[0], 'w') or die "failed to bind to listening socket:$!";

while (1) { if (my $conn = $listen_sock->accept) { .... } }

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 12

Page 13: Server Starter - a superdaemon to hot-deploy server programs

Net::Server::SS::PreFork

 subclass of Net::Server::PreFork # from command line % start_server --port=80 my_server.pl

# in my_server.pl use base qw(Net::Server::SS::PreFork);

sub process_request { #...code... }

__PACKAGE__->run();

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 13

Page 14: Server Starter - a superdaemon to hot-deploy server programs

Using together with HTTP::Server::Simple

 HTTP::Server::Simple can use Net::Server::SS::PreFork as a backend  and many WAFs support HTTP::Server::Simple

package MyServer; use base qw(HTTP::Server::Simple::CGI);

sub net_server { 'Net::Server::SS::PreFork' };

sub handle_request { print "HTTP/1.0 200 HOK\r\nContent-Type: text/plain\r\n\r\nHello World"; }

1;

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 14

Page 15: Server Starter - a superdaemon to hot-deploy server programs

Using together with PSGI / Plack

 started writing Plack::Impl::SSPreFork  on my github fork of Plack  uid, etc. aren't configurable yet :-(

# from command line % start_server --port=80 -- plackup -i SSPreFork MyApp.pm

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 15

Page 16: Server Starter - a superdaemon to hot-deploy server programs

Launching from daemontools

 daemontools  a (better) alternative to init.d scripts, by DJB

 start_server script is designed to be run under daemontools  restart using –h (SIGHUP)  all logs to STDERR

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 16

Page 17: Server Starter - a superdaemon to hot-deploy server programs

ToDo

 Support for FastCGI  although ... (ry

 init.d-style startup mode

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 17

Page 18: Server Starter - a superdaemon to hot-deploy server programs

Conclusion

 with Server::Starter, it is easy to write hot-deployable TCP servers

Sep 10 2009 Server::Starter - a superdaemon to hot-deploy server programs 18