You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by el...@apache.org on 2018/10/05 19:54:30 UTC

[trafficcontrol] branch master updated (8b2c71a -> 237b552)

This is an automated email from the ASF dual-hosted git repository.

elsloo pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git.


    from 8b2c71a  Remove some unused/duplicated dbhelper functions
     new 5946d36  Parse cachegroup localizationMethods into CacheLocation
     new e770951  Filter by enabled LocalizationMethods when selecting a CacheGroup
     new 8c2a2f5  Update Cachegroup API docs for localizationMethods
     new 4d3a443  Add unit test for parsing localizationMethods from CRConfig
     new 237b552  Add test of basic enabledLocalizationMethods filtering functionality

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 CHANGELOG.md                                       |  1 +
 docs/source/api/v12/cachegroup.rst                 | 89 ++++++++++++++++------
 .../traffic_router/core/cache/CacheLocation.java   | 31 +++++++-
 .../traffic_router/core/config/ConfigHandler.java  | 41 +++++++++-
 .../traffic_router/core/router/TrafficRouter.java  | 28 ++++++-
 .../core/config/ConfigHandlerTest.java             | 41 ++++++++++
 .../traffic_router/core/loc/CoverageZoneTest.java  | 21 ++++-
 .../core/router/TrafficRouterTest.java             |  2 +
 .../core/src/test/resources/publish/CrConfig.json  |  8 ++
 9 files changed, 230 insertions(+), 32 deletions(-)


[trafficcontrol] 02/05: Filter by enabled LocalizationMethods when selecting a CacheGroup

Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

elsloo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit e770951f3289e9e5f7844c7ebc857c83ab144573
Author: Rawlin Peters <ra...@comcast.com>
AuthorDate: Mon Aug 13 16:58:46 2018 -0600

    Filter by enabled LocalizationMethods when selecting a CacheGroup
---
 .../traffic_router/core/cache/CacheLocation.java   | 11 ++++++++-
 .../traffic_router/core/config/ConfigHandler.java  |  2 +-
 .../traffic_router/core/router/TrafficRouter.java  | 28 ++++++++++++++++++++--
 .../traffic_router/core/loc/CoverageZoneTest.java  |  2 ++
 .../core/router/TrafficRouterTest.java             |  2 ++
 5 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/cache/CacheLocation.java b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/cache/CacheLocation.java
index d71ffcb..e615f62 100644
--- a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/cache/CacheLocation.java
+++ b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/cache/CacheLocation.java
@@ -16,7 +16,9 @@
 package com.comcast.cdn.traffic_control.traffic_router.core.cache;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -57,7 +59,7 @@ public class CacheLocation {
 	 *            the coordinates of this location
 	 */
 	public CacheLocation(final String id, final Geolocation geolocation) {
-		this(id, geolocation, null, true, null);
+		this(id, geolocation, null, true, new HashSet<>());
 	}
 
 	/**
@@ -86,9 +88,16 @@ public class CacheLocation {
 		this.backupCacheGroups = backupCacheGroups;
 		this.useClosestGeoOnBackupFailure = useClosestGeoOnBackupFailure;
 		this.enabledLocalizationMethods = enabledLocalizationMethods;
+		if (this.enabledLocalizationMethods.isEmpty()) {
+			this.enabledLocalizationMethods.addAll(Arrays.asList(LocalizationMethod.values()));
+		}
 		caches = new HashMap<String, Cache>();
 	}
 
+	public boolean isEnabledFor(final LocalizationMethod localizationMethod) {
+		return enabledLocalizationMethods.contains(localizationMethod);
+	}
+
 	/**
 	 * Adds the specified cache to this location.
 	 * 
diff --git a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandler.java b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandler.java
index 29719e7..af0f9a0 100644
--- a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandler.java
+++ b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandler.java
@@ -690,7 +690,7 @@ public class ConfigHandler {
 
 			}
 
-			Set<CacheLocation.LocalizationMethod> enabledLocalizationMethods = parseLocalizationMethods(loc, jo);
+			final Set<CacheLocation.LocalizationMethod> enabledLocalizationMethods = parseLocalizationMethods(loc, jo);
 
 			try {
 				locations.add(
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 c67455c..be41704 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
@@ -21,6 +21,7 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -48,6 +49,7 @@ import org.xbill.DNS.Zone;
 
 import com.comcast.cdn.traffic_control.traffic_router.core.cache.Cache;
 import com.comcast.cdn.traffic_control.traffic_router.core.cache.CacheLocation;
+import com.comcast.cdn.traffic_control.traffic_router.core.cache.CacheLocation.LocalizationMethod;
 import com.comcast.cdn.traffic_control.traffic_router.core.cache.CacheRegister;
 import com.comcast.cdn.traffic_control.traffic_router.core.cache.InetRecord;
 import com.comcast.cdn.traffic_control.traffic_router.core.dns.ZoneManager;
@@ -252,7 +254,8 @@ public class TrafficRouter {
 		int locationsTested = 0;
 
 		final int locationLimit = ds.getLocationLimit();
-		final List<CacheLocation> cacheLocations1 = ds.filterAvailableLocations(getCacheRegister().getCacheLocations());
+		final List<CacheLocation> geoEnabledCacheLocations = filterEnabledLocations(getCacheRegister().getCacheLocations(), LocalizationMethod.GEO);
+		final List<CacheLocation> cacheLocations1 = ds.filterAvailableLocations(geoEnabledCacheLocations);
 		final List<CacheLocation> cacheLocations = orderCacheLocations(cacheLocations1, clientLocation);
 
 		for (final CacheLocation location : cacheLocations) {
@@ -711,6 +714,7 @@ public class TrafficRouter {
 	@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
 	public CacheLocation getCoverageZoneCacheLocation(final String ip, final String deliveryServiceId, final boolean useDeep, final Track track) {
 		final NetworkNode networkNode = useDeep ? getDeepNetworkNode(ip) : getNetworkNode(ip);
+		final LocalizationMethod localizationMethod = useDeep ? LocalizationMethod.DEEP_CZ : LocalizationMethod.CZ;
 
 		if (networkNode == null) {
 			return null;
@@ -724,6 +728,10 @@ public class TrafficRouter {
 			cacheLocation.loadDeepCaches(networkNode.getDeepCacheNames(), cacheRegister);
 		}
 
+		if (cacheLocation != null && !cacheLocation.isEnabledFor(localizationMethod)) {
+			return null;
+		}
+
 		if (cacheLocation != null && !getSupportingCaches(cacheLocation.getCaches(), deliveryService).isEmpty()) {
 			return cacheLocation;
 		}
@@ -739,6 +747,11 @@ public class TrafficRouter {
 
 		// find CacheLocation
 		cacheLocation = getCacheRegister().getCacheLocationById(networkNode.getLoc());
+		if (cacheLocation != null && !cacheLocation.isEnabledFor(localizationMethod)) {
+			track.continueGeo = false; // hit in the CZF but the cachegroup doesn't allow CZ-localization, don't fall back to GEO
+			return null;
+		}
+
 		if (cacheLocation != null && !getSupportingCaches(cacheLocation.getCaches(), deliveryService).isEmpty()) {
 			// lazy loading in case a CacheLocation has not yet been associated with this NetworkNode
 			networkNode.setCacheLocation(cacheLocation);
@@ -748,6 +761,9 @@ public class TrafficRouter {
 		if (cacheLocation != null && cacheLocation.getBackupCacheGroups() != null) {
 			for (final String cacheGroup : cacheLocation.getBackupCacheGroups()) {
 				final CacheLocation bkCacheLocation = getCacheRegister().getCacheLocationById(cacheGroup);
+				if (bkCacheLocation != null && !bkCacheLocation.isEnabledFor(localizationMethod)) {
+					continue;
+				}
 				if (bkCacheLocation != null && !getSupportingCaches(bkCacheLocation.getCaches(), deliveryService).isEmpty()) {
 					LOGGER.debug("Got backup CZ cache group " + bkCacheLocation.getId() + " for " + ip + ", ds " + deliveryServiceId);
 					if (track != null) {
@@ -767,7 +783,9 @@ public class TrafficRouter {
 
 		// We had a hit in the CZF but the name does not match a known cache location.
 		// Check whether the CZF entry has a geolocation and use it if so.
-		final CacheLocation closestCacheLocation = getClosestCacheLocation(cacheRegister.filterAvailableLocations(deliveryServiceId), networkNode.getGeolocation(), cacheRegister.getDeliveryService(deliveryServiceId));
+		List<CacheLocation> availableLocations = cacheRegister.filterAvailableLocations(deliveryServiceId);
+		availableLocations = filterEnabledLocations(availableLocations, localizationMethod);
+		final CacheLocation closestCacheLocation = getClosestCacheLocation(availableLocations, networkNode.getGeolocation(), cacheRegister.getDeliveryService(deliveryServiceId));
 		if (closestCacheLocation != null) {
 			LOGGER.debug("Got closest CZ cache group " + closestCacheLocation.getId() + " for " + ip + ", ds " + deliveryServiceId);
 			if (track != null) {
@@ -777,6 +795,12 @@ public class TrafficRouter {
 		return closestCacheLocation;
 	}
 
+	public List<CacheLocation> filterEnabledLocations(final Collection<CacheLocation> locations, final LocalizationMethod localizationMethod) {
+		return locations.stream()
+				.filter(loc -> loc.isEnabledFor(localizationMethod))
+				.collect(Collectors.toList());
+	}
+
 	public CacheLocation getDeepCoverageZoneCacheLocation(final String ip, final DeliveryService deliveryService) {
 		return getCoverageZoneCacheLocation(ip, deliveryService, true, null);
 	}
diff --git a/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/loc/CoverageZoneTest.java b/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/loc/CoverageZoneTest.java
index f340b2e..ce5bc1e 100644
--- a/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/loc/CoverageZoneTest.java
+++ b/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/loc/CoverageZoneTest.java
@@ -35,6 +35,7 @@ import java.util.List;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.not;
+import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyListOf;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
@@ -92,6 +93,7 @@ public class CoverageZoneTest {
 		when(trafficRouter.getCacheRegister()).thenReturn(cacheRegister);
 		when(trafficRouter.orderCacheLocations(cacheGroups,testLocation)).thenCallRealMethod();
 		when(trafficRouter.getSupportingCaches(anyListOf(Cache.class), eq(deliveryService))).thenCallRealMethod();
+		when(trafficRouter.filterEnabledLocations(anyListOf(CacheLocation.class), any(CacheLocation.LocalizationMethod.class))).thenCallRealMethod();
 		PowerMockito.when(trafficRouter, "getNetworkNode", "12.23.34.45").thenReturn(eastNetworkNode);
 		PowerMockito.when(trafficRouter, "getClosestCacheLocation", cacheGroups, testLocation, deliveryService).thenCallRealMethod();
 	}
diff --git a/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/router/TrafficRouterTest.java b/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/router/TrafficRouterTest.java
index 2e7a54a..d644445 100644
--- a/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/router/TrafficRouterTest.java
+++ b/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/router/TrafficRouterTest.java
@@ -17,6 +17,7 @@ package com.comcast.cdn.traffic_control.traffic_router.core.router;
 
 import com.comcast.cdn.traffic_control.traffic_router.core.cache.Cache;
 import com.comcast.cdn.traffic_control.traffic_router.core.cache.CacheLocation;
+import com.comcast.cdn.traffic_control.traffic_router.core.cache.CacheLocation.LocalizationMethod;
 import com.comcast.cdn.traffic_control.traffic_router.core.cache.CacheRegister;
 import com.comcast.cdn.traffic_control.traffic_router.core.cache.InetRecord;
 import com.comcast.cdn.traffic_control.traffic_router.core.config.CertificateChecker;
@@ -175,6 +176,7 @@ public class TrafficRouterTest {
         when(trafficRouter.getClientLocation(anyString(), any(DeliveryService.class), any(CacheLocation.class), any(Track.class))).thenReturn(clientLocation);
 
         when(trafficRouter.getCachesByGeo(any(DeliveryService.class), any(Geolocation.class), any(Track.class))).thenCallRealMethod();
+        when(trafficRouter.filterEnabledLocations(any(List.class), any(LocalizationMethod.class))).thenCallRealMethod();
         when(trafficRouter.orderCacheLocations(any(List.class), any(Geolocation.class))).thenCallRealMethod();
         when(trafficRouter.getSupportingCaches(any(List.class), any(DeliveryService.class))).thenCallRealMethod();
 


[trafficcontrol] 04/05: Add unit test for parsing localizationMethods from CRConfig

Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

elsloo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit 4d3a4430e35bdd26e6e5eb33908ee47efa5abdc1
Author: Rawlin Peters <ra...@comcast.com>
AuthorDate: Thu Oct 4 15:41:14 2018 -0600

    Add unit test for parsing localizationMethods from CRConfig
---
 .../core/config/ConfigHandlerTest.java             | 41 ++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandlerTest.java b/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandlerTest.java
index 0fde4a5..dcb47ad 100644
--- a/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandlerTest.java
+++ b/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandlerTest.java
@@ -15,9 +15,15 @@
 
 package com.comcast.cdn.traffic_control.traffic_router.core.config;
 
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.comcast.cdn.traffic_control.traffic_router.core.cache.CacheLocation.LocalizationMethod;
 import com.comcast.cdn.traffic_control.traffic_router.core.request.HTTPRequest;
 import org.junit.Before;
 import org.junit.Test;
@@ -241,4 +247,39 @@ public class ConfigHandlerTest {
         assertThat(urlType[0], equalTo("DS_URL"));
         assertThat(typeUrl[0], equalTo(path));
     }
+
+    @Test
+    public void testParseLocalizationMethods() throws Exception {
+        LocalizationMethod[] allMethods = new LocalizationMethod[] {
+                LocalizationMethod.CZ,
+                LocalizationMethod.DEEP_CZ,
+                LocalizationMethod.GEO,
+        };
+        Set<LocalizationMethod> expected = new HashSet<>();
+        expected.addAll(Arrays.asList(allMethods));
+
+        ObjectMapper mapper = new ObjectMapper();
+
+        String allMethodsString = "{\"localizationMethods\": [\"CZ\",\"DEEP_CZ\",\"GEO\"]}";
+        JsonNode allMethodsJson = mapper.readTree(allMethodsString);
+        Set<LocalizationMethod> actual = Whitebox.invokeMethod(handler, "parseLocalizationMethods", "foo", allMethodsJson);
+        assertThat(actual, equalTo(expected));
+
+        String noMethodsString = "{}";
+        JsonNode noMethodsJson = mapper.readTree(noMethodsString);
+        actual = Whitebox.invokeMethod(handler, "parseLocalizationMethods", "foo", noMethodsJson);
+        assertThat(actual, equalTo(expected));
+
+        String nullMethodsString = "{\"localizationMethods\": null}";
+        JsonNode nullMethodsJson = mapper.readTree(nullMethodsString);
+        actual = Whitebox.invokeMethod(handler, "parseLocalizationMethods", "foo", nullMethodsJson);
+        assertThat(actual, equalTo(expected));
+
+        String CZMethodsString = "{\"localizationMethods\": [\"CZ\"]}";
+        JsonNode CZMethodsJson = mapper.readTree(CZMethodsString);
+        expected.clear();
+        expected.add(LocalizationMethod.CZ);
+        actual = Whitebox.invokeMethod(handler, "parseLocalizationMethods", "foo", CZMethodsJson);
+        assertThat(actual, equalTo(expected));
+    }
 }


[trafficcontrol] 03/05: Update Cachegroup API docs for localizationMethods

Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

elsloo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit 8c2a2f51bf0f984483e33f88be6525e38594b387
Author: Rawlin Peters <ra...@comcast.com>
AuthorDate: Tue Aug 14 14:20:38 2018 -0600

    Update Cachegroup API docs for localizationMethods
---
 CHANGELOG.md                       |  1 +
 docs/source/api/v12/cachegroup.rst | 89 ++++++++++++++++++++++++++++----------
 2 files changed, 66 insertions(+), 24 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7ae2606..9f4ffdd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
 - Cachegroup-Coordinate Refactor: The Cachegroup API now creates/updates a Coordinate entity on Cachegroup creates/updates, and the `latitude` and `longitude` columns in the `cachegroup` table have been replaced with `coordinate` (a foreign key to Coordinate). Coordinates created from Cachegroups are given the name `from_cachegroup_\<cachegroup name\>`.
 - Geolocation-based Client Steering: two new steering target types are available to use for `CLIENT_STEERING` delivery services: `STEERING_GEO_ORDER` and `STEERING_GEO_WEIGHT`. When targets of these types have an Origin with a Coordinate, Traffic Router will order and prioritize them based upon the shortest total distance from client -> edge -> origin. Co-located targets are grouped together and can be weighted or ordered within the same location using `STEERING_GEO_WEIGHT` or `STEERING_ [...]
 - Tenancy is now the default behavior in Traffic Ops.  All database entries that reference a tenant now have a default of the root tenant.  This eliminates the need for the `use_tenancy` global parameter and will allow for code to be simplified as a result. If all user and delivery services reference the root tenant, then there will be no difference from having `use_tenancy` set to 0.
+- Cachegroup Localization Methods: The Cachegroup API now supports an optional `localizationMethods` field which specifies the localization methods allowed for that cachegroup (currently 'DEEP_CZ', 'CZ', and 'GEO'). By default if this field is null/empty, all localization methods are enabled. After Traffic Router has localized a client, it will only route that client to cachegroups that have enabled the localization method used. For example, this can be used to prevent GEO-localized traf [...]
 - Traffic Monitor Client Update: Traffic Monitor is updated to use the Traffic Ops v13 client.
 - Removed previously deprecated `traffic_monitor_java`
 - Added `infrastructure/cdn-in-a-box` for Apachecon 2018 demonstration
diff --git a/docs/source/api/v12/cachegroup.rst b/docs/source/api/v12/cachegroup.rst
index 2c6d8f9..aba7f4d 100644
--- a/docs/source/api/v12/cachegroup.rst
+++ b/docs/source/api/v12/cachegroup.rst
@@ -60,6 +60,8 @@ Cache Group
   +-----------------------------------+--------+--------------------------------------------------------------------------+
   | ``secondaryParentCachegroupName`` | string | Secondary parent cachegroup name.                                        |
   +-----------------------------------+--------+--------------------------------------------------------------------------+
+  | ``localizationMethods``           | array  | Array of enabled localization methods (as strings)                       |
+  +-----------------------------------+--------+--------------------------------------------------------------------------+
   | ``shortName``                     | string | Abbreviation of the Cache Group Name                                     |
   +-----------------------------------+--------+--------------------------------------------------------------------------+
   | ``typeId``                        | string | Unique identifier for the 'Type' of Cache Group entry                    |
@@ -86,6 +88,11 @@ Cache Group
            "shortName": "dcchi",
            "typeName": "MID_LOC",
            "typeId": "4",
+           "localizationMethods": [
+             "DEEP_CZ",
+             "CZ",
+             "GEO"
+           ],
            "fallbackToClosest":true
         },
         {
@@ -101,6 +108,7 @@ Cache Group
            "shortName": "dcchi",
            "typeName": "MID_LOC",
            "typeId": "4",
+           "localizationMethods": null,
            "fallbackToClosest":false
         }
      ],
@@ -166,6 +174,8 @@ Cache Group
   +-----------------------------------+--------+--------------------------------------------------------------------------+
   | ``secondaryParentCachegroupName`` | string | Secondary parent cachegroup name.                                        |
   +-----------------------------------+--------+--------------------------------------------------------------------------+
+  | ``localizationMethods``           | array  | Array of enabled localization methods (as strings)                       |
+  +-----------------------------------+--------+--------------------------------------------------------------------------+
   | ``shortName``                     | string | Abbreviation of the Cache Group Name                                     |
   +-----------------------------------+--------+--------------------------------------------------------------------------+
   | ``typeId``                        | string | Unique identifier for the 'Type' of Cache Group entry                    |
@@ -189,6 +199,11 @@ Cache Group
            "parentCachegroupName": null,
            "secondaryParentCachegroupId": null,
            "secondaryParentCachegroupName": null,
+           "localizationMethods": [
+             "DEEP_CZ",
+             "CZ",
+             "GEO"
+           ],
            "shortName": "dcchi",
            "typeName": "MID_LOC",
            "typeId": "4",
@@ -453,6 +468,8 @@ Cache Group
   +---------------------------------+----------+-------------------------------------------------------------------+
   | ``secondaryParentCachegroup``   | no       | Name of Secondary Parent Cache Group entry.                       |
   +---------------------------------+----------+-------------------------------------------------------------------+
+  | ``localizationMethods``         | no       | Array of enabled localization methods (as strings)                |
+  +---------------------------------+----------+-------------------------------------------------------------------+
   | ``typeId``                      | yes      | The type of Cache Group entry, "EDGE_LOC", "MID_LOC" or "ORG_LOC" |
   +---------------------------------+----------+-------------------------------------------------------------------+
   | ``fallbackToClosest``           | no       | Behaviour on configured fallbacks failure, true / false           |
@@ -466,6 +483,10 @@ Cache Group
         "latitude": 12,
         "longitude": 45,
         "parentCachegroup": "cache_group_mid",
+        "localizationMethods": [
+          "CZ",
+          "GEO"
+        ],
         "typeId": 6,
         "fallbackToClosest":true
     }
@@ -493,6 +514,8 @@ Cache Group
   +------------------------------------+--------+-------------------------------------------------------------------+
   | ``secondaryParentCachegroupId``    | string | id of Secondary Parent Cache Group entry.                         |
   +------------------------------------+--------+-------------------------------------------------------------------+
+  | ``localizationMethods``            | array  | Array of enabled localization methods (as strings)                |
+  +------------------------------------+--------+-------------------------------------------------------------------+
   | ``typeName``                       | string | The type of Cache Group entry, "EDGE_LOC", "MID_LOC" or "ORG_LOC" |
   +------------------------------------+--------+-------------------------------------------------------------------+
   | ``fallbackToClosest``              | bool   | Behaviour during non-availability/failure of configured fallbacks |
@@ -516,18 +539,22 @@ Cache Group
                   }
           ],
         "response": {
-            'longitude' : '45',
-            'lastUpdated' : '2016-01-25 13:55:30',
-            'shortName' : 'cg_edge',
-            'name' : 'cache_group_edge',
-            'parentCachegroup' : 'cache_group_mid',
-            'secondaryParentCachegroup' : null,
-            'latitude' : '12',
-            'typeName' : 'EDGE_LOC',
-            'id' : '104',
-            'parentCachegroupId' : '103',
-            'secondaryParentCachegroupId' : null,
-            'fallbackToClosest':true
+            "longitude" : "45",
+            "lastUpdated" : "2016-01-25 13:55:30",
+            "shortName" : "cg_edge",
+            "name" : "cache_group_edge",
+            "parentCachegroup" : "cache_group_mid",
+            "secondaryParentCachegroup" : null,
+            "latitude" : "12",
+            "localizationMethods": [
+              "CZ",
+              "GEO"
+            ],
+            "typeName" : "EDGE_LOC",
+            "id" : "104",
+            "parentCachegroupId" : "103",
+            "secondaryParentCachegroupId" : null,
+            "fallbackToClosest":true
         }
     }
    
@@ -566,6 +593,8 @@ Cache Group
   +---------------------------------+----------+-------------------------------------------------------------------+
   | ``secondaryParentCachegroup``   | no       | Name of Secondary Parent Cache Group entry.                       |
   +---------------------------------+----------+-------------------------------------------------------------------+
+  | ``localizationMethods``         | no       | Array of enabled localization methods (as strings)                |
+  +---------------------------------+----------+-------------------------------------------------------------------+
   | ``typeName``                    | yes      | The type of Cache Group entry, "EDGE_LOC", "MID_LOC" or "ORG_LOC" |
   +---------------------------------+----------+-------------------------------------------------------------------+
   | ``fallbackToClosest``           | no       | Behaviour on configured fallbacks failure, true / false           |
@@ -580,6 +609,11 @@ Cache Group
         "longitude": 45,
         "parentCachegroup": "cache_group_mid",
         "typeName": "EDGE_LOC",
+        "localizationMethods": [
+          "DEEP_CZ",
+          "CZ",
+          "GEO"
+        ],
         "fallbackToClosest":true
     }
 
@@ -606,6 +640,8 @@ Cache Group
   +------------------------------------+--------+-------------------------------------------------------------------+
   | ``secondaryParentCachegroupId``    | string | id of Secondary Parent Cache Group entry.                         |
   +------------------------------------+--------+-------------------------------------------------------------------+
+  | ``localizationMethods``            | array  | Array of enabled localization methods (as strings)                |
+  +------------------------------------+--------+-------------------------------------------------------------------+
   | ``typeName``                       | string | The type of Cache Group entry, "EDGE_LOC", "MID_LOC" or "ORG_LOC" |
   +------------------------------------+--------+-------------------------------------------------------------------+
   | ``fallbackToClosest``              | bool   | Behaviour during non-availability/failure of configured fallbacks |
@@ -629,18 +665,23 @@ Cache Group
                   }
           ],
         "response": {
-            'longitude' : '45',
-            'lastUpdated' : '2016-01-25 13:55:30',
-            'shortName' : 'cg_edge',
-            'name' : 'cache_group_edge',
-            'parentCachegroup' : 'cache_group_mid',
-            'secondaryParentCachegroup' : null,
-            'latitude' : '12',
-            'typeName' : 'EDGE_LOC',
-            'id' : '104',
-            'parentCachegroupId' : '103',
-            'secondaryParentCachegroupId' : null,
-            'fallbackToClosest':true
+            "longitude" : "45",
+            "lastUpdated" : "2016-01-25 13:55:30",
+            "shortName" : "cg_edge",
+            "name" : "cache_group_edge",
+            "parentCachegroup" : "cache_group_mid",
+            "secondaryParentCachegroup" : null,
+            "localizationMethods": [
+              "DEEP_CZ",
+              "CZ",
+              "GEO"
+            ],
+            "latitude" : "12",
+            "typeName" : "EDGE_LOC",
+            "id" : "104",
+            "parentCachegroupId" : "103",
+            "secondaryParentCachegroupId" : null,
+            "fallbackToClosest":true
         }
     }
 


[trafficcontrol] 01/05: Parse cachegroup localizationMethods into CacheLocation

Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

elsloo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit 5946d36c4f68d3ff403b23c50e61a71b44debf40
Author: Rawlin Peters <ra...@comcast.com>
AuthorDate: Thu Aug 2 16:57:12 2018 -0600

    Parse cachegroup localizationMethods into CacheLocation
---
 .../traffic_router/core/cache/CacheLocation.java   | 18 ++++++++--
 .../traffic_router/core/config/ConfigHandler.java  | 41 +++++++++++++++++++++-
 2 files changed, 56 insertions(+), 3 deletions(-)

diff --git a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/cache/CacheLocation.java b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/cache/CacheLocation.java
index 9c2b93d..d71ffcb 100644
--- a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/cache/CacheLocation.java
+++ b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/cache/CacheLocation.java
@@ -40,6 +40,13 @@ public class CacheLocation {
 	private final Map<String, Cache> caches;
 	private List<String> backupCacheGroups = null;
 	private boolean useClosestGeoOnBackupFailure = true;
+	private final Set<LocalizationMethod> enabledLocalizationMethods;
+
+	public enum LocalizationMethod {
+		DEEP_CZ,
+		CZ,
+		GEO
+	}
 
 	/**
 	 * Creates a CacheLocation with the specified ID at the specified location.
@@ -50,7 +57,7 @@ public class CacheLocation {
 	 *            the coordinates of this location
 	 */
 	public CacheLocation(final String id, final Geolocation geolocation) {
-		this(id, geolocation, null, true);
+		this(id, geolocation, null, true, null);
 	}
 
 	/**
@@ -67,11 +74,18 @@ public class CacheLocation {
 	 * @param useClosestGeoOnBackupFailure
 	 *            the backup fallback setting for this id
 	 */
-	public CacheLocation(final String id, final Geolocation geolocation, final List<String> backupCacheGroups, final boolean useClosestGeoOnBackupFailure) {
+	public CacheLocation(
+			final String id,
+			final Geolocation geolocation,
+			final List<String> backupCacheGroups,
+			final boolean useClosestGeoOnBackupFailure,
+			final Set<LocalizationMethod> enabledLocalizationMethods
+	) {
 		this.id = id;
 		this.geolocation = geolocation;
 		this.backupCacheGroups = backupCacheGroups;
 		this.useClosestGeoOnBackupFailure = useClosestGeoOnBackupFailure;
+		this.enabledLocalizationMethods = enabledLocalizationMethods;
 		caches = new HashMap<String, Cache>();
 	}
 
diff --git a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandler.java b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandler.java
index 90fc738..29719e7 100644
--- a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandler.java
+++ b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandler.java
@@ -19,6 +19,7 @@ import java.io.IOException;
 import java.net.UnknownHostException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -95,6 +96,8 @@ public class ConfigHandler {
 	private final static String NEUSTAR_POLLING_URL = "neustar.polling.url";
 	private final static String NEUSTAR_POLLING_INTERVAL = "neustar.polling.interval";
 
+	private final static String LOCALIZATION_METHODS = "localizationMethods";
+
 	public String getConfigDir() {
 		return configDir;
 	}
@@ -686,8 +689,19 @@ public class ConfigHandler {
 				}
 
 			}
+
+			Set<CacheLocation.LocalizationMethod> enabledLocalizationMethods = parseLocalizationMethods(loc, jo);
+
 			try {
-				locations.add(new CacheLocation(loc, new Geolocation(JsonUtils.getDouble(jo, "latitude"), JsonUtils.getDouble(jo, "longitude")), backupCacheGroups, useClosestOnBackupFailure));
+				locations.add(
+						new CacheLocation(
+								loc,
+								new Geolocation(
+										JsonUtils.getDouble(jo, "latitude"),
+										JsonUtils.getDouble(jo, "longitude")),
+								backupCacheGroups,
+								useClosestOnBackupFailure,
+								enabledLocalizationMethods));
 			} catch (JsonUtilsException e) {
 				LOGGER.warn(e,e);
 			}
@@ -695,6 +709,31 @@ public class ConfigHandler {
 		cacheRegister.setConfiguredLocations(locations);
 	}
 
+	private Set<CacheLocation.LocalizationMethod> parseLocalizationMethods(final String loc, final JsonNode jo) throws JsonUtilsException {
+		final Set<CacheLocation.LocalizationMethod> enabledLocalizationMethods = new HashSet<>();
+		if (jo != null && jo.hasNonNull(LOCALIZATION_METHODS) && JsonUtils.getJsonNode(jo, LOCALIZATION_METHODS).isArray()) {
+			final JsonNode localizationMethodsJson = JsonUtils.getJsonNode(jo, LOCALIZATION_METHODS);
+			for (final JsonNode methodJson : localizationMethodsJson) {
+				if (methodJson.isNull() || !methodJson.isTextual()) {
+					LOGGER.error("Location '" + loc + "' has a non-string localizationMethod, skipping");
+					continue;
+				}
+				final String method = methodJson.asText();
+				try {
+					enabledLocalizationMethods.add(CacheLocation.LocalizationMethod.valueOf(method));
+				} catch (IllegalArgumentException e) {
+					LOGGER.error("Location '" + loc + "' has an unknown localizationMethod (" + method + "), skipping");
+					continue;
+				}
+			}
+		}
+		// by default or if NO localization methods are explicitly enabled, enable ALL
+		if (enabledLocalizationMethods.isEmpty()) {
+			enabledLocalizationMethods.addAll(Arrays.asList(CacheLocation.LocalizationMethod.values()));
+		}
+		return enabledLocalizationMethods;
+	}
+
 	/**
 	 * Creates a {@link Map} of Monitors used by {@link TrafficMonitorWatcher} to pull TrConfigs.
 	 *


[trafficcontrol] 05/05: Add test of basic enabledLocalizationMethods filtering functionality

Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

elsloo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit 237b55234c1108f64f4c775015260dcde40f5893
Author: Rawlin Peters <ra...@comcast.com>
AuthorDate: Fri Oct 5 13:42:44 2018 -0600

    Add test of basic enabledLocalizationMethods filtering functionality
    
    When a client is CZ-localized, only route them to CZ-enabled
    cachegroups.
---
 .../traffic_router/core/cache/CacheLocation.java      |  4 ++++
 .../traffic_router/core/loc/CoverageZoneTest.java     | 19 ++++++++++++++++---
 .../core/src/test/resources/publish/CrConfig.json     |  8 ++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/cache/CacheLocation.java b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/cache/CacheLocation.java
index e615f62..1daa027 100644
--- a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/cache/CacheLocation.java
+++ b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/cache/CacheLocation.java
@@ -62,6 +62,10 @@ public class CacheLocation {
 		this(id, geolocation, null, true, new HashSet<>());
 	}
 
+	public CacheLocation(final String id, final Geolocation geoLocation, final Set<LocalizationMethod> enabledLocalizationMethods) {
+		this(id, geoLocation, null, true, enabledLocalizationMethods);
+	}
+
 	/**
 	 * Creates a CacheLocation with the specified ID at the specified location.
 	 * 
diff --git a/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/loc/CoverageZoneTest.java b/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/loc/CoverageZoneTest.java
index ce5bc1e..90b5979 100644
--- a/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/loc/CoverageZoneTest.java
+++ b/traffic_router/core/src/test/java/com/comcast/cdn/traffic_control/traffic_router/core/loc/CoverageZoneTest.java
@@ -17,6 +17,7 @@ package com.comcast.cdn.traffic_control.traffic_router.core.loc;
 
 import com.comcast.cdn.traffic_control.traffic_router.core.cache.Cache;
 import com.comcast.cdn.traffic_control.traffic_router.core.cache.CacheLocation;
+import com.comcast.cdn.traffic_control.traffic_router.core.cache.CacheLocation.LocalizationMethod;
 import com.comcast.cdn.traffic_control.traffic_router.core.cache.CacheRegister;
 import com.comcast.cdn.traffic_control.traffic_router.core.ds.DeliveryService;
 import com.comcast.cdn.traffic_control.traffic_router.core.router.TrafficRouter;
@@ -30,11 +31,12 @@ import org.powermock.modules.junit4.PowerMockRunner;
 import org.powermock.reflect.Whitebox;
 
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.Matchers.not;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyListOf;
 import static org.mockito.Matchers.eq;
@@ -58,9 +60,18 @@ public class CoverageZoneTest {
 		deliveryServices.add(deliveryServiceReference);
 
 		Geolocation testLocation = new Geolocation(40.0, -101);
+		Geolocation farEastLocation = new Geolocation(40.0, -101.5);
 		Geolocation eastLocation = new Geolocation(40.0, -100);
 		Geolocation westLocation = new Geolocation(40.0, -105);
 
+		Cache farEastCache1 = new Cache("far-east-cache-1", "hashid", 1);
+		farEastCache1.setIsAvailable(true);
+		Set<LocalizationMethod> lms = new HashSet<>();
+		lms.add(LocalizationMethod.GEO);
+		CacheLocation farEastCacheGroup = new CacheLocation("far-east-cache-group", farEastLocation, lms);
+		farEastCacheGroup.addCache(farEastCache1);
+		farEastCache1.setDeliveryServices(deliveryServices);
+
 		Cache eastCache1 = new Cache("east-cache-1", "hashid", 1);
 		eastCache1.setIsAvailable(true);
 		CacheLocation eastCacheGroup = new CacheLocation("east-cache-group", eastLocation);
@@ -74,6 +85,7 @@ public class CoverageZoneTest {
 		westCacheGroup.addCache(westCache1);
 
 		List<CacheLocation> cacheGroups = new ArrayList<CacheLocation>();
+		cacheGroups.add(farEastCacheGroup);
 		cacheGroups.add(eastCacheGroup);
 		cacheGroups.add(westCacheGroup);
 
@@ -91,16 +103,17 @@ public class CoverageZoneTest {
 		when(trafficRouter.getCoverageZoneCacheLocation("12.23.34.45", "delivery-service-1")).thenCallRealMethod();
 		when(trafficRouter.getCoverageZoneCacheLocation("12.23.34.45", "delivery-service-1", false, null)).thenCallRealMethod();
 		when(trafficRouter.getCacheRegister()).thenReturn(cacheRegister);
-		when(trafficRouter.orderCacheLocations(cacheGroups,testLocation)).thenCallRealMethod();
+		when(trafficRouter.orderCacheLocations(anyListOf(CacheLocation.class),any(Geolocation.class))).thenCallRealMethod();
 		when(trafficRouter.getSupportingCaches(anyListOf(Cache.class), eq(deliveryService))).thenCallRealMethod();
 		when(trafficRouter.filterEnabledLocations(anyListOf(CacheLocation.class), any(CacheLocation.LocalizationMethod.class))).thenCallRealMethod();
 		PowerMockito.when(trafficRouter, "getNetworkNode", "12.23.34.45").thenReturn(eastNetworkNode);
-		PowerMockito.when(trafficRouter, "getClosestCacheLocation", cacheGroups, testLocation, deliveryService).thenCallRealMethod();
+		PowerMockito.when(trafficRouter, "getClosestCacheLocation", anyListOf(CacheLocation.class), any(CacheLocation.class), any(DeliveryService.class)).thenCallRealMethod();
 	}
 
 	@Test
 	public void trafficRouterReturnsNearestCacheGroupForDeliveryService() throws Exception {
 		CacheLocation cacheLocation = trafficRouter.getCoverageZoneCacheLocation("12.23.34.45", "delivery-service-1");
 		assertThat(cacheLocation.getId(), equalTo("west-cache-group"));
+		// NOTE: far-east-cache-group is actually closer to the client but isn't enabled for CZ-localization and must be filtered out
 	}
 }
diff --git a/traffic_router/core/src/test/resources/publish/CrConfig.json b/traffic_router/core/src/test/resources/publish/CrConfig.json
index 6693a30..9821dba 100644
--- a/traffic_router/core/src/test/resources/publish/CrConfig.json
+++ b/traffic_router/core/src/test/resources/publish/CrConfig.json
@@ -1410,34 +1410,42 @@
   },
   "edgeLocations": {
     "location-0": {
+      "localizationMethods": ["CZ", "DEEP_CZ", "GEO"],
       "longitude": -107.480250,
       "latitude": 40.117800
     },
     "location-1": {
+      "localizationMethods": ["CZ", "DEEP_CZ", "GEO"],
       "longitude": -121.0,
       "latitude": 49.0
     },
     "location-2": {
+      "localizationMethods": ["CZ", "DEEP_CZ", "GEO"],
       "longitude": -111.0,
       "latitude": 49.0
     },
     "location-3": {
+      "localizationMethods": ["CZ", "DEEP_CZ", "GEO"],
       "longitude": -101.0,
       "latitude": 49.0
     },
     "location-4": {
+      "localizationMethods": ["CZ", "DEEP_CZ", "GEO"],
       "longitude": -91.0,
       "latitude": 49.0
     },
     "location-7": {
+      "localizationMethods": ["CZ", "DEEP_CZ", "GEO"],
       "longitude": -66.0,
       "latitude": 36.0
     },
     "location-8": {
+      "localizationMethods": ["CZ", "DEEP_CZ", "GEO"],
       "longitude": -76.0,
       "latitude": 36.0
     },
     "location-9": {
+      "localizationMethods": ["CZ", "DEEP_CZ", "GEO"],
       "longitude": -86.0,
       "latitude": 36.0
     }