1 |
#!/usr/bin/perl |
#!/usr/bin/perl |
2 |
|
|
3 |
|
package Sack::Server; |
4 |
|
|
5 |
use warnings; |
use warnings; |
6 |
use strict; |
use strict; |
7 |
|
|
8 |
|
my $debug = 0; |
9 |
|
|
10 |
use IO::Socket::INET; |
use IO::Socket::INET; |
11 |
use IO::Select; |
use IO::Select; |
12 |
|
|
13 |
use Data::Dump qw(dump); |
use Data::Dump qw(dump); |
14 |
use Storable qw(); |
use Storable qw(); |
15 |
use File::Slurp; |
use File::Slurp; |
16 |
|
use Cwd qw(abs_path); |
17 |
|
|
18 |
my @cloud = qw(localhost tab.lan llin.lan); |
use lib '/srv/Sack/lib'; |
19 |
|
use Sack::Merge; |
20 |
my $cloud_path = $ENV{CLOUD} || '/srv/Sack/etc/lib'; |
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; |
@cloud = read_file $cloud_path; |
28 |
@cloud = map { chomp $_; $_ } @cloud; |
@cloud = map { chomp $_; $_ } @cloud; |
29 |
|
|
30 |
warn "# cloud ",dump( @cloud ); |
warn "# cloud ",dump( @cloud ); |
31 |
|
|
32 |
my $listen_port = 4444; |
my $listen_port = 4444; |
33 |
|
my $http_port = 4480; |
34 |
|
|
35 |
my $node_path = $0; |
my $node_path = abs_path $0; |
36 |
$node_path =~ s{server.pl}{client.pl}; |
$node_path =~ s{Server}{Client}; |
37 |
|
|
38 |
my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die $!; |
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); |
my $sel = IO::Select->new($lsn); |
41 |
|
$sel->add( $www ); |
42 |
|
|
43 |
my $info; |
my $info; |
|
sub info { |
|
|
my $port = shift; |
|
|
push @{ $info->{$port} }, [ @_ ]; |
|
|
} |
|
44 |
|
|
45 |
sub fork_node { |
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 ) { |
71 |
system "echo $node_path | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional --verbose"; |
fork_ssh( $node_port++, $host ); |
|
fork_node( $node_port++, $host ); |
|
72 |
} |
} |
73 |
|
|
74 |
my $session; |
my $session; |
75 |
|
|
76 |
|
sub all_ports { keys %{ $session->{port} } } |
77 |
|
|
78 |
|
sub to_all { |
79 |
|
my $data = shift; |
80 |
|
foreach my $port ( all_ports ) { |
81 |
|
warn ">>>> [$port]\n" if $debug; |
82 |
|
Storable::store_fd( $data, $session->{port}->{$port} ); |
83 |
|
} |
84 |
|
} |
85 |
|
|
86 |
|
our @shard_load_queue; |
87 |
|
sub load_shard { |
88 |
|
my $shard = shift @_ || return; |
89 |
|
|
90 |
|
warn "# load_shard $shard\n"; |
91 |
|
|
92 |
|
$shard .= '/*' if -d $shard; |
93 |
|
|
94 |
|
my @shards = glob $shard; |
95 |
|
|
96 |
|
if ( ! @shards ) { |
97 |
|
warn "no shards for $shard\n"; |
98 |
|
return; |
99 |
|
} |
100 |
|
|
101 |
|
foreach my $s ( @shards ) { |
102 |
|
if ( my $node = $info->{shard}->{$s} ) { |
103 |
|
next if $node =~ m{^\d+$}; # not node number |
104 |
|
} |
105 |
|
$info->{shard}->{$s} = 'wait'; |
106 |
|
push @shard_load_queue, $s; |
107 |
|
warn "queued $s for loading\n"; |
108 |
|
} |
109 |
|
to_all { load => $shard }; |
110 |
|
$info->{pending}->{$_} = 'load' foreach all_ports; |
111 |
|
} |
112 |
|
|
113 |
|
sub run_view { |
114 |
|
my ( $path ) = @_; |
115 |
|
if ( ! -r $path ) { |
116 |
|
warn "ERROR view $path: $!"; |
117 |
|
return; |
118 |
|
} |
119 |
|
my $code = read_file $path; |
120 |
|
Sack::Merge->clean; |
121 |
|
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 }; |
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) { |
133 |
my $new = $lsn->accept; |
my $new = $lsn->accept; |
134 |
$sel->add($new); |
$sel->add($new); |
135 |
$session->{$new} = $new->peerport; |
$session->{peerport}->{ $new->peerport } = $new; |
136 |
warn "[socket] connect\n"; |
warn "[socket] connect\n"; |
137 |
Storable::store_fd( { ping => 1 }, $new ); |
Storable::store_fd( { ping => 1 }, $new ); |
138 |
info 0 => 'ping', $new->peerport; |
} elsif ( $sock == $www ) { |
139 |
|
my $client = $www->accept; |
140 |
|
Sack::Server::HTTP::request( $client, sub { |
141 |
|
my ( $send, $method, $param ) = @_; |
142 |
|
|
143 |
|
if ( $method =~ m{views} ) { |
144 |
|
run_view $method; |
145 |
|
print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n"; |
146 |
|
return 1; |
147 |
|
} elsif ( $method =~ m{^/tmp/sack} ) { |
148 |
|
load_shard $method; |
149 |
|
print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n"; |
150 |
|
return 1; |
151 |
|
} elsif ( $method =~ m{^/out/(.+)} ) { |
152 |
|
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 ); |
154 |
|
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 |
|
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| |
185 |
|
|
186 |
|
<style type="text/css"> |
187 |
|
|
188 |
|
.running { |
189 |
|
color: #f00; |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
|
.forked { |
194 |
|
color: #888; |
195 |
|
} |
196 |
|
|
197 |
|
.ping { |
198 |
|
color: #0f0; |
199 |
|
} |
200 |
|
|
201 |
|
.load { |
202 |
|
color: #f00; |
203 |
|
} |
204 |
|
|
205 |
|
.view { |
206 |
|
color: #00f; |
207 |
|
} |
208 |
|
|
209 |
|
.shards { |
210 |
|
font-family: monospace; |
211 |
|
// line-height: 0.8em; |
212 |
|
color: #0f0; |
213 |
|
} |
214 |
|
|
215 |
|
.send { |
216 |
|
color: #ff0; |
217 |
|
} |
218 |
|
|
219 |
|
.wait { |
220 |
|
color: #f00; |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
|
</style> |
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>| |
234 |
|
, join("\n", map { |
235 |
|
my $view = $_; |
236 |
|
my $html = ''; |
237 |
|
if ( my $s = $info->{view}->{$view} ) { |
238 |
|
my @nodes = sort keys %$s; |
239 |
|
my $total = 0; |
240 |
|
$html .= qq|<table><tr><th>node</th><th>rows</th><th>shards</th><th>∑</th><th>merge</th></tr>|; |
241 |
|
foreach my $node ( @nodes ) { |
242 |
|
my $h = ''; |
243 |
|
my $shard = 0; |
244 |
|
foreach my $path ( sort keys %{ $s->{$node} } ) { |
245 |
|
my $affected = $s->{$node}->{$path}; |
246 |
|
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; |
251 |
|
$total += $affected; |
252 |
|
} |
253 |
|
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>|; |
258 |
|
}; |
259 |
|
qq|<li><a href="$view">$view</a>$html| |
260 |
|
} glob '/srv/Sack/views/*/*.pl' ) |
261 |
|
, qq|</ul>| |
262 |
|
; |
263 |
|
|
264 |
|
print $send qq|<h1>Nodes</h1>|; |
265 |
|
foreach my $node ( keys %{ $info->{forked} } ) { |
266 |
|
my $attr = ''; |
267 |
|
if ( my $pending = $info->{pending}->{$node} ) { |
268 |
|
$attr .= qq| class="$pending"|; |
269 |
|
} |
270 |
|
print $send qq|<tt$attr>$node</tt>\n|; |
271 |
|
} |
272 |
|
|
273 |
|
print $send qq|<h1>Shards</h1><ul>|; |
274 |
|
foreach my $path ( glob '/tmp/sack/*' ) { |
275 |
|
print $send qq|<li><a href="$path">$path</a><div class="shards">|; |
276 |
|
foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) { |
277 |
|
my $i = $info->{shard}->{$s}; |
278 |
|
print $send qq|<span class="$i" title="$s">$i</span> |; |
279 |
|
} |
280 |
|
print $send qq|</div>|; |
281 |
|
} |
282 |
|
print $send qq|</ul>|; |
283 |
|
|
284 |
|
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; |
291 |
|
} ); |
292 |
} else { |
} else { |
293 |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
294 |
if ( $@ ) { |
if ( $@ ) { |
297 |
$sel->remove($sock); |
$sel->remove($sock); |
298 |
$sock->close; |
$sock->close; |
299 |
} else { |
} else { |
300 |
warn "<<<< ", dump($data), $/; |
warn "<<<< ", dump($data), $/ if $debug; |
301 |
if ( $data->{repl} ) { |
|
302 |
my $response = { repl => $$, info => $info }; |
if ( my $path = $data->{shard} ) { |
303 |
|
$info->{shard}->{ $path } = $data->{port}; |
304 |
|
# FIXME will need push for multiple copies of shards |
305 |
|
} |
306 |
|
|
307 |
|
if ( my $repl = $data->{repl} ) { |
308 |
|
my $response = { repl_pid => $$ }; |
309 |
|
if ( $repl =~ m/ping/ ) { |
310 |
|
to_all { ping => 1 }; |
311 |
|
} elsif ( $repl =~ m/info/ ) { |
312 |
|
$response->{info} = $info; |
313 |
|
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
314 |
|
load_shard $1; |
315 |
|
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
316 |
|
run_view $1; |
317 |
|
} elsif ( $repl =~ m{out} ) { |
318 |
|
my $out = Sack::Merge->out; |
319 |
|
warn "out ",dump( $out ); |
320 |
|
$response->{out} = $out; |
321 |
|
} elsif ( $repl =~ m{clean} ) { |
322 |
|
delete $info->{shard}; |
323 |
|
to_all { clean => 1 }; |
324 |
|
} elsif ( $repl eq 'exit' ) { |
325 |
|
to_all { exit => 1 }; |
326 |
|
sleep 1; |
327 |
|
exit; |
328 |
|
} elsif ( $repl eq '.' ) { |
329 |
|
$response->{'.'} = [ @responses ]; |
330 |
|
@responses = (); |
331 |
|
} elsif ( $repl =~ m{(\w+)\s*(.+)?} ) { |
332 |
|
to_all { $1 => $2 }; |
333 |
|
} else { |
334 |
|
$response->{error}->{repl} = $repl; |
335 |
|
} |
336 |
Storable::store_fd( $response, $sock ); |
Storable::store_fd( $response, $sock ); |
337 |
} elsif ( $data->{ping} ) { |
} elsif ( $data->{ping} ) { |
338 |
$info->{_peer_port}->{$sock->peerport} = $data->{port}; |
my $port = $data->{port}; |
339 |
info $data->{port} => 'peer port', $sock->peerport; |
delete( $info->{pending}->{$port} ) if $info->{pending}->{$port} eq 'forked'; |
340 |
|
$session->{port}->{ $data->{port} } = $sock; |
341 |
|
} elsif ( defined $data->{load} && $data->{load} eq 'shard' ) { |
342 |
|
if ( my $path = shift @shard_load_queue ) { |
343 |
|
$info->{shard}->{$path} = 'read'; |
344 |
|
my $shard = Storable::retrieve $path; |
345 |
|
$info->{shard}->{$path} = 'send'; |
346 |
|
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
347 |
|
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
348 |
|
} else { |
349 |
|
warn "no more shards for [", $data->{port}, "]\n"; |
350 |
|
delete $info->{pending}->{ $data->{port} }; |
351 |
|
} |
352 |
|
} elsif ( exists $data->{out} ) { |
353 |
|
my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out}; |
354 |
|
$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 { |
363 |
|
warn "UNKNOWN ",dump($data); |
364 |
} |
} |
365 |
} |
} |
366 |
} |
} |