From d2be9d38dade36f35791962aa948382abb72b030 Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Mon, 24 Aug 2020 23:33:10 +0200
Subject: [PATCH] Ensure that Cache options are reloaded when `consul reload`
is performed.
This will apply cache throttling parameters are properly applied:
* cache.EntryFetchMaxBurst
* cache.EntryFetchRate
When values are updated, a log is displayed in info.
---
agent/agent.go | 6 ++++++
agent/cache/cache.go | 33 ++++++++++++++++++++++++++++++---
2 files changed, 36 insertions(+), 3 deletions(-)
diff --git a/agent/agent.go b/agent/agent.go
index bc676b0bfa..ab240e0af3 100644
--- a/agent/agent.go
+++ b/agent/agent.go
@@ -3748,6 +3748,12 @@ func (a *Agent) reloadConfigInternal(newCfg *config.RuntimeConfig) error {
return err
}
+ if a.cache.ReloadOptions(newCfg.Cache) {
+ a.logger.Info("Cache options have been updated")
+ } else {
+ a.logger.Debug("Cache options have not been modified")
+ }
+
// Update filtered metrics
metrics.UpdateFilter(newCfg.Telemetry.AllowedPrefixes,
newCfg.Telemetry.BlockedPrefixes)
diff --git a/agent/cache/cache.go b/agent/cache/cache.go
index 44aeb3a046..ed32527e4d 100644
--- a/agent/cache/cache.go
+++ b/agent/cache/cache.go
@@ -144,16 +144,21 @@ type Options struct {
EntryFetchRate rate.Limit
}
-// New creates a new cache with the given RPC client and reasonable defaults.
-// Further settings can be tweaked on the returned value.
-func New(options Options) *Cache {
+// applyDefaultValuesOnOptions set default values on options and returned updated value
+func applyDefaultValuesOnOptions(options Options) Options {
if options.EntryFetchRate == 0.0 {
options.EntryFetchRate = DefaultEntryFetchRate
}
if options.EntryFetchMaxBurst == 0 {
options.EntryFetchMaxBurst = DefaultEntryFetchMaxBurst
}
+ return options
+}
+// New creates a new cache with the given RPC client and reasonable defaults.
+// Further settings can be tweaked on the returned value.
+func New(options Options) *Cache {
+ options = applyDefaultValuesOnOptions(options)
// Initialize the heap. The buffer of 1 is really important because
// its possible for the expiry loop to trigger the heap to update
// itself and it'd block forever otherwise.
@@ -234,6 +239,28 @@ func (c *Cache) RegisterType(n string, typ Type) {
c.types[n] = typeEntry{Name: n, Type: typ, Opts: &opts}
}
+// ReloadOptions updates the cache with the new options
+// return true if Cache is updated, false if already up to date
+func (c *Cache) ReloadOptions(options Options) bool {
+ options = applyDefaultValuesOnOptions(options)
+ if c.options.EntryFetchRate != options.EntryFetchRate || c.options.EntryFetchMaxBurst != options.EntryFetchMaxBurst {
+ c.entriesLock.RLock()
+ defer c.entriesLock.RUnlock()
+ for _, entry := range c.entries {
+ if c.options.EntryFetchRate != options.EntryFetchRate {
+ entry.FetchRateLimiter.SetLimit(options.EntryFetchRate)
+ }
+ if c.options.EntryFetchMaxBurst != options.EntryFetchMaxBurst {
+ entry.FetchRateLimiter.SetBurst(options.EntryFetchMaxBurst)
+ }
+ }
+ c.options.EntryFetchRate = options.EntryFetchRate
+ c.options.EntryFetchMaxBurst = options.EntryFetchMaxBurst
+ return true
+ }
+ return false
+}
+
// Get loads the data for the given type and request. If data satisfying the
// minimum index is present in the cache, it is returned immediately. Otherwise,
// this will block until the data is available or the request timeout is