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 |
|
|
17 |
|
|
18 |
use lib '/srv/Sack/lib'; |
use lib '/srv/Sack/lib'; |
19 |
use Sack::Merge; |
use Sack::Merge; |
20 |
|
use Sack::Server::HTTP; |
21 |
|
use Sack::Server::HTML; |
22 |
|
|
23 |
my @cloud; |
my @cloud; |
24 |
my $cloud_path = $ENV{CLOUD} || die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n"; |
my $cloud_path = $ENV{CLOUD} || "etc/cloud"; |
25 |
|
die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n" unless -e $cloud_path; |
26 |
@cloud = read_file $cloud_path; |
@cloud = read_file $cloud_path; |
27 |
@cloud = map { chomp $_; $_ } @cloud; |
@cloud = map { chomp $_; $_ } @cloud; |
28 |
|
|
29 |
warn "# cloud ",dump( @cloud ); |
warn "# cloud ",dump( @cloud ); |
30 |
|
|
31 |
my $listen_port = 4444; |
my $listen_port = 4444; |
32 |
|
my $http_port = 4480; |
33 |
|
|
34 |
my $node_path = abs_path $0; |
my $node_path = abs_path $0; |
35 |
$node_path =~ s{Server}{Client}; |
$node_path =~ s{Server}{Client}; |
36 |
|
|
37 |
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 $!"; |
38 |
|
my $www = IO::Socket::INET->new(Listen => 1, LocalPort => $http_port, Reuse => 1) or die "$http_port $!"; |
39 |
my $sel = IO::Select->new($lsn); |
my $sel = IO::Select->new($lsn); |
40 |
|
$sel->add( $www ); |
41 |
|
|
42 |
my $info; |
my $info; |
43 |
sub info { |
sub info { |
44 |
my $port = shift; |
my $port = shift; |
45 |
push @{ $info->{$port} }, [ @_ ]; |
push @{ $info->{node}->{$port} }, [ @_ ]; |
46 |
} |
} |
47 |
|
|
48 |
sub fork_ssh { |
sub fork_ssh { |
76 |
sub to_all { |
sub to_all { |
77 |
my $data = shift; |
my $data = shift; |
78 |
foreach my $port ( keys %{ $session->{port} } ) { |
foreach my $port ( keys %{ $session->{port} } ) { |
79 |
warn ">>>> [$port]\n"; |
warn ">>>> [$port]\n" if $debug; |
80 |
Storable::store_fd( $data, $session->{port}->{$port} ); |
Storable::store_fd( $data, $session->{port}->{$port} ); |
81 |
} |
} |
82 |
} |
} |
83 |
|
|
84 |
my @shard_paths; |
our @shard_load_queue; |
85 |
|
sub load_shard { |
86 |
|
my $shard = shift @_ || return; |
87 |
|
|
88 |
|
warn "# load_shard $shard\n"; |
89 |
|
|
90 |
|
my @shards = glob "$shard/*"; |
91 |
|
|
92 |
|
if ( ! @shards ) { |
93 |
|
warn "no shards for $shard\n"; |
94 |
|
return; |
95 |
|
} |
96 |
|
|
97 |
|
$info->{shard}->{$_} = 'wait' foreach @shards; |
98 |
|
warn "loading shard $shard from ", dump( @shards ); |
99 |
|
|
100 |
|
push @shard_load_queue, @shards; |
101 |
|
to_all { load => $shard }; |
102 |
|
} |
103 |
|
|
104 |
|
sub run_view { |
105 |
|
my ( $path ) = @_; |
106 |
|
if ( ! -r $path ) { |
107 |
|
warn "ERROR view $path: $!"; |
108 |
|
return; |
109 |
|
} |
110 |
|
my $code = read_file $path; |
111 |
|
Sack::Merge->clean; |
112 |
|
delete( $info->{view} ); |
113 |
|
delete( $info->{merge} ); |
114 |
|
to_all { code => $code, view => $path }; |
115 |
|
}; |
116 |
|
|
117 |
while (1) { |
while (1) { |
118 |
for my $sock ($sel->can_read(1)) { |
for my $sock ($sel->can_read(1)) { |
122 |
$session->{peerport}->{ $new->peerport } = $new; |
$session->{peerport}->{ $new->peerport } = $new; |
123 |
warn "[socket] connect\n"; |
warn "[socket] connect\n"; |
124 |
Storable::store_fd( { ping => 1 }, $new ); |
Storable::store_fd( { ping => 1 }, $new ); |
125 |
info 0 => 'ping', $new->peerport; |
} elsif ( $sock == $www ) { |
126 |
|
my $client = $www->accept; |
127 |
|
Sack::Server::HTTP::request( $client, sub { |
128 |
|
my ( $send, $method, $param ) = @_; |
129 |
|
|
130 |
|
if ( $method =~ m{views} ) { |
131 |
|
run_view $method; |
132 |
|
print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n"; |
133 |
|
return 1; |
134 |
|
} elsif ( $method =~ m{^/tmp/sack} ) { |
135 |
|
load_shard $method; |
136 |
|
print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n"; |
137 |
|
return 1; |
138 |
|
} elsif ( $method =~ m{^/out/(.+)} ) { |
139 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
140 |
|
Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param ); |
141 |
|
return 1; |
142 |
|
} |
143 |
|
|
144 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
145 |
|
|
146 |
|
print $send qq| |
147 |
|
|
148 |
|
<style type="text/css"> |
149 |
|
|
150 |
|
.running { |
151 |
|
color: #f00; |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
.forked { |
156 |
|
color: #f00; |
157 |
|
} |
158 |
|
|
159 |
|
.ping { |
160 |
|
color: #0f0; |
161 |
|
} |
162 |
|
|
163 |
|
.shards { |
164 |
|
font-family: monospace; |
165 |
|
line-height: 0.8em; |
166 |
|
color: #0f0; |
167 |
|
} |
168 |
|
|
169 |
|
.send { |
170 |
|
color: #ff0; |
171 |
|
} |
172 |
|
|
173 |
|
.wait { |
174 |
|
color: #f00; |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
|
</style> |
179 |
|
|
180 |
|
|; |
181 |
|
|
182 |
|
print $send qq|<h1>Views</h1><ul>| |
183 |
|
, join("\n", map { |
184 |
|
my $view = $_; |
185 |
|
my $html = ''; |
186 |
|
if ( my $s = $info->{view}->{$view} ) { |
187 |
|
my @nodes = sort keys %$s; |
188 |
|
my $total = 0; |
189 |
|
my $total_merged = 0; |
190 |
|
$html .= qq|<table><tr><th>node</th><th>rows</th><th>shards</th><th>∑</th><th>merge</th><th>∑</th></tr>|; |
191 |
|
foreach my $node ( @nodes ) { |
192 |
|
my $h = ''; |
193 |
|
my $shard = 0; |
194 |
|
foreach my $path ( sort keys %{ $s->{$node} } ) { |
195 |
|
my $affected = $s->{$node}->{$path}; |
196 |
|
my $n = '?'; |
197 |
|
$n = $1 if $path =~ m{/(\w)\w+/\d+}; |
198 |
|
my $class = $affected == 0 ? 'style="color: #888"' : ''; |
199 |
|
$h .= qq|<tt title="$affected $path"$class>$n</tt>|; |
200 |
|
$shard += $affected; |
201 |
|
$total += $affected; |
202 |
|
} |
203 |
|
my $merge = $info->{merge}->{$view}->{$node}; |
204 |
|
$total_merged += $merge; |
205 |
|
$html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td><td>$merge</td><td>$total_merged</td></tr>\n|; |
206 |
|
} |
207 |
|
|
208 |
|
$html .= qq|</table>|; |
209 |
|
}; |
210 |
|
qq|<li><a href="$view">$view</a>$html| |
211 |
|
} glob '/srv/Sack/views/*/*.pl' ) |
212 |
|
, qq|</ul>| |
213 |
|
; |
214 |
|
|
215 |
|
my $out = Sack::Merge->out; |
216 |
|
print $send qq|<h1>Results</h1><ul>| |
217 |
|
, join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out ) |
218 |
|
, qq|</ul>| |
219 |
|
; |
220 |
|
|
221 |
|
print $send qq|<h1>Nodes</h1> |
222 |
|
|, join("\n", map { |
223 |
|
my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} }); |
224 |
|
qq|<span class="$class">$_</span>|; |
225 |
|
} sort keys %{ $info->{node} } ); |
226 |
|
|
227 |
|
print $send qq|<h1>Data</h1><ul>|; |
228 |
|
foreach my $path ( glob '/tmp/sack/*' ) { |
229 |
|
print $send qq|<li><a href="$path">$path</a><div class="shards">|; |
230 |
|
foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) { |
231 |
|
my $i = $info->{shard}->{$s}; |
232 |
|
print $send qq|<span class=$i>$i</span> |; |
233 |
|
} |
234 |
|
print $send qq|</div>|; |
235 |
|
} |
236 |
|
print $send qq|</ul>|; |
237 |
|
|
238 |
|
if ( $param->{info} ) { |
239 |
|
print $send qq|<a href="?info=0">hide info</a>|; |
240 |
|
print $send '<pre>', dump($info), '</pre>' |
241 |
|
} else { |
242 |
|
print $send qq|<a href="?info=1">show info</a>|; |
243 |
|
} |
244 |
|
return 1; |
245 |
|
} ); |
246 |
} else { |
} else { |
247 |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
248 |
if ( $@ ) { |
if ( $@ ) { |
251 |
$sel->remove($sock); |
$sel->remove($sock); |
252 |
$sock->close; |
$sock->close; |
253 |
} else { |
} else { |
254 |
warn "<<<< ", dump($data), $/; |
warn "<<<< ", dump($data), $/ if $debug; |
255 |
|
|
256 |
if ( my $path = $data->{shard} ) { |
if ( my $path = $data->{shard} ) { |
257 |
push @{ $info->{shard}->{ $data->{port} } }, $path; |
$info->{shard}->{ $path } = $data->{port}; |
258 |
|
# FIXME will need push for multiple copies of shards |
259 |
} |
} |
260 |
|
|
261 |
if ( my $repl = $data->{repl} ) { |
if ( my $repl = $data->{repl} ) { |
265 |
} elsif ( $repl =~ m/info/ ) { |
} elsif ( $repl =~ m/info/ ) { |
266 |
$response->{info} = $info; |
$response->{info} = $info; |
267 |
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
268 |
my $name = $1 || 'shard'; |
load_shard $1; |
|
@shard_paths = glob "/tmp/sack/$name/*"; |
|
|
warn "loading shard $name from ", dump( @shard_paths ); |
|
|
to_all { load => $name }; |
|
269 |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
270 |
my $path = $1 || '/srv/Sack/views/00.demo.pl'; |
run_view $1; |
|
my $code = read_file $path; |
|
|
Sack::Merge->clean; |
|
|
to_all { view => $code, path => $path }; |
|
|
$response->{view}->{$path}->{running}; |
|
271 |
} elsif ( $repl =~ m{debug\s*(.+)?} ) { |
} elsif ( $repl =~ m{debug\s*(.+)?} ) { |
272 |
to_all { debug => $1 }; |
to_all { debug => $1 }; |
273 |
} elsif ( $repl =~ m{out} ) { |
} elsif ( $repl =~ m{out} ) { |
277 |
} elsif ( $repl =~ m{clean} ) { |
} elsif ( $repl =~ m{clean} ) { |
278 |
delete $info->{shard}; |
delete $info->{shard}; |
279 |
to_all { clean => 1 }; |
to_all { clean => 1 }; |
280 |
|
} elsif ( $repl eq 'exit' ) { |
281 |
|
to_all { exit => 1 }; |
282 |
|
sleep 1; |
283 |
|
exit; |
284 |
} else { |
} else { |
285 |
$response->{error}->{unknown} = $data; |
$response->{error}->{unknown} = $data; |
286 |
} |
} |
289 |
my $port = $data->{port}; |
my $port = $data->{port}; |
290 |
info $port => 'ping', $port; |
info $port => 'ping', $port; |
291 |
$session->{port}->{ $data->{port} } = $sock; |
$session->{port}->{ $data->{port} } = $sock; |
292 |
} elsif ( $data->{load} eq 'shard' ) { |
} elsif ( defined $data->{load} && $data->{load} eq 'shard' ) { |
293 |
if ( my $path = shift @shard_paths ) { |
if ( my $path = shift @shard_load_queue ) { |
294 |
warn "retrieve $path ", -s $path; |
$info->{shard}->{$path} = 'read'; |
295 |
my $shard = Storable::retrieve $path; |
my $shard = Storable::retrieve $path; |
296 |
|
$info->{shard}->{$path} = 'send'; |
297 |
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
298 |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
299 |
} else { |
} else { |
300 |
warn "no more shards for [", $data->{port}, "]\n"; |
warn "no more shards for [", $data->{port}, "]\n"; |
301 |
} |
} |
302 |
} elsif ( defined $data->{out} ) { |
} elsif ( exists $data->{out} ) { |
303 |
Sack::Merge->add( $data->{out} ); |
my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out}; |
304 |
|
$info->{merge}->{ $data->{view} }->{ $data->{port} } = $added; |
305 |
|
$info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard}; |
306 |
} else { |
} else { |
307 |
warn "UNKNOWN ",dump($data); |
warn "UNKNOWN ",dump($data); |
308 |
} |
} |