rawlinp commented on a change in pull request #2785: In Traffic Router Support 
Snapshots which only update Delivery Services
URL: https://github.com/apache/trafficcontrol/pull/2785#discussion_r239955341
 
 

 ##########
 File path: 
traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/config/SnapshotEventsProcessor.java
 ##########
 @@ -0,0 +1,376 @@
+/*
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.comcast.cdn.traffic_control.traffic_router.core.config;
+
+import com.comcast.cdn.traffic_control.traffic_router.core.cache.Cache;
+import 
com.comcast.cdn.traffic_control.traffic_router.core.cache.Cache.DeliveryServiceReference;
+import com.comcast.cdn.traffic_control.traffic_router.core.ds.DeliveryService;
+import com.comcast.cdn.traffic_control.traffic_router.core.util.JsonUtils;
+import 
com.comcast.cdn.traffic_control.traffic_router.core.util.JsonUtilsException;
+import com.fasterxml.jackson.databind.JsonNode;
+import org.apache.log4j.Logger;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+//import java.time.Instant;
+
+public class SnapshotEventsProcessor {
+       private static final Logger LOGGER = 
Logger.getLogger(SnapshotEventsProcessor.class);
+       final private Map<String, DeliveryService> creationEvents = new 
HashMap<>();
+       final private Map<String, DeliveryService> updateEvents = new 
HashMap<>();
+       final private Map<String, DeliveryService> deleteEvents = new 
HashMap<>();
+       final private Map<String, Cache> mappingEvents = new HashMap<>();
+       final private List<String> serverModEvents = new ArrayList<>();
+
+       public List<String> getDeleteCacheEvents() {
+               return deleteCacheEvents;
+       }
+
+       final private List<String> deleteCacheEvents = new ArrayList<>();
+       private JsonNode existingConfig = null;
+       private boolean loadall = false;
+
+       public static SnapshotEventsProcessor diffCrConfigs(final JsonNode 
newSnapDb,
+                                                           final JsonNode 
existingDb) throws
+                       JsonUtilsException, ParseException {
+               LOGGER.info("In diffCrConfigs");
+               final SnapshotEventsProcessor sepRet = new 
SnapshotEventsProcessor();
+
+               if (newSnapDb == null)
+               {
+                       final String errstr = "Required parameter 'newSnapDb' 
was null";
+                       LOGGER.error(errstr);
+                       JsonUtils.throwException(errstr);
+               }
+               // Load the entire crConfig from the snapshot if there isn't 
one saved on the filesystem
+               if (existingDb == null || existingDb.size()< 1) {
+                       sepRet.parseDeliveryServices(newSnapDb);
+                       sepRet.loadall = true;
+                       return sepRet;
+               }
+               // Load the entire crConfig from the snapshot if it is not a 
version supporting
+               // DS Snapshots
+               if (!sepRet.versionSupportsDsSnapshots(newSnapDb)){
+                       LOGGER.info("In diffCrConfig 'DS Snapshot' feature 
turned off.");
+                       sepRet.parseDeliveryServices(newSnapDb);
+                       sepRet.loadall = true;
+                       return sepRet;
+               }
+               // Verify that only DS related configurations have changed
+               if (sepRet.hasDiffsForcingReload(newSnapDb, existingDb)){
+                       LOGGER.info("hasDiffsForcingReload true");
+                       sepRet.parseDeliveryServices(newSnapDb);
+                       sepRet.loadall = true;
+                       return sepRet;
+               }
+               // process only the changes to Delivery Services if none of the 
above conditions are met
+               sepRet.diffDeliveryServices(newSnapDb, existingDb);
+               sepRet.diffCacheMappings(newSnapDb, existingDb);
+               return sepRet;
+       }
+
+       private boolean versionSupportsDsSnapshots(final JsonNode snapDb) 
throws JsonUtilsException {
+               if (snapDb == null) {
+                       return false;
+               }
+               return snapDb.has(ConfigHandler.versionKey);
+       }
+
+       private boolean hasDiffsForcingReload(final JsonNode newSnapDb, final 
JsonNode existingConfig) throws JsonUtilsException {
+               if (!JsonUtils.equalSubtrees(newSnapDb, 
existingConfig,ConfigHandler.configKey)) {
+                       LOGGER.info("Config diff found");
+                       return true;
+               }
+               if (!JsonUtils.equalSubtrees(newSnapDb, 
existingConfig,"contentRouters")) {
+                       LOGGER.info("ContentRouters diff found");
+                       return true;
+               }
+               if (!JsonUtils.equalSubtrees(newSnapDb, 
existingConfig,"monitors")) {
+                       LOGGER.info("Monitors diff found");
+                       return true;
+               }
+               if (!JsonUtils.equalSubtreesExcept(newSnapDb, 
existingConfig,"stats","date")) {
+                       LOGGER.info("Stats diff found");
+                       return true;
+               }
+               if (!JsonUtils.equalSubtrees(newSnapDb, 
existingConfig,"edgeLocations")) {
+                       LOGGER.info("EdgeLocation diff found");
+                       return true;
+               }
+               return !compareContentServers(newSnapDb,existingConfig);
+       }
+
+       private boolean compareContentServers(final JsonNode newSnapDb, final 
JsonNode existingConfig) throws JsonUtilsException {
 
 Review comment:
   do you mind renaming this `contentServersAreEqual`? That makes it clear what 
the return value actually means.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to