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_r237246069
 
 

 ##########
 File path: 
traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandler.java
 ##########
 @@ -320,16 +408,100 @@ private void parseTrafficOpsConfig(final JsonNode 
config, final JsonNode stats)
        /**
         * Parses the cache information from the configuration and updates the 
{@link CacheRegister}.
         *
-        * @param trConfig
-        *            the {@link TrafficRouterConfiguration}
+        * @param snap the {@link SnapshotEventsProcessor}
+        * @param cacheRegister the {@link CacheRegister}
+        */
+       @SuppressWarnings({"PMD.AvoidDeeplyNestedIfStmts" })
+       private void parseCacheConfig(final SnapshotEventsProcessor snap,
+                                     final CacheRegister cacheRegister) throws
+                       ParseException {
+               final Map<String, Cache> existingCacheMap = 
cacheRegister.getCacheMap();
+               // first Remove caches that have been deleted
+               existingCacheMap.entrySet().removeIf(exEntry-> {
+                       if 
(snap.getDeleteCacheEvents().contains(exEntry.getKey())){
+                               LOGGER.info("Removed cache from map: 
"+exEntry.getKey());
+                               return true;
+                       }
+                       return false;
+               });
+               // remove deleted caches from locations
+               // this works because 'loc.getCaches()' makes a copy
+               final Set<CacheLocation> exLocations = 
cacheRegister.getCacheLocations();
+               exLocations.forEach(loc->{
+                       loc.getCaches().forEach(cacheEntry-> {
+                               if 
(snap.getDeleteCacheEvents().contains(cacheEntry.getId())) {
+                                       LOGGER.info("Removed cache from 
location: "+cacheEntry.getId());
+                                       loc.removeCache(cacheEntry.getId());
+                               }
+                       });
+               });
+               cacheRegister.setConfiguredLocations(exLocations);
+               // scan remaining caches for ds link changes
+               final Iterator<String> cacheIter = 
existingCacheMap.keySet().iterator();
+               while (cacheIter.hasNext()) {
+                       final String cacheName = cacheIter.next();
+                       // If its in the modified list then replace the 
mappings in existing
+                       if 
(snap.getMappingEvents().keySet().contains(cacheName)) {
+                               LOGGER.info("Found mapping change for cache: 
"+cacheName);
+                               final Cache cache = 
existingCacheMap.get(cacheName);
+                               final Cache newCache = 
snap.getMappingEvents().get(cacheName);
+                               final Collection<DeliveryServiceReference> 
existingDsRefs = cache.getDeliveryServices();
+                               for (final DeliveryServiceReference dsr : 
existingDsRefs ) {
+                                       final String dsId = 
dsr.getDeliveryServiceId();
+                                       DeliveryService theDs = 
cacheRegister.getDeliveryService(dsId);
+                                       if (theDs == null) {
+                                               // This means the delivery 
service has been deleted, so we can only find it in the deleted list
+                                               theDs = 
snap.getDeleteEvents().get(dsId);
+                                               if (theDs == null) {
+                                                       // this means there is 
a bug somewhere
+                                                       // allow 
NullPointerException
+                                                       
LOGGER.error("parseCacheConfig: This DeliveryService has been deleted from the 
Cache " +
+                                                                       
"Register, but it is not in the list of Delete events: "+dsId);
+                                                       throw new 
ParseException("parseCacheConfig: This DeliveryService has been deleted from 
the" +
+                                                                       " Cache 
" +
+                                                                       
"Register, but it is not in the list of Delete events: "+dsId);
+                                               }
+                                               statTracker.removeTracks(theDs, 
dsr.getAliases());
+                                       }
+                                       else
+                                       {
+                                               final DeliveryServiceReference 
ndsr = newCache.getDeliveryService(dsId);
+                                               addNewStats(ndsr,theDs);
+                                       }
+                               }
+                               
cache.replaceDeliveryServices(newCache.getDeliveryServices());
+                               cache.getDeliveryServices().forEach(dsr-> 
LOGGER.info("cache = "+cache.getId()+" DSR = "+dsr.getDeliveryServiceId()));
+                       }
+                       else {
 
 Review comment:
   Do you mind inverting this conditional so that the smaller block is first 
and does a `continue` to cut down on some of this indentation?

----------------------------------------------------------------
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