/[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 275 by dpavlin, Wed Apr 28 20:20:35 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 ) {
# Line 59  sub fork_ssh { Line 57  sub fork_ssh {
57                  return;                  return;
58          } else {          } else {
59                  # child                  # child
60                    warn "[$port] cpio last version\n";
61                    system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional";
62    
63                  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|;
64                  warn "# exec: $cmd\n";                  warn "# exec: $cmd\n";
65                  exec $cmd;                  exec $cmd;
# Line 68  sub fork_ssh { Line 69  sub fork_ssh {
69  my $node_port = 4000;  my $node_port = 4000;
70    
71  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";  
72          fork_ssh( $node_port++, $host );          fork_ssh( $node_port++, $host );
73  }  }
74    
# Line 107  sub load_shard { Line 107  sub load_shard {
107                  push @shard_load_queue, $s;                  push @shard_load_queue, $s;
108                  warn "queued $s for loading\n";                  warn "queued $s for loading\n";
109          }          }
110    
111            # XXX depriciated but not removed yet
112            if ( 0 ) {
113          to_all { load => $shard };          to_all { load => $shard };
114          $info->{pending}->{$_} = 'load' foreach all_ports;          $info->{pending}->{$_} = 'load' foreach all_ports;
115            return;
116            }
117    
118            my @nodes = all_ports;
119            my $chunk = int( ( $#shard_load_queue + 1 ) / $#nodes );
120    
121            my $pid_port;
122    
123            foreach my $port ( all_ports ) {
124                    $info->{pending}->{$port} = 'load';
125                    my @shards = splice @shard_load_queue, 0, $chunk;
126    
127                    my $sock = $session->{port}->{$port};
128    
129                    if ( my $pid = fork ) {
130                            $pid_port->{$pid} = $port;
131                            # parent
132                    } elsif ( ! defined $pid ) {
133                            die "can't fork $!";
134                    } else {
135                            # child
136    
137                            warn ">>>> [$port] bulk_load ", $#shards + 1, " shards\n";
138                            Storable::store_fd( { bulk_load => [ @shards ] }, $sock );
139                            foreach my $s ( @shards ) {
140                                    warn ">>>> [$port] bulk_load $s\n";
141                                    Storable::store_fd( Storable::retrieve( $s ), $sock );
142                            }
143                            warn ">>>> [$port] bulk_load finished\n";
144                            exit;
145                    }
146    
147            }
148    
149            foreach my $child ( keys %$pid_port ) {
150                    warn "waitpid $child\n";
151                    waitpid($child, 0);
152                    delete( $info->{pending}->{ $pid_port->{$child} } );
153            }
154    
155            $info->{pending}->{$_} = 'view' foreach all_ports;
156            to_all { code => "# collect back loaded shards\n", view => 'noop' };
157  }  }
158    
159  sub run_view {  sub run_view {
# Line 170  while (1) { Line 215  while (1) {
215                                                  }                                                  }
216                                          };                                          };
217                                          warn "ERROR: $@" if $@;                                          warn "ERROR: $@" if $@;
218                                    } elsif ( $method =~ m{^/favicon.ico} ) {
219                                            print $send "HTTP/1.0 200 OK\r\nContent-Type: image/vnd.microsoft.icon\r\n\r\n", read_file( 'artwork/favicon.ico' );
220                                            return 1;
221                                    } elsif ( $method =~ m{/nodes} ) {
222                                            to_all { ping => 1 };
223                                            my @proc = ( 'MemTotal', 'MemFree', 'Buffers', 'Cached', 'VmSize', 'VmPeak' );
224                                            print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n",
225                                                    '<table><tr><th>', join('</th><th>', 'node', 'ip', @proc), '</th></tr>';
226                                            foreach my $node ( sort {
227                                                    $info->{host}->{$a} cmp $info->{host}->{$b} || # ip
228                                                    $a <=> $b # node
229                                            } keys %{$info->{proc}} ) {
230                                                    print $send '<tr><td>', join('</td><td align=right>',
231                                                            $node, $info->{host}->{$node}, map {
232                                                                    my $kb = $info->{proc}->{$node}->{$_};
233                                                                    $kb =~ s/\s+kb//i; $kb;
234                                                            } @proc ), '</td><tr>';
235                                            }
236                                            print $send '</table>';
237                                            return 1;
238                                  }                                  }
239    
240                                  my $refresh = $param->{refresh};                                  my $refresh = $param->{refresh};
241                                  $refresh = 1 if keys %{ $info->{pending} };                                  $refresh = 1 if grep { $info->{pending}->{$_} ne 'forked' } keys %{ $info->{pending} };
242                                    # we don't care about refresh is nodes are stuck in forked state!
243    
244                                  print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html"                                  print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html"
245                                          , ( $refresh ? "\r\nRefresh: $refresh" : '' )                                          , ( $refresh ? "\r\nRefresh: $refresh" : '' )
# Line 191  while (1) { Line 257  while (1) {
257    
258    
259  .forked {  .forked {
260  //      text-decoration: line-through;          color: #888;
261  }  }
262    
263  .ping {  .ping {
264          color: #ff0;          color: #0f0;
265  }  }
266    
267  .load {  .load {
# Line 206  while (1) { Line 272  while (1) {
272          color: #00f;          color: #00f;
273  }  }
274    
 .ready {  
         color: #0f0;  
 }  
   
275  .shards {  .shards {
276          font-family: monospace;          font-family: monospace;
277  //      line-height: 0.8em;  //      line-height: 0.8em;
# Line 266  while (1) { Line 328  while (1) {
328                                          ;                                          ;
329    
330                                  print $send qq|<h1>Nodes</h1>|;                                  print $send qq|<h1>Nodes</h1>|;
331                                  foreach my $node ( keys %{ $info->{node} } ) {                                  foreach my $node ( sort keys %{ $info->{forked} } ) {
332                                          my $class = join(' '                                          my $attr = qq| title="$info->{host}->{$node}"|;
333                                                  , map { $_->[0] } @{ $info->{node}->{$node} }                                          if ( my $pending = $info->{pending}->{$node} ) {
334                                          );                                                  $attr .= qq| class="$pending"|;
335                                          $class .= ' ' . ( $info->{pending}->{$node} || 'ready' );                                          }
336                                          print $send qq|<tt class="$class">$node</tt>\n|;                                          print $send qq|<tt$attr>$node</tt>\n|;
337                                  }                                  }
338                                    print $send qq|<a href=/nodes/>details</a>|;
339    
340                                  print $send qq|<h1>Shards</h1><ul>|;                                  print $send qq|<h1>Shards</h1><ul>|;
341                                  foreach my $path ( glob '/tmp/sack/*' ) {                                  foreach my $path ( glob '/tmp/sack/*' ) {
# Line 340  while (1) { Line 403  while (1) {
403                                          Storable::store_fd( $response, $sock );                                          Storable::store_fd( $response, $sock );
404                                  } elsif ( $data->{ping} ) {                                  } elsif ( $data->{ping} ) {
405                                          my $port = $data->{port};                                          my $port = $data->{port};
406                                          info $port => 'ping', $port;                                          delete( $info->{pending}->{$port} ) if defined $info->{pending}->{$port} && $info->{pending}->{$port} eq 'forked';
407                                          $session->{port}->{ $data->{port} } = $sock;                                          $session->{port}->{$port} = $sock;
408                                            foreach my $name ( keys %{$data->{proc}} ) {
409                                                    if ( $name eq 'loadavg' ) {
410                                                            $info->{proc}->{$port}->{loadavg} = [ split(/\s+/, $data->{proc}->{$name}) ];
411                                                    } else {
412                                                            foreach my $line ( split(/\n/,$data->{proc}->{$name}) ) {
413                                                                    my ($n,$v) = split(/:\s+/,$line);
414                                                                    $info->{proc}->{$port}->{$n} = $v;
415                                                            }
416                                                    }
417                                            }
418                                  } elsif ( defined $data->{load} && $data->{load} eq 'shard' ) {                                  } elsif ( defined $data->{load} && $data->{load} eq 'shard' ) {
419                                          if ( my $path = shift @shard_load_queue ) {                                          if ( my $path = shift @shard_load_queue ) {
420                                                  $info->{shard}->{$path} = 'read';                                                  $info->{shard}->{$path} = 'read';

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

  ViewVC Help
Powered by ViewVC 1.1.26