reset cache if outdated on call

This commit is contained in:
Samir Patel 2021-06-10 10:48:44 -05:00
parent 4baffff847
commit 486244ab58

19
api.go
View file

@ -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
}