1 |
#!/usr/bin/perl |
#!/usr/bin/perl |
2 |
|
|
3 |
|
package Sack::Server; |
4 |
|
|
5 |
use warnings; |
use warnings; |
6 |
use strict; |
use strict; |
7 |
|
|
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 |
|
|
19 |
my $cloud_path = '/srv/Sack/etc/lib'; |
my @cloud; |
20 |
|
my $cloud_path = $ENV{CLOUD} || die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n"; |
21 |
@cloud = read_file $cloud_path; |
@cloud = read_file $cloud_path; |
22 |
@cloud = map { chomp $_; $_ } @cloud; |
@cloud = map { chomp $_; $_ } @cloud; |
23 |
|
|
25 |
|
|
26 |
my $listen_port = 4444; |
my $listen_port = 4444; |
27 |
|
|
28 |
my $node_path = '/tmp/client.pl'; |
my $node_path = abs_path $0; |
29 |
|
$node_path =~ s{Server}{Client}; |
30 |
|
|
31 |
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 $!; |
32 |
my $sel = IO::Select->new($lsn); |
my $sel = IO::Select->new($lsn); |
37 |
push @{ $info->{$port} }, [ @_ ]; |
push @{ $info->{$port} }, [ @_ ]; |
38 |
} |
} |
39 |
|
|
40 |
sub fork_node { |
sub fork_ssh { |
41 |
my ( $port, $host ) = @_; |
my ( $port, $host ) = @_; |
42 |
|
|
43 |
if ( my $pid = fork ) { |
if ( my $pid = fork ) { |
59 |
my $node_port = 4000; |
my $node_port = 4000; |
60 |
|
|
61 |
foreach my $host ( @cloud ) { |
foreach my $host ( @cloud ) { |
62 |
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"; |
63 |
fork_node( $node_port++, $host ); |
fork_ssh( $node_port++, $host ); |
64 |
} |
} |
65 |
|
|
66 |
my $session; |
my $session; |
67 |
|
|
68 |
|
sub to_all { |
69 |
|
my $data = shift; |
70 |
|
foreach my $port ( keys %{ $session->{port} } ) { |
71 |
|
warn ">>>> [$port]\n"; |
72 |
|
Storable::store_fd( $data, $session->{port}->{$port} ); |
73 |
|
} |
74 |
|
} |
75 |
|
|
76 |
|
my @shard_paths; |
77 |
|
|
78 |
while (1) { |
while (1) { |
79 |
for my $sock ($sel->can_read(1)) { |
for my $sock ($sel->can_read(1)) { |
80 |
if ($sock == $lsn) { |
if ($sock == $lsn) { |
81 |
my $new = $lsn->accept; |
my $new = $lsn->accept; |
82 |
$sel->add($new); |
$sel->add($new); |
83 |
$session->{$new} = $new->peerport; |
$session->{peerport}->{ $new->peerport } = $new; |
84 |
warn "[socket] connect\n"; |
warn "[socket] connect\n"; |
85 |
Storable::store_fd( { ping => 1 }, $new ); |
Storable::store_fd( { ping => 1 }, $new ); |
86 |
info 0 => 'ping', $new->peerport; |
info 0 => 'ping', $new->peerport; |
93 |
$sock->close; |
$sock->close; |
94 |
} else { |
} else { |
95 |
warn "<<<< ", dump($data), $/; |
warn "<<<< ", dump($data), $/; |
96 |
if ( $data->{repl} ) { |
|
97 |
my $response = { repl => $$, info => $info }; |
if ( my $path = $data->{shard} ) { |
98 |
|
push @{ $info->{shard}->{ $data->{port} } }, $path; |
99 |
|
} |
100 |
|
|
101 |
|
if ( my $repl = $data->{repl} ) { |
102 |
|
my $response = { repl_pid => $$ }; |
103 |
|
if ( $repl =~ m/ping/ ) { |
104 |
|
to_all { ping => 1 }; |
105 |
|
} elsif ( $repl =~ m/info/ ) { |
106 |
|
$response->{info} = $info; |
107 |
|
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
108 |
|
my $name = $1 || 'shard'; |
109 |
|
@shard_paths = glob "/tmp/sack/$name/*"; |
110 |
|
warn "loading shard $name from ", dump( @shard_paths ); |
111 |
|
to_all { load => $name }; |
112 |
|
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
113 |
|
my $path = $1 || '/srv/Sack/views/00.demo.pl'; |
114 |
|
my $code = read_file $path; |
115 |
|
Sack::Merge->clean; |
116 |
|
to_all { view => $code, path => $path }; |
117 |
|
$response->{view}->{$path}->{running}; |
118 |
|
} elsif ( $repl =~ m{debug\s*(.+)?} ) { |
119 |
|
to_all { debug => $1 }; |
120 |
|
} elsif ( $repl =~ m{out} ) { |
121 |
|
my $out = Sack::Merge->out; |
122 |
|
warn "out ",dump( $out ); |
123 |
|
$response->{out} = $out; |
124 |
|
} elsif ( $repl =~ m{clean} ) { |
125 |
|
delete $info->{shard}; |
126 |
|
to_all { clean => 1 }; |
127 |
|
} else { |
128 |
|
$response->{error}->{unknown} = $data; |
129 |
|
} |
130 |
Storable::store_fd( $response, $sock ); |
Storable::store_fd( $response, $sock ); |
131 |
} elsif ( $data->{ping} ) { |
} elsif ( $data->{ping} ) { |
132 |
$info->{_peer_port}->{$sock->peerport} = $data->{port}; |
my $port = $data->{port}; |
133 |
info $data->{port} => 'peer port', $sock->peerport; |
info $port => 'ping', $port; |
134 |
|
$session->{port}->{ $data->{port} } = $sock; |
135 |
|
} elsif ( $data->{load} eq 'shard' ) { |
136 |
|
if ( my $path = shift @shard_paths ) { |
137 |
|
warn "retrieve $path ", -s $path; |
138 |
|
my $shard = Storable::retrieve $path; |
139 |
|
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
140 |
|
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
141 |
|
} else { |
142 |
|
warn "no more shards for [", $data->{port}, "]\n"; |
143 |
|
} |
144 |
|
} elsif ( defined $data->{out} ) { |
145 |
|
Sack::Merge->add( $data->{out} ); |
146 |
|
} else { |
147 |
|
warn "UNKNOWN ",dump($data); |
148 |
} |
} |
149 |
} |
} |
150 |
} |
} |