-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
reduce garbage in blockstore #4406
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bbc6de9
write messages to a temp blockstore when validating
Stebalien e2fbbdc
reject messages with invalid CIDs
Stebalien 4b38809
in-memory blockstore
Stebalien 15fe998
add an timed-cache blockstore
Stebalien ddade32
write bitswap blocks into a temporary, in-memory block cache
Stebalien 811f130
test timed cache blockstore
Stebalien 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package blockstore | ||
|
||
import ( | ||
"context" | ||
|
||
blocks "github.com/ipfs/go-block-format" | ||
"github.com/ipfs/go-cid" | ||
blockstore "github.com/ipfs/go-ipfs-blockstore" | ||
) | ||
|
||
type MemStore map[cid.Cid]blocks.Block | ||
|
||
func (m MemStore) DeleteBlock(k cid.Cid) error { | ||
delete(m, k) | ||
return nil | ||
} | ||
func (m MemStore) Has(k cid.Cid) (bool, error) { | ||
_, ok := m[k] | ||
return ok, nil | ||
} | ||
func (m MemStore) Get(k cid.Cid) (blocks.Block, error) { | ||
b, ok := m[k] | ||
if !ok { | ||
return nil, blockstore.ErrNotFound | ||
} | ||
return b, nil | ||
} | ||
|
||
// GetSize returns the CIDs mapped BlockSize | ||
func (m MemStore) GetSize(k cid.Cid) (int, error) { | ||
b, ok := m[k] | ||
if !ok { | ||
return 0, blockstore.ErrNotFound | ||
} | ||
return len(b.RawData()), nil | ||
} | ||
|
||
// Put puts a given block to the underlying datastore | ||
func (m MemStore) Put(b blocks.Block) error { | ||
// Convert to a basic block for safety, but try to reuse the existing | ||
// block if it's already a basic block. | ||
k := b.Cid() | ||
if _, ok := b.(*blocks.BasicBlock); !ok { | ||
// If we already have the block, abort. | ||
if _, ok := m[k]; ok { | ||
return nil | ||
} | ||
// the error is only for debugging. | ||
b, _ = blocks.NewBlockWithCid(b.RawData(), b.Cid()) | ||
} | ||
m[b.Cid()] = b | ||
return nil | ||
} | ||
|
||
// PutMany puts a slice of blocks at the same time using batching | ||
// capabilities of the underlying datastore whenever possible. | ||
func (m MemStore) PutMany(bs []blocks.Block) error { | ||
for _, b := range bs { | ||
_ = m.Put(b) // can't fail | ||
} | ||
return nil | ||
} | ||
|
||
// AllKeysChan returns a channel from which | ||
// the CIDs in the Blockstore can be read. It should respect | ||
// the given context, closing the channel if it becomes Done. | ||
func (m MemStore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) { | ||
ch := make(chan cid.Cid, len(m)) | ||
for k := range m { | ||
ch <- k | ||
} | ||
close(ch) | ||
return ch, nil | ||
} | ||
|
||
// HashOnRead specifies if every read block should be | ||
// rehashed to make sure it matches its CID. | ||
func (m MemStore) HashOnRead(enabled bool) { | ||
// no-op | ||
} |
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,68 @@ | ||
package blockstore | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
blocks "github.com/ipfs/go-block-format" | ||
"github.com/ipfs/go-cid" | ||
) | ||
|
||
type SyncStore struct { | ||
mu sync.RWMutex | ||
bs MemStore // specifically use a memStore to save indirection overhead. | ||
} | ||
|
||
func (m *SyncStore) DeleteBlock(k cid.Cid) error { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
return m.bs.DeleteBlock(k) | ||
} | ||
func (m *SyncStore) Has(k cid.Cid) (bool, error) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
return m.bs.Has(k) | ||
} | ||
func (m *SyncStore) Get(k cid.Cid) (blocks.Block, error) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
return m.bs.Get(k) | ||
} | ||
|
||
// GetSize returns the CIDs mapped BlockSize | ||
func (m *SyncStore) GetSize(k cid.Cid) (int, error) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
return m.bs.GetSize(k) | ||
} | ||
|
||
// Put puts a given block to the underlying datastore | ||
func (m *SyncStore) Put(b blocks.Block) error { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
return m.bs.Put(b) | ||
} | ||
|
||
// PutMany puts a slice of blocks at the same time using batching | ||
// capabilities of the underlying datastore whenever possible. | ||
func (m *SyncStore) PutMany(bs []blocks.Block) error { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
return m.bs.PutMany(bs) | ||
} | ||
|
||
// AllKeysChan returns a channel from which | ||
// the CIDs in the Blockstore can be read. It should respect | ||
// the given context, closing the channel if it becomes Done. | ||
func (m *SyncStore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
// this blockstore implementation doesn't do any async work. | ||
return m.bs.AllKeysChan(ctx) | ||
} | ||
|
||
// HashOnRead specifies if every read block should be | ||
// rehashed to make sure it matches its CID. | ||
func (m *SyncStore) HashOnRead(enabled bool) { | ||
// noop | ||
} |
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
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.
Can the comments on 326-328 be removed? I believe this addresses them.
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.
I think they're still valid? That is, at this point we know we have a "sane" block, but it may not be otherwise valid.
But I don't know the original intention of that comment.