/[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 21 by dpavlin, Tue Sep 22 17:15:03 2009 UTC revision 76 by dpavlin, Fri Sep 25 21:12:46 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.05';
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,          'port|listen=i' => \$port,
31          'connect=s'   => \@nodes,          'node|connect=i'   => \@nodes,
32          'debug!'   => \$debug,          'debug!'   => \$debug,
33  ) or die $!;  ) or die $!;
34    
# 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/.+$}{$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;
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    our @reports;
76    
77  sub report {  sub report {
78          my $description = shift;          my $description = join(' ',@_);
79          my $dt = time - $t;          my $dt = time - $t;
80          printf "%s in %1.4fs %.2f/s\n", $description, $dt, $input->size / $dt;          my $report = [ $description, $dt, $input->size / $dt ];
81            printf "[$port] %s in %1.4fs %.2f/s\n", @$report;
82            push @reports, $report;
83          $t = time;          $t = time;
84  }  }
85    
86    sub show_report {
87            "\n" . join( "\n", map { sprintf "%8.4fs %10.2f/s  %s", $_->[1], $_->[2], $_->[0] } @reports ) . "\n";
88    }
89    
90  report $input->size . ' records loaded';  report $input->size , 'records loaded';
91    
92  mkdir 'out' unless -e 'out';  mkdir 'out' unless -e 'out';
93    
# Line 82  our $cache; Line 97  our $cache;
97    
98  our $connected;  our $connected;
99    
100    sub node_sock {
101            my $node = shift;
102            my $sock = IO::Socket::INET->new(
103                            PeerAddr => '127.0.0.1',
104                            PeerPort => $node,
105                            Proto    => 'tcp',
106            );
107            
108            return $sock if $sock && $sock->connected;
109    
110            warn "[$port] can't connect to $node - $!\n"; # FIXME die?
111            return;
112    }
113    
114  sub send_nodes {  sub send_nodes {
115          my $content = $#_ > 0 ? pop @_ : '';    # no content with just one argument!          my $content = $#_ > 0 ? pop @_ : '';    # no content with just one argument!
116          my $header = defined $content ? length($content) : 0;          my $header = defined $content ? length($content) : 0;
117          $header .= ' ' . join(' ', @_) if @_;          $header .= ' ' . join(' ', @_) if @_;
118    
119          foreach my $node ( @nodes ) {          warn "# send_nodes ", dump(@_), " to ", dump @nodes;
120    
121                  my $sock = IO::Socket::INET->new(          foreach my $node ( @nodes ) {
                         PeerAddr => $node,  
                         Proto    => 'tcp',  
                 );  
122    
123                  if ( ! $sock ) {                  my $sock = node_sock($node) || next;
                         warn "can't connect to $node - $!"; # FIXME die?  
                         next;  
                 }  
124    
125                  print ">>>> $listen $node $header\n";                  warn "[$port] >>>> [$node] $header\n";
126                  print $sock "$header\n$content" || warn "can't send $header to $node: $!";                  print $sock "$header\n$content" || warn "can't send $header to $node: $!";
127    
128                  $connected->{$node} = $sock;                  $connected->{$node} = $sock;
# Line 110  sub get_node { Line 133  sub get_node {
133          my $node = shift;          my $node = shift;
134    
135          my $sock = $connected->{$node};          my $sock = $connected->{$node};
136          if ( ! $sock ) {          if ( ! $sock || ! $sock->connected ) {
137                  warn "ERROR: lost connection to $node";                  warn "[$port] no connection to $node";
138                  delete $connected->{$node};                  delete $connected->{$node};
139                  return;                  return;
140          }          }
141          chomp( my $size = <$sock> );          chomp( my $size = <$sock> );
142          warn "<<<< $listen $node $size bytes\n";          warn "[$port] <<<< [$node] $size bytes\n" if $debug || $size > 1024;
143          my $data;          my $data;
144          read $sock, $data, $size;          read $sock, $data, $size;
145          return $data;          return $data;
# Line 125  sub get_node { Line 148  sub get_node {
148  sub send_sock {  sub send_sock {
149          my ( $sock, $data ) = @_;          my ( $sock, $data ) = @_;
150          my $size   = length $data;          my $size   = length $data;
151          warn ">>>> $listen ", $sock->peerhost, " $size bytes";          warn "[$port] >>>> $size bytes\n" if $debug || $size > 1024;
152          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;
153  }  }
154    
155    sub pull_node_file {
156            my ( $node, $file ) = @_;
157    
158            my $path = "/dev/shm/sack.$node.$file";
159            return if -e $path; # FIXME
160    
161            my $sock = node_sock $node || die "not connected to $node";
162    
163            print $sock "0 file $file\n";
164    
165            my $size = <$sock>;
166            chomp($size);
167            warn "[$port] pull_node_file $node $file $size bytes\n";
168    
169            my $block = 4096;
170            my $buff  = ' ' x $block;
171    
172            open(my $fh, '>', $path) || die "can't open $path";
173            while ( read $sock, $buff, $block ) {
174                    print $fh $buff;
175            }
176            close($fh);
177    }
178    
179  sub merge_out {  sub merge_out {
180          my $new = shift;          my ( $from_node, $new ) = @_;
181    
182            my $t_merge = time();
183    
184            pull_node_file $from_node => 'nr_md5';
185            pull_node_file $from_node => 'md5';
186    
187            my $remote_digest = Sack::Digest->new( port => $from_node );
188            my ( $local, $remote ) = ( 0, 0 );
189    
190            my $tick = 0;
191            print STDERR "[$port] merge [$from_node]";
192    
193            my $missing;
194    
195          foreach my $k1 ( keys %$new ) {          foreach my $k1 ( keys %$new ) {
196    
197                  foreach my $k2 ( keys %{ $new->{$k1} } ) {                  foreach my $k2 ( keys %{ $new->{$k1} } ) {
198    
199                          my $n   =     $new->{$k1}->{$k2};                          my $n   = delete $new->{$k1}->{$k2};
200                          my $ref = ref $out->{$k1}->{$k2};  
201                            if ( $k1 =~ m{#} ) {
202                                    die "ASSERT $k1 $k2" unless $k2 =~ m{^\d+$};
203    #warn "XXX $k1 $k2";
204                                    my $md5 = $remote_digest->{nr_md5}->[$k2];
205    
206                                    if ( ! $md5 ) {
207                                            $missing->{nr_md5}->{$from_node}++; # FIXME die?
208                                            next;
209                                    }
210    
211                                    if ( my $local_k2 = $digest->{md5_nr}->{$md5} ) {
212                                            $k2 = $local_k2;
213                                            $local++;
214                                    } elsif ( my $full = $remote_digest->{md5}->{$md5} ) {
215                                            $k2 = $digest->to_int( $remote_digest->{md5}->{$md5} );
216                                            $remote++;
217                                    } else {
218                                            $missing->{md5}->{$from_node}++;
219                                    }
220                            }
221    
222                            my $ref = ref    $out->{$k1}->{$k2};
223    #warn "XXXX $k1 $k2 $ref";
224                          if ( ! defined $out->{$k1}->{$k2} ) {                          if ( ! defined $out->{$k1}->{$k2} ) {
225                                  $out->{$k1}->{$k2} = $n;                                  $out->{$k1}->{$k2} = $n;
226                          } elsif ( $k1 =~ m{\+} ) {                          } elsif ( $k1 =~ m{\+} ) {
# Line 155  sub merge_out { Line 237  sub merge_out {
237                          } else {                          } else {
238                                  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});
239                          }                          }
240    
241                            if ( $tick++ % 1000 == 0 ) {
242                                    print STDERR ".";
243                            } elsif ( $tick % 10000 == 0 ) {
244                                    print STDERR $tick;
245                            }
246                  }                  }
247          }          }
248    
249            $t_merge = time - $t_merge;
250            printf STDERR "%d in %.4fs %.2f/s local %.1f%% %d/%d\n", $tick, $t_merge, $tick / $t_merge, $local * 100 / $tick, $local, $remote;
251            push @reports, [ "$tick merged $from_node", $t_merge, $tick / $t_merge ];
252    
253            warn "[$port] missing ", dump $missing if $missing;
254    
255          warn "## merge out ", dump $out if $debug;          warn "## merge out ", dump $out if $debug;
256  }  }
257    
# Line 176  sub run_code { Line 270  sub run_code {
270          foreach my $pos ( $offset + 1 .. $offset + $input->size ) {          foreach my $pos ( $offset + 1 .. $offset + $input->size ) {
271                  my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );                  my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );
272                  if ( ! $rec ) {                  if ( ! $rec ) {
273                          warn "END at $pos";                          print STDERR "END @ $pos";
274                          last;                          last;
275                  }                  }
276    
277                  eval "$code";                  eval "$code";
278                  if ( $@ ) {                  if ( $@ ) {
279                          warn "ABORT [$pos] $@\n";                          warn "ABORT $pos $@\n";
280                          last;                          last;
281                  } else {                  } else {
282                          $affected++;                          $affected++;
283                  }                  }
284    
285                    $pos % 10000 == 0 ? print STDERR $pos :
286                    $pos % 1000  == 0 ? print STDERR "."  : 0 ;
287          };          };
288    
289          report "$affected affected records $view";          report "$affected affected $view";
290    
291          warn "WARN no \$out defined!" unless defined $out;          warn "WARN no \$out defined!" unless defined $out;
292    
293            $digest->sync;
294    
295          if ( $connected ) {          if ( $connected ) {
296                  warn "# get results from ", join(' ', keys %$connected );                  foreach my $node ( keys %$connected ) {
297                  merge_out( thaw( get_node( $_ ) ) ) foreach keys %$connected;                          warn "[$port] get_node [$node]\n";
298                            my $o = get_node $node;
299                            next unless $o;
300                            my $s = length $o;
301                            $o = thaw $o;
302                            warn "[$port] got $s bytes from [$node]\n";
303                            merge_out $node => $o;
304                    }
305          }          }
306  }  }
307    
# Line 212  sub run_views { Line 318  sub run_views {
318                  run_code $view => $code;                  run_code $view => $code;
319    
320                  if ( defined $out ) {                  if ( defined $out ) {
                         my $dump = dump $out;  
                         my $len  = length $dump;  
321    
322                          my $path = $view;                          my $path = $view;
323                          $path =~ s{views?/}{out/} || die "no view in $view";                          $path =~ s{views?/}{out/} || die "no view in $view";
324                          $path =~ s{\.pl}{};                          $path =~ s{\.pl}{.storable};
   
                         print "OUT $view $offset/$limit $len bytes $path"  
                                 , ( $len < 10000 ?  " \$out = $dump" : ' SAVED ONLY' )  
                                 , "\n"  
                                 ;  
325    
326                          unlink "$path.last" if -e "$path.last";                          unlink "$path.last" if -e "$path.last";
327                          rename $path, "$path.last";                          rename $path, "$path.last";
328                          write_file $path, $dump;  
329                          report "SAVE $path";                          store $out => $path;
330                            report "save $path", -s $path, "bytes";
331    
332                            if ( -s $path < 4096 ) {
333                                    print '$out = ', dump $digest->undigest_out($out);
334                            }
335                  }                  }
336    
337          }          }
338    
339  }  }
340    
341  if ( $listen ) {  
342    sub info_tabs {
343            "$port\t$offset\t$limit\t$num_records\t$path\t"
344            . join("\t", map {
345                    my $b = $_;
346                    $b =~ s{^.+\.$port\.([^/]+)$}{$1};
347                    "$b " . -s $_
348            } glob "/dev/shm/sack.$port.*" );
349    }
350    
351    
352    if ( $port ) {
353    
354            my $pid_path = "/tmp/sack.$port.pid";
355            if ( -e $pid_path ) {
356                    my $pid = read_file $pid_path;
357                    kill 9, $pid && warn "[$port] kill old $pid";
358            }
359            write_file $pid_path, $$;
360    
361    
362          my $sock = IO::Socket::INET->new(          my $sock = IO::Socket::INET->new(
363                  Listen    => SOMAXCONN,                  Listen    => SOMAXCONN,
364                  LocalAddr => '127.0.0.1',                  LocalAddr => '127.0.0.1',
365                  LocalPort => $listen,                  LocalPort => $port,
366                  Proto     => 'tcp',                  Proto     => 'tcp',
367                  Reuse     => 1,                  Reuse     => 1,
368          ) or die $!;          ) or die $!;
369    
370          while (1) {          while (1) {
371    
372                  warn "NODE listen on $listen\n";                  warn "[$port] accept path: $path offset: $offset limit: $limit #recs: $num_records\n";
373    
374                  my $client = $sock->accept();                  my $client = $sock->accept();
375    
376                  warn "<<<< $listen connect from ", $client->peerhost, $/;                  warn "[$port] <<<< connect from ", $client->peerhost, $/;
377    
378                  my @header = split(/\s/, <$client>);                  my @header = split(/\s/, <$client>);
379                  warn "# $listen header ",dump @header;                  warn "[$port] <<<< header ",dump(@header),$/;
380    
381                  my $size = shift @header;                  my $size = shift @header;
382    
# Line 263  if ( $listen ) { Line 387  if ( $listen ) {
387                          run_code $header[1] => $content;                          run_code $header[1] => $content;
388                          send_sock $client => freeze $out;                          send_sock $client => freeze $out;
389                  } elsif ( $header[0] eq 'info' ) {                  } elsif ( $header[0] eq 'info' ) {
390                          my $info = "$listen\t$offset\t$limit\t$path";                          my $info = info_tabs;
391                          $info .= "\t" . eval $header[1] if $header[1];                          warn "[$port] info $info\n";
392                          warn "info $info\n";                          $info .= "\n" . show_report if $content =~ m{r}i;
393                          send_sock $client => $info;                          send_sock $client => $info;
394                  } elsif ( $header[0] eq 'exit' ) {                  } elsif ( $header[0] eq 'exit' ) {
395                          warn "exit $listen";                          warn "[$port] exit";
396                          exit;                          exit;
397                    } elsif ( $header[0] eq 'file' ) {
398                            $digest->close;
399                            my $path = "/dev/shm/sack.$port.$header[1]";
400                            my $size = -s $path;
401                            warn "[$port] >>>> file $path $size bytes\n";
402                            print $client "$size\n";
403                            my $block = 4096;
404                            my $buff  = ' ' x $block;
405                            open(my $fh, '<', $path) || die "can't open $path";
406                            while ( read $fh, $buff, $block ) {
407                                    print $client $buff;
408                            }
409                            $digest->open;
410                  } else {                  } else {
411                          warn "WARN $listen unknown";                          warn "[$port] UNKNOWN $header[0]";
412                  }                  }
413    
414                    $client->close;
415    
416          }          }
417  }  }
418    
419    sub info {
420            my $detail = shift || '';
421    
422            send_nodes 'info' => $detail;
423    
424            my @info = (
425                    "port\toffset\tlimit\t#recs\tpath",
426                    "----\t------\t-----\t-----\t----",
427                    info_tabs,
428            );
429    
430            push @info, show_report if $detail =~ m{r}i;
431    
432            push @info, get_node $_ foreach @nodes;
433    
434            print "[$port] INFO", $detail ? " $detail" : '', " \n"
435                    , join("\n", @info)
436                    , "\n" ;
437    
438            return @info;
439    }
440    
441    info;
442    while ( keys %$connected != @nodes ) {
443            warn "[$port] wait for ", join(' ', grep { ! defined $connected->{$_} } @nodes );
444            sleep 1;
445    }
446  run_views;  run_views;
447    
448  while ( 1 ) {  while ( 1 ) {
# Line 295  Sacks Lorry v$VERSION - path: $path offs Line 461  Sacks Lorry v$VERSION - path: $path offs
461    
462  __HELP__  __HELP__
463          } elsif ( $cmd =~ m{^(vi|\\e|o)}i ) {          } elsif ( $cmd =~ m{^(vi|\\e|o)}i ) {
464                  system "vi out/*";                  #system "vi out/*";
465          } elsif ( $cmd =~ m{^i(?:nfo)?\s?(.+)?$}i ) {                  $digest->sync;
466                  print "# nodes: ", join(' ',@nodes), $/;                  system "bin/storableedit.pl", (glob('out/*.storable'))[0];
467            } elsif ( $cmd =~ m{^i(?:nfo)?\s?(\S+)?$}i ) {
468                  send_nodes 'info' => $2;                  info $1;
   
                 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;  
   
469          } elsif ( $cmd =~ m{^(q|e|x)}i ) {          } elsif ( $cmd =~ m{^(q|e|x)}i ) {
470                  warn "# exit";                  warn "# exit";
471                  send_nodes 'exit';                  send_nodes 'exit';
472                  exit;                  exit;
473          } elsif ( $cmd =~ m{^(v|r)}i ) {          } elsif ( $cmd =~ m{^(v|r)}i ) {
474                  run_views;                  run_views;
475            } elsif ( $cmd =~ m{^n(ode)?\s*(\d+)}i ) {
476                    push @nodes, $2;
477                    info;
478          } elsif ( $cmd ) {          } elsif ( $cmd ) {
479                  warn "UNKNOWN ", dump $cmd;                  warn "UNKNOWN ", dump $cmd;
480          }          }

Legend:
Removed from v.21  
changed lines
  Added in v.76

  ViewVC Help
Powered by ViewVC 1.1.26