/[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 235 - (show annotations)
Tue Nov 24 00:46:19 2009 UTC (14 years, 6 months ago) by dpavlin
File size: 7996 byte(s)
display number of merged keys from nodes

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
23 my @cloud;
24 my $cloud_path = $ENV{CLOUD} || "etc/cloud";
25 die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n" unless -e $cloud_path;
26 @cloud = read_file $cloud_path;
27 @cloud = map { chomp $_; $_ } @cloud;
28
29 warn "# cloud ",dump( @cloud );
30
31 my $listen_port = 4444;
32 my $http_port = 4480;
33
34 my $node_path = abs_path $0;
35 $node_path =~ s{Server}{Client};
36
37 my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die "$listen_port $!";
38 my $www = IO::Socket::INET->new(Listen => 1, LocalPort => $http_port, Reuse => 1) or die "$http_port $!";
39 my $sel = IO::Select->new($lsn);
40 $sel->add( $www );
41
42 my $info;
43 sub info {
44 my $port = shift;
45 push @{ $info->{node}->{$port} }, [ @_ ];
46 }
47
48 sub fork_ssh {
49 my ( $port, $host ) = @_;
50
51 if ( my $pid = fork ) {
52 # parent
53 info $port => 'forked', $pid;
54 return $port;
55
56 } elsif ( ! defined $pid ) {
57 warn "can't fork $host $port";
58 return;
59 } else {
60 # child
61 my $cmd = qq|ssh -F $cloud_path.ssh -R $port:127.0.0.1:$listen_port $host $node_path $port|;
62 warn "# exec: $cmd\n";
63 exec $cmd;
64 }
65 }
66
67 my $node_port = 4000;
68
69 foreach my $host ( @cloud ) {
70 system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional";
71 fork_ssh( $node_port++, $host );
72 }
73
74 my $session;
75
76 sub to_all {
77 my $data = shift;
78 foreach my $port ( keys %{ $session->{port} } ) {
79 warn ">>>> [$port]\n" if $debug;
80 Storable::store_fd( $data, $session->{port}->{$port} );
81 }
82 }
83
84 our @shard_load_queue;
85 sub load_shard {
86 my $shard = shift @_ || return;
87
88 warn "# load_shard $shard\n";
89
90 my @shards = glob "$shard/*";
91
92 if ( ! @shards ) {
93 warn "no shards for $shard\n";
94 return;
95 }
96
97 $info->{shard}->{$_} = 'wait' foreach @shards;
98 warn "loading shard $shard from ", dump( @shards );
99
100 push @shard_load_queue, @shards;
101 to_all { load => $shard };
102 }
103
104 sub run_view {
105 my ( $path ) = @_;
106 if ( ! -r $path ) {
107 warn "ERROR view $path: $!";
108 return;
109 }
110 my $code = read_file $path;
111 Sack::Merge->clean;
112 delete( $info->{view} );
113 delete( $info->{merge} );
114 to_all { code => $code, view => $path };
115 };
116
117 while (1) {
118 for my $sock ($sel->can_read(1)) {
119 if ($sock == $lsn) {
120 my $new = $lsn->accept;
121 $sel->add($new);
122 $session->{peerport}->{ $new->peerport } = $new;
123 warn "[socket] connect\n";
124 Storable::store_fd( { ping => 1 }, $new );
125 } elsif ( $sock == $www ) {
126 my $client = $www->accept;
127 Sack::Server::HTTP::request( $client, sub {
128 my ( $send, $method, $param ) = @_;
129
130 if ( $method =~ m{views} ) {
131 run_view $method;
132 print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
133 return 1;
134 } elsif ( $method =~ m{^/tmp/sack} ) {
135 load_shard $method;
136 print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
137 return 1;
138 } elsif ( $method =~ m{^/out/(.+)} ) {
139 print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
140 Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param );
141 return 1;
142 }
143
144 print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
145
146 print $send qq|
147
148 <style type="text/css">
149
150 .running {
151 color: #f00;
152 }
153
154
155 .forked {
156 color: #f00;
157 }
158
159 .ping {
160 color: #0f0;
161 }
162
163 .shards {
164 font-family: monospace;
165 line-height: 0.8em;
166 color: #0f0;
167 }
168
169 .send {
170 color: #ff0;
171 }
172
173 .wait {
174 color: #f00;
175 }
176
177
178 </style>
179
180 |;
181
182 print $send qq|<h1>Views</h1><ul>|
183 , join("\n", map {
184 my $view = $_;
185 my $html = '';
186 if ( my $s = $info->{view}->{$view} ) {
187 my @nodes = sort keys %$s;
188 my $total = 0;
189 my $total_merged = 0;
190 $html .= qq|<table><tr><th>node</th><th>rows</th><th>shards</th><th>&sum;</th><th>merge</th><th>&sum;</th></tr>|;
191 foreach my $node ( @nodes ) {
192 my $h = '';
193 my $shard = 0;
194 foreach my $path ( sort keys %{ $s->{$node} } ) {
195 my $affected = $s->{$node}->{$path};
196 my $n = '?';
197 $n = $1 if $path =~ m{/(\w)\w+/\d+};
198 my $class = $affected == 0 ? 'style="color: #888"' : '';
199 $h .= qq|<tt title="$affected $path"$class>$n</tt>|;
200 $shard += $affected;
201 $total += $affected;
202 }
203 my $merge = $info->{merge}->{$view}->{$node};
204 $total_merged += $merge;
205 $html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td><td>$merge</td><td>$total_merged</td></tr>\n|;
206 }
207
208 $html .= qq|</table>|;
209 };
210 qq|<li><a href="$view">$view</a>$html|
211 } glob '/srv/Sack/views/*/*.pl' )
212 , qq|</ul>|
213 ;
214
215 my $out = Sack::Merge->out;
216 print $send qq|<h1>Results</h1><ul>|
217 , join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out )
218 , qq|</ul>|
219 ;
220
221 print $send qq|<h1>Nodes</h1>
222 |, join("\n", map {
223 my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} });
224 qq|<span class="$class">$_</span>|;
225 } sort keys %{ $info->{node} } );
226
227 print $send qq|<h1>Data</h1><ul>|;
228 foreach my $path ( glob '/tmp/sack/*' ) {
229 print $send qq|<li><a href="$path">$path</a><div class="shards">|;
230 foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) {
231 my $i = $info->{shard}->{$s};
232 print $send qq|<span class=$i>$i</span> |;
233 }
234 print $send qq|</div>|;
235 }
236 print $send qq|</ul>|;
237
238 if ( $param->{info} ) {
239 print $send qq|<a href="?info=0">hide info</a>|;
240 print $send '<pre>', dump($info), '</pre>'
241 } else {
242 print $send qq|<a href="?info=1">show info</a>|;
243 }
244 return 1;
245 } );
246 } else {
247 my $data = eval { Storable::fd_retrieve( $sock ) };
248 if ( $@ ) {
249 delete $session->{$sock};
250 warn "[socket] disconnect: $@\n";
251 $sel->remove($sock);
252 $sock->close;
253 } else {
254 warn "<<<< ", dump($data), $/ if $debug;
255
256 if ( my $path = $data->{shard} ) {
257 $info->{shard}->{ $path } = $data->{port};
258 # FIXME will need push for multiple copies of shards
259 }
260
261 if ( my $repl = $data->{repl} ) {
262 my $response = { repl_pid => $$ };
263 if ( $repl =~ m/ping/ ) {
264 to_all { ping => 1 };
265 } elsif ( $repl =~ m/info/ ) {
266 $response->{info} = $info;
267 } elsif ( $repl =~ m{load\s*(\S+)?} ) {
268 load_shard $1;
269 } elsif ( $repl =~ m{view\s*(\S+)?} ) {
270 run_view $1;
271 } elsif ( $repl =~ m{debug\s*(.+)?} ) {
272 to_all { debug => $1 };
273 } elsif ( $repl =~ m{out} ) {
274 my $out = Sack::Merge->out;
275 warn "out ",dump( $out );
276 $response->{out} = $out;
277 } elsif ( $repl =~ m{clean} ) {
278 delete $info->{shard};
279 to_all { clean => 1 };
280 } elsif ( $repl eq 'exit' ) {
281 to_all { exit => 1 };
282 sleep 1;
283 exit;
284 } else {
285 $response->{error}->{unknown} = $data;
286 }
287 Storable::store_fd( $response, $sock );
288 } elsif ( $data->{ping} ) {
289 my $port = $data->{port};
290 info $port => 'ping', $port;
291 $session->{port}->{ $data->{port} } = $sock;
292 } elsif ( defined $data->{load} && $data->{load} eq 'shard' ) {
293 if ( my $path = shift @shard_load_queue ) {
294 $info->{shard}->{$path} = 'read';
295 my $shard = Storable::retrieve $path;
296 $info->{shard}->{$path} = 'send';
297 warn ">>>> [", $data->{port}, "] sending shard $path\n";
298 Storable::store_fd( { path => $path, shard => $shard }, $sock );
299 } else {
300 warn "no more shards for [", $data->{port}, "]\n";
301 }
302 } elsif ( exists $data->{out} ) {
303 my $added = Sack::Merge->add( $data->{out} ) if defined $data->{out};
304 $info->{merge}->{ $data->{view} }->{ $data->{port} } = $added;
305 $info->{view }->{ $data->{view} }->{ $data->{port} } = $data->{on_shard};
306 } else {
307 warn "UNKNOWN ",dump($data);
308 }
309 }
310 }
311 }
312 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26