-
Notifications
You must be signed in to change notification settings - Fork 118
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
Limit Accepted Block Cache + Store BlockID Index On-Disk #558
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
66db76b
change block storage to 50k
patrick-ogrady a429a17
only store a handful of blocks
patrick-ogrady eefbcea
fix lint
patrick-ogrady d68dc6c
emit instructions to install prometheus
patrick-ogrady 1e1ed8e
log block deletions
patrick-ogrady 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |
"github.com/ava-labs/avalanchego/cache" | ||
"github.com/ava-labs/avalanchego/database" | ||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/snow/choices" | ||
"github.com/ava-labs/avalanchego/utils/crypto/bls" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/warp" | ||
"go.uber.org/zap" | ||
|
@@ -34,9 +35,11 @@ func init() { | |
} | ||
|
||
const ( | ||
blockPrefix = 0x0 | ||
warpSignaturePrefix = 0x1 | ||
warpFetchPrefix = 0x2 | ||
blockPrefix = 0x0 // TODO: move to flat files (https://github.com/ava-labs/hypersdk/issues/553) | ||
blockIDHeightPrefix = 0x1 // ID -> Height | ||
blockHeightIDPrefix = 0x2 // Height -> ID (don't always need full block from disk) | ||
warpSignaturePrefix = 0x3 | ||
warpFetchPrefix = 0x4 | ||
) | ||
|
||
var ( | ||
|
@@ -46,19 +49,33 @@ var ( | |
signatureLRU = &cache.LRU[string, *chain.WarpSignature]{Size: 1024} | ||
) | ||
|
||
func PrefixBlockHeightKey(height uint64) []byte { | ||
func PrefixBlockKey(height uint64) []byte { | ||
k := make([]byte, 1+consts.Uint64Len) | ||
k[0] = blockPrefix | ||
binary.BigEndian.PutUint64(k[1:], height) | ||
return k | ||
} | ||
|
||
func PrefixBlockIDHeightKey(id ids.ID) []byte { | ||
k := make([]byte, 1+consts.IDLen) | ||
k[0] = blockIDHeightPrefix | ||
copy(k[1:], id[:]) | ||
return k | ||
} | ||
|
||
func PrefixBlockHeightIDKey(height uint64) []byte { | ||
k := make([]byte, 1+consts.Uint64Len) | ||
k[0] = blockHeightIDPrefix | ||
binary.BigEndian.PutUint64(k[1:], height) | ||
return k | ||
} | ||
|
||
func (vm *VM) HasGenesis() (bool, error) { | ||
return vm.HasDiskBlock(0) | ||
} | ||
|
||
func (vm *VM) GetGenesis() (*chain.StatefulBlock, error) { | ||
return vm.GetDiskBlock(0) | ||
func (vm *VM) GetGenesis(ctx context.Context) (*chain.StatelessBlock, error) { | ||
return vm.GetDiskBlock(ctx, 0) | ||
} | ||
|
||
func (vm *VM) SetLastAcceptedHeight(height uint64) error { | ||
|
@@ -96,16 +113,35 @@ func (vm *VM) shouldComapct(expiryHeight uint64) bool { | |
// compaction as storing blocks randomly on-disk (when using [block.ID]). | ||
func (vm *VM) UpdateLastAccepted(blk *chain.StatelessBlock) error { | ||
batch := vm.vmDB.NewBatch() | ||
if err := batch.Put(lastAccepted, binary.BigEndian.AppendUint64(nil, blk.Height())); err != nil { | ||
bigEndianHeight := binary.BigEndian.AppendUint64(nil, blk.Height()) | ||
if err := batch.Put(lastAccepted, bigEndianHeight); err != nil { | ||
return err | ||
} | ||
if err := batch.Put(PrefixBlockKey(blk.Height()), blk.Bytes()); err != nil { | ||
return err | ||
} | ||
if err := batch.Put(PrefixBlockHeightKey(blk.Height()), blk.Bytes()); err != nil { | ||
if err := batch.Put(PrefixBlockIDHeightKey(blk.ID()), bigEndianHeight); err != nil { | ||
return err | ||
} | ||
blkID := blk.ID() | ||
if err := batch.Put(PrefixBlockHeightIDKey(blk.Height()), blkID[:]); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We store a height lookup to avoid pulling entire blocks from disk to service historical queries. |
||
return err | ||
} | ||
expiryHeight := blk.Height() - uint64(vm.config.GetAcceptedBlockWindow()) | ||
var expired bool | ||
if expiryHeight > 0 && expiryHeight < blk.Height() { // ensure we don't free genesis | ||
if err := batch.Delete(PrefixBlockHeightKey(expiryHeight)); err != nil { | ||
if err := batch.Delete(PrefixBlockKey(expiryHeight)); err != nil { | ||
return err | ||
} | ||
blkID, err := vm.vmDB.Get(PrefixBlockHeightIDKey(expiryHeight)) | ||
if err == nil { | ||
if err := batch.Delete(PrefixBlockIDHeightKey(ids.ID(blkID))); err != nil { | ||
return err | ||
} | ||
} else { | ||
vm.Logger().Warn("unable to delete blkID", zap.Uint64("height", expiryHeight), zap.Error(err)) | ||
} | ||
if err := batch.Delete(PrefixBlockHeightIDKey(expiryHeight)); err != nil { | ||
return err | ||
} | ||
expired = true | ||
|
@@ -130,24 +166,40 @@ func (vm *VM) UpdateLastAccepted(blk *chain.StatelessBlock) error { | |
return nil | ||
} | ||
|
||
func (vm *VM) GetDiskBlock(height uint64) (*chain.StatefulBlock, error) { | ||
b, err := vm.vmDB.Get(PrefixBlockHeightKey(height)) | ||
func (vm *VM) GetDiskBlock(ctx context.Context, height uint64) (*chain.StatelessBlock, error) { | ||
b, err := vm.vmDB.Get(PrefixBlockKey(height)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return chain.UnmarshalBlock(b, vm) | ||
return chain.ParseBlock(ctx, b, choices.Accepted, vm) | ||
} | ||
|
||
func (vm *VM) HasDiskBlock(height uint64) (bool, error) { | ||
return vm.vmDB.Has(PrefixBlockHeightKey(height)) | ||
return vm.vmDB.Has(PrefixBlockKey(height)) | ||
} | ||
|
||
func (vm *VM) GetBlockHeightID(height uint64) (ids.ID, error) { | ||
b, err := vm.vmDB.Get(PrefixBlockHeightIDKey(height)) | ||
if err != nil { | ||
return ids.Empty, err | ||
} | ||
return ids.ID(b), nil | ||
} | ||
|
||
func (vm *VM) GetBlockIDHeight(blkID ids.ID) (uint64, error) { | ||
b, err := vm.vmDB.Get(PrefixBlockIDHeightKey(blkID)) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return binary.BigEndian.Uint64(b), nil | ||
} | ||
|
||
// CompactDiskBlocks forces compaction on the entire range of blocks up to [lastExpired]. | ||
// | ||
// This can be used to ensure we clean up all large tombstoned keys on a regular basis instead | ||
// of waiting for the database to run a compaction (and potentially delete GBs of data at once). | ||
func (vm *VM) CompactDiskBlocks(lastExpired uint64) error { | ||
return vm.vmDB.Compact([]byte{blockPrefix}, PrefixBlockHeightKey(lastExpired)) | ||
return vm.vmDB.Compact([]byte{blockPrefix}, PrefixBlockKey(lastExpired)) | ||
} | ||
|
||
func (vm *VM) GetDiskIsSyncing() (bool, error) { | ||
|
Oops, something went wrong.
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.
We need to store this on-disk now so that we can service historical
Get
requests. We make this tradeoff so that we can avoid db compaction (from storing blocks all over disk).