typo fix with root check in nginx module
[MogileFS-Server.git] / mogstored
blob608d786b205c34fcb001b2c0125589635f30fdf3
1 #!/usr/bin/perl
3 eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
4 if 0; # not running under some shell
7 # MogileFS storage node daemon
8 # (perlbal front-end)
10 # (c) 2004, Brad Fitzpatrick, <brad@danga.com>
11 # (c) 2006-2007, Six Apart, Ltd.
13 use strict;
14 use lib 'lib';
15 use Mogstored::HTTPServer;
17 use IO::Socket::INET;
18 use POSIX qw(WNOHANG);
19 use Perlbal 1.73;
20 use FindBin qw($Bin $RealScript);
22 use Mogstored::HTTPServer::Perlbal;
23 use Mogstored::HTTPServer::Lighttpd;
24 use Mogstored::HTTPServer::None;
25 use Mogstored::HTTPServer::Apache;
26 use Mogstored::HTTPServer::Nginx;
27 use Mogstored::SideChannelListener;
28 use Mogstored::SideChannelClient;
30 my $selfexe = "$Bin/$RealScript";
32 # State:
33 my %on_death; # pid -> subref (to run when pid dies)
34 my %devnum_to_device; # mogile device number (eg. 'dev1' would be '1') -> os device path (eg. '/dev/rd0')
35 my %osdevnum_to_device; # os device number (fetched via stat(file)[0]) -> os device path (ec. '/dev/rd0')
36 my %iostat_listeners; # fd => SideChannel client: clients interested in iostat data.
37 my $iostat_available = 1; # bool: iostat working. assume working to start.
38 my ($iostat_pipe_r, $iostat_pipe_w); # pipes for talking to iostat process
40 # Config:
41 my $opt_skipconfig;
42 my $opt_daemonize;
43 my $opt_config;
44 my $opt_iostat = 1; # default to on now
45 my $max_conns = 10000;
46 my $http_listen = "0.0.0.0:7500";
47 my $mgmt_listen = "0.0.0.0:7501";
48 my $docroot = "/var/mogdata";
49 my $default_config = "/etc/mogilefs/mogstored.conf";
50 my $server = $ENV{MOGSTORED_SERVER_TYPE} || "perlbal";
51 my $serverbin = "";
52 my $pidfile = undef;
54 # Rename binary in process list to make init scripts saner
55 $0 = "mogstored";
57 my %config_opts = (
58 'iostat' => \$opt_iostat,
59 's|skipconfig' => \$opt_skipconfig,
60 'daemonize|d' => \$opt_daemonize,
61 'config=s' => \$opt_config,
62 'httplisten=s' => \$http_listen,
63 'mgmtlisten=s' => \$mgmt_listen,
64 'docroot=s' => \$docroot,
65 'maxconns=i' => \$max_conns,
66 'server=s' => \$server,
67 'serverbin=s' => \$serverbin,
68 'pidfile=s' => \$pidfile,
70 usage() unless Getopt::Long::GetOptions(%config_opts);
72 die "Unknown server type. Valid options: --server={perlbal,lighttpd,apache,nginx,none}"
73 unless $server =~ /^perlbal|lighttpd|apache|nginx|none$/;
75 $opt_config = $default_config if ! $opt_config && -e $default_config;
76 load_config_file($opt_config => \%config_opts) if $opt_config && !$opt_skipconfig;
78 # initialize basic required Perlbal machinery, for any HTTP server
79 my $perlbal_init = qq{
80 CREATE SERVICE mogstored
81 SET role = web_server
82 SET docroot = $docroot
84 # don't listen... this is just a stub service.
85 CREATE SERVICE mgmt
86 SET role = management
87 ENABLE mgmt
89 $perlbal_init .= "\nSERVER pidfile = $pidfile" if defined($pidfile);
90 Perlbal::run_manage_commands($perlbal_init , sub { print STDERR "$_[0]\n"; });
92 # start HTTP server
93 my $httpsrv_class = "Mogstored::HTTPServer::" . ucfirst($server);
94 my $httpsrv = $httpsrv_class->new(
95 listen => $http_listen,
96 docroot => $docroot,
97 maxconns => $max_conns,
98 bin => $serverbin,
100 $httpsrv->start;
102 if ($opt_daemonize) {
103 $httpsrv->pre_daemonize;
104 Perlbal::daemonize();
105 } else {
106 print "Running.\n";
109 $httpsrv->post_daemonize;
111 # kill our children processes on exit:
112 my $parent_pid = $$;
114 $SIG{TERM} = $SIG{INT} = sub {
115 return unless $$ == $parent_pid; # don't let this be inherited
116 kill 'TERM', grep { $_ } keys %on_death;
117 POSIX::_exit(0);
120 setup_iostat_pipes();
121 start_disk_usage_process();
122 start_iostat_process() if $opt_iostat;
123 harvest_dead_children(); # every 2 seconds, it reschedules itself
124 setup_sidechannel_listener();
126 # now start the main loop
127 Perlbal::run();
129 ############################################################################
131 sub usage {
132 my $note = shift;
133 $note = $note ? "NOTE: $note\n\n" : "";
135 die "${note}Usage: mogstored [OPTS]
137 OPTS:
138 --daemonize -d Daemonize
139 --config=<file> Set config file (default is /etc/mogilefs/mogstored.conf)
140 --httplisten=<ip:port> IP/Port HTTP server listens on
141 --mgmtlisten=<ip:port> IP/Port management/sidechannel listens on
142 --docroot=<path> Docroot above device mount points. Defaults to /var/mogdata
143 --maxconns=<number> Number of simultaneous clients to serve. Default 10000
148 # accessor for SideChannelClient:
149 sub Mogstored::iostat_available {
150 return $iostat_available;
153 sub load_config_file {
154 my ($conffile, $opts) = @_;
156 # parse the mogstored config file, which is just lines of comments and
157 # "key = value" lines, where keys are just the same as command line
158 # options.
159 die "Config file $opt_config doesn't exist.\n" unless -e $conffile;
160 open my $fh, $conffile or die "Couldn't open config file for reading: $!";
161 while (<$fh>) {
162 s/\#.*//;
163 next unless /\S/;
164 if (/SERVER max_connect/i || /CREATE SERVICE/i) {
165 usage("Your $opt_config file is the old syntax. The new format is simply lines of <key> = <value> where keys are the same as mogstored's command line options.");
168 die "Unknown config syntax: $_\n" unless /^\s*(\w+)\s*=\s*(.+?)\s*$/;
169 my ($key, $val) = ($1, $2);
170 my $dest;
171 foreach my $ck (keys %$opts) {
172 next unless $ck =~ /^$key\b/;
173 $dest = $opts->{$ck};
175 die "Unknown config setting: $key\n" unless $dest;
176 $$dest = $val;
180 sub harvest_dead_children {
181 my $dead = waitpid(-1, WNOHANG);
182 if ($dead > 0) {
183 my $code = delete $on_death{$dead};
184 $code->() if $code;
186 Danga::Socket->AddTimer(2, \&harvest_dead_children);
189 sub Mogstored::on_pid_death {
190 my ($class, $pid, $code) = @_;
191 $on_death{$pid} = $code;
194 # returns $pid of child, if parent, else runs child.
195 sub start_disk_usage_process {
196 my $child = fork;
197 unless (defined $child) {
198 Perlbal::log('crit', "Fork error creating disk usage tracking process");
199 return undef;
202 # if we're the parent.
203 if ($child) {
204 $on_death{$child} = sub {
205 start_disk_usage_process(); # start a new one
207 return $child;
210 require Mogstored::ChildProcess::DiskUsage;
211 my $class = "Mogstored::ChildProcess::DiskUsage";
212 $class->pre_exec_init;
213 $class->exec;
216 sub Mogstored::iostat_subscribe {
217 my ($class, $sock) = @_;
218 $iostat_listeners{fileno($sock->sock)} = $sock;
221 sub Mogstored::iostat_unsubscribe {
222 my ($class, $sock) = @_;
223 my $fdno = fileno($sock->sock);
224 return unless defined $fdno;
225 delete $iostat_listeners{$fdno};
228 # to be honest, I have no clue why this exists. I just had to move it
229 # around for multi-server refactoring, and I felt better not
230 # understanding it but preserving than killing it. in particular, why
231 # is this "graceful"? (gets called from SideChannelClient's
232 # die_gracefully)
233 sub Mogstored::on_sidechannel_die_gracefully {
234 if ($$ == $parent_pid) {
235 kill 'TERM', grep { $_ } keys %on_death;
239 sub setup_sidechannel_listener {
240 Mogstored::SideChannelListener->new($mgmt_listen);
243 my $iostat_read_buf = "";
244 sub setup_iostat_pipes {
245 pipe ($iostat_pipe_r, $iostat_pipe_w);
246 IO::Handle::blocking($iostat_pipe_r, 0);
247 IO::Handle::blocking($iostat_pipe_w, 0);
249 Danga::Socket->AddOtherFds(fileno($iostat_pipe_r), sub {
250 read_from_iostat_child();
254 sub start_iostat_process {
255 my $pid = fork;
256 unless (defined $pid) {
257 warn "Fork for iostat failed: $!";
258 return;
261 if ($pid) {
262 # Parent
263 $on_death{$pid} = sub {
264 # Try a final read from data on the socket.
265 # Note that this doesn't internally loop... so it might miss data.
266 read_from_iostat_child();
267 # Kill any buffer so partial reads don't hurt us later.
268 drain_iostat_child_pipe();
269 start_iostat_process();
271 return;
274 require Mogstored::ChildProcess::IOStat;
275 my $class = "Mogstored::ChildProcess::IOStat";
276 $class->pre_exec_init;
277 $class->exec;
280 sub Mogstored::get_iostat_writer_pipe { $iostat_pipe_w }
282 sub drain_iostat_child_pipe {
283 my $data;
284 while (1) {
285 last unless sysread($iostat_pipe_r, $data, 10240) > 0;
287 $iostat_read_buf = '';
290 # (runs in parent event-loop process)
291 sub read_from_iostat_child {
292 my $data;
293 my $rv = sysread($iostat_pipe_r, $data, 10240);
294 return unless $rv && $rv > 0;
296 $iostat_read_buf .= $data;
298 # only write complete lines to sockets (in case for some reason we get
299 # a partial read and child process dies...)
300 while ($iostat_read_buf =~ s/(.+)\r?\n//) {
301 my $line = $1;
302 foreach my $out_sock (values %iostat_listeners) {
303 # where $line will be like "dev53\t53.23" or a "." to signal end of a group of devices.
304 # stop writing to the socket if the listener isn't picking it up.
305 next if $out_sock->{write_buf_size};
306 $out_sock->write("$line\n");
311 # Local Variables:
312 # mode: perl
313 # c-basic-indent: 4
314 # indent-tabs-mode: nil
315 # End:
317 __END__
319 =head1 NAME
321 mogstored -- MogileFS storage daemon
323 =head1 USAGE
325 This is the MogileFS storage daemon, which is just an HTTP server that
326 supports PUT, DELETE, etc. It's actually a wrapper around L<Perlbal>,
327 doing all the proper Perlbal config for you.
329 In addition, it monitors disk usage, I/O activity, etc, which are
330 checked from the L<MogileFS tracker|mogilefsd>.
332 =head1 AUTHORS
334 Brad Fitzpatrick E<lt>brad@danga.comE<gt>
336 Mark Smith E<lt>junior@danga.comE<gt>
338 Jonathan Steinert E<lt>jsteinert@sixapart.comE<gt>
340 =head1 ENVIRONMENT
342 =over 4
344 =item PERLBAL_XS_HEADERS
346 If defined and 0, Perlbal::XS::HTTPHeaders will not be used, if
347 present. Otherwise, it will be enabled by default, if installed and
348 loadable.
350 =back
352 =head1 COPYRIGHT
354 Copyright 2004, Danga Interactive
355 Copyright 2005-2006, Six Apart Ltd.
357 =head1 LICENSE
359 Same terms as Perl itself. Artistic/GPLv2, at your choosing.
361 =head1 SEE ALSO
363 L<MogileFS::Overview> -- high level overview of MogileFS
365 L<mogilefsd> -- MogileFS daemon
367 L<http://danga.com/mogilefs/>