15 |
|
|
16 |
use lib '/srv/Sack/lib'; |
use lib '/srv/Sack/lib'; |
17 |
use Sack::Merge; |
use Sack::Merge; |
18 |
|
use Sack::Server::HTTP; |
19 |
|
use Sack::Server::HTML; |
20 |
|
|
21 |
my @cloud; |
my @cloud; |
22 |
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"; |
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 = abs_path $0; |
my $node_path = abs_path $0; |
33 |
$node_path =~ s{Server}{Client}; |
$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_ssh { |
sub fork_ssh { |
79 |
} |
} |
80 |
} |
} |
81 |
|
|
82 |
my @shard_paths; |
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)) { |
107 |
$session->{peerport}->{ $new->peerport } = $new; |
$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 |
|
</style> |
155 |
|
|, join("\n", map { |
156 |
|
my $class = join(' ', map { $_->[0] } @{ $info->{node}->{$_} }); |
157 |
|
qq|<span class="$class">$_</span>|; |
158 |
|
} sort keys %{ $info->{node} } ); |
159 |
|
|
160 |
|
print $send qq|<h1>Data</h1><ul>|; |
161 |
|
foreach my $path ( glob '/tmp/sack/*' ) { |
162 |
|
print $send qq|<li><a href="$path">$path</a><br><tt>|; |
163 |
|
foreach my $s ( sort grep { m/$path/ } keys %{ $info->{shard} } ) { |
164 |
|
print $send $info->{shard}->{$s}, ' '; |
165 |
|
} |
166 |
|
print $send qq|</tt>|; |
167 |
|
} |
168 |
|
print $send qq|</ul>|; |
169 |
|
|
170 |
|
print $send '<pre>', dump($info), '</pre>'; |
171 |
|
|
172 |
|
} ); |
173 |
} else { |
} else { |
174 |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
175 |
if ( $@ ) { |
if ( $@ ) { |
181 |
warn "<<<< ", dump($data), $/; |
warn "<<<< ", dump($data), $/; |
182 |
|
|
183 |
if ( my $path = $data->{shard} ) { |
if ( my $path = $data->{shard} ) { |
184 |
push @{ $info->{shard}->{ $data->{port} } }, $path; |
$info->{shard}->{ $path } = $data->{port}; |
185 |
|
# FIXME will need push for multiple copies of shards |
186 |
} |
} |
187 |
|
|
188 |
if ( my $repl = $data->{repl} ) { |
if ( my $repl = $data->{repl} ) { |
192 |
} elsif ( $repl =~ m/info/ ) { |
} elsif ( $repl =~ m/info/ ) { |
193 |
$response->{info} = $info; |
$response->{info} = $info; |
194 |
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
195 |
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 }; |
|
196 |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
197 |
my $path = $1 || '/srv/Sack/views/00.demo.pl'; |
my $path = $1 || '/srv/Sack/views/00.demo.pl'; |
198 |
my $code = read_file $path; |
my $code = read_file $path; |
217 |
info $port => 'ping', $port; |
info $port => 'ping', $port; |
218 |
$session->{port}->{ $data->{port} } = $sock; |
$session->{port}->{ $data->{port} } = $sock; |
219 |
} elsif ( $data->{load} eq 'shard' ) { |
} elsif ( $data->{load} eq 'shard' ) { |
220 |
if ( my $path = shift @shard_paths ) { |
if ( my $path = shift @shard_load_queue ) { |
221 |
warn "retrieve $path ", -s $path; |
$info->{shard}->{$path} = 'read'; |
222 |
my $shard = Storable::retrieve $path; |
my $shard = Storable::retrieve $path; |
223 |
|
$info->{shard}->{$path} = 'send'; |
224 |
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
225 |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
226 |
} else { |
} else { |