File Coverage

blib/lib/MogileFS/ReplicationRequest.pm
Criterion Covered Total %
statement 40 50 80.0
branch 11 24 45.8
condition 2 4 50.0
subroutine 12 14 85.7
pod 0 12 0.0
total 65 104 62.5


line stmt bran cond sub pod time code
1             package MogileFS::ReplicationRequest;
2 21     21   85 use strict;
  21         33  
  21         645  
3 21     21   378 use MogileFS::Server;
  21         33  
  21         10697  
4             require Exporter;
5             our @ISA = qw(Exporter);
6             our @EXPORT_OK = qw(rr_upgrade ALL_GOOD TOO_GOOD TEMP_NO_ANSWER);
7              
8             my $no_answer = bless { temp_fail => 1 };
9 1     1 0 7 sub TEMP_NO_ANSWER () { $no_answer }
10             my $all_good = bless { all_good => 1 };
11 8     8 0 62 sub ALL_GOOD () { $all_good }
12             my $too_good = bless { all_good => 1, too_good => 1 };
13 7     7 0 21 sub TOO_GOOD () { $too_good }
14              
15             # upgrades the return values from old-style ReplicationPolicy classes
16             # to MogileFS::ReplicationRequest objects, unless they already are,
17             # in which case they're passed through unchanged. provides peaceful
18             # upgrade path for old plugins.
19             sub rr_upgrade {
20 0     0 0 0 my ($rv) = @_;
21 0 0       0 return $rv if ref $rv;
22 0 0       0 return TEMP_NO_ANSWER if !defined $rv;
23 0 0       0 return ALL_GOOD if !$rv;
24 0         0 return MogileFS::ReplicationRequest->replicate_to($rv);
25             }
26              
27             # for ideal replications
28             sub replicate_to {
29 0     0 0 0 my ($class, @devs) = @_;
30 0 0       0 @devs = map { ref $_ ? $_ : Mgd::device_factory()->get_by_id($_) } @devs;
  0         0  
31 0         0 return bless {
32             ideal_next => \@devs,
33             }, $class;
34             }
35              
36             sub new {
37 19     19 0 50 my ($class, %opts) = @_;
38 19         41 my $self = bless {}, $class;
39 19   50     71 $self->{ideal_next} = delete $opts{ideal} || [];
40 19   50     54 $self->{desperate_next} = delete $opts{desperate} || [];
41 19 50       36 Carp::croak("unknown args") if %opts;
42 19         73 return $self;
43             }
44              
45             ############################################################################
46              
47             sub is_happy {
48 4     4 0 16 my $self = shift;
49 4         20 return $self->{all_good};
50             }
51              
52             sub too_happy {
53 2     2 0 3 my $self = shift;
54 2         7 return $self->{too_good};
55             }
56              
57             sub temp_fail {
58 1     1 0 5 my $self = shift;
59 1         5 return $self->{temp_fail};
60             }
61              
62             # returns array of MogileFS::Device objs, in preferred order, one of
63             # which (but not multiple) would satisfy the replication policy
64             # for its next step. at which point the replication policy needs
65             # to be asked again what the next step is.
66             sub copy_to_one_of_ideally {
67 19     19 0 20 my $self = shift;
68 19 50       19 return @{ $self->{ideal_next} || [] };
  19         73  
69             }
70              
71             # like above, but replication policy isn't happy about these choices,
72             # so a reevaluation of this replication decision should be made in the
73             # future, when new disks/hosts might be available.
74             sub copy_to_one_of_desperate {
75 4     4 0 641 my $self = shift;
76 4 50       6 return @{ $self->{desperate_next} || [] };
  4         22  
77             }
78              
79             # for test suite..
80             sub t_as_string {
81 30     30 0 85 my $self = shift;
82 30 100       104 return "too_good" if $self->{too_good};
83 24 100       128 return "all_good" if $self->{all_good};
84 17 50       23 return "temp_fail" if $self->{temp_fail};
85 17         17 my @devs;
86 17 100       29 if (@devs = $self->copy_to_one_of_ideally) {
87 15         18 return "ideal(" . join(",", sort {$a<=>$b} map { $_->id } @devs) . ")";
  26         183  
  34         61  
88             }
89 2 50       7 if (@devs = $self->copy_to_one_of_desperate) {
90 2         4 return "desperate(" . join(",", sort {$a<=>$b} map { $_->id } @devs) . ")";
  1         13  
  3         6  
91             }
92 0           die "unknown $self type";
93             }
94              
95             1;
96