fix: client miss failed piece (#1194)

Signed-off-by: Jim Ma <majinjing3@gmail.com>
This commit is contained in:
Jim Ma 2022-03-25 15:20:19 +08:00 committed by Gaius
parent 1939ceff8f
commit d45bffea40
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
1 changed files with 16 additions and 1 deletions

View File

@ -561,7 +561,7 @@ loop:
break loop break loop
} }
pt.Debugf("receive peerPacket %v for peer %s", peerPacket, pt.peerID) pt.Debugf("receive peerPacket %v", peerPacket)
if peerPacket.Code != base.Code_Success { if peerPacket.Code != base.Code_Success {
if peerPacket.Code == base.Code_SchedNeedBackSource { if peerPacket.Code == base.Code_SchedNeedBackSource {
pt.needBackSource.Store(true) pt.needBackSource.Store(true)
@ -783,6 +783,7 @@ loop:
default: default:
} }
retry:
// 2, try to get pieces // 2, try to get pieces
pt.Debugf("try to get pieces, number: %d, limit: %d", num, limit) pt.Debugf("try to get pieces, number: %d, limit: %d", num, limit)
piecePacket, err := pt.pieceTaskPoller.preparePieceTasks( piecePacket, err := pt.pieceTaskPoller.preparePieceTasks(
@ -823,6 +824,7 @@ loop:
} }
// just need one piece // just need one piece
limit = 1 limit = 1
goto retry
} }
} }
@ -978,6 +980,7 @@ func (pt *peerTaskConductor) waitFailedPiece() (int32, bool) {
if pt.isCompleted() { if pt.isCompleted() {
return -1, false return -1, false
} }
wait:
// use no default branch select to wait failed piece or exit // use no default branch select to wait failed piece or exit
select { select {
case <-pt.successCh: case <-pt.successCh:
@ -989,6 +992,17 @@ func (pt *peerTaskConductor) waitFailedPiece() (int32, bool) {
case failed := <-pt.failedPieceCh: case failed := <-pt.failedPieceCh:
pt.Warnf("download piece/%d failed, retry", failed) pt.Warnf("download piece/%d failed, retry", failed)
return failed, true return failed, true
case _, ok := <-pt.peerPacketReady:
if ok {
// preparePieceTasksByPeer func already send piece result with error
pt.Infof("new peer client ready, but all pieces are already downloading, just wait failed pieces")
goto wait
}
// when scheduler says base.Code_SchedNeedBackSource, receivePeerPacket will close pt.peerPacketReady
pt.Infof("start download from source due to base.Code_SchedNeedBackSource")
pt.span.AddEvent("back source due to scheduler says need back source")
pt.backSource()
return -1, false
} }
} }
@ -1056,6 +1070,7 @@ func (pt *peerTaskConductor) downloadPiece(workerID int32, request *DownloadPiec
StartNum: uint32(request.piece.PieceNum), StartNum: uint32(request.piece.PieceNum),
Limit: 1, Limit: 1,
}) })
// Deprecated
// send to fail chan and retry // send to fail chan and retry
// try to send directly first, if failed channel is busy, create a new goroutine to do this // try to send directly first, if failed channel is busy, create a new goroutine to do this
select { select {