2020-09-07 18:31:43 +00:00
|
|
|
package exchange
|
2020-08-05 22:47:27 +00:00
|
|
|
|
2020-07-27 15:31:36 +00:00
|
|
|
// FIXME: This needs to be reviewed.
|
|
|
|
|
|
|
|
import (
|
2020-09-08 08:18:51 +00:00
|
|
|
"context"
|
2020-07-27 15:31:36 +00:00
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-09-08 08:18:51 +00:00
|
|
|
host "github.com/libp2p/go-libp2p-core/host"
|
2020-07-27 15:31:36 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2020-09-08 08:18:51 +00:00
|
|
|
"go.uber.org/fx"
|
2020-07-27 15:31:36 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/lib/peermgr"
|
|
|
|
)
|
|
|
|
|
|
|
|
type peerStats struct {
|
|
|
|
successes int
|
|
|
|
failures int
|
|
|
|
firstSeen time.Time
|
|
|
|
averageTime time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type bsPeerTracker struct {
|
|
|
|
lk sync.Mutex
|
|
|
|
|
|
|
|
peers map[peer.ID]*peerStats
|
|
|
|
avgGlobalTime time.Duration
|
|
|
|
|
|
|
|
pmgr *peermgr.PeerMgr
|
|
|
|
}
|
|
|
|
|
2020-09-08 08:18:51 +00:00
|
|
|
func newPeerTracker(lc fx.Lifecycle, h host.Host, pmgr *peermgr.PeerMgr) *bsPeerTracker {
|
|
|
|
bsPt := &bsPeerTracker{
|
2020-07-27 15:31:36 +00:00
|
|
|
peers: make(map[peer.ID]*peerStats),
|
|
|
|
pmgr: pmgr,
|
|
|
|
}
|
2020-09-08 08:18:51 +00:00
|
|
|
|
2020-11-20 14:15:44 +00:00
|
|
|
evtSub, err := h.EventBus().Subscribe(new(peermgr.FilPeerEvt))
|
2020-09-08 08:18:51 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-09-08 10:11:30 +00:00
|
|
|
|
2020-09-08 08:18:51 +00:00
|
|
|
go func() {
|
2020-11-20 14:15:44 +00:00
|
|
|
for evt := range evtSub.Out() {
|
|
|
|
pEvt := evt.(peermgr.FilPeerEvt)
|
|
|
|
switch pEvt.Type {
|
|
|
|
case peermgr.AddFilPeerEvt:
|
|
|
|
bsPt.addPeer(pEvt.ID)
|
|
|
|
case peermgr.RemoveFilPeerEvt:
|
|
|
|
bsPt.removePeer(pEvt.ID)
|
|
|
|
}
|
2020-11-06 16:26:14 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-09-08 08:18:51 +00:00
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(ctx context.Context) error {
|
2020-11-20 14:15:44 +00:00
|
|
|
return evtSub.Close()
|
2020-09-08 08:18:51 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return bsPt
|
2020-07-27 15:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bpt *bsPeerTracker) addPeer(p peer.ID) {
|
|
|
|
bpt.lk.Lock()
|
|
|
|
defer bpt.lk.Unlock()
|
|
|
|
if _, ok := bpt.peers[p]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bpt.peers[p] = &peerStats{
|
|
|
|
firstSeen: build.Clock.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// newPeerMul is how much better than average is the new peer assumed to be
|
|
|
|
// less than one to encourouge trying new peers
|
|
|
|
newPeerMul = 0.9
|
|
|
|
)
|
|
|
|
|
|
|
|
func (bpt *bsPeerTracker) prefSortedPeers() []peer.ID {
|
|
|
|
// TODO: this could probably be cached, but as long as its not too many peers, fine for now
|
|
|
|
bpt.lk.Lock()
|
|
|
|
defer bpt.lk.Unlock()
|
|
|
|
out := make([]peer.ID, 0, len(bpt.peers))
|
|
|
|
for p := range bpt.peers {
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort by 'expected cost' of requesting data from that peer
|
|
|
|
// additionally handle edge cases where not enough data is available
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
pi := bpt.peers[out[i]]
|
|
|
|
pj := bpt.peers[out[j]]
|
|
|
|
|
|
|
|
var costI, costJ float64
|
|
|
|
|
|
|
|
getPeerInitLat := func(p peer.ID) float64 {
|
2020-09-08 06:59:05 +00:00
|
|
|
return float64(bpt.avgGlobalTime) * newPeerMul
|
2020-07-27 15:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if pi.successes+pi.failures > 0 {
|
|
|
|
failRateI := float64(pi.failures) / float64(pi.failures+pi.successes)
|
|
|
|
costI = float64(pi.averageTime) + failRateI*float64(bpt.avgGlobalTime)
|
|
|
|
} else {
|
|
|
|
costI = getPeerInitLat(out[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
if pj.successes+pj.failures > 0 {
|
|
|
|
failRateJ := float64(pj.failures) / float64(pj.failures+pj.successes)
|
|
|
|
costJ = float64(pj.averageTime) + failRateJ*float64(bpt.avgGlobalTime)
|
|
|
|
} else {
|
|
|
|
costJ = getPeerInitLat(out[j])
|
|
|
|
}
|
|
|
|
|
|
|
|
return costI < costJ
|
|
|
|
})
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// xInvAlpha = (N+1)/2
|
|
|
|
|
2020-09-08 07:06:31 +00:00
|
|
|
localInvAlpha = 10 // 86% of the value is the last 19
|
|
|
|
globalInvAlpha = 25 // 86% of the value is the last 49
|
2020-07-27 15:31:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (bpt *bsPeerTracker) logGlobalSuccess(dur time.Duration) {
|
|
|
|
bpt.lk.Lock()
|
|
|
|
defer bpt.lk.Unlock()
|
|
|
|
|
|
|
|
if bpt.avgGlobalTime == 0 {
|
|
|
|
bpt.avgGlobalTime = dur
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delta := (dur - bpt.avgGlobalTime) / globalInvAlpha
|
|
|
|
bpt.avgGlobalTime += delta
|
|
|
|
}
|
|
|
|
|
|
|
|
func logTime(pi *peerStats, dur time.Duration) {
|
|
|
|
if pi.averageTime == 0 {
|
|
|
|
pi.averageTime = dur
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delta := (dur - pi.averageTime) / localInvAlpha
|
|
|
|
pi.averageTime += delta
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-08 07:06:31 +00:00
|
|
|
func (bpt *bsPeerTracker) logSuccess(p peer.ID, dur time.Duration, reqSize uint64) {
|
2020-07-27 15:31:36 +00:00
|
|
|
bpt.lk.Lock()
|
|
|
|
defer bpt.lk.Unlock()
|
|
|
|
|
|
|
|
var pi *peerStats
|
|
|
|
var ok bool
|
|
|
|
if pi, ok = bpt.peers[p]; !ok {
|
|
|
|
log.Warnw("log success called on peer not in tracker", "peerid", p.String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pi.successes++
|
2020-09-08 10:19:37 +00:00
|
|
|
if reqSize == 0 {
|
|
|
|
reqSize = 1
|
|
|
|
}
|
2020-09-08 07:06:31 +00:00
|
|
|
logTime(pi, dur/time.Duration(reqSize))
|
2020-07-27 15:31:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 07:06:31 +00:00
|
|
|
func (bpt *bsPeerTracker) logFailure(p peer.ID, dur time.Duration, reqSize uint64) {
|
2020-07-27 15:31:36 +00:00
|
|
|
bpt.lk.Lock()
|
|
|
|
defer bpt.lk.Unlock()
|
|
|
|
|
|
|
|
var pi *peerStats
|
|
|
|
var ok bool
|
|
|
|
if pi, ok = bpt.peers[p]; !ok {
|
|
|
|
log.Warn("log failure called on peer not in tracker", "peerid", p.String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pi.failures++
|
2020-09-08 10:19:37 +00:00
|
|
|
if reqSize == 0 {
|
|
|
|
reqSize = 1
|
|
|
|
}
|
2020-09-08 07:06:31 +00:00
|
|
|
logTime(pi, dur/time.Duration(reqSize))
|
2020-07-27 15:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bpt *bsPeerTracker) removePeer(p peer.ID) {
|
|
|
|
bpt.lk.Lock()
|
|
|
|
defer bpt.lk.Unlock()
|
|
|
|
delete(bpt.peers, p)
|
|
|
|
}
|