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 |
return $port; |
return $port; |
53 |
|
|
54 |
} elsif ( ! defined $pid ) { |
} elsif ( ! defined $pid ) { |
56 |
return; |
return; |
57 |
} else { |
} else { |
58 |
# child |
# child |
59 |
|
warn "[$port] cpio last version\n"; |
60 |
|
system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional"; |
61 |
|
|
62 |
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|; |
63 |
warn "# exec: $cmd\n"; |
warn "# exec: $cmd\n"; |
64 |
exec $cmd; |
exec $cmd; |
68 |
my $node_port = 4000; |
my $node_port = 4000; |
69 |
|
|
70 |
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"; |
|
71 |
fork_ssh( $node_port++, $host ); |
fork_ssh( $node_port++, $host ); |
72 |
} |
} |
73 |
|
|
74 |
my $session; |
my $session; |
75 |
|
|
76 |
|
sub all_ports { keys %{ $session->{port} } } |
77 |
|
|
78 |
sub to_all { |
sub to_all { |
79 |
my $data = shift; |
my $data = shift; |
80 |
foreach my $port ( keys %{ $session->{port} } ) { |
foreach my $port ( all_ports ) { |
81 |
warn ">>>> [$port]\n" if $debug; |
warn ">>>> [$port]\n" if $debug; |
82 |
Storable::store_fd( $data, $session->{port}->{$port} ); |
Storable::store_fd( $data, $session->{port}->{$port} ); |
83 |
} |
} |
89 |
|
|
90 |
warn "# load_shard $shard\n"; |
warn "# load_shard $shard\n"; |
91 |
|
|
92 |
my @shards = glob "$shard/*"; |
$shard .= '/*' if -d $shard; |
93 |
|
|
94 |
|
my @shards = glob $shard; |
95 |
|
|
96 |
if ( ! @shards ) { |
if ( ! @shards ) { |
97 |
warn "no shards for $shard\n"; |
warn "no shards for $shard\n"; |
98 |
return; |
return; |
99 |
} |
} |
100 |
|
|
101 |
$info->{shard}->{$_} = 'wait' foreach @shards; |
foreach my $s ( @shards ) { |
102 |
warn "loading shard $shard from ", dump( @shards ); |
if ( my $node = $info->{shard}->{$s} ) { |
103 |
|
next if $node =~ m{^\d+$}; # not node number |
104 |
push @shard_load_queue, @shards; |
} |
105 |
|
$info->{shard}->{$s} = 'wait'; |
106 |
|
push @shard_load_queue, $s; |
107 |
|
warn "queued $s for loading\n"; |
108 |
|
} |
109 |
to_all { load => $shard }; |
to_all { load => $shard }; |
110 |
|
$info->{pending}->{$_} = 'load' foreach all_ports; |
111 |
} |
} |
112 |
|
|
113 |
sub run_view { |
sub run_view { |
119 |
my $code = read_file $path; |
my $code = read_file $path; |
120 |
Sack::Merge->clean; |
Sack::Merge->clean; |
121 |
delete( $info->{view} ); |
delete( $info->{view} ); |
122 |
|
delete( $info->{merge} ); |
123 |
|
delete( $info->{shard} ); |
124 |
|
$info->{pending}->{$_} = 'view' foreach all_ports; |
125 |
to_all { code => $code, view => $path }; |
to_all { code => $code, view => $path }; |
126 |
}; |
}; |
127 |
|
|
128 |
|
our @responses; |
129 |
|
|
130 |
while (1) { |
while (1) { |
131 |
for my $sock ($sel->can_read(1)) { |
for my $sock ($sel->can_read(1)) { |
132 |
if ($sock == $lsn) { |
if ($sock == $lsn) { |
152 |
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"; |
153 |
Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param ); |
Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param ); |
154 |
return 1; |
return 1; |
155 |
|
} elsif ( $method =~ m{^/gnuplot} ) { |
156 |
|
eval { |
157 |
|
my $path = Sack::Server::Gnuplot::date( Sack::Merge->out, $param ); |
158 |
|
if ( -e $path ) { |
159 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: image/png\r\n\r\n"; |
160 |
|
open(my $fh, '<', $path) || die $path; |
161 |
|
my $b; |
162 |
|
while ( read($fh, $b, 4096) ) { |
163 |
|
print $send $b; |
164 |
|
} |
165 |
|
return 1; |
166 |
|
} else { |
167 |
|
print $send "HTTP/1.0 404 no graph\r\n\r\n"; |
168 |
|
return 1; |
169 |
|
} |
170 |
|
}; |
171 |
|
warn "ERROR: $@" if $@; |
172 |
} |
} |
173 |
|
|
174 |
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
my $refresh = $param->{refresh}; |
175 |
|
$refresh = 1 if grep { $info->{pending}->{$_} ne 'forked' } keys %{ $info->{pending} }; |
176 |
|
# we don't care about refresh is nodes are stuck in forked state! |
177 |
|
|
178 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html" |
179 |
|
, ( $refresh ? "\r\nRefresh: $refresh" : '' ) |
180 |
|
, "\r\n\r\n" |
181 |
|
, ( $refresh ? qq|<span style="color: #888; float:right">$refresh</span>| : '' ) |
182 |
|
; |
183 |
|
|
184 |
print $send qq| |
print $send qq| |
185 |
|
|
191 |
|
|
192 |
|
|
193 |
.forked { |
.forked { |
194 |
color: #f00; |
color: #888; |
195 |
} |
} |
196 |
|
|
197 |
.ping { |
.ping { |
198 |
color: #0f0; |
color: #0f0; |
199 |
} |
} |
200 |
|
|
201 |
|
.load { |
202 |
|
color: #f00; |
203 |
|
} |
204 |
|
|
205 |
|
.view { |
206 |
|
color: #00f; |
207 |
|
} |
208 |
|
|
209 |
.shards { |
.shards { |
210 |
font-family: monospace; |
font-family: monospace; |
211 |
line-height: 0.8em; |
// line-height: 0.8em; |
212 |
color: #0f0; |
color: #0f0; |
213 |
} |
} |
214 |
|
|
225 |
|
|
226 |
|; |
|; |
227 |
|
|
228 |
|
my $out = Sack::Merge->out; |
229 |
|
if ( my $li = join("\n", map { qq|<li><input type=checkbox name=filter value="$_"><a href="/out/$_" target="$_">$_</a>| } keys %$out ) ) { |
230 |
|
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|; |
231 |
|
} |
232 |
|
|
233 |
print $send qq|<h1>Views</h1><ul>| |
print $send qq|<h1>Views</h1><ul>| |
234 |
, join("\n", map { |
, join("\n", map { |
235 |
my $view = $_; |
my $view = $_; |
237 |
if ( my $s = $info->{view}->{$view} ) { |
if ( my $s = $info->{view}->{$view} ) { |
238 |
my @nodes = sort keys %$s; |
my @nodes = sort keys %$s; |
239 |
my $total = 0; |
my $total = 0; |
240 |
$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>|; |
241 |
foreach my $node ( @nodes ) { |
foreach my $node ( @nodes ) { |
242 |
my $h = ''; |
my $h = ''; |
243 |
my $shard = 0; |
my $shard = 0; |
244 |
foreach my $path ( sort keys %{ $s->{$node} } ) { |
foreach my $path ( sort keys %{ $s->{$node} } ) { |
245 |
my $affected = $s->{$node}->{$path}; |
my $affected = $s->{$node}->{$path}; |
246 |
$h .= qq|<span title="$affected $path">*</span>|; |
my $n = '?'; |
247 |
|
$n = $1 if $path =~ m{/(\w)\w+/\d+}; |
248 |
|
my $class = $affected == 0 ? 'style="color: #888"' : ''; |
249 |
|
$h .= qq|<tt title="$affected $path"$class>$n</tt>|; |
250 |
$shard += $affected; |
$shard += $affected; |
251 |
$total += $affected; |
$total += $affected; |
252 |
} |
} |
253 |
$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} || '?'; |
254 |
|
$html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td><td>$merge</td></tr>\n|; |
255 |
} |
} |
256 |
|
|
257 |
$html .= qq|</table>|; |
$html .= qq|</table>|; |
261 |
, qq|</ul>| |
, qq|</ul>| |
262 |
; |
; |
263 |
|
|
264 |
my $out = Sack::Merge->out; |
print $send qq|<h1>Nodes</h1>|; |
265 |
print $send qq|<h1>Results</h1><ul>| |
foreach my $node ( keys %{ $info->{forked} } ) { |
266 |
, join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out ) |
my $attr = ''; |
267 |
, qq|</ul>| |
if ( my $pending = $info->{pending}->{$node} ) { |
268 |
; |
$attr .= qq| class="$pending"|; |
269 |
|
} |
270 |
print $send qq|<h1>Nodes</h1> |
print $send qq|<tt$attr>$node</tt>\n|; |
271 |
|, join("\n", map { |
} |
|
my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} }); |
|
|
qq|<span class="$class">$_</span>|; |
|
|
} sort keys %{ $info->{node} } ); |
|
272 |
|
|
273 |
print $send qq|<h1>Data</h1><ul>|; |
print $send qq|<h1>Shards</h1><ul>|; |
274 |
foreach my $path ( glob '/tmp/sack/*' ) { |
foreach my $path ( glob '/tmp/sack/*' ) { |
275 |
print $send qq|<li><a href="$path">$path</a><div class="shards">|; |
print $send qq|<li><a href="$path">$path</a><div class="shards">|; |
276 |
foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) { |
foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) { |
277 |
my $i = $info->{shard}->{$s}; |
my $i = $info->{shard}->{$s}; |
278 |
print $send qq|<span class=$i>$i</span> |; |
print $send qq|<span class="$i" title="$s">$i</span> |; |
279 |
} |
} |
280 |
print $send qq|</div>|; |
print $send qq|</div>|; |
281 |
} |
} |
282 |
print $send qq|</ul>|; |
print $send qq|</ul>|; |
283 |
|
|
284 |
print $send '<pre>', dump($info), '</pre>' if $param->{debug}; |
if ( $param->{info} ) { |
285 |
|
print $send qq|<a href="?info=0">hide info</a>|; |
286 |
|
print $send '<pre>', dump($info), '</pre>' |
287 |
|
} else { |
288 |
|
print $send qq|<a href="?info=1">show info</a>|; |
289 |
|
} |
290 |
return 1; |
return 1; |
291 |
} ); |
} ); |
292 |
} else { |
} else { |
314 |
load_shard $1; |
load_shard $1; |
315 |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
316 |
run_view $1; |
run_view $1; |
|
} elsif ( $repl =~ m{debug\s*(.+)?} ) { |
|
|
to_all { debug => $1 }; |
|
317 |
} elsif ( $repl =~ m{out} ) { |
} elsif ( $repl =~ m{out} ) { |
318 |
my $out = Sack::Merge->out; |
my $out = Sack::Merge->out; |
319 |
warn "out ",dump( $out ); |
warn "out ",dump( $out ); |
325 |
to_all { exit => 1 }; |
to_all { exit => 1 }; |
326 |
sleep 1; |
sleep 1; |
327 |
exit; |
exit; |
328 |
|
} elsif ( $repl eq '.' ) { |
329 |
|
$response->{'.'} = [ @responses ]; |
330 |
|
@responses = (); |
331 |
|
} elsif ( $repl =~ m{(\w+)\s*(.+)?} ) { |
332 |
|
to_all { $1 => $2 }; |
333 |
} else { |
} else { |
334 |
$response->{error}->{unknown} = $data; |
$response->{error}->{repl} = $repl; |
335 |
} |
} |
336 |
Storable::store_fd( $response, $sock ); |
Storable::store_fd( $response, $sock ); |
337 |
} elsif ( $data->{ping} ) { |
} elsif ( $data->{ping} ) { |
338 |
my $port = $data->{port}; |
my $port = $data->{port}; |
339 |
info $port => 'ping', $port; |
delete( $info->{pending}->{$port} ) if $info->{pending}->{$port} eq 'forked'; |
340 |
$session->{port}->{ $data->{port} } = $sock; |
$session->{port}->{ $data->{port} } = $sock; |
341 |
} elsif ( defined $data->{load} && $data->{load} eq 'shard' ) { |
} elsif ( defined $data->{load} && $data->{load} eq 'shard' ) { |
342 |
if ( my $path = shift @shard_load_queue ) { |
if ( my $path = shift @shard_load_queue ) { |
347 |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
348 |
} else { |
} else { |
349 |
warn "no more shards for [", $data->{port}, "]\n"; |
warn "no more shards for [", $data->{port}, "]\n"; |
350 |
|
delete $info->{pending}->{ $data->{port} }; |
351 |
} |
} |
352 |
} elsif ( defined $data->{out} ) { |
} elsif ( exists $data->{out} ) { |
353 |
Sack::Merge->add( $data->{out} ); |
my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out}; |
354 |
$info->{view}->{ $data->{view} }->{ $data->{port} } = $data->{on_shard}; |
$info->{merge}->{ $data->{view} }->{ $data->{port} } = $added; |
355 |
|
$info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard}; |
356 |
|
# refresh shard allocation |
357 |
|
$info->{shard}->{ $_ } = $data->{port} foreach keys %{ $data->{on_shard} }; |
358 |
|
delete $info->{pending}->{ $data->{port} }; |
359 |
|
} elsif ( exists $data->{port} ) { |
360 |
|
push @responses, $data; |
361 |
|
warn "# ",dump($data),$/; |
362 |
} else { |
} else { |
363 |
warn "UNKNOWN ",dump($data); |
warn "UNKNOWN ",dump($data); |
364 |
} |
} |