/[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 20 by dpavlin, Tue Sep 22 16:02:49 2009 UTC revision 59 by dpavlin, Fri Sep 25 12:24:42 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;  use IO::Socket::INET;
13  use Storable qw/freeze thaw/;  use Storable qw/freeze thaw store/;
14    
15    
16  my $debug  = 0;  my $debug  = 0;
# Line 16  my $path   = '/data/isi/full.txt'; Line 18  my $path   = '/data/isi/full.txt';
18  my $limit  = 5000;  my $limit  = 5000;
19  my $offset = 0;  my $offset = 0;
20  my @views;  my @views;
21  my $listen;  my $port   = 0; # interactive
22  my @nodes;  my @nodes;
23    
24    
# Line 25  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          'listen|port=i' => \$listen,          'listen|port=i' => \$port,
31          'connect=s'   => \@nodes,          'connect=s'   => \@nodes,
32          'debug!'   => \$debug,          'debug!'   => \$debug,
33  ) or die $!;  ) or die $!;
# Line 33  GetOptions( Line 35  GetOptions(
35  my $t = time;  my $t = time;
36    
37    
38    sub send_nodes;
39    
40  our $prefix;  our $prefix;
41  BEGIN {  sub BEGIN {
42          $prefix = $0;          $prefix = $0;
43          if ( $prefix =~ s{^./}{} ) {          if ( $prefix !~ m{^/} ) {
44                  chomp( my $pwd = `pwd` );                  chomp( my $pwd = `pwd` );
45                  $prefix = "$pwd/$prefix";                  $prefix = "$pwd/$prefix";
46          }          }
47          $prefix =~ s{^(.*)/srv/Sack/bin.+$}{$1};          $prefix =~ s{^(.*)/srv/Sack/.+$}{$1};
48          warn "# prefix $prefix";          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/";  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   => "$prefix/$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;          $t = time;
# Line 74  our $connected; Line 93  our $connected;
93    
94  sub send_nodes {  sub send_nodes {
95          my $content = $#_ > 0 ? pop @_ : '';    # no content with just one argument!          my $content = $#_ > 0 ? pop @_ : '';    # no content with just one argument!
96          my $header = length($content);          my $header = defined $content ? length($content) : 0;
97          $header .= ' ' . join(' ', @_) if @_;          $header .= ' ' . join(' ', @_) if @_;
98    
99          foreach my $node ( @nodes ) {          foreach my $node ( @nodes ) {
100    
101                  my $sock = IO::Socket::INET->new(                  my $sock = IO::Socket::INET->new(
102                          PeerAddr => $node,                          PeerAddr => '127.0.0.1',
103                            PeerPort => $node,
104                          Proto    => 'tcp',                          Proto    => 'tcp',
105                  );                  );
106    
# Line 89  sub send_nodes { Line 109  sub send_nodes {
109                          next;                          next;
110                  }                  }
111    
112                  print ">>>> $node $header\n";                  warn "[$port] >>>> $node $header\n";
113                  print $sock "$header\n$content" || warn "can't send $header to $node: $!";                  print $sock "$header\n$content" || warn "can't send $header to $node: $!";
114    
115                  $connected->{$node} = $sock;                  $connected->{$node} = $sock;
# Line 101  sub get_node { Line 121  sub get_node {
121    
122          my $sock = $connected->{$node};          my $sock = $connected->{$node};
123          if ( ! $sock ) {          if ( ! $sock ) {
124                  warn "ERROR: lost connection to $node";                  warn "[$port] ERROR lost connection to $node";
125                  delete $connected->{$node};                  delete $connected->{$node};
126                  return;                  return;
127          }          }
128          chomp( my $size = <$sock> );          chomp( my $size = <$sock> );
129          warn "<<<< $node $size bytes\n";          warn "[$port] <<<< $node $size bytes\n" if $debug || $size > 1024;
130          my $data;          my $data;
131          read $sock, $data, $size;          read $sock, $data, $size;
132          return $data;          return $data;
# Line 115  sub get_node { Line 135  sub get_node {
135  sub send_sock {  sub send_sock {
136          my ( $sock, $data ) = @_;          my ( $sock, $data ) = @_;
137          my $size   = length $data;          my $size   = length $data;
138          warn ">>>> ", $sock->peerhost, " $size bytes";          warn "[$port] >>>> $size bytes\n" if $debug || $size > 1024;
139          print $sock "$size\n$data" || warn "can't send $size bytes to ", $sock->peerhost;          print $sock "$size\n$data" || warn "can't send $size bytes to ", $sock->peerhost;
140  }  }
141    
142  sub merge_out {  sub merge_out {
143          my $new = shift;          my ( $from_node, $new ) = @_;
144    
145            my $remote_digest = Sack::Digest->new( port => $from_node );
146            my ( $local, $remote ) = ( 0, 0 );
147    
148            my $tick = 0;
149            print STDERR "[$port] merge $from_node";
150    
151          foreach my $k1 ( keys %$new ) {          foreach my $k1 ( keys %$new ) {
152    
153                  foreach my $k2 ( keys %{ $new->{$k1} } ) {                  foreach my $k2 ( keys %{ $new->{$k1} } ) {
154    
155                          my $n   =     $new->{$k1}->{$k2};                          my $n   = delete $new->{$k1}->{$k2};
                         my $ref = ref $out->{$k1}->{$k2};  
156    
157                            if ( $k1 =~ m{#} ) {
158                                    die "ASSERT $k1 $k2" unless $k2 =~ m{^\d+$};
159    #warn "XXX $k1 $k2";
160                                    my $md5 = $remote_digest->{nr_md5}->[$k2] || warn "[$port] no2md5 $n not found in $from_node\n";
161                                    if ( my $local_k2 = $digest->{md5_nr}->{$md5} ) {
162                                            $k2 = $local_k2;
163                                            $local++;
164                                    } else {
165                                            $k2 = $digest->to_int( $remote_digest->{md5}->{$md5} );
166                                            $remote++;
167                                    }
168                            }
169    
170                            my $ref = ref    $out->{$k1}->{$k2};
171    #warn "XXXX $k1 $k2 $ref";
172                          if ( ! defined $out->{$k1}->{$k2} ) {                          if ( ! defined $out->{$k1}->{$k2} ) {
173                                  $out->{$k1}->{$k2} = $n;                                  $out->{$k1}->{$k2} = $n;
174                          } elsif ( $k1 =~ m{\+} ) {                          } elsif ( $k1 =~ m{\+} ) {
175  #                               warn "## agregate $k1 $k2";  #                               warn "## agregate $k1 $k2";
176                                  $out->{$k1}->{$k2} += $n;                                  $out->{$k1}->{$k2} += $n;
177                          } elsif ( $ref eq 'ARRAY' ) {                          } elsif ( $ref  eq 'ARRAY' ) {
178                                  push @{ $out->{$k1}->{$k2} }, $n;                                  if ( ref $n eq 'ARRAY' ) {
179                                            push @{ $out->{$k1}->{$k2} }, $_ foreach @$n;
180                                    } else {
181                                            push @{ $out->{$k1}->{$k2} }, $n;
182                                    }
183                          } elsif ( $ref eq '' ) {                          } elsif ( $ref eq '' ) {
184                                  $out->{$k1}->{$k2} = [ $out->{$k1}->{$k2}, $n ];                                  $out->{$k1}->{$k2} = [ $out->{$k1}->{$k2}, $n ];
185                          } else {                          } else {
186                                  die "can't merge $k2 [$ref] from ",dump($n), " into ", dump($out->{$k1}->{$k2});                                  die "can't merge $k2 [$ref] from ",dump($n), " into ", dump($out->{$k1}->{$k2});
187                          }                          }
188    
189                            if ( $tick++ % 1000 == 0 ) {
190                                    print STDERR ".";
191                            } elsif ( $tick % 10000 == 0 ) {
192                                    print STDERR $tick;
193                            }
194                  }                  }
195          }          }
196    
197            print STDERR "$tick\n";
198    
199            warn "[$port] merge local $local remote $remote from $from_node\n";
200          warn "## merge out ", dump $out if $debug;          warn "## merge out ", dump $out if $debug;
201  }  }
202    
203  sub run_code {  sub run_code {
204          my ( $view, $code ) = @_;          my ( $view, $code ) = @_;
205    
206          warn "\n#### CODE $view START ####\n$code\n#### CODE $view END ####\n";          warn "\n#### CODE $view START ####\n$code\n#### CODE $view END ####\n" if $debug;
207    
208          send_nodes view => $view => $code;          send_nodes view => $view => $code;
209    
# Line 162  sub run_code { Line 215  sub run_code {
215          foreach my $pos ( $offset + 1 .. $offset + $input->size ) {          foreach my $pos ( $offset + 1 .. $offset + $input->size ) {
216                  my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );                  my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );
217                  if ( ! $rec ) {                  if ( ! $rec ) {
218                          warn "END at $pos";                          print STDERR "END @ $pos";
219                          last;                          last;
220                  }                  }
221    
222                  eval "$code";                  eval "$code";
223                  if ( $@ ) {                  if ( $@ ) {
224                          warn "ABORT [$pos] $@\n";                          warn "ABORT $pos $@\n";
225                          last;                          last;
226                  } else {                  } else {
227                          $affected++;                          $affected++;
228                  }                  }
229    
230                    $pos % 10000 == 0 ? print STDERR $pos :
231                    $pos % 1000  == 0 ? print STDERR "."  : 0 ;
232          };          };
233    
234          report "$affected affected records $view";          report "\n[$port] RECS $affected $view";
235    
236          warn "WARN no \$out defined!" unless defined $out;          warn "WARN no \$out defined!" unless defined $out;
237    
238            $digest->sync;
239    
240          if ( $connected ) {          if ( $connected ) {
241                  warn "# get results from ", join(' ', keys %$connected );                  foreach my $node ( keys %$connected ) {
242                  merge_out( thaw( get_node( $_ ) ) ) foreach keys %$connected;                          warn "[$port] get_node $node\n";
243                            my $o = get_node $node;
244                            my $s = length $o;
245                            $o = thaw $o;
246                            warn "[$port] merge $node $s bytes\n";
247                            merge_out $node => $o;
248                    }
249          }          }
250  }  }
251    
# Line 198  sub run_views { Line 262  sub run_views {
262                  run_code $view => $code;                  run_code $view => $code;
263    
264                  if ( defined $out ) {                  if ( defined $out ) {
                         my $dump = dump $out;  
                         my $len  = length $dump;  
265    
266                          my $path = $view;                          my $path = $view;
267                          $path =~ s{views?/}{out/} || die "no view in $view";                          $path =~ s{views?/}{out/} || die "no view in $view";
268                          $path =~ s{\.pl}{};                          $path =~ s{\.pl}{.storable};
   
                         print "OUT $view $offset/$limit $len bytes $path"  
                                 , ( $len < 10000 ?  " \$out = $dump" : ' SAVED ONLY' )  
                                 , "\n"  
                                 ;  
269    
270                          unlink "$path.last" if -e "$path.last";                          unlink "$path.last" if -e "$path.last";
271                          rename $path, "$path.last";                          rename $path, "$path.last";
272                          write_file $path, $dump;  
273                          report "SAVE $path";                          store $out => $path;
274                            report "[$port] SAVE $path $offset-$limit", -s $path, "bytes";
275    
276                            if ( -s $path < 4096 ) {
277                                    print '$out = ', dump $digest->undigest_out($out);
278                            }
279                  }                  }
280    
281          }          }
282    
283  }  }
284    
285  if ( $listen ) {  
286    sub info_tabs {
287            "$port\t$offset\t$limit\t$num_records\t$path\t"
288            . join("\t", map {
289                    my $b = $_;
290                    $b =~ s{^.+\.$port\.([^/]+)$}{$1};
291                    "$b " . -s $_
292            } glob "/dev/shm/sack.$port.*" );
293    }
294    
295    
296    if ( $port ) {
297          my $sock = IO::Socket::INET->new(          my $sock = IO::Socket::INET->new(
298                  Listen    => SOMAXCONN,                  Listen    => SOMAXCONN,
299  #               LocalAddr => '0.0.0.0',                  LocalAddr => '127.0.0.1',
300                  LocalPort => $listen,                  LocalPort => $port,
301                  Proto     => 'tcp',                  Proto     => 'tcp',
302                  Reuse     => 1,                  Reuse     => 1,
303          ) or die $!;          ) or die $!;
304    
305          while (1) {          while (1) {
306    
307                  warn "NODE listen on $listen\n";                  warn "[$port] READY path: $path offset: $offset limit: $limit #recs: $num_records\n";
308    
309                  my $client = $sock->accept();                  my $client = $sock->accept();
310    
311                  warn "<<<< $listen connect from ", $client->peerhost, $/;                  warn "[$port] <<<< connect from ", $client->peerhost, $/;
312    
313                  my @header = split(/\s/, <$client>);                  my @header = split(/\s/, <$client>);
314                  warn "# header ",dump @header;                  warn "[$port] <<<< header ",dump(@header),$/;
315    
316                  my $size = shift @header;                  my $size = shift @header;
317    
# Line 249  if ( $listen ) { Line 322  if ( $listen ) {
322                          run_code $header[1] => $content;                          run_code $header[1] => $content;
323                          send_sock $client => freeze $out;                          send_sock $client => freeze $out;
324                  } elsif ( $header[0] eq 'info' ) {                  } elsif ( $header[0] eq 'info' ) {
325                          my $info = "$listen\t$offset\t$limit\t$path";                          my $info = info_tabs;
326                          warn "info $info\n";                          warn "[$port] info $info\n";
327                          send_sock $client => $info;                          send_sock $client => $info;
328                  } elsif ( $header[0] eq 'exit' ) {                  } elsif ( $header[0] eq 'exit' ) {
329                          warn "exit $listen";                          warn "[$port] exit";
330                          exit;                          exit;
331                  } else {                  } else {
332                          warn "WARN $listen unknown";                          warn "[$port] UNKNOWN $header[0]";
333                  }                  }
334    
335          }          }
336  }  }
337    
338  run_views;  sub info {
339            send_nodes 'info' => $2;
340    
341  while ( 1 ) {          my @info = (
342                    "port\toffset\tlimit\t#recs\tpath",
343                    "----\t------\t-----\t-----\t----",
344                    info_tabs,
345            );
346    
347            push @info, get_node $_ foreach @nodes;
348    
349            print "[$port] INFO\n"
350                    , join("\n", @info)
351                    , "\n\n" ;
352    
353          print "sack> ";          return @info;
354          my $cmd = <STDIN>;  }
355    
356          if ( $cmd =~ m{^(vi?|\\e|o(?:ut)?)}i ) {  info;
357                  system "vi out/*";  run_views;
         } elsif ( $cmd =~ m{^i(nfo)?}i ) {  
                 print "# nodes: ", join(' ',@nodes), $/;  
   
                 my @info = (  
                         "node\toffset\tlimit\tpath",  
                         "----\t------\t-----\t----",  
                         "here\t$offset\t$limit\t$path",  
                 );  
358    
359                  send_nodes 'info';  while ( 1 ) {
                 push @info, get_node $_ foreach @nodes;  
360    
361                  print "$_\n" foreach @info;          print "sack> ";
362            chomp( my $cmd = <STDIN> );
363    
364          } elsif ( $cmd =~ m{^(q(uit)|e(xit))}i ) {          if ( $cmd =~ m{^(h|\?)} ) {
365                    print << "__HELP__"
366    Sacks Lorry v$VERSION - path: $path offset: $offset limit: $limit
367    
368            View Run        run views
369            VI \\e Output   show output of last run
370            Info [\$VERSION]        instrospect
371            Quit EXit       shutdown
372    
373    __HELP__
374            } elsif ( $cmd =~ m{^(vi|\\e|o)}i ) {
375                    #system "vi out/*";
376                    $digest->sync;
377                    system "bin/storableedit.pl", (glob('out/*.storable'))[0];
378            } elsif ( $cmd =~ m{^i(?:nfo)?\s?(.+)?$}i ) {
379                    info;
380            } elsif ( $cmd =~ m{^(q|e|x)}i ) {
381                  warn "# exit";                  warn "# exit";
382                  send_nodes 'exit';                  send_nodes 'exit';
383                  exit;                  exit;
384          } else {          } elsif ( $cmd =~ m{^(v|r)}i ) {
385                  run_views;                  run_views;
386            } elsif ( $cmd =~ m{^n(ode)?\s*(\d+)}i ) {
387                    push @nodes, $1;
388                    info;
389            } elsif ( $cmd ) {
390                    warn "UNKNOWN ", dump $cmd;
391          }          }
392    
393  }  }

Legend:
Removed from v.20  
changed lines
  Added in v.59

  ViewVC Help
Powered by ViewVC 1.1.26