/[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 262 - (show annotations)
Sun Jan 31 16:44:56 2010 UTC (14 years, 4 months ago) by dpavlin
File size: 9172 byte(s)
send Refresh: header

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26