2020-01-10 12:09:21 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2021-10-07 14:31:10 +08:00
|
|
|
"container/heap"
|
2021-10-08 10:53:36 +08:00
|
|
|
"context"
|
2021-10-05 17:06:23 +08:00
|
|
|
"encoding/gob"
|
|
|
|
"reflect"
|
2021-10-08 10:53:36 +08:00
|
|
|
"runtime/pprof"
|
2020-01-10 12:09:21 +08:00
|
|
|
"time"
|
2021-05-09 22:53:32 +08:00
|
|
|
"unsafe"
|
2020-01-10 12:09:21 +08:00
|
|
|
|
2021-10-05 17:06:23 +08:00
|
|
|
"github.com/anacrolix/log"
|
2021-10-07 14:31:10 +08:00
|
|
|
"github.com/anacrolix/multiless"
|
2021-05-20 18:23:45 +08:00
|
|
|
|
2021-05-13 07:56:58 +08:00
|
|
|
request_strategy "github.com/anacrolix/torrent/request-strategy"
|
2020-01-10 12:09:21 +08:00
|
|
|
)
|
|
|
|
|
2021-12-01 11:38:47 +08:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 17:06:23 +08:00
|
|
|
func init() {
|
|
|
|
gob.Register(peerId{})
|
|
|
|
}
|
|
|
|
|
2021-09-10 21:02:20 +08:00
|
|
|
type peerId struct {
|
|
|
|
*Peer
|
|
|
|
ptr uintptr
|
|
|
|
}
|
2021-05-13 09:26:22 +08:00
|
|
|
|
2021-09-10 21:02:20 +08:00
|
|
|
func (p peerId) Uintptr() uintptr {
|
|
|
|
return p.ptr
|
2021-05-13 09:26:22 +08:00
|
|
|
}
|
|
|
|
|
2021-10-05 17:06:23 +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
|
|
|
|
2021-10-07 14:31:10 +08:00
|
|
|
type peerRequests struct {
|
2021-12-01 16:21:25 +08:00
|
|
|
requestIndexes []RequestIndex
|
|
|
|
peer *Peer
|
2021-10-07 14:31:10 +08:00
|
|
|
}
|
|
|
|
|
2021-10-12 10:06:32 +08:00
|
|
|
func (p *peerRequests) Len() int {
|
2021-10-07 14:31:10 +08:00
|
|
|
return len(p.requestIndexes)
|
|
|
|
}
|
|
|
|
|
2021-10-12 10:06:32 +08:00
|
|
|
func (p *peerRequests) Less(i, j int) bool {
|
2021-10-07 14:31:10 +08:00
|
|
|
leftRequest := p.requestIndexes[i]
|
|
|
|
rightRequest := p.requestIndexes[j]
|
|
|
|
t := p.peer.t
|
2021-12-01 16:21:25 +08:00
|
|
|
leftPieceIndex := leftRequest / t.chunksPerRegularPiece()
|
|
|
|
rightPieceIndex := rightRequest / t.chunksPerRegularPiece()
|
2021-10-07 14:31:10 +08:00
|
|
|
ml := multiless.New()
|
2021-10-11 15:21:24 +08:00
|
|
|
// 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),
|
|
|
|
)
|
|
|
|
}
|
2021-12-03 18:30:41 +08:00
|
|
|
leftPeer := t.pendingRequests[leftRequest]
|
|
|
|
rightPeer := t.pendingRequests[rightRequest]
|
|
|
|
ml = ml.Bool(rightPeer == p.peer, leftPeer == p.peer)
|
|
|
|
ml = ml.Bool(rightPeer == nil, leftPeer == nil)
|
|
|
|
if ml.Ok() {
|
|
|
|
return ml.MustLess()
|
|
|
|
}
|
|
|
|
if leftPeer != nil {
|
|
|
|
ml = ml.Uint64(
|
|
|
|
rightPeer.actualRequestState.Requests.GetCardinality(),
|
|
|
|
leftPeer.actualRequestState.Requests.GetCardinality(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ml = ml.CmpInt64(t.lastRequested[rightRequest].Sub(t.lastRequested[leftRequest]).Nanoseconds())
|
2021-12-01 16:21:25 +08:00
|
|
|
leftPiece := t.piece(int(leftPieceIndex))
|
|
|
|
rightPiece := t.piece(int(rightPieceIndex))
|
2021-10-07 14:31:10 +08:00
|
|
|
ml = ml.Int(
|
2021-12-03 18:30:41 +08:00
|
|
|
// Technically we would be happy with the cached priority here, except we don't actually
|
|
|
|
// cache it anymore, and Torrent.piecePriority just does another lookup of *Piece to resolve
|
|
|
|
// the priority through Piece.purePriority, which is probably slower.
|
2021-12-01 16:21:25 +08:00
|
|
|
-int(leftPiece.purePriority()),
|
|
|
|
-int(rightPiece.purePriority()),
|
2021-10-12 09:31:47 +08:00
|
|
|
)
|
2021-10-07 14:31:10 +08:00
|
|
|
ml = ml.Int(
|
2021-12-01 16:21:25 +08:00
|
|
|
int(leftPiece.availability),
|
|
|
|
int(rightPiece.availability))
|
2021-12-03 18:30:41 +08:00
|
|
|
return ml.Less()
|
2021-10-07 14:31:10 +08:00
|
|
|
}
|
|
|
|
|
2021-10-12 09:31:47 +08:00
|
|
|
func (p *peerRequests) Swap(i, j int) {
|
2021-10-07 14:31:10 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-27 07:13:39 +08:00
|
|
|
type desiredRequestState struct {
|
2021-12-03 18:30:41 +08:00
|
|
|
Requests peerRequests
|
2021-10-27 07:13:39 +08:00
|
|
|
Interested bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) getDesiredRequestState() (desired desiredRequestState) {
|
2021-12-03 13:41:52 +08:00
|
|
|
if !p.t.haveInfo() {
|
|
|
|
return
|
|
|
|
}
|
2021-11-29 10:07:18 +08:00
|
|
|
input := p.t.getRequestStrategyInput()
|
2021-10-07 14:31:10 +08:00
|
|
|
requestHeap := peerRequests{
|
2021-10-11 13:21:26 +08:00
|
|
|
peer: p,
|
2021-10-07 14:31:10 +08:00
|
|
|
}
|
|
|
|
request_strategy.GetRequestablePieces(
|
|
|
|
input,
|
2021-12-01 11:38:47 +08:00
|
|
|
p.t.cl.pieceRequestOrder[p.t.storage.Capacity],
|
2021-12-01 16:21:25 +08:00
|
|
|
func(ih InfoHash, pieceIndex int) {
|
|
|
|
if ih != p.t.infoHash {
|
2021-10-07 14:31:10 +08:00
|
|
|
return
|
2021-09-18 18:34:14 +08:00
|
|
|
}
|
2021-10-07 14:31:10 +08:00
|
|
|
if !p.peerHasPiece(pieceIndex) {
|
|
|
|
return
|
2021-05-20 18:23:45 +08:00
|
|
|
}
|
2021-10-11 13:21:26 +08:00
|
|
|
allowedFast := p.peerAllowedFast.ContainsInt(pieceIndex)
|
2021-12-01 16:21:25 +08:00
|
|
|
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
|
2021-12-12 13:56:00 +08:00
|
|
|
// if p.t.pendingRequests.Get(r) != 0 && !p.actualRequestState.Requests.Contains(r) {
|
2021-10-25 18:37:25 +08:00
|
|
|
// return
|
2021-12-12 13:56:00 +08:00
|
|
|
// }
|
2021-10-11 15:21:24 +08:00
|
|
|
if !allowedFast {
|
2021-12-03 18:30:41 +08:00
|
|
|
// We must signal interest to request this. TODO: We could set interested if the
|
|
|
|
// peers pieces (minus the allowed fast set) overlap with our missing pieces if
|
|
|
|
// there are any readers, or any pending pieces.
|
2021-10-11 15:21:24 +08:00
|
|
|
desired.Interested = true
|
2021-10-25 10:00:56 +08:00
|
|
|
// 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) {
|
2021-10-11 15:21:24 +08:00
|
|
|
// We can't request this right now.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 17:53:39 +08:00
|
|
|
requestHeap.requestIndexes = append(requestHeap.requestIndexes, r)
|
2021-10-07 14:31:10 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
)
|
2021-10-25 13:16:56 +08:00
|
|
|
p.t.assertPendingRequests()
|
2021-12-03 18:30:41 +08:00
|
|
|
desired.Requests = requestHeap
|
2021-10-07 14:31:10 +08:00
|
|
|
return
|
|
|
|
}
|
2021-09-20 13:09:28 +08:00
|
|
|
|
2021-10-21 07:28:57 +08:00
|
|
|
func (p *Peer) maybeUpdateActualRequestState() bool {
|
2021-10-08 10:53:36 +08:00
|
|
|
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.
|
2021-10-27 07:13:39 +08:00
|
|
|
func (p *Peer) applyRequestState(next desiredRequestState) bool {
|
2021-10-18 15:40:33 +08:00
|
|
|
current := &p.actualRequestState
|
2021-09-20 13:09:28 +08:00
|
|
|
if !p.setInterested(next.Interested) {
|
2021-09-18 18:34:14 +08:00
|
|
|
return false
|
|
|
|
}
|
2021-09-20 13:09:28 +08:00
|
|
|
more := true
|
2021-12-03 18:30:41 +08:00
|
|
|
requestHeap := &next.Requests
|
2021-12-10 15:04:45 +08:00
|
|
|
t := p.t
|
2021-12-03 18:30:41 +08:00
|
|
|
heap.Init(requestHeap)
|
|
|
|
for requestHeap.Len() != 0 && maxRequests(current.Requests.GetCardinality()) < p.nominalMaxRequests() {
|
|
|
|
req := heap.Pop(requestHeap).(RequestIndex)
|
2021-10-13 07:16:56 +08:00
|
|
|
if p.cancelledRequests.Contains(req) {
|
2021-10-18 15:40:33 +08:00
|
|
|
// 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-13 07:16:56 +08:00
|
|
|
}
|
2021-12-10 15:04:45 +08:00
|
|
|
existing := t.requestingPeer(req)
|
|
|
|
if existing != nil && existing != p && existing.uncancelledRequests() > current.Requests.GetCardinality() {
|
|
|
|
t.cancelRequest(req)
|
2021-12-12 13:56:00 +08:00
|
|
|
}
|
2021-10-26 12:03:04 +08:00
|
|
|
more = p.mustRequest(req)
|
2021-10-18 18:52:09 +08:00
|
|
|
if !more {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 10:10:02 +08:00
|
|
|
// TODO: This may need to change, we might want to update even if there were no requests due to
|
|
|
|
// filtering them for being recently requested already.
|
2021-10-18 16:06:33 +08:00
|
|
|
p.updateRequestsTimer.Stop()
|
2021-10-08 10:53:36 +08:00
|
|
|
if more {
|
|
|
|
p.needRequestUpdate = ""
|
2021-12-02 10:10:02 +08:00
|
|
|
if current.Interested {
|
2021-10-18 18:52:31 +08:00
|
|
|
p.updateRequestsTimer.Reset(3 * time.Second)
|
2021-10-18 15:40:33 +08:00
|
|
|
}
|
2021-10-08 10:53:36 +08:00
|
|
|
}
|
2021-09-18 18:34:14 +08:00
|
|
|
return more
|
2020-01-24 14:55:20 +08:00
|
|
|
}
|