This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
segment the memory peerstore + granular locks #78
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e7e511b
segment the memory peerstore; granular locks.
raulk 303b1b6
update segment size on gc
vyzo ea23986
segmented protocol info
vyzo 08caa87
refactor protocol functionality into ProtoBook
vyzo 5ed17f0
embed lock in segment
vyzo a9e3f07
global interned protocol table
vyzo 3140aaa
implement protobook for pstoreds
vyzo d340d29
cosmetics
vyzo afca355
index the peer ID strings directly to avoid copying the byte slice
vyzo 28b1a9f
don't track segment sizes, it's a fruitless optimization
vyzo 07ee3fb
make lock methods private
vyzo 7fac6b6
use segments in ds protobook instead of a lock array
vyzo 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,118 @@ | ||
package pstoreds | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
peer "github.com/libp2p/go-libp2p-peer" | ||
|
||
pstore "github.com/libp2p/go-libp2p-peerstore" | ||
) | ||
|
||
type dsProtoBook struct { | ||
lks [256]sync.RWMutex | ||
meta pstore.PeerMetadata | ||
} | ||
|
||
var _ pstore.ProtoBook = (*dsProtoBook)(nil) | ||
|
||
func NewProtoBook(meta pstore.PeerMetadata) pstore.ProtoBook { | ||
return &dsProtoBook{meta: meta} | ||
} | ||
|
||
func (pb *dsProtoBook) lock(p peer.ID) { | ||
pb.lks[byte(p[len(p)-1])].Lock() | ||
} | ||
|
||
func (pb *dsProtoBook) unlock(p peer.ID) { | ||
pb.lks[byte(p[len(p)-1])].Unlock() | ||
} | ||
|
||
func (pb *dsProtoBook) rlock(p peer.ID) { | ||
pb.lks[byte(p[len(p)-1])].RLock() | ||
} | ||
|
||
func (pb *dsProtoBook) runlock(p peer.ID) { | ||
pb.lks[byte(p[len(p)-1])].RUnlock() | ||
} | ||
|
||
func (pb *dsProtoBook) SetProtocols(p peer.ID, protos ...string) error { | ||
pb.lock(p) | ||
defer pb.unlock(p) | ||
|
||
protomap := make(map[string]struct{}, len(protos)) | ||
for _, proto := range protos { | ||
protomap[proto] = struct{}{} | ||
} | ||
|
||
return pb.meta.Put(p, "protocols", protomap) | ||
} | ||
|
||
func (pb *dsProtoBook) AddProtocols(p peer.ID, protos ...string) error { | ||
pb.lock(p) | ||
defer pb.unlock(p) | ||
|
||
pmap, err := pb.getProtocolMap(p) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, proto := range protos { | ||
pmap[proto] = struct{}{} | ||
} | ||
|
||
return pb.meta.Put(p, "protocols", pmap) | ||
} | ||
|
||
func (pb *dsProtoBook) GetProtocols(p peer.ID) ([]string, error) { | ||
pb.rlock(p) | ||
defer pb.runlock(p) | ||
|
||
pmap, err := pb.getProtocolMap(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res := make([]string, 0, len(pmap)) | ||
for proto := range pmap { | ||
res = append(res, proto) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (pb *dsProtoBook) SupportsProtocols(p peer.ID, protos ...string) ([]string, error) { | ||
pb.rlock(p) | ||
defer pb.runlock(p) | ||
|
||
pmap, err := pb.getProtocolMap(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res := make([]string, 0, len(protos)) | ||
for _, proto := range protos { | ||
if _, ok := pmap[proto]; ok { | ||
res = append(res, proto) | ||
} | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (pb *dsProtoBook) getProtocolMap(p peer.ID) (map[string]struct{}, error) { | ||
iprotomap, err := pb.meta.Get(p, "protocols") | ||
switch err { | ||
default: | ||
return nil, err | ||
case pstore.ErrNotFound: | ||
return make(map[string]struct{}), nil | ||
case nil: | ||
cast, ok := iprotomap.(map[string]struct{}) | ||
if !ok { | ||
return nil, fmt.Errorf("stored protocol set was not a map") | ||
} | ||
|
||
return cast, nil | ||
} | ||
} |
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.
nit: a
segment(p peer.ID) *segment
function would be nice (could even drop all of these functions and just callpb.segment(p).Lock()
.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.
ok, will do. I'll call it
get
similar to how we do it in the memory peerstore.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.
done.