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

Log unverified blockstore memory consumption #201

Merged
merged 5 commits into from
Aug 18, 2021
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package unverifiedblockstore

import (
"fmt"

logging "github.com/ipfs/go-log/v2"
ipld "github.com/ipld/go-ipld-prime"
)

var log = logging.Logger("gs-unverifiedbs")

type settableWriter interface {
SetBytes([]byte) error
}
Expand All @@ -15,6 +17,7 @@ type settableWriter interface {
type UnverifiedBlockStore struct {
inMemoryBlocks map[ipld.Link][]byte
storer ipld.Storer
dataSize uint64
}

// New initializes a new unverified store with the given storer function for writing
Expand All @@ -30,21 +33,26 @@ func New(storer ipld.Storer) *UnverifiedBlockStore {
// comes in as part of a traversal.
func (ubs *UnverifiedBlockStore) AddUnverifiedBlock(lnk ipld.Link, data []byte) {
ubs.inMemoryBlocks[lnk] = data
ubs.dataSize = ubs.dataSize + uint64(len(data))
Copy link
Member

Choose a reason for hiding this comment

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

I guess we don't need to synchronize because anyway the map is not synchronized, which suggests that this component is only ever called serially?

Copy link
Contributor Author

@aarshkshah1992 aarshkshah1992 Aug 18, 2021

Choose a reason for hiding this comment

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

@raulk Yeah - it's all done inside an event loop.

log.Debugw("added in-memory block", "data-size", ubs.dataSize)
aarshkshah1992 marked this conversation as resolved.
Show resolved Hide resolved
}

// PruneBlocks removes blocks from the unverified store without committing them,
// if the passed in function returns true for the given link
func (ubs *UnverifiedBlockStore) PruneBlocks(shouldPrune func(ipld.Link) bool) {
for link := range ubs.inMemoryBlocks {
for link,data := range ubs.inMemoryBlocks {
if shouldPrune(link) {
delete(ubs.inMemoryBlocks, link)
ubs.dataSize = ubs.dataSize - uint64(len(data))
}
}
log.Debugw("finished pruning in-memory blocks", "data-size", ubs.dataSize)
aarshkshah1992 marked this conversation as resolved.
Show resolved Hide resolved
}

// PruneBlock deletes an individual block from the store
func (ubs *UnverifiedBlockStore) PruneBlock(link ipld.Link) {
delete(ubs.inMemoryBlocks, link)
ubs.dataSize = ubs.dataSize - uint64(len(ubs.inMemoryBlocks[link]))
}

// VerifyBlock verifies the data for the given link as being part of a traversal,
Expand All @@ -55,6 +63,9 @@ func (ubs *UnverifiedBlockStore) VerifyBlock(lnk ipld.Link) ([]byte, error) {
return nil, fmt.Errorf("Block not found")
}
delete(ubs.inMemoryBlocks, lnk)
ubs.dataSize = ubs.dataSize - uint64(len(data))


aarshkshah1992 marked this conversation as resolved.
Show resolved Hide resolved
buffer, committer, err := ubs.storer(ipld.LinkContext{})
if err != nil {
return nil, err
Expand Down