1 |
#!/usr/bin/perl |
2 |
|
3 |
package Sack::Server; |
4 |
|
5 |
use warnings; |
6 |
use strict; |
7 |
|
8 |
my $debug = 0; |
9 |
|
10 |
use IO::Socket::INET; |
11 |
use IO::Select; |
12 |
|
13 |
use Data::Dump qw(dump); |
14 |
use Storable qw(); |
15 |
use File::Slurp; |
16 |
use Cwd qw(abs_path); |
17 |
|
18 |
use lib '/srv/Sack/lib'; |
19 |
use Sack::Merge; |
20 |
use Sack::Server::HTTP; |
21 |
use Sack::Server::HTML; |
22 |
use Sack::Server::Gnuplot; |
23 |
|
24 |
my @cloud; |
25 |
my $cloud_path = $ENV{CLOUD} || "etc/cloud"; |
26 |
die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n" unless -e $cloud_path; |
27 |
@cloud = read_file $cloud_path; |
28 |
@cloud = map { chomp $_; $_ } @cloud; |
29 |
|
30 |
warn "# cloud ",dump( @cloud ); |
31 |
|
32 |
my $listen_port = 4444; |
33 |
my $http_port = 4480; |
34 |
|
35 |
my $node_path = abs_path $0; |
36 |
$node_path =~ s{Server}{Client}; |
37 |
|
38 |
my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die "$listen_port $!"; |
39 |
my $www = IO::Socket::INET->new(Listen => 1, LocalPort => $http_port, Reuse => 1) or die "$http_port $!"; |
40 |
my $sel = IO::Select->new($lsn); |
41 |
$sel->add( $www ); |
42 |
|
43 |
my $info; |
44 |
|
45 |
sub fork_ssh { |
46 |
my ( $port, $host ) = @_; |
47 |
|
48 |
if ( my $pid = fork ) { |
49 |
# parent |
50 |
$info->{forked}->{$port} = $pid; |
51 |
$info->{pending}->{$port} = 'forked'; |
52 |
$info->{host}->{$port} = $host; |
53 |
return $port; |
54 |
|
55 |
} elsif ( ! defined $pid ) { |
56 |
warn "can't fork $host $port"; |
57 |
return; |
58 |
} else { |
59 |
# 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|; |
64 |
warn "# exec: $cmd\n"; |
65 |
exec $cmd; |
66 |
} |
67 |
} |
68 |
|
69 |
my $node_port = 4000; |
70 |
|
71 |
foreach my $host ( @cloud ) { |
72 |
fork_ssh( $node_port++, $host ); |
73 |
} |
74 |
|
75 |
my $session; |
76 |
|
77 |
sub all_ports { keys %{ $session->{port} } } |
78 |
|
79 |
sub to_all { |
80 |
my $data = shift; |
81 |
foreach my $port ( all_ports ) { |
82 |
warn ">>>> [$port]\n" if $debug; |
83 |
Storable::store_fd( $data, $session->{port}->{$port} ); |
84 |
} |
85 |
} |
86 |
|
87 |
our @shard_load_queue; |
88 |
sub load_shard { |
89 |
my $shard = shift @_ || return; |
90 |
|
91 |
warn "# load_shard $shard\n"; |
92 |
|
93 |
$shard .= '/*' if -d $shard; |
94 |
|
95 |
my @shards = glob $shard; |
96 |
|
97 |
if ( ! @shards ) { |
98 |
warn "no shards for $shard\n"; |
99 |
return; |
100 |
} |
101 |
|
102 |
foreach my $s ( @shards ) { |
103 |
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 |
# XXX depriciated but not removed yet |
112 |
if ( 0 ) { |
113 |
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 { |
160 |
my ( $path ) = @_; |
161 |
if ( ! -r $path ) { |
162 |
warn "ERROR view $path: $!"; |
163 |
return; |
164 |
} |
165 |
my $code = read_file $path; |
166 |
Sack::Merge->clean; |
167 |
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 }; |
172 |
}; |
173 |
|
174 |
our @responses; |
175 |
|
176 |
while (1) { |
177 |
for my $sock ($sel->can_read(1)) { |
178 |
if ($sock == $lsn) { |
179 |
my $new = $lsn->accept; |
180 |
$sel->add($new); |
181 |
$session->{peerport}->{ $new->peerport } = $new; |
182 |
warn "[socket] connect\n"; |
183 |
Storable::store_fd( { ping => 1 }, $new ); |
184 |
} elsif ( $sock == $www ) { |
185 |
my $client = $www->accept; |
186 |
Sack::Server::HTTP::request( $client, sub { |
187 |
my ( $send, $method, $param ) = @_; |
188 |
|
189 |
if ( $method =~ m{views} ) { |
190 |
run_view $method; |
191 |
print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n"; |
192 |
return 1; |
193 |
} elsif ( $method =~ m{^/tmp/sack} ) { |
194 |
load_shard $method; |
195 |
print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n"; |
196 |
return 1; |
197 |
} elsif ( $method =~ m{^/out/(.+)} ) { |
198 |
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 ); |
200 |
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 |
} elsif ( $method =~ m{/nodes} ) { |
222 |
to_all { ping => 1 }; |
223 |
my @proc = ( 'MemTotal', 'MemFree', 'Buffers', 'Cached', 'VmSize', 'VmPeak' ); |
224 |
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n", |
225 |
'<table><tr><th>', join('</th><th>', 'node', 'ip', @proc), '</th></tr>'; |
226 |
foreach my $node ( sort { |
227 |
$info->{host}->{$a} cmp $info->{host}->{$b} || # ip |
228 |
$a <=> $b # node |
229 |
} keys %{$info->{proc}} ) { |
230 |
print $send '<tr><td>', join('</td><td align=right>', |
231 |
$node, $info->{host}->{$node}, map { |
232 |
my $kb = $info->{proc}->{$node}->{$_}; |
233 |
$kb =~ s/\s+kb//i; $kb; |
234 |
} @proc ), '</td><tr>'; |
235 |
} |
236 |
print $send '</table>'; |
237 |
return 1; |
238 |
} |
239 |
|
240 |
my $refresh = $param->{refresh}; |
241 |
$refresh = 1 if grep { $info->{pending}->{$_} ne 'forked' } keys %{ $info->{pending} }; |
242 |
# we don't care about refresh is nodes are stuck in forked state! |
243 |
|
244 |
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html" |
245 |
, ( $refresh ? "\r\nRefresh: $refresh" : '' ) |
246 |
, "\r\n\r\n" |
247 |
, ( $refresh ? qq|<span style="color: #888; float:right">$refresh</span>| : '' ) |
248 |
; |
249 |
|
250 |
print $send qq| |
251 |
|
252 |
<style type="text/css"> |
253 |
|
254 |
.running { |
255 |
color: #f00; |
256 |
} |
257 |
|
258 |
|
259 |
.forked { |
260 |
color: #888; |
261 |
} |
262 |
|
263 |
.ping { |
264 |
color: #0f0; |
265 |
} |
266 |
|
267 |
.load { |
268 |
color: #f00; |
269 |
} |
270 |
|
271 |
.view { |
272 |
color: #00f; |
273 |
} |
274 |
|
275 |
.shards { |
276 |
font-family: monospace; |
277 |
// line-height: 0.8em; |
278 |
color: #0f0; |
279 |
} |
280 |
|
281 |
.send { |
282 |
color: #ff0; |
283 |
} |
284 |
|
285 |
.wait { |
286 |
color: #f00; |
287 |
} |
288 |
|
289 |
|
290 |
</style> |
291 |
|
292 |
|; |
293 |
|
294 |
my $out = Sack::Merge->out; |
295 |
if ( my $li = join("\n", map { qq|<li><input type=checkbox name=filter value="$_"><a href="/out/$_" target="$_">$_</a>| } keys %$out ) ) { |
296 |
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|; |
297 |
} |
298 |
|
299 |
print $send qq|<h1>Views</h1><ul>| |
300 |
, join("\n", map { |
301 |
my $view = $_; |
302 |
my $html = ''; |
303 |
if ( my $s = $info->{view}->{$view} ) { |
304 |
my @nodes = sort keys %$s; |
305 |
my $total = 0; |
306 |
$html .= qq|<table><tr><th>node</th><th>rows</th><th>shards</th><th>∑</th><th>merge</th></tr>|; |
307 |
foreach my $node ( @nodes ) { |
308 |
my $h = ''; |
309 |
my $shard = 0; |
310 |
foreach my $path ( sort keys %{ $s->{$node} } ) { |
311 |
my $affected = $s->{$node}->{$path}; |
312 |
my $n = '?'; |
313 |
$n = $1 if $path =~ m{/(\w)\w+/\d+}; |
314 |
my $class = $affected == 0 ? 'style="color: #888"' : ''; |
315 |
$h .= qq|<tt title="$affected $path"$class>$n</tt>|; |
316 |
$shard += $affected; |
317 |
$total += $affected; |
318 |
} |
319 |
my $merge = $info->{merge}->{$view}->{$node} || '?'; |
320 |
$html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td><td>$merge</td></tr>\n|; |
321 |
} |
322 |
|
323 |
$html .= qq|</table>|; |
324 |
}; |
325 |
qq|<li><a href="$view">$view</a>$html| |
326 |
} glob '/srv/Sack/views/*/*.pl' ) |
327 |
, qq|</ul>| |
328 |
; |
329 |
|
330 |
print $send qq|<h1>Nodes</h1>|; |
331 |
foreach my $node ( sort keys %{ $info->{forked} } ) { |
332 |
my $attr = qq| title="$info->{host}->{$node}"|; |
333 |
if ( my $pending = $info->{pending}->{$node} ) { |
334 |
$attr .= qq| class="$pending"|; |
335 |
} |
336 |
print $send qq|<tt$attr>$node</tt>\n|; |
337 |
} |
338 |
print $send qq|<a href=/nodes/>details</a>|; |
339 |
|
340 |
print $send qq|<h1>Shards</h1><ul>|; |
341 |
foreach my $path ( glob '/tmp/sack/*' ) { |
342 |
print $send qq|<li><a href="$path">$path</a><div class="shards">|; |
343 |
foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) { |
344 |
my $i = $info->{shard}->{$s}; |
345 |
print $send qq|<span class="$i" title="$s">$i</span> |; |
346 |
} |
347 |
print $send qq|</div>|; |
348 |
} |
349 |
print $send qq|</ul>|; |
350 |
|
351 |
if ( $param->{info} ) { |
352 |
print $send qq|<a href="?info=0">hide info</a>|; |
353 |
print $send '<pre>', dump($info), '</pre>' |
354 |
} else { |
355 |
print $send qq|<a href="?info=1">show info</a>|; |
356 |
} |
357 |
return 1; |
358 |
} ); |
359 |
} else { |
360 |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
361 |
if ( $@ ) { |
362 |
delete $session->{$sock}; |
363 |
warn "[socket] disconnect: $@\n"; |
364 |
$sel->remove($sock); |
365 |
$sock->close; |
366 |
} else { |
367 |
warn "<<<< ", dump($data), $/ if $debug; |
368 |
|
369 |
if ( my $path = $data->{shard} ) { |
370 |
$info->{shard}->{ $path } = $data->{port}; |
371 |
# FIXME will need push for multiple copies of shards |
372 |
} |
373 |
|
374 |
if ( my $repl = $data->{repl} ) { |
375 |
my $response = { repl_pid => $$ }; |
376 |
if ( $repl =~ m/ping/ ) { |
377 |
to_all { ping => 1 }; |
378 |
} elsif ( $repl =~ m/info/ ) { |
379 |
$response->{info} = $info; |
380 |
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
381 |
load_shard $1; |
382 |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
383 |
run_view $1; |
384 |
} elsif ( $repl =~ m{out} ) { |
385 |
my $out = Sack::Merge->out; |
386 |
warn "out ",dump( $out ); |
387 |
$response->{out} = $out; |
388 |
} elsif ( $repl =~ m{clean} ) { |
389 |
delete $info->{shard}; |
390 |
to_all { clean => 1 }; |
391 |
} elsif ( $repl eq 'exit' ) { |
392 |
to_all { exit => 1 }; |
393 |
sleep 1; |
394 |
exit; |
395 |
} elsif ( $repl eq '.' ) { |
396 |
$response->{'.'} = [ @responses ]; |
397 |
@responses = (); |
398 |
} elsif ( $repl =~ m{(\w+)\s*(.+)?} ) { |
399 |
to_all { $1 => $2 }; |
400 |
} else { |
401 |
$response->{error}->{repl} = $repl; |
402 |
} |
403 |
Storable::store_fd( $response, $sock ); |
404 |
} elsif ( $data->{ping} ) { |
405 |
my $port = $data->{port}; |
406 |
delete( $info->{pending}->{$port} ) if defined $info->{pending}->{$port} && $info->{pending}->{$port} eq 'forked'; |
407 |
$session->{port}->{$port} = $sock; |
408 |
foreach my $name ( keys %{$data->{proc}} ) { |
409 |
if ( $name eq 'loadavg' ) { |
410 |
$info->{proc}->{$port}->{loadavg} = [ split(/\s+/, $data->{proc}->{$name}) ]; |
411 |
} else { |
412 |
foreach my $line ( split(/\n/,$data->{proc}->{$name}) ) { |
413 |
my ($n,$v) = split(/:\s+/,$line); |
414 |
$info->{proc}->{$port}->{$n} = $v; |
415 |
} |
416 |
} |
417 |
} |
418 |
} elsif ( defined $data->{load} && $data->{load} eq 'shard' ) { |
419 |
if ( my $path = shift @shard_load_queue ) { |
420 |
$info->{shard}->{$path} = 'read'; |
421 |
my $shard = Storable::retrieve $path; |
422 |
$info->{shard}->{$path} = 'send'; |
423 |
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
424 |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
425 |
} else { |
426 |
warn "no more shards for [", $data->{port}, "]\n"; |
427 |
delete $info->{pending}->{ $data->{port} }; |
428 |
} |
429 |
} elsif ( exists $data->{out} ) { |
430 |
my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out}; |
431 |
$info->{merge}->{ $data->{view} }->{ $data->{port} } = $added; |
432 |
$info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard}; |
433 |
# refresh shard allocation |
434 |
$info->{shard}->{ $_ } = $data->{port} foreach keys %{ $data->{on_shard} }; |
435 |
delete $info->{pending}->{ $data->{port} }; |
436 |
} elsif ( exists $data->{port} ) { |
437 |
push @responses, $data; |
438 |
warn "# ",dump($data),$/; |
439 |
} else { |
440 |
warn "UNKNOWN ",dump($data); |
441 |
} |
442 |
} |
443 |
} |
444 |
} |
445 |
} |