/[Sack]/trunk/lib/Sack/Server.pm
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/lib/Sack/Server.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 270 - (show annotations)
Mon Feb 15 09:40:12 2010 UTC (14 years, 3 months ago) by dpavlin
File size: 10906 byte(s)
sort nodes in display

1 #!/usr/bin/perl
2
3 package Sack::Server;
4
5 use warnings;
6 use strict;
7
8 my $debug = 0;
9
10 use IO::Socket::INET;
11 use IO::Select;
12
13 use Data::Dump qw(dump);
14 use Storable qw();
15 use File::Slurp;
16 use Cwd qw(abs_path);
17
18 use lib '/srv/Sack/lib';
19 use Sack::Merge;
20 use Sack::Server::HTTP;
21 use Sack::Server::HTML;
22 use Sack::Server::Gnuplot;
23
24 my @cloud;
25 my $cloud_path = $ENV{CLOUD} || "etc/cloud";
26 die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n" unless -e $cloud_path;
27 @cloud = read_file $cloud_path;
28 @cloud = map { chomp $_; $_ } @cloud;
29
30 warn "# cloud ",dump( @cloud );
31
32 my $listen_port = 4444;
33 my $http_port = 4480;
34
35 my $node_path = abs_path $0;
36 $node_path =~ s{Server}{Client};
37
38 my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die "$listen_port $!";
39 my $www = IO::Socket::INET->new(Listen => 1, LocalPort => $http_port, Reuse => 1) or die "$http_port $!";
40 my $sel = IO::Select->new($lsn);
41 $sel->add( $www );
42
43 my $info;
44
45 sub fork_ssh {
46 my ( $port, $host ) = @_;
47
48 if ( my $pid = fork ) {
49 # parent
50 $info->{forked}->{$port} = $pid;
51 $info->{pending}->{$port} = 'forked';
52 return $port;
53
54 } elsif ( ! defined $pid ) {
55 warn "can't fork $host $port";
56 return;
57 } else {
58 # child
59 warn "[$port] cpio last version\n";
60 system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional";
61
62 my $cmd = qq|ssh -F $cloud_path.ssh -R $port:127.0.0.1:$listen_port $host $node_path $port|;
63 warn "# exec: $cmd\n";
64 exec $cmd;
65 }
66 }
67
68 my $node_port = 4000;
69
70 foreach my $host ( @cloud ) {
71 fork_ssh( $node_port++, $host );
72 }
73
74 my $session;
75
76 sub all_ports { keys %{ $session->{port} } }
77
78 sub to_all {
79 my $data = shift;
80 foreach my $port ( all_ports ) {
81 warn ">>>> [$port]\n" if $debug;
82 Storable::store_fd( $data, $session->{port}->{$port} );
83 }
84 }
85
86 our @shard_load_queue;
87 sub load_shard {
88 my $shard = shift @_ || return;
89
90 warn "# load_shard $shard\n";
91
92 $shard .= '/*' if -d $shard;
93
94 my @shards = glob $shard;
95
96 if ( ! @shards ) {
97 warn "no shards for $shard\n";
98 return;
99 }
100
101 foreach my $s ( @shards ) {
102 if ( my $node = $info->{shard}->{$s} ) {
103 next if $node =~ m{^\d+$}; # not node number
104 }
105 $info->{shard}->{$s} = 'wait';
106 push @shard_load_queue, $s;
107 warn "queued $s for loading\n";
108 }
109
110 # XXX depriciated but not removed yet
111 if ( 0 ) {
112 to_all { load => $shard };
113 $info->{pending}->{$_} = 'load' foreach all_ports;
114 return;
115 }
116
117 my @nodes = all_ports;
118 my $chunk = int( ( $#shard_load_queue + 1 ) / $#nodes );
119
120 my $pid_port;
121
122 foreach my $port ( all_ports ) {
123 $info->{pending}->{$port} = 'load';
124 my @shards = splice @shard_load_queue, 0, $chunk;
125
126 my $sock = $session->{port}->{$port};
127
128 if ( my $pid = fork ) {
129 $pid_port->{$pid} = $port;
130 # parent
131 } elsif ( ! defined $pid ) {
132 die "can't fork $!";
133 } else {
134 # child
135
136 warn ">>>> [$port] bulk_load ", $#shards + 1, " shards\n";
137 Storable::store_fd( { bulk_load => [ @shards ] }, $sock );
138 foreach my $s ( @shards ) {
139 warn ">>>> [$port] bulk_load $s\n";
140 Storable::store_fd( Storable::retrieve( $s ), $sock );
141 }
142 warn ">>>> [$port] bulk_load finished\n";
143 exit;
144 }
145
146 }
147
148 foreach my $child ( keys %$pid_port ) {
149 warn "waitpid $child\n";
150 waitpid($child, 0);
151 delete( $info->{pending}->{ $pid_port->{$child} } );
152 }
153
154 $info->{pending}->{$_} = 'view' foreach all_ports;
155 to_all { code => "# collect back loaded shards\n", view => 'noop' };
156 }
157
158 sub run_view {
159 my ( $path ) = @_;
160 if ( ! -r $path ) {
161 warn "ERROR view $path: $!";
162 return;
163 }
164 my $code = read_file $path;
165 Sack::Merge->clean;
166 delete( $info->{view} );
167 delete( $info->{merge} );
168 delete( $info->{shard} );
169 $info->{pending}->{$_} = 'view' foreach all_ports;
170 to_all { code => $code, view => $path };
171 };
172
173 our @responses;
174
175 while (1) {
176 for my $sock ($sel->can_read(1)) {
177 if ($sock == $lsn) {
178 my $new = $lsn->accept;
179 $sel->add($new);
180 $session->{peerport}->{ $new->peerport } = $new;
181 warn "[socket] connect\n";
182 Storable::store_fd( { ping => 1 }, $new );
183 } elsif ( $sock == $www ) {
184 my $client = $www->accept;
185 Sack::Server::HTTP::request( $client, sub {
186 my ( $send, $method, $param ) = @_;
187
188 if ( $method =~ m{views} ) {
189 run_view $method;
190 print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
191 return 1;
192 } elsif ( $method =~ m{^/tmp/sack} ) {
193 load_shard $method;
194 print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
195 return 1;
196 } elsif ( $method =~ m{^/out/(.+)} ) {
197 print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
198 Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param );
199 return 1;
200 } elsif ( $method =~ m{^/gnuplot} ) {
201 eval {
202 my $path = Sack::Server::Gnuplot::date( Sack::Merge->out, $param );
203 if ( -e $path ) {
204 print $send "HTTP/1.0 200 OK\r\nContent-Type: image/png\r\n\r\n";
205 open(my $fh, '<', $path) || die $path;
206 my $b;
207 while ( read($fh, $b, 4096) ) {
208 print $send $b;
209 }
210 return 1;
211 } else {
212 print $send "HTTP/1.0 404 no graph\r\n\r\n";
213 return 1;
214 }
215 };
216 warn "ERROR: $@" if $@;
217 }
218
219 my $refresh = $param->{refresh};
220 $refresh = 1 if grep { $info->{pending}->{$_} ne 'forked' } keys %{ $info->{pending} };
221 # we don't care about refresh is nodes are stuck in forked state!
222
223 print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html"
224 , ( $refresh ? "\r\nRefresh: $refresh" : '' )
225 , "\r\n\r\n"
226 , ( $refresh ? qq|<span style="color: #888; float:right">$refresh</span>| : '' )
227 ;
228
229 print $send qq|
230
231 <style type="text/css">
232
233 .running {
234 color: #f00;
235 }
236
237
238 .forked {
239 color: #888;
240 }
241
242 .ping {
243 color: #0f0;
244 }
245
246 .load {
247 color: #f00;
248 }
249
250 .view {
251 color: #00f;
252 }
253
254 .shards {
255 font-family: monospace;
256 // line-height: 0.8em;
257 color: #0f0;
258 }
259
260 .send {
261 color: #ff0;
262 }
263
264 .wait {
265 color: #f00;
266 }
267
268
269 </style>
270
271 |;
272
273 my $out = Sack::Merge->out;
274 if ( my $li = join("\n", map { qq|<li><input type=checkbox name=filter value="$_"><a href="/out/$_" target="$_">$_</a>| } keys %$out ) ) {
275 print $send qq|<h1>Results</h1><form method="get" target="gnuplot" action="/gnuplot/"><ul>$li</ul><input type=submit value="Show checked"></form><img src="/gnuplot/">\n|;
276 }
277
278 print $send qq|<h1>Views</h1><ul>|
279 , join("\n", map {
280 my $view = $_;
281 my $html = '';
282 if ( my $s = $info->{view}->{$view} ) {
283 my @nodes = sort keys %$s;
284 my $total = 0;
285 $html .= qq|<table><tr><th>node</th><th>rows</th><th>shards</th><th>&sum;</th><th>merge</th></tr>|;
286 foreach my $node ( @nodes ) {
287 my $h = '';
288 my $shard = 0;
289 foreach my $path ( sort keys %{ $s->{$node} } ) {
290 my $affected = $s->{$node}->{$path};
291 my $n = '?';
292 $n = $1 if $path =~ m{/(\w)\w+/\d+};
293 my $class = $affected == 0 ? 'style="color: #888"' : '';
294 $h .= qq|<tt title="$affected $path"$class>$n</tt>|;
295 $shard += $affected;
296 $total += $affected;
297 }
298 my $merge = $info->{merge}->{$view}->{$node} || '?';
299 $html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td><td>$merge</td></tr>\n|;
300 }
301
302 $html .= qq|</table>|;
303 };
304 qq|<li><a href="$view">$view</a>$html|
305 } glob '/srv/Sack/views/*/*.pl' )
306 , qq|</ul>|
307 ;
308
309 print $send qq|<h1>Nodes</h1>|;
310 foreach my $node ( sort keys %{ $info->{forked} } ) {
311 my $attr = '';
312 if ( my $pending = $info->{pending}->{$node} ) {
313 $attr .= qq| class="$pending"|;
314 }
315 print $send qq|<tt$attr>$node</tt>\n|;
316 }
317
318 print $send qq|<h1>Shards</h1><ul>|;
319 foreach my $path ( glob '/tmp/sack/*' ) {
320 print $send qq|<li><a href="$path">$path</a><div class="shards">|;
321 foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) {
322 my $i = $info->{shard}->{$s};
323 print $send qq|<span class="$i" title="$s">$i</span> |;
324 }
325 print $send qq|</div>|;
326 }
327 print $send qq|</ul>|;
328
329 if ( $param->{info} ) {
330 print $send qq|<a href="?info=0">hide info</a>|;
331 print $send '<pre>', dump($info), '</pre>'
332 } else {
333 print $send qq|<a href="?info=1">show info</a>|;
334 }
335 return 1;
336 } );
337 } else {
338 my $data = eval { Storable::fd_retrieve( $sock ) };
339 if ( $@ ) {
340 delete $session->{$sock};
341 warn "[socket] disconnect: $@\n";
342 $sel->remove($sock);
343 $sock->close;
344 } else {
345 warn "<<<< ", dump($data), $/ if $debug;
346
347 if ( my $path = $data->{shard} ) {
348 $info->{shard}->{ $path } = $data->{port};
349 # FIXME will need push for multiple copies of shards
350 }
351
352 if ( my $repl = $data->{repl} ) {
353 my $response = { repl_pid => $$ };
354 if ( $repl =~ m/ping/ ) {
355 to_all { ping => 1 };
356 } elsif ( $repl =~ m/info/ ) {
357 $response->{info} = $info;
358 } elsif ( $repl =~ m{load\s*(\S+)?} ) {
359 load_shard $1;
360 } elsif ( $repl =~ m{view\s*(\S+)?} ) {
361 run_view $1;
362 } elsif ( $repl =~ m{out} ) {
363 my $out = Sack::Merge->out;
364 warn "out ",dump( $out );
365 $response->{out} = $out;
366 } elsif ( $repl =~ m{clean} ) {
367 delete $info->{shard};
368 to_all { clean => 1 };
369 } elsif ( $repl eq 'exit' ) {
370 to_all { exit => 1 };
371 sleep 1;
372 exit;
373 } elsif ( $repl eq '.' ) {
374 $response->{'.'} = [ @responses ];
375 @responses = ();
376 } elsif ( $repl =~ m{(\w+)\s*(.+)?} ) {
377 to_all { $1 => $2 };
378 } else {
379 $response->{error}->{repl} = $repl;
380 }
381 Storable::store_fd( $response, $sock );
382 } elsif ( $data->{ping} ) {
383 my $port = $data->{port};
384 delete( $info->{pending}->{$port} ) if $info->{pending}->{$port} eq 'forked';
385 $session->{port}->{ $data->{port} } = $sock;
386 } elsif ( defined $data->{load} && $data->{load} eq 'shard' ) {
387 if ( my $path = shift @shard_load_queue ) {
388 $info->{shard}->{$path} = 'read';
389 my $shard = Storable::retrieve $path;
390 $info->{shard}->{$path} = 'send';
391 warn ">>>> [", $data->{port}, "] sending shard $path\n";
392 Storable::store_fd( { path => $path, shard => $shard }, $sock );
393 } else {
394 warn "no more shards for [", $data->{port}, "]\n";
395 delete $info->{pending}->{ $data->{port} };
396 }
397 } elsif ( exists $data->{out} ) {
398 my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out};
399 $info->{merge}->{ $data->{view} }->{ $data->{port} } = $added;
400 $info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard};
401 # refresh shard allocation
402 $info->{shard}->{ $_ } = $data->{port} foreach keys %{ $data->{on_shard} };
403 delete $info->{pending}->{ $data->{port} };
404 } elsif ( exists $data->{port} ) {
405 push @responses, $data;
406 warn "# ",dump($data),$/;
407 } else {
408 warn "UNKNOWN ",dump($data);
409 }
410 }
411 }
412 }
413 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26