From 486244ab58cd2fdd5658d9deab2ed2eee66ea269 Mon Sep 17 00:00:00 2001 From: Samir Patel <48686912+54mir@users.noreply.github.com> Date: Thu, 10 Jun 2021 10:48:44 -0500 Subject: [PATCH] reset cache if outdated on call --- api.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/api.go b/api.go index 7bc31142e..59cde8d1f 100644 --- a/api.go +++ b/api.go @@ -957,7 +957,17 @@ func (api *API) Usage(ctx context.Context, remote bool) (map[string]NodeUsage, e api.usageCache.muAssign.Lock() lastUpdated := api.usageCache.lastUpdated + cacheN := len(api.usageCache.data[api.server.nodeID].Disk.IndexUsage) api.usageCache.muAssign.Unlock() + holderN := len(api.holder.Indexes()) + + // reset cache if server was started with no data, and data has subsequently been added + if cacheN == 0 && holderN > 0 { + err := api.ResetUsageCache() + if err != nil { + api.server.logger.Infof("data detected but could not recalculate cache: %s", err) + } + } var t time.Time if lastUpdated == t { @@ -968,6 +978,15 @@ func (api *API) Usage(ctx context.Context, remote bool) (map[string]NodeUsage, e api.requestUsageOfNodes() } + // triggers recalculation of cache in background, ahead of schedule, if number of indexes in + // cache differs from number of indexes in holder + if cacheN > 0 && cacheN != holderN { + err := api.ResetUsageCache() + if err != nil { + api.server.logger.Infof("resetting cache in background: %s", err) + } + } + return api.usageCache.data, nil }