Network.pm

filter: Statementstmt 84.8% 56 / 66 Branchbran 50.0% 6 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 4 totaltotal 76.2% 74 / 97 CC 11 SCAR 16.9
CC
11
Cov
79%
check_cache
10.5
get_setting
6.0
zone_for_ip
2.0
CRAP 5.4
« ReplicationPolicy/HostsPerNetwork.pm ↑ Summary
1 package MogileFS::Network;
2  
3 =head1 NAME
4  
5 MogileFS::Network - Network awareness and extensions for MogileFS::Server
6  
7 =head1 DESCRIPTION
8  
9 This collection of modules adds multiple network awareness to the MogileFS
10 server. It provides two replication policies, 'MultipleNetworks' and
11 'HostsPerNetwork'; and also provides a plugin 'ZoneLocal' that causes
12 get_paths queries to be returned in a prioritized order based on locality of
13 storage.
14  
15 For information on configuring a location-aware installation of MogileFS
16 please check out the MogileFS wiki.
17  
18 L<http://code.google.com/p/mogilefs/wiki/ConfigureMultiNet>
19  
20 =cut
21  
22 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use Net::Netmask;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Net::Patricia;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use MogileFS::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 2 our $VERSION = "0.06";
30  
31 2 ▶use constant DEFAULT_RELOAD_INTERVAL => 60;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 2 my $trie = Net::Patricia->new(); # Net::Patricia object used for cache and lookup.
34 2 my $next_reload = 0; # Epoch time at or after which the trie expires and must be regenerated.
35 2 my $has_cached = MogileFS::Config->can('server_setting_cached');
36  
37 114 ▶sub zone_for_ip {
Subroutine zone_for_ip: called CC=2 SCAR=7.0
Pod undocumented
38 114 my $class = shift;
39 114 my $ip = shift;
40  
41 114 ▶ return unless $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip 0 114 114
42  
43 114 check_cache();
44  
45 114 return $trie->match_string($ip);
46 }
47  
48 114 ▶sub check_cache {
Subroutine check_cache: called CC=9 SCAR=23.5
Pod undocumented
49 # Reload the trie if it's expired
50 114 ▶ return unless (time() >= $next_reload);
Branch: 1 branch
BranchTrueFalseTotal
unless time >= $next_reload 99 15 114
51  
52 15 $trie = Net::Patricia->new();
53  
54 15 my @zones = split(/\s*,\s*/, get_setting("network_zones"));
55  
56 15 my @netmasks; # [ $bits, $netmask, $zone ], ...
57  
58 15 foreach my $zone (@zones) {
59 17 my $zone_masks = get_setting("zone_$zone");
60  
61 17 ▶ if (not $zone_masks) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($zone_masks) 0 17 17
62 0 warn "couldn't find network_zone <<zone_$zone>> check your server settings";
63 0 next;
64 }
65  
66 17 foreach my $network_string (split /[,\s]+/, $zone_masks) {
67 18 my $netmask = Net::Netmask->new2($network_string);
68  
69 18 ▶ if (Net::Netmask::errstr()) {
Branch: 1 branch
BranchTrueFalseTotal
if (Net::Netmask::errstr()) 0 18 18
70 0 warn "couldn't parse <$zone> as a netmask. error was <" . Net::Netmask::errstr().
71 ">. check your server settings";
72 0 next;
73 }
74  
75 18 push @netmasks, [$netmask->bits, $netmask, $zone];
76 }
77 }
78  
79 # Sort these by mask bit count, because Net::Patricia doesn't say in its docs whether add order
80 # or bit length is the overriding factor.
81 15 foreach my $set (sort { $a->[0] <=> $b->[0] } @netmasks) {
82 18 my ($bits, $netmask, $zone) = @$set;
83  
84 18 ▶ if (my $other_zone = $trie->match_exact_string("$netmask")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $other_zone = $trie->match_exact_string("$netmask")) 0 18 18
85 0 warn "duplicate netmask <$netmask> in network zones '$zone' and '$other_zone'. check your server settings";
86 }
87  
88 18 $trie->add_string("$netmask", $zone);
89 }
90  
91 15 ▶ my $interval = get_setting("network_reload_interval") || DEFAULT_RELOAD_INTERVAL;
Condition50%1/2
get_setting('network_reload_interval') || 60
l!l
015
MC/DC0%0/1
get_setting('network_reload_interval') || 60
get_setting('network_reload_interval')
Truth table50%1/2
A
A= get_setting('network_reload_interval')
Aresult
11
00
92  
93 15 $next_reload = time() + $interval;
94  
95 15 return 1;
96 }
97  
98 # This is a separate subroutine so I can redefine it at test time.
99 0 ▶sub get_setting {
Subroutine get_setting: not called CC=2 SCAR=17.9
Pod undocumented
100 0 my $key = shift;
101 0 ▶ if ($has_cached) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_cached) 0 0 0
102 0 my $val = MogileFS::Config->server_setting_cached($key);
103 0 return $val;
104 }
105 # Fall through to the server in case we don't have a cached value yet.
106 0 return MogileFS::Config->server_setting($key);
107 }
108  
109 15 ▶sub test_config {
Subroutine test_config: called CC=1 SCAR=0.0
Pod undocumented
110 15 my $class = shift;
111  
112 15 my %config = @_;
113  
114 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
115  
116 47 ▶ *get_setting = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 47 my $key = shift;
118 47 return $config{$key};
119 15 };
120  
121 15 $next_reload = 0;
122 }
123  
124 =head1 COPYRIGHT
125  
126 Copyright 2011 - Jonathan Steinert
127  
128 =head1 AUTHOR
129  
130 Jonathan Steinert
131  
132 =head1 LICENSE
133  
134 This module is licensed under the same terms as Perl itself.
135  
136 =cut
137  
138 2 1;
« ReplicationPolicy/HostsPerNetwork.pm ↑ Summary