/[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 24 by dpavlin, Tue Sep 22 21:27:13 2009 UTC revision 50 by dpavlin, Thu Sep 24 19:21:55 2009 UTC
# Line 3  Line 3 
3  use warnings;  use warnings;
4  use strict;  use strict;
5    
6  our $VERSION = '0.01';  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 18  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 = 0; # off  my $port   = 0; # interactive
22  my @nodes;  my @nodes;
23    
24    
# Line 27  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 40  sub send_nodes; Line 40  sub send_nodes;
40  our $prefix;  our $prefix;
41  sub 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/[\./]+bin.+$}{$1};
48          warn "# prefix $prefix";          warn "# prefix $prefix";
49    
50          $SIG{INT} = sub {          $SIG{INT} = sub {
51                  my $signame = shift;                  my $signame = shift;
52                  send_nodes 'exit';                  send_nodes 'exit';
53                    #clean if $clean;       # FIXME
54                  die "SIG$signame";                  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;
# Line 66  my $input = WebPAC::Input::ISI->new( Line 71  my $input = WebPAC::Input::ISI->new(
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 102  sub send_nodes { Line 108  sub send_nodes {
108                          next;                          next;
109                  }                  }
110    
111                  print ">>>> $listen $node $header\n";                  warn "[$port] >>>> $node $header\n";
112                  print $sock "$header\n$content" || warn "can't send $header to $node: $!";                  print $sock "$header\n$content" || warn "can't send $header to $node: $!";
113    
114                  $connected->{$node} = $sock;                  $connected->{$node} = $sock;
# Line 114  sub get_node { Line 120  sub get_node {
120    
121          my $sock = $connected->{$node};          my $sock = $connected->{$node};
122          if ( ! $sock ) {          if ( ! $sock ) {
123                  warn "ERROR: lost connection to $node";                  warn "[$port] ERROR lost connection to $node";
124                  delete $connected->{$node};                  delete $connected->{$node};
125                  return;                  return;
126          }          }
127          chomp( my $size = <$sock> );          chomp( my $size = <$sock> );
128          warn "<<<< $listen $node $size bytes\n";          warn "[$port] <<<< $node $size bytes\n";
129          my $data;          my $data;
130          read $sock, $data, $size;          read $sock, $data, $size;
131          return $data;          return $data;
# Line 128  sub get_node { Line 134  sub get_node {
134  sub send_sock {  sub send_sock {
135          my ( $sock, $data ) = @_;          my ( $sock, $data ) = @_;
136          my $size   = length $data;          my $size   = length $data;
137          warn ">>>> $listen ", $sock->peerhost, " $size bytes";          warn "[$port] >>>> ", $sock->peerhost, " $size bytes\n";
138          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;
139  }  }
140    
141  sub merge_out {  sub merge_out {
142          my $new = shift;          my ( $from_node, $new ) = @_;
143    
144            warn "### merge $from_node";
145    
146            my $from_port = $from_node;
147            $from_port =~ s{.+:(\d+)$}{$1};
148    
149            my $remote_digest = Sack::Digest->new( port => $from_port );
150            my ( $local, $remote ) = ( 0, 0 );
151    
152          foreach my $k1 ( keys %$new ) {          foreach my $k1 ( keys %$new ) {
153    
154                  foreach my $k2 ( keys %{ $new->{$k1} } ) {                  foreach my $k2 ( keys %{ $new->{$k1} } ) {
155    
156                          my $n   =     $new->{$k1}->{$k2};                          my $n   = delete $new->{$k1}->{$k2};
157                          my $ref = ref $out->{$k1}->{$k2};  
158                            if ( $k1 =~ m{#} ) {
159                                    die "ASSERT $k1 $k2" unless $k2 =~ m{^\d+$};
160    #warn "XXX $k1 $k2";
161                                    my $md5 = $remote_digest->{nr_md5}->[$k2] || warn "[$port] no2md5 $n not found in $from_port\n";
162                                    if ( my $local_k2 = $digest->{md5_nr}->{$md5} ) {
163                                            $k2 = $local_k2;
164                                            $local++;
165                                    } else {
166                                            $k2 = $digest->to_int( $remote_digest->{md5}->{$md5} );
167                                            $remote++;
168                                    }
169                            }
170    
171                            my $ref = ref    $out->{$k1}->{$k2};
172    #warn "XXXX $k1 $k2 $ref";
173                          if ( ! defined $out->{$k1}->{$k2} ) {                          if ( ! defined $out->{$k1}->{$k2} ) {
174                                  $out->{$k1}->{$k2} = $n;                                  $out->{$k1}->{$k2} = $n;
175                          } elsif ( $k1 =~ m{\+} ) {                          } elsif ( $k1 =~ m{\+} ) {
# Line 161  sub merge_out { Line 189  sub merge_out {
189                  }                  }
190          }          }
191    
192            warn "[$port] merge local $local remote $remote from $from_port\n";
193          warn "## merge out ", dump $out if $debug;          warn "## merge out ", dump $out if $debug;
194  }  }
195    
# Line 179  sub run_code { Line 208  sub run_code {
208          foreach my $pos ( $offset + 1 .. $offset + $input->size ) {          foreach my $pos ( $offset + 1 .. $offset + $input->size ) {
209                  my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );                  my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );
210                  if ( ! $rec ) {                  if ( ! $rec ) {
211                          warn "END at $pos";                          print STDERR "END @ $pos";
212                          last;                          last;
213                  }                  }
214    
215                  eval "$code";                  eval "$code";
216                  if ( $@ ) {                  if ( $@ ) {
217                          warn "ABORT [$pos] $@\n";                          warn "ABORT $pos $@\n";
218                          last;                          last;
219                  } else {                  } else {
220                          $affected++;                          $affected++;
221                  }                  }
222    
223                    $pos % 10000 == 0 ? print STDERR $pos :
224                    $pos % 1000  == 0 ? print STDERR "."  : 0 ;
225          };          };
226    
227          report "$affected affected records $view";          report "\n[$port] RECS $affected $view";
228    
229          warn "WARN no \$out defined!" unless defined $out;          warn "WARN no \$out defined!" unless defined $out;
230    
231            $digest->sync;
232    
233          if ( $connected ) {          if ( $connected ) {
234                  foreach my $node ( keys %$connected ) {                  foreach my $node ( keys %$connected ) {
235                          warn "# $listen get_node $node\n";                          warn "[$port] get_node $node\n";
236                          my $o = get_node $node;                          my $o = get_node $node;
237                          my $s = length $o;                          my $s = length $o;
238                          $o = thaw $o;                          $o = thaw $o;
239                          warn "# $listen merge $s bytes\n";                          warn "[$port] merge $node $s bytes\n";
240                          merge_out $o;                          merge_out $node => $o;
241                  }                  }
242          }          }
243  }  }
# Line 221  sub run_views { Line 255  sub run_views {
255                  run_code $view => $code;                  run_code $view => $code;
256    
257                  if ( defined $out ) {                  if ( defined $out ) {
                         my $dump = dump $out;  
                         my $len  = length $dump;  
258    
259                          my $path = $view;                          my $path = $view;
260                          $path =~ s{views?/}{out/} || die "no view in $view";                          $path =~ s{views?/}{out/} || die "no view in $view";
261                          $path =~ s{\.pl}{};                          $path =~ s{\.pl}{.storable};
   
                         print "OUT $view $offset/$limit $len bytes $path"  
                                 , ( $len < 10000 ?  " \$out = $dump" : ' SAVED ONLY' )  
                                 , "\n"  
                                 ;  
262    
263                          unlink "$path.last" if -e "$path.last";                          unlink "$path.last" if -e "$path.last";
264                          rename $path, "$path.last";                          rename $path, "$path.last";
265                          write_file $path, $dump;  
266                          report "SAVE $path";                          store $out => $path;
267                            report "[$port] SAVE $path $offset-$limit", -s $path, "bytes";
268    
269                            if ( -s $path < 4096 ) {
270                                    print '$out = ', dump $digest->undigest_out($out);
271                            }
272                  }                  }
273    
274          }          }
275    
276  }  }
277    
278  if ( $listen ) {  
279    sub info_tabs {
280            "$port\t$offset\t$limit\t$num_records\t$path\t"
281            . join("\t", map {
282                    my $b = $_;
283                    $b =~ s{^.+\.$port\.([^/]+)$}{$1};
284                    "$b " . -s $_
285            } glob "/dev/shm/sack.$port.*" );
286    }
287    
288    
289    if ( $port ) {
290          my $sock = IO::Socket::INET->new(          my $sock = IO::Socket::INET->new(
291                  Listen    => SOMAXCONN,                  Listen    => SOMAXCONN,
292                  LocalAddr => '127.0.0.1',                  LocalAddr => '127.0.0.1',
293                  LocalPort => $listen,                  LocalPort => $port,
294                  Proto     => 'tcp',                  Proto     => 'tcp',
295                  Reuse     => 1,                  Reuse     => 1,
296          ) or die $!;          ) or die $!;
297    
298          while (1) {          while (1) {
299    
300                  warn "NODE $listen ready - wating for connection\n";                  warn "[$port] READY path: $path offset: $offset limit: $limit #recs: $num_records\n";
301    
302                  my $client = $sock->accept();                  my $client = $sock->accept();
303    
304                  warn "<<<< $listen connect from ", $client->peerhost, $/;                  warn "[$port] <<<< connect from ", $client->peerhost, $/;
305    
306                  my @header = split(/\s/, <$client>);                  my @header = split(/\s/, <$client>);
307                  warn "<<<< $listen header ",dump(@header),$/;                  warn "[$port] <<<< header ",dump(@header),$/;
308    
309                  my $size = shift @header;                  my $size = shift @header;
310    
# Line 272  if ( $listen ) { Line 315  if ( $listen ) {
315                          run_code $header[1] => $content;                          run_code $header[1] => $content;
316                          send_sock $client => freeze $out;                          send_sock $client => freeze $out;
317                  } elsif ( $header[0] eq 'info' ) {                  } elsif ( $header[0] eq 'info' ) {
318                          my $info = "$listen\t$offset\t$limit\t$path";                          my $info = info_tabs;
319                          $info .= "\t" . eval $header[1] if $header[1];                          warn "[$port] info $info\n";
                         warn "info $info\n";  
320                          send_sock $client => $info;                          send_sock $client => $info;
321                  } elsif ( $header[0] eq 'exit' ) {                  } elsif ( $header[0] eq 'exit' ) {
322                          warn "exit $listen";                          warn "[$port] exit";
323                          exit;                          exit;
324                  } else {                  } else {
325                          warn "WARN $listen unknown";                          warn "[$port] UNKNOWN $header[0]";
326                  }                  }
327    
328          }          }
329  }  }
330    
331    sub info {
332            send_nodes 'info' => $2;
333    
334            my @info = (
335                    "port\toffset\tlimit\t#recs\tpath",
336                    "----\t------\t-----\t-----\t----",
337                    info_tabs,
338            );
339    
340            push @info, get_node $_ foreach @nodes;
341    
342            print "[$port] INFO\n"
343                    , join("\n", @info)
344                    , "\n\n" ;
345    
346            return @info;
347    }
348    
349    info;
350  run_views;  run_views;
351    
352  while ( 1 ) {  while ( 1 ) {
# Line 304  Sacks Lorry v$VERSION - path: $path offs Line 365  Sacks Lorry v$VERSION - path: $path offs
365    
366  __HELP__  __HELP__
367          } elsif ( $cmd =~ m{^(vi|\\e|o)}i ) {          } elsif ( $cmd =~ m{^(vi|\\e|o)}i ) {
368                  system "vi out/*";                  #system "vi out/*";
369                    system "bin/storableedit.pl", (glob('out/*.storable'))[0];
370          } elsif ( $cmd =~ m{^i(?:nfo)?\s?(.+)?$}i ) {          } elsif ( $cmd =~ m{^i(?:nfo)?\s?(.+)?$}i ) {
371                  print "# nodes: ", join(' ',@nodes), $/;                  info;
   
                 send_nodes 'info' => $2;  
   
                 my @info = (  
                         "node\toffset\tlimit\tpath",  
                         "----\t------\t-----\t----",  
                         "0\t$offset\t$limit\t$path",  
                 );  
   
                 push @info, get_node $_ foreach @nodes;  
   
                 print "$_\n" foreach @info;  
   
372          } elsif ( $cmd =~ m{^(q|e|x)}i ) {          } elsif ( $cmd =~ m{^(q|e|x)}i ) {
373                  warn "# exit";                  warn "# exit";
374                  send_nodes 'exit';                  send_nodes 'exit';

Legend:
Removed from v.24  
changed lines
  Added in v.50

  ViewVC Help
Powered by ViewVC 1.1.26