trafficcontrol-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r..@apache.org
Subject [trafficcontrol] 02/04: Fixing review comments
Date Fri, 14 Dec 2018 04:03:37 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 1e91b01c6abaffede05e50f0668cad5fe6f345cf
Author: Evan Zelkowitz <19699200+ezelkow1@users.noreply.github.com>
AuthorDate: Tue Dec 11 09:47:13 2018 -0700

    Fixing review comments
---
 traffic_monitor/config/config.go | 24 ++++++------
 traffic_monitor/towrap/towrap.go | 81 ++++++++++++++++++++++------------------
 2 files changed, 56 insertions(+), 49 deletions(-)

diff --git a/traffic_monitor/config/config.go b/traffic_monitor/config/config.go
index 4f38343..7ec8fe1 100644
--- a/traffic_monitor/config/config.go
+++ b/traffic_monitor/config/config.go
@@ -41,9 +41,9 @@ const (
 	//StaticFileDir is the directory that contains static html and js files.
 	StaticFileDir = "/opt/traffic_monitor/static/"
 	//CrConfigBackupFile is the default file name to store the last crconfig
-	CrConfigBackupFile = "crconfig.backup"
+	CRConfigBackupFile = "crconfig.backup"
 	//TmConfigBackupFile is the default file name to store the last tmconfig
-	TmConfigBackupFile = "tmconfig.backup"
+	TMConfigBackupFile = "tmconfig.backup"
 )
 
 // Config is the configuration for the application. It includes myriad data, such as polling
intervals and log locations.
@@ -72,8 +72,8 @@ type Config struct {
 	CRConfigHistoryCount         uint64        `json:"crconfig_history_count"`
 	TrafficOpsMinRetryInterval   time.Duration `json:"-"`
 	TrafficOpsMaxRetryInterval   time.Duration `json:"-"`
-	CrConfigBackupFile           string        `json:"crconfig_backup_file"`
-	TmConfigBackupFile           string        `json:"tmconfig_backup_file"`
+	CRConfigBackupFile           string        `json:"crconfig_backup_file"`
+	TMConfigBackupFile           string        `json:"tmconfig_backup_file"`
 	TrafficOpsDiskRetryMax       uint64        `json:"-"`
 }
 
@@ -109,8 +109,8 @@ var DefaultConfig = Config{
 	CRConfigHistoryCount:         20000,
 	TrafficOpsMinRetryInterval:   100 * time.Millisecond,
 	TrafficOpsMaxRetryInterval:   60000 * time.Millisecond,
-	CrConfigBackupFile:           CrConfigBackupFile,
-	TmConfigBackupFile:           TmConfigBackupFile,
+	CRConfigBackupFile:           CRConfigBackupFile,
+	TMConfigBackupFile:           TMConfigBackupFile,
 	TrafficOpsDiskRetryMax:       2,
 }
 
@@ -163,8 +163,8 @@ func (c *Config) UnmarshalJSON(data []byte) error {
 		TrafficOpsMinRetryIntervalMs   *uint64 `json:"traffic_ops_min_retry_interval_ms"`
 		TrafficOpsMaxRetryIntervalMs   *uint64 `json:"traffic_ops_max_retry_interval_ms"`
 		TrafficOpsDiskRetryMax         *uint64 `json:"traffic_ops_disk_retry_max"`
-		CrConfigBackupFile             *string `json:"crconfig_backup_file"`
-		TmConfigBackupFile             *string `json:"tmconfig_backup_file"`
+		CRConfigBackupFile             *string `json:"crconfig_backup_file"`
+		TMConfigBackupFile             *string `json:"tmconfig_backup_file"`
 		*Alias
 	}{
 		Alias: (*Alias)(c),
@@ -216,11 +216,11 @@ func (c *Config) UnmarshalJSON(data []byte) error {
 	if aux.TrafficOpsDiskRetryMax != nil {
 		c.TrafficOpsDiskRetryMax = *aux.TrafficOpsDiskRetryMax
 	}
-	if aux.CrConfigBackupFile != nil {
-		c.CrConfigBackupFile = *aux.CrConfigBackupFile
+	if aux.CRConfigBackupFile != nil {
+		c.CRConfigBackupFile = *aux.CRConfigBackupFile
 	}
-	if aux.TmConfigBackupFile != nil {
-		c.TmConfigBackupFile = *aux.TmConfigBackupFile
+	if aux.TMConfigBackupFile != nil {
+		c.TMConfigBackupFile = *aux.TMConfigBackupFile
 	}
 	return nil
 }
diff --git a/traffic_monitor/towrap/towrap.go b/traffic_monitor/towrap/towrap.go
index fc1cc9e..f6a9d4d 100644
--- a/traffic_monitor/towrap/towrap.go
+++ b/traffic_monitor/towrap/towrap.go
@@ -53,6 +53,8 @@ type ITrafficOpsSession interface {
 	BackupFileExists() bool
 }
 
+const localHostIP = "127.0.0.1"
+
 var ErrNilSession = fmt.Errorf("nil session")
 
 // TODO rename CRConfigCacheObj
@@ -68,8 +70,8 @@ type ByteMapCache struct {
 }
 
 func (s TrafficOpsSessionThreadsafe) BackupFileExists() bool {
-	if _, err := os.Stat(s.CrConfigBackupFile); !os.IsNotExist(err) {
-		if _, err = os.Stat(s.TmConfigBackupFile); !os.IsNotExist(err) {
+	if _, err := os.Stat(s.CRConfigBackupFile); !os.IsNotExist(err) {
+		if _, err = os.Stat(s.TMConfigBackupFile); !os.IsNotExist(err) {
 			return true
 		}
 	}
@@ -165,13 +167,13 @@ type TrafficOpsSessionThreadsafe struct {
 	m                  *sync.Mutex
 	lastCRConfig       ByteMapCache
 	crConfigHist       CRConfigHistoryThreadsafe
-	CrConfigBackupFile string
-	TmConfigBackupFile string
+	CRConfigBackupFile string
+	TMConfigBackupFile string
 }
 
 // NewTrafficOpsSessionThreadsafe returns a new threadsafe TrafficOpsSessionThreadsafe wrapping
the given `Session`.
 func NewTrafficOpsSessionThreadsafe(s *client.Session, crConfigHistoryLimit uint64, cfg config.Config)
TrafficOpsSessionThreadsafe {
-	return TrafficOpsSessionThreadsafe{session: &s, m: &sync.Mutex{}, lastCRConfig:
NewByteMapCache(), crConfigHist: NewCRConfigHistoryThreadsafe(crConfigHistoryLimit), CrConfigBackupFile:
cfg.CrConfigBackupFile, TmConfigBackupFile: cfg.TmConfigBackupFile}
+	return TrafficOpsSessionThreadsafe{session: &s, m: &sync.Mutex{}, lastCRConfig:
NewByteMapCache(), crConfigHist: NewCRConfigHistoryThreadsafe(crConfigHistoryLimit), CRConfigBackupFile:
cfg.CRConfigBackupFile, TMConfigBackupFile: cfg.TMConfigBackupFile}
 }
 
 // Set sets the internal Traffic Ops session. This is safe for multiple goroutines, being
aware they will race.
@@ -247,33 +249,35 @@ func (s TrafficOpsSessionThreadsafe) CRConfigRaw(cdn string) ([]byte,
error) {
 
 	ss := s.get()
 
-	var b []byte
-	var err error
 	var remoteAddr string
-	var reqInf client.ReqInf
 
 	if ss == nil {
 		return nil, ErrNilSession
+	}
+
+	b, reqInf, err := ss.GetCRConfig(cdn)
+	if err == nil {
+		remoteAddr = reqInf.RemoteAddr.String()
+		ioutil.WriteFile(s.CRConfigBackupFile, b, 0644)
 	} else {
-		b, reqInf, err = ss.GetCRConfig(cdn)
-		if err == nil {
-			remoteAddr = reqInf.RemoteAddr.String()
-			ioutil.WriteFile(s.CrConfigBackupFile, b, 0644)
-		} else {
-			if s.BackupFileExists() {
-				b, _ = ioutil.ReadFile(s.CrConfigBackupFile)
-				remoteAddr = "127.0.0.1"
-				log.Errorln("Error getting CRConfig from traffic_ops, backup file exists, reading from
file")
-			} else {
-				return nil, ErrNilSession
+		if s.BackupFileExists() {
+			b, err = ioutil.ReadFile(s.CRConfigBackupFile)
+			if err != nil {
+				err = errors.New("file Read Error: " + err.Error())
+				return nil, err
 			}
+			remoteAddr = localHostIP
+			log.Errorln("Error getting CRConfig from traffic_ops, backup file exists, reading from
file")
+			err = nil
+		} else {
+			return nil, ErrNilSession
 		}
 	}
 
 	hist := &CRConfigStat{time.Now(), remoteAddr, tc.CRConfigStats{}, err}
 	defer s.crConfigHist.Add(hist)
 
-	if err != nil && remoteAddr != "127.0.0.1" {
+	if err != nil {
 		return b, err
 	}
 
@@ -312,29 +316,32 @@ func (s TrafficOpsSessionThreadsafe) trafficMonitorConfigMapRaw(cdn
string) (*tc
 	if ss == nil {
 		return nil, ErrNilSession
 	}
-	var configMap *tc.TrafficMonitorConfigMap
-	var err error
 
-	configMap, _, err = ss.GetTrafficMonitorConfigMap(cdn)
-	if configMap != nil {
-		json := jsoniter.ConfigFastest
-		data, err := json.Marshal(*configMap)
-		if err == nil {
-			ioutil.WriteFile(s.TmConfigBackupFile, data, 0644)
+	configMap, _, err := ss.GetTrafficMonitorConfigMap(cdn)
+	if err != nil {
+		// Default error case, no backup file exists
+		if !s.BackupFileExists() {
+			return configMap, err
 		}
-	} else {
-		if s.BackupFileExists() {
-			b, _ := ioutil.ReadFile(s.TmConfigBackupFile)
-			log.Errorln("Error getting configMap from traffic_ops, backup file exists, reading from
file")
-			json := jsoniter.ConfigFastest
-			err = json.Unmarshal(b, &configMap)
-			if err != nil {
-				log.Warnf("Error unmarshaling TmConfigBackupFile, ", err)
-			}
 
+		b, err := ioutil.ReadFile(s.TMConfigBackupFile)
+		if err != nil {
+			return nil, errors.New("reading TMConfigBackupFile: " + err.Error())
 		}
+
+		log.Errorln("Error getting configMap from traffic_ops, backup file exists, reading from
file")
+		json := jsoniter.ConfigFastest
+		if err = json.Unmarshal(b, &configMap); err != nil {
+			log.Errorln("Error unmarshaling JSON from TMConfigBackupFile")
+		}
+		return configMap, err
 	}
 
+	json := jsoniter.ConfigFastest
+	data, err := json.Marshal(*configMap)
+	if err == nil {
+		ioutil.WriteFile(s.TMConfigBackupFile, data, 0644)
+	}
 	return configMap, err
 }
 


Mime
View raw message