-
Notifications
You must be signed in to change notification settings - Fork 179
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
[Badger] Refactor approvals to use badger batch updates #6466
Draft
zhangchiqing
wants to merge
9
commits into
leo/db-ops
Choose a base branch
from
leo/db-ops-approval
base: leo/db-ops
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6cc0019
add universal database operations
zhangchiqing f8823aa
add benchmark
zhangchiqing 0fc22e3
bench delete
zhangchiqing 7334a0b
benchmark RemoveByPrefix
zhangchiqing b82106a
add iterator benchmark
zhangchiqing 4fb6715
fix Traverse function
zhangchiqing 5c99655
add test to verify hierachical prefixes
zhangchiqing c0e4e2a
refactor approvals
zhangchiqing 87b20dd
add benchmark
zhangchiqing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package operation | ||
|
||
import ( | ||
"github.com/onflow/flow-go/model/flow" | ||
"github.com/onflow/flow-go/storage" | ||
) | ||
|
||
// InsertResultApproval inserts a ResultApproval by ID. | ||
// The same key (`approval.ID()`) necessitates that the value (full `approval`) is | ||
// also identical (otherwise, we would have a successful pre-image attack on our | ||
// cryptographic hash function). Therefore, concurrent calls to this function are safe. | ||
func InsertResultApproval(approval *flow.ResultApproval) func(storage.Writer) error { | ||
return Upsert(makePrefix(codeResultApproval, approval.ID()), approval) | ||
} | ||
|
||
// RetrieveResultApproval retrieves an approval by ID. | ||
func RetrieveResultApproval(approvalID flow.Identifier, approval *flow.ResultApproval) func(storage.Reader) error { | ||
return Retrieve(makePrefix(codeResultApproval, approvalID), approval) | ||
} | ||
|
||
// UnsafeIndexResultApproval inserts a ResultApproval ID keyed by ExecutionResult ID | ||
// and chunk index. If a value for this key exists, a storage.ErrAlreadyExists | ||
// error is returned. This operation is only used by the ResultApprovals store, | ||
// which is only used within a Verification node, where it is assumed that there | ||
// is only one approval per chunk. | ||
// CAUTION: Use of this function must be synchronized by storage.ResultApprovals. | ||
func UnsafeIndexResultApproval(resultID flow.Identifier, chunkIndex uint64, approvalID flow.Identifier) func(storage.Writer) error { | ||
return Upsert(makePrefix(codeIndexResultApprovalByChunk, resultID, chunkIndex), approvalID) | ||
} | ||
|
||
// LookupResultApproval finds a ResultApproval by result ID and chunk index. | ||
func LookupResultApproval(resultID flow.Identifier, chunkIndex uint64, approvalID *flow.Identifier) func(storage.Reader) error { | ||
return Retrieve(makePrefix(codeIndexResultApprovalByChunk, resultID, chunkIndex), approvalID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package operation_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/onflow/flow-go/model/flow" | ||
"github.com/onflow/flow-go/storage" | ||
"github.com/onflow/flow-go/storage/operation" | ||
"github.com/onflow/flow-go/storage/operation/dbtest" | ||
"github.com/onflow/flow-go/utils/unittest" | ||
) | ||
|
||
func BenchmarkRetrieveApprovals(b *testing.B) { | ||
dbtest.BenchWithDB(b, func(b *testing.B, db storage.DB) { | ||
b.Run("RetrieveApprovals", func(b *testing.B) { | ||
approval := unittest.ResultApprovalFixture() | ||
require.NoError(b, db.WithReaderBatchWriter(storage.OnlyWriter(operation.InsertResultApproval(approval)))) | ||
|
||
b.ResetTimer() | ||
|
||
approvalID := approval.ID() | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
var stored flow.ResultApproval | ||
require.NoError(b, operation.RetrieveResultApproval(approvalID, &stored)(db.Reader())) | ||
} | ||
}) | ||
|
||
}) | ||
}) | ||
} | ||
|
||
func BenchmarkInsertApproval(b *testing.B) { | ||
dbtest.BenchWithDB(b, func(b *testing.B, db storage.DB) { | ||
b.Run("InsertApprovals", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
approval := unittest.ResultApprovalFixture() | ||
require.NoError(b, db.WithReaderBatchWriter(storage.OnlyWriter(operation.InsertResultApproval(approval)))) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package badgerimpl | ||
|
||
import ( | ||
"github.com/dgraph-io/badger/v2" | ||
|
||
"github.com/onflow/flow-go/storage" | ||
) | ||
|
||
func ToDB(db *badger.DB) storage.DB { | ||
return &dbStore{db: db} | ||
} | ||
|
||
type dbStore struct { | ||
db *badger.DB | ||
} | ||
|
||
func (b *dbStore) Reader() storage.Reader { | ||
return dbReader{db: b.db} | ||
} | ||
|
||
func (b *dbStore) WithReaderBatchWriter(fn func(storage.ReaderBatchWriter) error) error { | ||
return WithReaderBatchWriter(b.db, fn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package badgerimpl | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/dgraph-io/badger/v2" | ||
|
||
"github.com/onflow/flow-go/storage" | ||
) | ||
|
||
type badgerIterator struct { | ||
iter *badger.Iterator | ||
lowerBound []byte | ||
upperBound []byte | ||
} | ||
|
||
var _ storage.Iterator = (*badgerIterator)(nil) | ||
|
||
func newBadgerIterator(db *badger.DB, startPrefix, endPrefix []byte, ops storage.IteratorOption) *badgerIterator { | ||
options := badger.DefaultIteratorOptions | ||
if ops.IterateKeyOnly { | ||
options.PrefetchValues = false | ||
} | ||
|
||
tx := db.NewTransaction(false) | ||
iter := tx.NewIterator(options) | ||
|
||
lowerBound, upperBound := storage.StartEndPrefixToLowerUpperBound(startPrefix, endPrefix) | ||
|
||
return &badgerIterator{ | ||
iter: iter, | ||
lowerBound: lowerBound, | ||
upperBound: upperBound, | ||
} | ||
} | ||
|
||
func (i *badgerIterator) SeekGE() { | ||
i.iter.Seek(i.lowerBound) | ||
} | ||
|
||
func (i *badgerIterator) Valid() bool { | ||
// if it's beyond the upper bound, it's invalid | ||
if !i.iter.Valid() { | ||
return false | ||
} | ||
key := i.iter.Item().Key() | ||
// "< 0" means the upperBound is exclusive | ||
valid := bytes.Compare(key, i.upperBound) < 0 | ||
return valid | ||
} | ||
|
||
func (i *badgerIterator) Next() { | ||
i.iter.Next() | ||
} | ||
|
||
func (i *badgerIterator) IterItem() storage.IterItem { | ||
return i.iter.Item() | ||
} | ||
|
||
var _ storage.IterItem = (*badger.Item)(nil) | ||
|
||
func (i *badgerIterator) Close() error { | ||
i.iter.Close() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package badgerimpl | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
"github.com/dgraph-io/badger/v2" | ||
|
||
"github.com/onflow/flow-go/module/irrecoverable" | ||
"github.com/onflow/flow-go/storage" | ||
) | ||
|
||
type dbReader struct { | ||
db *badger.DB | ||
} | ||
|
||
type noopCloser struct{} | ||
|
||
var _ io.Closer = (*noopCloser)(nil) | ||
|
||
func (noopCloser) Close() error { return nil } | ||
|
||
func (b dbReader) Get(key []byte) ([]byte, io.Closer, error) { | ||
tx := b.db.NewTransaction(false) | ||
defer tx.Discard() | ||
|
||
item, err := tx.Get(key) | ||
if err != nil { | ||
if errors.Is(err, badger.ErrKeyNotFound) { | ||
return nil, nil, storage.ErrNotFound | ||
} | ||
return nil, nil, irrecoverable.NewExceptionf("could not load data: %w", err) | ||
} | ||
|
||
var value []byte | ||
err = item.Value(func(val []byte) error { | ||
value = append([]byte{}, val...) | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, nil, irrecoverable.NewExceptionf("could not load value: %w", err) | ||
} | ||
|
||
return value, noopCloser{}, nil | ||
} | ||
|
||
func (b dbReader) NewIter(startPrefix, endPrefix []byte, ops storage.IteratorOption) (storage.Iterator, error) { | ||
return newBadgerIterator(b.db, startPrefix, endPrefix, ops), nil | ||
} | ||
|
||
// ToReader is a helper function to convert a *badger.DB to a Reader | ||
func ToReader(db *badger.DB) storage.Reader { | ||
return dbReader{db} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switching from badger to pebble in the future would just be changing
badgerimpl
topebbleimpl
here.