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