ReplicationPolicy/MultipleNetworks.pm

filter: Statementstmt 83.0% 103 / 124 Branchbran 70.0% 35 / 50 Conditioncond 67.3% 31 / 46 MC/DCmcdc 48.1% 13 / 27 Subroutinesub 76.9% 10 / 13 Podpod 0.0% 0 / 9 totaltotal 71.3% 192 / 269 CC 48 SCAR 32.1
CC
48
Cov
77%
replicate_to
36.4
clear_and_build_cache
30.0
network_for_ip
9.0
CRAP 24.8
« Plugin/ZoneLocal.pm ↑ Summary ReplicationPolicy/HostsPerNetwork.pm »
1 package MogileFS::ReplicationPolicy::MultipleNetworks;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base 'MogileFS::ReplicationPolicy';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use MogileFS::Util qw(weighted_list);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MogileFS::ReplicationRequest qw(ALL_GOOD TOO_GOOD TEMP_NO_ANSWER);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 my %cache;
9 my $age;
10  
11 60 ▶sub AVOIDNETWORK { return "AVOIDNETWORK"; }
Subroutine AVOIDNETWORK: called CC=1 SCAR=0.0
Pod undocumented
12  
13 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
14 1 my ($class, $mindevcount) = @_;
15 1 return bless {
16 mindevcount => $mindevcount,
17 }, $class;
18 }
19  
20 0 ▶sub new_from_policy_args {
Subroutine new_from_policy_args: not called CC=2 SCAR=17.9
Pod undocumented
21 0 my ($class, $argref) = @_;
22 # Note: "MultipleNetworks()" is okay, in which case the 'mindevcount'
23 # on the class is used. (see below)
24 0 ▶ $$argref =~ s/^\s* \( \s* (\d*) \s* \) \s*//x
Branch: 1 branch
BranchTrueFalseTotal
$$argref =~ s/^\s* \( \s* (\d*) \s* \) \s*//x or die "$class failed to parse args: $$argref" 0 0 0
25 or die "$class failed to parse args: $$argref";
26 0 return $class->new($1)
27 }
28  
29 0 ▶sub mindevcount { $_[0]{mindevcount} }
Subroutine mindevcount: not called CC=1 SCAR=6.9
Pod undocumented
30  
31 25 ▶sub replicate_to {
Subroutine replicate_to: called CC=34 SCAR=35.9
Pod undocumented
32 25 my ($self, %args) = @_;
33  
34 25 my $fid = delete $args{fid}; # fid scalar to copy
35 25 my $on_devs = delete $args{on_devs}; # arrayref of device objects
36 25 my $all_devs = delete $args{all_devs}; # hashref of { devid => MogileFS::Device }
37 25 my $failed = delete $args{failed}; # hashref of { devid => 1 } of failed attempts this round
38  
39 # old-style
40 25 my $min = delete $args{min};
41 25 ▶ $min = $self->{mindevcount} || $min;
Condition33%1/3
$self->{'mindevcount'} || $min
l!l&&r!l&&!r
0250
MC/DC0%0/2
$self->{'mindevcount'} || $min
$self->{'mindevcount'} $min
Truth table33%1/3
A || B
A= $self->{'mindevcount'}B= $min
ABresult
1X1
011
000
42  
43 25 ▶ warn "Unknown parameters: " . join(", ", sort keys %args) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 25 25
44 25 ▶ 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 25 25
Condition33%3/9
$on_devs and $all_devs and $failed and $fid
!ll&&!rl&&r
0025
$on_devs and $all_devs and $failed
!ll&&!rl&&r
0025
$on_devs and $all_devs
!ll&&!rl&&r
0025
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
45  
46 # number of devices we currently live on
47 25 my $already_on = @$on_devs;
48  
49 # a silly special case, bail out early.
50 25 ▶ return ALL_GOOD if $min == 1 && $already_on;
Branch: 1 branch
BranchTrueFalseTotal
if $min == 1 and $already_on 0 25 25
Condition33%1/3
$min == 1 and $already_on
!ll&&!rl&&r
2500
MC/DC0%0/2
$min == 1 and $already_on
$min == 1 $already_on
Truth table33%1/3
A and B
A= $min == 1B= $already_on
ABresult
0X0
100
111
51  
52 # total disks available which are candidates for having files on them
53 25 my $total_disks = scalar grep { $_->dstate->should_have_files } values %$all_devs;
54  
55 # if we have two copies and that's all the disks there are
56 # anywhere, be happy enough
57 25 ▶ return ALL_GOOD if $already_on >= 2 && $already_on == $total_disks;
Branch: 1 branch
BranchTrueFalseTotal
if $already_on >= 2 and $already_on == $total_disks 0 25 25
Condition66%2/3
$already_on >= 2 and $already_on == $total_disks
!ll&&!rl&&r
12130
MC/DC0%0/2
$already_on >= 2 and $already_on == $total_disks
$already_on >= 2 $already_on == $total_disks
Truth table66%2/3
A and B
A= $already_on >= 2B= $already_on == $total_disks
ABresult
0X0
100
111
58  
59 # see which and how many unique hosts/networks we're already on.
60 25 my %on_dev;
61 my %on_host;
62 25 my %on_network;
63 25 foreach my $dev (@$on_devs) {
64 43 $on_host{$dev->hostid} = 1;
65 43 $on_dev{$dev->id} = 1;
66  
67 43 my $on_ip = $dev->host->ip;
68 43 ▶ if ($on_ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_ip) 20 23 43
69 20 my $network = network_for_ip($on_ip);
70 20 $on_network{$network->desc} = $network;
71 }
72 }
73  
74 25 my $uniq_hosts_on = scalar keys %on_host;
75 25 ▶ my $uniq_networks_on = scalar keys %on_network || 1;
Condition100%2/2
scalar keys %on_network || 1
l!l
1213
MC/DC100%1/1
scalar keys %on_network || 1
scalar keys %on_network
Truth table100%2/2
A
A= scalar keys %on_network
Aresult
11
00
76  
77 25 my ($total_uniq_hosts, $total_uniq_networks) = unique_hosts_and_networks($all_devs);
78  
79 # target as many networks as we can, but not more than min
80 25 ▶ my $target_networks = ($min < $total_uniq_networks) ? $min : $total_uniq_networks;
Branch: 1 branch
BranchTrueFalseTotal
$min < $total_uniq_networks ? : 3 22 25
81  
82 # we're never good if our copies aren't on as many networks as possible
83 25 ▶ if (($target_networks / $uniq_networks_on) <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_networks / $uniq_networks_on <= 1) 18 7 25
84 18 ▶ return TOO_GOOD if $uniq_hosts_on > $min;
Branch: 1 branch
BranchTrueFalseTotal
if $uniq_hosts_on > $min 1 17 18
85 17 ▶ return TOO_GOOD if $uniq_hosts_on == $min && $already_on > $min;
Branch: 1 branch
BranchTrueFalseTotal
if $uniq_hosts_on == $min and $already_on > $min 2 15 17
Condition100%3/3
$uniq_hosts_on == $min and $already_on > $min
!ll&&!rl&&r
1232
MC/DC100%2/2
$uniq_hosts_on == $min and $already_on > $min
$uniq_hosts_on == $min $already_on > $min
Truth table100%3/3
A and B
A= $uniq_hosts_on == $minB= $already_on > $min
ABresult
0X0
100
111
86  
87 15 ▶ return ALL_GOOD if $uniq_hosts_on == $min;
Branch: 1 branch
BranchTrueFalseTotal
if $uniq_hosts_on == $min 3 12 15
88 12 ▶ return ALL_GOOD if $uniq_hosts_on >= $total_uniq_hosts && $already_on >= $min;
Branch: 1 branch
BranchTrueFalseTotal
if $uniq_hosts_on >= $total_uniq_hosts and $already_on >= $min 1 11 12
Condition100%3/3
$uniq_hosts_on >= $total_uniq_hosts and $already_on >= $min
!ll&&!rl&&r
1011
MC/DC100%2/2
$uniq_hosts_on >= $total_uniq_hosts and $already_on >= $min
$uniq_hosts_on >= $total_uniq_hosts $already_on >= $min
Truth table100%3/3
A and B
A= $uniq_hosts_on >= $total_uniq_hostsB= $already_on >= $min
ABresult
0X0
100
111
89 }
90  
91 # if there are more hosts we're not on yet, we want to exclude devices we're already
92 # on from our applicable host search.
93 # also exclude hosts on networks we're already on
94 18 my @skip_network = values %on_network;
95 18 my %skip_host; # hostid => 1
96 18 ▶ if ($uniq_hosts_on < $total_uniq_hosts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uniq_hosts_on < $total_uniq_hosts) 17 1 18
97 17 %skip_host = %on_host;
98  
99 17 ▶ if (@skip_network) {
Branch: 1 branch
BranchTrueFalseTotal
if (@skip_network) 9 8 17
100 # work out hosts from the devs passed to us
101 9 my %seen_host;
102 9 foreach my $device (values %$all_devs) {
103 62 ▶ next if ($seen_host{$device->host->id}++);
Branch: 1 branch
BranchTrueFalseTotal
if $seen_host{$device->host->id}++ 31 31 62
104  
105 31 foreach my $disliked_network (@skip_network) {
106 31 ▶ if (($disliked_network->match($device->host->ip)) and
Branch: 1 branch
BranchTrueFalseTotal
if ($disliked_network->match($device->host->ip) and not $skip_host{$device->host->id}) 6 25 31
Condition100%3/3
$disliked_network->match($device->host->ip) and not $skip_host{$device->host->id}
!ll&&!rl&&r
15106
MC/DC100%2/2
$disliked_network->match($device->host->ip) and not $skip_host{$device->host->id}
$disliked_network->match($device->host->ip) not $skip_host{$device->host->id}
Truth table100%3/3
A and not(B)
A= $disliked_network->match($device->host->ip)B= not $skip_host{$device->host->id}
ABresult
0X0
100
111
107 (not $skip_host{$device->host->id})) {
108 6 $skip_host{$device->host->id} = AVOIDNETWORK;
109 }
110 }
111 }
112 }
113 }
114  
115 my @all_dests = sort {
116 73 $b->percent_free <=> $a->percent_free
117 } grep {
118 18 ! $on_dev{$_->devid} &&
119 102 ▶ ! $failed->{$_->devid} &&
Condition83%5/6
!($on_dev{$_->devid} || $$failed{$_->devid}) && $_->should_get_replicated_files
!ll&&!rl&&r
251364
$on_dev{$_->devid} || $$failed{$_->devid}
l!l&&r!l&&!r
25077
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 18 ▶ return TEMP_NO_ANSWER unless @all_dests;
Branch: 1 branch
BranchTrueFalseTotal
unless @all_dests 0 18 18
124  
125 18 my @ideal = grep { ! $skip_host{$_->hostid} } @all_dests;
126 # wrong network is less desperate than wrong host
127 18 my @network_desp = grep { $skip_host{$_->hostid} &&
128 64 ▶ $skip_host{$_->hostid} eq AVOIDNETWORK } @all_dests;
Condition100%3/3
$skip_host{$_->hostid} && $skip_host{$_->hostid} eq AVOIDNETWORK()
!ll&&!rl&&r
371512
MC/DC100%2/2
$skip_host{$_->hostid} && $skip_host{$_->hostid} eq AVOIDNETWORK()
$skip_host{$_->hostid} $skip_host{$_->hostid} eq AVOIDNETWORK()
Truth table100%3/3
A && B
A= $skip_host{$_->hostid}B= $skip_host{$_->hostid} eq AVOIDNETWORK()
ABresult
0X0
100
111
129 18 my @host_desp = grep { $skip_host{$_->hostid} &&
130 64 ▶ $skip_host{$_->hostid} ne AVOIDNETWORK } @all_dests;
Condition100%3/3
$skip_host{$_->hostid} && $skip_host{$_->hostid} ne AVOIDNETWORK()
!ll&&!rl&&r
371215
MC/DC100%2/2
$skip_host{$_->hostid} && $skip_host{$_->hostid} ne AVOIDNETWORK()
$skip_host{$_->hostid} $skip_host{$_->hostid} ne AVOIDNETWORK()
Truth table100%3/3
A && B
A= $skip_host{$_->hostid}B= $skip_host{$_->hostid} ne AVOIDNETWORK()
ABresult
0X0
100
111
131  
132 18 @ideal = weighted_list(map { [$_, 100 * $_->percent_free] }
133 splice(@ideal, 0, 20));
134 18 @network_desp = weighted_list(map { [$_, 100 * $_->percent_free] }
135 splice(@network_desp, 0, 20));
136 18 @host_desp = weighted_list(map { [$_, 100 * $_->percent_free] }
137 splice(@host_desp, 0, 20));
138  
139 18 my @desp = (@network_desp, @host_desp);
140  
141 18 return MogileFS::ReplicationRequest->new(
142 ideal => \@ideal,
143 desperate => \@desp,
144 );
145 }
146  
147 # can't just scalar keys %cache to count networks
148 # might include networks for which we have no hosts yet
149 25 ▶sub unique_hosts_and_networks {
Subroutine unique_hosts_and_networks: called CC=4 SCAR=13.9
Pod undocumented
150 25 my ($devs) = @_;
151  
152 25 my %host;
153 my %netmask;
154 25 foreach my $devid (keys %$devs) {
155 133 my $dev = $devs->{$devid};
156 133 ▶ next unless $dev->dstate->should_get_repl_files;
Branch: 1 branch
BranchTrueFalseTotal
unless $dev->dstate->should_get_repl_files 2 131 133
157  
158 131 $host{$dev->hostid}++;
159  
160 131 my $ip = $dev->host->ip;
161 131 $netmask{network_for_ip($ip)->desc}++;
162 }
163  
164 25 ▶ return (scalar keys %host, scalar keys %netmask || 1);
Condition50%1/2
scalar keys %netmask || 1
l!l
250
MC/DC0%0/1
scalar keys %netmask || 1
scalar keys %netmask
Truth table50%1/2
A
A= scalar keys %netmask
Aresult
11
00
165 }
166  
167  
168 {
169 1 my %cache; # '192.168.0.0/24' => Net::Netmask->new2('192.168.0.0/24');
170 1 my $age; # increments every time we look
171  
172 # turn a server ip into a network
173 # defaults to /16 ranges
174 # this can be overridden with a "zone_$location" setting per network "zone" and
175 # a lookup field listing all "zones"
176 # e.g.
177 # mogadm settings set network_zones location1,location2
178 # mogadm settings set zone_location1 192.168.0.0/24
179 # mogadm settings set zone_location2 10.0.0.0/24
180 # zone names and netmasks must be unique
181 151 ▶ sub network_for_ip {
Subroutine network_for_ip: called CC=7 SCAR=22.0
Pod undocumented
182 151 my ($ip) = @_;
183  
184 151 ▶ if (not $ip) { # can happen in testing
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip) 56 95 151
185 56 return Net::Netmask->new('default');
186 }
187  
188 # clear the cache occasionally
189 95 ▶ if (($age == 0) or ($age++ > 500)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($age == 0 or $age++ > 500) 0 95 95
Condition33%1/3
$age == 0 or $age++ > 500
l!l&&r!l&&!r
0095
MC/DC0%0/2
$age == 0 or $age++ > 500
$age == 0 $age++ > 500
Truth table33%1/3
A or B
A= $age == 0B= $age++ > 500
ABresult
1X1
011
000
190 0 clear_and_build_cache();
191 0 $age = 1;
192 }
193  
194 95 my $network;
195 95 foreach my $zone (keys %cache) {
196 570 ▶ if ($cache{$zone}->match($ip)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache{$zone}->match($ip)) 185 385 570
197 185 $network = $cache{$zone};
198 }
199 }
200  
201 95 ▶ if (not $network) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($network) 0 95 95
202 0 ($network) = ($ip =~ m/(\d+\.\d+)./);
203 0 $network .= '/16'; # default
204 0 $network = Net::Netmask->new2($network);
205 }
206  
207 95 return $network;
208 }
209  
210 0 ▶ sub clear_and_build_cache {
Subroutine clear_and_build_cache: not called CC=5 SCAR=34.0
Pod undocumented
211 0 undef %cache;
212  
213 0 my @zones = split(/\s*,\s*/,MogileFS::Config->server_setting("network_zones"));
214  
215 0 foreach my $zone (@zones) {
216 0 my $netmask = MogileFS::Config->server_setting("zone_".$zone);
217  
218 0 ▶ if (not $netmask) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($netmask) 0 0 0
219 0 warn "couldn't find network_zone <<zone_".$zone.">> check your server settings";
220 0 next;
221 }
222  
223 0 ▶ if ($cache{$netmask}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache{$netmask}) 0 0 0
224 0 warn "duplicate netmask <$netmask> in network zones. check your server settings";
225 }
226  
227 0 $cache{$netmask} = Net::Netmask->new2($netmask);
228  
229 0 ▶ if (Net::Netmask::errstr()) {
Branch: 1 branch
BranchTrueFalseTotal
if (Net::Netmask::errstr()) 0 0 0
230 0 warn "couldn't parse <$zone> as a netmask. error was <".Net::Netmask::errstr().
231 ">. check your server settings";
232 }
233 }
234 }
235  
236 8 ▶ sub stuff_cache { # for testing, or it'll try the db
Subroutine stuff_cache: called CC=1 SCAR=0.0
Pod undocumented
237 8 my ($self, $ip, $netmask) = @_;
238  
239 8 $cache{$ip} = $netmask;
240 8 $age = 1;
241 }
242 }
243  
244 1 1;
245  
246 # Local Variables:
247 # mode: perl
248 # c-basic-indent: 4
249 # indent-tabs-mode: nil
250 # End:
251  
252 __END__
« Plugin/ZoneLocal.pm ↑ Summary ReplicationPolicy/HostsPerNetwork.pm »