Mogstored/SideChannelClient: add command for md5
[MogileFS-Server.git] / mogstored
blobd11fc2186badaa786fea9c0f8cb34b6adec2921f
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::SideChannelListener;
27 use Mogstored::SideChannelClient;
29 my $selfexe = "$Bin/$RealScript";
31 # State:
32 my %on_death; # pid -> subref (to run when pid dies)
33 my %devnum_to_device; # mogile device number (eg. 'dev1' would be '1') -> os device path (eg. '/dev/rd0')
34 my %osdevnum_to_device; # os device number (fetched via stat(file)[0]) -> os device path (ec. '/dev/rd0')
35 my %iostat_listeners; # fd => SideChannel client: clients interested in iostat data.
36 my $iostat_available = 1; # bool: iostat working. assume working to start.
37 my ($iostat_pipe_r, $iostat_pipe_w); # pipes for talking to iostat process
39 # Config:
40 my $opt_daemonize;
41 my $opt_config;
42 my $opt_iostat = 1; # default to on now
43 my $max_conns = 10000;
44 my $http_listen = "0.0.0.0:7500";
45 my $mgmt_listen = "0.0.0.0:7501";
46 my $docroot = "/var/mogdata";
47 my $default_config = "/etc/mogilefs/mogstored.conf";
48 my $server = $ENV{MOGSTORED_SERVER_TYPE} || "perlbal";
49 my $serverbin = "";
50 my $pidfile = undef;
52 # Rename binary in process list to make init scripts saner
53 $0 = "mogstored";
55 my %config_opts = (
56 'iostat' => \$opt_iostat,
57 'daemonize|d' => \$opt_daemonize,
58 'config=s' => \$opt_config,
59 'httplisten=s' => \$http_listen,
60 'mgmtlisten=s' => \$mgmt_listen,
61 'docroot=s' => \$docroot,
62 'maxconns=i' => \$max_conns,
63 'server=s' => \$server,
64 'serverbin=s' => \$serverbin,
65 'pidfile=s' => \$pidfile,
67 usage() unless Getopt::Long::GetOptions(%config_opts);
69 die "Unknown server type. Valid options: --server={perlbal,lighttpd,apache,none}"
70 unless $server =~ /^perlbal|lighttpd|apache|none$/;
72 $opt_config = $default_config if ! $opt_config && -e $default_config;
73 load_config_file($opt_config => \%config_opts) if $opt_config;
75 # initialize basic required Perlbal machinery, for any HTTP server
76 my $perlbal_init = qq{
77 CREATE SERVICE mogstored
78 SET role = web_server
79 SET docroot = $docroot
81 # don't listen... this is just a stub service.
82 CREATE SERVICE mgmt
83 SET role = management
84 ENABLE mgmt
86 $perlbal_init .= "\nSERVER pidfile = $pidfile" if defined($pidfile);
87 Perlbal::run_manage_commands($perlbal_init , sub { print STDERR "$_[0]\n"; });
89 # start HTTP server
90 my $httpsrv_class = "Mogstored::HTTPServer::" . ucfirst($server);
91 my $httpsrv = $httpsrv_class->new(
92 listen => $http_listen,
93 docroot => $docroot,
94 maxconns => $max_conns,
95 bin => $serverbin,
97 $httpsrv->start;
99 if ($opt_daemonize) {
100 $httpsrv->pre_daemonize;
101 Perlbal::daemonize();
102 } else {
103 print "Running.\n";
106 $httpsrv->post_daemonize;
108 # kill our children processes on exit:
109 my $parent_pid = $$;
111 $SIG{TERM} = $SIG{INT} = sub {
112 return unless $$ == $parent_pid; # don't let this be inherited
113 kill 'TERM', grep { $_ } keys %on_death;
114 POSIX::_exit(0);
117 setup_iostat_pipes();
118 start_disk_usage_process();
119 start_iostat_process() if $opt_iostat;
120 harvest_dead_children(); # every 2 seconds, it reschedules itself
121 setup_sidechannel_listener();
123 # now start the main loop
124 Perlbal::run();
126 ############################################################################
128 sub usage {
129 my $note = shift;
130 $note = $note ? "NOTE: $note\n\n" : "";
132 die "${note}Usage: mogstored [OPTS]
134 OPTS:
135 --daemonize -d Daemonize
136 --config=<file> Set config file (default is /etc/mogilefs/mogstored.conf)
137 --httplisten=<ip:port> IP/Port HTTP server listens on
138 --mgmtlisten=<ip:port> IP/Port management/sidechannel listens on
139 --docroot=<path> Docroot above device mount points. Defaults to /var/mogdata
140 --maxconns=<number> Number of simultaneous clients to serve. Default 10000
145 # accessor for SideChannelClient:
146 sub Mogstored::iostat_available {
147 return $iostat_available;
150 sub load_config_file {
151 my ($conffile, $opts) = @_;
153 # parse the mogstored config file, which is just lines of comments and
154 # "key = value" lines, where keys are just the same as command line
155 # options.
156 die "Config file $opt_config doesn't exist.\n" unless -e $conffile;
157 open my $fh, $conffile or die "Couldn't open config file for reading: $!";
158 while (<$fh>) {
159 s/\#.*//;
160 next unless /\S/;
161 if (/SERVER max_connect/i || /CREATE SERVICE/i) {
162 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.");
165 die "Unknown config syntax: $_\n" unless /^\s*(\w+)\s*=\s*(.+?)\s*$/;
166 my ($key, $val) = ($1, $2);
167 my $dest;
168 foreach my $ck (keys %$opts) {
169 next unless $ck =~ /^$key\b/;
170 $dest = $opts->{$ck};
172 die "Unknown config setting: $key\n" unless $dest;
173 $$dest = $val;
177 sub harvest_dead_children {
178 my $dead = waitpid(-1, WNOHANG);
179 if ($dead > 0) {
180 my $code = delete $on_death{$dead};
181 $code->() if $code;
183 Danga::Socket->AddTimer(2, \&harvest_dead_children);
186 sub Mogstored::on_pid_death {
187 my ($class, $pid, $code) = @_;
188 $on_death{$pid} = $code;
191 # returns $pid of child, if parent, else runs child.
192 sub start_disk_usage_process {
193 my $child = fork;
194 unless (defined $child) {
195 Perlbal::log('crit', "Fork error creating disk usage tracking process");
196 return undef;
199 # if we're the parent.
200 if ($child) {
201 $on_death{$child} = sub {
202 start_disk_usage_process(); # start a new one
204 return $child;
207 require Mogstored::ChildProcess::DiskUsage;
208 my $class = "Mogstored::ChildProcess::DiskUsage";
209 $class->pre_exec_init;
210 $class->exec;
213 sub Mogstored::iostat_subscribe {
214 my ($class, $sock) = @_;
215 $iostat_listeners{fileno($sock->sock)} = $sock;
218 sub Mogstored::iostat_unsubscribe {
219 my ($class, $sock) = @_;
220 my $fdno = fileno($sock->sock);
221 return unless defined $fdno;
222 delete $iostat_listeners{$fdno};
225 # to be honest, I have no clue why this exists. I just had to move it
226 # around for multi-server refactoring, and I felt better not
227 # understanding it but preserving than killing it. in particular, why
228 # is this "graceful"? (gets called from SideChannelClient's
229 # die_gracefully)
230 sub Mogstored::on_sidechannel_die_gracefully {
231 if ($$ == $parent_pid) {
232 kill 'TERM', grep { $_ } keys %on_death;
236 sub setup_sidechannel_listener {
237 Mogstored::SideChannelListener->new($mgmt_listen);
240 my $iostat_read_buf = "";
241 sub setup_iostat_pipes {
242 pipe ($iostat_pipe_r, $iostat_pipe_w);
243 IO::Handle::blocking($iostat_pipe_r, 0);
244 IO::Handle::blocking($iostat_pipe_w, 0);
246 Danga::Socket->AddOtherFds(fileno($iostat_pipe_r), sub {
247 read_from_iostat_child();
251 sub start_iostat_process {
252 my $pid = fork;
253 unless (defined $pid) {
254 warn "Fork for iostat failed: $!";
255 return;
258 if ($pid) {
259 # Parent
260 $on_death{$pid} = sub {
261 # Try a final read from data on the socket.
262 # Note that this doesn't internally loop... so it might miss data.
263 read_from_iostat_child();
264 # Kill any buffer so partial reads don't hurt us later.
265 drain_iostat_child_pipe();
266 start_iostat_process();
268 return;
271 require Mogstored::ChildProcess::IOStat;
272 my $class = "Mogstored::ChildProcess::IOStat";
273 $class->pre_exec_init;
274 $class->exec;
277 sub Mogstored::get_iostat_writer_pipe { $iostat_pipe_w }
279 sub drain_iostat_child_pipe {
280 my $data;
281 while (1) {
282 last unless sysread($iostat_pipe_r, $data, 10240) > 0;
284 $iostat_read_buf = '';
287 # (runs in parent event-loop process)
288 sub read_from_iostat_child {
289 my $data;
290 my $rv = sysread($iostat_pipe_r, $data, 10240);
291 return unless $rv && $rv > 0;
293 $iostat_read_buf .= $data;
295 # only write complete lines to sockets (in case for some reason we get
296 # a partial read and child process dies...)
297 while ($iostat_read_buf =~ s/(.+)\r?\n//) {
298 my $line = $1;
299 foreach my $out_sock (values %iostat_listeners) {
300 # where $line will be like "dev53\t53.23" or a "." to signal end of a group of devices.
301 # stop writing to the socket if the listener isn't picking it up.
302 next if $out_sock->{write_buf_size};
303 $out_sock->write("$line\n");
308 # Local Variables:
309 # mode: perl
310 # c-basic-indent: 4
311 # indent-tabs-mode: nil
312 # End:
314 __END__
316 =head1 NAME
318 mogstored -- MogileFS storage daemon
320 =head1 USAGE
322 This is the MogileFS storage daemon, which is just an HTTP server that
323 supports PUT, DELETE, etc. It's actually a wrapper around L<Perlbal>,
324 doing all the proper Perlbal config for you.
326 In addition, it monitors disk usage, I/O activity, etc, which are
327 checked from the L<MogileFS tracker|mogilefsd>.
329 =head1 AUTHORS
331 Brad Fitzpatrick E<lt>brad@danga.comE<gt>
333 Mark Smith E<lt>junior@danga.comE<gt>
335 Jonathan Steinert E<lt>jsteinert@sixapart.comE<gt>
337 =head1 ENVIRONMENT
339 =over 4
341 =item PERLBAL_XS_HEADERS
343 If defined and 0, Perlbal::XS::HTTPHeaders will not be used, if
344 present. Otherwise, it will be enabled by default, if installed and
345 loadable.
347 =back
349 =head1 COPYRIGHT
351 Copyright 2004, Danga Interactive
352 Copyright 2005-2006, Six Apart Ltd.
354 =head1 LICENSE
356 Same terms as Perl itself. Artistic/GPLv2, at your choosing.
358 =head1 SEE ALSO
360 L<MogileFS::Overview> -- high level overview of MogileFS
362 L<mogilefsd> -- MogileFS daemon
364 L<http://danga.com/mogilefs/>