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

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

Legend:
Removed from v.203  
changed lines
  Added in v.241

  ViewVC Help
Powered by ViewVC 1.1.26