You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/02/22 16:03:42 UTC

[GitHub] elsloo closed pull request #1866: Added maxmind.default.override parameter to CRConfig to handle maxmind default locations

elsloo closed pull request #1866: Added maxmind.default.override parameter to CRConfig to handle maxmind default locations
URL: https://github.com/apache/incubator-trafficcontrol/pull/1866
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/docs/source/admin/traffic_ops/configuration.rst b/docs/source/admin/traffic_ops/configuration.rst
index 89bb726a37..c63a5ecaf9 100644
--- a/docs/source/admin/traffic_ops/configuration.rst
+++ b/docs/source/admin/traffic_ops/configuration.rst
@@ -158,6 +158,10 @@ Many of the settings for the different servers in a Traffic Control CDN are cont
 +--------------------------+---------------+---------------------------------------------------------------------------------------------------------------------------------------+
 | geolocation6.polling.url | CRConfig.json | The location to get the IPv6 GeoLiteCity database from.                                                                               |
 +--------------------------+---------------+---------------------------------------------------------------------------------------------------------------------------------------+
+| maxmind.default.override | CRConfig.json | The destination geo coordinates to use for client location when maxmind returns a default location that matches the country code.     |
+|                          |               | This parameter can be specified multiple times with different values to support default overrides for multiple countries.             |
+|                          |               | Format: <CountryCode>;<Lat>,<Long>   Ex: US;37.751,-97.822                                                                            |
++--------------------------+---------------+---------------------------------------------------------------------------------------------------------------------------------------+
 
 These parameters should be set to reflect the local environment.
 
diff --git a/traffic_ops/app/lib/UI/Topology.pm b/traffic_ops/app/lib/UI/Topology.pm
index 93c176b7ca..72777f7e84 100644
--- a/traffic_ops/app/lib/UI/Topology.pm
+++ b/traffic_ops/app/lib/UI/Topology.pm
@@ -153,6 +153,19 @@ sub gen_crconfig_json {
             }
             $data_obj->{'config'}->{'requestHeaders'} = $headers;
         }
+        elsif ( $param eq 'maxmind.default.override' ) {
+            ( my $country_code, my $coordinates ) = split( /\;/, $row->parameter->value );
+            ( my $lat, my $long ) = split( /\,/, $coordinates );
+            my $geolocation = {
+                'countryCode' => "$country_code",
+                'lat' => $lat + 0,
+                'long' => $long + 0
+            };
+            if ( !$data_obj->{'config'}->{'maxmindDefaultOverride'} ) {
+                @{ $data_obj->{'config'}->{'maxmindDefaultOverride'} } = ();
+            }
+            push ( @{ $data_obj->{'config'}->{'maxmindDefaultOverride'} }, $geolocation );
+        }
         elsif ( !exists $requested_param_names{$param} ) {
             $data_obj->{'config'}->{$param} = $row->parameter->value;
         }
@@ -668,11 +681,21 @@ sub crconfig_strings {
             foreach my $key ( sort keys %{ $config_json->{'config'}->{$cfg} } ) {
                 $string .= "|$key:" . $config_json->{'config'}->{$cfg}->{$key};
             }
+            push( @config_strings, $string );
+        }
+        elsif ( $cfg eq 'maxmindDefaultOverride' ) {
+            foreach my $element ( @{ $config_json->{'config'}->{$cfg} } ) {
+                $string = "|param:$cfg";
+                foreach my $key ( sort keys %{ $element } ) {
+                    $string .= "|$key:" . $element->{$key};
+                }
+                push( @config_strings, $string );
+            }
         }
         else {
             $string = "|param:$cfg|value:" . $config_json->{'config'}->{$cfg} . "|";
+            push( @config_strings, $string );
         }
-        push( @config_strings, $string );
     }
     foreach my $rascal ( sort keys %{ $config_json->{'monitors'} } ) {
         my $return = &stringify_rascal( $config_json->{'monitors'}->{$rascal} );
diff --git a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/loc/MaxmindGeolocationService.java b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/loc/MaxmindGeolocationService.java
index b9150d2db6..1acc63eb91 100644
--- a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/loc/MaxmindGeolocationService.java
+++ b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/loc/MaxmindGeolocationService.java
@@ -150,6 +150,10 @@ public Geolocation createGeolocation(final CityResponse response) {
 			geolocation.setCountryName(response.getCountry().getName());
 		}
 
+		if (geolocation.getCity() == null && geolocation.getPostalCode() == null && response.getSubdivisions().isEmpty()) {
+			geolocation.setDefaultLocation(true);
+		}
+
 		return geolocation;
 	}
 }
\ No newline at end of file
diff --git a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/router/TrafficRouter.java b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/router/TrafficRouter.java
index 49be6d4572..da05a5cb10 100644
--- a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/router/TrafficRouter.java
+++ b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/router/TrafficRouter.java
@@ -22,6 +22,7 @@
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -86,6 +87,8 @@
 	private final ConsistentHasher consistentHasher = new ConsistentHasher();
 	private SteeringRegistry steeringRegistry;
 
+	private final Map<String, Geolocation> defaultGeolocationsOverride = new HashMap<String, Geolocation>();
+
 	public TrafficRouter(final CacheRegister cr, 
 			final GeolocationService geolocationService, 
 			final GeolocationService geolocationService6, 
@@ -99,6 +102,19 @@ public TrafficRouter(final CacheRegister cr,
 		this.federationRegistry = federationRegistry;
 		this.consistentDNSRouting = JsonUtils.optBoolean(cr.getConfig(), "consistent.dns.routing");
 		this.zoneManager = new ZoneManager(this, statTracker, trafficOpsUtils, trafficRouterManager);
+
+		if (cr.getConfig() != null) {
+			// maxmindDefaultOverride: {countryCode: , lat: , long: }
+			final JsonNode geolocations = cr.getConfig().get("maxmindDefaultOverride");
+			if (geolocations != null) {
+				for (final JsonNode geolocation : geolocations) {
+					final String countryCode = JsonUtils.optString(geolocation, "countryCode");
+					final double lat = JsonUtils.optDouble(geolocation, "lat");
+					final double longitude = JsonUtils.optDouble(geolocation, "long");
+					defaultGeolocationsOverride.put(countryCode, new Geolocation(lat, longitude));
+				}
+			}
+		}
 	}
 
 	public ZoneManager getZoneManager() {
@@ -309,6 +325,10 @@ public Geolocation getLocation(final String clientIP, final DeliveryService deli
 			}
 		}
 
+		if (clientLocation.isDefaultLocation() && defaultGeolocationsOverride.containsKey(clientLocation.getCountryCode())) {
+			clientLocation = defaultGeolocationsOverride.get(clientLocation.getCountryCode());
+		}
+
 		final List<Cache> caches = getCachesByGeo(deliveryService, clientLocation, track);
 
 		if (caches == null || caches.isEmpty()) {
diff --git a/traffic_router/geolocation/src/main/java/com/comcast/cdn/traffic_control/traffic_router/geolocation/Geolocation.java b/traffic_router/geolocation/src/main/java/com/comcast/cdn/traffic_control/traffic_router/geolocation/Geolocation.java
index a2f5ffa7e3..e28ba57afe 100644
--- a/traffic_router/geolocation/src/main/java/com/comcast/cdn/traffic_control/traffic_router/geolocation/Geolocation.java
+++ b/traffic_router/geolocation/src/main/java/com/comcast/cdn/traffic_control/traffic_router/geolocation/Geolocation.java
@@ -31,6 +31,7 @@
 	private String city;
 	private String countryCode;
 	private String countryName;
+	private boolean defaultLocation = false;
 
 	/**
 	 * Creates an immutable {@link Geolocation}.
@@ -145,6 +146,14 @@ public void setCountryName(String countryName) {
 		this.countryName = countryName;
 	}
 
+	public boolean isDefaultLocation() {
+		return defaultLocation;
+	}
+
+	public void setDefaultLocation(boolean defaultLocation) {
+		this.defaultLocation = defaultLocation;
+	}
+
 	@Override
 	public int hashCode() {
 		return new HashCodeBuilder(1, 31)


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services