Skip to content

Conversation

distractedm1nd
Copy link

@distractedm1nd distractedm1nd commented Apr 3, 2023

At celestia-node, we don't only use ShardAccessor's Blockstore to serve individual blocks, but also use the underlying mount to stream the CAR file to other full nodes. We do this by adding a Reader() io.Reader method to ShardAccessor that creates independent mmap-backed (when possible) readers. We would love to upstream it to avoid maintaining a fork.

accessor.go Outdated
Comment on lines 61 to 69
if sa.mmapr == nil {
if f, ok := sa.data.(*os.File); ok {
if mmapr, err := mmap.Open(f.Name()); err != nil {
log.Warnf("failed to mmap reader of type %T: %s; using reader as-is", sa.data, err)
} else {
sa.mmapr = mmapr
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Let's extract this logic to a tryMmap() method (and document it requires the lock to be held) and refactor the Blockstore() method to use that?

accessor.go Outdated
Comment on lines 71 to 73
if sa.mmapr != nil {
return sa.mmapr.ReadAt(p, 0)
}
Copy link
Member

Choose a reason for hiding this comment

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

Would move this above since we expect it to be the most common path when a shard gets reused.

accessor.go Outdated
Comment on lines 82 to 84
if sa.mmapr != nil {
r = sa.mmapr
} else if f, ok := sa.data.(*os.File); ok {
Copy link
Member

Choose a reason for hiding this comment

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

Let's extract the mmap logic; then make this method short-circuit for more readable and idiomatic code.

@Wondertan
Copy link

Crossposting my review celestiaorg#2 (comment)

also cc @raulk

@distractedm1nd distractedm1nd changed the title feat: implementing io.Reader for ShardAccessor feat: io.Reader factory for ShardAccessor Apr 13, 2023
@distractedm1nd distractedm1nd marked this pull request as ready for review April 13, 2023 08:39
Copy link

@Wondertan Wondertan left a comment

Choose a reason for hiding this comment

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

LGTM now

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.

3 participants