/[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 170 by dpavlin, Tue Nov 3 17:19:12 2009 UTC trunk/lib/Sack/Server.pm revision 233 by dpavlin, Mon Nov 23 23:52:53 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    
8    my $debug = 0;
9    
10  use IO::Socket::INET;  use IO::Socket::INET;
11  use IO::Select;  use IO::Select;
12    
13  use Data::Dump qw(dump);  use Data::Dump qw(dump);
14  use Storable qw();  use Storable qw();
15  use File::Slurp;  use File::Slurp;
16    use Cwd qw(abs_path);
17    
18  my @cloud = qw(localhost tab.lan llin.lan);  use lib '/srv/Sack/lib';
19    use Sack::Merge;
20  my $cloud_path = $ENV{CLOUD} || '/srv/Sack/etc/lib';  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;  @cloud = read_file $cloud_path;
27  @cloud = map { chomp $_; $_ } @cloud;  @cloud = map { chomp $_; $_ } @cloud;
28    
29  warn "# cloud ",dump( @cloud );  warn "# cloud ",dump( @cloud );
30    
31  my $listen_port = 4444;  my $listen_port = 4444;
32    my $http_port   = 4480;
33    
34  my $node_path = '/tmp/client.pl';  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 $!;  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);  my $sel = IO::Select->new($lsn);
40    $sel->add( $www );
41    
42  my $info;  my $info;
43  sub info {  sub info {
44          my $port = shift;          my $port = shift;
45          push @{ $info->{$port} }, [ @_ ];          push @{ $info->{node}->{$port} }, [ @_ ];
46  }  }
47    
48  sub fork_node {  sub fork_ssh {
49          my ( $port, $host ) = @_;          my ( $port, $host ) = @_;
50    
51          if ( my $pid = fork ) {          if ( my $pid = fork ) {
# Line 53  sub fork_node { Line 67  sub fork_node {
67  my $node_port = 4000;  my $node_port = 4000;
68    
69  foreach my $host ( @cloud ) {  foreach my $host ( @cloud ) {
70          system "echo $node_path | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional";          system "find /srv/Sack/ | cpio --create --dereference | ssh -T -F $cloud_path.ssh $host cpio --extract --make-directories --unconditional";
71          fork_node( $node_port++, $host );          fork_ssh( $node_port++, $host );
72  }  }
73    
74  my $session;  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            to_all { code => $code, view => $path };
114    };
115    
116  while (1) {  while (1) {
117          for my $sock ($sel->can_read(1)) {          for my $sock ($sel->can_read(1)) {
118                  if ($sock == $lsn) {                  if ($sock == $lsn) {
119                          my $new = $lsn->accept;                          my $new = $lsn->accept;
120                          $sel->add($new);                          $sel->add($new);
121                          $session->{$new} = $new->peerport;                          $session->{peerport}->{ $new->peerport } = $new;
122                          warn "[socket] connect\n";                          warn "[socket] connect\n";
123                          Storable::store_fd( { ping => 1 }, $new );                          Storable::store_fd( { ping => 1 }, $new );
124                          info 0 => 'ping', $new->peerport;                  } elsif ( $sock == $www ) {
125                            my $client = $www->accept;
126                            Sack::Server::HTTP::request( $client, sub {
127                                    my ( $send, $method, $param ) = @_;
128    
129                                    if ( $method =~ m{views} ) {
130                                            run_view $method;
131                                            print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
132                                            return 1;
133                                    } elsif ( $method =~ m{^/tmp/sack} ) {
134                                            load_shard $method;
135                                            print $send "HTTP/1.0 302 $method\r\nLocation: /\r\n\r\n";
136                                            return 1;
137                                    } elsif ( $method =~ m{^/out/(.+)} ) {
138                                            print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
139                                            Sack::Server::HTML::send_out( $send, Sack::Merge->out, $1, $param );
140                                            return 1;
141                                    }
142    
143                                    print $send "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
144    
145                                    print $send qq|
146    
147    <style type="text/css">
148    
149    .running {
150            color: #f00;
151    }
152    
153    
154    .forked {
155            color: #f00;
156    }
157    
158    .ping {
159            color: #0f0;
160    }
161    
162    .shards {
163            font-family: monospace;
164            line-height: 0.8em;
165            color: #0f0;
166    }
167    
168    .send {
169            color: #ff0;
170    }
171    
172    .wait {
173            color: #f00;
174    }
175    
176    
177    </style>
178    
179    |;
180    
181                                    print $send qq|<h1>Views</h1><ul>|
182                                            , join("\n", map {
183                                                    my $view = $_;
184                                                    my $html = '';
185                                                    if ( my $s = $info->{view}->{$view} ) {
186                                                            my @nodes = sort keys %$s;
187                                                            my $total = 0;
188                                                            $html .= qq|<table><tr><th>node</th><th>rows</th><th>shards</th><th>&sum;</th></tr>|;
189                                                            foreach my $node ( @nodes ) {
190                                                                    my $h = '';
191                                                                    my $shard = 0;
192                                                                    foreach my $path ( sort keys %{ $s->{$node} } ) {
193                                                                            my $affected = $s->{$node}->{$path};
194                                                                            my $n = '?';
195                                                                            $n = $1 if $path =~ m{/(\w)\w+/\d+};
196                                                                            $h .= qq|<tt title="$affected $path">$n</tt>|;
197                                                                            $shard += $affected;
198                                                                            $total += $affected;
199                                                                    }
200                                                                    $html .= qq|<tr><td><tt>$node</tt></td><td>$shard</td><td>$h</td><td>$total</td></tr>\n|;
201                                                            }
202    
203                                                            $html .= qq|</table>|;
204                                                    };
205                                                    qq|<li><a href="$view">$view</a>$html|
206                                            } glob '/srv/Sack/views/*/*.pl' )
207                                            , qq|</ul>|
208                                            ;
209    
210                                    my $out = Sack::Merge->out;    
211                                    print $send qq|<h1>Results</h1><ul>|
212                                            , join("\n", map { qq|<li><a href="/out/$_" target="$_">$_</a>| } keys %$out )
213                                            , qq|</ul>|
214                                            ;
215    
216                                    print $send qq|<h1>Nodes</h1>
217                                    |, join("\n", map {
218                                            my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} });
219                                            qq|<span class="$class">$_</span>|;
220                                    } sort keys %{ $info->{node} } );
221    
222                                    print $send qq|<h1>Data</h1><ul>|;
223                                    foreach my $path ( glob '/tmp/sack/*' ) {
224                                            print $send qq|<li><a href="$path">$path</a><div class="shards">|;
225                                            foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) {
226                                                    my $i = $info->{shard}->{$s};
227                                                    print $send qq|<span class=$i>$i</span> |;
228                                            }
229                                            print $send qq|</div>|;
230                                    }
231                                    print $send qq|</ul>|;
232    
233                                    if ( $param->{info} ) {
234                                            print $send qq|<a href="?info=0">hide info</a>|;
235                                            print $send '<pre>', dump($info), '</pre>'
236                                    } else {
237                                            print $send qq|<a href="?info=1">show info</a>|;
238                                    }
239                                    return 1;
240                            } );
241                  } else {                  } else {
242                          my $data = eval { Storable::fd_retrieve( $sock ) };                          my $data = eval { Storable::fd_retrieve( $sock ) };
243                          if ( $@ ) {                          if ( $@ ) {
# Line 76  while (1) { Line 246  while (1) {
246                                  $sel->remove($sock);                                  $sel->remove($sock);
247                                  $sock->close;                                  $sock->close;
248                          } else {                          } else {
249                                  warn "<<<< ", dump($data), $/;                                  warn "<<<< ", dump($data), $/ if $debug;
250                                  if ( $data->{repl} ) {  
251                                          my $response = { repl => $$, info => $info };                                  if ( my $path = $data->{shard} ) {
252                                            $info->{shard}->{ $path } = $data->{port};
253                                            # FIXME will need push for multiple copies of shards
254                                    }
255    
256                                    if ( my $repl = $data->{repl} ) {
257                                            my $response = { repl_pid => $$ };
258                                            if ( $repl =~ m/ping/ ) {
259                                                    to_all { ping => 1 };
260                                            } elsif ( $repl =~ m/info/ ) {
261                                                    $response->{info} = $info;
262                                            } elsif ( $repl =~ m{load\s*(\S+)?} ) {
263                                                    load_shard $1;
264                                            } elsif ( $repl =~ m{view\s*(\S+)?} ) {
265                                                    run_view $1;
266                                            } elsif ( $repl =~ m{debug\s*(.+)?} ) {
267                                                    to_all { debug => $1 };
268                                            } elsif ( $repl =~ m{out} ) {
269                                                    my $out = Sack::Merge->out;    
270                                                    warn "out ",dump( $out );
271                                                    $response->{out} = $out;
272                                            } elsif ( $repl =~ m{clean} ) {
273                                                    delete $info->{shard};
274                                                    to_all { clean => 1 };
275                                            } elsif ( $repl eq 'exit' ) {
276                                                    to_all { exit => 1 };
277                                                    sleep 1;
278                                                    exit;
279                                            } else {
280                                                    $response->{error}->{unknown} = $data;
281                                            }
282                                          Storable::store_fd( $response, $sock );                                          Storable::store_fd( $response, $sock );
283                                  } elsif ( $data->{ping} ) {                                  } elsif ( $data->{ping} ) {
284                                          $info->{_peer_port}->{$sock->peerport} = $data->{port};                                          my $port = $data->{port};
285                                          info $data->{port} => 'peer port', $sock->peerport;                                          info $port => 'ping', $port;
286                                            $session->{port}->{ $data->{port} } = $sock;
287                                    } elsif ( defined $data->{load} && $data->{load} eq 'shard' ) {
288                                            if ( my $path = shift @shard_load_queue ) {
289                                                    $info->{shard}->{$path} = 'read';
290                                                    my $shard = Storable::retrieve $path;
291                                                    $info->{shard}->{$path} = 'send';
292                                                    warn ">>>> [", $data->{port}, "] sending shard $path\n";
293                                                    Storable::store_fd( { path => $path, shard => $shard }, $sock );
294                                            } else {
295                                                    warn "no more shards for [", $data->{port}, "]\n";
296                                            }
297                                    } elsif ( defined $data->{out} ) {
298                                            Sack::Merge->add( $data->{out} );
299                                            $info->{view}->{ $data->{view} }->{ $data->{port} } = $data->{on_shard};
300                                    } else {
301                                            warn "UNKNOWN ",dump($data);
302                                  }                                  }
303                          }                          }
304                  }                  }

Legend:
Removed from v.170  
changed lines
  Added in v.233

  ViewVC Help
Powered by ViewVC 1.1.26