/[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 264 - (show annotations)
Mon Feb 1 22:03:02 2010 UTC (14 years, 3 months ago) by dpavlin
File size: 9744 byte(s)
move Results to top of page (plays well with refresh)

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 sub info {
45 my $port = shift;
46 push @{ $info->{node}->{$port} }, [ @_ ];
47 }
48
49 sub fork_ssh {
50 my ( $port, $host ) = @_;
51
52 if ( my $pid = fork ) {
53 # parent
54 info $port => 'forked', $pid;
55 return $port;
56
57 } elsif ( ! defined $pid ) {
58 warn "can't fork $host $port";
59 return;
60 } else {
61 # child
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 system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional";
72 fork_ssh( $node_port++, $host );
73 }
74
75 my $session;
76
77 sub all_ports { keys %{ $session->{port} } }
78
79 sub to_all {
80 my $data = shift;
81 foreach my $port ( all_ports ) {
82 warn ">>>> [$port]\n" if $debug;
83 Storable::store_fd( $data, $session->{port}->{$port} );
84 }
85 }
86
87 our @shard_load_queue;
88 sub load_shard {
89 my $shard = shift @_ || return;
90
91 warn "# load_shard $shard\n";
92
93 $shard .= '/*' if -d $shard;
94
95 my @shards = glob $shard;
96
97 if ( ! @shards ) {
98 warn "no shards for $shard\n";
99 return;
100 }
101
102 foreach my $s ( @shards ) {
103 if ( my $node = $info->{shard}->{$s} ) {
104 next if $node =~ m{^\d+$}; # not node number
105 }
106 $info->{shard}->{$s} = 'wait';
107 push @shard_load_queue, $s;
108 warn "queued $s for loading\n";
109 }
110 to_all { load => $shard };
111 $info->{pending}->{$_} = 'load' foreach all_ports;
112 }
113
114 sub run_view {
115 my ( $path ) = @_;
116 if ( ! -r $path ) {
117 warn "ERROR view $path: $!";
118 return;
119 }
120 my $code = read_file $path;
121 Sack::Merge->clean;
122 delete( $info->{view} );
123 delete( $info->{merge} );
124 delete( $info->{shard} );
125 $info->{pending}->{$_} = 'view' foreach all_ports;
126 to_all { code => $code, view => $path };
127 };
128
129 our @responses;
130
131 while (1) {
132 for my $sock ($sel->can_read(1)) {
133 if ($sock == $lsn) {
134 my $new = $lsn->accept;
135 $sel->add($new);
136 $session->{peerport}->{ $new->peerport } = $new;
137 warn "[socket] connect\n";
138 Storable::store_fd( { ping => 1 }, $new );
139 } elsif ( $sock == $www ) {
140 my $client = $www->accept;
141 Sack::Server::HTTP::request( $client, sub {
142 my ( $send, $method, $param ) = @_;
143
144 if ( $method =~ m{views} ) {
145 run_view $method;
146 print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
147 return 1;
148 } elsif ( $method =~ m{^/tmp/sack} ) {
149 load_shard $method;
150 print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
151 return 1;
152 } elsif ( $method =~ m{^/out/(.+)} ) {
153 print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
154 Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param );
155 return 1;
156 } elsif ( $method =~ m{^/gnuplot} ) {
157 eval {
158 my $path = Sack::Server::Gnuplot::date( Sack::Merge->out, $param );
159 if ( -e $path ) {
160 print $send "HTTP/1.0 200 OK\r\nContent-Type: image/png\r\n\r\n";
161 open(my $fh, '<', $path) || die $path;
162 my $b;
163 while ( read($fh, $b, 4096) ) {
164 print $send $b;
165 }
166 return 1;
167 } else {
168 print $send "HTTP/1.0 404 no graph\r\n\r\n";
169 return 1;
170 }
171 };
172 warn "ERROR: $@" if $@;
173 }
174
175 my $refresh = $param->{refresh};
176 $refresh = 1 if keys %{ $info->{pending} };
177
178 print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html"
179 , ( $refresh ? "\r\nRefresh: $refresh" : '' )
180 , "\r\n\r\n"
181 , ( $refresh ? qq|<span style="color: #888; float:right">$refresh</span>| : '' )
182 ;
183
184 print $send qq|
185
186 <style type="text/css">
187
188 .running {
189 color: #f00;
190 }
191
192
193 .forked {
194 // text-decoration: line-through;
195 }
196
197 .ping {
198 color: #ff0;
199 }
200
201 .load {
202 color: #f00;
203 }
204
205 .view {
206 color: #00f;
207 }
208
209 .ready {
210 color: #0f0;
211 }
212
213 .shards {
214 font-family: monospace;
215 // line-height: 0.8em;
216 color: #0f0;
217 }
218
219 .send {
220 color: #ff0;
221 }
222
223 .wait {
224 color: #f00;
225 }
226
227
228 </style>
229
230 |;
231
232 my $out = Sack::Merge->out;
233 if ( my $li = join("\n", map { qq|<li><input type=checkbox name=filter value="$_"><a href="/out/$_" target="$_">$_</a>| } keys %$out ) ) {
234 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|;
235 }
236
237 print $send qq|<h1>Views</h1><ul>|
238 , join("\n", map {
239 my $view = $_;
240 my $html = '';
241 if ( my $s = $info->{view}->{$view} ) {
242 my @nodes = sort keys %$s;
243 my $total = 0;
244 $html .= qq|<table><tr><th>node</th><th>rows</th><th>shards</th><th>&sum;</th><th>merge</th></tr>|;
245 foreach my $node ( @nodes ) {
246 my $h = '';
247 my $shard = 0;
248 foreach my $path ( sort keys %{ $s->{$node} } ) {
249 my $affected = $s->{$node}->{$path};
250 my $n = '?';
251 $n = $1 if $path =~ m{/(\w)\w+/\d+};
252 my $class = $affected == 0 ? 'style="color: #888"' : '';
253 $h .= qq|<tt title="$affected $path"$class>$n</tt>|;
254 $shard += $affected;
255 $total += $affected;
256 }
257 my $merge = $info->{merge}->{$view}->{$node} || '?';
258 $html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td><td>$merge</td></tr>\n|;
259 }
260
261 $html .= qq|</table>|;
262 };
263 qq|<li><a href="$view">$view</a>$html|
264 } glob '/srv/Sack/views/*/*.pl' )
265 , qq|</ul>|
266 ;
267
268 print $send qq|<h1>Nodes</h1>|;
269 foreach my $node ( keys %{ $info->{node} } ) {
270 my $class = join(' '
271 , map { $_->[0] } @{ $info->{node}->{$node} }
272 );
273 $class .= ' ' . ( $info->{pending}->{$node} || 'ready' );
274 print $send qq|<tt class="$class">$node</tt>\n|;
275 }
276
277 print $send qq|<h1>Shards</h1><ul>|;
278 foreach my $path ( glob '/tmp/sack/*' ) {
279 print $send qq|<li><a href="$path">$path</a><div class="shards">|;
280 foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) {
281 my $i = $info->{shard}->{$s};
282 print $send qq|<span class="$i" title="$s">$i</span> |;
283 }
284 print $send qq|</div>|;
285 }
286 print $send qq|</ul>|;
287
288 if ( $param->{info} ) {
289 print $send qq|<a href="?info=0">hide info</a>|;
290 print $send '<pre>', dump($info), '</pre>'
291 } else {
292 print $send qq|<a href="?info=1">show info</a>|;
293 }
294 return 1;
295 } );
296 } else {
297 my $data = eval { Storable::fd_retrieve( $sock ) };
298 if ( $@ ) {
299 delete $session->{$sock};
300 warn "[socket] disconnect: $@\n";
301 $sel->remove($sock);
302 $sock->close;
303 } else {
304 warn "<<<< ", dump($data), $/ if $debug;
305
306 if ( my $path = $data->{shard} ) {
307 $info->{shard}->{ $path } = $data->{port};
308 # FIXME will need push for multiple copies of shards
309 }
310
311 if ( my $repl = $data->{repl} ) {
312 my $response = { repl_pid => $$ };
313 if ( $repl =~ m/ping/ ) {
314 to_all { ping => 1 };
315 } elsif ( $repl =~ m/info/ ) {
316 $response->{info} = $info;
317 } elsif ( $repl =~ m{load\s*(\S+)?} ) {
318 load_shard $1;
319 } elsif ( $repl =~ m{view\s*(\S+)?} ) {
320 run_view $1;
321 } elsif ( $repl =~ m{out} ) {
322 my $out = Sack::Merge->out;
323 warn "out ",dump( $out );
324 $response->{out} = $out;
325 } elsif ( $repl =~ m{clean} ) {
326 delete $info->{shard};
327 to_all { clean => 1 };
328 } elsif ( $repl eq 'exit' ) {
329 to_all { exit => 1 };
330 sleep 1;
331 exit;
332 } elsif ( $repl eq '.' ) {
333 $response->{'.'} = [ @responses ];
334 @responses = ();
335 } elsif ( $repl =~ m{(\w+)\s*(.+)?} ) {
336 to_all { $1 => $2 };
337 } else {
338 $response->{error}->{repl} = $repl;
339 }
340 Storable::store_fd( $response, $sock );
341 } elsif ( $data->{ping} ) {
342 my $port = $data->{port};
343 info $port => 'ping', $port;
344 $session->{port}->{ $data->{port} } = $sock;
345 } elsif ( defined $data->{load} && $data->{load} eq 'shard' ) {
346 if ( my $path = shift @shard_load_queue ) {
347 $info->{shard}->{$path} = 'read';
348 my $shard = Storable::retrieve $path;
349 $info->{shard}->{$path} = 'send';
350 warn ">>>> [", $data->{port}, "] sending shard $path\n";
351 Storable::store_fd( { path => $path, shard => $shard }, $sock );
352 } else {
353 warn "no more shards for [", $data->{port}, "]\n";
354 delete $info->{pending}->{ $data->{port} };
355 }
356 } elsif ( exists $data->{out} ) {
357 my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out};
358 $info->{merge}->{ $data->{view} }->{ $data->{port} } = $added;
359 $info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard};
360 # refresh shard allocation
361 $info->{shard}->{ $_ } = $data->{port} foreach keys %{ $data->{on_shard} };
362 delete $info->{pending}->{ $data->{port} };
363 } elsif ( exists $data->{port} ) {
364 push @responses, $data;
365 warn "# ",dump($data),$/;
366 } else {
367 warn "UNKNOWN ",dump($data);
368 }
369 }
370 }
371 }
372 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26