/[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 253 by dpavlin, Fri Nov 27 13:48:54 2009 UTC revision 273 by dpavlin, Wed Apr 28 08:08:47 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    
75  my $session;  my $session;
76    
77    sub all_ports { keys %{ $session->{port} } }
78    
79  sub to_all {  sub to_all {
80          my $data = shift;          my $data = shift;
81          foreach my $port ( keys %{ $session->{port} } ) {          foreach my $port ( all_ports ) {
82                  warn ">>>> [$port]\n" if $debug;                  warn ">>>> [$port]\n" if $debug;
83                  Storable::store_fd( $data, $session->{port}->{$port} );                  Storable::store_fd( $data, $session->{port}->{$port} );
84          }          }
# Line 88  sub load_shard { Line 90  sub load_shard {
90    
91          warn "# load_shard $shard\n";          warn "# load_shard $shard\n";
92    
93          my @shards = glob "$shard/*";          $shard .= '/*' if -d $shard;
94    
95            my @shards = glob $shard;
96    
97          if ( ! @shards ) {          if ( ! @shards ) {
98                  warn "no shards for $shard\n";                  warn "no shards for $shard\n";
# Line 96  sub load_shard { Line 100  sub load_shard {
100          }          }
101    
102          foreach my $s ( @shards ) {          foreach my $s ( @shards ) {
103                  next if $info->{shard}->{$s} =~ m{^\d+$};                  if ( my $node = $info->{shard}->{$s} ) {
104                            next if $node =~ m{^\d+$}; # not node number
105                    }
106                  $info->{shard}->{$s} = 'wait';                  $info->{shard}->{$s} = 'wait';
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;
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 115  sub run_view { Line 167  sub run_view {
167          delete( $info->{view} );          delete( $info->{view} );
168          delete( $info->{merge} );          delete( $info->{merge} );
169          delete( $info->{shard} );          delete( $info->{shard} );
170            $info->{pending}->{$_} = 'view' foreach all_ports;
171          to_all { code => $code, view => $path };          to_all { code => $code, view => $path };
172  };  };
173    
# Line 162  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                                  }                                  }
222    
223                                  print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";                                  my $refresh = $param->{refresh};
224                                    $refresh = 1 if grep { $info->{pending}->{$_} ne 'forked' } keys %{ $info->{pending} };
225                                    # we don't care about refresh is nodes are stuck in forked state!
226    
227                                    print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html"
228                                            , ( $refresh ? "\r\nRefresh: $refresh" : '' )
229                                            , "\r\n\r\n"
230                                            , ( $refresh ? qq|<span style="color: #888; float:right">$refresh</span>| : '' )
231                                    ;
232    
233                                  print $send qq|                                  print $send qq|
234    
# Line 176  while (1) { Line 240  while (1) {
240    
241    
242  .forked {  .forked {
243          color: #f00;          color: #888;
244  }  }
245    
246  .ping {  .ping {
247          color: #0f0;          color: #0f0;
248  }  }
249    
250    .load {
251            color: #f00;
252    }
253    
254    .view {
255            color: #00f;
256    }
257    
258  .shards {  .shards {
259          font-family: monospace;          font-family: monospace;
260  //      line-height: 0.8em;  //      line-height: 0.8em;
# Line 202  while (1) { Line 274  while (1) {
274    
275  |;  |;
276    
277                                    my $out = Sack::Merge->out;    
278                                    if ( my $li = join("\n", map { qq|<li><input type=checkbox name=filter value="$_"><a href="/out/$_" target="$_">$_</a>| } keys %$out ) ) {
279                                            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|;
280                                    }
281    
282                                  print $send qq|<h1>Views</h1><ul>|                                  print $send qq|<h1>Views</h1><ul>|
283                                          , join("\n", map {                                          , join("\n", map {
284                                                  my $view = $_;                                                  my $view = $_;
# Line 233  while (1) { Line 310  while (1) {
310                                          , qq|</ul>|                                          , qq|</ul>|
311                                          ;                                          ;
312    
313                                  my $out = Sack::Merge->out;                                      print $send qq|<h1>Nodes</h1>|;
314                                  if ( my $li = join("\n", map { qq|<li><input type=checkbox name=filter value="$_"><a href="/out/$_" target="$_">$_</a>| } keys %$out ) ) {                                  foreach my $node ( sort keys %{ $info->{forked} } ) {
315                                          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|;                                          my $attr = qq| title="$info->{host}->{$node}"|;
316                                            if ( my $pending = $info->{pending}->{$node} ) {
317                                                    $attr .= qq| class="$pending"|;
318                                            }
319                                            print $send qq|<tt$attr>$node</tt>\n|;
320                                  }                                  }
321    
                                 print $send qq|<h1>Nodes</h1>  
                                 |, join("\n", map {  
                                         my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} });  
                                         qq|<tt class="$class">$_</tt>|;  
                                 } sort keys %{ $info->{node} } );  
   
322                                  print $send qq|<h1>Shards</h1><ul>|;                                  print $send qq|<h1>Shards</h1><ul>|;
323                                  foreach my $path ( glob '/tmp/sack/*' ) {                                  foreach my $path ( glob '/tmp/sack/*' ) {
324                                          print $send qq|<li><a href="$path">$path</a><div class="shards">|;                                          print $send qq|<li><a href="$path">$path</a><div class="shards">|;
# Line 310  while (1) { Line 385  while (1) {
385                                          Storable::store_fd( $response, $sock );                                          Storable::store_fd( $response, $sock );
386                                  } elsif ( $data->{ping} ) {                                  } elsif ( $data->{ping} ) {
387                                          my $port = $data->{port};                                          my $port = $data->{port};
388                                          info $port => 'ping', $port;                                          delete( $info->{pending}->{$port} ) if $info->{pending}->{$port} eq 'forked';
389                                          $session->{port}->{ $data->{port} } = $sock;                                          $session->{port}->{ $data->{port} } = $sock;
390                                  } elsif ( defined $data->{load} && $data->{load} eq 'shard' ) {                                  } elsif ( defined $data->{load} && $data->{load} eq 'shard' ) {
391                                          if ( my $path = shift @shard_load_queue ) {                                          if ( my $path = shift @shard_load_queue ) {
# Line 321  while (1) { Line 396  while (1) {
396                                                  Storable::store_fd( { path => $path, shard => $shard }, $sock );                                                  Storable::store_fd( { path => $path, shard => $shard }, $sock );
397                                          } else {                                          } else {
398                                                  warn "no more shards for [", $data->{port}, "]\n";                                                  warn "no more shards for [", $data->{port}, "]\n";
399                                                    delete $info->{pending}->{ $data->{port} };
400                                          }                                          }
401                                  } elsif ( exists $data->{out} ) {                                  } elsif ( exists $data->{out} ) {
402                                          my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out};                                          my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out};
# Line 328  while (1) { Line 404  while (1) {
404                                          $info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard};                                          $info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard};
405                                          # refresh shard allocation                                          # refresh shard allocation
406                                          $info->{shard}->{ $_ } = $data->{port} foreach keys %{ $data->{on_shard} };                                          $info->{shard}->{ $_ } = $data->{port} foreach keys %{ $data->{on_shard} };
407                                            delete $info->{pending}->{ $data->{port} };
408                                  } elsif ( exists $data->{port} ) {                                  } elsif ( exists $data->{port} ) {
409                                          push @responses, $data;                                          push @responses, $data;
410                                          warn "# ",dump($data),$/;                                          warn "# ",dump($data),$/;

Legend:
Removed from v.253  
changed lines
  Added in v.273

  ViewVC Help
Powered by ViewVC 1.1.26