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 ) { |
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; |
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 |
|
|
78 |
my $session; |
my $session; |
79 |
|
|
80 |
|
sub all_ports { keys %{ $session->{port} } } |
81 |
|
|
82 |
sub to_all { |
sub to_all { |
83 |
my $data = shift; |
my $data = shift; |
84 |
foreach my $port ( keys %{ $session->{port} } ) { |
foreach my $port ( all_ports ) { |
85 |
warn ">>>> [$port]\n" if $debug; |
warn ">>>> [$port]\n" if $debug; |
86 |
Storable::store_fd( $data, $session->{port}->{$port} ); |
Storable::store_fd( $data, $session->{port}->{$port} ); |
87 |
} |
} |
93 |
|
|
94 |
warn "# load_shard $shard\n"; |
warn "# load_shard $shard\n"; |
95 |
|
|
96 |
my @shards = glob "$shard/*"; |
$shard .= '/*' if -d $shard; |
97 |
|
|
98 |
|
my @shards = glob $shard; |
99 |
|
|
100 |
if ( ! @shards ) { |
if ( ! @shards ) { |
101 |
warn "no shards for $shard\n"; |
warn "no shards for $shard\n"; |
102 |
return; |
return; |
103 |
} |
} |
104 |
|
|
105 |
$info->{shard}->{$_} = 'wait' foreach @shards; |
foreach my $s ( @shards ) { |
106 |
warn "loading shard $shard from ", dump( @shards ); |
if ( my $node = $info->{shard}->{$s} ) { |
107 |
|
next if $node =~ m{^\d+$}; # not node number |
108 |
|
} |
109 |
|
$info->{shard}->{$s} = 'wait'; |
110 |
|
push @shard_load_queue, $s; |
111 |
|
warn "queued $s for loading\n"; |
112 |
|
} |
113 |
|
|
114 |
push @shard_load_queue, @shards; |
# 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; |
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 |
|
my $current = 0; |
143 |
|
my $total = $#shards + 1; |
144 |
|
foreach my $s ( @shards ) { |
145 |
|
warn ">>>> [$port] bulk_load $s $current/$total\n"; |
146 |
|
Storable::store_fd( Storable::retrieve( $s ), $sock ); |
147 |
|
$current++; |
148 |
|
} |
149 |
|
warn ">>>> [$port] bulk_load finished, $current shards\n"; |
150 |
|
exit; |
151 |
|
} |
152 |
|
|
153 |
|
} |
154 |
|
|
155 |
|
foreach my $child ( keys %$pid_port ) { |
156 |
|
warn "waitpid $child\n"; |
157 |
|
waitpid($child, 0); |
158 |
|
delete( $info->{pending}->{ $pid_port->{$child} } ); |
159 |
|
} |
160 |
|
|
161 |
|
$info->{pending}->{$_} = 'view' foreach all_ports; |
162 |
|
to_all { code => "# collect back loaded shards\n", view => 'noop' }; |
163 |
} |
} |
164 |
|
|
165 |
sub run_view { |
sub run_view { |
171 |
my $code = read_file $path; |
my $code = read_file $path; |
172 |
Sack::Merge->clean; |
Sack::Merge->clean; |
173 |
delete( $info->{view} ); |
delete( $info->{view} ); |
174 |
|
delete( $info->{merge} ); |
175 |
|
delete( $info->{shard} ); |
176 |
|
$info->{pending}->{$_} = 'view' foreach all_ports; |
177 |
to_all { code => $code, view => $path }; |
to_all { code => $code, view => $path }; |
178 |
}; |
}; |
179 |
|
|
180 |
|
our @responses; |
181 |
|
|
182 |
while (1) { |
while (1) { |
183 |
for my $sock ($sel->can_read(1)) { |
for my $sock ($sel->can_read(1)) { |
184 |
if ($sock == $lsn) { |
if ($sock == $lsn) { |
204 |
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"; |
205 |
Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param ); |
Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param ); |
206 |
return 1; |
return 1; |
207 |
|
} elsif ( $method =~ m{^/gnuplot} ) { |
208 |
|
eval { |
209 |
|
my $path = Sack::Server::Gnuplot::date( Sack::Merge->out, $param ); |
210 |
|
if ( -e $path ) { |
211 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: image/png\r\n\r\n"; |
212 |
|
open(my $fh, '<', $path) || die $path; |
213 |
|
my $b; |
214 |
|
while ( read($fh, $b, 4096) ) { |
215 |
|
print $send $b; |
216 |
|
} |
217 |
|
return 1; |
218 |
|
} else { |
219 |
|
print $send "HTTP/1.0 404 no graph\r\n\r\n"; |
220 |
|
return 1; |
221 |
|
} |
222 |
|
}; |
223 |
|
warn "ERROR: $@" if $@; |
224 |
|
} elsif ( $method =~ m{^/favicon.ico} ) { |
225 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: image/vnd.microsoft.icon\r\n\r\n", read_file( 'artwork/favicon.ico' ); |
226 |
|
return 1; |
227 |
|
} elsif ( $method =~ m{/nodes} ) { |
228 |
|
to_all { ping => 1 }; |
229 |
|
my @proc = ( 'MemTotal', 'MemFree', 'Buffers', 'Cached', 'VmSize', 'VmPeak' ); |
230 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n", |
231 |
|
'<table><tr><th>', join('</th><th>', 'node', 'ip', @proc), '</th></tr>'; |
232 |
|
foreach my $node ( sort { |
233 |
|
$info->{host}->{$a} cmp $info->{host}->{$b} || # ip |
234 |
|
$a <=> $b # node |
235 |
|
} keys %{$info->{proc}} ) { |
236 |
|
print $send '<tr><td>', join('</td><td align=right>', |
237 |
|
$node, $info->{host}->{$node}, map { |
238 |
|
my $kb = $info->{proc}->{$node}->{$_}; |
239 |
|
$kb =~ s/\s+kb//i; $kb; |
240 |
|
} @proc ), '</td><tr>'; |
241 |
|
} |
242 |
|
print $send '</table>'; |
243 |
|
return 1; |
244 |
} |
} |
245 |
|
|
246 |
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
my $refresh = $param->{refresh}; |
247 |
|
$refresh = 1 if grep { $info->{pending}->{$_} ne 'forked' } keys %{ $info->{pending} }; |
248 |
|
# we don't care about refresh is nodes are stuck in forked state! |
249 |
|
|
250 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html" |
251 |
|
, ( $refresh ? "\r\nRefresh: $refresh" : '' ) |
252 |
|
, "\r\n\r\n" |
253 |
|
, ( $refresh ? qq|<span style="color: #888; float:right">$refresh</span>| : '' ) |
254 |
|
; |
255 |
|
|
256 |
print $send qq| |
print $send qq| |
257 |
|
|
263 |
|
|
264 |
|
|
265 |
.forked { |
.forked { |
266 |
color: #f00; |
color: #888; |
267 |
} |
} |
268 |
|
|
269 |
.ping { |
.ping { |
270 |
color: #0f0; |
color: #0f0; |
271 |
} |
} |
272 |
|
|
273 |
|
.load { |
274 |
|
color: #f00; |
275 |
|
} |
276 |
|
|
277 |
|
.view { |
278 |
|
color: #00f; |
279 |
|
} |
280 |
|
|
281 |
.shards { |
.shards { |
282 |
font-family: monospace; |
font-family: monospace; |
283 |
line-height: 0.8em; |
// line-height: 0.8em; |
284 |
color: #0f0; |
color: #0f0; |
285 |
} |
} |
286 |
|
|
297 |
|
|
298 |
|; |
|; |
299 |
|
|
300 |
|
my $out = Sack::Merge->out; |
301 |
|
if ( my $li = join("\n", map { qq|<li><input type=checkbox name=filter value="$_"><a href="/out/$_" target="$_">$_</a>| } keys %$out ) ) { |
302 |
|
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|; |
303 |
|
} |
304 |
|
|
305 |
print $send qq|<h1>Views</h1><ul>| |
print $send qq|<h1>Views</h1><ul>| |
306 |
, join("\n", map { |
, join("\n", map { |
307 |
my $view = $_; |
my $view = $_; |
309 |
if ( my $s = $info->{view}->{$view} ) { |
if ( my $s = $info->{view}->{$view} ) { |
310 |
my @nodes = sort keys %$s; |
my @nodes = sort keys %$s; |
311 |
my $total = 0; |
my $total = 0; |
312 |
$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>|; |
313 |
foreach my $node ( @nodes ) { |
foreach my $node ( @nodes ) { |
314 |
my $h = ''; |
my $h = ''; |
315 |
my $shard = 0; |
my $shard = 0; |
316 |
foreach my $path ( sort keys %{ $s->{$node} } ) { |
foreach my $path ( sort keys %{ $s->{$node} } ) { |
317 |
my $affected = $s->{$node}->{$path}; |
my $affected = $s->{$node}->{$path}; |
318 |
$h .= qq|<span title="$affected $path">*</span>|; |
my $n = '?'; |
319 |
|
$n = $1 if $path =~ m{/(\w)\w+/\d+}; |
320 |
|
my $class = $affected == 0 ? 'style="color: #888"' : ''; |
321 |
|
$h .= qq|<tt title="$affected $path"$class>$n</tt>|; |
322 |
$shard += $affected; |
$shard += $affected; |
323 |
$total += $affected; |
$total += $affected; |
324 |
} |
} |
325 |
$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} || '?'; |
326 |
|
$html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td><td>$merge</td></tr>\n|; |
327 |
} |
} |
328 |
|
|
329 |
$html .= qq|</table>|; |
$html .= qq|</table>|; |
333 |
, qq|</ul>| |
, qq|</ul>| |
334 |
; |
; |
335 |
|
|
336 |
my $out = Sack::Merge->out; |
print $send qq|<h1>Nodes</h1>|; |
337 |
print $send qq|<h1>Results</h1><ul>| |
foreach my $node ( sort keys %{ $info->{forked} } ) { |
338 |
, join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out ) |
my $attr = qq| title="$info->{host}->{$node}"|; |
339 |
, qq|</ul>| |
if ( my $pending = $info->{pending}->{$node} ) { |
340 |
; |
$attr .= qq| class="$pending"|; |
341 |
|
} |
342 |
print $send qq|<h1>Nodes</h1> |
print $send qq|<tt$attr>$node</tt>\n|; |
343 |
|, join("\n", map { |
} |
344 |
my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} }); |
print $send qq|<a href=/nodes/>details</a>|; |
|
qq|<span class="$class">$_</span>|; |
|
|
} sort keys %{ $info->{node} } ); |
|
345 |
|
|
346 |
print $send qq|<h1>Data</h1><ul>|; |
print $send qq|<h1>Shards</h1><ul>|; |
347 |
foreach my $path ( glob '/tmp/sack/*' ) { |
foreach my $path ( glob '/tmp/sack/*' ) { |
348 |
print $send qq|<li><a href="$path">$path</a><div class="shards">|; |
print $send qq|<li><a href="$path">$path</a><div class="shards">|; |
349 |
foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) { |
foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) { |
350 |
my $i = $info->{shard}->{$s}; |
my $i = $info->{shard}->{$s}; |
351 |
print $send qq|<span class=$i>$i</span> |; |
print $send qq|<span class="$i" title="$s">$i</span> |; |
352 |
} |
} |
353 |
print $send qq|</div>|; |
print $send qq|</div>|; |
354 |
} |
} |
355 |
print $send qq|</ul>|; |
print $send qq|</ul>|; |
356 |
|
|
357 |
print $send '<pre>', dump($info), '</pre>' if $param->{debug}; |
if ( $param->{info} ) { |
358 |
|
print $send qq|<a href="?info=0">hide info</a>|; |
359 |
|
print $send '<pre>', dump($info), '</pre>' |
360 |
|
} else { |
361 |
|
print $send qq|<a href="?info=1">show info</a>|; |
362 |
|
} |
363 |
return 1; |
return 1; |
364 |
} ); |
} ); |
365 |
} else { |
} else { |
387 |
load_shard $1; |
load_shard $1; |
388 |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
389 |
run_view $1; |
run_view $1; |
|
} elsif ( $repl =~ m{debug\s*(.+)?} ) { |
|
|
to_all { debug => $1 }; |
|
390 |
} elsif ( $repl =~ m{out} ) { |
} elsif ( $repl =~ m{out} ) { |
391 |
my $out = Sack::Merge->out; |
my $out = Sack::Merge->out; |
392 |
warn "out ",dump( $out ); |
warn "out ",dump( $out ); |
398 |
to_all { exit => 1 }; |
to_all { exit => 1 }; |
399 |
sleep 1; |
sleep 1; |
400 |
exit; |
exit; |
401 |
|
} elsif ( $repl eq '.' ) { |
402 |
|
$response->{'.'} = [ @responses ]; |
403 |
|
@responses = (); |
404 |
|
} elsif ( $repl =~ m{(\w+)\s*(.+)?} ) { |
405 |
|
to_all { $1 => $2 }; |
406 |
} else { |
} else { |
407 |
$response->{error}->{unknown} = $data; |
$response->{error}->{repl} = $repl; |
408 |
} |
} |
409 |
Storable::store_fd( $response, $sock ); |
Storable::store_fd( $response, $sock ); |
410 |
} elsif ( $data->{ping} ) { |
} elsif ( $data->{ping} ) { |
411 |
my $port = $data->{port}; |
my $port = $data->{port}; |
412 |
info $port => 'ping', $port; |
delete( $info->{pending}->{$port} ) if defined $info->{pending}->{$port} && $info->{pending}->{$port} eq 'forked'; |
413 |
$session->{port}->{ $data->{port} } = $sock; |
$session->{port}->{$port} = $sock; |
414 |
|
foreach my $name ( keys %{$data->{proc}} ) { |
415 |
|
if ( $name eq 'loadavg' ) { |
416 |
|
$info->{proc}->{$port}->{loadavg} = [ split(/\s+/, $data->{proc}->{$name}) ]; |
417 |
|
} else { |
418 |
|
foreach my $line ( split(/\n/,$data->{proc}->{$name}) ) { |
419 |
|
my ($n,$v) = split(/:\s+/,$line); |
420 |
|
$info->{proc}->{$port}->{$n} = $v; |
421 |
|
} |
422 |
|
} |
423 |
|
} |
424 |
} elsif ( defined $data->{load} && $data->{load} eq 'shard' ) { |
} elsif ( defined $data->{load} && $data->{load} eq 'shard' ) { |
425 |
if ( my $path = shift @shard_load_queue ) { |
if ( my $path = shift @shard_load_queue ) { |
426 |
$info->{shard}->{$path} = 'read'; |
$info->{shard}->{$path} = 'read'; |
430 |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
431 |
} else { |
} else { |
432 |
warn "no more shards for [", $data->{port}, "]\n"; |
warn "no more shards for [", $data->{port}, "]\n"; |
433 |
|
delete $info->{pending}->{ $data->{port} }; |
434 |
} |
} |
435 |
} elsif ( defined $data->{out} ) { |
} elsif ( exists $data->{out} ) { |
436 |
Sack::Merge->add( $data->{out} ); |
my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out}; |
437 |
$info->{view}->{ $data->{view} }->{ $data->{port} } = $data->{on_shard}; |
$info->{merge}->{ $data->{view} }->{ $data->{port} } = $added; |
438 |
|
$info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard}; |
439 |
|
# refresh shard allocation |
440 |
|
$info->{shard}->{ $_ } = $data->{port} foreach keys %{ $data->{on_shard} }; |
441 |
|
delete $info->{pending}->{ $data->{port} }; |
442 |
|
} elsif ( exists $data->{port} ) { |
443 |
|
push @responses, $data; |
444 |
|
warn "# ",dump($data),$/; |
445 |
} else { |
} else { |
446 |
warn "UNKNOWN ",dump($data); |
warn "UNKNOWN ",dump($data); |
447 |
} |
} |