FedP2P/requesting.go

288 lines
8.1 KiB
Go
Raw Normal View History

package torrent
import (
"container/heap"
"context"
"encoding/gob"
"math/rand"
"reflect"
"runtime/pprof"
"time"
2021-05-09 22:53:32 +08:00
"unsafe"
"github.com/anacrolix/log"
"github.com/anacrolix/multiless"
request_strategy "github.com/anacrolix/torrent/request-strategy"
)
func (t *Torrent) requestStrategyPieceOrderState(i int) request_strategy.PieceRequestOrderState {
return request_strategy.PieceRequestOrderState{
Priority: t.piece(i).purePriority(),
Partial: t.piecePartiallyDownloaded(i),
Availability: t.piece(i).availability,
}
}
func init() {
gob.Register(peerId{})
}
type peerId struct {
*Peer
ptr uintptr
}
2021-05-13 09:26:22 +08:00
func (p peerId) Uintptr() uintptr {
return p.ptr
2021-05-13 09:26:22 +08:00
}
func (p peerId) GobEncode() (b []byte, _ error) {
*(*reflect.SliceHeader)(unsafe.Pointer(&b)) = reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&p.ptr)),
Len: int(unsafe.Sizeof(p.ptr)),
Cap: int(unsafe.Sizeof(p.ptr)),
}
return
}
func (p *peerId) GobDecode(b []byte) error {
if uintptr(len(b)) != unsafe.Sizeof(p.ptr) {
panic(len(b))
}
ptr := unsafe.Pointer(&b[0])
p.ptr = *(*uintptr)(ptr)
log.Printf("%p", ptr)
dst := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&p.Peer)),
Len: int(unsafe.Sizeof(p.Peer)),
Cap: int(unsafe.Sizeof(p.Peer)),
}
copy(*(*[]byte)(unsafe.Pointer(&dst)), b)
return nil
}
2021-11-08 11:47:01 +08:00
type (
RequestIndex = request_strategy.RequestIndex
chunkIndexType = request_strategy.ChunkIndex
)
2021-09-19 13:16:37 +08:00
type peerRequests struct {
requestIndexes []RequestIndex
peer *Peer
}
func (p *peerRequests) Len() int {
return len(p.requestIndexes)
}
func (p *peerRequests) Less(i, j int) bool {
leftRequest := p.requestIndexes[i]
rightRequest := p.requestIndexes[j]
t := p.peer.t
leftPieceIndex := leftRequest / t.chunksPerRegularPiece()
rightPieceIndex := rightRequest / t.chunksPerRegularPiece()
leftCurrent := p.peer.actualRequestState.Requests.Contains(leftRequest)
rightCurrent := p.peer.actualRequestState.Requests.Contains(rightRequest)
pending := func(index RequestIndex, current bool) int {
2021-10-09 16:00:58 +08:00
ret := t.pendingRequests.Get(index)
if current {
ret--
}
// See https://github.com/anacrolix/torrent/issues/679 for possible issues. This should be
// resolved.
if ret < 0 {
panic(ret)
}
return ret
}
ml := multiless.New()
// Push requests that can't be served right now to the end. But we don't throw them away unless
// there's a better alternative. This is for when we're using the fast extension and get choked
// but our requests could still be good when we get unchoked.
if p.peer.peerChoking {
ml = ml.Bool(
!p.peer.peerAllowedFast.Contains(leftPieceIndex),
!p.peer.peerAllowedFast.Contains(rightPieceIndex),
)
}
ml = ml.Int(
pending(leftRequest, leftCurrent),
pending(rightRequest, rightCurrent))
ml = ml.Bool(!leftCurrent, !rightCurrent)
leftPiece := t.piece(int(leftPieceIndex))
rightPiece := t.piece(int(rightPieceIndex))
ml = ml.Int(
-int(leftPiece.purePriority()),
-int(rightPiece.purePriority()),
)
ml = ml.Int(
int(leftPiece.availability),
int(rightPiece.availability))
ml = ml.Uint32(leftPieceIndex, rightPieceIndex)
ml = ml.Uint32(leftRequest, rightRequest)
return ml.MustLess()
}
func (p *peerRequests) Swap(i, j int) {
p.requestIndexes[i], p.requestIndexes[j] = p.requestIndexes[j], p.requestIndexes[i]
}
func (p *peerRequests) Push(x interface{}) {
p.requestIndexes = append(p.requestIndexes, x.(RequestIndex))
}
func (p *peerRequests) Pop() interface{} {
last := len(p.requestIndexes) - 1
x := p.requestIndexes[last]
p.requestIndexes = p.requestIndexes[:last]
return x
}
type desiredRequestState struct {
Requests []RequestIndex
Interested bool
}
func (p *Peer) getDesiredRequestState() (desired desiredRequestState) {
input := p.t.getRequestStrategyInput()
requestHeap := peerRequests{
peer: p,
}
request_strategy.GetRequestablePieces(
input,
p.t.cl.pieceRequestOrder[p.t.storage.Capacity],
func(ih InfoHash, pieceIndex int) {
if ih != p.t.infoHash {
return
}
if !p.peerHasPiece(pieceIndex) {
return
}
allowedFast := p.peerAllowedFast.ContainsInt(pieceIndex)
p.t.piece(pieceIndex).undirtiedChunksIter.Iter(func(ci request_strategy.ChunkIndex) {
2021-10-25 17:53:39 +08:00
r := p.t.pieceRequestIndexOffset(pieceIndex) + ci
// if p.t.pendingRequests.Get(r) != 0 && !p.actualRequestState.Requests.Contains(r) {
// return
// }
if !allowedFast {
// We must signal interest to request this
desired.Interested = true
// We can make or will allow sustaining a request here if we're not choked, or
// have made the request previously (presumably while unchoked), and haven't had
// the peer respond yet (and the request was retained because we are using the
// fast extension).
2021-10-25 17:53:39 +08:00
if p.peerChoking && !p.actualRequestState.Requests.Contains(r) {
// We can't request this right now.
return
}
}
2021-10-25 17:53:39 +08:00
requestHeap.requestIndexes = append(requestHeap.requestIndexes, r)
})
},
)
p.t.assertPendingRequests()
heap.Init(&requestHeap)
for requestHeap.Len() != 0 && len(desired.Requests) < p.nominalMaxRequests() {
requestIndex := heap.Pop(&requestHeap).(RequestIndex)
desired.Requests = append(desired.Requests, requestIndex)
}
return
}
2021-10-21 07:28:57 +08:00
func (p *Peer) maybeUpdateActualRequestState() bool {
if p.needRequestUpdate == "" {
return true
}
var more bool
pprof.Do(
context.Background(),
pprof.Labels("update request", p.needRequestUpdate),
func(_ context.Context) {
next := p.getDesiredRequestState()
more = p.applyRequestState(next)
},
)
return more
}
2021-10-21 07:28:57 +08:00
// Transmit/action the request state to the peer.
func (p *Peer) applyRequestState(next desiredRequestState) bool {
current := &p.actualRequestState
if !p.setInterested(next.Interested) {
return false
}
more := true
cancel := current.Requests.Clone()
for _, ri := range next.Requests {
cancel.Remove(ri)
}
2021-09-20 13:24:24 +08:00
cancel.Iterate(func(req uint32) bool {
more = p.cancel(req)
return more
})
if !more {
return false
}
shuffled := false
lastPending := 0
for i := 0; i < len(next.Requests); i++ {
req := next.Requests[i]
if p.cancelledRequests.Contains(req) {
// Waiting for a reject or piece message, which will suitably trigger us to update our
// requests, so we can skip this one with no additional consideration.
2021-10-18 18:52:09 +08:00
continue
}
2021-10-21 07:28:57 +08:00
// The cardinality of our desired requests shouldn't exceed the max requests since it's used
// in the calculation of the requests. However, if we cancelled requests and they haven't
2021-10-21 07:28:57 +08:00
// been rejected or serviced yet with the fast extension enabled, we can end up with more
// extra outstanding requests. We could subtract the number of outstanding cancels from the
// next request cardinality, but peers might not like that.
if maxRequests(current.Requests.GetCardinality()) >= p.nominalMaxRequests() {
// log.Printf("not assigning all requests [desired=%v, cancelled=%v, current=%v, max=%v]",
2021-10-18 16:06:33 +08:00
// next.Requests.GetCardinality(),
// p.cancelledRequests.GetCardinality(),
// current.Requests.GetCardinality(),
// p.nominalMaxRequests(),
// )
2021-10-18 18:52:09 +08:00
break
}
otherPending := p.t.pendingRequests.Get(next.Requests[0])
if p.actualRequestState.Requests.Contains(next.Requests[0]) {
otherPending--
}
if otherPending < lastPending {
// Pending should only rise. It's supposed to be the strongest ordering criteria. If it
// doesn't, our shuffling condition could be wrong.
panic(lastPending)
}
// If the request has already been requested by another peer, shuffle this and the rest of
// the requests (since according to the increasing condition, the rest of the indices
// already have an outstanding request with another peer).
if !shuffled && otherPending > 0 {
shuffleReqs := next.Requests[i:]
rand.Shuffle(len(shuffleReqs), func(i, j int) {
shuffleReqs[i], shuffleReqs[j] = shuffleReqs[j], shuffleReqs[i]
})
// log.Printf("shuffled reqs [%v:%v]", i, len(next.Requests))
shuffled = true
// Repeat this index
i--
continue
}
2021-10-26 12:03:04 +08:00
more = p.mustRequest(req)
2021-10-18 18:52:09 +08:00
if !more {
break
}
}
2021-10-18 16:06:33 +08:00
p.updateRequestsTimer.Stop()
if more {
p.needRequestUpdate = ""
2021-10-18 16:06:33 +08:00
if !current.Requests.IsEmpty() {
2021-10-18 18:52:31 +08:00
p.updateRequestsTimer.Reset(3 * time.Second)
}
}
return more
}