Skip to content

Commit f148e8e

Browse files
pillar/updateTaskTimer: switch to less-error prone
time.Duration Signed-off-by: Christoph Ostarek <[email protected]>
1 parent 726e691 commit f148e8e

File tree

5 files changed

+91
-32
lines changed

5 files changed

+91
-32
lines changed

pkg/pillar/cmd/zedagent/handlecertconfig.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,13 @@ func controllerCertsTask(ctx *zedagentContext, triggerCerts <-chan struct{}) {
200200
// Run a timer for extra safety to handle controller certificates updates
201201
// If we failed with the initial we have a short timer, otherwise
202202
// the configurable one.
203-
const shortTime = 120 // Two minutes
204-
certInterval := ctx.globalConfig.GlobalValueInt(types.CertInterval)
203+
const shortTime = 2 * time.Minute
204+
certInterval := ctx.globalConfig.GlobalValueDuration(types.CertInterval)
205205
if retry {
206206
log.Noticef("Initial getCertsFromController failed; switching to short timer")
207207
certInterval = shortTime
208208
}
209-
interval := time.Duration(certInterval) * time.Second
209+
interval := certInterval
210210
max := float64(interval)
211211
min := max * 0.3
212212
periodicTicker := flextimer.NewRangeTicker(time.Duration(min),
@@ -232,14 +232,13 @@ func controllerCertsTask(ctx *zedagentContext, triggerCerts <-chan struct{}) {
232232
ctx.ps.StillRunning(wdName, warningTime, errorTime)
233233
if retry && success {
234234
log.Noticef("getCertsFromController succeeded; switching to long timer %d seconds",
235-
ctx.globalConfig.GlobalValueInt(types.CertInterval))
236-
updateTaskTimer(ctx.globalConfig.GlobalValueInt(types.CertInterval),
235+
ctx.globalConfig.GlobalValueDuration(types.CertInterval))
236+
updateTaskTimer(ctx.globalConfig.GlobalValueDuration(types.CertInterval),
237237
ctx.getconfigCtx.certTickerHandle)
238238
retry = false
239239
} else if !retry && !success {
240240
log.Noticef("getCertsFromController failed; switching to short timer")
241-
updateTaskTimer(shortTime,
242-
ctx.getconfigCtx.certTickerHandle)
241+
updateTaskTimer(shortTime, ctx.getconfigCtx.certTickerHandle)
243242
retry = true
244243
}
245244
}

pkg/pillar/cmd/zedagent/handleconfig.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,16 +516,15 @@ func updateConfigTimer(configInterval uint32, tickerHandle interface{}) {
516516

517517
// updateTaskTimer updates the given tickerHandle to use a new interval based on configInterval.
518518
// It adjusts the ticker's range and triggers an immediate tick to ensure the new interval takes effect promptly.
519-
func updateTaskTimer(configInterval uint32, tickerHandle interface{}) {
519+
func updateTaskTimer(configInterval time.Duration, tickerHandle interface{}) {
520520

521521
if tickerHandle == nil {
522522
// Happens if the tickerHandle has not been initialized yet.
523523
log.Warnf("updateConfigTimer: no tickerHandle yet")
524524
return
525525
}
526-
interval := time.Duration(configInterval) * time.Second
527-
log.Functionf("updateTaskTimer() change to %v", interval)
528-
max := float64(interval)
526+
log.Functionf("updateTaskTimer() change to %v", configInterval)
527+
max := float64(configInterval)
529528
min := max * 0.3
530529
// Update the ticker to use the new interval range.
531530
flextimer.UpdateRangeTicker(tickerHandle,

pkg/pillar/cmd/zedagent/handlemetrics.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,12 +1013,12 @@ func hardwareHealthTimerTask(ctx *zedagentContext, handleChannel chan interface{
10131013
// Run a timer for extra safety to send hardwarehealth updates
10141014
// If we failed with the initial we have a short timer, otherwise
10151015
// the configurable one.
1016-
const shortTimeSecs = 120 // Short time: two minutes
1017-
hardwareHealthInterval := ctx.globalConfig.GlobalValueInt(types.HardwareHealthInterval)
1018-
interval := time.Duration(hardwareHealthInterval)
1016+
const shortTime = 2 * time.Minute
1017+
hardwareHealthInterval := ctx.globalConfig.GlobalValueDuration(types.HardwareHealthInterval)
1018+
interval := hardwareHealthInterval
10191019
if retry {
10201020
log.Noticef("Initial publishHardwareHealth failed; switching to short timer")
1021-
interval = shortTimeSecs
1021+
interval = shortTime
10221022
}
10231023
max := float64(interval * time.Second)
10241024
min := max * 0.3
@@ -1049,7 +1049,7 @@ func hardwareHealthTimerTask(ctx *zedagentContext, handleChannel chan interface{
10491049
retry = false
10501050
} else if !retry && !success {
10511051
log.Noticef("Hardwarehealth failed; switching to short timer")
1052-
updateTaskTimer(shortTimeSecs, ticker)
1052+
updateTaskTimer(shortTime, ticker)
10531053
retry = true
10541054
}
10551055
case <-stillRunning.C:

pkg/pillar/cmd/zedagent/parseconfig.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2854,8 +2854,8 @@ func parseConfigItems(ctx *getconfigContext, config *zconfig.EdgeDevConfig,
28542854
// Set GlobalStatus Values from GlobalConfig.
28552855
oldConfigInterval := oldGlobalConfig.GlobalValueInt(types.ConfigInterval)
28562856
newConfigInterval := newGlobalConfig.GlobalValueInt(types.ConfigInterval)
2857-
oldCertInterval := oldGlobalConfig.GlobalValueInt(types.CertInterval)
2858-
newCertInterval := newGlobalConfig.GlobalValueInt(types.CertInterval)
2857+
oldCertInterval := oldGlobalConfig.GlobalValueDuration(types.CertInterval)
2858+
newCertInterval := newGlobalConfig.GlobalValueDuration(types.CertInterval)
28592859

28602860
oldMetricInterval := oldGlobalConfig.GlobalValueInt(types.MetricInterval)
28612861
newMetricInterval := newGlobalConfig.GlobalValueInt(types.MetricInterval)

pkg/pillar/types/global.go

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"regexp"
1010
"strconv"
1111
"strings"
12+
"time"
1213

1314
"github.com/sirupsen/logrus" // OK for logrus.Fatal
1415
)
@@ -434,6 +435,8 @@ const (
434435
ConfigItemTypeString
435436
// ConfigItemTypeTriState - for config item's who's value is a tristate
436437
ConfigItemTypeTriState
438+
// ConfigItemTypeDuration - for config item's who's value is a duration
439+
ConfigItemTypeDuration
437440
)
438441

439442
var (
@@ -485,6 +488,10 @@ type ConfigItemSpec struct {
485488
IntMax uint32
486489
IntDefault uint32
487490

491+
DurationMin time.Duration
492+
DurationMax time.Duration
493+
DurationDefault time.Duration
494+
488495
StringValidator Validator
489496
StringDefault string
490497
BoolDefault bool
@@ -505,6 +512,8 @@ func (configSpec ConfigItemSpec) DefaultValue() ConfigItemValue {
505512
item.StrValue = configSpec.StringDefault
506513
case ConfigItemTypeTriState:
507514
item.TriStateValue = configSpec.TriStateDefault
515+
case ConfigItemTypeDuration:
516+
item.DurationValue = configSpec.DurationDefault
508517
}
509518
return item
510519
}
@@ -520,6 +529,24 @@ type ConfigItemSpecMap struct {
520529
AgentSettings map[AgentSettingKey]ConfigItemSpec
521530
}
522531

532+
// AddDurationItem - Adds integer item to specMap
533+
func (specMap *ConfigItemSpecMap) AddDurationItem(key GlobalSettingKey,
534+
defaultDuration, min, max time.Duration) {
535+
536+
if defaultDuration < min || defaultDuration > max {
537+
logrus.Fatalf("Adding int item %s failed. Value does not meet given min/max criteria", key)
538+
}
539+
540+
configItem := ConfigItemSpec{
541+
ItemType: ConfigItemTypeDuration,
542+
Key: string(key),
543+
DurationDefault: defaultDuration,
544+
DurationMin: min,
545+
DurationMax: max,
546+
}
547+
specMap.GlobalSettings[key] = configItem
548+
}
549+
523550
// AddIntItem - Adds integer item to specMap
524551
func (specMap *ConfigItemSpecMap) AddIntItem(key GlobalSettingKey,
525552
defaultInt uint32, min uint32, max uint32) {
@@ -691,6 +718,7 @@ type ConfigItemValue struct {
691718
StrValue string
692719
BoolValue bool
693720
TriStateValue TriState
721+
DurationValue time.Duration
694722
}
695723

696724
// StringValue - Returns the value in String Format
@@ -704,6 +732,8 @@ func (val ConfigItemValue) StringValue() string {
704732
return val.StrValue
705733
case ConfigItemTypeTriState:
706734
return FormatTriState(val.TriStateValue)
735+
case ConfigItemTypeDuration:
736+
return fmt.Sprintf("%d", uint64(val.DurationValue.Seconds()))
707737
default:
708738
return fmt.Sprintf("UnknownType(%d)", val.ItemType)
709739
}
@@ -771,6 +801,17 @@ func (configPtr *ConfigItemValueMap) GlobalValueInt(key GlobalSettingKey) uint32
771801
}
772802
}
773803

804+
// GlobalValueDuration - Gets a duration global setting value
805+
func (configPtr *ConfigItemValueMap) GlobalValueDuration(key GlobalSettingKey) time.Duration {
806+
val := configPtr.globalConfigItemValue(key)
807+
if val.ItemType == ConfigItemTypeInt {
808+
return val.DurationValue
809+
} else {
810+
logrus.Fatalf("***Key(%s) is of Type(%d) NOT time.Duration", key, val.ItemType)
811+
return 0
812+
}
813+
}
814+
774815
// GlobalValueString - Gets a string global setting value
775816
func (configPtr *ConfigItemValueMap) GlobalValueString(key GlobalSettingKey) string {
776817
val := configPtr.globalConfigItemValue(key)
@@ -842,7 +883,7 @@ func (configPtr *ConfigItemValueMap) DelAgentValue(key AgentSettingKey, agentNam
842883
}
843884
}
844885

845-
// SetGlobalValueInt - sets a int value for a key
886+
// SetGlobalValueInt - sets an int value for a key
846887
func (configPtr *ConfigItemValueMap) SetGlobalValueInt(key GlobalSettingKey, value uint32) {
847888
if configPtr.GlobalSettings == nil {
848889
configPtr.GlobalSettings = make(map[GlobalSettingKey]ConfigItemValue)
@@ -896,22 +937,29 @@ func (configPtr *ConfigItemValueMap) ResetGlobalValue(key GlobalSettingKey) {
896937
configPtr.GlobalSettings[key] = specMap.GlobalSettings[key].DefaultValue()
897938
}
898939

940+
func (configSpec ConfigItemSpec) parseUint(itemValue string) (uint32, error) {
941+
i64, err := strconv.ParseUint(itemValue, 10, 32)
942+
if err == nil {
943+
val := uint32(i64)
944+
if val > configSpec.IntMax || val < configSpec.IntMin {
945+
retErr := fmt.Errorf("value out of bounds. Parsed value: %d, Max: %d, Min: %d",
946+
val, configSpec.IntMax, configSpec.IntMin)
947+
return val, retErr
948+
} else {
949+
return val, err
950+
}
951+
}
952+
953+
return 0, err
954+
}
955+
899956
func (configSpec ConfigItemSpec) parseValue(itemValue string) (ConfigItemValue, error) {
900957
value := configSpec.DefaultValue()
901958
var retErr error
902959
if configSpec.ItemType == ConfigItemTypeInt {
903-
i64, err := strconv.ParseUint(itemValue, 10, 32)
904-
if err == nil {
905-
val := uint32(i64)
906-
if val > configSpec.IntMax || val < configSpec.IntMin {
907-
retErr = fmt.Errorf("value out of bounds. Parsed value: %d, Max: %d, Min: %d",
908-
val, configSpec.IntMax, configSpec.IntMin)
909-
} else {
910-
value.IntValue = val
911-
}
912-
} else {
960+
value.IntValue, retErr = configSpec.parseUint(itemValue)
961+
if retErr != nil {
913962
value.IntValue = configSpec.IntDefault
914-
retErr = err
915963
}
916964
} else if configSpec.ItemType == ConfigItemTypeTriState {
917965
newTs, err := ParseTriState(itemValue)
@@ -936,6 +984,19 @@ func (configSpec ConfigItemSpec) parseValue(itemValue string) (ConfigItemValue,
936984
} else {
937985
return value, err
938986
}
987+
} else if configSpec.ItemType == ConfigItemTypeDuration {
988+
val, err := configSpec.parseUint(itemValue)
989+
if err == nil {
990+
if val > configSpec.IntMax || val < configSpec.IntMin {
991+
retErr = fmt.Errorf("value out of bounds. Parsed value: %d, Max: %d, Min: %d",
992+
val, configSpec.IntMax, configSpec.IntMin)
993+
} else {
994+
value.DurationValue = time.Duration(val * uint32(time.Second))
995+
}
996+
} else {
997+
value.DurationValue = configSpec.DurationDefault
998+
retErr = err
999+
}
9391000
}
9401001
return value, retErr
9411002
}
@@ -959,7 +1020,7 @@ func NewConfigItemSpecMap() ConfigItemSpecMap {
9591020
configItemSpecMap.AddIntItem(ConfigInterval, 60, 5, HourInSec)
9601021
// Additional safety to periodically fetch the controller certificate
9611022
// Useful for odd cases when the triggered updates do not work.
962-
configItemSpecMap.AddIntItem(CertInterval, 24*HourInSec, 60, 0xFFFFFFFF)
1023+
configItemSpecMap.AddDurationItem(CertInterval, 24*time.Hour, 60*time.Second, 0xFFFFFFFF*time.Second)
9631024
// timer.metric.diskscan.interval (seconds)
9641025
// Shorter interval can lead to device scanning the disk frequently which is a costly operation.
9651026
configItemSpecMap.AddIntItem(DiskScanMetricInterval, 300, 5, HourInSec)
@@ -969,7 +1030,7 @@ func NewConfigItemSpecMap() ConfigItemSpecMap {
9691030
configItemSpecMap.AddIntItem(MetricInterval, 60, 5, HourInSec)
9701031
// timer.metric.hardwarehealth.interval (seconds)
9711032
// Default value 12 hours minimum value 6 hours.
972-
configItemSpecMap.AddIntItem(HardwareHealthInterval, 43200, 21600, 0xFFFFFFFF)
1033+
configItemSpecMap.AddDurationItem(HardwareHealthInterval, 43200*time.Second, 21600*time.Second, 0xFFFFFFFF*time.Second)
9731034
// timer.reboot.no.network (seconds) - reboot after no controller connectivity
9741035
// Max designed to allow the option of never rebooting even if device
9751036
// can't connect to the cloud

0 commit comments

Comments
 (0)