1 |
#!/usr/bin/perl |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
our $VERSION = '0.06'; |
7 |
|
8 |
use Time::HiRes qw(time); |
9 |
use Data::Dump qw(dump); |
10 |
use File::Slurp; |
11 |
use Getopt::Long; |
12 |
use IO::Socket::INET; |
13 |
use Storable qw/freeze thaw store/; |
14 |
|
15 |
|
16 |
my $debug = 0; |
17 |
my $path = '/data/isi/full.txt'; |
18 |
my $limit = 5000; |
19 |
my $offset = 0; |
20 |
my @views; |
21 |
my $port = 0; # interactive |
22 |
my @nodes; |
23 |
|
24 |
|
25 |
GetOptions( |
26 |
'path=s' => \$path, |
27 |
'offset=i' => \$offset, |
28 |
'limit=i' => \$limit, |
29 |
'view=s' => \@views, |
30 |
'port|listen=i' => \$port, |
31 |
'node|connect=i' => \@nodes, |
32 |
'debug!' => \$debug, |
33 |
) or die $!; |
34 |
|
35 |
my $t = time; |
36 |
|
37 |
|
38 |
sub send_nodes; |
39 |
|
40 |
our $prefix; |
41 |
sub BEGIN { |
42 |
$prefix = $0; |
43 |
if ( $prefix !~ m{^/} ) { |
44 |
chomp( my $pwd = `pwd` ); |
45 |
$prefix = "$pwd/$prefix"; |
46 |
} |
47 |
$prefix =~ s{^(.*)/srv/Sack/.+$}{$1}; |
48 |
warn "# prefix $prefix"; |
49 |
|
50 |
$SIG{INT} = sub { |
51 |
my $signame = shift; |
52 |
send_nodes 'exit'; |
53 |
#clean if $clean; # FIXME |
54 |
die "SIG$signame"; |
55 |
}; |
56 |
|
57 |
sub port2color { |
58 |
my $port = shift; |
59 |
return "\e[1m0\e[0m" if $port == 0; |
60 |
|
61 |
my $c = ( $port % 6 ) + 31; |
62 |
return "\e[${c}m$port\e[0m"; |
63 |
} |
64 |
|
65 |
$SIG{__WARN__} = sub { |
66 |
return unless @_; |
67 |
my $msg = join('', @_); |
68 |
if ( $msg !~ m{[\n\r]$} ) { |
69 |
my @loc = caller(1); |
70 |
$msg .= " in $loc[1] +$loc[2]\n" if @loc; |
71 |
} |
72 |
$msg =~ s{\[(0|\d\d\d\d)\]}{ '[' . port2color($1) . ']' }eg; |
73 |
print STDERR $msg; |
74 |
return 1; |
75 |
}; |
76 |
|
77 |
} |
78 |
|
79 |
our $digest_module = 'Sack::Digest::' . ( $ENV{SACK_DIGEST} || 'BerkeleyDB' ); |
80 |
use lib "$prefix/srv/Sack/lib/"; |
81 |
my $digest_path = $digest_module; |
82 |
$digest_path =~ s{::}{/}g; |
83 |
require "lib/$digest_path.pm"; |
84 |
|
85 |
our $digest = $digest_module->new( port => $port, clean => 1 ); |
86 |
sub digest { $digest->to_int($_[0]) } |
87 |
|
88 |
warn "[$port] using $digest_module\n"; |
89 |
|
90 |
use lib "$prefix/srv/webpac2/lib/"; |
91 |
use WebPAC::Input::ISI; |
92 |
|
93 |
$WebPAC::Input::ISI::subfields = undef; # disable parsing of subfields |
94 |
|
95 |
my $input = WebPAC::Input::ISI->new( |
96 |
path => "$prefix/$path", |
97 |
offset => $offset, |
98 |
limit => $limit, |
99 |
); |
100 |
|
101 |
our $num_records = $input->size; |
102 |
our @reports; |
103 |
|
104 |
sub report { |
105 |
my $description = join(' ',@_); |
106 |
my $dt = time - $t; |
107 |
my $report = [ $description, $dt, $input->size / $dt ]; |
108 |
warn sprintf "[$port] %s in %1.4fs %.2f/s\n", @$report; |
109 |
push @reports, $report; |
110 |
$t = time; |
111 |
} |
112 |
|
113 |
sub show_report { |
114 |
"\n" . join( "\n", map { sprintf "%8.4fs %10.2f/s %s", $_->[1], $_->[2], $_->[0] } @reports ) . "\n"; |
115 |
} |
116 |
|
117 |
report $input->size , 'records loaded'; |
118 |
|
119 |
mkdir 'out' unless -e 'out'; |
120 |
|
121 |
our $out; |
122 |
|
123 |
our $cache; |
124 |
|
125 |
our $connected; |
126 |
|
127 |
sub node_sock { |
128 |
my $node = shift; |
129 |
my $sock = IO::Socket::INET->new( |
130 |
PeerAddr => '127.0.0.1', |
131 |
PeerPort => $node, |
132 |
Proto => 'tcp', |
133 |
); |
134 |
|
135 |
return $sock if $sock && $sock->connected; |
136 |
|
137 |
warn "[$port] can't connect to $node - $!\n"; # FIXME die? |
138 |
return; |
139 |
} |
140 |
|
141 |
sub send_nodes { |
142 |
my $content = $#_ > 0 ? pop @_ : ''; # no content with just one argument! |
143 |
my $header = defined $content ? length($content) : 0; |
144 |
$header .= ' ' . join(' ', @_) if @_; |
145 |
|
146 |
warn "# send_nodes ", dump(@_), " to ", dump @nodes; |
147 |
|
148 |
foreach my $node ( @nodes ) { |
149 |
|
150 |
my $sock = node_sock($node) || next; |
151 |
|
152 |
warn "[$port] >>>> [$node] $header\n"; |
153 |
print $sock "$header\n$content" || warn "can't send $header to $node: $!"; |
154 |
|
155 |
$connected->{$node} = $sock; |
156 |
} |
157 |
} |
158 |
|
159 |
sub get_node { |
160 |
my $node = shift; |
161 |
|
162 |
my $sock = $connected->{$node}; |
163 |
if ( ! $sock || ! $sock->connected ) { |
164 |
warn "[$port] no connection to $node"; |
165 |
delete $connected->{$node}; |
166 |
return; |
167 |
} |
168 |
chomp( my $size = <$sock> ); |
169 |
warn "[$port] <<<< [$node] $size bytes\n" if $debug || $size > 1024; |
170 |
my $data; |
171 |
read $sock, $data, $size; |
172 |
return $data; |
173 |
} |
174 |
|
175 |
sub send_sock { |
176 |
my ( $sock, $data ) = @_; |
177 |
my $size = length $data; |
178 |
warn "[$port] >>>> $size bytes\n" if $debug || $size > 1024; |
179 |
print $sock "$size\n$data" || warn "can't send $size bytes to ", $sock->peerhost; |
180 |
} |
181 |
|
182 |
sub pull_node_file { |
183 |
my ( $node, $file ) = @_; |
184 |
|
185 |
my $path = "/dev/shm/sack.$node.$file"; |
186 |
return if -e $path; # FIXME |
187 |
|
188 |
my $sock = node_sock $node || die "not connected to $node"; |
189 |
|
190 |
print $sock "0 file $file\n"; |
191 |
|
192 |
my $size = <$sock>; |
193 |
chomp($size); |
194 |
warn "[$port] pull_node_file [$node] $file $size bytes\n"; |
195 |
|
196 |
my $block = 4096; |
197 |
my $buff = ' ' x $block; |
198 |
|
199 |
open(my $fh, '>', $path) || die "can't open $path"; |
200 |
while ( read $sock, $buff, $block ) { |
201 |
print $fh $buff; |
202 |
} |
203 |
close($fh); |
204 |
} |
205 |
|
206 |
my $merge_digest_mapping; |
207 |
|
208 |
sub merge_out { |
209 |
my ( $from_node, $new ) = @_; |
210 |
|
211 |
my $t_merge = time(); |
212 |
|
213 |
pull_node_file $from_node => 'nr_md5'; |
214 |
pull_node_file $from_node => 'md5'; |
215 |
|
216 |
my $remote_digest = Sack::Digest->new( port => $from_node ); |
217 |
my ( $local, $remote ) = ( 0, 0 ); |
218 |
|
219 |
my $tick = 0; |
220 |
warn "[$port] merge [$from_node]"; |
221 |
|
222 |
my $missing; |
223 |
|
224 |
foreach my $k1 ( keys %$new ) { |
225 |
|
226 |
foreach my $k2 ( keys %{ $new->{$k1} } ) { |
227 |
|
228 |
my $n = delete $new->{$k1}->{$k2}; |
229 |
|
230 |
if ( $k1 =~ m{#} ) { |
231 |
die "ASSERT $k1 $k2" unless $k2 =~ m{^\d+$}; |
232 |
#warn "XXX $k1 $k2"; |
233 |
|
234 |
if ( defined $merge_digest_mapping->{$from_node}->[ $k2 ] ) { |
235 |
$k2 = $merge_digest_mapping->{$from_node}->[ $k2 ]; |
236 |
} else { |
237 |
|
238 |
my $md5 = $remote_digest->{nr_md5}->[$k2]; |
239 |
|
240 |
if ( ! $md5 ) { |
241 |
$missing->{nr_md5}->{$from_node}++; # FIXME die? |
242 |
next; |
243 |
} |
244 |
|
245 |
my $local_k2; |
246 |
|
247 |
if ( $local_k2 = $digest->{md5_nr}->{$md5} ) { |
248 |
$local++; |
249 |
} elsif ( my $full = $remote_digest->{md5}->{$md5} ) { |
250 |
$local_k2 = $digest->to_int( $remote_digest->{md5}->{$md5} ); |
251 |
$remote++; |
252 |
} else { |
253 |
$missing->{md5}->{$from_node}++; |
254 |
} |
255 |
|
256 |
$k2 = $merge_digest_mapping->{$from_node}->[ $k2 ] = $local_k2; |
257 |
|
258 |
} |
259 |
} |
260 |
|
261 |
my $ref = ref $out->{$k1}->{$k2}; |
262 |
#warn "XXXX $k1 $k2 $ref"; |
263 |
if ( ! defined $out->{$k1}->{$k2} ) { |
264 |
$out->{$k1}->{$k2} = $n; |
265 |
} elsif ( $k1 =~ m{\+} ) { |
266 |
# warn "## agregate $k1 $k2"; |
267 |
$out->{$k1}->{$k2} += $n; |
268 |
} elsif ( $ref eq 'ARRAY' ) { |
269 |
if ( ref $n eq 'ARRAY' ) { |
270 |
push @{ $out->{$k1}->{$k2} }, $_ foreach @$n; |
271 |
} else { |
272 |
push @{ $out->{$k1}->{$k2} }, $n; |
273 |
} |
274 |
} elsif ( $ref eq '' ) { |
275 |
$out->{$k1}->{$k2} = [ $out->{$k1}->{$k2}, $n ]; |
276 |
} else { |
277 |
die "can't merge $k2 [$ref] from ",dump($n), " into ", dump($out->{$k1}->{$k2}); |
278 |
} |
279 |
|
280 |
if ( $tick++ % 1000 == 0 ) { |
281 |
print STDERR "."; |
282 |
} elsif ( $tick % 10000 == 0 ) { |
283 |
print STDERR $tick; |
284 |
} |
285 |
} |
286 |
} |
287 |
|
288 |
$t_merge = time - $t_merge; |
289 |
my $digests = $local + $remote; |
290 |
warn sprintf "\n[$port] merge %d in %.4fs %.2f/s digests local: %.1f%% %d/%d\n", $tick, $t_merge, $digests / $t_merge, $local * 100 / ( $digests || 1 ), $local, $remote; |
291 |
push @reports, [ "$tick merged [$from_node]", $t_merge, $digests / $t_merge ]; |
292 |
|
293 |
warn "[$port] missing ", dump $missing if $missing; |
294 |
|
295 |
warn "## merge out ", dump $out if $debug; |
296 |
} |
297 |
|
298 |
our $rec; |
299 |
|
300 |
sub run_code { |
301 |
my ( $view, $code ) = @_; |
302 |
|
303 |
warn "\n#### CODE $view START ####\n$code\n#### CODE $view END ####\n" if $debug; |
304 |
|
305 |
send_nodes view => $view => $code; |
306 |
|
307 |
undef $out; |
308 |
|
309 |
my $affected = 0; |
310 |
$t = time; |
311 |
|
312 |
my $coderef = eval "sub { $code }"; |
313 |
if ( $@ ) { |
314 |
warn "ABORT code: $@"; |
315 |
return; |
316 |
} |
317 |
|
318 |
foreach my $pos ( $offset + 1 .. $offset + $input->size ) { |
319 |
$rec = $cache->{$pos} ||= $input->fetch_rec( $pos ); |
320 |
if ( ! $rec ) { |
321 |
print STDERR "END @ $pos"; |
322 |
last; |
323 |
} |
324 |
|
325 |
eval { $coderef->() }; |
326 |
if ( $@ ) { |
327 |
warn "ABORT $pos $@\n"; |
328 |
last; |
329 |
} else { |
330 |
$affected++; |
331 |
} |
332 |
|
333 |
$pos % 10000 == 0 ? print STDERR $pos : |
334 |
$pos % 1000 == 0 ? print STDERR "." : 0 ; |
335 |
}; |
336 |
|
337 |
report "$affected affected $view"; |
338 |
|
339 |
warn "WARN no \$out defined!" unless defined $out; |
340 |
|
341 |
$digest->sync; |
342 |
|
343 |
if ( $connected ) { |
344 |
foreach my $node ( keys %$connected ) { |
345 |
warn "[$port] get_node [$node]\n"; |
346 |
my $o = get_node $node; |
347 |
next unless $o; |
348 |
my $s = length $o; |
349 |
$o = thaw $o; |
350 |
warn "[$port] got $s bytes from [$node]\n"; |
351 |
merge_out $node => $o; |
352 |
} |
353 |
} |
354 |
} |
355 |
|
356 |
sub run_views { |
357 |
@views = sort glob 'views/*.pl' unless @views; |
358 |
warn "# views ", dump @views; |
359 |
|
360 |
foreach my $view ( @views ) { |
361 |
|
362 |
next if system("perl -c $view") != 0; |
363 |
|
364 |
my $code = read_file $view; |
365 |
|
366 |
run_code $view => $code; |
367 |
|
368 |
if ( defined $out ) { |
369 |
|
370 |
my $path = $view; |
371 |
$path =~ s{views?/}{out/} || die "no view in $view"; |
372 |
$path =~ s{\.pl}{.storable}; |
373 |
|
374 |
unlink "$path.last" if -e "$path.last"; |
375 |
rename $path, "$path.last"; |
376 |
|
377 |
store $out => $path; |
378 |
report "save $path", -s $path, "bytes"; |
379 |
|
380 |
if ( -s $path < 4096 ) { |
381 |
print '$out = ', dump $digest->undigest_out($out); |
382 |
} |
383 |
} |
384 |
|
385 |
} |
386 |
|
387 |
} |
388 |
|
389 |
|
390 |
sub info_tabs { |
391 |
my $port_col = port2color($port); |
392 |
"$port_col\t$offset\t$limit\t$num_records\t$path\t" |
393 |
. join("\t", map { |
394 |
my $b = $_; |
395 |
$b =~ s{^.+\.$port\.([^/]+)$}{$1}; |
396 |
"$b " . -s $_ |
397 |
} glob "/dev/shm/sack.$port.*" ); |
398 |
} |
399 |
|
400 |
|
401 |
if ( $port ) { |
402 |
|
403 |
my $pid_path = "/tmp/sack.$port.pid"; |
404 |
if ( -e $pid_path ) { |
405 |
my $pid = read_file $pid_path; |
406 |
kill 9, $pid && warn "[$port] kill old $pid\n"; |
407 |
} |
408 |
write_file $pid_path, $$; |
409 |
|
410 |
|
411 |
my $sock = IO::Socket::INET->new( |
412 |
Listen => SOMAXCONN, |
413 |
LocalAddr => '127.0.0.1', |
414 |
LocalPort => $port, |
415 |
Proto => 'tcp', |
416 |
Reuse => 1, |
417 |
) or die "[$port] die $!"; |
418 |
|
419 |
while (1) { |
420 |
|
421 |
warn "[$port] accept path: $path offset: $offset limit: $limit #recs: $num_records\n"; |
422 |
|
423 |
my $client = $sock->accept(); |
424 |
|
425 |
warn "[$port] <<<< connect from ", $client->peerhost, $/; |
426 |
|
427 |
my @header = split(/\s/, <$client>); |
428 |
warn "[$port] <<<< header ",dump(@header),$/; |
429 |
|
430 |
my $size = shift @header; |
431 |
|
432 |
my $content; |
433 |
read $client, $content, $size; |
434 |
|
435 |
if ( $header[0] eq 'view' ) { |
436 |
run_code $header[1] => $content; |
437 |
send_sock $client => freeze $out; |
438 |
} elsif ( $header[0] eq 'info' ) { |
439 |
my $info = info_tabs; |
440 |
warn "[$port] info $info\n"; |
441 |
$info .= "\n" . show_report if $content =~ m{r}i; |
442 |
send_sock $client => $info; |
443 |
} elsif ( $header[0] eq 'exit' ) { |
444 |
warn "[$port] exit"; |
445 |
exit; |
446 |
} elsif ( $header[0] eq 'file' ) { |
447 |
$digest->close; |
448 |
my $path = "/dev/shm/sack.$port.$header[1]"; |
449 |
my $size = -s $path; |
450 |
warn "[$port] >>>> file $path $size bytes\n"; |
451 |
print $client "$size\n"; |
452 |
my $block = 4096; |
453 |
my $buff = ' ' x $block; |
454 |
open(my $fh, '<', $path) || die "can't open $path"; |
455 |
while ( read $fh, $buff, $block ) { |
456 |
print $client $buff; |
457 |
} |
458 |
$digest->open; |
459 |
} else { |
460 |
warn "[$port] UNKNOWN $header[0]"; |
461 |
} |
462 |
|
463 |
$client->close; |
464 |
|
465 |
} |
466 |
} |
467 |
|
468 |
sub info { |
469 |
my $detail = shift || ''; |
470 |
|
471 |
send_nodes 'info' => $detail; |
472 |
|
473 |
my @info = ( |
474 |
"port\toffset\tlimit\t#recs\tpath", |
475 |
"----\t------\t-----\t-----\t----", |
476 |
info_tabs, |
477 |
); |
478 |
|
479 |
push @info, show_report if $detail =~ m{r}i; |
480 |
|
481 |
push @info, get_node $_ foreach @nodes; |
482 |
|
483 |
print "[$port] INFO", $detail ? " $detail" : '', " \n" |
484 |
, join("\n", @info) |
485 |
, "\n" ; |
486 |
|
487 |
return @info; |
488 |
} |
489 |
|
490 |
info; |
491 |
while ( keys %$connected != @nodes ) { |
492 |
warn "[$port] wait for [", join('] [', grep { ! defined $connected->{$_} } @nodes ), "]\n"; |
493 |
sleep 1; |
494 |
info; |
495 |
} |
496 |
run_views; |
497 |
|
498 |
while ( 1 ) { |
499 |
|
500 |
print "sack> "; |
501 |
chomp( my $cmd = <STDIN> ); |
502 |
|
503 |
if ( $cmd =~ m{^(h|\?)} ) { |
504 |
print << "__HELP__" |
505 |
Sacks Lorry v$VERSION - path: $path offset: $offset limit: $limit |
506 |
|
507 |
View Run run views |
508 |
VI \\e Output show output of last run |
509 |
Info [Report] info with optional report |
510 |
Quit EXit shutdown |
511 |
|
512 |
__HELP__ |
513 |
} elsif ( $cmd =~ m{^(vi|\\e|o)}i ) { |
514 |
#system "vi out/*"; |
515 |
$digest->sync; |
516 |
system "bin/storableedit.pl", (glob('out/*.storable'))[0]; |
517 |
} elsif ( $cmd =~ m{^i(?:nfo)?\s?(\S+)?$}i ) { |
518 |
info $1; |
519 |
} elsif ( $cmd =~ m{^(q|e|x)}i ) { |
520 |
warn "# exit"; |
521 |
send_nodes 'exit'; |
522 |
exit; |
523 |
} elsif ( $cmd =~ m{^(v|r)}i ) { |
524 |
run_views; |
525 |
} elsif ( $cmd =~ m{^n(ode)?\s*(\d+)}i ) { |
526 |
push @nodes, $2; |
527 |
info; |
528 |
} elsif ( $cmd ) { |
529 |
warn "UNKNOWN ", dump $cmd; |
530 |
} |
531 |
|
532 |
} |
533 |
|