/[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

revision 264 by dpavlin, Mon Feb 1 22:03:02 2010 UTC revision 276 by dpavlin, Fri Apr 30 14:34:49 2010 UTC
# Line 41  my $sel = IO::Select->new($lsn); Line 41  my $sel = IO::Select->new($lsn);
41  $sel->add( $www );  $sel->add( $www );
42    
43  my $info;  my $info;
 sub info {  
         my $port = shift;  
         push @{ $info->{node}->{$port} }, [ @_ ];  
 }  
44    
45  sub fork_ssh {  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 68  sub fork_ssh { Line 72  sub fork_ssh {
72  my $node_port = 4000;  my $node_port = 4000;
73    
74  foreach my $host ( @cloud ) {  foreach my $host ( @cloud ) {
         system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional";  
75          fork_ssh( $node_port++, $host );          fork_ssh( $node_port++, $host );
76  }  }
77    
# Line 107  sub load_shard { Line 110  sub load_shard {
110                  push @shard_load_queue, $s;                  push @shard_load_queue, $s;
111                  warn "queued $s for loading\n";                  warn "queued $s for loading\n";
112          }          }
113    
114            # XXX depriciated but not removed yet
115            if ( 0 ) {
116          to_all { load => $shard };          to_all { load => $shard };
117          $info->{pending}->{$_} = 'load' foreach all_ports;          $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 {  sub run_view {
# Line 170  while (1) { Line 218  while (1) {
218                                                  }                                                  }
219                                          };                                          };
220                                          warn "ERROR: $@" if $@;                                          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};                                  my $refresh = $param->{refresh};
244                                  $refresh = 1 if keys %{ $info->{pending} };                                  $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"                                  print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html"
248                                          , ( $refresh ? "\r\nRefresh: $refresh" : '' )                                          , ( $refresh ? "\r\nRefresh: $refresh" : '' )
# Line 191  while (1) { Line 260  while (1) {
260    
261    
262  .forked {  .forked {
263  //      text-decoration: line-through;          color: #888;
264  }  }
265    
266  .ping {  .ping {
267          color: #ff0;          color: #0f0;
268  }  }
269    
270  .load {  .load {
# Line 206  while (1) { Line 275  while (1) {
275          color: #00f;          color: #00f;
276  }  }
277    
 .ready {  
         color: #0f0;  
 }  
   
278  .shards {  .shards {
279          font-family: monospace;          font-family: monospace;
280  //      line-height: 0.8em;  //      line-height: 0.8em;
# Line 266  while (1) { Line 331  while (1) {
331                                          ;                                          ;
332    
333                                  print $send qq|<h1>Nodes</h1>|;                                  print $send qq|<h1>Nodes</h1>|;
334                                  foreach my $node ( keys %{ $info->{node} } ) {                                  foreach my $node ( sort keys %{ $info->{forked} } ) {
335                                          my $class = join(' '                                          my $attr = qq| title="$info->{host}->{$node}"|;
336                                                  , map { $_->[0] } @{ $info->{node}->{$node} }                                          if ( my $pending = $info->{pending}->{$node} ) {
337                                          );                                                  $attr .= qq| class="$pending"|;
338                                          $class .= ' ' . ( $info->{pending}->{$node} || 'ready' );                                          }
339                                          print $send qq|<tt class="$class">$node</tt>\n|;                                          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>|;                                  print $send qq|<h1>Shards</h1><ul>|;
344                                  foreach my $path ( glob '/tmp/sack/*' ) {                                  foreach my $path ( glob '/tmp/sack/*' ) {
# Line 340  while (1) { Line 406  while (1) {
406                                          Storable::store_fd( $response, $sock );                                          Storable::store_fd( $response, $sock );
407                                  } elsif ( $data->{ping} ) {                                  } elsif ( $data->{ping} ) {
408                                          my $port = $data->{port};                                          my $port = $data->{port};
409                                          info $port => 'ping', $port;                                          delete( $info->{pending}->{$port} ) if defined $info->{pending}->{$port} && $info->{pending}->{$port} eq 'forked';
410                                          $session->{port}->{ $data->{port} } = $sock;                                          $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' ) {                                  } elsif ( defined $data->{load} && $data->{load} eq 'shard' ) {
422                                          if ( my $path = shift @shard_load_queue ) {                                          if ( my $path = shift @shard_load_queue ) {
423                                                  $info->{shard}->{$path} = 'read';                                                  $info->{shard}->{$path} = 'read';

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

  ViewVC Help
Powered by ViewVC 1.1.26