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:31 UTC

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

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.
 	 *