Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api/{{version}/deliveryservices/{id}/health returns no info if the delivery service uses a topology #6827

Merged
merged 4 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed Traffic Ops ignoring the configured database port value, which was prohibiting the use of anything other than port 5432 (the PostgreSQL default)
- [#6580](https://github.com/apache/trafficcontrol/issues/6580) Fixed cache config generation remap.config targets for MID-type servers in a Topology with other caches as parents and HTTPS origins.
- Traffic Router: fixed a null pointer exception that caused snapshots to be rejected if a topology cachegroup did not have any online/reported/admin\_down caches
- [#6271](https://github.com/apache/trafficcontrol/issues/6271) `api/{{version}/deliveryservices/{id}/health` returns no info if the delivery service uses a topology.
- [#6549](https://github.com/apache/trafficcontrol/issues/6549) Fixed internal server error while deleting a delivery service created from a DSR (Traafic Ops).
- [#6538](https://github.com/apache/trafficcontrol/pull/6538) Fixed the incorrect use of secure.port on TrafficRouter and corrected to the httpsPort value from the TR server configuration.
- [#6562](https://github.com/apache/trafficcontrol/pull/6562) Fixed incorrect template in Ansible dataset loader role when fallbackToClosest is defined.
Expand Down
51 changes: 48 additions & 3 deletions traffic_ops/traffic_ops_golang/deliveryservice/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/http"
"strings"

"github.com/apache/trafficcontrol/lib/go-log"
"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/lib/go-util"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
Expand Down Expand Up @@ -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")
srijeet0406 marked this conversation as resolved.
Show resolved Hide resolved
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)
srijeet0406 marked this conversation as resolved.
Show resolved Hide resolved
} else {
for _, n := range top.Nodes {
cacheGroupNameMap[n] = true
}
}
}
}
for cacheName, avail := range crStates.Caches {
cache, ok := crConfig.ContentServers[string(cacheName)]
if !ok {
continue // TODO warn?
}
if _, ok := cache.DeliveryServices[string(ds)]; !ok {
continue
if topology == "" {
if _, ok := cache.DeliveryServices[string(ds)]; !ok {
continue
}
}
if cache.ServerStatus == nil || *cache.ServerStatus != tc.CRConfigServerStatus(tc.CacheStatusReported) {
continue
Expand All @@ -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)
srijeet0406 marked this conversation as resolved.
Show resolved Hide resolved
for _, cap := range cache.Capabilities {
cacheCapabilities[cap] = true
}
for _, rc := range deliveryService.RequiredCapabilities {
if _, ok = cacheCapabilities[rc]; !ok {
skip = true
continue
srijeet0406 marked this conversation as resolved.
Show resolved Hide resolved
}
}
if skip {
continue
}
}
cgHealth := data[tc.CacheGroupName(*cache.CacheGroup)]
cgHealth.Name = tc.CacheGroupName(*cache.CacheGroup)
if avail.IsAvailable {
Expand Down
165 changes: 165 additions & 0 deletions traffic_ops/traffic_ops_golang/deliveryservice/health_test.go
Original file line number Diff line number Diff line change
@@ -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 = `
srijeet0406 marked this conversation as resolved.
Show resolved Hide resolved
{
"config": {},
"contentServers": {
"cache1": {
"cacheGroup": "cg1",
"capabilities": [
"cap1",
"cap2"
],
"status": "REPORTED",
"type": "EDGE",
"deliveryServices": {
"ds1": [
"edge.ds1.test.net"
]
}
},
"cache2": {
"cacheGroup": "cg2",
"capabilities": [
"cap2",
"cap3"
],
"status": "REPORTED",
"type": "EDGE"
},
"cache3": {
"cacheGroup": "cg2",
"capabilities": [
"cap3",
"cap4"
],
"status": "REPORTED",
"type": "EDGE"
}
},
"deliveryServices": {
"ds1": {},
"ds2-topology": {
"topology": "test_topology",
"requiredCapabilities": ["cap2"]
}
},
"contentRouters": {
"tr1": {}
},
"edgeLocations": {
"edge1": {}
},
"trafficRouterLocations": {
"tr-loc": {}
},
"monitors": {
"tm-host": {}
},
"stats": {},
"topologies": {
"test_topology": {
"nodes": [
"cg2"
]
}
}
}
`
const crStatesJson = `
{
"caches": {
"cache1": {
"isAvailable": true,
"ipv4Available": true,
"ipv6Available": true,
"status": "REPORTED - available",
"lastPoll": "2022-05-11T19:50:55.036253631Z"
},
"cache2": {
"isAvailable": true,
"ipv4Available": true,
"ipv6Available": true,
"status": "REPORTED - available",
"lastPoll": "2022-05-11T19:51:06.965095596Z"
},
"cache3": {
"isAvailable": true,
"ipv4Available": true,
"ipv6Available": true,
"status": "REPORTED - available",
"lastPoll": "2022-05-11T19:51:06.965095596Z"
}
},
"deliveryServices": {
"ds1": {
"disabledLocations": [],
"isAvailable": true
},
"ds2-topology": {
"disabledLocations": [],
"isAvailable": true
}
}
}
`

func TestAddHealth(t *testing.T) {
crStates := tc.CRStates{}
crConfig := tc.CRConfig{}
err := json.Unmarshal([]byte(crStatesJson), &crStates)
if err != nil {
t.Fatalf("error unmarshalling crStates: %v", err)
}
err = json.Unmarshal([]byte(crConfigJson), &crConfig)
if err != nil {
t.Fatalf("error unmarshalling crConfig: %v", err)
}
data := make(map[tc.CacheGroupName]tc.HealthDataCacheGroup)
data[tc.CacheGroupName("cache1")] = tc.HealthDataCacheGroup{
Offline: 0,
Online: 0,
Name: "cg1",
}
data[tc.CacheGroupName("cache2")] = tc.HealthDataCacheGroup{
Offline: 0,
Online: 0,
Name: "cg2",
}
data[tc.CacheGroupName("cache3")] = tc.HealthDataCacheGroup{
Offline: 0,
Online: 0,
Name: "cg2",
}
_, available, unAvailable := addHealth("ds1", data, 0, 0, crStates, crConfig)
if available != 1 || unAvailable != 0 {
t.Errorf("expected ds1 to have 1 online and 0 offline caches, but got %d online and %d offline instead", available, unAvailable)
}
// Even though there are 2 REPORTED EDGE caches in cg2, the result should just include 1, because one of them should get filtered out because it's missing a required capability (cap2)
_, available, unAvailable = addHealth("ds2-topology", data, 0, 0, crStates, crConfig)
if available != 1 || unAvailable != 0 {
t.Errorf("expected ds2-topology to have 1 online and 0 offline caches, but got %d online and %d offline instead", available, unAvailable)
}
}