Skip to content

Commit

Permalink
check for unknown KV, fix upper bound bug
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Oct 17, 2024
1 parent f5dc02c commit f6d9cef
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
18 changes: 16 additions & 2 deletions cmd/juno/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@ func dbSize(cmd *cobra.Command, args []string) error {
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 @@ -220,6 +221,19 @@ func dbSize(cmd *cobra.Command, args []string) error {
}
}

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

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

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L228

Added line #L228 was 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 234 in cmd/juno/dbcmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/dbcmd.go#L232-L234

Added lines #L232 - L234 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 @@ const (
// 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
)

var (
Expand Down Expand Up @@ -128,17 +129,21 @@ func (i *Item) add(size utils.DataSize) {
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 @@ func CalculatePrefixSize(ctx context.Context, pDB *DB, prefix []byte) (*Item, er

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

0 comments on commit f6d9cef

Please sign in to comment.