/[Sack]/trunk/bin/sack.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bin/sack.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 9 by dpavlin, Mon Sep 21 20:05:41 2009 UTC revision 67 by dpavlin, Fri Sep 25 14:35:54 2009 UTC
# Line 3  Line 3 
3  use warnings;  use warnings;
4  use strict;  use strict;
5    
6    our $VERSION = '0.04';
7    
8  use Time::HiRes qw(time);  use Time::HiRes qw(time);
9  use Data::Dump qw(dump);  use Data::Dump qw(dump);
10  use File::Slurp;  use File::Slurp;
11  use Getopt::Long;  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';  my $path   = '/data/isi/full.txt';
18  my $limit  = 10000;  my $limit  = 5000;
19  my $offset = 0;  my $offset = 0;
20  my @views;  my @views;
21    my $port   = 0; # interactive
22    my @nodes;
23    
24    
25  GetOptions(  GetOptions(
# Line 20  GetOptions( Line 27  GetOptions(
27          'offset=i' => \$offset,          'offset=i' => \$offset,
28          'limit=i'  => \$limit,          'limit=i'  => \$limit,
29          'view=s'   => \@views,          'view=s'   => \@views,
30            'port|listen=i' => \$port,
31            'node|connect=i'   => \@nodes,
32            'debug!'   => \$debug,
33  ) or die $!;  ) or die $!;
34    
35  my $t = time;  my $t = time;
36    
37  use lib '/srv/webpac2/lib/';  
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    
58    use lib "$prefix/srv/Sack/lib/";
59    use Sack::Digest;
60    our $digest = Sack::Digest->new( port => $port, clean => 1 );
61    sub digest { $digest->to_int($_[0]) }
62    
63    use lib "$prefix/srv/webpac2/lib/";
64  use WebPAC::Input::ISI;  use WebPAC::Input::ISI;
65    
66    $WebPAC::Input::ISI::subfields = undef; # disable parsing of subfields
67    
68  my $input = WebPAC::Input::ISI->new(  my $input = WebPAC::Input::ISI->new(
69          path   => $path,          path   => "$prefix/$path",
70          offset => $offset,          offset => $offset,
71          limit  => $limit,          limit  => $limit,
72  );  );
73    
74    our $num_records = $input->size;
75    
76  sub report {  sub report {
77          my $description = shift;          my $description = join(' ',@_);
78          my $dt = time - $t;          my $dt = time - $t;
79          printf "%s in %1.4fs %.2f/s\n", $description, $dt, $input->size / $dt;          printf "%s in %1.4fs %.2f/s\n", $description, $dt, $input->size / $dt;
80            $t = time;
81  }  }
82    
83    
# Line 48  our $out; Line 89  our $out;
89    
90  our $cache;  our $cache;
91    
92  sub run_views {  our $connected;
         @views = sort glob 'views/*.pl' unless @views;  
         warn "# views ", dump @views;  
93    
94          foreach my $view ( @views ) {  sub node_sock {
95            my $node = shift;
96            my $sock = IO::Socket::INET->new(
97                            PeerAddr => '127.0.0.1',
98                            PeerPort => $node,
99                            Proto    => 'tcp',
100            );
101            
102            return $sock if $sock && $sock->connected;
103    
104                  my ( $nr, $package ) = ( $1, $2 )          warn "[$port] can't connect to $node - $!\n"; # FIXME die?
105                          if $view =~ m{/(\d+)\.([^/]+(\.pl)?$)};          return;
106    }
107    
108                  undef $out;  sub send_nodes {
109            my $content = $#_ > 0 ? pop @_ : '';    # no content with just one argument!
110            my $header = defined $content ? length($content) : 0;
111            $header .= ' ' . join(' ', @_) if @_;
112    
113                  next if system("perl -c $view") != 0;          warn "# send_nodes ", dump @nodes;
114    
115                  my $code = read_file $view;          foreach my $node ( @nodes ) {
116                  warn "## CODE\n$code\n## CODE\n";  
117                    my $sock = node_sock($node) || next;
118    
119                    warn "[$port] >>>> [$node] $header\n";
120                    print $sock "$header\n$content" || warn "can't send $header to $node: $!";
121    
122                  my $affected = 0;                  $connected->{$node} = $sock;
123                  $t = time;          }
124    }
125    
126    sub get_node {
127            my $node = shift;
128    
129            my $sock = $connected->{$node};
130            if ( ! $sock || ! $sock->connected ) {
131                    warn "[$port] no connection to $node";
132                    delete $connected->{$node};
133                    return;
134            }
135            chomp( my $size = <$sock> );
136            warn "[$port] <<<< [$node] $size bytes\n" if $debug || $size > 1024;
137            my $data;
138            read $sock, $data, $size;
139            return $data;
140    }
141    
142                  foreach my $pos ( $offset + 1 .. $offset + $input->size ) {  sub send_sock {
143                          my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );          my ( $sock, $data ) = @_;
144                          if ( ! $rec ) {          my $size   = length $data;
145                                  warn "END at $pos";          warn "[$port] >>>> $size bytes\n" if $debug || $size > 1024;
146                                  last;          print $sock "$size\n$data" || warn "can't send $size bytes to ", $sock->peerhost;
147    }
148    
149    sub pull_node_file {
150            my ( $node, $file ) = @_;
151    
152            my $path = "/dev/shm/sack.$node.$file";
153    #       return if -e $path; # FIXME
154    
155            my $sock = node_sock $node || die "not connected to $node";
156    
157            print $sock "0 file $file\n";
158    
159            my $size = <$sock>;
160            chomp($size);
161            warn "[$port] pull_node_file $node $file $size bytes\n";
162    
163            my $block = 4096;
164            my $buff  = ' ' x $block;
165    
166            open(my $fh, '>', $path) || die "can't open $path";
167            while ( read $sock, $buff, $block ) {
168                    print $fh $buff;
169            }
170            close($fh);
171    }
172    
173    sub merge_out {
174            my ( $from_node, $new ) = @_;
175    
176            my $t_merge = time();
177    
178            pull_node_file $from_node => 'nr_md5';
179            pull_node_file $from_node => 'md5';
180    
181            my $remote_digest = Sack::Digest->new( port => $from_node );
182            my ( $local, $remote ) = ( 0, 0 );
183    
184            my $tick = 0;
185            print STDERR "[$port] merge [$from_node]";
186    
187            my $missing;
188    
189            foreach my $k1 ( keys %$new ) {
190    
191                    foreach my $k2 ( keys %{ $new->{$k1} } ) {
192    
193                            my $n   = delete $new->{$k1}->{$k2};
194    
195                            if ( $k1 =~ m{#} ) {
196                                    die "ASSERT $k1 $k2" unless $k2 =~ m{^\d+$};
197    #warn "XXX $k1 $k2";
198                                    my $md5 = $remote_digest->{nr_md5}->[$k2];
199    
200                                    if ( ! $md5 ) {
201                                            $missing->{nr_md5}->{$from_node}++; # FIXME die?
202                                            next;
203                                    }
204    
205                                    if ( my $local_k2 = $digest->{md5_nr}->{$md5} ) {
206                                            $k2 = $local_k2;
207                                            $local++;
208                                    } elsif ( my $full = $remote_digest->{md5}->{$md5} ) {
209                                            $k2 = $digest->to_int( $remote_digest->{md5}->{$md5} );
210                                            $remote++;
211                                    } else {
212                                            $missing->{md5}->{$from_node}++;
213                                    }
214                          }                          }
215    
216                          eval "$code";                          my $ref = ref    $out->{$k1}->{$k2};
217                          if ( $@ ) {  #warn "XXXX $k1 $k2 $ref";
218                                  warn "ERROR [$pos] $@\n";                          if ( ! defined $out->{$k1}->{$k2} ) {
219                                    $out->{$k1}->{$k2} = $n;
220                            } elsif ( $k1 =~ m{\+} ) {
221    #                               warn "## agregate $k1 $k2";
222                                    $out->{$k1}->{$k2} += $n;
223                            } elsif ( $ref  eq 'ARRAY' ) {
224                                    if ( ref $n eq 'ARRAY' ) {
225                                            push @{ $out->{$k1}->{$k2} }, $_ foreach @$n;
226                                    } else {
227                                            push @{ $out->{$k1}->{$k2} }, $n;
228                                    }
229                            } elsif ( $ref eq '' ) {
230                                    $out->{$k1}->{$k2} = [ $out->{$k1}->{$k2}, $n ];
231                          } else {                          } else {
232                                  $affected++;                                  die "can't merge $k2 [$ref] from ",dump($n), " into ", dump($out->{$k1}->{$k2});
233                            }
234    
235                            if ( $tick++ % 1000 == 0 ) {
236                                    print STDERR ".";
237                            } elsif ( $tick % 10000 == 0 ) {
238                                    print STDERR $tick;
239                          }                          }
240                  };                  }
241            }
242    
243            $t_merge = time - $t_merge;
244            printf STDERR "%d in %.4fs %.2f/s local %.1f%% %d/%d\n", $tick, $t_merge, $t_merge / $tick, $local * 100 / $tick, $local, $remote;
245    
246            warn "[$port] missing ", dump $missing if $missing;
247    
248            warn "## merge out ", dump $out if $debug;
249    }
250    
251    sub run_code {
252            my ( $view, $code ) = @_;
253    
254                  report "$affected affected records $view";          warn "\n#### CODE $view START ####\n$code\n#### CODE $view END ####\n" if $debug;
255    
256                  if ( defined $out ) {          send_nodes view => $view => $code;
257                          my $dump = dump $out;  
258                          my $len  = length $dump;          undef $out;
259                          my $path = "out/$nr.$package";  
260                          print "out $view $offset/$limit $len bytes $path"          my $affected = 0;
261                                  , ( $len < 10000 ?  " \$out = $dump" : ' SAVED ONLY' )          $t = time;
262                                  , "\n"  
263                                  ;          foreach my $pos ( $offset + 1 .. $offset + $input->size ) {
264                    my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );
265                    if ( ! $rec ) {
266                            print STDERR "END @ $pos";
267                            last;
268                    }
269    
270                    eval "$code";
271                    if ( $@ ) {
272                            warn "ABORT $pos $@\n";
273                            last;
274                    } else {
275                            $affected++;
276                    }
277    
278                    $pos % 10000 == 0 ? print STDERR $pos :
279                    $pos % 1000  == 0 ? print STDERR "."  : 0 ;
280            };
281    
282            report "\n[$port] RECS $affected $view";
283    
284            warn "WARN no \$out defined!" unless defined $out;
285    
286            $digest->sync;
287    
288            if ( $connected ) {
289                    foreach my $node ( keys %$connected ) {
290                            warn "[$port] get_node $node\n";
291                            my $o = get_node $node;
292                            next unless $o;
293                            my $s = length $o;
294                            $o = thaw $o;
295                            warn "[$port] merge $node $s bytes\n";
296                            merge_out $node => $o;
297                    }
298            }
299    }
300    
301    sub run_views {
302            @views = sort glob 'views/*.pl' unless @views;
303            warn "# views ", dump @views;
304    
305            foreach my $view ( @views ) {
306    
307                    next if system("perl -c $view") != 0;
308    
309                    my $code = read_file $view;
310    
311                    run_code $view => $code;
312    
313                    if ( defined $out ) {
314    
315                            my $path = $view;
316                            $path =~ s{views?/}{out/} || die "no view in $view";
317                            $path =~ s{\.pl}{.storable};
318    
319                          unlink "$path.last" if -e "$path.last";                          unlink "$path.last" if -e "$path.last";
320                          rename $path, "$path.last";                          rename $path, "$path.last";
321                          write_file $path, $dump;  
322                          report "save $path";                          store $out => $path;
323                            report "[$port] SAVE $path $offset-$limit", -s $path, "bytes";
324    
325                            if ( -s $path < 4096 ) {
326                                    print '$out = ', dump $digest->undigest_out($out);
327                            }
328                    }
329    
330            }
331    
332    }
333    
334    
335    sub info_tabs {
336            "$port\t$offset\t$limit\t$num_records\t$path\t"
337            . join("\t", map {
338                    my $b = $_;
339                    $b =~ s{^.+\.$port\.([^/]+)$}{$1};
340                    "$b " . -s $_
341            } glob "/dev/shm/sack.$port.*" );
342    }
343    
344    
345    if ( $port ) {
346            my $sock = IO::Socket::INET->new(
347                    Listen    => SOMAXCONN,
348                    LocalAddr => '127.0.0.1',
349                    LocalPort => $port,
350                    Proto     => 'tcp',
351                    Reuse     => 1,
352            ) or die $!;
353    
354            while (1) {
355    
356                    warn "[$port] READY path: $path offset: $offset limit: $limit #recs: $num_records\n";
357    
358                    my $client = $sock->accept();
359    
360                    warn "[$port] <<<< connect from ", $client->peerhost, $/;
361    
362                    my @header = split(/\s/, <$client>);
363                    warn "[$port] <<<< header ",dump(@header),$/;
364    
365                    my $size = shift @header;
366    
367                    my $content;
368                    read $client, $content, $size;
369    
370                    if ( $header[0] eq 'view' ) {
371                            run_code $header[1] => $content;
372                            send_sock $client => freeze $out;
373                    } elsif ( $header[0] eq 'info' ) {
374                            my $info = info_tabs;
375                            warn "[$port] info $info\n";
376                            send_sock $client => $info;
377                    } elsif ( $header[0] eq 'exit' ) {
378                            warn "[$port] exit";
379                            exit;
380                    } elsif ( $header[0] eq 'file' ) {
381                            $digest->close;
382                            my $path = "/dev/shm/sack.$port.$header[1]";
383                            my $size = -s $path;
384                            warn "[$port] >>>> file $path $size bytes\n";
385                            print $client "$size\n";
386                            my $block = 4096;
387                            my $buff  = ' ' x $block;
388                            open(my $fh, '<', $path) || die "can't open $path";
389                            while ( read $fh, $buff, $block ) {
390                                    print $client $buff;
391                            }
392                            $digest->open;
393                  } else {                  } else {
394                          warn "W: no \$out defined!";                          warn "[$port] UNKNOWN $header[0]";
395                  }                  }
396    
397          }          }
398  }  }
399    
400    sub info {
401            send_nodes 'info' => $2;
402    
403            my @info = (
404                    "port\toffset\tlimit\t#recs\tpath",
405                    "----\t------\t-----\t-----\t----",
406                    info_tabs,
407            );
408    
409            push @info, get_node $_ foreach @nodes;
410    
411            print "[$port] INFO\n"
412                    , join("\n", @info)
413                    , "\n\n" ;
414    
415            return @info;
416    }
417    
418    info;
419  run_views;  run_views;
420    
421  while ( 1 ) {  while ( 1 ) {
422    
423          print "sack> ";          print "sack> ";
424          my $cmd = <STDIN>;          chomp( my $cmd = <STDIN> );
425    
426          if ( $cmd =~ m{(vi|\\e|out)}i ) {          if ( $cmd =~ m{^(h|\?)} ) {
427                  system "vi out/*";                  print << "__HELP__"
428          } else {  Sacks Lorry v$VERSION - path: $path offset: $offset limit: $limit
429    
430            View Run        run views
431            VI \\e Output   show output of last run
432            Info [\$VERSION]        instrospect
433            Quit EXit       shutdown
434    
435    __HELP__
436            } elsif ( $cmd =~ m{^(vi|\\e|o)}i ) {
437                    #system "vi out/*";
438                    $digest->sync;
439                    system "bin/storableedit.pl", (glob('out/*.storable'))[0];
440            } elsif ( $cmd =~ m{^i(?:nfo)?\s?(.+)?$}i ) {
441                    info;
442            } elsif ( $cmd =~ m{^(q|e|x)}i ) {
443                    warn "# exit";
444                    send_nodes 'exit';
445                    exit;
446            } elsif ( $cmd =~ m{^(v|r)}i ) {
447                  run_views;                  run_views;
448            } elsif ( $cmd =~ m{^n(ode)?\s*(\d+)}i ) {
449                    push @nodes, $2;
450                    info;
451            } elsif ( $cmd ) {
452                    warn "UNKNOWN ", dump $cmd;
453          }          }
454    
455  }  }

Legend:
Removed from v.9  
changed lines
  Added in v.67

  ViewVC Help
Powered by ViewVC 1.1.26