2018-04-03 20:00:35 +02:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
2018-04-05 17:35:57 +02:00
|
|
|
"time"
|
2018-04-03 20:00:35 +02:00
|
|
|
|
|
|
|
"github.com/lbryio/errors.go"
|
|
|
|
"github.com/lbryio/lbry.go/stopOnce"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type nodeFinder struct {
|
|
|
|
findValue bool // true if we're using findValue
|
2018-04-05 22:05:28 +02:00
|
|
|
target Bitmap
|
2018-04-03 20:00:35 +02:00
|
|
|
dht *DHT
|
|
|
|
|
|
|
|
done *stopOnce.Stopper
|
|
|
|
|
|
|
|
findValueMutex *sync.Mutex
|
|
|
|
findValueResult []Node
|
|
|
|
|
|
|
|
activeNodesMutex *sync.Mutex
|
|
|
|
activeNodes []Node
|
|
|
|
|
2018-04-05 17:35:57 +02:00
|
|
|
shortlistMutex *sync.Mutex
|
|
|
|
shortlist []Node
|
2018-04-05 22:05:28 +02:00
|
|
|
shortlistAdded map[Bitmap]bool
|
|
|
|
|
|
|
|
outstandingRequestsMutex *sync.RWMutex
|
|
|
|
outstandingRequests uint
|
2018-04-03 20:00:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type findNodeResponse struct {
|
|
|
|
Found bool
|
|
|
|
Nodes []Node
|
|
|
|
}
|
|
|
|
|
2018-04-05 22:05:28 +02:00
|
|
|
func newNodeFinder(dht *DHT, target Bitmap, findValue bool) *nodeFinder {
|
2018-04-03 20:00:35 +02:00
|
|
|
return &nodeFinder{
|
2018-04-05 17:35:57 +02:00
|
|
|
dht: dht,
|
|
|
|
target: target,
|
|
|
|
findValue: findValue,
|
|
|
|
findValueMutex: &sync.Mutex{},
|
|
|
|
activeNodesMutex: &sync.Mutex{},
|
|
|
|
shortlistMutex: &sync.Mutex{},
|
2018-04-05 22:05:28 +02:00
|
|
|
shortlistAdded: make(map[Bitmap]bool),
|
2018-04-05 17:35:57 +02:00
|
|
|
done: stopOnce.New(),
|
2018-04-05 22:05:28 +02:00
|
|
|
outstandingRequestsMutex: &sync.RWMutex{},
|
2018-04-03 20:00:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nf *nodeFinder) Find() (findNodeResponse, error) {
|
2018-04-05 22:05:28 +02:00
|
|
|
if nf.findValue {
|
|
|
|
log.Debugf("[%s] starting an iterative Find for the value %s", nf.dht.node.id.HexShort(), nf.target.HexShort())
|
|
|
|
} else {
|
|
|
|
log.Debugf("[%s] starting an iterative Find for nodes near %s", nf.dht.node.id.HexShort(), nf.target.HexShort())
|
|
|
|
}
|
2018-04-03 20:00:35 +02:00
|
|
|
nf.appendNewToShortlist(nf.dht.rt.GetClosest(nf.target, alpha))
|
|
|
|
if len(nf.shortlist) == 0 {
|
|
|
|
return findNodeResponse{}, errors.Err("no nodes in routing table")
|
|
|
|
}
|
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
|
|
|
|
for i := 0; i < alpha; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
nf.iterationWorker(i + 1)
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// TODO: what to do if we have less than K active nodes, shortlist is empty, but we
|
2018-04-05 22:05:28 +02:00
|
|
|
// TODO: have other nodes in our routing table whom we have not contacted. prolly contact them
|
2018-04-03 20:00:35 +02:00
|
|
|
|
|
|
|
result := findNodeResponse{}
|
|
|
|
if nf.findValue && len(nf.findValueResult) > 0 {
|
|
|
|
result.Found = true
|
|
|
|
result.Nodes = nf.findValueResult
|
|
|
|
} else {
|
|
|
|
result.Nodes = nf.activeNodes
|
|
|
|
if len(result.Nodes) > bucketSize {
|
|
|
|
result.Nodes = result.Nodes[:bucketSize]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nf *nodeFinder) iterationWorker(num int) {
|
|
|
|
log.Debugf("[%s] starting worker %d", nf.dht.node.id.HexShort(), num)
|
|
|
|
defer func() { log.Debugf("[%s] stopping worker %d", nf.dht.node.id.HexShort(), num) }()
|
|
|
|
|
|
|
|
for {
|
|
|
|
maybeNode := nf.popFromShortlist()
|
|
|
|
if maybeNode == nil {
|
|
|
|
// TODO: block if there are pending requests out from other workers. there may be more shortlist values coming
|
2018-04-05 22:05:28 +02:00
|
|
|
log.Debugf("[%s] worker %d: no nodes in shortlist, waiting...", nf.dht.node.id.HexShort(), num)
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2018-04-03 20:00:35 +02:00
|
|
|
} else {
|
2018-04-05 17:35:57 +02:00
|
|
|
node := *maybeNode
|
|
|
|
|
|
|
|
if node.id.Equals(nf.dht.node.id) {
|
|
|
|
continue // cannot contact self
|
|
|
|
}
|
|
|
|
|
|
|
|
req := Request{Arg: &nf.target}
|
|
|
|
if nf.findValue {
|
|
|
|
req.Method = findValueMethod
|
|
|
|
} else {
|
|
|
|
req.Method = findNodeMethod
|
|
|
|
}
|
|
|
|
|
2018-04-05 22:05:28 +02:00
|
|
|
log.Debugf("[%s] worker %d: contacting %s", nf.dht.node.id.HexShort(), num, node.id.HexShort())
|
|
|
|
|
|
|
|
nf.incrementOutstanding()
|
2018-04-05 17:35:57 +02:00
|
|
|
|
|
|
|
var res *Response
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
resCh := nf.dht.tm.SendAsync(ctx, node, req)
|
|
|
|
select {
|
|
|
|
case res = <-resCh:
|
|
|
|
case <-nf.done.Chan():
|
|
|
|
log.Debugf("[%s] worker %d: canceled", nf.dht.node.id.HexShort(), num)
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if res == nil {
|
|
|
|
// nothing to do, response timed out
|
2018-04-05 22:05:28 +02:00
|
|
|
log.Debugf("[%s] worker %d: timed out waiting for %s", nf.dht.node.id.HexShort(), num, node.id.HexShort())
|
2018-04-05 17:35:57 +02:00
|
|
|
} else if nf.findValue && res.FindValueKey != "" {
|
|
|
|
log.Debugf("[%s] worker %d: got value", nf.dht.node.id.HexShort(), num)
|
|
|
|
nf.findValueMutex.Lock()
|
|
|
|
nf.findValueResult = res.FindNodeData
|
|
|
|
nf.findValueMutex.Unlock()
|
|
|
|
nf.done.Stop()
|
|
|
|
return
|
|
|
|
} else {
|
2018-04-05 22:05:28 +02:00
|
|
|
log.Debugf("[%s] worker %d: got contacts", nf.dht.node.id.HexShort(), num)
|
2018-04-05 17:35:57 +02:00
|
|
|
nf.insertIntoActiveList(node)
|
|
|
|
nf.appendNewToShortlist(res.FindNodeData)
|
|
|
|
}
|
2018-04-05 22:05:28 +02:00
|
|
|
|
|
|
|
nf.decrementOutstanding() // this is all the way down here because we need to add to shortlist first
|
2018-04-03 20:00:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if nf.isSearchFinished() {
|
|
|
|
log.Debugf("[%s] worker %d: search is finished", nf.dht.node.id.HexShort(), num)
|
|
|
|
nf.done.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nf *nodeFinder) appendNewToShortlist(nodes []Node) {
|
2018-04-05 17:35:57 +02:00
|
|
|
nf.shortlistMutex.Lock()
|
|
|
|
defer nf.shortlistMutex.Unlock()
|
2018-04-03 20:00:35 +02:00
|
|
|
|
|
|
|
for _, n := range nodes {
|
2018-04-05 17:35:57 +02:00
|
|
|
if _, ok := nf.shortlistAdded[n.id]; !ok {
|
|
|
|
nf.shortlist = append(nf.shortlist, n)
|
|
|
|
nf.shortlistAdded[n.id] = true
|
2018-04-03 20:00:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sortNodesInPlace(nf.shortlist, nf.target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nf *nodeFinder) popFromShortlist() *Node {
|
2018-04-05 17:35:57 +02:00
|
|
|
nf.shortlistMutex.Lock()
|
|
|
|
defer nf.shortlistMutex.Unlock()
|
2018-04-03 20:00:35 +02:00
|
|
|
|
|
|
|
if len(nf.shortlist) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
first := nf.shortlist[0]
|
|
|
|
nf.shortlist = nf.shortlist[1:]
|
|
|
|
return &first
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nf *nodeFinder) insertIntoActiveList(node Node) {
|
|
|
|
nf.activeNodesMutex.Lock()
|
|
|
|
defer nf.activeNodesMutex.Unlock()
|
|
|
|
|
|
|
|
inserted := false
|
|
|
|
for i, n := range nf.activeNodes {
|
|
|
|
if node.id.Xor(nf.target).Less(n.id.Xor(nf.target)) {
|
|
|
|
nf.activeNodes = append(nf.activeNodes[:i], append([]Node{node}, nf.activeNodes[i:]...)...)
|
|
|
|
inserted = true
|
2018-04-05 17:35:57 +02:00
|
|
|
break
|
2018-04-03 20:00:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !inserted {
|
|
|
|
nf.activeNodes = append(nf.activeNodes, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nf *nodeFinder) isSearchFinished() bool {
|
|
|
|
if nf.findValue && len(nf.findValueResult) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-nf.done.Chan():
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-04-05 22:05:28 +02:00
|
|
|
if !nf.areRequestsOutstanding() {
|
|
|
|
nf.shortlistMutex.Lock()
|
|
|
|
defer nf.shortlistMutex.Unlock()
|
2018-04-03 20:00:35 +02:00
|
|
|
|
2018-04-05 22:05:28 +02:00
|
|
|
if len(nf.shortlist) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2018-04-03 20:00:35 +02:00
|
|
|
|
2018-04-05 22:05:28 +02:00
|
|
|
nf.activeNodesMutex.Lock()
|
|
|
|
defer nf.activeNodesMutex.Unlock()
|
2018-04-03 20:00:35 +02:00
|
|
|
|
2018-04-05 22:05:28 +02:00
|
|
|
if len(nf.activeNodes) >= bucketSize && nf.activeNodes[bucketSize-1].id.Xor(nf.target).Less(nf.shortlist[0].id.Xor(nf.target)) {
|
|
|
|
// we have at least K active nodes, and we don't have any closer nodes yet to contact
|
|
|
|
return true
|
|
|
|
}
|
2018-04-03 20:00:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2018-04-05 22:05:28 +02:00
|
|
|
|
|
|
|
func (nf *nodeFinder) incrementOutstanding() {
|
|
|
|
nf.outstandingRequestsMutex.Lock()
|
|
|
|
defer nf.outstandingRequestsMutex.Unlock()
|
|
|
|
nf.outstandingRequests++
|
|
|
|
}
|
|
|
|
func (nf *nodeFinder) decrementOutstanding() {
|
|
|
|
nf.outstandingRequestsMutex.Lock()
|
|
|
|
defer nf.outstandingRequestsMutex.Unlock()
|
|
|
|
if nf.outstandingRequests > 0 {
|
|
|
|
nf.outstandingRequests--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (nf *nodeFinder) areRequestsOutstanding() bool {
|
|
|
|
nf.outstandingRequestsMutex.RLock()
|
|
|
|
defer nf.outstandingRequestsMutex.RUnlock()
|
|
|
|
return nf.outstandingRequests > 0
|
|
|
|
}
|