You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by zr...@apache.org on 2022/11/18 18:07:28 UTC

[trafficcontrol] branch master updated: TR: use hostname from CRConfig if only a single TR in CRConfig (#7205)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 9a76c99c29 TR: use hostname from CRConfig if only a single TR in CRConfig (#7205)
9a76c99c29 is described below

commit 9a76c99c295f0889c0c82ab3c6605a23a16b0ae0
Author: Rawlin Peters <ra...@apache.org>
AuthorDate: Fri Nov 18 11:07:21 2022 -0700

    TR: use hostname from CRConfig if only a single TR in CRConfig (#7205)
    
    In situations where there is only a single TR in the CRConfig but
    actually multiple TRs in the system, using the server hostname in zone
    generation leads to the creation of invalid mname (master name) field in
    the SOA record unless the server hostname actually matches the single
    hostname in the CRConfig. In this scenario, it's preferable to just
    assume the hostname of the single TR in the CRConfig so that the
    resulting SOA mname is a real, resolvable name. That way all the TRs in
    the system will use the same SOA mname.
    
    Otherwise, TR servers that aren't in the CRConfig will generate an SOA
    mname like my-tr.mycdn.example.com without actually resolving that to an
    A/AAAA record.
---
 .../traffic_router/core/dns/ZoneManager.java       | 14 ++++++++++-
 .../core/dns/ZoneManagerUnitTest.java              | 28 +++++++++++++++++++++-
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/dns/ZoneManager.java b/traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/dns/ZoneManager.java
index d1a27ef025..17cda2e957 100644
--- a/traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/dns/ZoneManager.java
+++ b/traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/dns/ZoneManager.java
@@ -46,6 +46,7 @@ import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
+import com.google.common.collect.Iterators;
 import org.apache.traffic_control.traffic_router.core.edge.Node.IPVersions;
 import org.apache.traffic_control.traffic_router.core.util.JsonUtils;
 import org.apache.traffic_control.traffic_router.core.util.JsonUtilsException;
@@ -438,7 +439,7 @@ public class ZoneManager extends Resolver {
 
 	private static List<Record> fillZones(final Map<String, List<Record>> zoneMap, final Map<String, DeliveryService> dsMap, final TrafficRouter tr, final List<Record> superRecords, final LoadingCache<ZoneKey, Zone> zc, final LoadingCache<ZoneKey, Zone> dzc, final List<Runnable> generationTasks, final BlockingQueue<Runnable> primingTasks, final ConcurrentMap<String, ZoneKey> newDomainsToZoneKeys)
 			throws IOException {
-		final String hostname = InetAddress.getLocalHost().getHostName().replaceAll("\\..*", "");
+		final String hostname = getTRLocalHostname(tr);
 
 		final List<Record> records = new ArrayList<Record>();
 
@@ -453,6 +454,17 @@ public class ZoneManager extends Resolver {
 		return records;
 	}
 
+	protected static String getTRLocalHostname(final TrafficRouter tr) throws UnknownHostException {
+		// if there is only one TR in the CRConfig, just use that TR's hostname
+		// instead of checking for the local server's hostname
+		final boolean singleTR = Iterators.size(tr.getCacheRegister().getTrafficRouters().fieldNames()) == 1;
+		if (singleTR) {
+			return tr.getCacheRegister().getTrafficRouters().fieldNames().next();
+		} else {
+			return InetAddress.getLocalHost().getHostName().replaceAll("\\..*", "");
+		}
+	}
+
 	@SuppressWarnings({"PMD.ExcessiveParameterList"})
 	private static List<Record> createZone(final String domain, final Map<String, List<Record>> zoneMap, final Map<String, DeliveryService> dsMap,
 			final TrafficRouter tr, final LoadingCache<ZoneKey, Zone> zc, final LoadingCache<ZoneKey, Zone> dzc, final List<Runnable> generationTasks,
diff --git a/traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/dns/ZoneManagerUnitTest.java b/traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/dns/ZoneManagerUnitTest.java
index 5445622ce9..a15e686568 100644
--- a/traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/dns/ZoneManagerUnitTest.java
+++ b/traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/dns/ZoneManagerUnitTest.java
@@ -15,6 +15,8 @@
 
 package org.apache.traffic_control.traffic_router.core.dns;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -67,7 +69,7 @@ import static org.powermock.api.mockito.PowerMockito.doCallRealMethod;
 import static org.powermock.api.mockito.PowerMockito.whenNew;
 
 @RunWith(PowerMockRunner.class)
-@PrepareForTest({ZoneManager.class, SignatureManager.class})
+@PrepareForTest({ZoneManager.class, SignatureManager.class, InetAddress.class})
 @PowerMockIgnore("javax.management.*")
 public class ZoneManagerUnitTest {
     ZoneManager zoneManager;
@@ -91,6 +93,30 @@ public class ZoneManagerUnitTest {
 
     }
 
+    @Test
+    public void testGetLocalTRHostnameUsesSingleTRHostname() throws Exception {
+        JsonNode trs = new ObjectMapper().readTree("{\"tr-01\": {}}");
+        when(cacheRegister.getTrafficRouters()).thenReturn(trs);
+        InetAddress localhost = mock(InetAddress.class);
+        when(localhost.getHostName()).thenReturn("real-local-hostname");
+        PowerMockito.mockStatic(InetAddress.class);
+        when(InetAddress.getLocalHost()).thenReturn(localhost);
+        String actual = ZoneManager.getTRLocalHostname(zoneManager.getTrafficRouter());
+        assertThat("hostname of TR server in the CRConfig is returned when there is only one TR in the CRConfig", actual, equalTo("tr-01"));
+    }
+
+    @Test
+    public void testGetLocalTRHostnameUsesRealLocalHostNameIfMultipleTR() throws Exception {
+        JsonNode trs = new ObjectMapper().readTree("{\"tr-01\": {}, \"tr-02\":  {}}");
+        when(cacheRegister.getTrafficRouters()).thenReturn(trs);
+        InetAddress localhost = mock(InetAddress.class);
+        when(localhost.getHostName()).thenReturn("real-local-hostname");
+        PowerMockito.mockStatic(InetAddress.class);
+        when(InetAddress.getLocalHost()).thenReturn(localhost);
+        String actual = ZoneManager.getTRLocalHostname(zoneManager.getTrafficRouter());
+        assertThat("real local hostname of TR server is returned when there are multiple TRs in the CRConfig", actual, equalTo("real-local-hostname"));
+    }
+
     @Test
     public void itMarksResultTypeAndLocationInDNSAccessRecord() throws Exception {
         final Name qname = Name.fromString("edge.www.google.com.");