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_r239950394
 
 

 ##########
 File path: 
traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/config/ConfigHandler.java
 ##########
 @@ -423,61 +590,124 @@ private void parseCacheConfig(final JsonNode 
contentServers, final CacheRegister
                statTracker.initialize(statMap, cacheRegister);
        }
 
-       private Map<String, DeliveryService> parseDeliveryServiceConfig(final 
JsonNode allDeliveryServices) throws JsonUtilsException {
-               final Map<String,DeliveryService> deliveryServiceMap = new 
HashMap<>();
-
-               final Iterator<String> deliveryServiceIter = 
allDeliveryServices.fieldNames();
-               while (deliveryServiceIter.hasNext()) {
-                       final String deliveryServiceId = 
deliveryServiceIter.next();
-                       final JsonNode deliveryServiceJson = 
JsonUtils.getJsonNode(allDeliveryServices, deliveryServiceId);
-                       final DeliveryService deliveryService = new 
DeliveryService(deliveryServiceId, deliveryServiceJson);
-                       boolean isDns = false;
-
-                       final JsonNode matchsets = 
JsonUtils.getJsonNode(deliveryServiceJson, "matchsets");
-
-                       for (final JsonNode matchset : matchsets) {
-                               final String protocol = 
JsonUtils.getString(matchset, "protocol");
-                               if ("DNS".equals(protocol)) {
-                                       isDns = true;
+       @SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity", 
"PMD.AvoidCatchingThrowable"})
+       private void parseDeliveryServiceMatchSets(final 
SnapshotEventsProcessor snap,
+                                                  final CacheRegister 
cacheRegister) throws
+                       JsonUtilsException {
+               final List<JsonUtilsException> jues = new ArrayList<>();
+               if (cacheRegister.getDnsDeliveryServiceMatchers() == null) {
+                       cacheRegister.setDnsDeliveryServiceMatchers(new 
TreeSet<>());
+               }
+               if (cacheRegister.getHttpDeliveryServiceMatchers() == null) {
+                       cacheRegister.setHttpDeliveryServiceMatchers(new 
TreeSet<>());
+               }
+               if (cacheRegister.getDeliveryServices() == null) {
+                       cacheRegister.setDeliveryServiceMap(new HashMap<>());
+               }
+               final TreeSet<DeliveryServiceMatcher> dnsServiceMatchers = 
cacheRegister.getDnsDeliveryServiceMatchers();
+               final TreeSet<DeliveryServiceMatcher> httpServiceMatchers = 
cacheRegister.getHttpDeliveryServiceMatchers();
+               final Map<String, DeliveryService> masterDsMap = 
cacheRegister.getDeliveryServices();
+               final Map<String, DeliveryService> deliveryServiceMap = 
snap.getChangeEvents();
+               deliveryServiceMap.forEach((deliveryServiceId, deliveryService) 
-> {
+                       try {
+                               final JsonNode matchsets = 
deliveryService.getMatchsets();
+                               for (final JsonNode matchset : matchsets) {
+                                       final String protocol = 
JsonUtils.getString(matchset, "protocol");
+                                       final DeliveryServiceMatcher 
deliveryServiceMatcher = new DeliveryServiceMatcher(deliveryService);
+                                       if ("HTTP".equals(protocol)) {
+                                               
httpServiceMatchers.removeIf(dsm -> 
dsm.getDeliveryService().getId().equals(deliveryServiceId));
+                                               
httpServiceMatchers.add(deliveryServiceMatcher);
+                                       } else if ("DNS".equals(protocol)) {
+                                               dnsServiceMatchers.removeIf(dsm 
-> dsm.getDeliveryService().getId().equals(deliveryServiceId));
+                                               
dnsServiceMatchers.add(deliveryServiceMatcher);
+                                       }
+                                       for (final JsonNode matcherJo : 
JsonUtils.getJsonNode(matchset, "matchlist")) {
+                                               final Type type = 
Type.valueOf(JsonUtils.getString(matcherJo, "match-type"));
+                                               final String target = 
JsonUtils.optString(matcherJo, "target");
+                                               
deliveryServiceMatcher.addMatch(type, JsonUtils.getString(matcherJo, "regex"), 
target);
+                                       }
+                               }
+                               final String rurl = 
deliveryService.getGeoRedirectUrl();
+                               if (rurl != null) {
+                                       final int idx = rurl.indexOf("://");
+                                       if (idx < 0) {
+                                               //this is a relative url, 
belongs to this ds
+                                               
deliveryService.setGeoRedirectUrlType(DS_URL);
+                                       } else {
+                                               //this is a url with protocol, 
must check further
+                                               //first, parse the url, if url 
invalid it will throw Exception
+                                               final URL url = new URL(rurl);
+                                               //make a fake HTTPRequest for 
the redirect url
+                                               final HTTPRequest req = new 
HTTPRequest(url);
+                                               
deliveryService.setGeoRedirectFile(url.getFile());
+                                               //try select the ds by the 
redirect fake HTTPRequest
+                                               final DeliveryService rds = 
cacheRegister.getDeliveryService(req, true);
+                                               if (rds == null || 
rds.getId().equals(deliveryService.getId())) {
+                                                       //the redirect url not 
belongs to this ds
+                                                       
deliveryService.setGeoRedirectUrlType(NOT_DS_URL);
+                                               } else {
+                                                       
deliveryService.setGeoRedirectUrlType(DS_URL);
+                                               }
+                                       }
                                }
+                               masterDsMap.put(deliveryServiceId, 
deliveryService);
+                       } catch (JsonUtilsException e) {
+                               jues.add(e);
+                       } catch (MalformedURLException mue) {
+                               LOGGER.error("fatal error, failed to init NGB 
redirect with Exception: " + mue.getMessage());
                        }
-
-                       deliveryService.setDns(isDns);
-                       deliveryServiceMap.put(deliveryServiceId, 
deliveryService);
+               });
+               if (!jues.isEmpty()) {
+                       throw jues.get(0);
                }
-
-               return deliveryServiceMap;
+               // remove delivery services that have been deleted in the 
snapshot
+               if (!snap.getDeleteEvents().isEmpty()) {
 
 Review comment:
   this `if` can be removed, right? `forEach` handles that case I believe.

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