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

Legend:
Removed from v.6  
changed lines
  Added in v.58

  ViewVC Help
Powered by ViewVC 1.1.26