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