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 ) { |
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; |
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 |
} |
} |
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"; |
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 { |
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 |
|
|
217 |
warn "ERROR: $@" if $@; |
warn "ERROR: $@" if $@; |
218 |
} |
} |
219 |
|
|
220 |
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
my $refresh = $param->{refresh}; |
221 |
|
$refresh = 1 if grep { $info->{pending}->{$_} ne 'forked' } keys %{ $info->{pending} }; |
222 |
|
# we don't care about refresh is nodes are stuck in forked state! |
223 |
|
|
224 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html" |
225 |
|
, ( $refresh ? "\r\nRefresh: $refresh" : '' ) |
226 |
|
, "\r\n\r\n" |
227 |
|
, ( $refresh ? qq|<span style="color: #888; float:right">$refresh</span>| : '' ) |
228 |
|
; |
229 |
|
|
230 |
print $send qq| |
print $send qq| |
231 |
|
|
237 |
|
|
238 |
|
|
239 |
.forked { |
.forked { |
240 |
color: #f00; |
color: #888; |
241 |
} |
} |
242 |
|
|
243 |
.ping { |
.ping { |
244 |
color: #0f0; |
color: #0f0; |
245 |
} |
} |
246 |
|
|
247 |
|
.load { |
248 |
|
color: #f00; |
249 |
|
} |
250 |
|
|
251 |
|
.view { |
252 |
|
color: #00f; |
253 |
|
} |
254 |
|
|
255 |
.shards { |
.shards { |
256 |
font-family: monospace; |
font-family: monospace; |
257 |
line-height: 0.8em; |
// line-height: 0.8em; |
258 |
color: #0f0; |
color: #0f0; |
259 |
} |
} |
260 |
|
|
271 |
|
|
272 |
|; |
|; |
273 |
|
|
274 |
|
my $out = Sack::Merge->out; |
275 |
|
if ( my $li = join("\n", map { qq|<li><input type=checkbox name=filter value="$_"><a href="/out/$_" target="$_">$_</a>| } keys %$out ) ) { |
276 |
|
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|; |
277 |
|
} |
278 |
|
|
279 |
print $send qq|<h1>Views</h1><ul>| |
print $send qq|<h1>Views</h1><ul>| |
280 |
, join("\n", map { |
, join("\n", map { |
281 |
my $view = $_; |
my $view = $_; |
307 |
, qq|</ul>| |
, qq|</ul>| |
308 |
; |
; |
309 |
|
|
310 |
my $out = Sack::Merge->out; |
print $send qq|<h1>Nodes</h1>|; |
311 |
if ( my $li = join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out ) ) { |
foreach my $node ( sort keys %{ $info->{forked} } ) { |
312 |
print $send qq|<h1>Results</h1><ul>$li</ul><img src="/gnuplot/">\n|; |
my $attr = qq| title="$info->{host}->{$node}"|; |
313 |
|
if ( my $pending = $info->{pending}->{$node} ) { |
314 |
|
$attr .= qq| class="$pending"|; |
315 |
|
} |
316 |
|
print $send qq|<tt$attr>$node</tt>\n|; |
317 |
} |
} |
318 |
|
|
|
print $send qq|<h1>Nodes</h1> |
|
|
|, join("\n", map { |
|
|
my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} }); |
|
|
qq|<span class="$class">$_</span>|; |
|
|
} sort keys %{ $info->{node} } ); |
|
|
|
|
319 |
print $send qq|<h1>Shards</h1><ul>|; |
print $send qq|<h1>Shards</h1><ul>|; |
320 |
foreach my $path ( glob '/tmp/sack/*' ) { |
foreach my $path ( glob '/tmp/sack/*' ) { |
321 |
print $send qq|<li><a href="$path">$path</a><div class="shards">|; |
print $send qq|<li><a href="$path">$path</a><div class="shards">|; |
382 |
Storable::store_fd( $response, $sock ); |
Storable::store_fd( $response, $sock ); |
383 |
} elsif ( $data->{ping} ) { |
} elsif ( $data->{ping} ) { |
384 |
my $port = $data->{port}; |
my $port = $data->{port}; |
385 |
info $port => 'ping', $port; |
delete( $info->{pending}->{$port} ) if $info->{pending}->{$port} eq 'forked'; |
386 |
$session->{port}->{ $data->{port} } = $sock; |
$session->{port}->{ $data->{port} } = $sock; |
387 |
} elsif ( defined $data->{load} && $data->{load} eq 'shard' ) { |
} elsif ( defined $data->{load} && $data->{load} eq 'shard' ) { |
388 |
if ( my $path = shift @shard_load_queue ) { |
if ( my $path = shift @shard_load_queue ) { |
393 |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
394 |
} else { |
} else { |
395 |
warn "no more shards for [", $data->{port}, "]\n"; |
warn "no more shards for [", $data->{port}, "]\n"; |
396 |
|
delete $info->{pending}->{ $data->{port} }; |
397 |
} |
} |
398 |
} elsif ( exists $data->{out} ) { |
} elsif ( exists $data->{out} ) { |
399 |
my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out}; |
my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out}; |
401 |
$info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard}; |
$info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard}; |
402 |
# refresh shard allocation |
# refresh shard allocation |
403 |
$info->{shard}->{ $_ } = $data->{port} foreach keys %{ $data->{on_shard} }; |
$info->{shard}->{ $_ } = $data->{port} foreach keys %{ $data->{on_shard} }; |
404 |
|
delete $info->{pending}->{ $data->{port} }; |
405 |
} elsif ( exists $data->{port} ) { |
} elsif ( exists $data->{port} ) { |
406 |
push @responses, $data; |
push @responses, $data; |
407 |
warn "# ",dump($data),$/; |
warn "# ",dump($data),$/; |