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 |
sub info { |
45 |
my $port = shift; |
46 |
push @{ $info->{node}->{$port} }, [ @_ ]; |
47 |
} |
48 |
|
49 |
sub fork_ssh { |
50 |
my ( $port, $host ) = @_; |
51 |
|
52 |
if ( my $pid = fork ) { |
53 |
# parent |
54 |
info $port => 'forked', $pid; |
55 |
return $port; |
56 |
|
57 |
} elsif ( ! defined $pid ) { |
58 |
warn "can't fork $host $port"; |
59 |
return; |
60 |
} else { |
61 |
# child |
62 |
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"; |
64 |
exec $cmd; |
65 |
} |
66 |
} |
67 |
|
68 |
my $node_port = 4000; |
69 |
|
70 |
foreach my $host ( @cloud ) { |
71 |
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 ); |
73 |
} |
74 |
|
75 |
my $session; |
76 |
|
77 |
sub to_all { |
78 |
my $data = shift; |
79 |
foreach my $port ( keys %{ $session->{port} } ) { |
80 |
warn ">>>> [$port]\n" if $debug; |
81 |
Storable::store_fd( $data, $session->{port}->{$port} ); |
82 |
} |
83 |
} |
84 |
|
85 |
our @shard_load_queue; |
86 |
sub load_shard { |
87 |
my $shard = shift @_ || return; |
88 |
|
89 |
warn "# load_shard $shard\n"; |
90 |
|
91 |
$shard .= '/*' if -d $shard; |
92 |
|
93 |
my @shards = glob $shard; |
94 |
|
95 |
if ( ! @shards ) { |
96 |
warn "no shards for $shard\n"; |
97 |
return; |
98 |
} |
99 |
|
100 |
foreach my $s ( @shards ) { |
101 |
if ( my $node = $info->{shard}->{$s} ) { |
102 |
next if $node =~ m{^\d+$}; # not node number |
103 |
} |
104 |
$info->{shard}->{$s} = 'wait'; |
105 |
push @shard_load_queue, $s; |
106 |
warn "queued $s for loading\n"; |
107 |
} |
108 |
to_all { load => $shard }; |
109 |
} |
110 |
|
111 |
sub run_view { |
112 |
my ( $path ) = @_; |
113 |
if ( ! -r $path ) { |
114 |
warn "ERROR view $path: $!"; |
115 |
return; |
116 |
} |
117 |
my $code = read_file $path; |
118 |
Sack::Merge->clean; |
119 |
delete( $info->{view} ); |
120 |
delete( $info->{merge} ); |
121 |
delete( $info->{shard} ); |
122 |
to_all { code => $code, view => $path }; |
123 |
}; |
124 |
|
125 |
our @responses; |
126 |
|
127 |
while (1) { |
128 |
for my $sock ($sel->can_read(1)) { |
129 |
if ($sock == $lsn) { |
130 |
my $new = $lsn->accept; |
131 |
$sel->add($new); |
132 |
$session->{peerport}->{ $new->peerport } = $new; |
133 |
warn "[socket] connect\n"; |
134 |
Storable::store_fd( { ping => 1 }, $new ); |
135 |
} elsif ( $sock == $www ) { |
136 |
my $client = $www->accept; |
137 |
Sack::Server::HTTP::request( $client, sub { |
138 |
my ( $send, $method, $param ) = @_; |
139 |
|
140 |
if ( $method =~ m{views} ) { |
141 |
run_view $method; |
142 |
print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n"; |
143 |
return 1; |
144 |
} elsif ( $method =~ m{^/tmp/sack} ) { |
145 |
load_shard $method; |
146 |
print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n"; |
147 |
return 1; |
148 |
} elsif ( $method =~ m{^/out/(.+)} ) { |
149 |
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
150 |
Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param ); |
151 |
return 1; |
152 |
} elsif ( $method =~ m{^/gnuplot} ) { |
153 |
eval { |
154 |
my $path = Sack::Server::Gnuplot::date( Sack::Merge->out, $param ); |
155 |
if ( -e $path ) { |
156 |
print $send "HTTP/1.0 200 OK\r\nContent-Type: image/png\r\n\r\n"; |
157 |
open(my $fh, '<', $path) || die $path; |
158 |
my $b; |
159 |
while ( read($fh, $b, 4096) ) { |
160 |
print $send $b; |
161 |
} |
162 |
return 1; |
163 |
} else { |
164 |
print $send "HTTP/1.0 404 no graph\r\n\r\n"; |
165 |
return 1; |
166 |
} |
167 |
}; |
168 |
warn "ERROR: $@" if $@; |
169 |
} |
170 |
|
171 |
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
172 |
|
173 |
print $send qq| |
174 |
|
175 |
<style type="text/css"> |
176 |
|
177 |
.running { |
178 |
color: #f00; |
179 |
} |
180 |
|
181 |
|
182 |
.forked { |
183 |
color: #f00; |
184 |
} |
185 |
|
186 |
.ping { |
187 |
color: #0f0; |
188 |
} |
189 |
|
190 |
.shards { |
191 |
font-family: monospace; |
192 |
// line-height: 0.8em; |
193 |
color: #0f0; |
194 |
} |
195 |
|
196 |
.send { |
197 |
color: #ff0; |
198 |
} |
199 |
|
200 |
.wait { |
201 |
color: #f00; |
202 |
} |
203 |
|
204 |
|
205 |
</style> |
206 |
|
207 |
|; |
208 |
|
209 |
print $send qq|<h1>Views</h1><ul>| |
210 |
, join("\n", map { |
211 |
my $view = $_; |
212 |
my $html = ''; |
213 |
if ( my $s = $info->{view}->{$view} ) { |
214 |
my @nodes = sort keys %$s; |
215 |
my $total = 0; |
216 |
$html .= qq|<table><tr><th>node</th><th>rows</th><th>shards</th><th>∑</th><th>merge</th></tr>|; |
217 |
foreach my $node ( @nodes ) { |
218 |
my $h = ''; |
219 |
my $shard = 0; |
220 |
foreach my $path ( sort keys %{ $s->{$node} } ) { |
221 |
my $affected = $s->{$node}->{$path}; |
222 |
my $n = '?'; |
223 |
$n = $1 if $path =~ m{/(\w)\w+/\d+}; |
224 |
my $class = $affected == 0 ? 'style="color: #888"' : ''; |
225 |
$h .= qq|<tt title="$affected $path"$class>$n</tt>|; |
226 |
$shard += $affected; |
227 |
$total += $affected; |
228 |
} |
229 |
my $merge = $info->{merge}->{$view}->{$node} || '?'; |
230 |
$html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td><td>$merge</td></tr>\n|; |
231 |
} |
232 |
|
233 |
$html .= qq|</table>|; |
234 |
}; |
235 |
qq|<li><a href="$view">$view</a>$html| |
236 |
} glob '/srv/Sack/views/*/*.pl' ) |
237 |
, qq|</ul>| |
238 |
; |
239 |
|
240 |
my $out = Sack::Merge->out; |
241 |
if ( my $li = join("\n", map { qq|<li><input type=checkbox name=filter value="$_"><a href="/out/$_" target="$_">$_</a>| } keys %$out ) ) { |
242 |
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|; |
243 |
} |
244 |
|
245 |
print $send qq|<h1>Nodes</h1> |
246 |
|, join("\n", map { |
247 |
my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} }); |
248 |
qq|<tt class="$class">$_</tt>|; |
249 |
} sort keys %{ $info->{node} } ); |
250 |
|
251 |
print $send qq|<h1>Shards</h1><ul>|; |
252 |
foreach my $path ( glob '/tmp/sack/*' ) { |
253 |
print $send qq|<li><a href="$path">$path</a><div class="shards">|; |
254 |
foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) { |
255 |
my $i = $info->{shard}->{$s}; |
256 |
print $send qq|<span class="$i" title="$s">$i</span> |; |
257 |
} |
258 |
print $send qq|</div>|; |
259 |
} |
260 |
print $send qq|</ul>|; |
261 |
|
262 |
if ( $param->{info} ) { |
263 |
print $send qq|<a href="?info=0">hide info</a>|; |
264 |
print $send '<pre>', dump($info), '</pre>' |
265 |
} else { |
266 |
print $send qq|<a href="?info=1">show info</a>|; |
267 |
} |
268 |
return 1; |
269 |
} ); |
270 |
} else { |
271 |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
272 |
if ( $@ ) { |
273 |
delete $session->{$sock}; |
274 |
warn "[socket] disconnect: $@\n"; |
275 |
$sel->remove($sock); |
276 |
$sock->close; |
277 |
} else { |
278 |
warn "<<<< ", dump($data), $/ if $debug; |
279 |
|
280 |
if ( my $path = $data->{shard} ) { |
281 |
$info->{shard}->{ $path } = $data->{port}; |
282 |
# FIXME will need push for multiple copies of shards |
283 |
} |
284 |
|
285 |
if ( my $repl = $data->{repl} ) { |
286 |
my $response = { repl_pid => $$ }; |
287 |
if ( $repl =~ m/ping/ ) { |
288 |
to_all { ping => 1 }; |
289 |
} elsif ( $repl =~ m/info/ ) { |
290 |
$response->{info} = $info; |
291 |
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
292 |
load_shard $1; |
293 |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
294 |
run_view $1; |
295 |
} elsif ( $repl =~ m{out} ) { |
296 |
my $out = Sack::Merge->out; |
297 |
warn "out ",dump( $out ); |
298 |
$response->{out} = $out; |
299 |
} elsif ( $repl =~ m{clean} ) { |
300 |
delete $info->{shard}; |
301 |
to_all { clean => 1 }; |
302 |
} elsif ( $repl eq 'exit' ) { |
303 |
to_all { exit => 1 }; |
304 |
sleep 1; |
305 |
exit; |
306 |
} elsif ( $repl eq '.' ) { |
307 |
$response->{'.'} = [ @responses ]; |
308 |
@responses = (); |
309 |
} elsif ( $repl =~ m{(\w+)\s*(.+)?} ) { |
310 |
to_all { $1 => $2 }; |
311 |
} else { |
312 |
$response->{error}->{repl} = $repl; |
313 |
} |
314 |
Storable::store_fd( $response, $sock ); |
315 |
} elsif ( $data->{ping} ) { |
316 |
my $port = $data->{port}; |
317 |
info $port => 'ping', $port; |
318 |
$session->{port}->{ $data->{port} } = $sock; |
319 |
} elsif ( defined $data->{load} && $data->{load} eq 'shard' ) { |
320 |
if ( my $path = shift @shard_load_queue ) { |
321 |
$info->{shard}->{$path} = 'read'; |
322 |
my $shard = Storable::retrieve $path; |
323 |
$info->{shard}->{$path} = 'send'; |
324 |
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
325 |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
326 |
} else { |
327 |
warn "no more shards for [", $data->{port}, "]\n"; |
328 |
} |
329 |
} elsif ( exists $data->{out} ) { |
330 |
my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out}; |
331 |
$info->{merge}->{ $data->{view} }->{ $data->{port} } = $added; |
332 |
$info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard}; |
333 |
# refresh shard allocation |
334 |
$info->{shard}->{ $_ } = $data->{port} foreach keys %{ $data->{on_shard} }; |
335 |
} elsif ( exists $data->{port} ) { |
336 |
push @responses, $data; |
337 |
warn "# ",dump($data),$/; |
338 |
} else { |
339 |
warn "UNKNOWN ",dump($data); |
340 |
} |
341 |
} |
342 |
} |
343 |
} |
344 |
} |