chore: report client back source error (#1209)

Signed-off-by: Jim Ma <majinjing3@gmail.com>
This commit is contained in:
Jim Ma 2022-03-30 14:18:50 +08:00 committed by Gaius
parent 7a5857d389
commit a413dc4bdd
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
6 changed files with 121 additions and 34 deletions

View File

@ -1164,6 +1164,8 @@ func (pt *peerTaskConductor) ReportPieceResult(request *DownloadPieceRequest, re
code = base.Code_ClientConnectionError code = base.Code_ClientConnectionError
} else if isPieceNotFound(err) { } else if isPieceNotFound(err) {
code = base.Code_ClientPieceNotFound code = base.Code_ClientPieceNotFound
} else if isBackSourceError(err) {
code = base.Code_ClientBackSourceError
} }
pt.reportFailResult(request, result, code) pt.reportFailResult(request, result, code)
} }

View File

@ -70,6 +70,10 @@ type pieceDownloadError struct {
err error err error
} }
type backSourceError struct {
err error
}
func isConnectionError(err error) bool { func isConnectionError(err error) bool {
if e, ok := err.(*pieceDownloadError); ok { if e, ok := err.(*pieceDownloadError); ok {
return e.connectionError return e.connectionError
@ -84,6 +88,13 @@ func isPieceNotFound(err error) bool {
return false return false
} }
func isBackSourceError(err error) bool {
if _, ok := err.(*backSourceError); ok {
return true
}
return false
}
func (e *pieceDownloadError) Error() string { func (e *pieceDownloadError) Error() string {
if e.connectionError { if e.connectionError {
return fmt.Sprintf("connect with %s with error: %s", e.target, e.err) return fmt.Sprintf("connect with %s with error: %s", e.target, e.err)
@ -91,6 +102,10 @@ func (e *pieceDownloadError) Error() string {
return fmt.Sprintf("download %s with error status: %s", e.target, e.status) return fmt.Sprintf("download %s with error status: %s", e.target, e.status)
} }
func (e *backSourceError) Error() string {
return e.err.Error()
}
var _ PieceDownloader = (*pieceDownloader)(nil) var _ PieceDownloader = (*pieceDownloader)(nil)
var defaultTransport http.RoundTripper = &http.Transport{ var defaultTransport http.RoundTripper = &http.Transport{

View File

@ -23,6 +23,7 @@ import (
"math" "math"
"net" "net"
"net/http" "net/http"
"net/url"
"time" "time"
"golang.org/x/time/rate" "golang.org/x/time/rate"
@ -333,13 +334,13 @@ func (pm *pieceManager) downloadKnownLengthSource(ctx context.Context, pt Task,
} }
if err != nil { if err != nil {
log.Errorf("download piece %d error: %s", pieceNum, err) log.Errorf("download piece %d error: %s", pieceNum, err)
pt.ReportPieceResult(request, result, err) pt.ReportPieceResult(request, result, detectBackSourceError(err))
return err return err
} }
if result.Size != int64(size) { if result.Size != int64(size) {
log.Errorf("download piece %d size not match, desired: %d, actual: %d", pieceNum, size, result.Size) log.Errorf("download piece %d size not match, desired: %d, actual: %d", pieceNum, size, result.Size)
pt.ReportPieceResult(request, result, err) pt.ReportPieceResult(request, result, detectBackSourceError(err))
return storage.ErrShortRead return storage.ErrShortRead
} }
@ -356,7 +357,7 @@ func (pm *pieceManager) downloadKnownLengthSource(ctx context.Context, pt Task,
}) })
if err != nil { if err != nil {
log.Errorf("update task failed %s", err) log.Errorf("update task failed %s", err)
pt.ReportPieceResult(request, result, err) pt.ReportPieceResult(request, result, detectBackSourceError(err))
return err return err
} }
} }
@ -401,7 +402,7 @@ func (pm *pieceManager) downloadUnknownLengthSource(ctx context.Context, pt Task
}, },
} }
if err != nil { if err != nil {
pt.ReportPieceResult(request, result, err) pt.ReportPieceResult(request, result, detectBackSourceError(err))
log.Errorf("download piece %d error: %s", pieceNum, err) log.Errorf("download piece %d error: %s", pieceNum, err)
return err return err
} }
@ -412,7 +413,7 @@ func (pm *pieceManager) downloadUnknownLengthSource(ctx context.Context, pt Task
} else if result.Size > int64(size) { } else if result.Size > int64(size) {
err = fmt.Errorf("piece %d size %d should not great than %d", pieceNum, result.Size, size) err = fmt.Errorf("piece %d size %d should not great than %d", pieceNum, result.Size, size)
log.Errorf(err.Error()) log.Errorf(err.Error())
pt.ReportPieceResult(request, result, err) pt.ReportPieceResult(request, result, detectBackSourceError(err))
return err return err
} }
@ -430,7 +431,7 @@ func (pm *pieceManager) downloadUnknownLengthSource(ctx context.Context, pt Task
}) })
if err != nil { if err != nil {
log.Errorf("update task failed %s", err) log.Errorf("update task failed %s", err)
pt.ReportPieceResult(request, result, err) pt.ReportPieceResult(request, result, detectBackSourceError(err))
return err return err
} }
// content length is aligning at piece size // content length is aligning at piece size
@ -446,3 +447,11 @@ func (pm *pieceManager) downloadUnknownLengthSource(ctx context.Context, pt Task
log.Infof("download from source ok") log.Infof("download from source ok")
return nil return nil
} }
func detectBackSourceError(err error) error {
// TODO ensure all source plugin use *url.Error for back source
if e, ok := err.(*url.Error); ok {
return &backSourceError{err: e}
}
return err
}

View File

@ -234,3 +234,59 @@ func TestPieceManager_DownloadSource(t *testing.T) {
}) })
} }
} }
func TestDetectBackSourceError(t *testing.T) {
assert := testifyassert.New(t)
testCases := []struct {
name string
genError func() error
detectError bool
isBackSourceError bool
}{
{
name: "is back source error - connect error",
genError: func() error {
_, err := http.Get("http://127.0.0.1:12345")
return err
},
detectError: true,
isBackSourceError: true,
},
{
name: "is back source error - timeout",
genError: func() error {
client := http.Client{
Timeout: 10 * time.Millisecond,
}
request, _ := http.NewRequest(http.MethodGet, "http://127.0.0.2:12345", nil)
_, err := client.Do(request)
return err
},
detectError: true,
isBackSourceError: true,
},
{
name: "not back source error",
genError: func() error {
return fmt.Errorf("test")
},
detectError: true,
isBackSourceError: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.genError()
err = detectBackSourceError(err)
if tc.detectError {
assert.NotNil(err)
} else {
assert.Nil(err)
}
assert.Equal(tc.isBackSourceError, isBackSourceError(err))
})
}
}

View File

@ -60,6 +60,7 @@ const (
Code_ClientPieceDownloadFail Code = 4005 Code_ClientPieceDownloadFail Code = 4005
Code_ClientRequestLimitFail Code = 4006 Code_ClientRequestLimitFail Code = 4006
Code_ClientConnectionError Code = 4007 Code_ClientConnectionError Code = 4007
Code_ClientBackSourceError Code = 4008
Code_ClientPieceNotFound Code = 4404 Code_ClientPieceNotFound Code = 4404
// scheduler response error 5000-5999 // scheduler response error 5000-5999
Code_SchedError Code = 5000 Code_SchedError Code = 5000
@ -96,6 +97,7 @@ var (
4005: "ClientPieceDownloadFail", 4005: "ClientPieceDownloadFail",
4006: "ClientRequestLimitFail", 4006: "ClientRequestLimitFail",
4007: "ClientConnectionError", 4007: "ClientConnectionError",
4008: "ClientBackSourceError",
4404: "ClientPieceNotFound", 4404: "ClientPieceNotFound",
5000: "SchedError", 5000: "SchedError",
5001: "SchedNeedBackSource", 5001: "SchedNeedBackSource",
@ -126,6 +128,7 @@ var (
"ClientPieceDownloadFail": 4005, "ClientPieceDownloadFail": 4005,
"ClientRequestLimitFail": 4006, "ClientRequestLimitFail": 4006,
"ClientConnectionError": 4007, "ClientConnectionError": 4007,
"ClientBackSourceError": 4008,
"ClientPieceNotFound": 4404, "ClientPieceNotFound": 4404,
"SchedError": 5000, "SchedError": 5000,
"SchedNeedBackSource": 5001, "SchedNeedBackSource": 5001,
@ -835,7 +838,7 @@ var file_pkg_rpc_base_base_proto_rawDesc = []byte{
0x68, 0x18, 0x07, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x68, 0x18, 0x07, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x12, 0x24, 0x0a, 0x0e, 0x70, 0x69, 0x65, 0x63, 0x65, 0x5f, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x12, 0x24, 0x0a, 0x0e, 0x70, 0x69, 0x65, 0x63, 0x65, 0x5f,
0x6d, 0x64, 0x35, 0x5f, 0x73, 0x69, 0x67, 0x6e, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x6d, 0x64, 0x35, 0x5f, 0x73, 0x69, 0x67, 0x6e, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c,
0x70, 0x69, 0x65, 0x63, 0x65, 0x4d, 0x64, 0x35, 0x53, 0x69, 0x67, 0x6e, 0x2a, 0xa1, 0x05, 0x0a, 0x70, 0x69, 0x65, 0x63, 0x65, 0x4d, 0x64, 0x35, 0x53, 0x69, 0x67, 0x6e, 0x2a, 0xbd, 0x05, 0x0a,
0x04, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x11, 0x0a, 0x0d, 0x58, 0x5f, 0x55, 0x4e, 0x53, 0x50, 0x45, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x11, 0x0a, 0x0d, 0x58, 0x5f, 0x55, 0x4e, 0x53, 0x50, 0x45,
0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x07, 0x53, 0x75, 0x63, 0x63, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x07, 0x53, 0x75, 0x63, 0x63,
0x65, 0x73, 0x73, 0x10, 0xc8, 0x01, 0x12, 0x16, 0x0a, 0x11, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x65, 0x73, 0x73, 0x10, 0xc8, 0x01, 0x12, 0x16, 0x0a, 0x11, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
@ -859,33 +862,34 @@ var file_pkg_rpc_base_base_proto_rawDesc = []byte{
0x10, 0xa5, 0x1f, 0x12, 0x1b, 0x0a, 0x16, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x10, 0xa5, 0x1f, 0x12, 0x1b, 0x0a, 0x16, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71,
0x75, 0x65, 0x73, 0x74, 0x4c, 0x69, 0x6d, 0x69, 0x74, 0x46, 0x61, 0x69, 0x6c, 0x10, 0xa6, 0x1f, 0x75, 0x65, 0x73, 0x74, 0x4c, 0x69, 0x6d, 0x69, 0x74, 0x46, 0x61, 0x69, 0x6c, 0x10, 0xa6, 0x1f,
0x12, 0x1a, 0x0a, 0x15, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x12, 0x1a, 0x0a, 0x15, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63,
0x74, 0x69, 0x6f, 0x6e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0xa7, 0x1f, 0x12, 0x18, 0x0a, 0x13, 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0xa7, 0x1f, 0x12, 0x1a, 0x0a, 0x15,
0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x50, 0x69, 0x65, 0x63, 0x65, 0x4e, 0x6f, 0x74, 0x46, 0x6f, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x42, 0x61, 0x63, 0x6b, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65,
0x75, 0x6e, 0x64, 0x10, 0xb4, 0x22, 0x12, 0x0f, 0x0a, 0x0a, 0x53, 0x63, 0x68, 0x65, 0x64, 0x45, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0xa8, 0x1f, 0x12, 0x18, 0x0a, 0x13, 0x43, 0x6c, 0x69, 0x65,
0x72, 0x72, 0x6f, 0x72, 0x10, 0x88, 0x27, 0x12, 0x18, 0x0a, 0x13, 0x53, 0x63, 0x68, 0x65, 0x64, 0x6e, 0x74, 0x50, 0x69, 0x65, 0x63, 0x65, 0x4e, 0x6f, 0x74, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x10,
0x4e, 0x65, 0x65, 0x64, 0x42, 0x61, 0x63, 0x6b, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x10, 0x89, 0xb4, 0x22, 0x12, 0x0f, 0x0a, 0x0a, 0x53, 0x63, 0x68, 0x65, 0x64, 0x45, 0x72, 0x72, 0x6f, 0x72,
0x27, 0x12, 0x12, 0x0a, 0x0d, 0x53, 0x63, 0x68, 0x65, 0x64, 0x50, 0x65, 0x65, 0x72, 0x47, 0x6f, 0x10, 0x88, 0x27, 0x12, 0x18, 0x0a, 0x13, 0x53, 0x63, 0x68, 0x65, 0x64, 0x4e, 0x65, 0x65, 0x64,
0x6e, 0x65, 0x10, 0x8a, 0x27, 0x12, 0x16, 0x0a, 0x11, 0x53, 0x63, 0x68, 0x65, 0x64, 0x50, 0x65, 0x42, 0x61, 0x63, 0x6b, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x10, 0x89, 0x27, 0x12, 0x12, 0x0a,
0x65, 0x72, 0x4e, 0x6f, 0x74, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x10, 0x8c, 0x27, 0x12, 0x23, 0x0a, 0x0d, 0x53, 0x63, 0x68, 0x65, 0x64, 0x50, 0x65, 0x65, 0x72, 0x47, 0x6f, 0x6e, 0x65, 0x10, 0x8a,
0x1e, 0x53, 0x63, 0x68, 0x65, 0x64, 0x50, 0x65, 0x65, 0x72, 0x50, 0x69, 0x65, 0x63, 0x65, 0x52, 0x27, 0x12, 0x16, 0x0a, 0x11, 0x53, 0x63, 0x68, 0x65, 0x64, 0x50, 0x65, 0x65, 0x72, 0x4e, 0x6f,
0x65, 0x73, 0x75, 0x6c, 0x74, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x61, 0x69, 0x6c, 0x10, 0x74, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x10, 0x8c, 0x27, 0x12, 0x23, 0x0a, 0x1e, 0x53, 0x63, 0x68,
0x8d, 0x27, 0x12, 0x19, 0x0a, 0x14, 0x53, 0x63, 0x68, 0x65, 0x64, 0x54, 0x61, 0x73, 0x6b, 0x53, 0x65, 0x64, 0x50, 0x65, 0x65, 0x72, 0x50, 0x69, 0x65, 0x63, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c,
0x74, 0x61, 0x74, 0x75, 0x73, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0x8e, 0x27, 0x12, 0x0d, 0x0a, 0x74, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x61, 0x69, 0x6c, 0x10, 0x8d, 0x27, 0x12, 0x19,
0x08, 0x43, 0x44, 0x4e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0xf0, 0x2e, 0x12, 0x18, 0x0a, 0x13, 0x0a, 0x14, 0x53, 0x63, 0x68, 0x65, 0x64, 0x54, 0x61, 0x73, 0x6b, 0x53, 0x74, 0x61, 0x74, 0x75,
0x43, 0x44, 0x4e, 0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x72, 0x79, 0x46, 0x73, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0x8e, 0x27, 0x12, 0x0d, 0x0a, 0x08, 0x43, 0x44, 0x4e,
0x61, 0x69, 0x6c, 0x10, 0xf1, 0x2e, 0x12, 0x18, 0x0a, 0x13, 0x43, 0x44, 0x4e, 0x54, 0x61, 0x73, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0xf0, 0x2e, 0x12, 0x18, 0x0a, 0x13, 0x43, 0x44, 0x4e, 0x54,
0x6b, 0x44, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x46, 0x61, 0x69, 0x6c, 0x10, 0xf2, 0x2e, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x72, 0x79, 0x46, 0x61, 0x69, 0x6c, 0x10,
0x12, 0x14, 0x0a, 0x0f, 0x43, 0x44, 0x4e, 0x54, 0x61, 0x73, 0x6b, 0x4e, 0x6f, 0x74, 0x46, 0x6f, 0xf1, 0x2e, 0x12, 0x18, 0x0a, 0x13, 0x43, 0x44, 0x4e, 0x54, 0x61, 0x73, 0x6b, 0x44, 0x6f, 0x77,
0x75, 0x6e, 0x64, 0x10, 0x84, 0x32, 0x12, 0x18, 0x0a, 0x13, 0x49, 0x6e, 0x76, 0x61, 0x6c, 0x69, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x46, 0x61, 0x69, 0x6c, 0x10, 0xf2, 0x2e, 0x12, 0x14, 0x0a, 0x0f,
0x64, 0x52, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x54, 0x79, 0x70, 0x65, 0x10, 0xd9, 0x36, 0x43, 0x44, 0x4e, 0x54, 0x61, 0x73, 0x6b, 0x4e, 0x6f, 0x74, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x10,
0x2a, 0x17, 0x0a, 0x0a, 0x50, 0x69, 0x65, 0x63, 0x65, 0x53, 0x74, 0x79, 0x6c, 0x65, 0x12, 0x09, 0x84, 0x32, 0x12, 0x18, 0x0a, 0x13, 0x49, 0x6e, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x52, 0x65, 0x73,
0x0a, 0x05, 0x50, 0x4c, 0x41, 0x49, 0x4e, 0x10, 0x00, 0x2a, 0x2c, 0x0a, 0x09, 0x53, 0x69, 0x7a, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x54, 0x79, 0x70, 0x65, 0x10, 0xd9, 0x36, 0x2a, 0x17, 0x0a, 0x0a,
0x65, 0x53, 0x63, 0x6f, 0x70, 0x65, 0x12, 0x0a, 0x0a, 0x06, 0x4e, 0x4f, 0x52, 0x4d, 0x41, 0x4c, 0x50, 0x69, 0x65, 0x63, 0x65, 0x53, 0x74, 0x79, 0x6c, 0x65, 0x12, 0x09, 0x0a, 0x05, 0x50, 0x4c,
0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x53, 0x4d, 0x41, 0x4c, 0x4c, 0x10, 0x01, 0x12, 0x08, 0x0a, 0x41, 0x49, 0x4e, 0x10, 0x00, 0x2a, 0x2c, 0x0a, 0x09, 0x53, 0x69, 0x7a, 0x65, 0x53, 0x63, 0x6f,
0x04, 0x54, 0x49, 0x4e, 0x59, 0x10, 0x02, 0x42, 0x22, 0x5a, 0x20, 0x64, 0x37, 0x79, 0x2e, 0x69, 0x70, 0x65, 0x12, 0x0a, 0x0a, 0x06, 0x4e, 0x4f, 0x52, 0x4d, 0x41, 0x4c, 0x10, 0x00, 0x12, 0x09,
0x6f, 0x2f, 0x64, 0x72, 0x61, 0x67, 0x6f, 0x6e, 0x66, 0x6c, 0x79, 0x2f, 0x76, 0x32, 0x2f, 0x70, 0x0a, 0x05, 0x53, 0x4d, 0x41, 0x4c, 0x4c, 0x10, 0x01, 0x12, 0x08, 0x0a, 0x04, 0x54, 0x49, 0x4e,
0x6b, 0x67, 0x2f, 0x72, 0x70, 0x63, 0x2f, 0x62, 0x61, 0x73, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x59, 0x10, 0x02, 0x42, 0x22, 0x5a, 0x20, 0x64, 0x37, 0x79, 0x2e, 0x69, 0x6f, 0x2f, 0x64, 0x72,
0x74, 0x6f, 0x33, 0x61, 0x67, 0x6f, 0x6e, 0x66, 0x6c, 0x79, 0x2f, 0x76, 0x32, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x72,
0x70, 0x63, 0x2f, 0x62, 0x61, 0x73, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (

View File

@ -46,6 +46,7 @@ enum Code{
ClientPieceDownloadFail = 4005; ClientPieceDownloadFail = 4005;
ClientRequestLimitFail = 4006; ClientRequestLimitFail = 4006;
ClientConnectionError = 4007; ClientConnectionError = 4007;
ClientBackSourceError = 4008;
ClientPieceNotFound = 4404; ClientPieceNotFound = 4404;
// scheduler response error 5000-5999 // scheduler response error 5000-5999