mattjackson220 commented on a change in pull request #4916:
URL: https://github.com/apache/trafficcontrol/pull/4916#discussion_r469506285



##########
File path: traffic_monitor/health/cache.go
##########
@@ -144,177 +191,153 @@ func EvalCache(result cache.ResultInfo, resultStats 
*threadsafe.ResultStatValHis
                }
 
                if !inThreshold(threshold, resultStatNum) {
-                       return false, result.UsingIPv4, eventDesc(status, 
exceedsThresholdMsg(stat, threshold, resultStatNum)), stat
+                       return false, eventDesc(status, 
exceedsThresholdMsg(stat, threshold, resultStatNum)), stat
                }
        }
 
-       return avail, result.UsingIPv4, eventDescVal, eventMsg
+       return avail, eventDescVal, eventMsg
 }
 
-// CalcAvailabilityWithStats calculates the availability of each cache in 
results.
-// statResultHistory may be nil, in which case stats won't be used to 
calculate availability.
-func CalcAvailability(results []cache.Result, pollerName string, 
statResultHistory *threadsafe.ResultStatHistory, mc 
tc.LegacyTrafficMonitorConfigMap, toData todata.TOData, 
localCacheStatusThreadsafe threadsafe.CacheAvailableStatus, localStates 
peer.CRStatesThreadsafe, events ThreadsafeEvents, protocol 
config.PollingProtocol) {
-       localCacheStatuses := localCacheStatusThreadsafe.Get().Copy()
-       statResults := (*threadsafe.ResultStatValHistory)(nil)
-       statResultsVal := (*map[string]threadsafe.ResultStatValHistory)(nil)
-       processAvailableTuple := func(tuple cache.AvailableTuple, serverInfo 
tc.LegacyTrafficServer) bool {
-               switch protocol {
-               case config.IPv4Only:
+// getProcessAvailableTuple gets a function to process an availability tuple
+// based on the protocol used.
+func getProcessAvailableTuple(protocol config.PollingProtocol) 
func(cache.AvailableTuple, tc.TrafficServer) bool {
+       switch protocol {
+       case config.IPv4Only:
+               return func(tuple cache.AvailableTuple, _ tc.TrafficServer) 
bool {
                        return tuple.IPv4
-               case config.IPv6Only:
+               }
+       case config.IPv6Only:
+               return func(tuple cache.AvailableTuple, _ tc.TrafficServer) 
bool {
                        return tuple.IPv6
-               case config.Both:
-                       // only report availability based on defined IP 
addresses
-                       if serverInfo.IP == "" {
+               }
+       case config.Both:
+               return func(tuple cache.AvailableTuple, serverInfo 
tc.TrafficServer) bool {
+                       if serverInfo.IPv4() == "" {
                                return tuple.IPv6
-                       } else if serverInfo.IP6 == "" {
+                       } else if serverInfo.IPv6() == "" {
                                return tuple.IPv4
                        }
-                       // if both IP addresses are defined then report 
availability based on both
                        return tuple.IPv4 || tuple.IPv6
-               default:
-                       log.Errorln("received an unknown PollingProtocol: " + 
protocol.String())
                }
-               return false
+       default:
+               log.Errorf("received an unknown Polling Protocol: %s", protocol)
        }
+       return func(cache.AvailableTuple, tc.TrafficServer) bool { return false 
}
+}
+
+// CalcAvailability calculates the availability of each cache in results.
+// statResultHistory may be nil, in which case stats won't be used to calculate
+// availability.
+func CalcAvailability(
+       results []cache.Result,
+       pollerName string,
+       statResultHistory *threadsafe.ResultStatHistory,
+       mc tc.TrafficMonitorConfigMap,
+       toData todata.TOData,
+       localCacheStatusThreadsafe threadsafe.CacheAvailableStatus,
+       localStates peer.CRStatesThreadsafe,
+       events ThreadsafeEvents,
+       protocol config.PollingProtocol,
+) {
+       localCacheStatuses := localCacheStatusThreadsafe.Get().Copy()
+       var statResultsVal *threadsafe.CacheStatHistory
+       processAvailableTuple := getProcessAvailableTuple(protocol)
 
        for _, result := range results {
                if statResultHistory != nil {
-                       t := 
statResultHistory.LoadOrStore(tc.CacheName(result.ID))
+                       t := statResultHistory.LoadOrStore(result.ID)
                        statResultsVal = &t
                }
                serverInfo, ok := mc.TrafficServer[result.ID]
                if !ok {
                        log.Errorf("Cache %v missing from from Traffic Ops 
Monitor Config - treating as OFFLINE\n", result.ID)
                }
 
-               if _, ok := localCacheStatuses[tc.CacheName(result.ID)]; !ok {
-                       localCacheStatuses[tc.CacheName(result.ID)] = 
make(map[string]cache.AvailableStatus)
+               availStatus := cache.AvailableStatus{
+                       LastCheckedIPv4:    result.UsingIPv4,
+                       ProcessedAvailable: true,
+                       Poller:             pollerName,
+                       Status:             serverInfo.ServerStatus,
                }
 
+               lastStatus, ok := localCacheStatuses[result.ID]
+               if ok {
+                       availStatus.Available = lastStatus.Available
+               }
+
+               reasons := []string{}
                resultInfo := cache.ToInfo(result)
-               for interfaceName, _ := range result.Interfaces() {
-                       if statResultsVal != nil {
-                               t := (*statResultsVal)[interfaceName]
-                               statResults = &t
+               for _, inf := range serverInfo.Interfaces {
+                       if !inf.Monitor {
+                               continue
                        }
-                       isAvailable, usingIPv4, whyAvailable, unavailableStat 
:= EvalCache(resultInfo, statResults, &mc)
-
-                       // if the cache is now Available, and was previously 
unavailable due to a threshold, make sure this poller contains the stat which 
exceeded the threshold.
-                       previousStatus, hasPreviousStatus := 
localCacheStatuses[tc.CacheName(result.ID)][interfaceName]
-                       availableTuple := cache.AvailableTuple{}
-
-                       if hasPreviousStatus {
-                               availableTuple = previousStatus.Available
-                               availableTuple.SetAvailability(usingIPv4, 
isAvailable)
-
-                               if processAvailableTuple(availableTuple, 
serverInfo) {
-                                       if 
!processAvailableTuple(previousStatus.Available, serverInfo) && 
previousStatus.UnavailableStat != "" {
-                                               if 
!result.HasStat(previousStatus.UnavailableStat) {
-                                                       return
-                                               }
-                                       }
-                               }
+
+                       available, why := 
EvalInterface(resultInfo.InterfaceVitals, inf)
+                       if result.UsingIPv4 {
+                               availStatus.Available.IPv4 = 
availStatus.Available.IPv4 && available

Review comment:
       yea the original plan was for each cycle to only update the status of 
what its checking. so if we are checking IPv6 then the Available.IPv4 should 
remain as it was and vice versa. it might be as easy as just removing the 
`availStatus.Available.IPvX &&` from these lines but havent had a chance to 
test that yet




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

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


Reply via email to