2014-09-17 07:19:40 -07:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
2014-09-18 19:30:04 -07:00
|
|
|
"sync"
|
|
|
|
|
2014-10-21 03:13:10 -07:00
|
|
|
inet "github.com/jbenet/go-ipfs/net"
|
2014-09-17 07:19:40 -07:00
|
|
|
peer "github.com/jbenet/go-ipfs/peer"
|
|
|
|
queue "github.com/jbenet/go-ipfs/peer/queue"
|
2014-10-28 02:17:46 -07:00
|
|
|
"github.com/jbenet/go-ipfs/routing"
|
2014-09-18 19:30:04 -07:00
|
|
|
kb "github.com/jbenet/go-ipfs/routing/kbucket"
|
2014-09-17 07:19:40 -07:00
|
|
|
u "github.com/jbenet/go-ipfs/util"
|
2014-09-18 19:30:04 -07:00
|
|
|
todoctr "github.com/jbenet/go-ipfs/util/todocounter"
|
2014-09-17 07:19:40 -07:00
|
|
|
|
|
|
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
2014-12-24 02:13:38 -08:00
|
|
|
ctxgroup "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
|
2014-09-17 07:19:40 -07:00
|
|
|
)
|
|
|
|
|
2014-09-19 05:23:57 -07:00
|
|
|
var maxQueryConcurrency = AlphaValue
|
2014-09-18 19:30:04 -07:00
|
|
|
|
2014-09-17 07:19:40 -07:00
|
|
|
type dhtQuery struct {
|
2014-09-18 19:30:04 -07:00
|
|
|
// the key we're querying for
|
|
|
|
key u.Key
|
2014-09-17 07:19:40 -07:00
|
|
|
|
2014-10-21 01:18:38 -07:00
|
|
|
// dialer used to ensure we're connected to peers
|
2014-10-21 03:13:10 -07:00
|
|
|
dialer inet.Dialer
|
2014-10-21 01:18:38 -07:00
|
|
|
|
2014-09-17 07:19:40 -07:00
|
|
|
// the function to execute per peer
|
|
|
|
qfunc queryFunc
|
2014-09-18 19:30:04 -07:00
|
|
|
|
|
|
|
// the concurrency parameter
|
|
|
|
concurrency int
|
|
|
|
}
|
|
|
|
|
|
|
|
type dhtQueryResult struct {
|
2014-12-19 12:19:56 -08:00
|
|
|
value []byte // GetValue
|
|
|
|
peer peer.PeerInfo // FindPeer
|
|
|
|
providerPeers []peer.PeerInfo // GetProviders
|
|
|
|
closerPeers []peer.PeerInfo // *
|
2014-09-18 19:30:04 -07:00
|
|
|
success bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// constructs query
|
2014-10-21 03:13:10 -07:00
|
|
|
func newQuery(k u.Key, d inet.Dialer, f queryFunc) *dhtQuery {
|
2014-09-18 19:30:04 -07:00
|
|
|
return &dhtQuery{
|
|
|
|
key: k,
|
2014-10-21 01:18:38 -07:00
|
|
|
dialer: d,
|
2014-09-18 19:30:04 -07:00
|
|
|
qfunc: f,
|
|
|
|
concurrency: maxQueryConcurrency,
|
|
|
|
}
|
2014-09-17 07:19:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryFunc is a function that runs a particular query with a given peer.
|
|
|
|
// It returns either:
|
|
|
|
// - the value
|
|
|
|
// - a list of peers potentially better able to serve the query
|
|
|
|
// - an error
|
2014-12-19 12:19:56 -08:00
|
|
|
type queryFunc func(context.Context, peer.ID) (*dhtQueryResult, error)
|
2014-09-18 19:30:04 -07:00
|
|
|
|
|
|
|
// Run runs the query at hand. pass in a list of peers to use first.
|
2014-12-19 12:19:56 -08:00
|
|
|
func (q *dhtQuery) Run(ctx context.Context, peers []peer.ID) (*dhtQueryResult, error) {
|
2014-09-18 19:30:04 -07:00
|
|
|
runner := newQueryRunner(ctx, q)
|
|
|
|
return runner.Run(peers)
|
|
|
|
}
|
|
|
|
|
|
|
|
type dhtQueryRunner struct {
|
|
|
|
|
|
|
|
// the query to run
|
|
|
|
query *dhtQuery
|
|
|
|
|
|
|
|
// peersToQuery is a list of peers remaining to query
|
|
|
|
peersToQuery *queue.ChanQueue
|
|
|
|
|
|
|
|
// peersSeen are all the peers queried. used to prevent querying same peer 2x
|
2014-12-19 12:19:56 -08:00
|
|
|
peersSeen peer.Set
|
2014-09-17 07:19:40 -07:00
|
|
|
|
2014-09-18 19:30:04 -07:00
|
|
|
// rateLimit is a channel used to rate limit our processing (semaphore)
|
|
|
|
rateLimit chan struct{}
|
|
|
|
|
|
|
|
// peersRemaining is a counter of peers remaining (toQuery + processing)
|
|
|
|
peersRemaining todoctr.Counter
|
|
|
|
|
2014-12-24 02:13:38 -08:00
|
|
|
// context group
|
|
|
|
cg ctxgroup.ContextGroup
|
2014-09-18 19:30:04 -07:00
|
|
|
|
|
|
|
// result
|
|
|
|
result *dhtQueryResult
|
|
|
|
|
|
|
|
// result errors
|
|
|
|
errs []error
|
|
|
|
|
|
|
|
// lock for concurrent access to fields
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func newQueryRunner(ctx context.Context, q *dhtQuery) *dhtQueryRunner {
|
|
|
|
return &dhtQueryRunner{
|
|
|
|
query: q,
|
|
|
|
peersToQuery: queue.NewChanQueue(ctx, queue.NewXORDistancePQ(q.key)),
|
|
|
|
peersRemaining: todoctr.NewSyncCounter(),
|
2014-12-19 12:19:56 -08:00
|
|
|
peersSeen: peer.Set{},
|
2014-09-18 19:30:04 -07:00
|
|
|
rateLimit: make(chan struct{}, q.concurrency),
|
2014-12-24 02:13:38 -08:00
|
|
|
cg: ctxgroup.WithContext(ctx),
|
2014-09-18 19:30:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
func (r *dhtQueryRunner) Run(peers []peer.ID) (*dhtQueryResult, error) {
|
2014-10-30 06:35:29 -07:00
|
|
|
log.Debugf("Run query with %d peers.", len(peers))
|
2014-09-25 15:10:57 -07:00
|
|
|
if len(peers) == 0 {
|
|
|
|
log.Warning("Running query with no peers!")
|
|
|
|
return nil, nil
|
|
|
|
}
|
2014-09-25 18:29:05 -07:00
|
|
|
|
2014-09-18 19:30:04 -07:00
|
|
|
// setup concurrency rate limiting
|
|
|
|
for i := 0; i < r.query.concurrency; i++ {
|
|
|
|
r.rateLimit <- struct{}{}
|
2014-09-17 07:19:40 -07:00
|
|
|
}
|
|
|
|
|
2014-09-18 19:30:04 -07:00
|
|
|
// add all the peers we got first.
|
|
|
|
for _, p := range peers {
|
2014-12-24 02:13:38 -08:00
|
|
|
r.addPeerToQuery(r.cg.Context(), p, "") // don't have access to self here...
|
2014-09-17 07:19:40 -07:00
|
|
|
}
|
|
|
|
|
2014-09-18 19:41:41 -07:00
|
|
|
// go do this thing.
|
2014-12-24 02:13:38 -08:00
|
|
|
// do it as a child func to make sure Run exits
|
|
|
|
// ONLY AFTER spawn workers has exited.
|
|
|
|
r.cg.AddChildFunc(r.spawnWorkers)
|
2014-09-18 19:41:41 -07:00
|
|
|
|
|
|
|
// so workers are working.
|
|
|
|
|
|
|
|
// wait until they're done.
|
2014-10-28 02:17:46 -07:00
|
|
|
err := routing.ErrNotFound
|
2014-09-19 08:07:56 -07:00
|
|
|
|
2014-09-17 07:19:40 -07:00
|
|
|
select {
|
2014-09-18 19:30:04 -07:00
|
|
|
case <-r.peersRemaining.Done():
|
2014-12-24 02:13:38 -08:00
|
|
|
r.cg.Close()
|
2014-09-18 19:30:04 -07:00
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
|
|
|
if len(r.errs) > 0 {
|
2014-09-19 08:07:56 -07:00
|
|
|
err = r.errs[0]
|
2014-09-18 19:30:04 -07:00
|
|
|
}
|
|
|
|
|
2014-12-24 02:13:38 -08:00
|
|
|
case <-r.cg.Closed():
|
2014-09-18 19:30:04 -07:00
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
2014-12-24 02:13:38 -08:00
|
|
|
err = r.cg.Context().Err() // collect the error.
|
2014-09-19 08:07:56 -07:00
|
|
|
}
|
2014-09-18 19:30:04 -07:00
|
|
|
|
2014-09-19 08:07:56 -07:00
|
|
|
if r.result != nil && r.result.success {
|
|
|
|
return r.result, nil
|
2014-09-18 19:30:04 -07:00
|
|
|
}
|
|
|
|
|
2014-09-19 08:07:56 -07:00
|
|
|
return nil, err
|
2014-09-18 19:30:04 -07:00
|
|
|
}
|
|
|
|
|
2014-12-24 02:13:38 -08:00
|
|
|
func (r *dhtQueryRunner) addPeerToQuery(ctx context.Context, next peer.ID, benchmark peer.ID) {
|
2014-11-21 08:03:11 -08:00
|
|
|
// if new peer is ourselves...
|
2014-12-19 12:19:56 -08:00
|
|
|
if next == r.query.dialer.LocalPeer() {
|
2014-11-21 08:03:11 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// if new peer further away than whom we got it from, don't bother (loops)
|
2014-12-19 12:19:56 -08:00
|
|
|
// TODO----------- this benchmark should be replaced by a heap:
|
|
|
|
// we should be doing the s/kademlia "continue to search"
|
|
|
|
// (i.e. put all of them in a heap sorted by dht distance and then just
|
|
|
|
// pull from the the top until a) you exhaust all peers you get,
|
|
|
|
// b) you succeed, c) your context expires.
|
|
|
|
if benchmark != "" && kb.Closer(benchmark, next, r.query.key) {
|
2014-09-18 19:30:04 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// if already seen, no need.
|
|
|
|
r.Lock()
|
2014-12-19 12:19:56 -08:00
|
|
|
_, found := r.peersSeen[next]
|
2014-09-18 19:30:04 -07:00
|
|
|
if found {
|
|
|
|
r.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2014-12-19 12:19:56 -08:00
|
|
|
r.peersSeen[next] = struct{}{}
|
2014-09-18 19:30:04 -07:00
|
|
|
r.Unlock()
|
|
|
|
|
2014-10-30 06:35:29 -07:00
|
|
|
log.Debugf("adding peer to query: %v\n", next)
|
2014-09-18 19:41:41 -07:00
|
|
|
|
2014-09-18 19:30:04 -07:00
|
|
|
// do this after unlocking to prevent possible deadlocks.
|
|
|
|
r.peersRemaining.Increment(1)
|
|
|
|
select {
|
|
|
|
case r.peersToQuery.EnqChan <- next:
|
2014-12-24 02:13:38 -08:00
|
|
|
case <-ctx.Done():
|
2014-09-17 07:19:40 -07:00
|
|
|
}
|
2014-09-18 19:30:04 -07:00
|
|
|
}
|
|
|
|
|
2014-12-24 02:13:38 -08:00
|
|
|
func (r *dhtQueryRunner) spawnWorkers(parent ctxgroup.ContextGroup) {
|
2014-09-18 19:30:04 -07:00
|
|
|
for {
|
2014-09-18 19:41:41 -07:00
|
|
|
|
2014-09-18 19:30:04 -07:00
|
|
|
select {
|
|
|
|
case <-r.peersRemaining.Done():
|
|
|
|
return
|
|
|
|
|
2014-12-24 02:13:38 -08:00
|
|
|
case <-r.cg.Closing():
|
2014-09-18 19:30:04 -07:00
|
|
|
return
|
|
|
|
|
2014-09-18 19:41:41 -07:00
|
|
|
case p, more := <-r.peersToQuery.DeqChan:
|
|
|
|
if !more {
|
|
|
|
return // channel closed.
|
|
|
|
}
|
2014-12-24 02:13:38 -08:00
|
|
|
log.Debugf("spawning worker for: %v", p)
|
|
|
|
|
|
|
|
// do it as a child func to make sure Run exits
|
|
|
|
// ONLY AFTER spawn workers has exited.
|
|
|
|
parent.AddChildFunc(func(cg ctxgroup.ContextGroup) {
|
|
|
|
r.queryPeer(cg, p)
|
|
|
|
})
|
2014-09-18 19:30:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-17 07:19:40 -07:00
|
|
|
|
2014-12-24 02:13:38 -08:00
|
|
|
func (r *dhtQueryRunner) queryPeer(cg ctxgroup.ContextGroup, p peer.ID) {
|
2014-10-31 03:20:26 +00:00
|
|
|
log.Debugf("spawned worker for: %v", p)
|
2014-09-18 19:41:41 -07:00
|
|
|
|
2014-09-18 19:30:04 -07:00
|
|
|
// make sure we rate limit concurrency.
|
|
|
|
select {
|
|
|
|
case <-r.rateLimit:
|
2014-12-24 02:13:38 -08:00
|
|
|
case <-cg.Closing():
|
2014-09-18 19:30:04 -07:00
|
|
|
r.peersRemaining.Decrement(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-21 01:18:38 -07:00
|
|
|
// ok let's do this!
|
2014-10-30 06:35:29 -07:00
|
|
|
log.Debugf("running worker for: %v", p)
|
2014-10-21 01:18:38 -07:00
|
|
|
|
|
|
|
// make sure we do this when we exit
|
|
|
|
defer func() {
|
|
|
|
// signal we're done proccessing peer p
|
2014-10-30 06:35:29 -07:00
|
|
|
log.Debugf("completing worker for: %v", p)
|
2014-10-21 01:18:38 -07:00
|
|
|
r.peersRemaining.Decrement(1)
|
|
|
|
r.rateLimit <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// make sure we're connected to the peer.
|
2014-12-24 02:13:38 -08:00
|
|
|
err := r.query.dialer.DialPeer(cg.Context(), p)
|
2014-10-21 01:18:38 -07:00
|
|
|
if err != nil {
|
2014-10-30 06:35:29 -07:00
|
|
|
log.Debugf("ERROR worker for: %v -- err connecting: %v", p, err)
|
2014-10-21 01:18:38 -07:00
|
|
|
r.Lock()
|
|
|
|
r.errs = append(r.errs, err)
|
|
|
|
r.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2014-09-18 19:41:41 -07:00
|
|
|
|
2014-09-18 19:30:04 -07:00
|
|
|
// finally, run the query against this peer
|
2014-12-24 02:13:38 -08:00
|
|
|
res, err := r.query.qfunc(cg.Context(), p)
|
2014-09-18 19:30:04 -07:00
|
|
|
|
|
|
|
if err != nil {
|
2014-10-30 06:35:29 -07:00
|
|
|
log.Debugf("ERROR worker for: %v %v", p, err)
|
2014-09-18 19:30:04 -07:00
|
|
|
r.Lock()
|
|
|
|
r.errs = append(r.errs, err)
|
|
|
|
r.Unlock()
|
|
|
|
|
|
|
|
} else if res.success {
|
2014-10-30 06:35:29 -07:00
|
|
|
log.Debugf("SUCCESS worker for: %v", p, res)
|
2014-09-18 19:30:04 -07:00
|
|
|
r.Lock()
|
|
|
|
r.result = res
|
|
|
|
r.Unlock()
|
2014-12-24 02:13:38 -08:00
|
|
|
go r.cg.Close() // signal to everyone that we're done.
|
|
|
|
// must be async, as we're one of the children, and Close blocks.
|
2014-09-18 19:30:04 -07:00
|
|
|
|
2014-12-23 03:14:30 -08:00
|
|
|
} else if len(res.closerPeers) > 0 {
|
|
|
|
log.Debugf("PEERS CLOSER -- worker for: %v (%d closer peers)", p, len(res.closerPeers))
|
2014-09-18 19:30:04 -07:00
|
|
|
for _, next := range res.closerPeers {
|
2014-12-19 12:19:56 -08:00
|
|
|
// add their addresses to the dialer's peerstore
|
2014-12-24 02:13:38 -08:00
|
|
|
conns := r.query.dialer.ConnsToPeer(next.ID)
|
|
|
|
if len(conns) == 0 {
|
|
|
|
log.Infof("PEERS CLOSER -- worker for %v FOUND NEW PEER: %s %s", p, next.ID, next.Addrs)
|
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
r.query.dialer.Peerstore().AddAddresses(next.ID, next.Addrs)
|
2014-12-24 02:13:38 -08:00
|
|
|
r.addPeerToQuery(cg.Context(), next.ID, p)
|
2014-12-23 03:14:30 -08:00
|
|
|
log.Debugf("PEERS CLOSER -- worker for: %v added %v (%v)", p, next.ID, next.Addrs)
|
2014-09-18 19:30:04 -07:00
|
|
|
}
|
2014-12-23 03:14:30 -08:00
|
|
|
} else {
|
|
|
|
log.Debugf("QUERY worker for: %v - not found, and no closer peers.", p)
|
2014-09-17 07:19:40 -07:00
|
|
|
}
|
|
|
|
}
|