From ed4351aab03875feafb86c8702521bdbd551b445 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Thu, 17 Nov 2022 19:44:58 +0300 Subject: [PATCH] [#2074] write-cache: Do not flush same object twice Signed-off-by: Pavel Karpy --- CHANGELOG.md | 1 + pkg/local_object_storage/writecache/flush.go | 23 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efaa1f726..2ab68472a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ Changelog for NeoFS Node - Removing all trees by container ID if tree ID is empty in `pilorama.Forest.TreeDrop` (#1940) - Concurrent mode changes in the metabase and blobstor (#2057) - Panic in IR when performing HEAD requests (#2069) +- Write-cache flush duplication (#2074) ### Removed ### Updated diff --git a/pkg/local_object_storage/writecache/flush.go b/pkg/local_object_storage/writecache/flush.go index 68d90e799..77167f8a9 100644 --- a/pkg/local_object_storage/writecache/flush.go +++ b/pkg/local_object_storage/writecache/flush.go @@ -1,6 +1,7 @@ package writecache import ( + "bytes" "errors" "time" @@ -57,7 +58,7 @@ func (c *cache) runFlushLoop() { } func (c *cache) flushDB() { - lastKey := []byte{} + var lastKey []byte var m []objectInfo for { select { @@ -79,7 +80,25 @@ func (c *cache) flushDB() { _ = c.db.View(func(tx *bbolt.Tx) error { b := tx.Bucket(defaultBucket) cs := b.Cursor() - for k, v := cs.Seek(lastKey); k != nil && len(m) < flushBatchSize; k, v = cs.Next() { + + var k, v []byte + + if len(lastKey) == 0 { + k, v = cs.First() + } else { + k, v = cs.Seek(lastKey) + if bytes.Equal(k, lastKey) { + k, v = cs.Next() + } + } + + for ; k != nil && len(m) < flushBatchSize; k, v = cs.Next() { + if len(lastKey) == len(k) { + copy(lastKey, k) + } else { + lastKey = slice.Copy(k) + } + if _, ok := c.flushed.Peek(string(k)); ok { continue }