]> git.openstreetmap.org Git - dns.git/commitdiff
Add dynamic mapping of tile caches to render servers
authorTom Hughes <tom@compton.nu>
Fri, 9 Aug 2013 23:50:11 +0000 (00:50 +0100)
committerTom Hughes <tom@compton.nu>
Sun, 11 Aug 2013 13:24:17 +0000 (14:24 +0100)
.gitignore
Makefile
bin/mkcountries [new file with mode: 0755]
bin/mkgeo
origins/.gitkeep [new file with mode: 0644]
src/render.openstreetmap [new file with mode: 0644]

index 80e66a75d6eda9037edb323a5e896cc8e26ed235..2b813248a3ba18629d9d74b4a68fd78952511110 100644 (file)
@@ -1,3 +1,4 @@
 data/
 json/
 kml/
 data/
 json/
 kml/
+origins/
index 8c520d3f370c35684915288f8145cf6082662908..93fb129c1bd38b9f7362278b99cf1cc139b59834 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,8 @@ all: data/openstreetmap.org data/openstreetmap.com data/openstreetmap.net \
      data/stateofthemap.eu \
      data/opengeodata.org \
      data/switch2osm.org data/switch2osm.com \
      data/stateofthemap.eu \
      data/opengeodata.org \
      data/switch2osm.org data/switch2osm.com \
-     data/tile.openstreetmap.org
+     data/tile.openstreetmap.org \
+     data/render.openstreetmap.org
 
 clean:
        rm lib/countries.xml data/*
 
 clean:
        rm lib/countries.xml data/*
@@ -43,8 +44,14 @@ data/switch2osm.org: src/switch2osm
 data/switch2osm.com: src/switch2osm
 data/stateofthemap.eu: src/stateofthemap-eu
 
 data/switch2osm.com: src/switch2osm
 data/stateofthemap.eu: src/stateofthemap-eu
 
-data/tile.openstreetmap.org json/tile.openstreetmap.org.json: src/tile.openstreetmap bandwidth/tile.openstreetmap.yml bin/mkgeo lib/countries.xml
-       bin/mkgeo tile.openstreetmap tile.openstreetmap.org
+origins/tile.openstreetmap.yml: bin/mkcountries lib/countries.xml bandwidth/tile.openstreetmap.yml
+       bin/mkcountries bandwidth/tile.openstreetmap.yml origins/tile.openstreetmap.yml
+
+data/tile.openstreetmap.org json/tile.openstreetmap.org.json origins/render.openstreetmap.yml: bin/mkgeo origins/tile.openstreetmap.yml src/tile.openstreetmap
+       bin/mkgeo origins/tile.openstreetmap.yml src/tile.openstreetmap tile.openstreetmap.org origins/render.openstreetmap.yml
+
+data/render.openstreetmap.org json/render.openstreetmap.org.json: bin/mkgeo origins/render.openstreetmap.yml src/render.openstreetmap
+       bin/mkgeo origins/render.openstreetmap.yml src/render.openstreetmap render.openstreetmap.org
 
 data/%:
        sed -e 's/$(notdir $<):/$(notdir $@):/g' < $< > $@
 
 data/%:
        sed -e 's/$(notdir $<):/$(notdir $@):/g' < $< > $@
diff --git a/bin/mkcountries b/bin/mkcountries
new file mode 100755 (executable)
index 0000000..42863a7
--- /dev/null
@@ -0,0 +1,84 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use XML::TreeBuilder;
+use YAML;
+
+# Get arguments
+my $bandwidthfile = shift @ARGV;
+my $originsfile = shift @ARGV;
+
+# Initialise origins
+my $origins = {};
+
+# Create a parser for the country database
+my $countries = XML::TreeBuilder->new;
+
+# Parse the country database
+$countries->parsefile("lib/countries.xml");
+
+# Load the per-country bandwidth details
+my $bandwidth = YAML::LoadFile($bandwidthfile);
+
+# Fill in country table and work out which clusters each can use
+foreach my $country ($countries->look_down("_tag" => "country"))
+{
+    my $code = $country->look_down("_tag" => "countryCode")->as_text;
+    my $name = $country->look_down("_tag" => "countryName")->as_text;
+    my $population = $country->look_down("_tag" => "population")->as_text;
+    my $bandwidth = $bandwidth->{$code} || 0;
+    my $continent = $country->look_down("_tag" => "continent")->as_text;
+    my $west = $country->look_down("_tag" => "west")->as_text;
+    my $north = $country->look_down("_tag" => "north")->as_text;
+    my $east = $country->look_down("_tag" => "east")->as_text;
+    my $south = $country->look_down("_tag" => "south")->as_text;
+    my $lat = centre_lat($south, $north);
+    my $lon = centre_lon($west, $east);
+
+    $origins->{$code} = {
+        code => $code, name => $name,
+        country => $code, continent => $continent,
+        bandwidth => $bandwidth, lat => $lat, lon => $lon
+    };
+}
+
+# Save the origins
+YAML::DumpFile($originsfile, $origins);
+
+exit 0;
+
+#
+# Find the centre value between two latitudes
+#
+sub centre_lat
+{
+    my $south = shift;
+    my $north = shift;
+
+    return ( $south + $north ) / 2;
+}
+
+#
+# Find the centre value between two longitudes
+#
+sub centre_lon
+{
+    my $west = shift;
+    my $east = shift;
+    my $lon;
+
+    if ($west < $east)
+    {
+        $lon = ( $west + $east ) / 2;
+    }
+    else
+    {
+        $lon = ( $west + $east + 360 ) / 2;
+    }
+
+    $lon = $lon - 360 if $lon > 180;
+
+    return $lon
+}
index 9ec063e0fc9148529d22ea44faaddb503d8d68de..f72666897f39a06c8fc3ac5dabb88de2292286ae 100755 (executable)
--- a/bin/mkgeo
+++ b/bin/mkgeo
@@ -7,12 +7,14 @@ use IO::File;
 use Math::Trig qw(deg2rad pip2 great_circle_distance);
 use JSON::XS;
 use LWP::UserAgent;
 use Math::Trig qw(deg2rad pip2 great_circle_distance);
 use JSON::XS;
 use LWP::UserAgent;
-use XML::TreeBuilder;
 use YAML;
 
 use YAML;
 
-my $source = shift @ARGV;
+my $originfile = shift @ARGV;
+my $clusterfile = shift @ARGV;
 my $zone = shift @ARGV;
 my $zone = shift @ARGV;
-my $clusters = YAML::LoadFile("src/${source}");
+my $targetoriginfile = shift @ARGV;
+my $origins = YAML::LoadFile($originfile);
+my $clusters = YAML::LoadFile($clusterfile);
 my @servers;
 
 # Initialise cluster details
 my @servers;
 
 # Initialise cluster details
@@ -110,70 +112,40 @@ while (my($name,$cluster) = each %$clusters)
     $cluster->{bandwidth_used} = 0;
 }
 
     $cluster->{bandwidth_used} = 0;
 }
 
-my %countries = ();
 my @mappings = ();
 
 my @mappings = ();
 
-# Create a parser for the country database
-my $countries = XML::TreeBuilder->new;
-
-# Parse the country database
-$countries->parsefile("lib/countries.xml");
-
-# Load the per-country bandwidth details
-my $bandwidth = YAML::LoadFile("bandwidth/${source}.yml");
-
-# Fill in country table and work out which clusters each can use
-foreach my $country ($countries->look_down("_tag" => "country"))
+# Scan origins and work out which clusters each can use
+foreach my $origin (values %$origins)
 {
 {
-    my $code = $country->look_down("_tag" => "countryCode")->as_text;
-    my $name = $country->look_down("_tag" => "countryName")->as_text;
-    my $population = $country->look_down("_tag" => "population")->as_text;
-    my $bandwidth = $bandwidth->{$code} || 0;
-    my $continent = $country->look_down("_tag" => "continent")->as_text;
-    my $west = $country->look_down("_tag" => "west")->as_text;
-    my $north = $country->look_down("_tag" => "north")->as_text;
-    my $east = $country->look_down("_tag" => "east")->as_text;
-    my $south = $country->look_down("_tag" => "south")->as_text;
-    my $lat = centre_lat( $south, $north );
-    my $lon = centre_lon( $west, $east );
-
-    $countries{$code} = {
-        code => $code, name => $name, continent => $continent,
-        bandwidth => $bandwidth, lat => $lat, lon => $lon
-    };
-
     foreach my $cluster (values %$clusters)
     {
     foreach my $cluster (values %$clusters)
     {
-        my $match = match_country($cluster, $code, $continent);
+        my $match = match_origin($cluster, $origin);
 
         if ($cluster->{status} eq "up" && $match ne "denied")
         {
             my $priority = $match eq "preferred" ? 20 : 10;
 
         if ($cluster->{status} eq "up" && $match ne "denied")
         {
             my $priority = $match eq "preferred" ? 20 : 10;
-            my $distance = distance($lat, $lon, $cluster->{lat}, $cluster->{lon});
+            my $distance = distance($origin->{lat}, $origin->{lon}, $cluster->{lat}, $cluster->{lon});
 
             push @mappings, {
 
             push @mappings, {
-                country => $countries{$code}, cluster => $cluster,
+                origin => $origin, cluster => $cluster,
                 priority => $priority, distance => $distance
             };
         }
     }
 }
 
                 priority => $priority, distance => $distance
             };
         }
     }
 }
 
-# Discard the parsed country database
-$countries->delete;
-
 # Allocate each country to a cluster
 # Allocate each country to a cluster
-allocate_clusters(\@mappings);
+allocate_clusters(@mappings);
 
 
-# If we failed to allocate every country then loop, increasing
+# If we failed to allocate every origin then loop, increasing
 # the bandwidth for each cluster by a little and retrying until
 # we manage to allocate everything
 # the bandwidth for each cluster by a little and retrying until
 # we manage to allocate everything
-while (grep { !exists($_->{cluster}) } values %countries)
+while (grep { !exists($_->{cluster}) } values %$origins)
 {
     # Clear any existing mappings of countries to clusters
 {
     # Clear any existing mappings of countries to clusters
-    foreach my $country (values %countries)
+    foreach my $origin (values %$origins)
     {
     {
-        delete $country->{cluster};
+        delete $origin->{cluster};
     }
 
     # Reset bandwidth usage for clusters and increase limits by 10%
     }
 
     # Reset bandwidth usage for clusters and increase limits by 10%
@@ -184,22 +156,25 @@ while (grep { !exists($_->{cluster}) } values %countries)
     }
 
     # Try the allocate again
     }
 
     # Try the allocate again
-    allocate_clusters(\@mappings);
+    allocate_clusters(@mappings);
 }
 
 # Create JSON collection object
 my @json;
 
 }
 
 # Create JSON collection object
 my @json;
 
+# Create target origins object
+my $targetorigins = {};
+
 # Open output files
 my $zonefile = IO::File->new("> data/${zone}") || die "$!";
 my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!";
 
 # Output details for each country
 # Open output files
 my $zonefile = IO::File->new("> data/${zone}") || die "$!";
 my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!";
 
 # Output details for each country
-foreach my $country (values %countries)
+foreach my $origin (values %$origins)
 {
 {
-    my $cluster = $country->{cluster};
-    my $clon = $country->{lon};
-    my $clat = $country->{lat};
+    my $cluster = $origin->{cluster};
+    my $clon = $origin->{lon};
+    my $clat = $origin->{lat};
     my $slon = $cluster->{lon};
     my $slat = $cluster->{lat};
 
     my $slon = $cluster->{lon};
     my $slat = $cluster->{lat};
 
@@ -212,8 +187,8 @@ foreach my $country (values %countries)
         $clon = $clon + 360;
     }
 
         $clon = $clon + 360;
     }
 
-    $zonefile->print("# $country->{name}\n");
-    $zonefile->print("C\L$country->{code}\E.${zone}:$cluster->{name}.${zone}:600\n");
+    $zonefile->print("# $origin->{name}\n");
+    $zonefile->print("C\L$origin->{code}\E.${zone}:$cluster->{name}.${zone}:600\n");
 
     push @json, {
         type => "Feature",
 
     push @json, {
         type => "Feature",
@@ -222,17 +197,30 @@ foreach my $country (values %countries)
             coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ]
         },
         properties => {
             coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ]
         },
         properties => {
-            country => $country->{name},
+            origin => $origin->{name},
             server => $cluster->{name},
             colour => $cluster->{colour}
         }
     };
             server => $cluster->{name},
             colour => $cluster->{colour}
         }
     };
+
+    unless (exists($targetorigins->{$cluster->{name}}))
+    {
+        $targetorigins->{$cluster->{name}} = {
+            code => $cluster->{name},
+            name => $cluster->{name},
+            lat => $cluster->{lat},
+            lon => $cluster->{lon},
+            bandwidth => 0
+        };
+    }
+
+    $targetorigins->{$cluster->{name}}->{bandwidth} += $origin->{bandwidth};
 }
 
 # Output default records for IPs that can't be mapped to a country
 foreach my $cluster (grep { $clusters->{$_}->{default} } keys %$clusters)
 {
 }
 
 # Output default records for IPs that can't be mapped to a country
 foreach my $cluster (grep { $clusters->{$_}->{default} } keys %$clusters)
 {
-    $zonefile->print("# Unknown countries\n");
+    $zonefile->print("# Unknown origins\n");
     $zonefile->print("Cxx.${zone}:${cluster}.${zone}:600\n");
 }
 
     $zonefile->print("Cxx.${zone}:${cluster}.${zone}:600\n");
 }
 
@@ -262,85 +250,53 @@ $jsonfile->print(encode_json(\@json));
 $jsonfile->close();
 $zonefile->close();
 
 $jsonfile->close();
 $zonefile->close();
 
-exit 0;
-
-#
-# Find the centre value between two latitudes
-#
-sub centre_lat
-{
-    my $south = shift;
-    my $north = shift;
-
-    return ( $south + $north ) / 2;
-}
-
-#
-# Find the centre value between two longitudes
-#
-sub centre_lon
-{
-    my $west = shift;
-    my $east = shift;
-    my $lon;
-
-    if ($west < $east)
-    {
-        $lon = ( $west + $east ) / 2;
-    }
-    else
-    {
-        $lon = ( $west + $east + 360 ) / 2;
-    }
-
-    $lon = $lon - 360 if $lon > 180;
+# Output the target details in origin format if required
+YAML::DumpFile($targetoriginfile, $targetorigins) if $targetoriginfile;
 
 
-    return $lon
-}
+exit 0;
 
 #
 
 #
-# Match a country against a cluster
+# Match an origin against a cluster
 #
 #
-sub match_country
+sub match_origin
 {
     my $cluster = shift;
 {
     my $cluster = shift;
-    my $country = shift;
-    my $continent = shift;
+    my $origin = shift;
     my $match;
 
     if ($cluster->{preferred} &&
         $cluster->{preferred}->{countries} &&
     my $match;
 
     if ($cluster->{preferred} &&
         $cluster->{preferred}->{countries} &&
-        grep { $_ eq $country } @{$cluster->{preferred}->{countries}})
+        grep { $_ eq $origin->{country} } @{$cluster->{preferred}->{countries}})
     {
         $match = "preferred";
     }
     elsif ($cluster->{preferred} &&
            $cluster->{preferred}->{continents} &&
     {
         $match = "preferred";
     }
     elsif ($cluster->{preferred} &&
            $cluster->{preferred}->{continents} &&
-           grep { $_ eq $continent } @{$cluster->{preferred}->{continents}})
+           grep { $_ eq $origin->{continent} } @{$cluster->{preferred}->{continents}})
     {
         $match = "preferred";
     }
     elsif ($cluster->{allowed} &&
            $cluster->{allowed}->{countries} &&
     {
         $match = "preferred";
     }
     elsif ($cluster->{allowed} &&
            $cluster->{allowed}->{countries} &&
-           grep { $_ eq $country } @{$cluster->{allowed}->{countries}})
+           grep { $_ eq $origin->{country} } @{$cluster->{allowed}->{countries}})
     {
         $match = "allowed";
     }
     elsif ($cluster->{allowed} &&
            $cluster->{allowed}->{continents} &&
     {
         $match = "allowed";
     }
     elsif ($cluster->{allowed} &&
            $cluster->{allowed}->{continents} &&
-           grep { $_ eq $continent } @{$cluster->{allowed}->{continents}})
+           grep { $_ eq $origin->{continent} } @{$cluster->{allowed}->{continents}})
     {
         $match = "allowed";
     }
     elsif ($cluster->{denied} &&
            $cluster->{denied}->{countries} &&
     {
         $match = "allowed";
     }
     elsif ($cluster->{denied} &&
            $cluster->{denied}->{countries} &&
-           grep { $_ eq $country } @{$cluster->{preferred}->{countries}})
+           grep { $_ eq $origin->{country} } @{$cluster->{preferred}->{countries}})
     {
         $match = "denied";
     }
     elsif ($cluster->{denied} &&
            $cluster->{denied}->{continents} &&
     {
         $match = "denied";
     }
     elsif ($cluster->{denied} &&
            $cluster->{denied}->{continents} &&
-           grep { $_ eq $continent } @{$cluster->{preferred}->{continents}})
+           grep { $_ eq $origin->{continent} } @{$cluster->{preferred}->{continents}})
     {
         $match = "denied";
     }
     {
         $match = "denied";
     }
@@ -370,26 +326,61 @@ sub distance
 }
 
 #
 }
 
 #
-# Allocate each country to a cluster
+# Allocate each origin to a cluster
 #
 sub allocate_clusters
 {
 #
 sub allocate_clusters
 {
-    my $mappings = shift;
+    my @mappings = sort { compare_mappings($a, $b) } @_;
 
 
-    # Loop over the mappings, trying to assign each country to the
+    # Loop over the mappings, trying to assign each origin to the
     # nearest cluster, but subject to the bandwidth limits
     # nearest cluster, but subject to the bandwidth limits
-    foreach my $mapping (sort {  $b->{priority} <=> $a->{priority} || $a->{distance} <=> $b->{distance} } @$mappings)
+    while (my $mapping = shift @mappings)
     {
     {
-        my $country = $mapping->{country};
-        my $cluster = $mapping->{cluster};
+        my @group;
 
 
-        if (!exists($country->{cluster}) &&
-            $cluster->{bandwidth_used} + $country->{bandwidth} <= $cluster->{bandwidth_limit})
+        push @group, $mapping;
+
+        while (@mappings && compare_mappings($mapping, $mappings[0]) == 0)
+        {
+            push @group, shift @mappings;
+        }
+
+        for my $mapping (sort compare_bandwidth @group)
         {
         {
-            $country->{cluster} = $cluster;
-            $cluster->{bandwidth_used} = $cluster->{bandwidth_used} + $country->{bandwidth};
+            my $origin = $mapping->{origin};
+            my $cluster = $mapping->{cluster};
+
+            if (!exists($origin->{cluster}) &&
+                $cluster->{bandwidth_used} + $origin->{bandwidth} <= $cluster->{bandwidth_limit})
+            {
+                $origin->{cluster} = $cluster;
+                $cluster->{bandwidth_used} = $cluster->{bandwidth_used} + $origin->{bandwidth};
+            }
         }
     }
 
     return;
 }
         }
     }
 
     return;
 }
+
+#
+# Compare two mappings to decide which to use
+#
+sub compare_mappings
+{
+    my $a = shift;
+    my $b = shift;
+
+    return $b->{priority} <=> $a->{priority} ||
+           $a->{distance} <=> $b->{distance};
+}
+
+#
+# Compare two mappings to decide which to try first
+#
+sub compare_bandwidth
+{
+    my $a_used = ( $a->{cluster}->{bandwidth_used} * 100.0 ) / ( $a->{cluster}->{bandwidth_limit} * 1.0 );
+    my $b_used = ( $b->{cluster}->{bandwidth_used} * 100.0 ) / ( $b->{cluster}->{bandwidth_limit} * 1.0 );
+
+    return $a_used <=> $b_used;
+}
diff --git a/origins/.gitkeep b/origins/.gitkeep
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/render.openstreetmap b/src/render.openstreetmap
new file mode 100644 (file)
index 0000000..70764c1
--- /dev/null
@@ -0,0 +1,33 @@
+orm:
+  lat: 51.507222
+  lon: -0.1275
+  pingdom: 923426
+  colour: "#bfa730"
+  bandwidth: 500
+  ipv4: 193.63.75.98
+  ipv6: 200106300012050002e081fffec52a8c
+
+yevaud:
+  lat: 51.507222
+  lon: -0.1275
+  pingdom: 923428
+  colour: "#412c84"
+  bandwidth: 250
+  ipv4: 128.40.168.104
+
+# Spare colours:
+#
+#  269926
+#  bf8230
+#  a1b92e
+#  7c1f7c
+#  25567b
+#  bf6530
+#  bfb830
+#  562781
+#  1f7c65
+#  bf3030
+#  bf9430
+#  7ab02c
+#  a1285f
+#  2c3d82