From 9de91542fe0f12e245c11e97dceda5f04673bb74 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 2a85313f1..dfc4f884f 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 9717fd8a3..710fa67f2 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 }) @@ -89,7 +93,7 @@ func (b *Blobovnicza) Delete(ctx context.Context, prm DeletePrm) (DeleteRes, err zap.String("binary size", stringifyByteSize(dataSize)), zap.String("range", stringifyBounds(sizeLowerBound, sizeUpperBound)), ) - 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 c701bb2d8..ddfd9e3a1 100644 --- a/pkg/local_object_storage/blobovnicza/put.go +++ b/pkg/local_object_storage/blobovnicza/put.go @@ -57,8 +57,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) @@ -75,11 +76,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 cd1f69725..1dff75aed 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 d2f576fd3..01093b8d7 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) } }