ReplicationPolicy/HostsPerNetwork.pm

filter: Statementstmt 91.2% 83 / 91 Branchbran 75.0% 27 / 36 Conditioncond 60.0% 12 / 20 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 3 totaltotal 78.7% 133 / 169 CC 30 SCAR 30.7
CC
30
Cov
83%
replicate_to
28.5
new_from_policy_args
12.0
new
2.0
CRAP 21.4
« ReplicationPolicy/MultipleNetworks.pm ↑ Summary Network.pm »
1 package MogileFS::ReplicationPolicy::HostsPerNetwork;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use base 'MogileFS::ReplicationPolicy';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use MogileFS::Network;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MogileFS::Util qw(weighted_list);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use MogileFS::ReplicationRequest qw(ALL_GOOD TOO_GOOD TEMP_NO_ANSWER);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 14 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
13 14 my $class = shift;
14 14 my %args = @_;
15  
16 14 my $self = bless {}, $class;
17  
18 $self->{hosts_per_zone} = delete $args{hosts_per_zone}
19 14 ▶ if $args{hosts_per_zone};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"hosts_per_zone"} 14 0 14
20  
21 14 return $self;
22 }
23  
24 0 ▶sub new_from_policy_args {
Subroutine new_from_policy_args: not called CC=3 SCAR=24.8
Pod undocumented
25 0 my ($class, $argref) = @_;
26 # Note: "MultipleNetworks()" is okay, in which case the 'mindevcount'
27 # on the class is used. (see below)
28 0 ▶ $$argref =~ s/^\s* \( \s* ( [^)]*?) \s* \) \s*//x
Branch: 1 branch
BranchTrueFalseTotal
$$argref =~ s/^\s* \( \s* ( [^)]*?) \s* \) \s*//x or die "$class failed to parse args: $$argref" 0 0 0
29 or die "$class failed to parse args: $$argref";
30  
31 0 my @args = split /\s*,\s*/, $1;
32 0 my %hosts_per_zone;
33  
34 0 foreach my $arg (@args) {
35 0 my ($zone, $count) = split /\s*=\s*/, $arg;
36 0 $hosts_per_zone{$zone} = $count;
37 }
38  
39 0 return $class->new(hosts_per_zone => \%hosts_per_zone);
40 }
41  
42 14 ▶sub replicate_to {
Subroutine replicate_to: called CC=27 SCAR=33.5
Pod undocumented
43 14 my ($self, %args) = @_;
44  
45 14 my $hosts_per_zone = $self->{hosts_per_zone};
46  
47 14 my $fid = delete $args{fid}; # fid scalar to copy
48 14 my $on_devs = delete $args{on_devs}; # arrayref of device objects
49 14 my $all_devs = delete $args{all_devs}; # hashref of { devid => MogileFS::Device }
50 14 my $failed = delete $args{failed}; # hashref of { devid => 1 } of failed attempts this round
51  
52 14 delete $args{min}; # We don't use this.
53  
54 14 ▶ warn "Unknown parameters: " . join(", ", sort keys %args) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 14 14
55 14 ▶ die "Missing parameters" unless $on_devs && $all_devs && $failed && $fid;
Branch: 1 branch
BranchTrueFalseTotal
unless $on_devs and $all_devs and $failed and $fid 0 14 14
Condition33%3/9
$on_devs and $all_devs and $failed and $fid
!ll&&!rl&&r
0014
$on_devs and $all_devs and $failed
!ll&&!rl&&r
0014
$on_devs and $all_devs
!ll&&!rl&&r
0014
MC/DC0%0/4
$on_devs and $all_devs and $failed and $fid
$on_devs $all_devs $failed $fid
Truth table20%1/5
A and B and C and D
A= $on_devsB= $all_devsC= $failedD= $fid
ABCDresult
0XXX0
10XX0
110X0
11100
11111
56  
57 # see which and how many unique hosts/networks we're already on.
58 14 my %on_dev;
59 my %on_host;
60  
61 14 my %on_host_per_zone;
62 14 my %on_dev_per_zone;
63  
64 14 foreach my $dev (@$on_devs) {
65 24 my $on_ip = $dev->host->ip;
66 24 my $hostid = $dev->host->id;
67  
68 24 ▶ if ($on_ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_ip) 24 0 24
69 24 my $zone = MogileFS::Network->zone_for_ip($on_ip);
70  
71 24 $on_dev_per_zone{$zone}++;
72  
73 # If we've already counted this host, then don't increment it for this zone
74 24 ▶ $on_host_per_zone{$zone}++ unless $on_host{$dev->hostid};
Branch: 1 branch
BranchTrueFalseTotal
unless $on_host{$dev->hostid} 21 3 24
75 }
76  
77 24 $on_dev{$dev->id}++;
78 24 $on_host{$dev->hostid}++;
79 }
80  
81 14 my %available_hosts_per_zone;
82 my %available_hosts;
83  
84 14 foreach my $dev (values %$all_devs) {
85 62 ▶ next unless $dev->dstate->should_have_files;
Branch: 1 branch
BranchTrueFalseTotal
unless $dev->dstate->should_have_files 1 61 62
86 61 my $ip = $dev->host->ip;
87 61 my $hostid = $dev->host->id;
88 61 my $zone = MogileFS::Network->zone_for_ip($ip);
89 61 ▶ $available_hosts_per_zone{$zone}++ unless $available_hosts{$hostid};
Branch: 1 branch
BranchTrueFalseTotal
unless $available_hosts{$hostid} 33 28 61
90 61 $available_hosts{$hostid}++;
91 }
92  
93 14 my %needed_network;
94 14 my $too_good = 0;
95  
96 14 ▶ while (my ($zone, $needed) = each %$hosts_per_zone) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hosts_per_zone) 14 14 28
97 # If we already on all hosts in the target zone, and we're still not happy, then
98 # we need to start doubling up on devices, but now devs is not to exceed the requested
99 # number of hosts.
100 14 ▶ my $on = ($needed <= $available_hosts_per_zone{$zone}) ? $on_host_per_zone{$zone} : $on_dev_per_zone{$zone};
Branch: 1 branch
BranchTrueFalseTotal
$needed <= $available_hosts_per_zone{$zone} ? : 12 2 14
101 14 ▶ $on ||= 0;
Condition100%2/2
$on ||= 0
l!l
131
MC/DC100%1/1
$on ||= 0
$on
Truth table100%2/2
A
A= $on
Aresult
11
00
102  
103 14 ▶ if ($on < $needed) {
Branch: 2 branches
BranchTrueFalseTotal
if ($on < $needed) { } 10 4 14
elsif ($on_dev_per_zone{$zone} > $needed) { } 2 2 4
104 10 $needed_network{$zone} = 1;
105 } elsif ($on_dev_per_zone{$zone} > $needed) {
106 2 $too_good++;
107 }
108 }
109  
110 14 ▶ unless (keys %needed_network) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %needed_network) 4 10 14
111 4 ▶ return TOO_GOOD if $too_good;
Branch: 1 branch
BranchTrueFalseTotal
if $too_good 2 2 4
112 2 return ALL_GOOD;
113 }
114  
115 my @all_dests = sort {
116 18 $b->percent_free <=> $a->percent_free
117 } grep {
118 10 ! $on_dev{$_->devid} &&
119 45 ▶ ! $failed->{$_->devid} &&
Condition83%5/6
!($on_dev{$_->devid} || $$failed{$_->devid}) && $_->should_get_replicated_files
!ll&&!rl&&r
13824
$on_dev{$_->devid} || $$failed{$_->devid}
l!l&&r!l&&!r
13032
MC/DC66%2/3
!($on_dev{$_->devid} || $$failed{$_->devid}) && $_->should_get_replicated_files
$on_dev{$_->devid} $$failed{$_->devid} $_->should_get_replicated_files
Truth table75%3/4
not(A || B) && C
A= $on_dev{$_->devid}B= $$failed{$_->devid}C= $_->should_get_replicated_files
ABCresult
1XX0
01X0
0000
0011
120 $_->should_get_replicated_files
121 } values %$all_devs;
122  
123 10 ▶ return TEMP_NO_ANSWER unless @all_dests;
Branch: 1 branch
BranchTrueFalseTotal
unless @all_dests 0 10 10
124  
125 10 my @ideal;
126 my @desp;
127  
128 10 foreach my $dev (@all_dests) {
129 24 my $ip = $dev->host->ip;
130 24 my $host_id = $dev->host->id;
131 24 my $zone = MogileFS::Network->zone_for_ip($ip);
132  
133 # If we don't need more devices in this current network
134 # zone, then don't include the current device.
135 24 ▶ next unless $needed_network{$zone};
Branch: 1 branch
BranchTrueFalseTotal
unless $needed_network{$zone} 0 24 24
136  
137 24 ▶ if ($on_host{$host_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_host{$host_id}) { } 11 13 24
138 11 push @desp, $dev;
139 } else {
140 13 push @ideal, $dev;
141 }
142 }
143  
144 10 ▶ return TEMP_NO_ANSWER unless @desp or @ideal;
Branch: 1 branch
BranchTrueFalseTotal
unless @desp or @ideal 0 10 10
Condition66%2/3
@desp or @ideal
l!l&&r!l&&!r
820
MC/DC0%0/2
@desp or @ideal
@desp @ideal
Truth table66%2/3
A or B
A= @despB= @ideal
ABresult
1X1
011
000
145  
146 10 @ideal = weighted_list(map { [$_, 100 * $_->percent_free] }
147 splice(@ideal, 0, 20));
148 10 @desp = weighted_list(map { [$_, 100 * $_->percent_free] }
149 splice(@desp, 0, 20));
150  
151 10 return MogileFS::ReplicationRequest->new(
152 ideal => \@ideal,
153 desperate => \@desp,
154 );
155 }
156  
157 1 1;
158  
159 # Local Variables:
160 # mode: perl
161 # c-basic-indent: 4
162 # indent-tabs-mode: nil
163 # End:
164 # vim: filetype=perl softtabstop=4 expandtab
« ReplicationPolicy/MultipleNetworks.pm ↑ Summary Network.pm »