Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix upper bound issue in CalculatePrefixSize #2223

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions cmd/juno/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@
items [][]string
)

for _, b := range db.BucketValues() {
buckets := db.BucketValues()
for _, b := range buckets {
fmt.Fprintf(cmd.OutOrStdout(), "Calculating size of %s, remaining buckets: %d\n", b, len(db.BucketValues())-int(b)-1)
bucketItem, err := pebble.CalculatePrefixSize(cmd.Context(), pebbleDB.(*pebble.DB), []byte{byte(b)})
bucketItem, err := pebble.CalculatePrefixSize(cmd.Context(), pebbleDB.(*pebble.DB), []byte{byte(b)}, true)
if err != nil {
return err
}
Expand All @@ -228,6 +229,20 @@
}
}

// check if there is any data left in the db
lastBucket := buckets[len(buckets)-1]
fmt.Fprintln(cmd.OutOrStdout(), "Calculating remaining data in the db")
lastBucketItem, err := pebble.CalculatePrefixSize(cmd.Context(), pebbleDB.(*pebble.DB), []byte{byte(lastBucket + 1)}, false)
if err != nil {
return err
}

Check warning on line 238 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L237-L238

Added lines #L237 - L238 were not covered by tests

if lastBucketItem.Count > 0 {
items = append(items, []string{"Unknown", lastBucketItem.Size.String(), fmt.Sprintf("%d", lastBucketItem.Count)})
totalSize += lastBucketItem.Size
totalCount += lastBucketItem.Count
}

Check warning on line 244 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L241-L244

Added lines #L241 - L244 were not covered by tests

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Bucket", "Size", "Count"})
table.AppendBulk(items)
Expand Down
27 changes: 24 additions & 3 deletions db/pebble/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// minCache is the minimum amount of memory in megabytes to allocate to pebble read and write caching.
// This is also pebble's default value.
minCacheSizeMB = 8
byteLimit = 0xff
weiihann marked this conversation as resolved.
Show resolved Hide resolved
)

var (
Expand Down Expand Up @@ -128,17 +129,21 @@
i.Size += size
}

func CalculatePrefixSize(ctx context.Context, pDB *DB, prefix []byte) (*Item, error) {
func CalculatePrefixSize(ctx context.Context, pDB *DB, prefix []byte, withUpperBound bool) (*Item, error) {
var (
err error
v []byte

item = &Item{}
)

const upperBoundofPrefix = 0xff
pebbleDB := pDB.Impl().(*pebble.DB)
it, err := pebbleDB.NewIter(&pebble.IterOptions{LowerBound: prefix, UpperBound: append(prefix, upperBoundofPrefix)})
iterOpt := &pebble.IterOptions{LowerBound: prefix}
if withUpperBound {
iterOpt.UpperBound = upperBound(prefix)
}

it, err := pebbleDB.NewIter(iterOpt)
if err != nil {
// No need to call utils.RunAndWrapOnError() since iterator couldn't be created
return nil, err
Expand All @@ -158,3 +163,19 @@

return item, utils.RunAndWrapOnError(it.Close, err)
}

func upperBound(prefix []byte) []byte {
var ub []byte

for i := len(prefix) - 1; i >= 0; i-- {
if prefix[i] == byteLimit {
continue

Check warning on line 172 in db/pebble/db.go

View check run for this annotation

Codecov / codecov/patch

db/pebble/db.go#L172

Added line #L172 was not covered by tests
}
ub = make([]byte, i+1)
copy(ub, prefix)
ub[i]++
return ub
}

return nil

Check warning on line 180 in db/pebble/db.go

View check run for this annotation

Codecov / codecov/patch

db/pebble/db.go#L180

Added line #L180 was not covered by tests
}
8 changes: 4 additions & 4 deletions db/pebble/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func TestCalculatePrefixSize(t *testing.T) {
t.Run("empty db", func(t *testing.T) {
testDB := pebble.NewMemTest(t).(*pebble.DB)

s, err := pebble.CalculatePrefixSize(context.Background(), testDB, []byte("0"))
s, err := pebble.CalculatePrefixSize(context.Background(), testDB, []byte("0"), true)
require.NoError(t, err)
assert.Zero(t, s.Count)
assert.Zero(t, s.Size)
Expand All @@ -435,7 +435,7 @@ func TestCalculatePrefixSize(t *testing.T) {
require.NoError(t, testDB.Update(func(txn db.Transaction) error {
return txn.Set(append([]byte("0"), []byte("randomKey")...), []byte("someValue"))
}))
s, err := pebble.CalculatePrefixSize(context.Background(), testDB.(*pebble.DB), []byte("1"))
s, err := pebble.CalculatePrefixSize(context.Background(), testDB.(*pebble.DB), []byte("1"), true)
require.NoError(t, err)
assert.Zero(t, s.Count)
assert.Zero(t, s.Size)
Expand All @@ -455,7 +455,7 @@ func TestCalculatePrefixSize(t *testing.T) {
return txn.Set(k3, v3)
}))

s, err := pebble.CalculatePrefixSize(context.Background(), testDB.(*pebble.DB), p)
s, err := pebble.CalculatePrefixSize(context.Background(), testDB.(*pebble.DB), p, true)
require.NoError(t, err)
assert.Equal(t, uint(3), s.Count)
assert.Equal(t, utils.DataSize(expectedSize), s.Size)
Expand All @@ -464,7 +464,7 @@ func TestCalculatePrefixSize(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()

s, err := pebble.CalculatePrefixSize(ctx, testDB.(*pebble.DB), p)
s, err := pebble.CalculatePrefixSize(ctx, testDB.(*pebble.DB), p, true)
assert.EqualError(t, err, context.Canceled.Error())
assert.Zero(t, s.Count)
assert.Zero(t, s.Size)
Expand Down
Loading