rawlinp commented on a change in pull request #6527:
URL: https://github.com/apache/trafficcontrol/pull/6527#discussion_r804124720
##########
File path: traffic_monitor/manager/monitorconfig.go
##########
@@ -337,6 +379,103 @@ func monitorConfigListen(
}
}
+// getCacheGroupsToPoll returns the name of this Traffic Monitor's cache group
+// and the set of cache groups it needs to poll.
+func getCacheGroupsToPoll(distributedPolling bool, hostname string, monitors
map[string]tc.TrafficMonitor,
+ caches map[string]tc.TrafficServer, allCacheGroups
map[string]tc.TMCacheGroup) (string, map[string]tc.TMCacheGroup, error) {
+ tmGroupSet := make(map[string]tc.TMCacheGroup)
+ cacheGroupSet := make(map[string]tc.TMCacheGroup)
+ tmGroupToPolledCacheGroups :=
make(map[string]map[string]tc.TMCacheGroup)
+ thisTMGroup := ""
+
+ for _, tm := range monitors {
+ if tm.HostName == hostname {
+ thisTMGroup = tm.Location
+ }
+ if tc.CacheStatusFromString(tm.ServerStatus) ==
tc.CacheStatusOnline {
+ tmGroupSet[tm.Location] = allCacheGroups[tm.Location]
+ if _, ok := tmGroupToPolledCacheGroups[tm.Location];
!ok {
+ tmGroupToPolledCacheGroups[tm.Location] =
make(map[string]tc.TMCacheGroup)
+ }
+ }
+ }
+ if thisTMGroup == "" {
+ return "", nil, fmt.Errorf("unable to find cache group for this
Traffic Monitor (%s) in monitoring config snapshot", hostname)
+ }
+
+ for _, c := range caches {
+ status := tc.CacheStatusFromString(c.ServerStatus)
+ if status == tc.CacheStatusOnline || status ==
tc.CacheStatusReported || status == tc.CacheStatusAdminDown {
+ cacheGroupSet[c.CacheGroup] =
allCacheGroups[c.CacheGroup]
+ }
+ }
+
+ if !distributedPolling {
+ return thisTMGroup, cacheGroupSet, nil
+ }
+
+ tmGroups := make([]string, 0, len(tmGroupSet))
+ for tg := range tmGroupSet {
+ tmGroups = append(tmGroups, tg)
+ }
+ sort.Strings(tmGroups)
+ cgs := make([]string, 0, len(cacheGroupSet))
+ for cg := range cacheGroupSet {
+ cgs = append(cgs, cg)
+ }
+ sort.Strings(cgs)
+ tmGroupCount := len(tmGroups)
+ var closest string
+ for tmi := 0; len(cgs) > 0; tmi = (tmi + 1) % tmGroupCount {
+ tmGroup := tmGroups[tmi]
+ closest, cgs = findAndRemoveClosestCachegroup(cgs,
allCacheGroups[tmGroup], allCacheGroups)
+ tmGroupToPolledCacheGroups[tmGroup][closest] =
allCacheGroups[closest]
+ }
+ return thisTMGroup, tmGroupToPolledCacheGroups[thisTMGroup], nil
+}
+
+func findAndRemoveClosestCachegroup(remainingCacheGroups []string, target
tc.TMCacheGroup, allCacheGroups map[string]tc.TMCacheGroup) (string, []string) {
+ shortestDistance := math.MaxFloat64
+ shortestIndex := -1
+ for i := 0; i < len(remainingCacheGroups); i++ {
+ distance := getDistance(target,
allCacheGroups[remainingCacheGroups[i]])
+ if distance < shortestDistance {
+ shortestDistance = distance
+ shortestIndex = i
+ }
+ }
+ closest := remainingCacheGroups[shortestIndex]
+ remainingCacheGroups = append(remainingCacheGroups[:shortestIndex],
remainingCacheGroups[shortestIndex+1:]...)
+ return closest, remainingCacheGroups
+}
+
+const meanEarthRadius = 6371.0
+const x = math.Pi / 180
+
+// toRadians converts degrees to radians.
+func toRadians(d float64) float64 {
+ return d * x
+}
+
+// getDistance gets the great circle distance in kilometers between x and y.
+func getDistance(x, y tc.TMCacheGroup) float64 {
Review comment:
Maybe? I basically just transliterated this from TR, which I assume is
correct because we've been using it for years.
--
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]