Skip to content

Commit

Permalink
Traffic Router DNS zone calculation optimizations (#7641)
Browse files Browse the repository at this point in the history
* Traffic Router zone (Delivery service) calculation optimizations

* remove debug

* add changelog

* improve performance, maybe?

* fix truth
  • Loading branch information
srijeet0406 authored Jul 13, 2023
1 parent 068d30f commit 7316c56
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- [#7602](https://github.com/apache/trafficcontrol/pull/7602) *t3c* added installed package data to t3c-apply-metadata.json
- [#7618](https://github.com/apache/trafficcontrol/pull/7618) *Traffic Portal* Add the ability to inspect a user provider cert, or the cert chain on DS SSL keys.
- [#7619](https://github.com/apache/trafficcontrol/pull/7619) Traffic Ops* added optional field `oauth_user_attribute` for OAuth login credentials
- [#7641](https://github.com/apache/trafficcontrol/pull/7641) *Traffic Router* Added further optimization to TR's algorithm of figuring out the zone for an incoming request.

### Changed
- [#7584](https://github.com/apache/trafficcontrol/pull/7584) *Documentation* Upgrade Traffic Control Sphinx documentation Makefile OS intelligent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,16 @@ public class ConfigHandler {
private final AtomicBoolean cancelled = new AtomicBoolean(false);
private final AtomicBoolean isProcessing = new AtomicBoolean(false);

private final Map<String, DeliveryService> fqdnToDeliveryService = new HashMap<>();
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 Map<String, DeliveryService> getFQDNToDeliveryServiceMap() {
return fqdnToDeliveryService;
}

public String getConfigDir() {
return configDir;
}
Expand Down Expand Up @@ -182,7 +187,7 @@ public boolean processConfig(final String jsonStr) throws JsonUtilsException, IO
cacheRegister.setStats(stats);
parseTrafficOpsConfig(config, stats);

final Map<String, DeliveryService> deliveryServiceMap = parseDeliveryServiceConfig(JsonUtils.getJsonNode(jo, deliveryServicesKey));
final Map<String, DeliveryService> deliveryServiceMap = parseDeliveryServiceConfig(JsonUtils.getJsonNode(jo, deliveryServicesKey), cacheRegister);

parseCertificatesConfig(config);
certificatesPublisher.setDeliveryServicesJson(deliveryServicesJson);
Expand Down Expand Up @@ -448,7 +453,7 @@ private void parseCacheConfig(final JsonNode contentServers, final CacheRegister
statTracker.initialize(statMap, cacheRegister);
}

private Map<String, DeliveryService> parseDeliveryServiceConfig(final JsonNode allDeliveryServices) throws JsonUtilsException {
private Map<String, DeliveryService> parseDeliveryServiceConfig(final JsonNode allDeliveryServices, final CacheRegister cacheRegister) throws JsonUtilsException {
final Map<String,DeliveryService> deliveryServiceMap = new HashMap<>();

final Iterator<String> deliveryServiceIter = allDeliveryServices.fieldNames();
Expand All @@ -469,6 +474,9 @@ private Map<String, DeliveryService> parseDeliveryServiceConfig(final JsonNode a

deliveryService.setDns(isDns);
deliveryServiceMap.put(deliveryServiceId, deliveryService);
fqdnToDeliveryService.put(deliveryService.getRoutingName() + "." + deliveryService.getDomain(), deliveryService);
fqdnToDeliveryService.put("_." + deliveryService.getDomain(), deliveryService);
cacheRegister.setFQDNToDeliveryServiceMap(fqdnToDeliveryService);
}

return deliveryServiceMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class CacheRegister {
private Map<String,Cache> allCaches;
private TreeSet<DeliveryServiceMatcher> deliveryServiceMatchers;
private Map<String, DeliveryService> dsMap;
private Map<String, DeliveryService> fqdnToDeliveryServiceMap;
private JsonNode config;
private JsonNode stats;
private int edgeTrafficRouterCount;
Expand Down Expand Up @@ -148,6 +149,14 @@ public void setDeliveryServiceMatchers(final TreeSet<DeliveryServiceMatcher> mat
* @return the DeliveryService that matches the request
*/
public DeliveryService getDeliveryService(final Request request) {
final String requestName = request.getHostname();
final Map<String, DeliveryService> map = getFQDNToDeliveryServiceMap();
if (map != null) {
final DeliveryService ds = map.get(requestName);
if (ds != null) {
return ds;
}
}
if (deliveryServiceMatchers == null) {
return null;
}
Expand Down Expand Up @@ -179,6 +188,14 @@ public void setDeliveryServiceMap(final Map<String, DeliveryService> dsMap) {
this.dsMap = dsMap;
}

public Map<String, DeliveryService> getFQDNToDeliveryServiceMap() {
return fqdnToDeliveryServiceMap;
}

public void setFQDNToDeliveryServiceMap(final Map<String, DeliveryService> fqdnToDeliveryServiceMap) {
this.fqdnToDeliveryServiceMap = fqdnToDeliveryServiceMap;
}

public JsonNode getTrafficRouters() {
return trafficRouters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@

package org.apache.traffic_control.traffic_router.core.edge;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.traffic_control.traffic_router.core.ds.DeliveryService;
import org.apache.traffic_control.traffic_router.core.ds.DeliveryServiceMatcher;
import org.apache.traffic_control.traffic_router.core.request.DNSRequest;
import org.apache.traffic_control.traffic_router.core.request.HTTPRequest;
import org.apache.traffic_control.traffic_router.core.request.Request;
import org.apache.traffic_control.traffic_router.core.util.JsonUtilsException;
import org.junit.Before;
import org.junit.Test;
import org.xbill.DNS.Name;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;

import static org.apache.traffic_control.traffic_router.core.ds.DeliveryServiceMatcher.Type.HOST;
Expand Down Expand Up @@ -90,4 +100,75 @@ public void itReturnsNullForDeliveryServiceWhenItHasNoMatchers() {
httpRequest.setPath("foo/abcde/bar");
assertThat(cacheRegister.getDeliveryService(httpRequest), nullValue());
}

@Test
public void itReturnsDeliveryServiceFromFQDNMapForHTTPRequest() throws JsonUtilsException {
String requestName = "http://foo.service01.kabletown.com/";
HTTPRequest httpRequest = new HTTPRequest();
httpRequest.setHostname("foo.service01.kabletown.com");
httpRequest.setRequestedUrl(requestName);
Map<String, DeliveryService> map = new HashMap<>();

ObjectNode node = JsonNodeFactory.instance.objectNode();
ArrayNode domainNode = node.putArray("domains");
domainNode.add("kabletown.com");
node.put("routingName","foo");
node.put("coverageZoneOnly", false);
DeliveryService ds = new DeliveryService("service01", node);

map.put("foo.service01.kabletown.com", ds);
map.put("_.service01.kabletown.com", ds);
cacheRegister.setFQDNToDeliveryServiceMap(map);

DeliveryService answer = cacheRegister.getDeliveryService(httpRequest);
assertThat("FQDNToDeliveryServiceMap was expected to have the key foo.service01.kabletown.com",
cacheRegister.getFQDNToDeliveryServiceMap().containsKey("foo.service01.kabletown.com"));
assertThat("Returned Delivery Service was expected to have the ID service01",
answer.getId().equals("service01"));


httpRequest.setRequestedUrl("http://_.service01.kabletown.com");
answer = cacheRegister.getDeliveryService(httpRequest);
assertThat("FQDNToDeliveryServiceMap was expected to have the key _.service01.kabletown.com",
cacheRegister.getFQDNToDeliveryServiceMap().containsKey("_.service01.kabletown.com"));
assertThat("Returned Delivery Service was expected to have the ID service01",
answer.getId().equals("service01"));
}

@Test
public void itReturnsDeliveryServiceFromFQDNMapForDNSRequest() throws JsonUtilsException, TextParseException {
final Name name = Name.fromString("edge.example.com.");
DNSRequest dnsRequest = new DNSRequest("example.com", name, Type.A);
dnsRequest.setClientIP("10.10.10.10");
dnsRequest.setHostname(name.relativize(Name.root).toString());

Map<String, DeliveryService> map = new HashMap<>();

ObjectNode node = JsonNodeFactory.instance.objectNode();
ArrayNode domainNode = node.putArray("domains");
domainNode.add("example.com");
node.put("routingName","edge");
node.put("coverageZoneOnly", false);
DeliveryService ds = new DeliveryService("example", node);

map.put("edge.example.com", ds);
map.put("_.example.com", ds);
cacheRegister.setFQDNToDeliveryServiceMap(map);

DeliveryService answer = cacheRegister.getDeliveryService(dnsRequest);
assertThat("FQDNToDeliveryServiceMap was expected to have the key edge.example.com",
cacheRegister.getFQDNToDeliveryServiceMap().containsKey("edge.example.com"));
assertThat("Returned Delivery Service was expected to have the ID example",
answer.getId().equals("example"));

final Name underscoreName = Name.fromString("_.example.com");
dnsRequest = new DNSRequest("example.com", underscoreName, Type.A);
dnsRequest.setClientIP("10.10.10.10");
dnsRequest.setHostname(name.relativize(Name.root).toString());
answer = cacheRegister.getDeliveryService(dnsRequest);
assertThat("FQDNToDeliveryServiceMap was expected to have the key _.example.com",
cacheRegister.getFQDNToDeliveryServiceMap().containsKey("_.example.com"));
assertThat("Returned Delivery Service was expected to have the ID example",
answer.getId().equals("example"));
}
}

0 comments on commit 7316c56

Please sign in to comment.