1 |
#!/usr/bin/perl |
#!/usr/bin/perl |
2 |
|
|
3 |
|
package Sack::Server; |
4 |
|
|
5 |
use warnings; |
use warnings; |
6 |
use strict; |
use strict; |
7 |
|
|
13 |
use File::Slurp; |
use File::Slurp; |
14 |
use Cwd qw(abs_path); |
use Cwd qw(abs_path); |
15 |
|
|
16 |
my @cloud = qw(localhost tab.lan llin.lan); |
my @cloud; |
17 |
|
my $cloud_path = $ENV{CLOUD} || die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n"; |
|
my $cloud_path = $ENV{CLOUD} || '/srv/Sack/etc/lib'; |
|
18 |
@cloud = read_file $cloud_path; |
@cloud = read_file $cloud_path; |
19 |
@cloud = map { chomp $_; $_ } @cloud; |
@cloud = map { chomp $_; $_ } @cloud; |
20 |
|
|
23 |
my $listen_port = 4444; |
my $listen_port = 4444; |
24 |
|
|
25 |
my $node_path = abs_path $0; |
my $node_path = abs_path $0; |
26 |
$node_path =~ s{server.pl}{client.pl}; |
$node_path =~ s{Server}{Client}; |
27 |
|
|
28 |
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 $!; |
29 |
my $sel = IO::Select->new($lsn); |
my $sel = IO::Select->new($lsn); |
34 |
push @{ $info->{$port} }, [ @_ ]; |
push @{ $info->{$port} }, [ @_ ]; |
35 |
} |
} |
36 |
|
|
37 |
sub fork_node { |
sub fork_ssh { |
38 |
my ( $port, $host ) = @_; |
my ( $port, $host ) = @_; |
39 |
|
|
40 |
if ( my $pid = fork ) { |
if ( my $pid = fork ) { |
57 |
|
|
58 |
foreach my $host ( @cloud ) { |
foreach my $host ( @cloud ) { |
59 |
system "find /srv/Sack/ | 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"; |
60 |
fork_node( $node_port++, $host ); |
fork_ssh( $node_port++, $host ); |
61 |
} |
} |
62 |
|
|
63 |
my $session; |
my $session; |
64 |
|
|
65 |
|
sub to_all { |
66 |
|
my $data = shift; |
67 |
|
foreach my $port ( keys %{ $session->{port} } ) { |
68 |
|
warn ">>>> [$port]\n"; |
69 |
|
Storable::store_fd( $data, $session->{port}->{$port} ); |
70 |
|
} |
71 |
|
} |
72 |
|
|
73 |
|
my @shard_paths; |
74 |
|
|
75 |
while (1) { |
while (1) { |
76 |
for my $sock ($sel->can_read(1)) { |
for my $sock ($sel->can_read(1)) { |
77 |
if ($sock == $lsn) { |
if ($sock == $lsn) { |
90 |
$sock->close; |
$sock->close; |
91 |
} else { |
} else { |
92 |
warn "<<<< ", dump($data), $/; |
warn "<<<< ", dump($data), $/; |
93 |
if ( $data->{repl} ) { |
|
94 |
my $response = { repl => $$ }; |
if ( my $path = $data->{shard} ) { |
95 |
if ( $data->{repl} =~ m/ping/ ) { |
push @{ $info->{shard}->{ $data->{port} } }, $path; |
96 |
foreach my $port ( keys %{ $session->{port} } ) { |
} |
97 |
warn ">>>> [$port]\n"; |
|
98 |
Storable::store_fd( { ping => 1 }, $session->{port}->{$port} ); |
if ( my $repl = $data->{repl} ) { |
99 |
} |
my $response = { repl_pid => $$ }; |
100 |
} elsif ( $data->{repl} =~ m/info/ ) { |
if ( $repl =~ m/ping/ ) { |
101 |
|
to_all { ping => 1 }; |
102 |
|
} elsif ( $repl =~ m/info/ ) { |
103 |
$response->{info} = $info; |
$response->{info} = $info; |
104 |
|
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
105 |
|
my $name = $1 || 'shard'; |
106 |
|
@shard_paths = glob "/tmp/sack/$name/*"; |
107 |
|
warn "loading shards ", dump( @shard_paths ); |
108 |
|
to_all { load => $name }; |
109 |
|
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
110 |
|
my $path = $1 || '/srv/Sack/views/00.demo.pl'; |
111 |
|
my $code = read_file $path; |
112 |
|
to_all { view => $code, path => $path }; |
113 |
|
$response->{view}->{$path}->{running}; |
114 |
|
} elsif ( $repl =~ m{debug\s*(.+)?} ) { |
115 |
|
to_all { debug => $1 }; |
116 |
|
} else { |
117 |
|
$response->{error}->{unknown} = $data; |
118 |
} |
} |
119 |
Storable::store_fd( $response, $sock ); |
Storable::store_fd( $response, $sock ); |
120 |
} elsif ( $data->{ping} ) { |
} elsif ( $data->{ping} ) { |
121 |
my $port = $data->{port}; |
my $port = $data->{port}; |
122 |
info $port => 'ping', $port; |
info $port => 'ping', $port; |
123 |
$session->{port}->{ $data->{port} } = $sock; |
$session->{port}->{ $data->{port} } = $sock; |
124 |
|
} elsif ( $data->{load} eq 'shard' ) { |
125 |
|
if ( my $path = shift @shard_paths ) { |
126 |
|
warn "retrieve $path ", -s $path; |
127 |
|
my $shard = Storable::retrieve $path; |
128 |
|
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
129 |
|
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
130 |
|
} else { |
131 |
|
warn "no more shards for [", $data->{port}, "]\n"; |
132 |
|
} |
133 |
|
} else { |
134 |
|
warn "UNKNOWN ",dump($data); |
135 |
} |
} |
136 |
} |
} |
137 |
} |
} |