ocket8888 commented on code in PR #6827:
URL: https://github.com/apache/trafficcontrol/pull/6827#discussion_r878470342


##########
traffic_ops/traffic_ops_golang/deliveryservice/health.go:
##########
@@ -132,7 +160,24 @@ func addHealth(ds tc.DeliveryServiceName, data 
map[tc.CacheGroupName]tc.HealthDa
                if cache.CacheGroup == nil {
                        continue // TODO warn?
                }
-
+               if topology != "" {
+                       if _, ok := cacheGroupNameMap[*cache.CacheGroup]; !ok {
+                               continue
+                       }
+                       cacheCapabilities = make(map[string]bool)

Review Comment:
   This shadows a variable of the same name in an outer scope; it should be 
renamed to avoid collisions and confusion.
   
   Also: nit but you know how big this needs to be. It's exactly the size of 
the cache server's `Capabilities` array, since it's not legal for those to 
contain duplicates. So you can construct with that in mind to cut down on 
re-allocations.
   
   Also the value of each map key is unused, so to save an insignificant amount 
of space this could just be a `map[string]struct{}`



##########
traffic_ops/traffic_ops_golang/deliveryservice/health.go:
##########
@@ -132,7 +160,24 @@ func addHealth(ds tc.DeliveryServiceName, data 
map[tc.CacheGroupName]tc.HealthDa
                if cache.CacheGroup == nil {
                        continue // TODO warn?
                }
-
+               if topology != "" {
+                       if _, ok := cacheGroupNameMap[*cache.CacheGroup]; !ok {
+                               continue
+                       }
+                       cacheCapabilities = make(map[string]bool)
+                       for _, cap := range cache.Capabilities {
+                               cacheCapabilities[cap] = true
+                       }
+                       for _, rc := range deliveryService.RequiredCapabilities 
{
+                               if _, ok = cacheCapabilities[rc]; !ok {
+                                       skip = true
+                                       continue

Review Comment:
   at this point you know that it shouldn't be counted. There's no reason to 
continue to check the rest of the Capabilities, just `break` here instead.



##########
traffic_ops/traffic_ops_golang/deliveryservice/health.go:
##########
@@ -115,13 +116,40 @@ func getMonitorHealth(tx *sql.Tx, ds 
tc.DeliveryServiceName, monitorFQDNs []stri
 
 // addHealth adds the given cache states to the given data and totals, and 
returns the new data and totals
 func addHealth(ds tc.DeliveryServiceName, data 
map[tc.CacheGroupName]tc.HealthDataCacheGroup, totalOnline uint64, totalOffline 
uint64, crStates tc.CRStates, crConfig tc.CRConfig) 
(map[tc.CacheGroupName]tc.HealthDataCacheGroup, uint64, uint64) {
+
+       var deliveryService tc.CRConfigDeliveryService
+       var ok bool
+       var topology string
+       var cacheGroupNameMap = make(map[string]bool)
+       var cacheCapabilities = make(map[string]bool)
+       var skip bool
+
+       if deliveryService, ok = crConfig.DeliveryServices[string(ds)]; !ok {
+               log.Errorln("delivery service not found in CRConfig")

Review Comment:
   Should this return an error instead of logging it and returning data as 
though the DS *were* found?



##########
traffic_ops/traffic_ops_golang/deliveryservice/health_test.go:
##########
@@ -0,0 +1,165 @@
+package deliveryservice
+
+/*
+
+   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.
+*/
+
+import (
+       "github.com/apache/trafficcontrol/lib/go-tc"
+
+       "encoding/json"
+       "testing"
+)
+
+const crConfigJson = `

Review Comment:
   Instead of making two JSON string constants and then doing nothing with them 
but unmarshalling into a real struct, why not just create that struct 
immediately, so you get compile-time type safety?



##########
traffic_ops/traffic_ops_golang/deliveryservice/health.go:
##########
@@ -115,13 +116,40 @@ func getMonitorHealth(tx *sql.Tx, ds 
tc.DeliveryServiceName, monitorFQDNs []stri
 
 // addHealth adds the given cache states to the given data and totals, and 
returns the new data and totals
 func addHealth(ds tc.DeliveryServiceName, data 
map[tc.CacheGroupName]tc.HealthDataCacheGroup, totalOnline uint64, totalOffline 
uint64, crStates tc.CRStates, crConfig tc.CRConfig) 
(map[tc.CacheGroupName]tc.HealthDataCacheGroup, uint64, uint64) {
+
+       var deliveryService tc.CRConfigDeliveryService
+       var ok bool
+       var topology string
+       var cacheGroupNameMap = make(map[string]bool)
+       var cacheCapabilities = make(map[string]bool)
+       var skip bool
+
+       if deliveryService, ok = crConfig.DeliveryServices[string(ds)]; !ok {
+               log.Errorln("delivery service not found in CRConfig")
+               return map[tc.CacheGroupName]tc.HealthDataCacheGroup{}, 0, 0
+       }
+
+       if deliveryService.Topology != nil {
+               topology = *deliveryService.Topology
+               if topology != "" {
+                       if top, ok := crConfig.Topologies[topology]; !ok {
+                               log.Errorf("CRConfig topologies does not 
contain DS topology: %s", topology)

Review Comment:
   Should this return an error instead of logging this and returning data as 
though the DS's topology *were* found?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to