/[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

Diff of /trunk/lib/Sack/Server.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/experiments/protocol-v3/server.pl revision 173 by dpavlin, Tue Nov 3 17:28:57 2009 UTC trunk/lib/Sack/Server.pm revision 222 by dpavlin, Sun Nov 22 22:57:15 2009 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2    
3    package Sack::Server;
4    
5  use warnings;  use warnings;
6  use strict;  use strict;
7    
# Line 9  use IO::Select; Line 11  use IO::Select;
11  use Data::Dump qw(dump);  use Data::Dump qw(dump);
12  use Storable qw();  use Storable qw();
13  use File::Slurp;  use File::Slurp;
14    use Cwd qw(abs_path);
15    
16  my @cloud = qw(localhost tab.lan llin.lan);  use lib '/srv/Sack/lib';
17    use Sack::Merge;
18  my $cloud_path = $ENV{CLOUD} || '/srv/Sack/etc/lib';  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;  @cloud = read_file $cloud_path;
25  @cloud = map { chomp $_; $_ } @cloud;  @cloud = map { chomp $_; $_ } @cloud;
26    
27  warn "# cloud ",dump( @cloud );  warn "# cloud ",dump( @cloud );
28    
29  my $listen_port = 4444;  my $listen_port = 4444;
30    my $http_port   = 4480;
31    
32  my $node_path = $0;  my $node_path = abs_path $0;
33  $node_path =~ s{server.pl}{client.pl};  $node_path =~ s{Server}{Client};
34    
35  my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => $listen_port, Reuse => 1) or die $!;  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);  my $sel = IO::Select->new($lsn);
38    $sel->add( $www );
39    
40  my $info;  my $info;
41  sub info {  sub info {
42          my $port = shift;          my $port = shift;
43          push @{ $info->{$port} }, [ @_ ];          push @{ $info->{node}->{$port} }, [ @_ ];
44  }  }
45    
46  sub fork_node {  sub fork_ssh {
47          my ( $port, $host ) = @_;          my ( $port, $host ) = @_;
48    
49          if ( my $pid = fork ) {          if ( my $pid = fork ) {
# Line 54  sub fork_node { Line 65  sub fork_node {
65  my $node_port = 4000;  my $node_port = 4000;
66    
67  foreach my $host ( @cloud ) {  foreach my $host ( @cloud ) {
68          system "echo $node_path | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional --verbose";          system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional";
69          fork_node( $node_port++, $host );          fork_ssh( $node_port++, $host );
70  }  }
71    
72  my $session;  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    our @shard_load_queue;
83    sub load_shard {
84            my $shard = shift @_ || return;
85    
86            warn "# load_shard $shard\n";
87    
88            my @shards = glob "$shard/*";
89    
90            if ( ! @shards ) {
91                    warn "no shards for $shard\n";
92                    return;
93            }
94    
95            $info->{shard}->{$_} = 'wait' foreach @shards;
96            warn "loading shard $shard from ", dump( @shards );
97    
98            push @shard_load_queue, @shards;
99            to_all { load => $shard };
100    }
101    
102  while (1) {  while (1) {
103          for my $sock ($sel->can_read(1)) {          for my $sock ($sel->can_read(1)) {
104                  if ($sock == $lsn) {                  if ($sock == $lsn) {
105                          my $new = $lsn->accept;                          my $new = $lsn->accept;
106                          $sel->add($new);                          $sel->add($new);
107                          $session->{$new} = $new->peerport;                          $session->{peerport}->{ $new->peerport } = $new;
108                          warn "[socket] connect\n";                          warn "[socket] connect\n";
109                          Storable::store_fd( { ping => 1 }, $new );                          Storable::store_fd( { ping => 1 }, $new );
110                          info 0 => 'ping', $new->peerport;                  } elsif ( $sock == $www ) {
111                            my $client = $www->accept;
112                            Sack::Server::HTTP::request( $client, sub {
113                                    my ( $send, $method, $param ) = @_;
114    
115                                    if ( $method =~ m{views} ) {
116                                            warn "$method ", -s $method, " bytes";
117                                            my $code = read_file $method;
118                                            Sack::Merge->clean;
119                                            to_all { view => $code, path => $method };
120                                            print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
121                                            return 1;
122                                    } elsif ( $method =~ m{^/tmp/sack} ) {
123                                            load_shard $method;
124                                            print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
125                                            return 1;
126                                    } elsif ( $method =~ m{^/out/(.+)} ) {
127                                            print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
128                                            Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param );
129                                            return 1;
130                                    }
131    
132                                    print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
133    
134                                    print $send qq|<h1>Views</h1><ul>|
135                                            , join("\n", map { qq|<li><a href="$_">$_</a>| } glob '/srv/Sack/views/*/*.pl' )
136                                            , qq|</ul>|
137                                            ;
138    
139                                    my $out = Sack::Merge->out;    
140                                    print $send qq|<h1>Results</h1><ul>|
141                                            , join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out )
142                                            , qq|</ul>|
143                                            ;
144    
145                                    print $send qq|<h1>Nodes</h1>
146    <style type="text/css">
147    .forked {
148            color: #f00;
149    }
150    
151    .ping {
152            color: #0f0;
153    }
154    
155    .shards {
156            font-family: monospace;
157            line-height: 0.8em;
158            color: #0f0;
159    }
160    
161    .send {
162            color: #ff0;
163    }
164    
165    .wait {
166            color: #f00;
167    }
168    
169    
170    </style>
171                                    |, join("\n", map {
172                                            my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} });
173                                            qq|<span class="$class">$_</span>|;
174                                    } sort keys %{ $info->{node} } );
175    
176                                    print $send qq|<h1>Data</h1><ul>|;
177                                    foreach my $path ( glob '/tmp/sack/*' ) {
178                                            print $send qq|<li><a href="$path">$path</a><div class="shards">|;
179                                            foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) {
180                                                    my $i = $info->{shard}->{$s};
181                                                    print $send qq|<span class=$i>$i</span> |;
182                                            }
183                                            print $send qq|</div>|;
184                                    }
185                                    print $send qq|</ul>|;
186    
187    #                               print $send '<pre>', dump($info), '</pre>';
188    
189                            } );
190                  } else {                  } else {
191                          my $data = eval { Storable::fd_retrieve( $sock ) };                          my $data = eval { Storable::fd_retrieve( $sock ) };
192                          if ( $@ ) {                          if ( $@ ) {
# Line 78  while (1) { Line 196  while (1) {
196                                  $sock->close;                                  $sock->close;
197                          } else {                          } else {
198                                  warn "<<<< ", dump($data), $/;                                  warn "<<<< ", dump($data), $/;
199                                  if ( $data->{repl} ) {  
200                                          my $response = { repl => $$, info => $info };                                  if ( my $path = $data->{shard} ) {
201                                            $info->{shard}->{ $path } = $data->{port};
202                                            # FIXME will need push for multiple copies of shards
203                                    }
204    
205                                    if ( my $repl = $data->{repl} ) {
206                                            my $response = { repl_pid => $$ };
207                                            if ( $repl =~ m/ping/ ) {
208                                                    to_all { ping => 1 };
209                                            } elsif ( $repl =~ m/info/ ) {
210                                                    $response->{info} = $info;
211                                            } elsif ( $repl =~ m{load\s*(\S+)?} ) {
212                                                    load_shard $1;
213                                            } elsif ( $repl =~ m{view\s*(\S+)?} ) {
214                                                    my $path = $1 || '/srv/Sack/views/00.demo.pl';
215                                                    my $code = read_file $path;
216                                                    Sack::Merge->clean;
217                                                    to_all { view => $code, path => $path };
218                                                    $response->{view}->{$path}->{running};
219                                            } elsif ( $repl =~ m{debug\s*(.+)?} ) {
220                                                    to_all { debug => $1 };
221                                            } elsif ( $repl =~ m{out} ) {
222                                                    my $out = Sack::Merge->out;    
223                                                    warn "out ",dump( $out );
224                                                    $response->{out} = $out;
225                                            } elsif ( $repl =~ m{clean} ) {
226                                                    delete $info->{shard};
227                                                    to_all { clean => 1 };
228                                            } else {
229                                                    $response->{error}->{unknown} = $data;
230                                            }
231                                          Storable::store_fd( $response, $sock );                                          Storable::store_fd( $response, $sock );
232                                  } elsif ( $data->{ping} ) {                                  } elsif ( $data->{ping} ) {
233                                          $info->{_peer_port}->{$sock->peerport} = $data->{port};                                          my $port = $data->{port};
234                                          info $data->{port} => 'peer port', $sock->peerport;                                          info $port => 'ping', $port;
235                                            $session->{port}->{ $data->{port} } = $sock;
236                                    } elsif ( $data->{load} eq 'shard' ) {
237                                            if ( my $path = shift @shard_load_queue ) {
238                                                    $info->{shard}->{$path} = 'read';
239                                                    my $shard = Storable::retrieve $path;
240                                                    $info->{shard}->{$path} = 'send';
241                                                    warn ">>>> [", $data->{port}, "] sending shard $path\n";
242                                                    Storable::store_fd( { path => $path, shard => $shard }, $sock );
243                                            } else {
244                                                    warn "no more shards for [", $data->{port}, "]\n";
245                                            }
246                                    } elsif ( defined $data->{out} ) {
247                                            Sack::Merge->add( $data->{out} );
248                                    } else {
249                                            warn "UNKNOWN ",dump($data);
250                                  }                                  }
251                          }                          }
252                  }                  }

Legend:
Removed from v.173  
changed lines
  Added in v.222

  ViewVC Help
Powered by ViewVC 1.1.26