You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by oc...@apache.org on 2021/01/15 18:48:52 UTC

[trafficcontrol] branch 5.0.x updated: Only additionally get MID servers if Delivery Service is not topology-based (#5441)

This is an automated email from the ASF dual-hosted git repository.

ocket8888 pushed a commit to branch 5.0.x
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/5.0.x by this push:
     new 19c0487  Only additionally get MID servers if Delivery Service is not topology-based (#5441)
19c0487 is described below

commit 19c0487f8de71f36219aa0a5d9535ad4d2080229
Author: Zach Hoffman <zr...@apache.org>
AuthorDate: Fri Jan 15 10:42:34 2021 -0700

    Only additionally get MID servers if Delivery Service is not topology-based (#5441)
    
    * Only additionally get MID servers if Delivery Service is not
    topology-based
    
    * Move atlanta-mid-01 to a MID_LOC Cache Group
    
    (cherry picked from commit b4abdb43366f923025b73d8036a9874ccde9c4b2)
---
 traffic_ops/testing/api/v3/servers_test.go         | 33 ++++++++-
 traffic_ops/testing/api/v3/tc-fixtures.json        | 79 +++++++++++++++++++++-
 traffic_ops/traffic_ops_golang/server/servers.go   | 34 +++++-----
 .../traffic_ops_golang/server/servers_test.go      |  2 +-
 4 files changed, 127 insertions(+), 21 deletions(-)

diff --git a/traffic_ops/testing/api/v3/servers_test.go b/traffic_ops/testing/api/v3/servers_test.go
index 1d8abe3..adde2e2 100644
--- a/traffic_ops/testing/api/v3/servers_test.go
+++ b/traffic_ops/testing/api/v3/servers_test.go
@@ -511,7 +511,7 @@ func GetTestServersQueryParameters(t *testing.T) {
 	if err != nil {
 		t.Fatalf("Failed to get server by Delivery Service ID: %v", err)
 	}
-	if len(servers.Response) != 2 {
+	if len(servers.Response) != 3 {
 		t.Fatalf("expected to get 2 servers for Delivery Service: %d, actual: %d", *ds.ID, len(servers.Response))
 	}
 
@@ -579,6 +579,7 @@ func GetTestServersQueryParameters(t *testing.T) {
 	expectedHostnames := map[string]bool{
 		"edge1-cdn1-cg3":                 false,
 		"edge2-cdn1-cg3":                 false,
+		"atlanta-mid-01":                 false,
 		"atlanta-mid-16":                 false,
 		"edgeInCachegroup3":              false,
 		"midInSecondaryCachegroupInCDN1": false,
@@ -627,6 +628,35 @@ func GetTestServersQueryParameters(t *testing.T) {
 	if !containsOrigin {
 		t.Fatalf("did not find origin server %s when querying servers by dsId after assigning %s to delivery service %s", originHostname, originHostname, topDsXmlId)
 	}
+
+	const topDsWithNoMids = "ds-based-top-with-no-mids"
+	dses, _, err = TOSession.GetDeliveryServicesV30WithHdr(nil, url.Values{"xmlId": []string{topDsWithNoMids}})
+	if err != nil {
+		t.Fatalf("Failed to get Delivery Services: %v", err)
+	}
+	if len(dses) < 1 {
+		t.Fatal("Failed to get at least one Delivery Service")
+	}
+
+	ds = dses[0]
+	if ds.ID == nil {
+		t.Fatal("Got Delivery Service with nil ID")
+	}
+	params.Set("dsId", strconv.Itoa(*ds.ID))
+
+	response, _, err = TOSession.GetServersWithHdr(&params, nil)
+	if err != nil {
+		t.Fatalf("Failed to get servers by Topology-based Delivery Service ID with xmlId %s: %s", topDsWithNoMids, err)
+	}
+	if len(response.Response) == 0 {
+		t.Fatalf("Did not find any servers for Topology-based Delivery Service with xmlId %s: %s", topDsWithNoMids, err)
+	}
+	for _, server := range response.Response {
+		if tc.CacheTypeFromString(server.Type) == tc.CacheTypeMid {
+			t.Fatalf("Expected to find no %s-typed servers when querying servers by the ID for Delivery Service with XMLID %s but found %s-typed server %s", tc.CacheTypeMid, topDsWithNoMids, tc.CacheTypeMid, *server.HostName)
+		}
+	}
+
 	params.Del("dsId")
 	params.Add("topology", topology)
 	expectedHostnames = map[string]bool{
@@ -634,6 +664,7 @@ func GetTestServersQueryParameters(t *testing.T) {
 		"denver-mso-org-02":              false,
 		"edge1-cdn1-cg3":                 false,
 		"edge2-cdn1-cg3":                 false,
+		"atlanta-mid-01":                 false,
 		"atlanta-mid-16":                 false,
 		"atlanta-mid-17":                 false,
 		"edgeInCachegroup3":              false,
diff --git a/traffic_ops/testing/api/v3/tc-fixtures.json b/traffic_ops/testing/api/v3/tc-fixtures.json
index 2bbcd85..40ad1b0 100644
--- a/traffic_ops/testing/api/v3/tc-fixtures.json
+++ b/traffic_ops/testing/api/v3/tc-fixtures.json
@@ -1205,6 +1205,71 @@
         },
         {
             "active": true,
+            "cdnName": "cdn1",
+            "cacheurl": "",
+            "ccrDnsTtl": 3600,
+            "checkPath": "",
+            "consistentHashQueryParams": [],
+            "deepCachingType": "NEVER",
+            "displayName": "top-ds-in-cdn1",
+            "dnsBypassCname": null,
+            "dnsBypassIp": "",
+            "dnsBypassIp6": "",
+            "dnsBypassTtl": 30,
+            "dscp": 40,
+            "edgeHeaderRewrite": null,
+            "fqPacingRate": 0,
+            "geoLimit": 0,
+            "geoLimitCountries": "",
+            "geoLimitRedirectURL": null,
+            "geoProvider": 0,
+            "globalMaxMbps": 0,
+            "globalMaxTps": 0,
+            "httpBypassFqdn": "",
+            "infoUrl": "TBD",
+            "initialDispersion": 1,
+            "ipv6RoutingEnabled": true,
+            "lastUpdated": "2018-04-06 16:48:51+00",
+            "logsEnabled": false,
+            "longDesc": "",
+            "longDesc1": "",
+            "longDesc2": "",
+            "matchList": [
+                {
+                    "pattern": ".*\\.ds-based-top-with-no-mids\\..*",
+                    "setNumber": 0,
+                    "type": "HOST_REGEXP"
+                }
+            ],
+            "maxDnsAnswers": 0,
+            "midHeaderRewrite": null,
+            "missLat": 41.881944,
+            "missLong": -87.627778,
+            "multiSiteOrigin": false,
+            "orgServerFqdn": "http://example.org",
+            "originShield": null,
+            "profileDescription": null,
+            "profileName": null,
+            "protocol": 0,
+            "qstringIgnore": 0,
+            "rangeRequestHandling": 0,
+            "regexRemap": null,
+            "regionalGeoBlocking": false,
+            "remapText": null,
+            "routingName": "cdn",
+            "signed": false,
+            "signingAlgorithm": null,
+            "sslKeyVersion": 0,
+            "tenant": "tenant1",
+            "tenantName": "tenant1",
+            "topology": "top-with-no-mids",
+            "type": "HTTP",
+            "xmlId": "ds-based-top-with-no-mids",
+            "anonymousBlockingEnabled": false,
+            "maxRequestHeaderBytes": 131072
+        },
+        {
+            "active": true,
             "cdnName": "cdn2",
             "cacheurl": "",
             "ccrDnsTtl": 3600,
@@ -2741,7 +2806,7 @@
             "xmppPasswd": "X"
         },
         {
-            "cachegroup": "cachegroup1",
+            "cachegroup": "parentCachegroup",
             "cdnName": "cdn1",
             "domainName": "ga.atlanta.kabletown.net",
             "guid": null,
@@ -2778,7 +2843,7 @@
             "mgmtIpNetmask": "",
             "offlineReason": null,
             "physLocation": "Denver",
-            "profile": "EDGE1",
+            "profile": "MID1",
             "rack": "RR 119.02",
             "revalPending": false,
             "routerHostName": "",
@@ -4695,6 +4760,16 @@
                     "parents": [0]
                 }
             ]
+        },
+        {
+            "name": "top-with-no-mids",
+            "description": "A topology that has no MID_LOC cachegroups",
+            "nodes": [
+                {
+                    "cachegroup": "cachegroup1",
+                    "parents": []
+                }
+            ]
         }
     ],
     "types": [
diff --git a/traffic_ops/traffic_ops_golang/server/servers.go b/traffic_ops/traffic_ops_golang/server/servers.go
index 0baf08e..ea23be6 100644
--- a/traffic_ops/traffic_ops_golang/server/servers.go
+++ b/traffic_ops/traffic_ops_golang/server/servers.go
@@ -810,13 +810,14 @@ func getServers(h http.Header, params map[string]string, tx *sqlx.Tx, user *auth
 	usesMids := false
 	queryAddition := ""
 	dsHasRequiredCapabilities := false
+	var dsID int
 	var cdnID int
 	var serverCount uint64
 	var err error
 
 	if dsIDStr, ok := params[`dsId`]; ok {
 		// don't allow query on ds outside user's tenant
-		dsID, err := strconv.Atoi(dsIDStr)
+		dsID, err = strconv.Atoi(dsIDStr)
 		if err != nil {
 			return nil, 0, errors.New("dsId must be an integer"), nil, http.StatusNotFound, nil
 		}
@@ -931,7 +932,7 @@ func getServers(h http.Header, params map[string]string, tx *sqlx.Tx, user *auth
 
 	// if ds requested uses mid-tier caches, add those to the list as well
 	if usesMids {
-		midIDs, userErr, sysErr, errCode := getMidServers(ids, servers, cdnID, tx)
+		midIDs, userErr, sysErr, errCode := getMidServers(ids, servers, dsID, cdnID, tx)
 
 		log.Debugf("getting mids: %v, %v, %s\n", userErr, sysErr, http.StatusText(errCode))
 
@@ -1022,36 +1023,35 @@ func getServers(h http.Header, params map[string]string, tx *sqlx.Tx, user *auth
 }
 
 // getMidServers gets the mids used by the edges provided with an option to filter for a given cdn
-func getMidServers(edgeIDs []int, servers map[int]tc.ServerNullable, cdnID int, tx *sqlx.Tx) ([]int, error, error, int) {
+func getMidServers(edgeIDs []int, servers map[int]tc.ServerNullable, dsID int, cdnID int, tx *sqlx.Tx) ([]int, error, error, int) {
 	if len(edgeIDs) == 0 {
 		return nil, nil, nil, http.StatusOK
 	}
 
-	filters := []interface{}{
-		edgeIDs,
+	filters := map[string]interface{}{
+		"cache_type_mid": tc.CacheTypeMid,
+		"edge_ids":       pq.Array(edgeIDs),
+		"ds_id":          dsID,
 	}
 
 	// TODO: include secondary parent?
-	q := selectQuery + `
-	WHERE t.name = 'MID' AND s.cachegroup IN (
+	query := selectQuery + `
+	WHERE t.name = :cache_type_mid AND s.cachegroup IN (
 	SELECT cg.parent_cachegroup_id FROM cachegroup AS cg
 	WHERE cg.id IN (
 	SELECT s.cachegroup FROM server AS s
-	WHERE s.id IN (?)))
+	WHERE s.id = ANY(:edge_ids)))
+	AND (SELECT d.topology
+		FROM deliveryservice d
+		WHERE d.id = :ds_id) IS NULL
 	`
 
 	if cdnID > 0 {
-		q += ` AND s.cdn_id = ?`
-		filters = append(filters, cdnID)
+		query += ` AND s.cdn_id = :cdn_id`
+		filters["cdn_id"] = cdnID
 	}
 
-	query, args, err := sqlx.In(q, filters...)
-	if err != nil {
-		return nil, nil, fmt.Errorf("constructing mid servers query: %v", err), http.StatusInternalServerError
-	}
-	query = tx.Rebind(query)
-
-	rows, err := tx.Queryx(query, args...)
+	rows, err := tx.NamedQuery(query, filters)
 	if err != nil {
 		return nil, err, nil, http.StatusBadRequest
 	}
diff --git a/traffic_ops/traffic_ops_golang/server/servers_test.go b/traffic_ops/traffic_ops_golang/server/servers_test.go
index 7121cfd..5a00f67 100644
--- a/traffic_ops/traffic_ops_golang/server/servers_test.go
+++ b/traffic_ops/traffic_ops_golang/server/servers_test.go
@@ -479,7 +479,7 @@ func TestGetMidServers(t *testing.T) {
 
 	mock.ExpectBegin()
 	mock.ExpectQuery("SELECT").WillReturnRows(rows2)
-	mid, userErr, sysErr, errCode := getMidServers(serverIDs, serverMap, 0, db.MustBegin())
+	mid, userErr, sysErr, errCode := getMidServers(serverIDs, serverMap, 0, 0, db.MustBegin())
 
 	if userErr != nil || sysErr != nil {
 		t.Fatalf("getMidServers expected: no errors, actual: %v %v with status: %s", userErr, sysErr, http.StatusText(errCode))