/[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 17 - (show annotations)
Tue Sep 22 13:40:02 2009 UTC (14 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 5584 byte(s)
fix warning
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use Time::HiRes qw(time);
7 use Data::Dump qw(dump);
8 use File::Slurp;
9 use Getopt::Long;
10 use IO::Socket::INET;
11 use Storable qw/freeze thaw/;
12
13
14 my $path = '/data/isi/full.txt';
15 my $limit = 5000;
16 my $offset = 0;
17 my @views;
18 my $listen;
19 my @nodes;
20
21
22 GetOptions(
23 'path=s' => \$path,
24 'offset=i' => \$offset,
25 'limit=i' => \$limit,
26 'view=s' => \@views,
27 'listen|port=i' => \$listen,
28 'connect=s' => \@nodes,
29 ) or die $!;
30
31 my $t = time;
32
33
34 our $prefix;
35 BEGIN {
36 $prefix = $0;
37 if ( $prefix =~ s{^./}{} ) {
38 chomp( my $pwd = `pwd` );
39 $prefix = "$pwd/$prefix";
40 }
41 $prefix =~ s{^(.*)/srv/Sack/bin.+$}{$1};
42 warn "# prefix $prefix";
43 }
44
45
46 use lib "$prefix/srv/webpac2/lib/";
47 use WebPAC::Input::ISI;
48 my $input = WebPAC::Input::ISI->new(
49 path => "$prefix/$path",
50 offset => $offset,
51 limit => $limit,
52 );
53
54
55 sub report {
56 my $description = shift;
57 my $dt = time - $t;
58 printf "%s in %1.4fs %.2f/s\n", $description, $dt, $input->size / $dt;
59 $t = time;
60 }
61
62
63 report $input->size . ' records loaded';
64
65 mkdir 'out' unless -e 'out';
66
67 our $out;
68
69 our $cache;
70
71 our $connected;
72
73 sub send_nodes {
74 my $content = $#_ > 0 ? pop @_ : ''; # no content with just one argument!
75 my $header = length($content);
76 $header .= ' ' . join(' ', @_) if @_;
77
78 foreach my $node ( @nodes ) {
79
80 my $sock = IO::Socket::INET->new(
81 PeerAddr => $node,
82 Proto => 'tcp',
83 );
84
85 if ( ! $sock ) {
86 warn "can't connect to $node - $!"; # FIXME die?
87 next;
88 }
89
90 print ">>>> $node $header\n";
91 print $sock "$header\n$content" || warn "can't send $header to $node: $!";
92
93 $connected->{$node} = $sock;
94 }
95 }
96
97 sub get_node {
98 my $node = shift;
99
100 my $sock = $connected->{$node};
101 if ( ! $sock ) {
102 warn "ERROR: lost connection to $node";
103 delete $connected->{$node};
104 return;
105 }
106 chomp( my $size = <$sock> );
107 warn "<<<< $node $size bytes\n";
108 my $data;
109 read $sock, $data, $size;
110 return $data;
111 }
112
113 sub send_sock {
114 my ( $sock, $data ) = @_;
115 my $size = length $data;
116 warn ">>>> ", $sock->peerhost, " $size bytes";
117 print $sock "$size\n$data" || warn "can't send $size bytes to ", $sock->peerhost;
118 }
119
120 sub merge_out {
121 my $new = shift;
122
123 warn "## merge $new\n";
124
125 foreach my $k1 ( keys %$new ) {
126
127 foreach my $k2 ( keys %{ $new->{$k1} } ) {
128
129 my $n = $new->{$k1}->{$k2};
130 my $ref = ref $out->{$k1}->{$k2};
131
132 if ( ! defined $out->{$k1}->{$k2} ) {
133 $out->{$k1}->{$k2} = $n;
134 } elsif ( $k1 =~ m{\+} ) {
135 warn "# agregate $k1 $k2";
136 $out->{$k1}->{$k2} += $n;
137 } elsif ( $ref eq 'ARRAY' ) {
138 push @{ $out->{$k1}->{$k2} }, $n;
139 } elsif ( $ref eq '' ) {
140 $out->{$k1}->{$k2} = [ $out->{$k1}->{$k2}, $n ];
141 } else {
142 die "can't merge $k2 [$ref] from ",dump($n), " into ", dump($out->{$k1}->{$k2});
143 }
144 }
145 }
146
147 warn "## merge out ", dump $out;
148 }
149
150 sub run_code {
151 my ( $view, $code ) = @_;
152
153 warn "\n#### CODE $view START ####\n$code\n#### CODE $view END ####\n";
154
155 send_nodes view => $view => $code;
156
157 undef $out;
158
159 my $affected = 0;
160 $t = time;
161
162 foreach my $pos ( $offset + 1 .. $offset + $input->size ) {
163 my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );
164 if ( ! $rec ) {
165 warn "END at $pos";
166 last;
167 }
168
169 eval "$code";
170 if ( $@ ) {
171 warn "ERROR [$pos] $@\n";
172 } else {
173 $affected++;
174 }
175 };
176
177 report "$affected affected records $view";
178
179 warn "WARN no \$out defined!" unless defined $out;
180
181 if ( $connected ) {
182 warn "# get results from ", join(' ', keys %$connected );
183 merge_out( thaw( get_node( $_ ) ) ) foreach keys %$connected;
184 }
185 }
186
187 sub run_views {
188 @views = sort glob 'views/*.pl' unless @views;
189 warn "# views ", dump @views;
190
191 foreach my $view ( @views ) {
192
193 next if system("perl -c $view") != 0;
194
195 my $code = read_file $view;
196
197 run_code $view => $code;
198
199 if ( defined $out ) {
200 my $dump = dump $out;
201 my $len = length $dump;
202
203 my $path = $view;
204 $path =~ s{views?/}{out/} || die "no view in $view";
205 $path =~ s{\.pl}{};
206
207 print "OUT $view $offset/$limit $len bytes $path"
208 , ( $len < 10000 ? " \$out = $dump" : ' SAVED ONLY' )
209 , "\n"
210 ;
211
212 unlink "$path.last" if -e "$path.last";
213 rename $path, "$path.last";
214 write_file $path, $dump;
215 report "SAVE $path";
216 }
217
218 }
219
220 }
221
222 if ( $listen ) {
223 my $sock = IO::Socket::INET->new(
224 Listen => SOMAXCONN,
225 # LocalAddr => '0.0.0.0',
226 LocalPort => $listen,
227 Proto => 'tcp',
228 Reuse => 1,
229 ) or die $!;
230
231 while (1) {
232
233 warn "NODE listen on $listen\n";
234
235 my $client = $sock->accept();
236
237 warn "<<<< $listen connect from ", $client->peerhost, $/;
238
239 my @header = split(/\s/, <$client>);
240 warn "# header ",dump @header;
241
242 my $size = shift @header;
243
244 my $content;
245 read $client, $content, $size;
246
247 if ( $header[0] eq 'view' ) {
248 run_code $header[1] => $content;
249 send_sock $client => freeze $out;
250 } elsif ( $header[0] eq 'info' ) {
251 my $info = "$listen\t$offset\t$limit\t$path";
252 warn "info $info\n";
253 send_sock $client => $info;
254 } elsif ( $header[0] eq 'exit' ) {
255 warn "exit $listen";
256 exit;
257 } else {
258 warn "WARN $listen unknown";
259 }
260
261 }
262 }
263
264 run_views;
265
266 while ( 1 ) {
267
268 print "sack> ";
269 my $cmd = <STDIN>;
270
271 if ( $cmd =~ m{^(vi?|\\e|o(?:ut)?)}i ) {
272 system "vi out/*";
273 } elsif ( $cmd =~ m{^i(nfo)?}i ) {
274 print "# nodes: ", join(' ',@nodes), $/;
275
276 my @info = (
277 "node\toffset\tlimit\tpath",
278 "----\t------\t-----\t----",
279 "here\t$offset\t$limit\t$path",
280 );
281
282 send_nodes 'info';
283 push @info, get_node $_ foreach @nodes;
284
285 print "$_\n" foreach @info;
286
287 } elsif ( $cmd =~ m{^(q(uit)|e(xit))}i ) {
288 warn "# exit";
289 send_nodes 'exit';
290 exit;
291 } else {
292 run_views;
293 }
294
295 }
296

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26