/[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 21 - (show annotations)
Tue Sep 22 17:15:03 2009 UTC (14 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 6306 byte(s)
- added $VERSION [0.01]
- exit connected nodes on SIGINT
- fix merge when shards produce array of values
- make code display debug option
- listen on 127.0.0.1 (we are using ssh tunnels anyway)
- changed commands a bit and added interactive help on h or ?

1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 our $VERSION = '0.01';
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/;
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 $listen = 0; # off
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' => \$listen,
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 =~ s{^./}{} ) {
44 chomp( my $pwd = `pwd` );
45 $prefix = "$pwd/$prefix";
46 }
47 $prefix =~ s{^(.*)/srv/Sack/bin.+$}{$1};
48 warn "# prefix $prefix";
49
50 $SIG{INT} = sub {
51 my $signame = shift;
52 send_nodes 'exit';
53 die "SIG$signame";
54 };
55 }
56
57
58 use lib "$prefix/srv/webpac2/lib/";
59 use WebPAC::Input::ISI;
60 my $input = WebPAC::Input::ISI->new(
61 path => "$prefix/$path",
62 offset => $offset,
63 limit => $limit,
64 );
65
66
67 sub report {
68 my $description = shift;
69 my $dt = time - $t;
70 printf "%s in %1.4fs %.2f/s\n", $description, $dt, $input->size / $dt;
71 $t = time;
72 }
73
74
75 report $input->size . ' records loaded';
76
77 mkdir 'out' unless -e 'out';
78
79 our $out;
80
81 our $cache;
82
83 our $connected;
84
85 sub send_nodes {
86 my $content = $#_ > 0 ? pop @_ : ''; # no content with just one argument!
87 my $header = defined $content ? length($content) : 0;
88 $header .= ' ' . join(' ', @_) if @_;
89
90 foreach my $node ( @nodes ) {
91
92 my $sock = IO::Socket::INET->new(
93 PeerAddr => $node,
94 Proto => 'tcp',
95 );
96
97 if ( ! $sock ) {
98 warn "can't connect to $node - $!"; # FIXME die?
99 next;
100 }
101
102 print ">>>> $listen $node $header\n";
103 print $sock "$header\n$content" || warn "can't send $header to $node: $!";
104
105 $connected->{$node} = $sock;
106 }
107 }
108
109 sub get_node {
110 my $node = shift;
111
112 my $sock = $connected->{$node};
113 if ( ! $sock ) {
114 warn "ERROR: lost connection to $node";
115 delete $connected->{$node};
116 return;
117 }
118 chomp( my $size = <$sock> );
119 warn "<<<< $listen $node $size bytes\n";
120 my $data;
121 read $sock, $data, $size;
122 return $data;
123 }
124
125 sub send_sock {
126 my ( $sock, $data ) = @_;
127 my $size = length $data;
128 warn ">>>> $listen ", $sock->peerhost, " $size bytes";
129 print $sock "$size\n$data" || warn "can't send $size bytes to ", $sock->peerhost;
130 }
131
132 sub merge_out {
133 my $new = shift;
134
135 foreach my $k1 ( keys %$new ) {
136
137 foreach my $k2 ( keys %{ $new->{$k1} } ) {
138
139 my $n = $new->{$k1}->{$k2};
140 my $ref = ref $out->{$k1}->{$k2};
141
142 if ( ! defined $out->{$k1}->{$k2} ) {
143 $out->{$k1}->{$k2} = $n;
144 } elsif ( $k1 =~ m{\+} ) {
145 # warn "## agregate $k1 $k2";
146 $out->{$k1}->{$k2} += $n;
147 } elsif ( $ref eq 'ARRAY' ) {
148 if ( ref $n eq 'ARRAY' ) {
149 push @{ $out->{$k1}->{$k2} }, $_ foreach @$n;
150 } else {
151 push @{ $out->{$k1}->{$k2} }, $n;
152 }
153 } elsif ( $ref eq '' ) {
154 $out->{$k1}->{$k2} = [ $out->{$k1}->{$k2}, $n ];
155 } else {
156 die "can't merge $k2 [$ref] from ",dump($n), " into ", dump($out->{$k1}->{$k2});
157 }
158 }
159 }
160
161 warn "## merge out ", dump $out if $debug;
162 }
163
164 sub run_code {
165 my ( $view, $code ) = @_;
166
167 warn "\n#### CODE $view START ####\n$code\n#### CODE $view END ####\n" if $debug;
168
169 send_nodes view => $view => $code;
170
171 undef $out;
172
173 my $affected = 0;
174 $t = time;
175
176 foreach my $pos ( $offset + 1 .. $offset + $input->size ) {
177 my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );
178 if ( ! $rec ) {
179 warn "END at $pos";
180 last;
181 }
182
183 eval "$code";
184 if ( $@ ) {
185 warn "ABORT [$pos] $@\n";
186 last;
187 } else {
188 $affected++;
189 }
190 };
191
192 report "$affected affected records $view";
193
194 warn "WARN no \$out defined!" unless defined $out;
195
196 if ( $connected ) {
197 warn "# get results from ", join(' ', keys %$connected );
198 merge_out( thaw( get_node( $_ ) ) ) foreach keys %$connected;
199 }
200 }
201
202 sub run_views {
203 @views = sort glob 'views/*.pl' unless @views;
204 warn "# views ", dump @views;
205
206 foreach my $view ( @views ) {
207
208 next if system("perl -c $view") != 0;
209
210 my $code = read_file $view;
211
212 run_code $view => $code;
213
214 if ( defined $out ) {
215 my $dump = dump $out;
216 my $len = length $dump;
217
218 my $path = $view;
219 $path =~ s{views?/}{out/} || die "no view in $view";
220 $path =~ s{\.pl}{};
221
222 print "OUT $view $offset/$limit $len bytes $path"
223 , ( $len < 10000 ? " \$out = $dump" : ' SAVED ONLY' )
224 , "\n"
225 ;
226
227 unlink "$path.last" if -e "$path.last";
228 rename $path, "$path.last";
229 write_file $path, $dump;
230 report "SAVE $path";
231 }
232
233 }
234
235 }
236
237 if ( $listen ) {
238 my $sock = IO::Socket::INET->new(
239 Listen => SOMAXCONN,
240 LocalAddr => '127.0.0.1',
241 LocalPort => $listen,
242 Proto => 'tcp',
243 Reuse => 1,
244 ) or die $!;
245
246 while (1) {
247
248 warn "NODE listen on $listen\n";
249
250 my $client = $sock->accept();
251
252 warn "<<<< $listen connect from ", $client->peerhost, $/;
253
254 my @header = split(/\s/, <$client>);
255 warn "# $listen header ",dump @header;
256
257 my $size = shift @header;
258
259 my $content;
260 read $client, $content, $size;
261
262 if ( $header[0] eq 'view' ) {
263 run_code $header[1] => $content;
264 send_sock $client => freeze $out;
265 } elsif ( $header[0] eq 'info' ) {
266 my $info = "$listen\t$offset\t$limit\t$path";
267 $info .= "\t" . eval $header[1] if $header[1];
268 warn "info $info\n";
269 send_sock $client => $info;
270 } elsif ( $header[0] eq 'exit' ) {
271 warn "exit $listen";
272 exit;
273 } else {
274 warn "WARN $listen unknown";
275 }
276
277 }
278 }
279
280 run_views;
281
282 while ( 1 ) {
283
284 print "sack> ";
285 chomp( my $cmd = <STDIN> );
286
287 if ( $cmd =~ m{^(h|\?)} ) {
288 print << "__HELP__"
289 Sacks Lorry v$VERSION - path: $path offset: $offset limit: $limit
290
291 View Run run views
292 VI \\e Output show output of last run
293 Info [\$VERSION] instrospect
294 Quit EXit shutdown
295
296 __HELP__
297 } elsif ( $cmd =~ m{^(vi|\\e|o)}i ) {
298 system "vi out/*";
299 } elsif ( $cmd =~ m{^i(?:nfo)?\s?(.+)?$}i ) {
300 print "# nodes: ", join(' ',@nodes), $/;
301
302 send_nodes 'info' => $2;
303
304 my @info = (
305 "node\toffset\tlimit\tpath",
306 "----\t------\t-----\t----",
307 "0\t$offset\t$limit\t$path",
308 );
309
310 push @info, get_node $_ foreach @nodes;
311
312 print "$_\n" foreach @info;
313
314 } elsif ( $cmd =~ m{^(q|e|x)}i ) {
315 warn "# exit";
316 send_nodes 'exit';
317 exit;
318 } elsif ( $cmd =~ m{^(v|r)}i ) {
319 run_views;
320 } elsif ( $cmd ) {
321 warn "UNKNOWN ", dump $cmd;
322 }
323
324 }
325

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26