Yenta/Status.pm untested

filter: Statementstmt 0.0% 0 / 217 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 26 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 361 CC 36 SCAR 36.4
CC
36
Cov
0%
update
132.0
save_status
56.0
periodic
30.0
CRAP 37.9
1 # -*- perl -*-
2  
3 # Copyright (c) 2009 AdCopy
4 # Author: Jeff Weisberg
5 # Created: 2009-Apr-02 11:16 (EDT)
6 # Function: track status of peers
7 #
8 # $Id: Status.pm,v 1.38 2010/10/30 19:46:16 jaw Exp $
9  
10 package AC::Yenta::Status;
11 use AC::Yenta::Kibitz::Status;
12 use AC::Yenta::Debug 'status';
13 use AC::Yenta::Config;
14 use AC::Yenta::MySelf;
15 use AC::Misc;
16 use Sys::Hostname;
17 use JSON;
18 use Socket;
19 require 'AC/protobuf/yenta_status.pl';
20 use strict;
21  
22 my $KEEPDOWN = 1800; # keep data about down servers for how long?
23 my $KEEPLOST = 600; # keep data about servers we have not heard about for how long?
24 my $SAVEMAX = 1800; # do not save if older than
25  
26 my $PORT;
27  
28 our $DATA = bless {
29 allpeer => {}, # yenta_status
30 sceptical => {},
31 mappeer => {}, # {map} => { id => id }
32 peermap => {}, # {id} => @map
33 datacenter => {}, # {dc} => { id => id }
34 peertype => {}, # {ss} => { id => id }
35 };
36  
37 sub init {
38 my $port = shift;
39  
40 $PORT = $port;
41  
42 AC::DC::Sched->new(
43 info => 'kibitz status',
44 freq => (conf_value('time_status_kibitz') || 5),
45 func => \&periodic,
46 );
47 AC::DC::Sched->new(
48 info => 'save status',
49 freq => (conf_value('time_status_save') || 15),
50 func => \&save_status,
51 );
52 }
53  
54 # start up a client every so often
55 sub periodic {
56  
57 # clean up down or lost peers
58 for my $id ( keys %{$DATA->{allpeer}} ){
59 my $p = $DATA->{allpeer}{$id};
60 next unless $p;
61  
62 next if $p->{status} == 200 && $p->{timestamp} > $^T - $KEEPLOST;
63 _maybe_remove( $id );
64 }
65  
66 # randomly pick a peer
67 my($id, $ip, $port) = _random_peer();
68 return unless $id;
69  
70 # start a client
71 debug("starting status kibitz client to $id");
72  
73 my $c = AC::Yenta::Kibitz::Status::Client->new( $ip, $port,
74 info => "status client: $id",
75 status_peer => $id,
76 );
77 __PACKAGE__->isdown($id) unless $c;
78  
79 $c->start();
80 }
81  
82 sub _random_peer {
83  
84 # sceptical
85 my @peer = values %{$DATA->{sceptical}};
86  
87 # known peer
88 unless( @peer ){
89 @peer = map { $DATA->{allpeer}{$_} } keys %{$DATA->{peertype}{yenta}};
90 }
91  
92 if( @peer ){
93 my $p = $peer[ rand(@peer) ];
94 debug("using peer $p->{server_id}");
95 return ($p->{server_id}, $p->{ip}, undef)
96 if int rand(@peer+1); # sometimes use the seed
97 }
98  
99 # seed peer
100 my $seed = conf_value('seedpeer');
101 my $p = $seed->[ rand(@$seed) ];
102 my ($ip, $port) = split /:/, $p;
103 $port ||= my_port();
104  
105 # don't talk to self. any of my addrs.
106 my $ipinfo = my_network_info();
107 for my $i (@$ipinfo){
108 return if $ip eq $i->{ipa} && $port == $PORT;
109 }
110  
111 return("seed/$ip:$port", $ip, $port);
112 }
113  
114 # server list for save file
115 sub server_list {
116 my $type = shift;
117  
118 ($type, my $where) = split m|/|, $type;
119 # where - no longer used
120 $where ||= my_datacenter();
121  
122 my @peer = keys %{ $DATA->{peertype}{$type} };
123 return unless @peer;
124  
125 # nothing too old
126 @peer = grep { $DATA->{allpeer}{$_}{lastup} > $^T - $SAVEMAX } @peer;
127 return unless @peer;
128  
129 return map { $DATA->{allpeer}{$_} } @peer;
130 }
131  
132 # save a list of peers, in case I crash, and for others to use
133 sub save_status {
134  
135 my $save = conf_value('savestatus');
136 my $here = my_datacenter();
137  
138 # also save locally running services
139 my @mon = AC::Yenta::Monitor::export();
140  
141 for my $s ( @$save ){
142 my $file = $s->{file};
143 my $types = $s->{type};
144  
145 my @peer;
146 for my $type (@$types){
147 push @peer, server_list($type);
148  
149 for my $m (@mon){
150 push @peer, $m if $m->{subsystem} eq $type;
151 }
152 }
153  
154 next unless @peer;
155  
156 debug("saving peer status file");
157 unless( open(FILE, ">$file.tmp") ){
158 problem("cannot open save file '$file.tmp': $!");
159 return;
160 }
161  
162 for my $pd (@peer){
163 # only save best addr in save file
164 my($ip, $port) = AC::Yenta::IO::TCP::Client->use_addr_port( $pd->{ip} );
165  
166 my $data = {
167 id => $pd->{server_id},
168 addr => $ip,
169 port => int($port),
170 status => int($pd->{status}),
171 subsystem => $pd->{subsystem},
172 environment => $pd->{environment},
173 sort_metric => int($pd->{sort_metric}),
174 is_local => ($here eq $pd->{datacenter} ? 1 : 0),
175 };
176 if( $pd->{subsystem} eq 'yenta' ){
177 $data->{map} = $pd->{map};
178 }
179  
180 print FILE encode_json( $data ), "\n";
181 }
182  
183 close FILE;
184 unless( rename("$file.tmp", $file) ){
185 problem("cannot rename save file '$file': $!");
186 }
187  
188 }
189 }
190  
191 ################################################################
192 # diagnostic reports
193 sub report {
194  
195 my $res;
196  
197 for my $v (AC::Yenta::Kibitz::Status::_myself(), AC::Yenta::Monitor::export(), values %{$DATA->{allpeer}} ){
198 my $id = sprintf '%-28s', $v->{server_id};
199 my $metric = int( $v->{sort_metric} );
200 $res .= "$id $v->{hostname}\t$v->{datacenter}\t$v->{subsystem}\t$v->{environment}\t$v->{status}\t$metric\n";
201 }
202  
203 return $res;
204 }
205  
206 sub report_long {
207  
208 my $res;
209  
210 # for my $v (AC::Yenta::Kibitz::Status::_myself(), AC::Yenta::Monitor::export(), values %{$DATA->{allpeer}} ){
211 # $res .= dumper( $v ) . "\n\n";
212 # }
213 return $res;
214 }
215 ################################################################
216  
217 sub my_port { $PORT }
218  
219  
220 sub my_instance_id {
221 my $class = shift;
222 return my_server_id() . sprintf('%04x', $$);
223 }
224  
225 sub peer {
226 my $class = shift;
227 my $id = shift;
228  
229 return $DATA->{allpeer}{$id};
230 }
231  
232 sub allpeers {
233 my $class = shift;
234  
235 return values %{$DATA->{allpeer}};
236 }
237  
238 sub mappeers {
239 my $class = shift;
240 my $map = shift;
241  
242 return keys %{ $DATA->{mappeer}{$map} };
243 }
244  
245 sub datacenters {
246 my $class = shift;
247  
248 return $DATA->{datacenter};
249 }
250 ################################################################
251  
252 sub _remove {
253 my $id = shift;
254  
255 my $ss = $DATA->{allpeer}{$id}{subsystem};
256 delete $DATA->{peertype}{$ss}{$id} if $ss;
257  
258 my $dc = $DATA->{allpeer}{$id}{datacenter};
259 delete $DATA->{datacenter}{$dc}{$id} if $dc;
260  
261 verbose("deleting peer: $id");
262 delete $DATA->{allpeer}{$id};
263  
264 # remove map info
265 for my $map ( @{$DATA->{peermap}{$id}} ){
266 delete $DATA->{mappeer}{$map}{$id};
267 }
268 delete $DATA->{peermap}{$id};
269  
270 # delete its monitored items
271 for my $p (keys %{$DATA->{allpeer}}){
272 next unless $DATA->{allpeer}{$p}{via} eq $id;
273 _remove($p);
274 }
275 }
276  
277 sub _maybe_remove {
278 my $id = shift;
279  
280 my $d = $DATA->{allpeer}{$id};
281  
282 if( ($^T - $d->{lastup} > $KEEPDOWN) || ($^T - $d->{timestamp} > $KEEPLOST) ){
283  
284 _remove($id);
285 }
286 }
287  
288 sub isdown {
289 my $class = shift;
290 my $id = shift;
291  
292 debug("marking peer '$id' as down");
293  
294 if( ! $DATA->{allpeer}{$id} ){
295 return unless $DATA->{sceptical}{$id};
296 $DATA->{allpeer}{$id} = $DATA->{sceptical}{$id};
297 delete $DATA->{sceptical}{$id};
298 }
299  
300 if( $DATA->{allpeer}{$id} ){
301 $DATA->{allpeer}{$id}{timestamp} = $^T;
302 $DATA->{allpeer}{$id}{status} = 0;
303 $DATA->{allpeer}{$id}{path} = my_server_id();
304 }
305 _maybe_remove( $id );
306 }
307  
308 ################################################################
309  
310 sub _env_ok {
311 my $class = shift;
312 my $id = shift;
313 my $up = shift;
314  
315 # if( $up->{environment} ne conf_value('environment') ){
316 # verbose("ignoring update from $id - wrong env: $up->{environment}");
317 # return;
318 # }
319 return 1;
320 }
321  
322 sub update_sceptical {
323 my $class = shift;
324 my $id = shift; # ->server_id
325 my $up = shift;
326  
327 return unless $class->_env_ok($id, $up);
328  
329 if( $DATA->{allpeer}{$id} ){
330 # already known
331 delete $DATA->{sceptical}{$id};
332 return;
333 }
334  
335 debug("rcvd update (sceptical) about $id");
336 $up->{id} = $id;
337 $DATA->{sceptical}{$id} = $up;
338 }
339  
340 sub update {
341 my $class = shift;
342 my $id = shift; # -> server_id
343 my $up = shift;
344  
345 return unless $class->_env_ok($id, $up);
346  
347 # only keep it if it is relatively fresh
348 return unless $up->{timestamp} > $^T - $KEEPLOST;
349  
350 $up->{id} = $id;
351 my $previnfo = $DATA->{allpeer}{$id};
352 verbose("discovered new peer: $id ($up->{hostname})") unless $previnfo;
353  
354 # only keep it if it is newer than what we have
355 return if $previnfo && $up->{timestamp} <= $previnfo->{timestamp};
356  
357 $up->{path} .= ' ' . my_server_id();
358  
359 # debug("updating $id => $up->{status} => " . dumper($up));
360 debug("updating $id => $up->{status}");
361  
362 $DATA->{allpeer}{$id} = $up;
363  
364 if( $up->{status} != 200 ){
365 _maybe_remove( $id );
366 return ;
367 }
368  
369 # update datacenter info
370 unless( $DATA->{datacenter}{$up->{datacenter}}{$id} ){
371 my $pdc = $previnfo->{datacenter};
372 delete $DATA->{datacenter}{$pdc}{$id} if $pdc;
373 $DATA->{datacenter}{$up->{datacenter}}{$id} = $id;
374 }
375  
376 # update subsystem info
377 unless( $DATA->{peertype}{$up->{subsystem}}{$id} ){
378 my $ss = $previnfo->{subsystem};
379 delete $DATA->{peertype}{$ss}{$id} if $ss;
380 $DATA->{peertype}{$up->{subsystem}}{$id} = $id;
381 }
382  
383 # update map info
384 $DATA->{peermap}{$id} ||= [];
385 $up->{map} ||= [];
386 my @curmap = @{$DATA->{peermap}{$id}};
387 my @newmap = sort @{$up->{map}};
388  
389 return if "@curmap" eq "@newmap"; # unchanged
390  
391 # what do we need to add/remove
392 my (%remove, %add);
393 @remove{@curmap} = @curmap;
394 @add{@newmap} = @newmap;
395 delete $remove{$_} for @newmap;
396 delete $add{$_} for @curmap;
397  
398 for my $map (keys %remove){
399 debug("removing $map from $id");
400 delete $DATA->{mappeer}{$map}{$id};
401 }
402 for my $map (keys %add){
403 debug("adding $map to $id");
404 $DATA->{mappeer}{$map}{$id} = $id;
405 }
406 $DATA->{peermap}{$id} = \@newmap;
407 }
408