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:33 UTC
[trafficcontrol] 04/08: Read cachegroup localizationMethods in
CRConfig
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 57ce8e4e7f0969c1a23b49a1027b176cf57a020a
Author: Rawlin Peters <ra...@comcast.com>
AuthorDate: Mon Jul 16 17:27:52 2018 -0600
Read cachegroup localizationMethods in CRConfig
---
lib/go-tc/crconfig.go | 10 ++++------
lib/go-tc/enum.go | 23 ++++++++++++++++++++++
.../traffic_ops_golang/crconfig/edgelocations.go | 11 +++++++++--
3 files changed, 36 insertions(+), 8 deletions(-)
diff --git a/lib/go-tc/crconfig.go b/lib/go-tc/crconfig.go
index 18133bf..1cf10d5 100644
--- a/lib/go-tc/crconfig.go
+++ b/lib/go-tc/crconfig.go
@@ -151,18 +151,16 @@ type CRConfigDispersion struct {
Shuffled bool `json:"shuffled,string"`
}
-
type CRConfigBackupLocations struct {
FallbackToClosest bool `json:"fallbackToClosest,string"`
List []string `json:"list,omitempty"`
-
}
type CRConfigLatitudeLongitude struct {
- Lat float64 `json:"latitude"`
- Lon float64 `json:"longitude"`
- BackupLocations CRConfigBackupLocations `json:"backupLocations,omitempty"`
-
+ Lat float64 `json:"latitude"`
+ Lon float64 `json:"longitude"`
+ BackupLocations CRConfigBackupLocations `json:"backupLocations,omitempty"`
+ LocalizationMethods []LocalizationMethod `json:"localizationMethods"`
}
type CRConfigLatitudeLongitudeShort struct {
diff --git a/lib/go-tc/enum.go b/lib/go-tc/enum.go
index b2dfa44..7a55301 100644
--- a/lib/go-tc/enum.go
+++ b/lib/go-tc/enum.go
@@ -29,8 +29,10 @@ package tc
*/
import (
+ "database/sql/driver"
"encoding/json"
"errors"
+ "fmt"
"strconv"
"strings"
)
@@ -242,6 +244,27 @@ func (m LocalizationMethod) MarshalJSON() ([]byte, error) {
return json.Marshal(m.String())
}
+func (m *LocalizationMethod) Scan(value interface{}) error {
+ if value == nil {
+ return errors.New("LocalizationMethod cannot be null")
+ }
+ sv, err := driver.String.ConvertValue(value)
+ if err != nil {
+ return errors.New("failed to scan LocalizationMethod: " + err.Error())
+ }
+
+ switch v := sv.(type) {
+ case []byte:
+ *m = LocalizationMethodFromString(string(v))
+ if *m == LocalizationMethodInvalid {
+ return errors.New(string(v) + " is not a valid LocalizationMethod")
+ }
+ return nil
+ default:
+ return fmt.Errorf("failed to scan LocalizationMethod, unknown input type: %T", value)
+ }
+}
+
// DeepCachingType represents a Delivery Service's deep caching type. The string values of this type should match the Traffic Ops values.
type DeepCachingType string
diff --git a/traffic_ops/traffic_ops_golang/crconfig/edgelocations.go b/traffic_ops/traffic_ops_golang/crconfig/edgelocations.go
index 364d336..86d8038 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/edgelocations.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/edgelocations.go
@@ -24,6 +24,7 @@ import (
"errors"
"github.com/apache/trafficcontrol/lib/go-tc"
+ "github.com/lib/pq"
)
func makeLocations(cdn string, db *sql.DB) (map[string]tc.CRConfigLatitudeLongitude, map[string]tc.CRConfigLatitudeLongitude, error) {
@@ -32,7 +33,9 @@ func makeLocations(cdn string, db *sql.DB) (map[string]tc.CRConfigLatitudeLongit
// TODO test whether it's faster to do a single query, joining lat/lon into servers
q := `
-select cg.name, cg.id, t.name as type, co.latitude, co.longitude, cg.fallback_to_closest from cachegroup as cg
+select cg.name, cg.id, t.name as type, co.latitude, co.longitude, cg.fallback_to_closest,
+(SELECT array_agg(method::text) FROM cachegroup_localization_method WHERE cachegroup = cg.id)
+from cachegroup as cg
left join coordinate as co on co.id = cg.coordinate
inner join server as s on s.cachegroup = cg.id
inner join type as t on t.id = s.type
@@ -54,9 +57,13 @@ and (st.name = 'REPORTED' or st.name = 'ONLINE' or st.name = 'ADMIN_DOWN')
ttype := ""
var fallbackToClosest *bool
latlon := tc.CRConfigLatitudeLongitude{}
- if err := rows.Scan(&cachegroup, &primaryCacheID, &ttype, &latlon.Lat, &latlon.Lon, &fallbackToClosest); err != nil {
+ if err := rows.Scan(&cachegroup, &primaryCacheID, &ttype, &latlon.Lat, &latlon.Lon, &fallbackToClosest, pq.Array(&latlon.LocalizationMethods)); err != nil {
return nil, nil, errors.New("Error scanning cachegroup: " + err.Error())
}
+ if len(latlon.LocalizationMethods) == 0 {
+ // to keep current default behavior when localizationMethods is unset/empty, enable all current localization methods
+ latlon.LocalizationMethods = []tc.LocalizationMethod{tc.LocalizationMethodGeo, tc.LocalizationMethodCZ, tc.LocalizationMethodDeepCZ}
+ }
if ttype == RouterTypeName {
routerLocs[cachegroup] = latlon
} else {