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/02/22 22:11:17 UTC

[trafficcontrol] branch master updated: Add support for File URLs to database loader (#6578)

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 0783c7d  Add support for File URLs to database loader (#6578)
0783c7d is described below

commit 0783c7dce510df760dc6f5e139af9ed6e8c78086
Author: ocket8888 <oc...@apache.org>
AuthorDate: Tue Feb 22 15:11:01 2022 -0700

    Add support for File URLs to database loader (#6578)
    
    * Add support for File URLs to database loader
    
    (cherry picked from commit 7abaa1a896695d07f126dd0493fc05f71226d617)
    
    * Update CHANGELOG
    
    * Remove extraneous characters from protocol specifier
    
    * Fix not checking for last modified date for file URIs
    
    Note that this functionality is implementation-dependent.
---
 CHANGELOG.md                                       |  1 +
 .../core/loc/AbstractServiceUpdater.java           | 22 +++++++++++++++-------
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6e69e98..113616d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
 - Added a new Traffic Ops endpoint to `GET` capacity and telemetry data for CDNi integration.
 - Added a Traffic Ops endpoints to `PUT` a requested configuration change for a full configuration or per host and an endpoint to approve or deny the request.
 - Traffic Monitor config option `distributed_polling` which enables the ability for Traffic Monitor to poll a subset of the CDN and divide into "local peer groups" and "distributed peer groups". Traffic Monitors in the same group are local peers, while Traffic Monitors in other groups are distibuted peers. Each TM group polls the same set of cachegroups and gets availability data for the other cachegroups from other TM groups. This allows each TM to be responsible for polling a subset of [...]
+- Traffic Router: Add support for `file`-protocol URLs for the `geolocation.polling.url` for the Geolocation database.
 
 ### Fixed
 - Update traffic_portal dependencies to mitigate `npm audit` issues.
diff --git a/traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/loc/AbstractServiceUpdater.java b/traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/loc/AbstractServiceUpdater.java
index aa6de94..c91706c 100644
--- a/traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/loc/AbstractServiceUpdater.java
+++ b/traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/loc/AbstractServiceUpdater.java
@@ -23,6 +23,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.net.URLConnection;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
@@ -42,6 +43,7 @@ import org.apache.traffic_control.traffic_router.core.router.TrafficRouterManage
 
 import static org.apache.commons.codec.digest.DigestUtils.md5Hex;
 
+@SuppressWarnings({"PMD.CyclomaticComplexity"})
 public abstract class AbstractServiceUpdater {
 	private static final Logger LOGGER = LogManager.getLogger(AbstractServiceUpdater.class);
 
@@ -350,13 +352,15 @@ public abstract class AbstractServiceUpdater {
 	protected String tmpPrefix = "loc";
 	protected String tmpSuffix = ".dat";
 
+	@SuppressWarnings({"PMD.CyclomaticComplexity"})
 	protected File downloadDatabase(final String url, final File existingDb) throws IOException {
 		LOGGER.info("[" + getClass().getSimpleName() + "] Downloading database: " + url);
 		final URL dbURL = new URL(url);
-		final HttpURLConnection conn = (HttpURLConnection) dbURL.openConnection();
+		final URLConnection conn = dbURL.openConnection();
 
-		if (useModifiedTimestamp(existingDb)) {
-			conn.setIfModifiedSince(existingDb.lastModified());
+		final long existingLastModified = existingDb.lastModified();
+		if (conn instanceof HttpURLConnection && useModifiedTimestamp(existingDb)) {
+			conn.setIfModifiedSince(existingLastModified);
 			if (eTag != null) {
 				conn.setRequestProperty("If-None-Match", eTag);
 			}
@@ -366,10 +370,14 @@ public abstract class AbstractServiceUpdater {
 		try (InputStream in = conn.getInputStream();
 			 OutputStream out = new FileOutputStream(outputFile)
 		) {
-			eTag = conn.getHeaderField("ETag");
-
-			if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
-				LOGGER.info("[" + getClass().getSimpleName() + "] " + url + " not modified since our existing database's last update time of " + new Date(existingDb.lastModified()));
+			if (conn instanceof HttpURLConnection) {
+				eTag = conn.getHeaderField("ETag");
+				if (((HttpURLConnection) conn).getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
+					LOGGER.info("[" + getClass().getSimpleName() + "] " + url + " not modified since our existing database's last update time of " + new Date(existingLastModified));
+					return existingDb;
+				}
+			} else if (dbURL.getProtocol().equals("file") && conn.getLastModified() > 0 && conn.getLastModified() <= existingLastModified) {
+				LOGGER.info("[" + getClass().getSimpleName() + "] " + url + " not modified since our existing database's last update time of " + new Date(existingLastModified));
 				return existingDb;
 			}