From ad0697adc405bc01eb99127729a5b8e2be3f0e43 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Thu, 12 Oct 2023 21:35:08 +0300 Subject: [PATCH] [#661] blobovnicza: Compute size with record size To get more accurate size of blobovnicza use record size (lenght of key + lenght of data). Signed-off-by: Dmitrii Stepanov --- pkg/local_object_storage/blobovnicza/control.go | 9 +++++---- pkg/local_object_storage/blobovnicza/delete.go | 10 +++++++--- pkg/local_object_storage/blobovnicza/put.go | 7 ++++--- pkg/local_object_storage/blobovnicza/sizes.go | 5 ++--- pkg/local_object_storage/blobovnicza/sizes_test.go | 2 +- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/pkg/local_object_storage/blobovnicza/control.go b/pkg/local_object_storage/blobovnicza/control.go index 2a85313f..dfc4f884 100644 --- a/pkg/local_object_storage/blobovnicza/control.go +++ b/pkg/local_object_storage/blobovnicza/control.go @@ -116,10 +116,11 @@ func (b *Blobovnicza) initializeCounters() error { } return b.iterateAllDataBuckets(tx, func(lower, upper uint64, b *bbolt.Bucket) (bool, error) { - keysN := uint64(b.Stats().KeyN) - size += keysN * upper - items += keysN - return false, nil + return false, b.ForEach(func(k, v []byte) error { + size += uint64(len(k) + len(v)) + items++ + return nil + }) }) }) if err != nil { diff --git a/pkg/local_object_storage/blobovnicza/delete.go b/pkg/local_object_storage/blobovnicza/delete.go index ff3a7a29..ab8575ad 100644 --- a/pkg/local_object_storage/blobovnicza/delete.go +++ b/pkg/local_object_storage/blobovnicza/delete.go @@ -49,6 +49,7 @@ func (b *Blobovnicza) Delete(ctx context.Context, prm DeletePrm) (DeleteRes, err var sizeUpperBound uint64 var sizeLowerBound uint64 var dataSize uint64 + var recordSize uint64 err := b.boltDB.Update(func(tx *bbolt.Tx) error { err := b.iterateAllDataBuckets(tx, func(lower, upper uint64, buck *bbolt.Bucket) (bool, error) { @@ -60,6 +61,7 @@ func (b *Blobovnicza) Delete(ctx context.Context, prm DeletePrm) (DeleteRes, err dataSize = uint64(len(objData)) sizeLowerBound = lower sizeUpperBound = upper + recordSize = dataSize + uint64(len(addrKey)) found = true return true, buck.Delete(addrKey) }) @@ -71,8 +73,10 @@ func (b *Blobovnicza) Delete(ctx context.Context, prm DeletePrm) (DeleteRes, err if count > 0 { count-- } - if size >= sizeUpperBound { - size -= sizeUpperBound + if size >= recordSize { + size -= recordSize + } else { + size = 0 } return count, size }) @@ -90,7 +94,7 @@ func (b *Blobovnicza) Delete(ctx context.Context, prm DeletePrm) (DeleteRes, err zap.String("range", stringifyBounds(sizeLowerBound, sizeUpperBound)), zap.String("trace_id", tracingPkg.GetTraceID(ctx)), ) - b.itemDeleted(sizeUpperBound) + b.itemDeleted(recordSize) } return DeleteRes{}, err diff --git a/pkg/local_object_storage/blobovnicza/put.go b/pkg/local_object_storage/blobovnicza/put.go index 608739be..d15b6e2b 100644 --- a/pkg/local_object_storage/blobovnicza/put.go +++ b/pkg/local_object_storage/blobovnicza/put.go @@ -56,8 +56,9 @@ func (b *Blobovnicza) Put(ctx context.Context, prm PutPrm) (PutRes, error) { defer span.End() sz := uint64(len(prm.objData)) - bucketName, upperBound := bucketForSize(sz) + bucketName := bucketForSize(sz) key := addressKey(prm.addr) + recordSize := sz + uint64(len(key)) err := b.boltDB.Batch(func(tx *bbolt.Tx) error { buck := tx.Bucket(bucketName) @@ -74,11 +75,11 @@ func (b *Blobovnicza) Put(ctx context.Context, prm PutPrm) (PutRes, error) { } return updateMeta(tx, func(count, size uint64) (uint64, uint64) { - return count + 1, size + upperBound + return count + 1, size + recordSize }) }) if err == nil { - b.itemAdded(upperBound) + b.itemAdded(recordSize) } return PutRes{}, err diff --git a/pkg/local_object_storage/blobovnicza/sizes.go b/pkg/local_object_storage/blobovnicza/sizes.go index cd1f6972..1dff75ae 100644 --- a/pkg/local_object_storage/blobovnicza/sizes.go +++ b/pkg/local_object_storage/blobovnicza/sizes.go @@ -29,9 +29,8 @@ func bucketKeyFromBounds(upperBound uint64) []byte { return buf[:ln] } -func bucketForSize(sz uint64) ([]byte, uint64) { - upperBound := upperPowerOfTwo(sz) - return bucketKeyFromBounds(upperBound), upperBound +func bucketForSize(sz uint64) []byte { + return bucketKeyFromBounds(upperPowerOfTwo(sz)) } func upperPowerOfTwo(v uint64) uint64 { diff --git a/pkg/local_object_storage/blobovnicza/sizes_test.go b/pkg/local_object_storage/blobovnicza/sizes_test.go index d2f576fd..01093b8d 100644 --- a/pkg/local_object_storage/blobovnicza/sizes_test.go +++ b/pkg/local_object_storage/blobovnicza/sizes_test.go @@ -34,7 +34,7 @@ func TestSizes(t *testing.T) { upperBound: 4 * firstBucketBound, }, } { - key, _ := bucketForSize(item.sz) + key := bucketForSize(item.sz) require.Equal(t, bucketKeyFromBounds(item.upperBound), key) } }