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 pebbledb memory corruption #3020

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 8 additions & 16 deletions database/pebble/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,18 @@ func (b *batch) Write() error {
return database.ErrClosed
}

if !b.written {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change shouldn't actually change any behavior... it should just make it more clear.

// This batch has not been written to the database yet.
if err := updateError(b.batch.Commit(pebble.Sync)); err != nil {
if b.written {
// pebble doesn't support writing a batch twice so we have to clone the
// batch before writing it.
newBatch := b.db.pebbleDB.NewBatch()
if err := newBatch.Apply(b.batch, nil); err != nil {
return err
}
b.written = true
return nil
b.batch = newBatch
}

// pebble doesn't support writing a batch twice so we have to clone
// [b] and commit the clone.
batchClone := b.db.pebbleDB.NewBatch()

// Copy the batch.
if err := batchClone.Apply(b.batch, nil); err != nil {
return err
}

// Commit the new batch.
return updateError(batchClone.Commit(pebble.Sync))
b.written = true
return updateError(b.batch.Commit(pebble.Sync))
}

func (b *batch) Reset() {
Expand Down
2 changes: 1 addition & 1 deletion database/pebble/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestBatch(t *testing.T) {
require := require.New(t)
dirName := t.TempDir()

db, err := New(dirName, DefaultConfigBytes, logging.NoLog{}, "", prometheus.NewRegistry())
db, err := New(dirName, nil, logging.NoLog{}, "", prometheus.NewRegistry())
require.NoError(err)

batchIntf := db.NewBatch()
Expand Down
22 changes: 6 additions & 16 deletions database/pebble/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package pebble

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -44,18 +43,8 @@ var (
MaxOpenFiles: 4096,
MaxConcurrentCompactions: 1,
}

DefaultConfigBytes []byte
Copy link
Contributor Author

Choose a reason for hiding this comment

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

nil is the default, this was never needed.

)

func init() {
var err error
DefaultConfigBytes, err = json.Marshal(DefaultConfig)
if err != nil {
panic(err)
}
}

type Database struct {
lock sync.RWMutex
pebbleDB *pebble.DB
Expand Down Expand Up @@ -200,17 +189,18 @@ func (db *Database) Compact(start []byte, end []byte) error {
}

if end == nil {
// The database.Database spec treats a nil [limit] as a key after all keys
// but pebble treats a nil [limit] as a key before all keys in Compact.
// Use the greatest key in the database as the [limit] to get the desired behavior.
// The database.Database spec treats a nil [limit] as a key after all
// keys but pebble treats a nil [limit] as a key before all keys in
// Compact. Use the greatest key in the database as the [limit] to get
// the desired behavior.
it := db.pebbleDB.NewIter(&pebble.IterOptions{})

if !it.Last() {
// The database is empty.
return it.Close()
}

end = it.Key()
end = slices.Clone(it.Key())
if err := it.Close(); err != nil {
Copy link
Contributor Author

@StephenButtolph StephenButtolph May 14, 2024

Choose a reason for hiding this comment

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

I do not think it was previously safe to hold end after calling it.Close.

return err
}
Expand Down Expand Up @@ -273,7 +263,7 @@ func keyRange(start, prefix []byte) *pebble.IterOptions {
LowerBound: prefix,
UpperBound: prefixToUpperBound(prefix),
}
if bytes.Compare(start, prefix) == 1 {
if pebble.DefaultComparer.Compare(start, prefix) == 1 {
opt.LowerBound = start
}
return opt
Expand Down
2 changes: 1 addition & 1 deletion database/pebble/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func newDB(t testing.TB) *Database {
folder := t.TempDir()
db, err := New(folder, DefaultConfigBytes, logging.NoLog{}, "pebble", prometheus.NewRegistry())
db, err := New(folder, nil, logging.NoLog{}, "pebble", prometheus.NewRegistry())
require.NoError(t, err)
return db.(*Database)
}
Expand Down
15 changes: 9 additions & 6 deletions database/pebble/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
var (
_ database.Iterator = (*iter)(nil)

errCouldntGetValue = errors.New("couldnt get iterator value")
errCouldNotGetValue = errors.New("could not get iterator value")
)

type iter struct {
Expand Down Expand Up @@ -63,16 +63,16 @@ func (it *iter) Next() bool {
return false
}

it.nextKey = it.iter.Key()

var err error
it.nextVal, err = it.iter.ValueAndErr()
key := it.iter.Key()
value, err := it.iter.ValueAndErr()
if err != nil {
it.hasNext = false
it.err = fmt.Errorf("%w: %w", errCouldntGetValue, err)
it.err = fmt.Errorf("%w: %w", errCouldNotGetValue, err)
return false
}

it.nextKey = key
it.nextVal = value
return true
}

Expand Down Expand Up @@ -122,6 +122,9 @@ func (it *iter) release() {
return
}

it.nextKey = slices.Clone(it.nextKey)
it.nextVal = slices.Clone(it.nextVal)
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved

// Remove the iterator from the list of open iterators.
it.db.openIterators.Remove(it)

Expand Down
Loading