| 1 | package Bio::Graphics::Browser2::Render::Slave::Status; | ||
| 2 | # $Id$ | ||
| 3 | |||
| 4 | # This module keeps track of when slaves were last contacted and their | ||
| 5 | # current status. If a slave is down and there are alternatives defined, | ||
| 6 | # then we will not try contacting it for a while using an exponential | ||
| 7 | # dropoff. | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use Fcntl qw(:flock O_RDWR O_CREAT); | ||
| 12 | use DB_File; | ||
| 13 | use constant INITIAL_DELAY => 30; # initially recheck a down server after 30 sec | ||
| 14 | use constant MAX_DELAY => 600; # periodically recheck at 10 min intervals max | ||
| 15 | use constant DECAY => 1.5; # at each subsequent failure, increase recheck interval by this amount | ||
| 16 | use constant DEBUG => 0; | ||
| 17 | |||
| 18 | sub new { | ||
| 19 | my $class = shift; | ||
| 20 | my $path = shift; | ||
| 21 | my $has_dbfilelock = eval "require DB_File::Lock; 1"; | ||
| 22 | |||
| 23 | return bless { | ||
| 24 | path => $path, | ||
| 25 | canlock => $has_dbfilelock, | ||
| 26 | },ref $class || $class; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub can_lock { shift->{canlock} } | ||
| 30 | |||
| 31 | sub db { | ||
| 32 | my $self = shift; | ||
| 33 | my $write = shift; | ||
| 34 | |||
| 35 | return $self->{hash} ||= {} | ||
| 36 | unless $self->can_lock; | ||
| 37 | |||
| 38 | my $locking = $write ? 'write' : 'read'; | ||
| 39 | my $mode = $write ? O_CREAT|O_RDWR : O_RDONLY; | ||
| 40 | my $perms = 0666; | ||
| 41 | my $path = $self->{path}; | ||
| 42 | |||
| 43 | my %h; | ||
| 44 | tie (%h,'DB_File::Lock',$path,$mode,$perms,$DB_HASH,$locking); | ||
| 45 | return \%h; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub status { | ||
| 49 | my $self = shift; | ||
| 50 | my $slave = shift; | ||
| 51 | my $db = shift || $self->db(0); | ||
| 52 | defined $db or return 'up'; | ||
| 53 | |||
| 54 | my $packed = $db->{$slave}; | ||
| 55 | return 'up' unless defined $packed; | ||
| 56 | |||
| 57 | my ($status,$last_checked,$check_time) = unpack('CLL',$packed); | ||
| 58 | return 'up' if $status; | ||
| 59 | return 'up' if (time() - $last_checked) >= $check_time; | ||
| 60 | |||
| 61 | return 'down'; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub mark_up { | ||
| 65 | my $self = shift; | ||
| 66 | my $slave = shift; | ||
| 67 | warn "marking $slave up" if DEBUG; | ||
| 68 | my $db = $self->db(1) or return; | ||
| 69 | |||
| 70 | my $packed = pack('CLL',1,time(),INITIAL_DELAY); | ||
| 71 | $db->{$slave} = $packed; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub mark_down { | ||
| 75 | my $self = shift; | ||
| 76 | my $slave = shift; | ||
| 77 | |||
| 78 | warn "marking $slave down" if DEBUG; | ||
| 79 | |||
| 80 | my $db = $self->db(1) or return; | ||
| 81 | |||
| 82 | unless (my $pack = $db->{$slave}) { | ||
| 83 | $db->{$slave} = pack('CLL',0,time(),INITIAL_DELAY); | ||
| 84 | } else { | ||
| 85 | my ($status,$last_checked,$checktime) = unpack('CLL',$pack); | ||
| 86 | my $new_checktime = $checktime * DECAY; | ||
| 87 | $new_checktime = MAX_DELAY if $new_checktime > MAX_DELAY; | ||
| 88 | $db->{$slave} = $status ? pack('CLL',0,time(),INITIAL_DELAY) | ||
| 89 | : pack('CLL',0,time(),$checktime*DECAY); | ||
| 90 | } | ||
| 91 | |||
| 92 | } | ||
| 93 | |||
| 94 | # randomly select the first slave that is marked "up" | ||
| 95 | sub select { | ||
| 96 | my $self = shift; | ||
| 97 | my @slaves = @_; | ||
| 98 | |||
| 99 | # open db handle once in order to prevent multiple reopenings | ||
| 100 | # of the database | ||
| 101 | my $db = $self->db(0); | ||
| 102 | my @up = grep {$self->status($_,$db) eq 'up'} @slaves; | ||
| 103 | |||
| 104 | warn "[$$] up slaves = @up" if DEBUG; | ||
| 105 | |||
| 106 | return $up[rand @up]; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub up_slaves { | ||
| 110 | my $self = shift; | ||
| 111 | my $db = shift || $self->db(0); | ||
| 112 | my @slaves = keys %$db; | ||
| 113 | return grep {$self->status($_,$db) eq 'up'} @slaves; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | |||
| 118 | =head1 Author | ||
| 119 | |||
| 120 | Lincoln D. Stein <lincoln.stein@gmail.com> | ||
| 121 | |||
| 122 | |||
| 123 |