Changeset 1179

Show
Ignore:
Timestamp:
05/22/08 00:38:26 (6 months ago)
Author:
hachi
Message:

Many fixes from last commit.

Tests added in the last commit weren't testing enough of the code.

-- Test config load code properly
-- Switch MogileFS::Network to returning bare zone names rather than

"zone_$name"

-- Update tests to reflect zone names not including "zone_"
-- Make MogileFS::Network run properly again.
-- Add use warnings to HostsPerNetwork replication policy.

Fix testing to include testing the config load/process code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/server-plugins/MogileFS-Network/lib/MogileFS/Network.pm

    r1178 r1179  
    3232    $trie = Net::Patricia->new(); 
    3333 
    34     my @zones = split(/\s*,\s*/,MogileFS::Config->server_setting("network_zones")); 
     34    my @zones = split(/\s*,\s*/, get_setting("network_zones")); 
    3535 
    3636    my @netmasks; # [ $bits, $netmask, $zone ], ... 
    3737 
    3838    foreach my $zone (@zones) { 
    39         my $zone_masks = MogileFS::Config->server_setting("zone_$zone"); 
     39        my $zone_masks = get_setting("zone_$zone"); 
     40 
     41        if (not $zone_masks) { 
     42            warn "couldn't find network_zone <<zone_$zone>> check your server settings"; 
     43            next; 
     44        } 
    4045 
    4146        foreach my $network_string (split /[,\s]+/, $zone_masks) { 
    42             if (not $network_string) { 
    43                 warn "couldn't find network_zone <<zone_$zone>> check your server settings"; 
    44                 next; 
    45             } 
    46  
    47             #if ($cache{$zone}) { 
    48             #    warn "duplicate netmask <$netmask> in network zones. check your server settings"; 
    49             #} 
    50  
    51             #$cache{$zone} = Net::Netmask->new2($netmask); 
    5247            my $netmask = Net::Netmask->new2($network_string); 
    5348 
     
    6257    } 
    6358 
     59    # Sort these by mask bit count, because Net::Patricia doesn't say in its docs whether add order 
     60    # or bit length is the overriding factor. 
    6461    foreach my $set (sort { $a->[0] <=> $b->[0] } @netmasks) { 
    6562        my ($bits, $netmask, $zone) = @$set; 
     63 
     64        if (my $other_zone = $trie->match_exact_string("$netmask")) { 
     65            warn "duplicate netmask <$netmask> in network zones '$zone' and '$other_zone'. check your server settings"; 
     66        } 
    6667 
    6768        $trie->add_string("$netmask", $zone); 
    6869    } 
    6970 
    70     my $interval = MogileFS::Config->server_setting("network_reload_interval") 
    71                    || DEFAULT_RELOAD_INTERVAL; 
    72  
    73     clear_and_build_cache(); 
     71    my $interval = get_setting("network_reload_interval") || DEFAULT_RELOAD_INTERVAL; 
    7472 
    7573    $next_reload = time() + $interval; 
     
    7876} 
    7977 
    80 sub stuff_cache { # for testing, or it'll try the db 
    81     my ($self, $zone, $netmask) = @_; 
     78# This is a seperate subroutine so I can redefine it at test time. 
     79sub get_setting { 
     80    my $key = shift; 
     81    return MogileFS::Config->server_setting($key); 
     82
    8283 
    83     $trie->add_string("$netmask", $zone); 
    84     $next_reload = time() + 120; # If the test takes more than two minutes we're gonna break 
     84sub test_config { 
     85    my $class = shift; 
     86 
     87    my %config = @_; 
     88 
     89    no warnings 'redefine'; 
     90 
     91    *get_setting = sub { 
     92        my $key = shift; 
     93        return $config{$key}; 
     94    }; 
     95 
     96    $next_reload = 0; 
    8597} 
    8698 
  • trunk/server-plugins/MogileFS-Network/lib/MogileFS/ReplicationPolicy/HostsPerNetwork.pm

    r1162 r1179  
    22 
    33use strict; 
     4use warnings; 
     5 
    46use base 'MogileFS::ReplicationPolicy'; 
    57 
  • trunk/server-plugins/MogileFS-Network/t/hosts-per-zone-replpol.t

    r1170 r1179  
    114114    my $polclass = "MogileFS::ReplicationPolicy::HostsPerNetwork"; 
    115115 
    116     my $pol = $polclass->new(hosts_per_zone => { zone_one => $min }); 
     116    my $pol = $polclass->new(hosts_per_zone => { one => $min }); 
    117117 
    118     MogileFS::Network->stuff_cache(zone_one => Net::Netmask->new('127.0.0.0/16')); 
     118    MogileFS::Network->test_config( 
     119        zone_one      => '127.0.0.0/16', 
     120        network_zones => 'one', 
     121    ); 
     122 
    119123    my $rr = $pol->replicate_to( 
    120124                                fid      => 1, 
  • trunk/server-plugins/MogileFS-Network/t/network_zones.t

    r1178 r1179  
    33use strict; 
    44use warnings; 
    5 use Test::More tests => 4
     5use Test::More tests => 5
    66use FindBin qw($Bin); 
    77 
    88use MogileFS::Network; 
    99 
    10 set(zone_one => '127.0.0.0/16'); 
    11 set(zone_two => '10.0.0.0/8'); 
    12 set(zone_three => '10.1.0.0/16'); 
     10MogileFS::Network->test_config( 
     11    zone_one    => '127.0.0.0/16', 
     12    zone_two    => '10.0.0.0/8, 172.16.0.0/16', 
     13    zone_three => '10.1.0.0/16', 
     14    network_zones => 'one, two, three', 
     15); 
    1316 
    14 is(lookup('127.0.0.1'), 'zone_one', "Standard match"); 
    15 is(lookup('10.0.0.1'), 'zone_two', "Outer netblock match"); 
    16 is(lookup('10.1.0.1'), 'zone_three', "Inner netblock match"); 
     17 
     18is(lookup('127.0.0.1'), 'one', "Standard match"); 
     19is(lookup('10.0.0.1'), 'two', "Outer netblock match"); 
     20is(lookup('10.1.0.1'), 'three', "Inner netblock match"); 
     21is(lookup('172.16.0.1'), 'two', "Zone with multiple netblocks"); 
    1722is(lookup('192.168.0.1'), undef, "Unknown zone"); 
    1823 
     
    2025    return MogileFS::Network->zone_for_ip(@_); 
    2126} 
    22  
    23 sub set { 
    24     MogileFS::Network->stuff_cache(@_); 
    25 }