13 |
use File::Slurp; |
use File::Slurp; |
14 |
use Cwd qw(abs_path); |
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; |
my @cloud; |
21 |
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"; |
22 |
|
die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n" unless -e $cloud_path; |
23 |
@cloud = read_file $cloud_path; |
@cloud = read_file $cloud_path; |
24 |
@cloud = map { chomp $_; $_ } @cloud; |
@cloud = map { chomp $_; $_ } @cloud; |
25 |
|
|
26 |
warn "# cloud ",dump( @cloud ); |
warn "# cloud ",dump( @cloud ); |
27 |
|
|
28 |
my $listen_port = 4444; |
my $listen_port = 4444; |
29 |
|
my $http_port = 4480; |
30 |
|
|
31 |
my $node_path = abs_path $0; |
my $node_path = abs_path $0; |
32 |
$node_path =~ s{Server}{Client}; |
$node_path =~ s{Server}{Client}; |
33 |
|
|
34 |
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 $!"; |
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); |
my $sel = IO::Select->new($lsn); |
37 |
|
$sel->add( $www ); |
38 |
|
|
39 |
my $info; |
my $info; |
40 |
sub info { |
sub info { |
78 |
} |
} |
79 |
} |
} |
80 |
|
|
81 |
|
my @shard_paths; |
82 |
|
|
83 |
while (1) { |
while (1) { |
84 |
for my $sock ($sel->can_read(1)) { |
for my $sock ($sel->can_read(1)) { |
85 |
if ($sock == $lsn) { |
if ($sock == $lsn) { |
89 |
warn "[socket] connect\n"; |
warn "[socket] connect\n"; |
90 |
Storable::store_fd( { ping => 1 }, $new ); |
Storable::store_fd( { ping => 1 }, $new ); |
91 |
info 0 => 'ping', $new->peerport; |
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 |
|
print $send "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n" |
98 |
|
, dump( $info ) |
99 |
|
; |
100 |
|
} ); |
101 |
} else { |
} else { |
102 |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
my $data = eval { Storable::fd_retrieve( $sock ) }; |
103 |
if ( $@ ) { |
if ( $@ ) { |
107 |
$sock->close; |
$sock->close; |
108 |
} else { |
} else { |
109 |
warn "<<<< ", dump($data), $/; |
warn "<<<< ", dump($data), $/; |
110 |
if ( $data->{repl} ) { |
|
111 |
my $response = { repl => $$ }; |
if ( my $path = $data->{shard} ) { |
112 |
if ( $data->{repl} =~ m/ping/ ) { |
push @{ $info->{shard}->{ $data->{port} } }, $path; |
113 |
|
} |
114 |
|
|
115 |
|
if ( my $repl = $data->{repl} ) { |
116 |
|
my $response = { repl_pid => $$ }; |
117 |
|
if ( $repl =~ m/ping/ ) { |
118 |
to_all { ping => 1 }; |
to_all { ping => 1 }; |
119 |
} elsif ( $data->{repl} =~ m/info/ ) { |
} elsif ( $repl =~ m/info/ ) { |
120 |
$response->{info} = $info; |
$response->{info} = $info; |
121 |
|
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
122 |
|
my $name = $1 || 'shard'; |
123 |
|
@shard_paths = glob "/tmp/sack/$name/*"; |
124 |
|
warn "loading shard $name from ", dump( @shard_paths ); |
125 |
|
to_all { load => $name }; |
126 |
|
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
127 |
|
my $path = $1 || '/srv/Sack/views/00.demo.pl'; |
128 |
|
my $code = read_file $path; |
129 |
|
Sack::Merge->clean; |
130 |
|
to_all { view => $code, path => $path }; |
131 |
|
$response->{view}->{$path}->{running}; |
132 |
|
} elsif ( $repl =~ m{debug\s*(.+)?} ) { |
133 |
|
to_all { debug => $1 }; |
134 |
|
} elsif ( $repl =~ m{out} ) { |
135 |
|
my $out = Sack::Merge->out; |
136 |
|
warn "out ",dump( $out ); |
137 |
|
$response->{out} = $out; |
138 |
|
} elsif ( $repl =~ m{clean} ) { |
139 |
|
delete $info->{shard}; |
140 |
|
to_all { clean => 1 }; |
141 |
} else { |
} else { |
142 |
$response->{error}->{unknown} = $data; |
$response->{error}->{unknown} = $data; |
143 |
} |
} |
146 |
my $port = $data->{port}; |
my $port = $data->{port}; |
147 |
info $port => 'ping', $port; |
info $port => 'ping', $port; |
148 |
$session->{port}->{ $data->{port} } = $sock; |
$session->{port}->{ $data->{port} } = $sock; |
149 |
|
} elsif ( $data->{load} eq 'shard' ) { |
150 |
|
if ( my $path = shift @shard_paths ) { |
151 |
|
warn "retrieve $path ", -s $path; |
152 |
|
my $shard = Storable::retrieve $path; |
153 |
|
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
154 |
|
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
155 |
|
} else { |
156 |
|
warn "no more shards for [", $data->{port}, "]\n"; |
157 |
|
} |
158 |
|
} elsif ( defined $data->{out} ) { |
159 |
|
Sack::Merge->add( $data->{out} ); |
160 |
|
} else { |
161 |
|
warn "UNKNOWN ",dump($data); |
162 |
} |
} |
163 |
} |
} |
164 |
} |
} |