From 2efe9cc1beec5df36727a1cd5562a86b77f79eb8 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Fri, 11 Aug 2023 12:51:41 +0300 Subject: [PATCH] [#585] writecache: Fix DB counter Signed-off-by: Dmitrii Stepanov --- .../writecache/writecachebbolt/delete.go | 12 ++++++++++-- .../writecache/writecachebbolt/put.go | 13 ++++++++++++- .../writecache/writecachebbolt/storage.go | 11 +++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/pkg/local_object_storage/writecache/writecachebbolt/delete.go b/pkg/local_object_storage/writecache/writecachebbolt/delete.go index 105526c27..e4d9c219f 100644 --- a/pkg/local_object_storage/writecache/writecachebbolt/delete.go +++ b/pkg/local_object_storage/writecache/writecachebbolt/delete.go @@ -2,6 +2,7 @@ package writecachebbolt import ( "context" + "math" "time" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common" @@ -49,9 +50,12 @@ func (c *cache) Delete(ctx context.Context, addr oid.Address) error { if dataSize > 0 { storageType = writecache.StorageTypeDB + var recordDeleted bool err := c.db.Update(func(tx *bbolt.Tx) error { b := tx.Bucket(defaultBucket) - err := b.Delete([]byte(saddr)) + key := []byte(saddr) + recordDeleted = b.Get(key) != nil + err := b.Delete(key) return err }) if err != nil { @@ -62,6 +66,10 @@ func (c *cache) Delete(ctx context.Context, addr oid.Address) error { storagelog.StorageTypeField(wcStorageType), storagelog.OpField("db DELETE"), ) + if recordDeleted { + c.objCounters.cDB.Add(math.MaxUint64) + c.estimateCacheSize() + } deleted = true return nil } @@ -75,7 +83,7 @@ func (c *cache) Delete(ctx context.Context, addr oid.Address) error { storagelog.OpField("fstree DELETE"), ) deleted = true + c.estimateCacheSize() } - return metaerr.Wrap(err) } diff --git a/pkg/local_object_storage/writecache/writecachebbolt/put.go b/pkg/local_object_storage/writecache/writecachebbolt/put.go index 645e0bf8c..1d9957219 100644 --- a/pkg/local_object_storage/writecache/writecachebbolt/put.go +++ b/pkg/local_object_storage/writecache/writecachebbolt/put.go @@ -85,9 +85,15 @@ func (c *cache) putSmall(obj objectInfo) error { return ErrOutOfSpace } + var newRecord bool err := c.db.Batch(func(tx *bbolt.Tx) error { b := tx.Bucket(defaultBucket) - return b.Put([]byte(obj.addr), obj.data) + key := []byte(obj.addr) + newRecord = b.Get(key) == nil + if newRecord { + return b.Put(key, obj.data) + } + return nil }) if err == nil { storagelog.Write(c.log, @@ -95,6 +101,10 @@ func (c *cache) putSmall(obj objectInfo) error { storagelog.StorageTypeField(wcStorageType), storagelog.OpField("db PUT"), ) + if newRecord { + c.objCounters.cDB.Add(1) + c.estimateCacheSize() + } } return err } @@ -121,6 +131,7 @@ func (c *cache) putBig(ctx context.Context, addr string, prm common.PutPrm) erro storagelog.StorageTypeField(wcStorageType), storagelog.OpField("fstree PUT"), ) + c.estimateCacheSize() return nil } diff --git a/pkg/local_object_storage/writecache/writecachebbolt/storage.go b/pkg/local_object_storage/writecache/writecachebbolt/storage.go index d79eb2963..bbd66d57c 100644 --- a/pkg/local_object_storage/writecache/writecachebbolt/storage.go +++ b/pkg/local_object_storage/writecache/writecachebbolt/storage.go @@ -3,6 +3,7 @@ package writecachebbolt import ( "context" "fmt" + "math" "os" "git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs" @@ -68,9 +69,12 @@ func (c *cache) openStore(readOnly bool) error { } func (c *cache) deleteFromDB(key string) { + var recordDeleted bool err := c.db.Batch(func(tx *bbolt.Tx) error { b := tx.Bucket(defaultBucket) - return b.Delete([]byte(key)) + key := []byte(key) + recordDeleted = !recordDeleted && b.Get(key) != nil + return b.Delete(key) }) if err == nil { @@ -80,7 +84,10 @@ func (c *cache) deleteFromDB(key string) { storagelog.StorageTypeField(wcStorageType), storagelog.OpField("db DELETE"), ) - c.estimateCacheSize() + if recordDeleted { + c.objCounters.cDB.Add(math.MaxUint64) + c.estimateCacheSize() + } } else { c.log.Error(logs.WritecacheCantRemoveObjectsFromTheDatabase, zap.Error(err)) }