Skip to content
Open
Changes from all 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
17 changes: 17 additions & 0 deletions pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type PebbleDB struct {
}

var _ DB = (*PebbleDB)(nil)
const flushThreshold = 3_500_000_000 // ~3.5 GB

func NewPebbleDB(name string, dir string) (*PebbleDB, error) {
opts := &pebble.Options{}
Expand Down Expand Up @@ -250,6 +251,14 @@ func (b *pebbleDBBatch) Set(key, value []byte) error {
return errBatchClosed
}

// Prevent Pebble batch from exceeding 4 GB hard limit
if b.batch.Len() > flushThreshold {
if err := b.batch.Commit(pebble.Sync); err != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Batch Flush Sync Inconsistency

The automatic batch flush uses pebble.Sync, which is inconsistent with the pebble.NoSync used by the Write() method. This introduces unexpected synchronous writes during batch building, potentially causing performance slowdowns and violating user expectations.

Fix in Cursor Fix in Web

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think this is necessary due to the location the commit is called, as it "interrupts" a normal batch operation, this should not simply continue until the batch is committed.

return err
}
b.batch.Reset()
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Batch Size Check Timing Issue

The batch size check in pebbleDBBatch.Set and Delete happens before adding the operation. This means a large operation can push the batch over Pebble's 4GB hard limit, even if the current size is below the flushThreshold.

Additional Locations (1)

Fix in Cursor Fix in Web

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion this would be unlikely to happen. Single operations normally are much smaller, just batching them can lead to the issue.

return b.batch.Set(key, value, nil)
}

Expand All @@ -262,6 +271,14 @@ func (b *pebbleDBBatch) Delete(key []byte) error {
return errBatchClosed
}

// Prevent Pebble batch from exceeding 4 GB hard limit
if b.batch.Len() > flushThreshold {
if err := b.batch.Commit(pebble.Sync); err != nil {
return err
}
b.batch.Reset()
}

return b.batch.Delete(key, nil)
}

Expand Down
Loading