You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by de...@apache.org on 2018/08/06 15:48:35 UTC

[trafficcontrol] 06/08: Make cachegroups select query return localizationMethods as an array

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

dewrich pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit d674b31a3df996dab9eac047b96dc801d831c3fc
Author: Rawlin Peters <ra...@comcast.com>
AuthorDate: Tue Jul 17 17:27:28 2018 -0600

    Make cachegroups select query return localizationMethods as an array
    
    Similar to the CRConfig edgeLocations, scan localizationMethods directly
    from a postgres array rather than doing a separate query.
---
 lib/go-tc/v13/cachegroups.go                       |  4 +-
 .../traffic_ops_golang/cachegroup/cachegroups.go   | 64 +++++++---------------
 .../cachegroup/cachegroups_test.go                 | 41 +++++++++-----
 3 files changed, 48 insertions(+), 61 deletions(-)

diff --git a/lib/go-tc/v13/cachegroups.go b/lib/go-tc/v13/cachegroups.go
index 222216a..c2fce59 100644
--- a/lib/go-tc/v13/cachegroups.go
+++ b/lib/go-tc/v13/cachegroups.go
@@ -51,7 +51,7 @@ type CacheGroup struct {
 	SecondaryParentName         string                  `json:"secondaryParentCachegroupName" db:"secondary_parent_cachegroup_name"`
 	SecondaryParentCachegroupID int                     `json:"secondaryParentCachegroupId" db:"secondary_parent_cachegroup_id"`
 	FallbackToClosest           bool                    `json:"fallbackToClosest" db:"fallback_to_closest"`
-	LocalizationMethods         []tc.LocalizationMethod `json:"localizationMethods"`
+	LocalizationMethods         []tc.LocalizationMethod `json:"localizationMethods" db:"localization_methods"`
 	Type                        string                  `json:"typeName" db:"type_name"` // aliased to type_name to disambiguate struct scans due to join on 'type' table
 	TypeID                      int                     `json:"typeId" db:"type_id"`     // aliased to type_id to disambiguate struct scans due join on 'type' table
 	LastUpdated                 tc.TimeNoMod            `json:"lastUpdated" db:"last_updated"`
@@ -68,7 +68,7 @@ type CacheGroupNullable struct {
 	SecondaryParentName         *string                  `json:"secondaryParentCachegroupName" db:"secondary_parent_cachegroup_name"`
 	SecondaryParentCachegroupID *int                     `json:"secondaryParentCachegroupId" db:"secondary_parent_cachegroup_id"`
 	FallbackToClosest           *bool                    `json:"fallbackToClosest" db:"fallback_to_closest"`
-	LocalizationMethods         *[]tc.LocalizationMethod `json:"localizationMethods"`
+	LocalizationMethods         *[]tc.LocalizationMethod `json:"localizationMethods" db:"localization_methods"`
 	Type                        *string                  `json:"typeName" db:"type_name"` // aliased to type_name to disambiguate struct scans due to join on 'type' table
 	TypeID                      *int                     `json:"typeId" db:"type_id"`     // aliased to type_id to disambiguate struct scans due join on 'type' table
 	LastUpdated                 *tc.TimeNoMod            `json:"lastUpdated" db:"last_updated"`
diff --git a/traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go b/traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
index 68e0bc3..d9e7ab4 100644
--- a/traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
+++ b/traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
@@ -261,34 +261,6 @@ func (cg *TOCacheGroup) createLocalizationMethods() error {
 	return nil
 }
 
-func readGetLocalizationMethods(cacheGroups []TOCacheGroup, tx *sqlx.Tx) (map[int][]tc.LocalizationMethod, error) {
-	ids := make([]int, len(cacheGroups))
-	for i, cg := range cacheGroups {
-		ids[i] = *cg.ID
-	}
-	q := `SELECT cachegroup, method::text FROM cachegroup_localization_method WHERE cachegroup = ANY($1)`
-	rows, err := tx.Query(q, pq.Array(ids))
-	if err != nil {
-		return nil, errors.New("querying cachegroup localization methods: " + err.Error())
-	}
-	defer rows.Close()
-
-	methods := make(map[int][]tc.LocalizationMethod, len(cacheGroups))
-	for rows.Next() {
-		cg := 0
-		methodStr := ""
-		if err := rows.Scan(&cg, &methodStr); err != nil {
-			return nil, errors.New("scanning cachegroup localization methods: " + err.Error())
-		}
-		method := tc.LocalizationMethodFromString(methodStr)
-		if method == tc.LocalizationMethodInvalid {
-			return nil, errors.New("getting cachegroup localization methods: got invalid localization method: '" + methodStr + "'")
-		}
-		methods[cg] = append(methods[cg], method)
-	}
-	return methods, nil
-}
-
 func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	var coordinateID *int
 	if cg.Latitude != nil && cg.Longitude != nil {
@@ -371,30 +343,33 @@ func (cg *TOCacheGroup) Read(parameters map[string]string) ([]interface{}, []err
 	}
 	defer rows.Close()
 
-	cacheGroups := []TOCacheGroup{}
+	cacheGroups := []interface{}{}
 	for rows.Next() {
 		var s TOCacheGroup
-		if err = rows.StructScan(&s); err != nil {
+		lms := make([]tc.LocalizationMethod, 0)
+		if err = rows.Scan(
+			&s.ID,
+			&s.Name,
+			&s.ShortName,
+			&s.Latitude,
+			&s.Longitude,
+			pq.Array(&lms),
+			&s.ParentCachegroupID,
+			&s.ParentName,
+			&s.SecondaryParentCachegroupID,
+			&s.SecondaryParentName,
+			&s.Type,
+			&s.TypeID,
+			&s.LastUpdated,
+		); err != nil {
 			log.Errorf("error parsing CacheGroup rows: %v", err)
 			return nil, []error{tc.DBError}, tc.SystemError
 		}
+		s.LocalizationMethods = &lms
 		cacheGroups = append(cacheGroups, s)
 	}
 
-	returnable := make([]interface{}, len(cacheGroups))
-	cacheGroupsToLocalizationMethods, err := readGetLocalizationMethods(cacheGroups, cg.ReqInfo.Tx)
-	if err != nil {
-		log.Errorln(err.Error())
-		return nil, []error{tc.DBError}, tc.SystemError
-	}
-	for i, cacheGroup := range cacheGroups {
-		if localizationMethods, ok := cacheGroupsToLocalizationMethods[*cacheGroup.ID]; ok {
-			cacheGroup.LocalizationMethods = &localizationMethods
-		}
-		returnable[i] = cacheGroup
-	}
-
-	return returnable, []error{}, tc.NoError
+	return cacheGroups, []error{}, tc.NoError
 }
 
 //The TOCacheGroup implementation of the Updater interface
@@ -573,6 +548,7 @@ cachegroup.name,
 cachegroup.short_name,
 coordinate.latitude,
 coordinate.longitude,
+(SELECT array_agg(CAST(method as text)) AS localization_methods FROM cachegroup_localization_method clm WHERE clm.cachegroup = cachegroup.id),
 cachegroup.parent_cachegroup_id,
 cgp.name AS parent_cachegroup_name,
 cachegroup.secondary_parent_cachegroup_id,
diff --git a/traffic_ops/traffic_ops_golang/cachegroup/cachegroups_test.go b/traffic_ops/traffic_ops_golang/cachegroup/cachegroups_test.go
index c39b53a..2335f70 100644
--- a/traffic_ops/traffic_ops_golang/cachegroup/cachegroups_test.go
+++ b/traffic_ops/traffic_ops_golang/cachegroup/cachegroups_test.go
@@ -65,10 +65,14 @@ func getTestCacheGroups() []v13.CacheGroup {
 		Longitude:                   90.7,
 		ParentCachegroupID:          1,
 		SecondaryParentCachegroupID: 1,
-		LocalizationMethods:         []tc.LocalizationMethod{},
-		Type:                        "MID_LOC",
-		TypeID:                      7,
-		LastUpdated:                 tc.TimeNoMod{Time: time.Now()},
+		LocalizationMethods: []tc.LocalizationMethod{
+			tc.LocalizationMethodDeepCZ,
+			tc.LocalizationMethodCZ,
+			tc.LocalizationMethodGeo,
+		},
+		Type:        "MID_LOC",
+		TypeID:      7,
+		LastUpdated: tc.TimeNoMod{Time: time.Now()},
 	}
 	cgs = append(cgs, testCG2)
 
@@ -86,10 +90,21 @@ func TestReadCacheGroups(t *testing.T) {
 	defer db.Close()
 
 	testCGs := getTestCacheGroups()
-	cols := test.ColsFromStructByTag("db", v13.CacheGroup{})
-	rows := sqlmock.NewRows(cols)
-
-	methodsRows := sqlmock.NewRows([]string{"cachegroup", "method"})
+	rows := sqlmock.NewRows([]string{
+		"id",
+		"name",
+		"short_name",
+		"latitude",
+		"longitude",
+		"localization_methods",
+		"parent_cachegroup_id",
+		"parent_cachegroup_name",
+		"secondary_parent_cachegroup_id",
+		"secondary_parent_cachegroup_name",
+		"type_name",
+		"type_id",
+		"last_updated",
+	})
 
 	for _, ts := range testCGs {
 		rows = rows.AddRow(
@@ -98,22 +113,18 @@ func TestReadCacheGroups(t *testing.T) {
 			ts.ShortName,
 			ts.Latitude,
 			ts.Longitude,
-			ts.ParentName,
+			[]byte("{DEEP_CZ,CZ,GEO}"),
 			ts.ParentCachegroupID,
-			ts.SecondaryParentName,
+			ts.ParentName,
 			ts.SecondaryParentCachegroupID,
-			ts.FallbackToClosest,
+			ts.SecondaryParentName,
 			ts.Type,
 			ts.TypeID,
 			ts.LastUpdated,
 		)
-		for _, m := range ts.LocalizationMethods {
-			methodsRows.AddRow(ts.ID, m)
-		}
 	}
 	mock.ExpectBegin()
 	mock.ExpectQuery("SELECT").WillReturnRows(rows)
-	mock.ExpectQuery("SELECT").WillReturnRows(methodsRows)
 	mock.ExpectCommit()
 	v := map[string]string{"id": "1"}