/[Sack]/trunk/lib/Sack/Server.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Sack/Server.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/experiments/protocol-v3/server.pl revision 173 by dpavlin, Tue Nov 3 17:28:57 2009 UTC trunk/lib/Sack/Server.pm revision 276 by dpavlin, Fri Apr 30 14:34:49 2010 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2    
3    package Sack::Server;
4    
5  use warnings;  use warnings;
6  use strict;  use strict;
7    
8    my $debug = 0;
9    
10  use IO::Socket::INET;  use IO::Socket::INET;
11  use IO::Select;  use IO::Select;
12    
13  use Data::Dump qw(dump);  use Data::Dump qw(dump);
14  use Storable qw();  use Storable qw();
15  use File::Slurp;  use File::Slurp;
16    use Cwd qw(abs_path);
17    
18  my @cloud = qw(localhost tab.lan llin.lan);  use lib '/srv/Sack/lib';
19    use Sack::Merge;
20  my $cloud_path = $ENV{CLOUD} || '/srv/Sack/etc/lib';  use Sack::Server::HTTP;
21    use Sack::Server::HTML;
22    use Sack::Server::Gnuplot;
23    
24    my @cloud;
25    my $cloud_path = $ENV{CLOUD} || "etc/cloud";
26    die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n" unless -e $cloud_path;
27  @cloud = read_file $cloud_path;  @cloud = read_file $cloud_path;
28  @cloud = map { chomp $_; $_ } @cloud;  @cloud = map { chomp $_; $_ } @cloud;
29    
30  warn "# cloud ",dump( @cloud );  warn "# cloud ",dump( @cloud );
31    
32  my $listen_port = 4444;  my $listen_port = 4444;
33    my $http_port   = 4480;
34    
35  my $node_path = $0;  my $node_path = abs_path $0;
36  $node_path =~ s{server.pl}{client.pl};  $node_path =~ s{Server}{Client};
37    
38  my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die $!;  my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die "$listen_port $!";
39    my $www = IO::Socket::INET->new(Listen => 1, LocalPort => $http_port,   Reuse => 1) or die "$http_port $!";
40  my $sel = IO::Select->new($lsn);  my $sel = IO::Select->new($lsn);
41    $sel->add( $www );
42    
43  my $info;  my $info;
 sub info {  
         my $port = shift;  
         push @{ $info->{$port} }, [ @_ ];  
 }  
44    
45  sub fork_node {  sub fork_ssh {
46          my ( $port, $host ) = @_;          my ( $port, $host ) = @_;
47    
48          if ( my $pid = fork ) {          if ( my $pid = fork ) {
49                  # parent                  # parent
50                  info $port => 'forked', $pid;                  $info->{forked}->{$port} = $pid;
51                    $info->{pending}->{$port} = 'forked';
52                    $info->{host}->{$port} = $host;
53                  return $port;                  return $port;
54    
55          } elsif ( ! defined $pid ) {          } elsif ( ! defined $pid ) {
56                  warn "can't fork $host $port";                  warn "can't fork $host $port";
57                  return;                  return;
58            } elsif ( $host =~ m/^(127.0.0.1|localhost)$/ ) {
59                    warn "# localhost, just execute node\n";
60                    exec $node_path, $port, $listen_port;
61          } else {          } else {
62                  # child                  # child
63                    warn "[$port] cpio last version\n";
64                    system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional";
65    
66                  my $cmd = qq|ssh -F $cloud_path.ssh -R $port:127.0.0.1:$listen_port $host $node_path $port|;                  my $cmd = qq|ssh -F $cloud_path.ssh -R $port:127.0.0.1:$listen_port $host $node_path $port|;
67                  warn "# exec: $cmd\n";                  warn "# exec: $cmd\n";
68                  exec $cmd;                  exec $cmd;
# Line 54  sub fork_node { Line 72  sub fork_node {
72  my $node_port = 4000;  my $node_port = 4000;
73    
74  foreach my $host ( @cloud ) {  foreach my $host ( @cloud ) {
75          system "echo $node_path | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional --verbose";          fork_ssh( $node_port++, $host );
         fork_node( $node_port++, $host );  
76  }  }
77    
78  my $session;  my $session;
79    
80    sub all_ports { keys %{ $session->{port} } }
81    
82    sub to_all {
83            my $data = shift;
84            foreach my $port ( all_ports ) {
85                    warn ">>>> [$port]\n" if $debug;
86                    Storable::store_fd( $data, $session->{port}->{$port} );
87            }
88    }
89    
90    our @shard_load_queue;
91    sub load_shard {
92            my $shard = shift @_ || return;
93    
94            warn "# load_shard $shard\n";
95    
96            $shard .= '/*' if -d $shard;
97    
98            my @shards = glob $shard;
99    
100            if ( ! @shards ) {
101                    warn "no shards for $shard\n";
102                    return;
103            }
104    
105            foreach my $s ( @shards ) {
106                    if ( my $node = $info->{shard}->{$s} ) {
107                            next if $node =~ m{^\d+$}; # not node number
108                    }
109                    $info->{shard}->{$s} = 'wait';
110                    push @shard_load_queue, $s;
111                    warn "queued $s for loading\n";
112            }
113    
114            # XXX depriciated but not removed yet
115            if ( 0 ) {
116            to_all { load => $shard };
117            $info->{pending}->{$_} = 'load' foreach all_ports;
118            return;
119            }
120    
121            my @nodes = all_ports;
122            my $chunk = int( ( $#shard_load_queue + 1 ) / $#nodes );
123    
124            my $pid_port;
125    
126            foreach my $port ( all_ports ) {
127                    $info->{pending}->{$port} = 'load';
128                    my @shards = splice @shard_load_queue, 0, $chunk;
129    
130                    my $sock = $session->{port}->{$port};
131    
132                    if ( my $pid = fork ) {
133                            $pid_port->{$pid} = $port;
134                            # parent
135                    } elsif ( ! defined $pid ) {
136                            die "can't fork $!";
137                    } else {
138                            # child
139    
140                            warn ">>>> [$port] bulk_load ", $#shards + 1, " shards\n";
141                            Storable::store_fd( { bulk_load => [ @shards ] }, $sock );
142                            foreach my $s ( @shards ) {
143                                    warn ">>>> [$port] bulk_load $s\n";
144                                    Storable::store_fd( Storable::retrieve( $s ), $sock );
145                            }
146                            warn ">>>> [$port] bulk_load finished\n";
147                            exit;
148                    }
149    
150            }
151    
152            foreach my $child ( keys %$pid_port ) {
153                    warn "waitpid $child\n";
154                    waitpid($child, 0);
155                    delete( $info->{pending}->{ $pid_port->{$child} } );
156            }
157    
158            $info->{pending}->{$_} = 'view' foreach all_ports;
159            to_all { code => "# collect back loaded shards\n", view => 'noop' };
160    }
161    
162    sub run_view {
163            my ( $path ) = @_;
164            if ( ! -r $path ) {
165                    warn "ERROR view $path: $!";
166                    return;
167            }
168            my $code = read_file $path;
169            Sack::Merge->clean;
170            delete( $info->{view} );
171            delete( $info->{merge} );
172            delete( $info->{shard} );
173            $info->{pending}->{$_} = 'view' foreach all_ports;
174            to_all { code => $code, view => $path };
175    };
176    
177    our @responses;
178    
179  while (1) {  while (1) {
180          for my $sock ($sel->can_read(1)) {          for my $sock ($sel->can_read(1)) {
181                  if ($sock == $lsn) {                  if ($sock == $lsn) {
182                          my $new = $lsn->accept;                          my $new = $lsn->accept;
183                          $sel->add($new);                          $sel->add($new);
184                          $session->{$new} = $new->peerport;                          $session->{peerport}->{ $new->peerport } = $new;
185                          warn "[socket] connect\n";                          warn "[socket] connect\n";
186                          Storable::store_fd( { ping => 1 }, $new );                          Storable::store_fd( { ping => 1 }, $new );
187                          info 0 => 'ping', $new->peerport;                  } elsif ( $sock == $www ) {
188                            my $client = $www->accept;
189                            Sack::Server::HTTP::request( $client, sub {
190                                    my ( $send, $method, $param ) = @_;
191    
192                                    if ( $method =~ m{views} ) {
193                                            run_view $method;
194                                            print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
195                                            return 1;
196                                    } elsif ( $method =~ m{^/tmp/sack} ) {
197                                            load_shard $method;
198                                            print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
199                                            return 1;
200                                    } elsif ( $method =~ m{^/out/(.+)} ) {
201                                            print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
202                                            Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param );
203                                            return 1;
204                                    } elsif ( $method =~ m{^/gnuplot} ) {
205                                            eval {
206                                                    my $path = Sack::Server::Gnuplot::date( Sack::Merge->out, $param );
207                                                    if ( -e $path ) {
208                                                            print $send "HTTP/1.0 200 OK\r\nContent-Type: image/png\r\n\r\n";
209                                                            open(my $fh, '<', $path) || die $path;
210                                                            my $b;
211                                                            while ( read($fh, $b, 4096) ) {
212                                                                    print $send $b;
213                                                            }
214                                                            return 1;
215                                                    } else {
216                                                            print $send "HTTP/1.0 404 no graph\r\n\r\n";
217                                                            return 1;
218                                                    }
219                                            };
220                                            warn "ERROR: $@" if $@;
221                                    } elsif ( $method =~ m{^/favicon.ico} ) {
222                                            print $send "HTTP/1.0 200 OK\r\nContent-Type: image/vnd.microsoft.icon\r\n\r\n", read_file( 'artwork/favicon.ico' );
223                                            return 1;
224                                    } elsif ( $method =~ m{/nodes} ) {
225                                            to_all { ping => 1 };
226                                            my @proc = ( 'MemTotal', 'MemFree', 'Buffers', 'Cached', 'VmSize', 'VmPeak' );
227                                            print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n",
228                                                    '<table><tr><th>', join('</th><th>', 'node', 'ip', @proc), '</th></tr>';
229                                            foreach my $node ( sort {
230                                                    $info->{host}->{$a} cmp $info->{host}->{$b} || # ip
231                                                    $a <=> $b # node
232                                            } keys %{$info->{proc}} ) {
233                                                    print $send '<tr><td>', join('</td><td align=right>',
234                                                            $node, $info->{host}->{$node}, map {
235                                                                    my $kb = $info->{proc}->{$node}->{$_};
236                                                                    $kb =~ s/\s+kb//i; $kb;
237                                                            } @proc ), '</td><tr>';
238                                            }
239                                            print $send '</table>';
240                                            return 1;
241                                    }
242    
243                                    my $refresh = $param->{refresh};
244                                    $refresh = 1 if grep { $info->{pending}->{$_} ne 'forked' } keys %{ $info->{pending} };
245                                    # we don't care about refresh is nodes are stuck in forked state!
246    
247                                    print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html"
248                                            , ( $refresh ? "\r\nRefresh: $refresh" : '' )
249                                            , "\r\n\r\n"
250                                            , ( $refresh ? qq|<span style="color: #888; float:right">$refresh</span>| : '' )
251                                    ;
252    
253                                    print $send qq|
254    
255    <style type="text/css">
256    
257    .running {
258            color: #f00;
259    }
260    
261    
262    .forked {
263            color: #888;
264    }
265    
266    .ping {
267            color: #0f0;
268    }
269    
270    .load {
271            color: #f00;
272    }
273    
274    .view {
275            color: #00f;
276    }
277    
278    .shards {
279            font-family: monospace;
280    //      line-height: 0.8em;
281            color: #0f0;
282    }
283    
284    .send {
285            color: #ff0;
286    }
287    
288    .wait {
289            color: #f00;
290    }
291    
292    
293    </style>
294    
295    |;
296    
297                                    my $out = Sack::Merge->out;    
298                                    if ( my $li = join("\n", map { qq|<li><input type=checkbox name=filter value="$_"><a href="/out/$_" target="$_">$_</a>| } keys %$out ) ) {
299                                            print $send qq|<h1>Results</h1><form method="get" target="gnuplot" action="/gnuplot/"><ul>$li</ul><input type=submit value="Show checked"></form><img src="/gnuplot/">\n|;
300                                    }
301    
302                                    print $send qq|<h1>Views</h1><ul>|
303                                            , join("\n", map {
304                                                    my $view = $_;
305                                                    my $html = '';
306                                                    if ( my $s = $info->{view}->{$view} ) {
307                                                            my @nodes = sort keys %$s;
308                                                            my $total = 0;
309                                                            $html .= qq|<table><tr><th>node</th><th>rows</th><th>shards</th><th>&sum;</th><th>merge</th></tr>|;
310                                                            foreach my $node ( @nodes ) {
311                                                                    my $h = '';
312                                                                    my $shard = 0;
313                                                                    foreach my $path ( sort keys %{ $s->{$node} } ) {
314                                                                            my $affected = $s->{$node}->{$path};
315                                                                            my $n = '?';
316                                                                            $n = $1 if $path =~ m{/(\w)\w+/\d+};
317                                                                            my $class = $affected == 0 ? 'style="color: #888"' : '';
318                                                                            $h .= qq|<tt title="$affected $path"$class>$n</tt>|;
319                                                                            $shard += $affected;
320                                                                            $total += $affected;
321                                                                    }
322                                                                    my $merge = $info->{merge}->{$view}->{$node} || '?';
323                                                                    $html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td><td>$merge</td></tr>\n|;
324                                                            }
325    
326                                                            $html .= qq|</table>|;
327                                                    };
328                                                    qq|<li><a href="$view">$view</a>$html|
329                                            } glob '/srv/Sack/views/*/*.pl' )
330                                            , qq|</ul>|
331                                            ;
332    
333                                    print $send qq|<h1>Nodes</h1>|;
334                                    foreach my $node ( sort keys %{ $info->{forked} } ) {
335                                            my $attr = qq| title="$info->{host}->{$node}"|;
336                                            if ( my $pending = $info->{pending}->{$node} ) {
337                                                    $attr .= qq| class="$pending"|;
338                                            }
339                                            print $send qq|<tt$attr>$node</tt>\n|;
340                                    }
341                                    print $send qq|<a href=/nodes/>details</a>|;
342    
343                                    print $send qq|<h1>Shards</h1><ul>|;
344                                    foreach my $path ( glob '/tmp/sack/*' ) {
345                                            print $send qq|<li><a href="$path">$path</a><div class="shards">|;
346                                            foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) {
347                                                    my $i = $info->{shard}->{$s};
348                                                    print $send qq|<span class="$i" title="$s">$i</span> |;
349                                            }
350                                            print $send qq|</div>|;
351                                    }
352                                    print $send qq|</ul>|;
353    
354                                    if ( $param->{info} ) {
355                                            print $send qq|<a href="?info=0">hide info</a>|;
356                                            print $send '<pre>', dump($info), '</pre>'
357                                    } else {
358                                            print $send qq|<a href="?info=1">show info</a>|;
359                                    }
360                                    return 1;
361                            } );
362                  } else {                  } else {
363                          my $data = eval { Storable::fd_retrieve( $sock ) };                          my $data = eval { Storable::fd_retrieve( $sock ) };
364                          if ( $@ ) {                          if ( $@ ) {
# Line 77  while (1) { Line 367  while (1) {
367                                  $sel->remove($sock);                                  $sel->remove($sock);
368                                  $sock->close;                                  $sock->close;
369                          } else {                          } else {
370                                  warn "<<<< ", dump($data), $/;                                  warn "<<<< ", dump($data), $/ if $debug;
371                                  if ( $data->{repl} ) {  
372                                          my $response = { repl => $$, info => $info };                                  if ( my $path = $data->{shard} ) {
373                                            $info->{shard}->{ $path } = $data->{port};
374                                            # FIXME will need push for multiple copies of shards
375                                    }
376    
377                                    if ( my $repl = $data->{repl} ) {
378                                            my $response = { repl_pid => $$ };
379                                            if ( $repl =~ m/ping/ ) {
380                                                    to_all { ping => 1 };
381                                            } elsif ( $repl =~ m/info/ ) {
382                                                    $response->{info} = $info;
383                                            } elsif ( $repl =~ m{load\s*(\S+)?} ) {
384                                                    load_shard $1;
385                                            } elsif ( $repl =~ m{view\s*(\S+)?} ) {
386                                                    run_view $1;
387                                            } elsif ( $repl =~ m{out} ) {
388                                                    my $out = Sack::Merge->out;    
389                                                    warn "out ",dump( $out );
390                                                    $response->{out} = $out;
391                                            } elsif ( $repl =~ m{clean} ) {
392                                                    delete $info->{shard};
393                                                    to_all { clean => 1 };
394                                            } elsif ( $repl eq 'exit' ) {
395                                                    to_all { exit => 1 };
396                                                    sleep 1;
397                                                    exit;
398                                            } elsif ( $repl eq '.' ) {
399                                                    $response->{'.'} = [ @responses ];
400                                                    @responses = ();
401                                            } elsif ( $repl =~ m{(\w+)\s*(.+)?} ) {
402                                                    to_all { $1 => $2 };
403                                            } else {
404                                                    $response->{error}->{repl} = $repl;
405                                            }
406                                          Storable::store_fd( $response, $sock );                                          Storable::store_fd( $response, $sock );
407                                  } elsif ( $data->{ping} ) {                                  } elsif ( $data->{ping} ) {
408                                          $info->{_peer_port}->{$sock->peerport} = $data->{port};                                          my $port = $data->{port};
409                                          info $data->{port} => 'peer port', $sock->peerport;                                          delete( $info->{pending}->{$port} ) if defined $info->{pending}->{$port} && $info->{pending}->{$port} eq 'forked';
410                                            $session->{port}->{$port} = $sock;
411                                            foreach my $name ( keys %{$data->{proc}} ) {
412                                                    if ( $name eq 'loadavg' ) {
413                                                            $info->{proc}->{$port}->{loadavg} = [ split(/\s+/, $data->{proc}->{$name}) ];
414                                                    } else {
415                                                            foreach my $line ( split(/\n/,$data->{proc}->{$name}) ) {
416                                                                    my ($n,$v) = split(/:\s+/,$line);
417                                                                    $info->{proc}->{$port}->{$n} = $v;
418                                                            }
419                                                    }
420                                            }
421                                    } elsif ( defined $data->{load} && $data->{load} eq 'shard' ) {
422                                            if ( my $path = shift @shard_load_queue ) {
423                                                    $info->{shard}->{$path} = 'read';
424                                                    my $shard = Storable::retrieve $path;
425                                                    $info->{shard}->{$path} = 'send';
426                                                    warn ">>>> [", $data->{port}, "] sending shard $path\n";
427                                                    Storable::store_fd( { path => $path, shard => $shard }, $sock );
428                                            } else {
429                                                    warn "no more shards for [", $data->{port}, "]\n";
430                                                    delete $info->{pending}->{ $data->{port} };
431                                            }
432                                    } elsif ( exists $data->{out} ) {
433                                            my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out};
434                                            $info->{merge}->{ $data->{view} }->{ $data->{port} } = $added;
435                                            $info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard};
436                                            # refresh shard allocation
437                                            $info->{shard}->{ $_ } = $data->{port} foreach keys %{ $data->{on_shard} };
438                                            delete $info->{pending}->{ $data->{port} };
439                                    } elsif ( exists $data->{port} ) {
440                                            push @responses, $data;
441                                            warn "# ",dump($data),$/;
442                                    } else {
443                                            warn "UNKNOWN ",dump($data);
444                                  }                                  }
445                          }                          }
446                  }                  }

Legend:
Removed from v.173  
changed lines
  Added in v.276

  ViewVC Help
Powered by ViewVC 1.1.26