Skip to content
This repository has been archived by the owner on Feb 26, 2019. It is now read-only.

Commit

Permalink
Adds support for geolocation within the coverage zone file. This reso…
Browse files Browse the repository at this point in the history
…lves Comcast#1157.
  • Loading branch information
elsloo committed Mar 14, 2016
1 parent da966b3 commit 76f6798
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.TreeMap;

import com.comcast.cdn.traffic_control.traffic_router.core.util.CidrAddress;
import com.comcast.cdn.traffic_control.traffic_router.geolocation.Geolocation;

import org.apache.log4j.Logger;
import org.apache.wicket.ajax.json.JSONArray;
import org.apache.wicket.ajax.json.JSONException;
Expand All @@ -44,6 +46,7 @@ public class NetworkNode implements Comparable<NetworkNode> {
private CidrAddress cidrAddress;
private String loc;
private CacheLocation cacheLocation = null;
private Geolocation geolocation = null;
protected Map<NetworkNode,NetworkNode> children;

public static NetworkNode getInstance() {
Expand Down Expand Up @@ -73,6 +76,14 @@ public static NetworkNode generateTree(final JSONObject json) {

for (String loc : JSONObject.getNames(coverageZones)) {
final JSONObject locData = coverageZones.getJSONObject(loc);
final JSONObject coordinates = locData.optJSONObject("coordinates");
Geolocation geolocation = null;

if (coordinates != null && coordinates.has("latitude") && coordinates.has("longitude")) {
final double latitude = coordinates.optDouble("latitude");
final double longitude = coordinates.optDouble("longitude");
geolocation = new Geolocation(latitude, longitude);
}

try {
final JSONArray network6 = locData.getJSONArray("network6");
Expand All @@ -81,7 +92,7 @@ public static NetworkNode generateTree(final JSONObject json) {
final String ip = network6.getString(i);

try {
root.add6(new NetworkNode(ip, loc));
root.add6(new NetworkNode(ip, loc, geolocation));
} catch (NetworkNodeException ex) {
LOGGER.error(ex, ex);
}
Expand All @@ -97,7 +108,7 @@ public static NetworkNode generateTree(final JSONObject json) {
final String ip = network.getString(i);

try {
root.add(new NetworkNode(ip, loc));
root.add(new NetworkNode(ip, loc, geolocation));
} catch (NetworkNodeException ex) {
LOGGER.error(ex, ex);
}
Expand All @@ -120,11 +131,16 @@ public static NetworkNode generateTree(final JSONObject json) {
}

public NetworkNode(final String str) throws NetworkNodeException {
this(str, null);
this(str, null, null);
}

public NetworkNode(final String str, final String loc) throws NetworkNodeException {
this(str, loc, null);
}

public NetworkNode(final String str, final String loc, final Geolocation geolocation) throws NetworkNodeException {
this.loc = loc;
this.geolocation = geolocation;
cidrAddress = CidrAddress.fromString(str);
}

Expand Down Expand Up @@ -198,8 +214,8 @@ public String getLoc() {
return loc;
}

public void setLoc(final String loc) {
this.loc = loc;
public Geolocation getGeolocation() {
return geolocation;
}

public CacheLocation getCacheLocation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public List<Cache> getCachesByGeo(final Request request, final DeliveryService d
int locationsTested = 0;

final int locationLimit = ds.getLocationLimit();
final List<CacheLocation> cacheLocations = orderCacheLocations(request, getCacheRegister().getCacheLocations(null), ds, clientLocation);
final List<CacheLocation> cacheLocations = orderCacheLocations(getCacheRegister().getCacheLocations(null), ds, clientLocation);

for (final CacheLocation location : cacheLocations) {
final List<Cache> caches = selectCache(location, ds);
Expand All @@ -215,7 +215,7 @@ public List<Cache> getCachesByGeo(final Request request, final DeliveryService d
return null;
}
protected List<Cache> selectCache(final Request request, final DeliveryService ds, final Track track) throws GeolocationException {
final CacheLocation cacheLocation = getCoverageZoneCache(request.getClientIP());
final CacheLocation cacheLocation = getCoverageZoneCache(request.getClientIP(), ds);
List<Cache> caches = selectCachesByCZ(ds, cacheLocation, track);

if (caches != null) {
Expand Down Expand Up @@ -275,7 +275,7 @@ public DNSRouteResult route(final DNSRequest request, final Track track) throws
return result;
}

final CacheLocation cacheLocation = getCoverageZoneCache(request.getClientIP());
final CacheLocation cacheLocation = getCoverageZoneCache(request.getClientIP(), ds);
List<Cache> caches = selectCachesByCZ(ds, cacheLocation, track);

if (caches != null) {
Expand Down Expand Up @@ -440,36 +440,54 @@ public HTTPRouteResult route(final HTTPRequest request, final Track track) throw
return routeResult;
}

protected CacheLocation getCoverageZoneCache(final String ip) {
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
protected CacheLocation getCoverageZoneCache(final String ip, final DeliveryService ds) {
NetworkNode nn = null;
try {
nn = NetworkNode.getInstance().getNetwork(ip);
} catch (NetworkNodeException e) {
LOGGER.warn(e);
}

if (nn == null) {
return null;
}

final String locId = nn.getLoc();
final CacheLocation cl = nn.getCacheLocation();
if(cl != null) {

if (cl != null) {
return cl;
}
if(locId == null) {

if (locId == null) {
return null;
}

// find CacheLocation
final Collection<CacheLocation> caches = getCacheRegister()
.getCacheLocations();
// find CacheLocation
final Collection<CacheLocation> caches = getCacheRegister().getCacheLocations();

for (final CacheLocation cl2 : caches) {
if (cl2.getId().equals(locId)) {
nn.setCacheLocation(cl2);
return cl2;
}
}

/*
* 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.
*/
if (nn.getGeolocation() != null) {
final List<CacheLocation> cacheLocations = orderCacheLocations(caches, ds, nn.getGeolocation());

for (CacheLocation cacheLocation : cacheLocations) {
if (cacheLocation.getCaches() != null && !cacheLocation.getCaches().isEmpty()) {
return cacheLocation;
}
}
}

return null;
}

Expand Down Expand Up @@ -591,18 +609,16 @@ protected SortedMap<Double, Cache> consistentHash(final List<Cache> caches,
* If the client's location could not be determined, then the list is
* unsorted.
*
* @param request
* the client's request
* @param cacheLocations
* the collection of CacheLocations to order
* @param ds
* @return the ordered list of locations
*/
public List<CacheLocation> orderCacheLocations(final Request request, final Collection<CacheLocation> cacheLocations, final DeliveryService ds, final Geolocation clientLocation) {
public List<CacheLocation> orderCacheLocations(final Collection<CacheLocation> cacheLocations, final DeliveryService ds, final Geolocation clientLocation) {
final List<CacheLocation> locations = new ArrayList<CacheLocation>();

for(final CacheLocation cl : cacheLocations) {
if(ds.isLocationAvailable(cl)) {
for (final CacheLocation cl : cacheLocations) {
if (ds.isLocationAvailable(cl)) {
locations.add(cl);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void itSetsDetailsWhenCacheNotFoundByGeolocation() throws Exception {
when(deliveryService.isCoverageZoneOnly()).thenReturn(false);

doReturn(deliveryService).when(trafficRouter).selectDeliveryService(request, false);
doReturn(cacheLocation).when(trafficRouter).getCoverageZoneCache("192.168.34.56");
doReturn(cacheLocation).when(trafficRouter).getCoverageZoneCache("192.168.34.56", deliveryService);
doReturn(cacheRegister).when(trafficRouter).getCacheRegister();

trafficRouter.route(request, track);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void before() throws Exception {

trafficRouter = spy(trafficRouter);

doCallRealMethod().when(trafficRouter).getCoverageZoneCache(anyString());
doCallRealMethod().when(trafficRouter).getCoverageZoneCache(anyString(), any(DeliveryService.class));

doCallRealMethod().when(trafficRouter).selectCache(any(Request.class), any(DeliveryService.class), any(Track.class));
doCallRealMethod().when(trafficRouter, "selectCache", any(CacheLocation.class), any(DeliveryService.class));
Expand Down Expand Up @@ -164,10 +164,10 @@ public void itSupportsMinimalDNSRouteRequestTPS() throws Exception {
System.out.println("TPS was " + tps + " for routing dns request with hostname " + names);

for (ResultType resultType : ResultType.values()) {
if (resultType != ResultType.CZ && resultType != ResultType.GEO) {
assertThat(stats.get(resultType), equalTo(0));
} else {
if (resultType == ResultType.CZ) {
assertThat(stats.get(resultType), greaterThan(0));
} else {
assertThat(stats.get(resultType), equalTo(0));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ public URL routeTest(final HTTPRequest request, String[] rstrs) throws TrafficRo

final String zoneId = null; // getZoneManager().getZone(request.getRequestedUrl());
Geolocation clientLocation = getGeolocationService().location(request.getClientIP());
final List<CacheLocation> cacheLocations = orderCacheLocations(request,
getCacheRegister().getCacheLocations(zoneId), ds, clientLocation);
final List<CacheLocation> cacheLocations = orderCacheLocations(getCacheRegister().getCacheLocations(zoneId), ds, clientLocation);
for (final CacheLocation location : cacheLocations) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Trying location: " + location.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void itSetsResultToGeo() throws Exception {
when(trafficRouter.getClientLocation(any(Request.class), any(DeliveryService.class), any(CacheLocation.class), any(Track.class))).thenReturn(clientLocation);

when(trafficRouter.getCachesByGeo(any(Request.class), any(DeliveryService.class), any(Geolocation.class), any(Track.class))).thenCallRealMethod();
when(trafficRouter.orderCacheLocations(any(Request.class), any(Collection.class), any(DeliveryService.class), any(Geolocation.class))).thenCallRealMethod();
when(trafficRouter.orderCacheLocations(any(Collection.class), any(DeliveryService.class), any(Geolocation.class))).thenCallRealMethod();
when(trafficRouter.getSupportingCaches(any(List.class), any(DeliveryService.class))).thenCallRealMethod();

HTTPRequest httpRequest = new HTTPRequest();
Expand Down

0 comments on commit 76f6798

Please sign in to comment.