19 |
use Sack::Merge; |
use Sack::Merge; |
20 |
use Sack::Server::HTTP; |
use Sack::Server::HTTP; |
21 |
use Sack::Server::HTML; |
use Sack::Server::HTML; |
22 |
|
use Sack::Server::Gnuplot; |
23 |
|
|
24 |
my @cloud; |
my @cloud; |
25 |
my $cloud_path = $ENV{CLOUD} || "etc/cloud"; |
my $cloud_path = $ENV{CLOUD} || "etc/cloud"; |
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"; |
99 |
return; |
return; |
100 |
} |
} |
101 |
|
|
102 |
$info->{shard}->{$_} = 'wait' foreach @shards; |
foreach my $s ( @shards ) { |
103 |
warn "loading shard $shard from ", dump( @shards ); |
if ( my $node = $info->{shard}->{$s} ) { |
104 |
|
next if $node =~ m{^\d+$}; # not node number |
105 |
|
} |
106 |
|
$info->{shard}->{$s} = 'wait'; |
107 |
|
push @shard_load_queue, $s; |
108 |
|
warn "queued $s for loading\n"; |
109 |
|
} |
110 |
|
|
111 |
push @shard_load_queue, @shards; |
# 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 { |
165 |
my $code = read_file $path; |
my $code = read_file $path; |
166 |
Sack::Merge->clean; |
Sack::Merge->clean; |
167 |
delete( $info->{view} ); |
delete( $info->{view} ); |
168 |
|
delete( $info->{merge} ); |
169 |
|
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 |
|
|
174 |
|
our @responses; |
175 |
|
|
176 |
while (1) { |
while (1) { |
177 |
for my $sock ($sel->can_read(1)) { |
for my $sock ($sel->can_read(1)) { |
178 |
if ($sock == $lsn) { |
if ($sock == $lsn) { |
198 |
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
199 |
Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param ); |
Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param ); |
200 |
return 1; |
return 1; |
201 |
|
} elsif ( $method =~ m{^/gnuplot} ) { |
202 |
|
eval { |
203 |
|
my $path = Sack::Server::Gnuplot::date( Sack::Merge->out, $param ); |
204 |
|
if ( -e $path ) { |
205 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: image/png\r\n\r\n"; |
206 |
|
open(my $fh, '<', $path) || die $path; |
207 |
|
my $b; |
208 |
|
while ( read($fh, $b, 4096) ) { |
209 |
|
print $send $b; |
210 |
|
} |
211 |
|
return 1; |
212 |
|
} else { |
213 |
|
print $send "HTTP/1.0 404 no graph\r\n\r\n"; |
214 |
|
return 1; |
215 |
|
} |
216 |
|
}; |
217 |
|
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 |
|
|
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; |
261 |
color: #0f0; |
color: #0f0; |
262 |
} |
} |
263 |
|
|
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 = $_; |
286 |
if ( my $s = $info->{view}->{$view} ) { |
if ( my $s = $info->{view}->{$view} ) { |
287 |
my @nodes = sort keys %$s; |
my @nodes = sort keys %$s; |
288 |
my $total = 0; |
my $total = 0; |
289 |
$html .= qq|<table><tr><th>node</th><th>rows</th><th>shards</th><th>∑</th></tr>|; |
$html .= qq|<table><tr><th>node</th><th>rows</th><th>shards</th><th>∑</th><th>merge</th></tr>|; |
290 |
foreach my $node ( @nodes ) { |
foreach my $node ( @nodes ) { |
291 |
my $h = ''; |
my $h = ''; |
292 |
my $shard = 0; |
my $shard = 0; |
293 |
foreach my $path ( sort keys %{ $s->{$node} } ) { |
foreach my $path ( sort keys %{ $s->{$node} } ) { |
294 |
my $affected = $s->{$node}->{$path}; |
my $affected = $s->{$node}->{$path}; |
295 |
$h .= qq|<span title="$affected $path">*</span>|; |
my $n = '?'; |
296 |
|
$n = $1 if $path =~ m{/(\w)\w+/\d+}; |
297 |
|
my $class = $affected == 0 ? 'style="color: #888"' : ''; |
298 |
|
$h .= qq|<tt title="$affected $path"$class>$n</tt>|; |
299 |
$shard += $affected; |
$shard += $affected; |
300 |
$total += $affected; |
$total += $affected; |
301 |
} |
} |
302 |
$html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td></tr>\n|; |
my $merge = $info->{merge}->{$view}->{$node} || '?'; |
303 |
|
$html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td><td>$merge</td></tr>\n|; |
304 |
} |
} |
305 |
|
|
306 |
$html .= qq|</table>|; |
$html .= qq|</table>|; |
310 |
, qq|</ul>| |
, qq|</ul>| |
311 |
; |
; |
312 |
|
|
313 |
my $out = Sack::Merge->out; |
print $send qq|<h1>Nodes</h1>|; |
314 |
print $send qq|<h1>Results</h1><ul>| |
foreach my $node ( sort keys %{ $info->{forked} } ) { |
315 |
, join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out ) |
my $attr = qq| title="$info->{host}->{$node}"|; |
316 |
, qq|</ul>| |
if ( my $pending = $info->{pending}->{$node} ) { |
317 |
; |
$attr .= qq| class="$pending"|; |
318 |
|
} |
319 |
print $send qq|<h1>Nodes</h1> |
print $send qq|<tt$attr>$node</tt>\n|; |
320 |
|, join("\n", map { |
} |
|
my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} }); |
|
|
qq|<span class="$class">$_</span>|; |
|
|
} sort keys %{ $info->{node} } ); |
|
321 |
|
|
322 |
print $send qq|<h1>Data</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">|; |
325 |
foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) { |
foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) { |
326 |
my $i = $info->{shard}->{$s}; |
my $i = $info->{shard}->{$s}; |
327 |
print $send qq|<span class=$i>$i</span> |; |
print $send qq|<span class="$i" title="$s">$i</span> |; |
328 |
} |
} |
329 |
print $send qq|</div>|; |
print $send qq|</div>|; |
330 |
} |
} |
331 |
print $send qq|</ul>|; |
print $send qq|</ul>|; |
332 |
|
|
333 |
print $send '<pre>', dump($info), '</pre>' if $param->{debug}; |
if ( $param->{info} ) { |
334 |
|
print $send qq|<a href="?info=0">hide info</a>|; |
335 |
|
print $send '<pre>', dump($info), '</pre>' |
336 |
|
} else { |
337 |
|
print $send qq|<a href="?info=1">show info</a>|; |
338 |
|
} |
339 |
return 1; |
return 1; |
340 |
} ); |
} ); |
341 |
} else { |
} else { |
363 |
load_shard $1; |
load_shard $1; |
364 |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
365 |
run_view $1; |
run_view $1; |
|
} elsif ( $repl =~ m{debug\s*(.+)?} ) { |
|
|
to_all { debug => $1 }; |
|
366 |
} elsif ( $repl =~ m{out} ) { |
} elsif ( $repl =~ m{out} ) { |
367 |
my $out = Sack::Merge->out; |
my $out = Sack::Merge->out; |
368 |
warn "out ",dump( $out ); |
warn "out ",dump( $out ); |
374 |
to_all { exit => 1 }; |
to_all { exit => 1 }; |
375 |
sleep 1; |
sleep 1; |
376 |
exit; |
exit; |
377 |
|
} elsif ( $repl eq '.' ) { |
378 |
|
$response->{'.'} = [ @responses ]; |
379 |
|
@responses = (); |
380 |
|
} elsif ( $repl =~ m{(\w+)\s*(.+)?} ) { |
381 |
|
to_all { $1 => $2 }; |
382 |
} else { |
} else { |
383 |
$response->{error}->{unknown} = $data; |
$response->{error}->{repl} = $repl; |
384 |
} |
} |
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 ) { |
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 ( defined $data->{out} ) { |
} elsif ( exists $data->{out} ) { |
402 |
Sack::Merge->add( $data->{out} ); |
my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out}; |
403 |
$info->{view}->{ $data->{view} }->{ $data->{port} } = $data->{on_shard}; |
$info->{merge}->{ $data->{view} }->{ $data->{port} } = $added; |
404 |
|
$info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard}; |
405 |
|
# refresh shard allocation |
406 |
|
$info->{shard}->{ $_ } = $data->{port} foreach keys %{ $data->{on_shard} }; |
407 |
|
delete $info->{pending}->{ $data->{port} }; |
408 |
|
} elsif ( exists $data->{port} ) { |
409 |
|
push @responses, $data; |
410 |
|
warn "# ",dump($data),$/; |
411 |
} else { |
} else { |
412 |
warn "UNKNOWN ",dump($data); |
warn "UNKNOWN ",dump($data); |
413 |
} |
} |