/[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 213 - (show annotations)
Sun Nov 22 15:31:38 2009 UTC (14 years, 5 months ago) by dpavlin
File size: 5719 byte(s)
move table generation in separate package and add sorting

1 #!/usr/bin/perl
2
3 package Sack::Server;
4
5 use warnings;
6 use strict;
7
8 use IO::Socket::INET;
9 use IO::Select;
10
11 use Data::Dump qw(dump);
12 use Storable qw();
13 use File::Slurp;
14 use Cwd qw(abs_path);
15
16 use lib '/srv/Sack/lib';
17 use Sack::Merge;
18 use Sack::Server::HTTP;
19 use Sack::Server::HTML;
20
21 my @cloud;
22 my $cloud_path = $ENV{CLOUD} || "etc/cloud";
23 die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n" unless -e $cloud_path;
24 @cloud = read_file $cloud_path;
25 @cloud = map { chomp $_; $_ } @cloud;
26
27 warn "# cloud ",dump( @cloud );
28
29 my $listen_port = 4444;
30 my $http_port = 4480;
31
32 my $node_path = abs_path $0;
33 $node_path =~ s{Server}{Client};
34
35 my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die "$listen_port $!";
36 my $www = IO::Socket::INET->new(Listen => 1, LocalPort => $http_port, Reuse => 1) or die "$http_port $!";
37 my $sel = IO::Select->new($lsn);
38 $sel->add( $www );
39
40 my $info;
41 sub info {
42 my $port = shift;
43 push @{ $info->{node}->{$port} }, [ @_ ];
44 }
45
46 sub fork_ssh {
47 my ( $port, $host ) = @_;
48
49 if ( my $pid = fork ) {
50 # parent
51 info $port => 'forked', $pid;
52 return $port;
53
54 } elsif ( ! defined $pid ) {
55 warn "can't fork $host $port";
56 return;
57 } else {
58 # child
59 my $cmd = qq|ssh -F $cloud_path.ssh -R $port:127.0.0.1:$listen_port $host $node_path $port|;
60 warn "# exec: $cmd\n";
61 exec $cmd;
62 }
63 }
64
65 my $node_port = 4000;
66
67 foreach my $host ( @cloud ) {
68 system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional";
69 fork_ssh( $node_port++, $host );
70 }
71
72 my $session;
73
74 sub to_all {
75 my $data = shift;
76 foreach my $port ( keys %{ $session->{port} } ) {
77 warn ">>>> [$port]\n";
78 Storable::store_fd( $data, $session->{port}->{$port} );
79 }
80 }
81
82 my @shard_paths;
83
84 while (1) {
85 for my $sock ($sel->can_read(1)) {
86 if ($sock == $lsn) {
87 my $new = $lsn->accept;
88 $sel->add($new);
89 $session->{peerport}->{ $new->peerport } = $new;
90 warn "[socket] connect\n";
91 Storable::store_fd( { ping => 1 }, $new );
92 info 0 => 'ping', $new->peerport;
93 } elsif ( $sock == $www ) {
94 my $client = $www->accept;
95 Sack::Server::HTTP::request( $client, sub {
96 my ( $send, $method, $param ) = @_;
97
98 if ( $method =~ m{views} ) {
99 warn "$method ", -s $method, " bytes";
100 my $code = read_file $method;
101 Sack::Merge->clean;
102 to_all { view => $code, path => $method };
103 print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
104 return 1;
105 } elsif ( $method =~ m{^/tmp/sack} ) {
106 @shard_paths = glob "$method/*";
107 warn "loading shard $method from ", dump( @shard_paths );
108 to_all { load => $method };
109 print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
110 return 1;
111 } elsif ( $method =~ m{^/out/(.+)} ) {
112 print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
113 Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param );
114 return 1;
115 }
116
117 print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
118
119 print $send qq|<h1>Views</h1><ul>|
120 , join("\n", map { qq|<li><a href="$_">$_</a>| } glob '/srv/Sack/views/*/*.pl' )
121 , qq|</ul>|
122 ;
123
124 my $out = Sack::Merge->out;
125 print $send qq|<h1>Results</h1><ul>|
126 , join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out )
127 , qq|</ul>|
128 ;
129
130 print $send '<h1>Info</h1><pre>' . dump( $info ) . '</pre>';
131
132 print $send qq|<h1>Data</h1><ul>|
133 , join("\n", map { qq|<li><a href="$_">$_</a>| } glob '/tmp/sack/*' )
134 , qq|</ul>|
135 ;
136 } );
137 } else {
138 my $data = eval { Storable::fd_retrieve( $sock ) };
139 if ( $@ ) {
140 delete $session->{$sock};
141 warn "[socket] disconnect: $@\n";
142 $sel->remove($sock);
143 $sock->close;
144 } else {
145 warn "<<<< ", dump($data), $/;
146
147 if ( my $path = $data->{shard} ) {
148 $info->{shard}->{ $path } = $data->{port};
149 # FIXME will need push for multiple copies of shards
150 }
151
152 if ( my $repl = $data->{repl} ) {
153 my $response = { repl_pid => $$ };
154 if ( $repl =~ m/ping/ ) {
155 to_all { ping => 1 };
156 } elsif ( $repl =~ m/info/ ) {
157 $response->{info} = $info;
158 } elsif ( $repl =~ m{load\s*(\S+)?} ) {
159 my $name = $1 || 'shard';
160 @shard_paths = glob "/tmp/sack/$name/*";
161 warn "loading shard $name from ", dump( @shard_paths );
162 to_all { load => $name };
163 } elsif ( $repl =~ m{view\s*(\S+)?} ) {
164 my $path = $1 || '/srv/Sack/views/00.demo.pl';
165 my $code = read_file $path;
166 Sack::Merge->clean;
167 to_all { view => $code, path => $path };
168 $response->{view}->{$path}->{running};
169 } elsif ( $repl =~ m{debug\s*(.+)?} ) {
170 to_all { debug => $1 };
171 } elsif ( $repl =~ m{out} ) {
172 my $out = Sack::Merge->out;
173 warn "out ",dump( $out );
174 $response->{out} = $out;
175 } elsif ( $repl =~ m{clean} ) {
176 delete $info->{shard};
177 to_all { clean => 1 };
178 } else {
179 $response->{error}->{unknown} = $data;
180 }
181 Storable::store_fd( $response, $sock );
182 } elsif ( $data->{ping} ) {
183 my $port = $data->{port};
184 info $port => 'ping', $port;
185 $session->{port}->{ $data->{port} } = $sock;
186 } elsif ( $data->{load} eq 'shard' ) {
187 if ( my $path = shift @shard_paths ) {
188 warn "retrieve $path ", -s $path;
189 my $shard = Storable::retrieve $path;
190 warn ">>>> [", $data->{port}, "] sending shard $path\n";
191 Storable::store_fd( { path => $path, shard => $shard }, $sock );
192 } else {
193 warn "no more shards for [", $data->{port}, "]\n";
194 }
195 } elsif ( defined $data->{out} ) {
196 Sack::Merge->add( $data->{out} );
197 } else {
198 warn "UNKNOWN ",dump($data);
199 }
200 }
201 }
202 }
203 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26