Plugin/ZoneLocal.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 156 CC 17 SCAR 33.1
CC
17
Cov
0%
filter_slaves_current_zone
56.0
check_slaves_list
30.0
load
20.0
CRAP 27.5
1 # ZoneLocal plugin for MogileFS, by hachi
2  
3 package MogileFS::Plugin::ZoneLocal;
4  
5 use strict;
6 use warnings;
7  
8 use MogileFS::Worker::Query;
9 use MogileFS::Network;
10 use MogileFS::Util qw/error/;
11  
12 sub prioritize_devs_current_zone;
13  
14 sub load {
15 my $local_network = MogileFS::Config->config('local_network');
16 die "must define 'local_network' (ie: 10.5.0.0/16) in your mogilefsd.conf"
17 unless $local_network;
18 my $local_zone_test = MogileFS::Network->zone_for_ip($local_network);
19 die "Could not resolve a local zone for $local_network. Please ensure this IP is within a configured zone"
20 unless $local_zone_test;
21  
22 MogileFS::register_global_hook( 'cmd_get_paths_order_devices', sub {
23 my $devices = shift;
24 my $sorted_devs = shift;
25  
26 @$sorted_devs = prioritize_devs_current_zone(
27 $MogileFS::REQ_client_ip,
28 \&MogileFS::Worker::Query::sort_devs_by_utilization,
29 @$devices
30 );
31  
32 return 1;
33 });
34  
35 MogileFS::register_global_hook( 'cmd_create_open_order_devices', sub {
36 my $devices = shift;
37 my $sorted_devs = shift;
38  
39 @$sorted_devs = prioritize_devs_current_zone(
40 $MogileFS::REQ_client_ip,
41 \&MogileFS::Worker::Query::sort_devs_by_freespace,
42 @$devices
43 );
44  
45 return 1;
46 });
47  
48 MogileFS::register_global_hook( 'replicate_order_final_choices', sub {
49 my $devs = shift;
50 my $choices = shift;
51  
52 my @sorted = prioritize_devs_current_zone(
53 MogileFS::Config->config('local_network'),
54 sub { return @_; },
55 map { $devs->{$_} } @$choices);
56 @$choices = map { $_->id } @sorted;
57  
58 return 1;
59 });
60  
61 MogileFS::register_global_hook( 'slave_list_filter', sub {
62 my $slaves_list = shift;
63  
64 @$slaves_list = filter_slaves_current_zone(
65 MogileFS::Config->config('local_network'),
66 @$slaves_list);
67  
68 return 1;
69 });
70  
71 MogileFS::register_global_hook( 'slave_list_check', sub {
72 my $slaves_list = shift;
73  
74 my $slave_skip_filtering = MogileFS::Config->server_setting('slave_skip_filtering');
75  
76 check_slaves_list(@$slaves_list)
77 unless defined $slave_skip_filtering && $slave_skip_filtering eq 'on';
78  
79 return 1;
80 });
81  
82 return 1;
83 }
84  
85 sub unload {
86 # remove our hooks
87 MogileFS::unregister_global_hook( 'cmd_get_paths_order_devices' );
88 MogileFS::unregister_global_hook( 'cmd_create_open_order_devices' );
89 MogileFS::unregister_global_hook( 'replicate_order_final_choices' );
90  
91 return 1;
92 }
93  
94 sub prioritize_devs_current_zone {
95 my $local_ip = shift;
96 my $sorter = shift;
97 my $current_zone = MogileFS::Network->zone_for_ip($local_ip);
98 error("Cannot find current zone for local ip $local_ip")
99 unless defined $current_zone;
100  
101 my (@this_zone, @other_zone);
102  
103 foreach my $dev (@_) {
104 my $ip = $dev->host->ip;
105 my $host_id = $dev->host->id;
106 my $zone = MogileFS::Network->zone_for_ip($ip);
107 error("Cannot find zone for remote IP $ip")
108 unless defined $zone;
109  
110 if ($current_zone eq $zone) {
111 push @this_zone, $dev;
112 } else {
113 push @other_zone, $dev;
114 }
115 }
116  
117 return $sorter->(@this_zone), $sorter->(@other_zone);
118 }
119  
120 # TODO: This could be further improved by making split lists as with above,
121 # then changing core code to try other slaves if all local ones are dead.
122 sub filter_slaves_current_zone {
123 my $local_ip = shift;
124  
125 my $current_zone = MogileFS::Network->zone_for_ip($local_ip);
126 error("Cannot find current zone for local ip $local_ip")
127 unless defined $current_zone;
128  
129 my @list = ();
130 foreach my $slave (@_) {
131 my $dsn = $slave->[0];
132 if ($dsn =~ m/host=([^;]+)/) {
133 my $host = $1;
134 # Don't need the port.
135 $host =~ s/:(\d+)//;
136 if ($host eq 'localhost') {
137 # "localhost" is a special case for saying "use a unix dmoain
138 # socket", which will always be local to the box.
139 push @list, $slave;
140 next;
141 }
142 unless ($host =~ m/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/) {
143 # TODO: Pick the least depressing way to deal with this
144 # Must cache forward/negative lookups and blah blah blah.
145 error("Must specify slave host by IP address, not name: $dsn");
146 next;
147 }
148 my $zone = MogileFS::Network->zone_for_ip($host);
149 unless (defined $zone) {
150 error("Cannot find zone for slave IP $host");
151 next;
152 }
153  
154 if ($current_zone eq $zone) {
155 push @list, $slave;
156 }
157 } else {
158 error("Slave DSN must specify IP address via host= argument: $dsn");
159 }
160 }
161  
162 return @list;
163 }
164  
165 sub check_slaves_list {
166 foreach my $slave (@_) {
167 my $dsn = $slave->[0];
168 if ($dsn =~ m/host=([^;]+)/) {
169 my $host = $1;
170 # Don't need the port.
171 $host =~ s/:(\d+)//;
172 if ($host eq 'localhost') {
173 # "localhost" is a special case for saying "use a unix dmoain
174 # socket", which will always be local to the box.
175 next;
176 }
177 unless ($host =~ m/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/) {
178 # TODO: Pick the least depressing way to deal with this
179 # Must cache forward/negative lookups and blah blah blah.
180 die("Must specify slave host by IP address, not name: $dsn");
181 }
182 my $zone = MogileFS::Network->zone_for_ip($host);
183 unless (defined $zone) {
184 die("Cannot find zone for slave IP $host");
185 }
186 } else {
187 die("Slave DSN must specify IP address via host= argument: $dsn");
188 }
189 }
190 }
191  
192 1;