Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Commit

Permalink
Fix dConfig in the inputhost; add string validator for dConfig (#199)
Browse files Browse the repository at this point in the history
* Fix dConfig in the inputhost; add string validator for dConfig

* Add log lines for inputhost set operations
  • Loading branch information
Guillaume Bailey authored and Kiran RG committed May 30, 2017
1 parent ffa0947 commit 417e166
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
20 changes: 19 additions & 1 deletion common/dconfigclient/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package dconfigclient

import (
"fmt"
"regexp"

log "github.com/Sirupsen/logrus"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ func GenerateIntMaxMinVerifier(dconfigKey string, minV int, maxV int) Verifier {
verifierFunc := func(_ interface{}, newV interface{}) bool {
newValue, okNew := newV.(int)
if !okNew {
log.WithFields(log.Fields{"dconfigKey": dconfigKey, "newValue": fmt.Sprintf("%v", newV)}).Error("verify failed for the dconfig key; please use digit only")
log.WithFields(log.Fields{"dconfigKey": dconfigKey, "newValue": fmt.Sprintf("%v", newV)}).Error("type-assertion to int failed for the dconfig key")
return false
}
if newValue < minV && newValue > maxV {
Expand All @@ -74,6 +75,23 @@ func GenerateIntMaxMinVerifier(dconfigKey string, minV int, maxV int) Verifier {
return verifierFunc
}

// GenerateStringRegexpVerifier return a dconfig Verifier for the dconfigKey
func GenerateStringRegexpVerifier(dconfigKey string, r *regexp.Regexp) Verifier {
verifierFunc := func(_ interface{}, newV interface{}) bool {
newValue, okNew := newV.(string)
if !okNew {
log.WithFields(log.Fields{"dconfigKey": dconfigKey, "newValue": fmt.Sprintf("%v", newV)}).Error("type-assertion to string failed for the dconfig key")
return false
}
if !r.MatchString(newValue) {
log.WithFields(log.Fields{"dconfigKey": dconfigKey, "newValue": fmt.Sprintf("%v", newValue), "regexp": r.String()}).Error("verify for the dconfigKey failed; new value doesn't match regular expression")
return false
}
return true
}
return verifierFunc
}

// GenerateStringHandler return a dconfig Handler for the dconfigKey
func GenerateStringHandler(dconfigKey string, setter SetterString, getter GetterString) Handler {
handlerFunc := func(newConfig interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion common/dconfigclient/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type (
// AddVerifiers register all the handler for the dynamic config values
AddVerifiers(verifierMap map[string]Verifier)

// Refresh relesh the dynamic config client
// Refresh the dynamic config client
Refresh()

// StartBackGroundRefresh start the back fround refresh for dynamic config
Expand Down
7 changes: 7 additions & 0 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,13 @@ func NewCliHelper() CliHelper {
}
}

// OverrideValueByPrefixJoinedValidatorRegexp is a regular expression that validates a comma separated list of OverrideValueByPrefix rules
// Note that this presumes that the overrides []string will be split with strings.Split(rules, ",") or similar
var OverrideValueByPrefixJoinedValidatorRegexp = regexp.MustCompile(`^[^=]*=[0-9]+(,[^=]*=[0-9]+)*$`)

// OverrideValueByPrefixSingleRuleValidatorRegexp is a regular expression that validates a single OverrideValueByPrefix rule
var OverrideValueByPrefixSingleRuleValidatorRegexp = regexp.MustCompile(`^[^=]*=[0-9]+$`)

var overrideValueByPrefixLogMapLock sync.RWMutex
var overrideValueByPrefixLogMap = make(map[string]struct{})

Expand Down
27 changes: 3 additions & 24 deletions services/inputhost/dynamicConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package inputhost

import (
log "github.com/Sirupsen/logrus"
"github.com/uber/cherami-server/common"
dconfig "github.com/uber/cherami-server/common/dconfigclient"
)
Expand All @@ -41,7 +40,7 @@ const (
UkeyTestShortExts = "inputhost.TestShortExtentsByPath"
)

func (h *InputHost) registerInt() {
func (h *InputHost) registerDConfig() {
// Add handler function for the dynamic config value
handlerMap := make(map[string]dconfig.Handler)
handlerMap[UkeyHostOverall] = dconfig.GenerateIntHandler(UkeyHostOverall, h.SetHostConnLimit, h.GetHostConnLimitOverall)
Expand All @@ -58,33 +57,13 @@ func (h *InputHost) registerInt() {
verifierMap[UkeyMaxConnPerDest] = dconfig.GenerateIntMaxMinVerifier(UkeyMaxConnPerDest, 1, common.MaxHostMaxConnPerDestination)
verifierMap[UkeyExtMsgs] = dconfig.GenerateIntMaxMinVerifier(UkeyExtMsgs, 1, common.MaxHostPerExtentMsgsLimitPerSecond)
verifierMap[UkeyConnMsgs] = dconfig.GenerateIntMaxMinVerifier(UkeyConnMsgs, 1, common.MaxHostPerConnMsgsLimitPerSecond)
verifierMap[UkeyTestShortExts] = dconfig.GenerateStringRegexpVerifier(UkeyTestShortExts, common.OverrideValueByPrefixJoinedValidatorRegexp)
h.dConfigClient.AddVerifiers(verifierMap)
}

// LoadUconfig load the dynamic config values for key
func (h *InputHost) LoadUconfig() {
valueUcfg, ok := h.dConfigClient.GetOrDefault(UkeyHostOverall, common.HostOverallConnLimit).(int)
if ok {
h.SetHostConnLimit(int32(valueUcfg))
log.WithField(UkeyHostOverall, valueUcfg).
Info("Update the uconfig value")
} else {
log.Errorf("Cannot get %s from uconfig, Using right format", UkeyHostOverall)
}

str, ok := h.dConfigClient.GetOrDefault(UkeyTestShortExts, ``).(string)
if ok {
h.SetTestShortExtentsByPath(str)
log.WithField(UkeyTestShortExts, str).Info(`Updated`)
} else {
log.WithField(UkeyTestShortExts, str).Warn(`Failed update, type assertion failed`)
}
}

// uconfigManage do the work for uconfig
func (h *InputHost) dynamicConfigManage() {
h.registerDConfig()
h.dConfigClient.Refresh()
h.LoadUconfig()
h.registerInt()
h.dConfigClient.StartBackGroundRefresh()
}
8 changes: 8 additions & 0 deletions services/inputhost/inputhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ func (h *InputHost) GetHostConnLimitPerSecond() int {

// SetHostConnLimitPerSecond sets the rate at which this host can accept conns
func (h *InputHost) SetHostConnLimitPerSecond(connLimit int32) {
h.logger.WithField(`val`, connLimit).Info(`SetHostConnLimitPerSecond`)
atomic.StoreInt32(&h.hostConnLimitPerSecond, connLimit)
h.SetTokenBucketValue(int32(connLimit))
}
Expand All @@ -903,6 +904,7 @@ func (h *InputHost) GetExtMsgsLimitPerSecond() int {

// SetExtMsgsLimitPerSecond sets the rate limit for per extent per second
func (h *InputHost) SetExtMsgsLimitPerSecond(connLimit int32) {
h.logger.WithField(`val`, connLimit).Info(`SetExtMsgsLimitPerSecond`)
atomic.StoreInt32(&h.extMsgsLimitPerSecond, connLimit)
h.updateExtTokenBucket(int32(connLimit))
}
Expand All @@ -914,6 +916,7 @@ func (h *InputHost) GetConnMsgsLimitPerSecond() int {

// SetConnMsgsLimitPerSecond sets the rate limit for per connection per second
func (h *InputHost) SetConnMsgsLimitPerSecond(connLimit int32) {
h.logger.WithField(`val`, connLimit).Info(`SetConnMsgsLimitPerSecond`)
atomic.StoreInt32(&h.connMsgsLimitPerSecond, connLimit)
h.updateConnTokenBucket(int32(connLimit))
}
Expand All @@ -925,12 +928,14 @@ func (h *InputHost) GetTokenBucketValue() common.TokenBucket {

// SetTokenBucketValue sets token bucket for hostConnLimitPerSecond
func (h *InputHost) SetTokenBucketValue(connLimit int32) {
h.logger.WithField(`val`, connLimit).Info(`SetTokenBucketValue`)
tokenBucket := common.NewTokenBucket(int(connLimit), common.NewRealTimeSource())
h.tokenBucketValue.Store(tokenBucket)
}

// SetHostConnLimit sets the conn limit for this host
func (h *InputHost) SetHostConnLimit(connLimit int32) {
h.logger.WithField(`val`, connLimit).Info(`SetHostConnLimit`)
atomic.StoreInt32(&h.hostConnLimit, connLimit)
}

Expand All @@ -941,6 +946,7 @@ func (h *InputHost) GetHostConnLimitOverall() int {

// SetMaxConnPerDest sets the max connection limit per destination
func (h *InputHost) SetMaxConnPerDest(connLimit int32) {
h.logger.WithField(`val`, connLimit).Info(`SetMaxConnPerDest`)
atomic.StoreInt32(&h.maxConnLimit, connLimit)
}

Expand All @@ -961,11 +967,13 @@ func (h *InputHost) GetNodeStatus() controller.NodeStatus {

// SetNodeStatus sets the status of this host
func (h *InputHost) SetNodeStatus(status controller.NodeStatus) {
h.logger.WithField(`val`, status).Info(`SetNodeStatus`)
h.nodeStatus.Store(status)
}

// SetTestShortExtentsByPath sets path override that enables testing short extents
func (h *InputHost) SetTestShortExtentsByPath(override string) {
h.logger.WithField(`val`, override).Info(`SetTestShortExtentsByPath`)
h.testShortExtentsByPath = override
}

Expand Down

0 comments on commit 417e166

Please sign in to comment.