You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2020/07/17 20:24:43 UTC

[GitHub] [trafficcontrol] srijeet0406 opened a new pull request #4892: /cdns/capacity returns 500 error: capacity was zero

srijeet0406 opened a new pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892


   <!--
   ************ STOP!! ************
   If this Pull Request is intended to fix a security vulnerability, DO NOT submit it! Instead, contact
   the Apache Software Foundation Security Team at security@trafficcontrol.apache.org and follow the
   guidelines at https://www.apache.org/security/ regarding vulnerability disclosure.
   -->
   ## What does this PR (Pull Request) do?
   <!-- Explain the changes you made here. If this fixes an Issue, identify it by
   replacing the text in the checkbox item with the Issue number e.g.
   
   - [x] This PR fixes #9001 OR is not related to any Issue
   
   ^ This will automatically close Issue number 9001 when the Pull Request is
   merged (The '#' is important).
   
   Be sure you check the box properly, see the "The following criteria are ALL
   met by this PR" section for details.
   -->
   
   - [x] This PR fixes #4848  <!-- You can check for an issue here: https://github.com/apache/trafficcontrol/issues -->
   
   
   ## Which Traffic Control components are affected by this PR?
   <!-- Please delete all components from this list that are NOT affected by this
   Pull Request. Also, feel free to add the name of a tool or script that is
   affected but not on the list.
   
   Additionally, if this Pull Request does NOT affect documentation, please
   explain why documentation is not required. -->
   
   - CDN in a Box
   - Traffic Ops
   
   ## What is the best way to verify this PR?
   <!-- Please include here ALL the steps necessary to test your Pull Request. If
   it includes tests (and most should), outline here the steps needed to run the
   tests. If not, lay out the manual testing procedure and please explain why
   tests are unnecessary for this Pull Request. -->
   
   Two types of tests:
   1.) Test in a system where you dont have any monitors for your cdns. In such a system, if you make a call to GET `/cdns/capacity`, it should return a 500, and the error logs should show the reason of the failure as `no monitors available`
   2.) Test it in a system that has monitors, etc. (a full setup, like CiaB). Do a snapshot of one of the cdns, and then try to GET the `/cdns/capacity`. You should get a valid response this time.
   
   ## If this is a bug fix, what versions of Traffic Control are affected?
   <!-- If this PR fixes a bug, please list here all of the affected versions - to
   the best of your knowledge. It's also pretty helpful to include a commit hash
   of where 'master' is at the time this PR is opened (if it affects master),
   because what 'master' means will change over time. For example, if this PR
   fixes a bug that's present in master (at commit hash '1df853c8'), in v4.0.0,
   and in the current 4.0.1 Release candidate (e.g. RC1), then this list would
   look like:
   
   - master (1df853c8)
   - 4.0.0
   - 4.0.1 (RC1)
   
   If you don't know what other versions might have this bug, AND don't know how
   to find the commit hash of 'master', then feel free to leave this section
   blank (or, preferably, delete it entirely).
    -->
   
   - master
   ## The following criteria are ALL met by this PR
   <!-- Check the boxes to signify that the associated statement is true. To
   "check a box", replace the space inside of the square brackets with an 'x'.
   e.g.
   
   - [ x] <- Wrong
   - [x ] <- Wrong
   - [] <- Wrong
   - [*] <- Wrong
   - [x] <- Correct!
   
   -->
   
   - [x] I have explained why tests are unnecessary
   - [x] This PR includes documentation
   - [x] This PR includes an update to CHANGELOG.md
   - [x] This PR includes any and all required license headers
   - [x] This PR does not include a database migration
   - [x] This PR **DOES NOT FIX A SERIOUS SECURITY VULNERABILITY** (see [the Apache Software Foundation's security guidelines](https://www.apache.org/security/) for details)
   
   
   ## Additional Information
   <!-- If you would like to include any additional information on the PR for
   potential reviewers please put it here.
   
   Some examples of this would be:
   
   - Before and after screenshots/gifs of the Traffic Portal if it is affected
   - Links to other dependent Pull Requests
   - References to relevant context (e.g. new/updates to dependent libraries,
   mailing list records, blueprints)
   
   Feel free to leave this section blank (or, preferably, delete it entirely).
   -->
   
   <!--
   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you 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.
   -->
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] zrhoffman commented on a change in pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
zrhoffman commented on a change in pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892#discussion_r457611698



##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -117,10 +119,22 @@ func getMonitorsCapacity(tx *sql.Tx, monitors map[tc.CDNName][]string) (Capacity
 	}, nil
 }
 
+func checkServiceInterface(tx *sql.Tx, name tc.InterfaceName, cacheName tc.CacheName) bool {

Review comment:
       Since `checkServiceInterface` is used only once, it can be moved closer to the function that uses it.

##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -166,21 +181,39 @@ func addCapacity(cap CapData, cacheStats CacheStats, crStates tc.CRStates, crCon
 		if !strings.HasPrefix(*cache.ServerType, string(tc.CacheTypeEdge)) {
 			continue
 		}
-		if len(stats.KBPS) < 1 || len(stats.MaxKBPS) < 1 {
-			continue
+		var kbps, maxKbps float64
+		for intName, intStats := range stats {
+			if len(intStats.KBPS) < 1 || len(intStats.MaxKBPS) < 1 {
+				continue
+			}
+			if checkServiceInterface(tx, intName, cacheName) {
+				if len(intStats.KBPS) >= 1 {
+					if kbps < intStats.KBPS[0].Value {

Review comment:
       This can be a single *if* statement

##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -166,21 +181,39 @@ func addCapacity(cap CapData, cacheStats CacheStats, crStates tc.CRStates, crCon
 		if !strings.HasPrefix(*cache.ServerType, string(tc.CacheTypeEdge)) {
 			continue
 		}
-		if len(stats.KBPS) < 1 || len(stats.MaxKBPS) < 1 {
-			continue
+		var kbps, maxKbps float64
+		for intName, intStats := range stats {
+			if len(intStats.KBPS) < 1 || len(intStats.MaxKBPS) < 1 {
+				continue
+			}
+			if checkServiceInterface(tx, intName, cacheName) {
+				if len(intStats.KBPS) >= 1 {
+					if kbps < intStats.KBPS[0].Value {
+						kbps = intStats.KBPS[0].Value
+					}
+				}
+				if len(intStats.MaxKBPS) >= 1 {
+					if maxKbps < intStats.MaxKBPS[0].Value {

Review comment:
       This can also be a single *if* statement

##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -117,10 +119,22 @@ func getMonitorsCapacity(tx *sql.Tx, monitors map[tc.CDNName][]string) (Capacity
 	}, nil
 }
 
+func checkServiceInterface(tx *sql.Tx, name tc.InterfaceName, cacheName tc.CacheName) bool {
+	var result *bool
+	if err := tx.QueryRow(`SELECT i.service_address FROM ip_address AS i JOIN server as s on s.id = i.server WHERE i.interface=$1 AND s.host_name=$2`, name, cacheName).Scan(&result); err != nil {
+		log.Errorln("Error checking for service interface ", err)
+		return false
+	}
+	if *result == true {
+		return true
+	}
+	return false

Review comment:
       Since we're returning `true` when `*result` is `true` and `false` otherwise, this can just be
   
   ```golang
   return *result
   ```

##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -117,10 +119,22 @@ func getMonitorsCapacity(tx *sql.Tx, monitors map[tc.CDNName][]string) (Capacity
 	}, nil
 }
 
+func checkServiceInterface(tx *sql.Tx, name tc.InterfaceName, cacheName tc.CacheName) bool {
+	var result *bool
+	if err := tx.QueryRow(`SELECT i.service_address FROM ip_address AS i JOIN server as s on s.id = i.server WHERE i.interface=$1 AND s.host_name=$2`, name, cacheName).Scan(&result); err != nil {

Review comment:
       Please reformat this query to have more line breaks for readability.

##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -166,21 +181,39 @@ func addCapacity(cap CapData, cacheStats CacheStats, crStates tc.CRStates, crCon
 		if !strings.HasPrefix(*cache.ServerType, string(tc.CacheTypeEdge)) {
 			continue
 		}
-		if len(stats.KBPS) < 1 || len(stats.MaxKBPS) < 1 {
-			continue
+		var kbps, maxKbps float64
+		for intName, intStats := range stats {
+			if len(intStats.KBPS) < 1 || len(intStats.MaxKBPS) < 1 {
+				continue
+			}
+			if checkServiceInterface(tx, intName, cacheName) {

Review comment:
       Can this *if* statement's be extracted to save a level of indentation?
   
   Currently:
   ```golang
   if condition {
       /* several lines of code */
   }
   ```
   
   Restructured:
   ```golang
   if ! condition {
      continue
   }
    /* several lines of code */
   ```

##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -166,21 +181,39 @@ func addCapacity(cap CapData, cacheStats CacheStats, crStates tc.CRStates, crCon
 		if !strings.HasPrefix(*cache.ServerType, string(tc.CacheTypeEdge)) {
 			continue
 		}
-		if len(stats.KBPS) < 1 || len(stats.MaxKBPS) < 1 {
-			continue
+		var kbps, maxKbps float64
+		for intName, intStats := range stats {
+			if len(intStats.KBPS) < 1 || len(intStats.MaxKBPS) < 1 {
+				continue
+			}
+			if checkServiceInterface(tx, intName, cacheName) {

Review comment:
       Can `checkServiceInterface()` be rewritten so it only needs to be called a single time instead of once per cache?

##########
File path: lib/go-tc/enum.go
##########
@@ -55,6 +55,9 @@ type DeliveryServiceName string
 // CacheType is the type (or tier) of a CDN cache.
 type CacheType string
 
+// InterfaceName is the name of the interface

Review comment:
       This doesn't help a user who doesn't already know what interfaces are and how they are used. Maybe *server interface*?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] ocket8888 merged pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
ocket8888 merged pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] zrhoffman commented on a change in pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
zrhoffman commented on a change in pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892#discussion_r459073724



##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -166,25 +174,44 @@ func addCapacity(cap CapData, cacheStats CacheStats, crStates tc.CRStates, crCon
 		if !strings.HasPrefix(*cache.ServerType, string(tc.CacheTypeEdge)) {
 			continue
 		}
-		if len(stats.KBPS) < 1 || len(stats.MaxKBPS) < 1 {
-			continue
-		}
+		kbps, maxKbps := checkServiceInterface(tx, cacheName, stats)
 		if string(*cache.ServerStatus) == string(tc.CacheStatusReported) || string(*cache.ServerStatus) == string(tc.CacheStatusOnline) {
 			if crStates.Caches[cacheName].IsAvailable {
-				cap.Available += float64(stats.KBPS[0].Value)
+				cap.Available += kbps
 			} else {
-				cap.Unavailable += float64(stats.KBPS[0].Value)
+				cap.Unavailable += kbps
 			}
 		} else if string(*cache.ServerStatus) == string(tc.CacheStatusAdminDown) {
-			cap.Maintenance += float64(stats.KBPS[0].Value)
+			cap.Maintenance += kbps
 		} else {
 			continue // don't add capacity for OFFLINE or other statuses
 		}
-		cap.Capacity += float64(stats.MaxKBPS[0].Value) - thresholds[*cache.Profile]
+		cap.Capacity += maxKbps - thresholds[*cache.Profile]
 	}
 	return cap
 }
 
+func checkServiceInterface(tx *sql.Tx, cacheName tc.CacheName, stats map[tc.InterfaceName]CacheStat) (float64, float64) {
+	var result *bool
+	var kbps, maxKbps float64
+
+	for intName, intStats := range stats {
+		if err := tx.QueryRow(checkServiceAddressQuery, intName, cacheName).Scan(&result); err != nil {

Review comment:
       Is there a way to restructure this so that the query is not inside any loop? Ideally, we would run 1 query overall for all servers their interfaces.

##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -51,13 +51,20 @@ func GetCapacity(w http.ResponseWriter, r *http.Request) {
 const MonitorProxyParameter = "tm.traffic_mon_fwd_proxy"
 const MonitorRequestTimeout = time.Second * 10
 const MonitorOnlineStatus = "ONLINE"
+const checkServiceAddressQuery = `
+SELECT i.service_address FROM ip_address AS i 
+JOIN server as s on s.id = i.server 

Review comment:
       These 2 lines have trailing whitespace

##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity_test.go
##########
@@ -0,0 +1,87 @@
+package cdn
+
+import (
+	"github.com/apache/trafficcontrol/lib/go-tc"
+	"github.com/jmoiron/sqlx"
+	"gopkg.in/DATA-DOG/go-sqlmock.v1"
+	"testing"
+)
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+func TestCheckServiceInterface(t *testing.T) {
+	m := make(map[tc.InterfaceName]CacheStat)
+	var data1 []CacheStatData
+	var data2 []CacheStatData
+	kbpsData := CacheStatData{Value: 24.5}
+	maxKbpsData := CacheStatData{Value: 66.8}
+	data1 = append(data1, kbpsData)
+	data2 = append(data2, maxKbpsData)
+
+	c := CacheStat{
+		KBPS:    data1,
+		MaxKBPS: data2,
+	}
+	m["notservice"] = c
+
+	kbpsData = CacheStatData{Value: 50.0}
+	maxKbpsData = CacheStatData{Value: 100.9}
+	data1 = nil
+	data2 = nil
+	data1 = append(data1, kbpsData)
+	data2 = append(data2, maxKbpsData)
+
+	c = CacheStat{
+		KBPS:    data1,
+		MaxKBPS: data2,
+	}
+
+	m["service"] = c
+
+	mockDB, mock, err := sqlmock.New()
+	if err != nil {
+		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
+	}
+	defer mockDB.Close()
+
+	db := sqlx.NewDb(mockDB, "sqlmock")
+	defer db.Close()
+
+	cols := []string{"service_address"}
+	rows := sqlmock.NewRows(cols)
+	rows = rows.AddRow(
+		false,
+	)
+	mock.ExpectBegin()
+	mock.ExpectQuery("SELECT").WithArgs("notservice", "host").WillReturnRows(rows)
+	rows = sqlmock.NewRows(cols)
+	rows = rows.AddRow(
+		true,
+	)
+
+	mock.ExpectQuery("SELECT").WithArgs("service", "host").WillReturnRows(rows)
+	mock.ExpectCommit()
+
+	kbps, maxKbps := checkServiceInterface(db.MustBegin().Tx, "host", m)
+
+	if kbps != 50.0 || maxKbps != 100.9 {
+		t.Fatalf("Expected kbps = 50.0, got %v, expected maxKbps = 100.9, got %v", kbps, maxKbps)
+	}
+}

Review comment:
       Formatting: `gofmt` adds a newline at the end of this file.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] srijeet0406 commented on a change in pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
srijeet0406 commented on a change in pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892#discussion_r457688787



##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -166,21 +181,39 @@ func addCapacity(cap CapData, cacheStats CacheStats, crStates tc.CRStates, crCon
 		if !strings.HasPrefix(*cache.ServerType, string(tc.CacheTypeEdge)) {
 			continue
 		}
-		if len(stats.KBPS) < 1 || len(stats.MaxKBPS) < 1 {
-			continue
+		var kbps, maxKbps float64
+		for intName, intStats := range stats {
+			if len(intStats.KBPS) < 1 || len(intStats.MaxKBPS) < 1 {
+				continue
+			}
+			if checkServiceInterface(tx, intName, cacheName) {

Review comment:
       You mean once per interface, or once per cache?
   




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] zrhoffman commented on a change in pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
zrhoffman commented on a change in pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892#discussion_r457704125



##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -166,21 +181,39 @@ func addCapacity(cap CapData, cacheStats CacheStats, crStates tc.CRStates, crCon
 		if !strings.HasPrefix(*cache.ServerType, string(tc.CacheTypeEdge)) {
 			continue
 		}
-		if len(stats.KBPS) < 1 || len(stats.MaxKBPS) < 1 {
-			continue
+		var kbps, maxKbps float64
+		for intName, intStats := range stats {
+			if len(intStats.KBPS) < 1 || len(intStats.MaxKBPS) < 1 {
+				continue
+			}
+			if checkServiceInterface(tx, intName, cacheName) {

Review comment:
       Sounds good to me




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] srijeet0406 commented on a change in pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
srijeet0406 commented on a change in pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892#discussion_r457702518



##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -166,21 +181,39 @@ func addCapacity(cap CapData, cacheStats CacheStats, crStates tc.CRStates, crCon
 		if !strings.HasPrefix(*cache.ServerType, string(tc.CacheTypeEdge)) {
 			continue
 		}
-		if len(stats.KBPS) < 1 || len(stats.MaxKBPS) < 1 {
-			continue
+		var kbps, maxKbps float64
+		for intName, intStats := range stats {
+			if len(intStats.KBPS) < 1 || len(intStats.MaxKBPS) < 1 {
+				continue
+			}
+			if checkServiceInterface(tx, intName, cacheName) {

Review comment:
       Instead of doing a separate sql query to get the interfaces, we can just make use of the `stats` structure that we have, where we already have all the interface names. Saves us a DB call. But yeah, I can make it so that the `checkServiceInterface()` function will not need to be called once per interface. Does that make sense?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] ocket8888 commented on a change in pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on a change in pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892#discussion_r458914717



##########
File path: CHANGELOG.md
##########
@@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
 - Updated /deliveryservices/{{ID}}/servers/eligible to use multiple interfaces in API v3
 
 ### Fixed
+- Fixed #4848 - `GET /api/x/cdns/capacity` gives back 500, with the message `capacity was zero`. [Related github issue](https://github.com/apache/trafficcontrol/issues/4848)

Review comment:
       This is a nitpick, but you should link the actual issue text to the issue instead of adding it as post-text when the issue in question was mentioned (and, if viewed in Github, actually linked) earlier:
   
   ```markdown
   - Fixed [#4848](https://github.com/apache/trafficcontrol/issues/4848) - `GET /api/x/cdns/capacity` gives back 500, with the message `capacity was zero`.
   ```
   
   but since the changelog is primarily found on GitHub, you really don't need to explicitly link at all if you don't want to, like the entry directly below yours.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] zrhoffman commented on a change in pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
zrhoffman commented on a change in pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892#discussion_r459073724



##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -166,25 +174,44 @@ func addCapacity(cap CapData, cacheStats CacheStats, crStates tc.CRStates, crCon
 		if !strings.HasPrefix(*cache.ServerType, string(tc.CacheTypeEdge)) {
 			continue
 		}
-		if len(stats.KBPS) < 1 || len(stats.MaxKBPS) < 1 {
-			continue
-		}
+		kbps, maxKbps := checkServiceInterface(tx, cacheName, stats)
 		if string(*cache.ServerStatus) == string(tc.CacheStatusReported) || string(*cache.ServerStatus) == string(tc.CacheStatusOnline) {
 			if crStates.Caches[cacheName].IsAvailable {
-				cap.Available += float64(stats.KBPS[0].Value)
+				cap.Available += kbps
 			} else {
-				cap.Unavailable += float64(stats.KBPS[0].Value)
+				cap.Unavailable += kbps
 			}
 		} else if string(*cache.ServerStatus) == string(tc.CacheStatusAdminDown) {
-			cap.Maintenance += float64(stats.KBPS[0].Value)
+			cap.Maintenance += kbps
 		} else {
 			continue // don't add capacity for OFFLINE or other statuses
 		}
-		cap.Capacity += float64(stats.MaxKBPS[0].Value) - thresholds[*cache.Profile]
+		cap.Capacity += maxKbps - thresholds[*cache.Profile]
 	}
 	return cap
 }
 
+func checkServiceInterface(tx *sql.Tx, cacheName tc.CacheName, stats map[tc.InterfaceName]CacheStat) (float64, float64) {
+	var result *bool
+	var kbps, maxKbps float64
+
+	for intName, intStats := range stats {
+		if err := tx.QueryRow(checkServiceAddressQuery, intName, cacheName).Scan(&result); err != nil {

Review comment:
       Is there a way to restructure this so that the query is not inside any loop? Ideally, we would run 1 query overall for all servers and their interfaces.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] zrhoffman commented on pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
zrhoffman commented on pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892#issuecomment-665302506


   /cdns/capacity works, API tests pass, unit tests pass, code is formatted correctly. LGTM!


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] srijeet0406 commented on a change in pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
srijeet0406 commented on a change in pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892#discussion_r457702518



##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -166,21 +181,39 @@ func addCapacity(cap CapData, cacheStats CacheStats, crStates tc.CRStates, crCon
 		if !strings.HasPrefix(*cache.ServerType, string(tc.CacheTypeEdge)) {
 			continue
 		}
-		if len(stats.KBPS) < 1 || len(stats.MaxKBPS) < 1 {
-			continue
+		var kbps, maxKbps float64
+		for intName, intStats := range stats {
+			if len(intStats.KBPS) < 1 || len(intStats.MaxKBPS) < 1 {
+				continue
+			}
+			if checkServiceInterface(tx, intName, cacheName) {

Review comment:
       Instead of doing a separate sql query to get the interfaces, we can just make use of the `stats` structure tha we have, where we already have all the interface names. Saves us a DB call. But yeah, I can make it so that the `checkServiceInterface()` function will not need to be called once per interface. Does that make sense?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] srijeet0406 commented on pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
srijeet0406 commented on pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892#issuecomment-665104866


   @zrhoffman About the query performance, we have to traverse over a list of interfaces to get the service interface, because the stats associated with the service interface are the only stats that we are interested in. We dont really want any of the other ones. So, even if we get everything except the `aggregate` key, we would still have to run through that list and get the stats associated with only the service interface. 


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] zrhoffman commented on a change in pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
zrhoffman commented on a change in pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892#discussion_r457700308



##########
File path: traffic_ops/traffic_ops_golang/cdn/capacity.go
##########
@@ -166,21 +181,39 @@ func addCapacity(cap CapData, cacheStats CacheStats, crStates tc.CRStates, crCon
 		if !strings.HasPrefix(*cache.ServerType, string(tc.CacheTypeEdge)) {
 			continue
 		}
-		if len(stats.KBPS) < 1 || len(stats.MaxKBPS) < 1 {
-			continue
+		var kbps, maxKbps float64
+		for intName, intStats := range stats {
+			if len(intStats.KBPS) < 1 || len(intStats.MaxKBPS) < 1 {
+				continue
+			}
+			if checkServiceInterface(tx, intName, cacheName) {

Review comment:
       Yes I meant instead of once per interface. Ideally it will only be called once overall per request before entering the cache loop. Workflow could be:
   
   1. From your list of cache servers, get all interfaces by server. `ARRAY_AGG` can be used to list more than 1 interface in a single column so that it's only 1 row per server (with no performance penalty)
   
   2. Store the result to a struct
   
   3. In your interface loop, check the interface using that struct as the data source




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [trafficcontrol] zrhoffman removed a comment on pull request #4892: /cdns/capacity returns 500 error: capacity was zero

Posted by GitBox <gi...@apache.org>.
zrhoffman removed a comment on pull request #4892:
URL: https://github.com/apache/trafficcontrol/pull/4892#issuecomment-665302506


   /cdns/capacity works, API tests pass, unit tests pass, code is formatted correctly. LGTM!


----------------------------------------------------------------
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:
users@infra.apache.org