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 |
|
|
19 |
my @cloud; |
my @cloud; |
20 |
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"; |
21 |
|
die "start with: CLOUD=etc/cloud perl -I/srv/Sack/lib $0\n" unless -e $cloud_path; |
22 |
@cloud = read_file $cloud_path; |
@cloud = read_file $cloud_path; |
23 |
@cloud = map { chomp $_; $_ } @cloud; |
@cloud = map { chomp $_; $_ } @cloud; |
24 |
|
|
74 |
} |
} |
75 |
} |
} |
76 |
|
|
77 |
|
my @shard_paths; |
78 |
|
|
79 |
while (1) { |
while (1) { |
80 |
for my $sock ($sel->can_read(1)) { |
for my $sock ($sel->can_read(1)) { |
81 |
if ($sock == $lsn) { |
if ($sock == $lsn) { |
94 |
$sock->close; |
$sock->close; |
95 |
} else { |
} else { |
96 |
warn "<<<< ", dump($data), $/; |
warn "<<<< ", dump($data), $/; |
97 |
if ( $data->{repl} ) { |
|
98 |
my $response = { repl => $$ }; |
if ( my $path = $data->{shard} ) { |
99 |
if ( $data->{repl} =~ m/ping/ ) { |
push @{ $info->{shard}->{ $data->{port} } }, $path; |
100 |
|
} |
101 |
|
|
102 |
|
if ( my $repl = $data->{repl} ) { |
103 |
|
my $response = { repl_pid => $$ }; |
104 |
|
if ( $repl =~ m/ping/ ) { |
105 |
to_all { ping => 1 }; |
to_all { ping => 1 }; |
106 |
} elsif ( $data->{repl} =~ m/info/ ) { |
} elsif ( $repl =~ m/info/ ) { |
107 |
$response->{info} = $info; |
$response->{info} = $info; |
108 |
|
} elsif ( $repl =~ m{load\s*(\S+)?} ) { |
109 |
|
my $name = $1 || 'shard'; |
110 |
|
@shard_paths = glob "/tmp/sack/$name/*"; |
111 |
|
warn "loading shard $name from ", dump( @shard_paths ); |
112 |
|
to_all { load => $name }; |
113 |
|
} elsif ( $repl =~ m{view\s*(\S+)?} ) { |
114 |
|
my $path = $1 || '/srv/Sack/views/00.demo.pl'; |
115 |
|
my $code = read_file $path; |
116 |
|
Sack::Merge->clean; |
117 |
|
to_all { view => $code, path => $path }; |
118 |
|
$response->{view}->{$path}->{running}; |
119 |
|
} elsif ( $repl =~ m{debug\s*(.+)?} ) { |
120 |
|
to_all { debug => $1 }; |
121 |
|
} elsif ( $repl =~ m{out} ) { |
122 |
|
my $out = Sack::Merge->out; |
123 |
|
warn "out ",dump( $out ); |
124 |
|
$response->{out} = $out; |
125 |
|
} elsif ( $repl =~ m{clean} ) { |
126 |
|
delete $info->{shard}; |
127 |
|
to_all { clean => 1 }; |
128 |
} else { |
} else { |
129 |
$response->{error}->{unknown} = $data; |
$response->{error}->{unknown} = $data; |
130 |
} |
} |
133 |
my $port = $data->{port}; |
my $port = $data->{port}; |
134 |
info $port => 'ping', $port; |
info $port => 'ping', $port; |
135 |
$session->{port}->{ $data->{port} } = $sock; |
$session->{port}->{ $data->{port} } = $sock; |
136 |
|
} elsif ( $data->{load} eq 'shard' ) { |
137 |
|
if ( my $path = shift @shard_paths ) { |
138 |
|
warn "retrieve $path ", -s $path; |
139 |
|
my $shard = Storable::retrieve $path; |
140 |
|
warn ">>>> [", $data->{port}, "] sending shard $path\n"; |
141 |
|
Storable::store_fd( { path => $path, shard => $shard }, $sock ); |
142 |
|
} else { |
143 |
|
warn "no more shards for [", $data->{port}, "]\n"; |
144 |
|
} |
145 |
|
} elsif ( defined $data->{out} ) { |
146 |
|
Sack::Merge->add( $data->{out} ); |
147 |
|
} else { |
148 |
|
warn "UNKNOWN ",dump($data); |
149 |
} |
} |
150 |
} |
} |
151 |
} |
} |