Bump version again
[MogileFS-Network.git] / lib / MogileFS / Network.pm
blob3f3c6b4930d38617de1a505c60a12e7b74649311
1 package MogileFS::Network;
3 =head1 NAME
5 MogileFS::Network - Network awareness and extensions for MogileFS::Server
7 =head1 DESCRIPTION
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.
15 For information on configuring a location-aware installation of MogileFS
16 please check out the MogileFS wiki.
18 L<http://code.google.com/p/mogilefs/wiki/ConfigureMultiNet>
20 =cut
22 use strict;
23 use warnings;
25 use Net::Netmask;
26 use Net::Patricia;
27 use MogileFS::Config;
29 our $VERSION = "0.04";
31 use constant DEFAULT_RELOAD_INTERVAL => 60;
33 my $trie = Net::Patricia->new(); # Net::Patricia object used for cache and lookup.
34 my $next_reload = 0; # Epoch time at or after which the trie expires and must be regenerated.
36 sub zone_for_ip {
37 my $class = shift;
38 my $ip = shift;
40 return unless $ip;
42 check_cache();
44 return $trie->match_string($ip);
47 sub check_cache {
48 # Reload the trie if it's expired
49 return unless (time() >= $next_reload);
51 $trie = Net::Patricia->new();
53 my @zones = split(/\s*,\s*/, get_setting("network_zones"));
55 my @netmasks; # [ $bits, $netmask, $zone ], ...
57 foreach my $zone (@zones) {
58 my $zone_masks = get_setting("zone_$zone");
60 if (not $zone_masks) {
61 warn "couldn't find network_zone <<zone_$zone>> check your server settings";
62 next;
65 foreach my $network_string (split /[,\s]+/, $zone_masks) {
66 my $netmask = Net::Netmask->new2($network_string);
68 if (Net::Netmask::errstr()) {
69 warn "couldn't parse <$zone> as a netmask. error was <" . Net::Netmask::errstr().
70 ">. check your server settings";
71 next;
74 push @netmasks, [$netmask->bits, $netmask, $zone];
78 # Sort these by mask bit count, because Net::Patricia doesn't say in its docs whether add order
79 # or bit length is the overriding factor.
80 foreach my $set (sort { $a->[0] <=> $b->[0] } @netmasks) {
81 my ($bits, $netmask, $zone) = @$set;
83 if (my $other_zone = $trie->match_exact_string("$netmask")) {
84 warn "duplicate netmask <$netmask> in network zones '$zone' and '$other_zone'. check your server settings";
87 $trie->add_string("$netmask", $zone);
90 my $interval = get_setting("network_reload_interval") || DEFAULT_RELOAD_INTERVAL;
92 $next_reload = time() + $interval;
94 return 1;
97 # This is a separate subroutine so I can redefine it at test time.
98 sub get_setting {
99 my $key = shift;
100 return MogileFS::Config->server_setting($key);
103 sub test_config {
104 my $class = shift;
106 my %config = @_;
108 no warnings 'redefine';
110 *get_setting = sub {
111 my $key = shift;
112 return $config{$key};
115 $next_reload = 0;
118 =head1 COPYRIGHT
120 Copyright 2011 - Jonathan Steinert
122 =head1 AUTHOR
124 Jonathan Steinert
126 =head1 LICENSE
128 This module is licensed under the same terms as Perl itself.
130 =cut