From 8bf472bb75aa7cbb747aa4876517f97db948babe Mon Sep 17 00:00:00 2001 From: Samir Patel <48686912+54mir@users.noreply.github.com> Date: Mon, 14 Jun 2021 10:48:52 -0500 Subject: [PATCH] add duty cycle config flag --- api.go | 10 ++++++++-- ctl/server.go | 3 +++ server/config.go | 6 ++++++ server/server.go | 2 +- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/api.go b/api.go index 0ff042165..c9766e27c 100644 --- a/api.go +++ b/api.go @@ -1061,15 +1061,21 @@ func (api *API) calculateUsage() { } // Periodically calculates disk usage -func (api *API) RefreshUsageCache() { +func (api *API) RefreshUsageCache(dutyCycle float64) { trigger := make(chan bool) defer close(trigger) + + if dutyCycle <= 0 { + dutyCycle = 20 + } + multiplier := int(math.Ceil(100/dutyCycle)) - 1 + api.usageCache = &usageCache{ data: make(map[string]NodeUsage), refreshInterval: time.Hour, resetTrigger: trigger, lastCalcDuration: 0, - waitMultiplier: time.Duration(5), + waitMultiplier: time.Duration(multiplier), } for { start := time.Now() diff --git a/ctl/server.go b/ctl/server.go index 6c885d213..98472ea95 100644 --- a/ctl/server.go +++ b/ctl/server.go @@ -110,4 +110,7 @@ func BuildServerFlags(cmd *cobra.Command, srv *server.Command) { flags.DurationVar((*time.Duration)(&srv.Config.Postgres.WriteTimeout), "postgres.write-timeout", time.Duration(srv.Config.Postgres.WriteTimeout), "Timeout for writes on a postgres connection. (set 0 to disable)") flags.Uint32Var(&srv.Config.Postgres.MaxStartupSize, "postgres.max-startup-size", srv.Config.Postgres.MaxStartupSize, "Maximum acceptable size of a postgres startup packet, in bytes. (set 0 to disable)") flags.Uint16Var(&srv.Config.Postgres.ConnectionLimit, "postgres.connection-limit", srv.Config.Postgres.ConnectionLimit, "Maximum number of simultaneous postgres connections to allow. (set 0 to disable)") + + // Disk and Memory usage cache for ui/usage endpoint + flags.Float64Var(&srv.Config.UsageDutyCycle, "usage-duty-cycle", srv.Config.UsageDutyCycle, "Sets the percentage of time that is spent recalculating the disk and memory usage cache. 100.0 for always-running, must be > 0.") } diff --git a/server/config.go b/server/config.go index f59040271..e744b86e2 100644 --- a/server/config.go +++ b/server/config.go @@ -224,6 +224,9 @@ type Config struct { // LookupDBDSN is an external database to connect to for `ExternalLookup` queries. LookupDBDSN string `toml:"lookup-db-dsn"` + + // The percentage of time spent recalculating the disk and memory usage cache. + UsageDutyCycle float64 `toml:"usage-duty-cycle"` } // MustValidate checks that all ports in a Config are unique and not zero. @@ -359,6 +362,9 @@ func NewConfig() *Config { c.Etcd.PeerCertFile = "" c.Etcd.PeerKeyFile = "" + // Disk and Memory Usage + c.UsageDutyCycle = 20.0 + return c } diff --git a/server/server.go b/server/server.go index febdb13ee..11ff5bf1b 100644 --- a/server/server.go +++ b/server/server.go @@ -277,7 +277,7 @@ func (m *Command) Start() (err error) { } } - go m.API.RefreshUsageCache() + go m.API.RefreshUsageCache(m.Config.UsageDutyCycle) _ = testhook.Opened(pilosa.NewAuditor(), m, nil) close(m.Started)