/[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

Contents of /trunk/bin/sack.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 60 - (show annotations)
Fri Sep 25 13:02:32 2009 UTC (14 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 8005 byte(s)
tweaks in output, fix (add) node
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 our $VERSION = '0.04';
7
8 use Time::HiRes qw(time);
9 use Data::Dump qw(dump);
10 use File::Slurp;
11 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';
18 my $limit = 5000;
19 my $offset = 0;
20 my @views;
21 my $port = 0; # interactive
22 my @nodes;
23
24
25 GetOptions(
26 'path=s' => \$path,
27 'offset=i' => \$offset,
28 'limit=i' => \$limit,
29 'view=s' => \@views,
30 'listen|port=i' => \$port,
31 'connect=s' => \@nodes,
32 'debug!' => \$debug,
33 ) or die $!;
34
35 my $t = time;
36
37
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;
65
66 $WebPAC::Input::ISI::subfields = undef; # disable parsing of subfields
67
68 my $input = WebPAC::Input::ISI->new(
69 path => "$prefix/$path",
70 offset => $offset,
71 limit => $limit,
72 );
73
74 our $num_records = $input->size;
75
76 sub report {
77 my $description = join(' ',@_);
78 my $dt = time - $t;
79 printf "%s in %1.4fs %.2f/s\n", $description, $dt, $input->size / $dt;
80 $t = time;
81 }
82
83
84 report $input->size . ' records loaded';
85
86 mkdir 'out' unless -e 'out';
87
88 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 warn "# send_nodes ", dump @nodes;
100
101 foreach my $node ( @nodes ) {
102
103 my $sock = IO::Socket::INET->new(
104 PeerAddr => '127.0.0.1',
105 PeerPort => $node,
106 Proto => 'tcp',
107 );
108
109 if ( ! $sock ) {
110 warn "[$port] can't connect to $node - $!\n"; # FIXME die?
111 next;
112 }
113
114 warn "[$port] >>>> $node $header\n";
115 print $sock "$header\n$content" || warn "can't send $header to $node: $!";
116
117 $connected->{$node} = $sock;
118 }
119 }
120
121 sub get_node {
122 my $node = shift;
123
124 my $sock = $connected->{$node};
125 if ( ! $sock ) {
126 warn "[$port] ERROR lost connection to $node";
127 delete $connected->{$node};
128 return;
129 }
130 chomp( my $size = <$sock> );
131 warn "[$port] <<<< $node $size bytes\n" if $debug || $size > 1024;
132 my $data;
133 read $sock, $data, $size;
134 return $data;
135 }
136
137 sub send_sock {
138 my ( $sock, $data ) = @_;
139 my $size = length $data;
140 warn "[$port] >>>> $size bytes\n" if $debug || $size > 1024;
141 print $sock "$size\n$data" || warn "can't send $size bytes to ", $sock->peerhost;
142 }
143
144 sub merge_out {
145 my ( $from_node, $new ) = @_;
146
147 my $remote_digest = Sack::Digest->new( port => $from_node );
148 my ( $local, $remote ) = ( 0, 0 );
149
150 my $tick = 0;
151 print STDERR "[$port] merge $from_node";
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_node\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 if ( $tick++ % 1000 == 0 ) {
192 print STDERR ".";
193 } elsif ( $tick % 10000 == 0 ) {
194 print STDERR $tick;
195 }
196 }
197 }
198
199 warn "$tick $local/$remote\n";
200
201 warn "## merge out ", dump $out if $debug;
202 }
203
204 sub run_code {
205 my ( $view, $code ) = @_;
206
207 warn "\n#### CODE $view START ####\n$code\n#### CODE $view END ####\n" if $debug;
208
209 send_nodes view => $view => $code;
210
211 undef $out;
212
213 my $affected = 0;
214 $t = time;
215
216 foreach my $pos ( $offset + 1 .. $offset + $input->size ) {
217 my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );
218 if ( ! $rec ) {
219 print STDERR "END @ $pos";
220 last;
221 }
222
223 eval "$code";
224 if ( $@ ) {
225 warn "ABORT $pos $@\n";
226 last;
227 } else {
228 $affected++;
229 }
230
231 $pos % 10000 == 0 ? print STDERR $pos :
232 $pos % 1000 == 0 ? print STDERR "." : 0 ;
233 };
234
235 report "\n[$port] RECS $affected $view";
236
237 warn "WARN no \$out defined!" unless defined $out;
238
239 $digest->sync;
240
241 if ( $connected ) {
242 foreach my $node ( keys %$connected ) {
243 warn "[$port] get_node $node\n";
244 my $o = get_node $node;
245 my $s = length $o;
246 $o = thaw $o;
247 warn "[$port] merge $node $s bytes\n";
248 merge_out $node => $o;
249 report "[$port] merged $node";
250 }
251 }
252 }
253
254 sub run_views {
255 @views = sort glob 'views/*.pl' unless @views;
256 warn "# views ", dump @views;
257
258 foreach my $view ( @views ) {
259
260 next if system("perl -c $view") != 0;
261
262 my $code = read_file $view;
263
264 run_code $view => $code;
265
266 if ( defined $out ) {
267
268 my $path = $view;
269 $path =~ s{views?/}{out/} || die "no view in $view";
270 $path =~ s{\.pl}{.storable};
271
272 unlink "$path.last" if -e "$path.last";
273 rename $path, "$path.last";
274
275 store $out => $path;
276 report "[$port] SAVE $path $offset-$limit", -s $path, "bytes";
277
278 if ( -s $path < 4096 ) {
279 print '$out = ', dump $digest->undigest_out($out);
280 }
281 }
282
283 }
284
285 }
286
287
288 sub info_tabs {
289 "$port\t$offset\t$limit\t$num_records\t$path\t"
290 . join("\t", map {
291 my $b = $_;
292 $b =~ s{^.+\.$port\.([^/]+)$}{$1};
293 "$b " . -s $_
294 } glob "/dev/shm/sack.$port.*" );
295 }
296
297
298 if ( $port ) {
299 my $sock = IO::Socket::INET->new(
300 Listen => SOMAXCONN,
301 LocalAddr => '127.0.0.1',
302 LocalPort => $port,
303 Proto => 'tcp',
304 Reuse => 1,
305 ) or die $!;
306
307 while (1) {
308
309 warn "[$port] READY path: $path offset: $offset limit: $limit #recs: $num_records\n";
310
311 my $client = $sock->accept();
312
313 warn "[$port] <<<< connect from ", $client->peerhost, $/;
314
315 my @header = split(/\s/, <$client>);
316 warn "[$port] <<<< header ",dump(@header),$/;
317
318 my $size = shift @header;
319
320 my $content;
321 read $client, $content, $size;
322
323 if ( $header[0] eq 'view' ) {
324 run_code $header[1] => $content;
325 send_sock $client => freeze $out;
326 } elsif ( $header[0] eq 'info' ) {
327 my $info = info_tabs;
328 warn "[$port] info $info\n";
329 send_sock $client => $info;
330 } elsif ( $header[0] eq 'exit' ) {
331 warn "[$port] exit";
332 exit;
333 } else {
334 warn "[$port] UNKNOWN $header[0]";
335 }
336
337 }
338 }
339
340 sub info {
341 send_nodes 'info' => $2;
342
343 my @info = (
344 "port\toffset\tlimit\t#recs\tpath",
345 "----\t------\t-----\t-----\t----",
346 info_tabs,
347 );
348
349 push @info, get_node $_ foreach @nodes;
350
351 print "[$port] INFO\n"
352 , join("\n", @info)
353 , "\n\n" ;
354
355 return @info;
356 }
357
358 info;
359 run_views;
360
361 while ( 1 ) {
362
363 print "sack> ";
364 chomp( my $cmd = <STDIN> );
365
366 if ( $cmd =~ m{^(h|\?)} ) {
367 print << "__HELP__"
368 Sacks Lorry v$VERSION - path: $path offset: $offset limit: $limit
369
370 View Run run views
371 VI \\e Output show output of last run
372 Info [\$VERSION] instrospect
373 Quit EXit shutdown
374
375 __HELP__
376 } elsif ( $cmd =~ m{^(vi|\\e|o)}i ) {
377 #system "vi out/*";
378 $digest->sync;
379 system "bin/storableedit.pl", (glob('out/*.storable'))[0];
380 } elsif ( $cmd =~ m{^i(?:nfo)?\s?(.+)?$}i ) {
381 info;
382 } elsif ( $cmd =~ m{^(q|e|x)}i ) {
383 warn "# exit";
384 send_nodes 'exit';
385 exit;
386 } elsif ( $cmd =~ m{^(v|r)}i ) {
387 run_views;
388 } elsif ( $cmd =~ m{^n(ode)?\s*(\d+)}i ) {
389 push @nodes, $2;
390 info;
391 } elsif ( $cmd ) {
392 warn "UNKNOWN ", dump $cmd;
393 }
394
395 }
396

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26