search

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2018 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultNClosestResponses is the default number of peers to find closest to the key.
	DefaultNClosestResponses = uint(6)

	// DefaultNMaxErrors is the default maximum number of errors tolerated during a search.
	DefaultNMaxErrors = uint(3)

	// DefaultConcurrency is the default number of parallel search workers.
	DefaultConcurrency = uint(1)

	// DefaultQueryTimeout is the timeout for each query to a peer.
	DefaultQueryTimeout = 3 * time.Second
)

Variables

View Source
var (
	// ErrTooManyFindErrors indicates when a search has encountered too many Find request
	// errors.
	ErrTooManyFindErrors = errors.New("too many Find errors")
)

Functions

func AddPeers

func AddPeers(
	queried map[string]struct{},
	unqueried ClosestPeers,
	doc comm.Doctor,
	peers []*api.PeerAddress,
	fromer peer.Fromer,
)

AddPeers adds a list of peer address to the unqueried heap.

func NewTestPeers

func NewTestPeers(rng *rand.Rand, n int) (
	[]peer.Peer, map[string]peer.Peer, map[string][]*api.PeerAddress, []int, ecid.ID,
)

NewTestPeers creates a collection of test peers with fixed addresses in each's routing table (such that all find queries return the same addresses). It also returns the indices of the peers that peer 0 has in its routing table.

func NewTestSeeds

func NewTestSeeds(peers []peer.Peer, selfPeerIdxs []int) []peer.Peer

NewTestSeeds creates a list of peers to use as seeds for a search.

Types

type ClosestPeers

type ClosestPeers interface {
	PeerDistanceHeap
}

ClosestPeers is a min-heap of peers with the closest peer at the root.

func NewClosestPeers

func NewClosestPeers(target id.ID, capacity uint) ClosestPeers

NewClosestPeers returns a ClosestPeers instance for a given target with a given capacity.

type FarthestPeers

type FarthestPeers interface {
	PeerDistanceHeap
}

FarthestPeers is a max-heap of peers with the farthest peer at the root.

func NewFarthestPeers

func NewFarthestPeers(target id.ID, capacity uint) FarthestPeers

NewFarthestPeers returns a FarthestPeers instance for a given target with a given capacity.

type Parameters

type Parameters struct {
	// NClosestResponses is the required number of peers closest to the key we need to receive
	// responses from
	NClosestResponses uint

	// NMaxErrors is the maximum number of errors tolerated when querying peers during the search
	NMaxErrors uint

	// Concurrency is the number of concurrent queries to use in search
	Concurrency uint

	// Timeout for queries to individual peers
	Timeout time.Duration
}

Parameters defines the parameters of the search.

func NewDefaultParameters

func NewDefaultParameters() *Parameters

NewDefaultParameters creates an instance with default parameters.

func (*Parameters) MarshalLogObject

func (p *Parameters) MarshalLogObject(oe zapcore.ObjectEncoder) error

MarshalLogObject converts the Parameters into an object (which will become json) for logging.

type PeerDistanceHeap

type PeerDistanceHeap interface {
	heap.Interface

	// SafePush pushes a peer onto the heap and subsequently removes a peer if the number of
	// peers exceeds the capacity. At capacity, these peer removals guarantee that the head
	// always becomes closer to the target or stays the same with each push.
	SafePush(peer.Peer)

	// SafePushMany pushed an array of peers.
	SafePushMany([]peer.Peer)

	// PeakDistance returns the distance from the root of the heap to the target.
	PeakDistance() *big.Int

	// PeakPeer returns (but does not remove) the the root of the heap.
	PeakPeer() peer.Peer

	// ToAPI creates an array of api.PeerAddresses from the peers in the heap.
	ToAPI() []*api.PeerAddress

	// Peers returns the ordered peers in the heap, starting from the top.
	Peers() []peer.Peer

	// In returns whether a peer ID is in the heap
	In(id.ID) bool

	// Distance returns the distance from a peer to the target.
	Distance(peer.Peer) *big.Int

	// Capacity return the maximum number of peers allowed in the heap.
	Capacity() int
}

PeerDistanceHeap represents a heap of peers sorted by their distance to a given target.

type QueryQueue added in v0.2.0

type QueryQueue struct {
	Peers chan peer.Peer
	// contains filtered or unexported fields
}

QueryQueue is a thin wrapper aound a Peer channel with improved concurrency robustness.

func NewQueryQueue added in v0.2.0

func NewQueryQueue() *QueryQueue

NewQueryQueue returns a new QueryQueue.

func (*QueryQueue) MaybeClose added in v0.2.0

func (qq *QueryQueue) MaybeClose()

MaybeClose closes the queue if it hasn't already been closed.

func (*QueryQueue) MaybeSend added in v0.2.0

func (qq *QueryQueue) MaybeSend(p peer.Peer)

MaybeSend sends a peer on the queue if it hasn't been closed.

type ResponseProcessor

type ResponseProcessor interface {
	// Process handles an api.FindResponse, adding newly discovered peers to the unqueried
	// ClosestPeers heap.
	Process(*api.FindResponse, *Search) error
}

ResponseProcessor handles an api.FindResponse

func NewResponseProcessor

func NewResponseProcessor(f peer.Fromer, doc comm.Doctor) ResponseProcessor

NewResponseProcessor creates a new ResponseProcessor instance.

type Result

type Result struct {
	// Value found when looking for one, otherwise nil
	Value *api.Document

	// Closest is a heap of the responding peers found closest to the target
	Closest FarthestPeers

	// Unqueried is a heap of peers that were not yet queried
	Unqueried ClosestPeers

	// Queried is a set of all peers (keyed by peer.ID().String()) that have been queried (but
	// haven't yet necessarily responded or errored)
	Queried map[string]struct{}

	// Responded is a map of all peers that responded during search
	Responded map[string]peer.Peer

	// Errored contains the errors received by each peer (via string representation of peer ID)
	Errored map[string]error

	// FatalErr is a fatal error that occurred during the search
	FatalErr error
}

Result holds search's (intermediate) result: collections of peers and possibly the value.

func NewInitialResult

func NewInitialResult(key id.ID, params *Parameters) *Result

NewInitialResult creates a new Result object for the beginning of a search.

func (*Result) MarshalLogObject

func (r *Result) MarshalLogObject(oe zapcore.ObjectEncoder) error

MarshalLogObject converts the Result into an object (which will become json) for logging.

type Search struct {
	// ID search is looking for or close to
	Key id.ID

	// CreatRq creates new Find requests
	CreatRq func() *api.FindRequest

	// result of the search
	Result *Result

	// parameters defining the search
	Params *Parameters

	// mutex used to synchronizes reads and writes to this instance
	Mu sync.Mutex
}

Search contains things involved in a search for a particular target.

func NewSearch

func NewSearch(peerID, orgID ecid.ID, key id.ID, params *Parameters) *Search

NewSearch creates a new Search instance for a given target, search type, and search parameters.

func (*Search) AddQueried added in v0.2.0

func (s *Search) AddQueried(p peer.Peer)

AddQueried adds a peer to the queried set.

func (*Search) Errored

func (s *Search) Errored() bool

Errored returns whether the search has encountered too many errors when querying the peers.

func (*Search) Exhausted

func (s *Search) Exhausted() bool

Exhausted returns whether the search has exhausted all unqueried peers close to the target.

func (*Search) Finished

func (s *Search) Finished() bool

Finished returns whether the search has finished, either because it has found the target or closest peers or errored or exhausted the list of peers to query. This operation is concurrency safe.

func (*Search) FoundClosestPeers

func (s *Search) FoundClosestPeers() bool

FoundClosestPeers returns whether the search has found the closest peers to a target. This event occurs when it has received responses from the required number of peers, and the max distance of those peers to the target is less than the min distance of the peers we haven't queried yet.

func (*Search) FoundValue

func (s *Search) FoundValue() bool

FoundValue returns whether the search has found the target value.

func (*Search) MarshalLogObject

func (s *Search) MarshalLogObject(oe zapcore.ObjectEncoder) error

MarshalLogObject converts the Search into an object (which will become json) for logging.

type Searcher

type Searcher interface {
	// Search executes a search from a list of seeds.
	Search(search *Search, seeds []peer.Peer) error
}

Searcher executes searches for particular keys.

func NewDefaultSearcher

func NewDefaultSearcher(
	peerSigner client.Signer,
	orgSigner client.Signer,
	rec comm.QueryRecorder,
	doc comm.Doctor,
	clients client.Pool,
) Searcher

NewDefaultSearcher creates a new Searcher with default sub-object instantiations.

func NewSearcher

func NewSearcher(
	peerSigner client.Signer,
	orgSigner client.Signer,
	rec comm.QueryRecorder,
	doc comm.Doctor,
	c client.FinderCreator,
	rp ResponseProcessor,
) Searcher

NewSearcher returns a new Searcher with the given Querier and ResponseProcessor.

func NewTestSearcher

func NewTestSearcher(
	peersMap map[string]peer.Peer,
	peerConnectedAddrs map[string][]*api.PeerAddress,
	rec comm.QueryRecorder,
) Searcher

NewTestSearcher creates a new Searcher instance with a FindCreator and FindResponseProcessor that each just return fixed addresses and peers, respectively.

type TestFinderCreator

type TestFinderCreator struct {
	// contains filtered or unexported fields
}

TestFinderCreator mocks the FindQuerier interface. The Create() method returns a fixed api.FindPeersResponse, derived from a list of addresses in the client.

func (*TestFinderCreator) Create

func (c *TestFinderCreator) Create(address string) (api.Finder, error)

Create creates an api.Finder that mocks a real query to a peer and returns a fixed list of addresses stored in the TestConnector mock of the pConn api.Connector.

type TestFromer

type TestFromer struct {
	Peers map[string]peer.Peer
}

TestFromer mocks the Fromer interface. The FromAPI() method returns a pre-stored peer for that ID, allowing us to circumvent the creation of new peer.Peer and api.Connector objects and use existing test peers with their testConnector (mocking api.Connector) values instead.

func (*TestFromer) FromAPI

func (f *TestFromer) FromAPI(apiAddress *api.PeerAddress) peer.Peer

FromAPI mocks creating a new peer.Peer instance, instead looking up an existing peer stored in the TestFromer instance.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL