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

[filedao] get recent blocks from staging buffer #4488

Merged
merged 2 commits into from
Nov 15, 2024
Merged

[filedao] get recent blocks from staging buffer #4488

merged 2 commits into from
Nov 15, 2024

Conversation

dustinxie
Copy link
Member

@dustinxie dustinxie commented Nov 14, 2024

Description

as title

Fixes #4489

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • [] New feature (non-breaking change which adds functionality)
  • Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

buffer []*block.Store
deser *block.Deserializer
Copy link
Member Author

Choose a reason for hiding this comment

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

not needed

// stagingKey is the position of block in the staging buffer
func stagingKey(height uint64, header *FileHeader) uint64 {
return (height - header.Start) % header.BlockStoreSize
}
Copy link
Member Author

Choose a reason for hiding this comment

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

replaced by stagingBuffer.slot()

func (fd *fileDAOv2) getFromStagingBuffer(height uint64) (*block.Store, error) {
if fd.loadTip().Height-height >= fd.header.BlockStoreSize {
return nil, ErrNotSupported
}
Copy link
Member Author

@dustinxie dustinxie Nov 14, 2024

Choose a reason for hiding this comment

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

improve the logic, nothing to do with blkStore.Size() now
staging buffer contains the top BlockStoreSize blocks, for example, if tip height = 100, it has blocks 100, 99, ... 85

Copy link
Member

Choose a reason for hiding this comment

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

The return value of blkStore.Size() is currently incorrect, and it seems that this PR did not fix it. Shouldn't the Size method be removed? If Size is used elsewhere, it should also be checked for similar issues.

Copy link
Member Author

Choose a reason for hiding this comment

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

will have another PR

Copy link

codecov bot commented Nov 14, 2024

Codecov Report

Attention: Patch coverage is 93.75000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 72.86%. Comparing base (436e4d1) to head (288d723).
Report is 25 commits behind head on master.

Files with missing lines Patch % Lines
blockchain/filedao/staging_buffer.go 87.50% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4488      +/-   ##
==========================================
- Coverage   74.83%   72.86%   -1.97%     
==========================================
  Files         378      382       +4     
  Lines       31624    32565     +941     
==========================================
+ Hits        23666    23730      +64     
- Misses       6747     7612     +865     
- Partials     1211     1223      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -178,12 +173,7 @@ func (fd *fileDAOv2) getBlock(height uint64) (*block.Block, error) {
return nil, db.ErrNotExist
}
// check whether block in staging buffer or not
storeKey := blockStoreKey(height, fd.header)
if storeKey >= fd.blkStore.Size() {
Copy link
Member Author

Choose a reason for hiding this comment

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

root-cause of the bug:
the batch has not been written to DB yet, but blkStore.Size() already increased 1. This caused code go to L190 attempting to read the block from DB, which is not yet written to disk yet.

func (s *stagingBuffer) Serialize() ([]byte, error) {
blkStores := []*iotextypes.BlockStore{}
// blob sidecar data are stored separately
s.lock.Lock()
Copy link
Member

@envestcc envestcc Nov 14, 2024

Choose a reason for hiding this comment

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

seems unnecessary to lock (rlock at most), b/c Put and Serialize will not be called parallelly.

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. in current code, they are not called parallelly. As 2 public methods, should consider synchronize access?
  2. Serialize is actually also a read operation, so can change to RLock

if err != nil {
return nil, err
}
if blkStore, err := fd.getFromStagingBuffer(height); err == nil {
Copy link
Member

@envestcc envestcc Nov 14, 2024

Choose a reason for hiding this comment

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

It is better to check for errors and throw if unexpected

Copy link
Member Author

Choose a reason for hiding this comment

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

updated in 2nd commit

@dustinxie dustinxie merged commit e3f51ac into master Nov 15, 2024
3 checks passed
@dustinxie dustinxie deleted the stbuffer branch November 15, 2024 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

failed to get block at height
3 participants