From bd77f29fc4a32e5c362dce956104a8861ed32595 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Thu, 5 Nov 2020 07:34:08 -0800 Subject: [PATCH] Don't replace caches that are receiving updates (#10834) Keep caches while they are receiving updates. Move update code to separate function. --- cmd/metacache-bucket.go | 19 +------------------ cmd/metacache.go | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/cmd/metacache-bucket.go b/cmd/metacache-bucket.go index b3453f61d..a69b79e66 100644 --- a/cmd/metacache-bucket.go +++ b/cmd/metacache-bucket.go @@ -372,24 +372,7 @@ func (b *bucketMetacache) updateCacheEntry(update metacache) (metacache, error) logger.Info("updateCacheEntry: bucket %s list id %v not found", b.bucket, update.id) return update, errFileNotFound } - - existing.lastUpdate = UTCNow() - - if existing.status == scanStateStarted && update.status == scanStateSuccess { - existing.ended = UTCNow() - existing.endedCycle = update.endedCycle - } - - if existing.status == scanStateStarted && update.status != scanStateStarted { - existing.status = update.status - } - - if existing.error == "" && update.error != "" { - existing.error = update.error - existing.status = scanStateError - existing.ended = UTCNow() - } - existing.fileNotFound = existing.fileNotFound || update.fileNotFound + existing.update(update) b.caches[update.id] = existing b.updated = true return existing, nil diff --git a/cmd/metacache.go b/cmd/metacache.go index d63d85012..579681479 100644 --- a/cmd/metacache.go +++ b/cmd/metacache.go @@ -99,8 +99,11 @@ func (m *metacache) canBeReplacedBy(other *metacache) bool { if other.status == scanStateNone || other.status == scanStateError { return false } + if m.status == scanStateStarted && time.Since(m.lastUpdate) < metacacheMaxRunningAge { + return false + } // Keep it around a bit longer. - if time.Since(m.lastHandout) < time.Hour { + if time.Since(m.lastHandout) < time.Hour || time.Since(m.lastUpdate) < metacacheMaxRunningAge { return false } @@ -138,6 +141,28 @@ func baseDirFromPrefix(prefix string) string { return b } +// update cache with new status. +// The updates are conditional so multiple callers can update with different states. +func (m *metacache) update(update metacache) { + m.lastUpdate = UTCNow() + + if m.status == scanStateStarted && update.status == scanStateSuccess { + m.ended = UTCNow() + m.endedCycle = update.endedCycle + } + + if m.status == scanStateStarted && update.status != scanStateStarted { + m.status = update.status + } + + if m.error == "" && update.error != "" { + m.error = update.error + m.status = scanStateError + m.ended = UTCNow() + } + m.fileNotFound = m.fileNotFound || update.fileNotFound +} + // delete all cache data on disks. func (m *metacache) delete(ctx context.Context) { if m.bucket == "" || m.id == "" {