/[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 212 - (show annotations)
Sat Nov 21 16:34:12 2009 UTC (14 years, 5 months ago) by dpavlin
File size: 5338 byte(s)
load shards and run views over web

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26