diff --git a/client/daemon/peer/peertask_conductor.go b/client/daemon/peer/peertask_conductor.go index 6c5311e0d..955bcbdfe 100644 --- a/client/daemon/peer/peertask_conductor.go +++ b/client/daemon/peer/peertask_conductor.go @@ -1164,6 +1164,8 @@ func (pt *peerTaskConductor) ReportPieceResult(request *DownloadPieceRequest, re code = base.Code_ClientConnectionError } else if isPieceNotFound(err) { code = base.Code_ClientPieceNotFound + } else if isBackSourceError(err) { + code = base.Code_ClientBackSourceError } pt.reportFailResult(request, result, code) } diff --git a/client/daemon/peer/piece_downloader.go b/client/daemon/peer/piece_downloader.go index c7c07b0c2..a6baa84db 100644 --- a/client/daemon/peer/piece_downloader.go +++ b/client/daemon/peer/piece_downloader.go @@ -70,6 +70,10 @@ type pieceDownloadError struct { err error } +type backSourceError struct { + err error +} + func isConnectionError(err error) bool { if e, ok := err.(*pieceDownloadError); ok { return e.connectionError @@ -84,6 +88,13 @@ func isPieceNotFound(err error) bool { return false } +func isBackSourceError(err error) bool { + if _, ok := err.(*backSourceError); ok { + return true + } + return false +} + func (e *pieceDownloadError) Error() string { if e.connectionError { 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) } +func (e *backSourceError) Error() string { + return e.err.Error() +} + var _ PieceDownloader = (*pieceDownloader)(nil) var defaultTransport http.RoundTripper = &http.Transport{ diff --git a/client/daemon/peer/piece_manager.go b/client/daemon/peer/piece_manager.go index 60876fa1e..29e553aee 100644 --- a/client/daemon/peer/piece_manager.go +++ b/client/daemon/peer/piece_manager.go @@ -23,6 +23,7 @@ import ( "math" "net" "net/http" + "net/url" "time" "golang.org/x/time/rate" @@ -333,13 +334,13 @@ func (pm *pieceManager) downloadKnownLengthSource(ctx context.Context, pt Task, } if err != nil { log.Errorf("download piece %d error: %s", pieceNum, err) - pt.ReportPieceResult(request, result, err) + pt.ReportPieceResult(request, result, detectBackSourceError(err)) return err } if result.Size != int64(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 } @@ -356,7 +357,7 @@ func (pm *pieceManager) downloadKnownLengthSource(ctx context.Context, pt Task, }) if err != nil { log.Errorf("update task failed %s", err) - pt.ReportPieceResult(request, result, err) + pt.ReportPieceResult(request, result, detectBackSourceError(err)) return err } } @@ -401,7 +402,7 @@ func (pm *pieceManager) downloadUnknownLengthSource(ctx context.Context, pt Task }, } if err != nil { - pt.ReportPieceResult(request, result, err) + pt.ReportPieceResult(request, result, detectBackSourceError(err)) log.Errorf("download piece %d error: %s", pieceNum, err) return err } @@ -412,7 +413,7 @@ func (pm *pieceManager) downloadUnknownLengthSource(ctx context.Context, pt Task } else if result.Size > int64(size) { err = fmt.Errorf("piece %d size %d should not great than %d", pieceNum, result.Size, size) log.Errorf(err.Error()) - pt.ReportPieceResult(request, result, err) + pt.ReportPieceResult(request, result, detectBackSourceError(err)) return err } @@ -430,7 +431,7 @@ func (pm *pieceManager) downloadUnknownLengthSource(ctx context.Context, pt Task }) if err != nil { log.Errorf("update task failed %s", err) - pt.ReportPieceResult(request, result, err) + pt.ReportPieceResult(request, result, detectBackSourceError(err)) return err } // 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") 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 +} diff --git a/client/daemon/peer/piece_manager_test.go b/client/daemon/peer/piece_manager_test.go index 0d292251a..391136ae6 100644 --- a/client/daemon/peer/piece_manager_test.go +++ b/client/daemon/peer/piece_manager_test.go @@ -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)) + }) + } +} diff --git a/pkg/rpc/base/base.pb.go b/pkg/rpc/base/base.pb.go index a55ea0b9d..9b1d2aa38 100644 --- a/pkg/rpc/base/base.pb.go +++ b/pkg/rpc/base/base.pb.go @@ -60,6 +60,7 @@ const ( Code_ClientPieceDownloadFail Code = 4005 Code_ClientRequestLimitFail Code = 4006 Code_ClientConnectionError Code = 4007 + Code_ClientBackSourceError Code = 4008 Code_ClientPieceNotFound Code = 4404 // scheduler response error 5000-5999 Code_SchedError Code = 5000 @@ -96,6 +97,7 @@ var ( 4005: "ClientPieceDownloadFail", 4006: "ClientRequestLimitFail", 4007: "ClientConnectionError", + 4008: "ClientBackSourceError", 4404: "ClientPieceNotFound", 5000: "SchedError", 5001: "SchedNeedBackSource", @@ -126,6 +128,7 @@ var ( "ClientPieceDownloadFail": 4005, "ClientRequestLimitFail": 4006, "ClientConnectionError": 4007, + "ClientBackSourceError": 4008, "ClientPieceNotFound": 4404, "SchedError": 5000, "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, 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, - 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, 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, @@ -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, 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, - 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0xa7, 0x1f, 0x12, 0x18, 0x0a, 0x13, - 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x50, 0x69, 0x65, 0x63, 0x65, 0x4e, 0x6f, 0x74, 0x46, 0x6f, - 0x75, 0x6e, 0x64, 0x10, 0xb4, 0x22, 0x12, 0x0f, 0x0a, 0x0a, 0x53, 0x63, 0x68, 0x65, 0x64, 0x45, - 0x72, 0x72, 0x6f, 0x72, 0x10, 0x88, 0x27, 0x12, 0x18, 0x0a, 0x13, 0x53, 0x63, 0x68, 0x65, 0x64, - 0x4e, 0x65, 0x65, 0x64, 0x42, 0x61, 0x63, 0x6b, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x10, 0x89, - 0x27, 0x12, 0x12, 0x0a, 0x0d, 0x53, 0x63, 0x68, 0x65, 0x64, 0x50, 0x65, 0x65, 0x72, 0x47, 0x6f, - 0x6e, 0x65, 0x10, 0x8a, 0x27, 0x12, 0x16, 0x0a, 0x11, 0x53, 0x63, 0x68, 0x65, 0x64, 0x50, 0x65, - 0x65, 0x72, 0x4e, 0x6f, 0x74, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x10, 0x8c, 0x27, 0x12, 0x23, 0x0a, - 0x1e, 0x53, 0x63, 0x68, 0x65, 0x64, 0x50, 0x65, 0x65, 0x72, 0x50, 0x69, 0x65, 0x63, 0x65, 0x52, - 0x65, 0x73, 0x75, 0x6c, 0x74, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x61, 0x69, 0x6c, 0x10, - 0x8d, 0x27, 0x12, 0x19, 0x0a, 0x14, 0x53, 0x63, 0x68, 0x65, 0x64, 0x54, 0x61, 0x73, 0x6b, 0x53, - 0x74, 0x61, 0x74, 0x75, 0x73, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0x8e, 0x27, 0x12, 0x0d, 0x0a, - 0x08, 0x43, 0x44, 0x4e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0xf0, 0x2e, 0x12, 0x18, 0x0a, 0x13, - 0x43, 0x44, 0x4e, 0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x72, 0x79, 0x46, - 0x61, 0x69, 0x6c, 0x10, 0xf1, 0x2e, 0x12, 0x18, 0x0a, 0x13, 0x43, 0x44, 0x4e, 0x54, 0x61, 0x73, - 0x6b, 0x44, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x46, 0x61, 0x69, 0x6c, 0x10, 0xf2, 0x2e, - 0x12, 0x14, 0x0a, 0x0f, 0x43, 0x44, 0x4e, 0x54, 0x61, 0x73, 0x6b, 0x4e, 0x6f, 0x74, 0x46, 0x6f, - 0x75, 0x6e, 0x64, 0x10, 0x84, 0x32, 0x12, 0x18, 0x0a, 0x13, 0x49, 0x6e, 0x76, 0x61, 0x6c, 0x69, - 0x64, 0x52, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x54, 0x79, 0x70, 0x65, 0x10, 0xd9, 0x36, - 0x2a, 0x17, 0x0a, 0x0a, 0x50, 0x69, 0x65, 0x63, 0x65, 0x53, 0x74, 0x79, 0x6c, 0x65, 0x12, 0x09, - 0x0a, 0x05, 0x50, 0x4c, 0x41, 0x49, 0x4e, 0x10, 0x00, 0x2a, 0x2c, 0x0a, 0x09, 0x53, 0x69, 0x7a, - 0x65, 0x53, 0x63, 0x6f, 0x70, 0x65, 0x12, 0x0a, 0x0a, 0x06, 0x4e, 0x4f, 0x52, 0x4d, 0x41, 0x4c, - 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x53, 0x4d, 0x41, 0x4c, 0x4c, 0x10, 0x01, 0x12, 0x08, 0x0a, - 0x04, 0x54, 0x49, 0x4e, 0x59, 0x10, 0x02, 0x42, 0x22, 0x5a, 0x20, 0x64, 0x37, 0x79, 0x2e, 0x69, - 0x6f, 0x2f, 0x64, 0x72, 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, + 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0xa7, 0x1f, 0x12, 0x1a, 0x0a, 0x15, + 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x42, 0x61, 0x63, 0x6b, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, + 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0xa8, 0x1f, 0x12, 0x18, 0x0a, 0x13, 0x43, 0x6c, 0x69, 0x65, + 0x6e, 0x74, 0x50, 0x69, 0x65, 0x63, 0x65, 0x4e, 0x6f, 0x74, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x10, + 0xb4, 0x22, 0x12, 0x0f, 0x0a, 0x0a, 0x53, 0x63, 0x68, 0x65, 0x64, 0x45, 0x72, 0x72, 0x6f, 0x72, + 0x10, 0x88, 0x27, 0x12, 0x18, 0x0a, 0x13, 0x53, 0x63, 0x68, 0x65, 0x64, 0x4e, 0x65, 0x65, 0x64, + 0x42, 0x61, 0x63, 0x6b, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x10, 0x89, 0x27, 0x12, 0x12, 0x0a, + 0x0d, 0x53, 0x63, 0x68, 0x65, 0x64, 0x50, 0x65, 0x65, 0x72, 0x47, 0x6f, 0x6e, 0x65, 0x10, 0x8a, + 0x27, 0x12, 0x16, 0x0a, 0x11, 0x53, 0x63, 0x68, 0x65, 0x64, 0x50, 0x65, 0x65, 0x72, 0x4e, 0x6f, + 0x74, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x10, 0x8c, 0x27, 0x12, 0x23, 0x0a, 0x1e, 0x53, 0x63, 0x68, + 0x65, 0x64, 0x50, 0x65, 0x65, 0x72, 0x50, 0x69, 0x65, 0x63, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, + 0x74, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x61, 0x69, 0x6c, 0x10, 0x8d, 0x27, 0x12, 0x19, + 0x0a, 0x14, 0x53, 0x63, 0x68, 0x65, 0x64, 0x54, 0x61, 0x73, 0x6b, 0x53, 0x74, 0x61, 0x74, 0x75, + 0x73, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0x8e, 0x27, 0x12, 0x0d, 0x0a, 0x08, 0x43, 0x44, 0x4e, + 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0xf0, 0x2e, 0x12, 0x18, 0x0a, 0x13, 0x43, 0x44, 0x4e, 0x54, + 0x61, 0x73, 0x6b, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x72, 0x79, 0x46, 0x61, 0x69, 0x6c, 0x10, + 0xf1, 0x2e, 0x12, 0x18, 0x0a, 0x13, 0x43, 0x44, 0x4e, 0x54, 0x61, 0x73, 0x6b, 0x44, 0x6f, 0x77, + 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x46, 0x61, 0x69, 0x6c, 0x10, 0xf2, 0x2e, 0x12, 0x14, 0x0a, 0x0f, + 0x43, 0x44, 0x4e, 0x54, 0x61, 0x73, 0x6b, 0x4e, 0x6f, 0x74, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x10, + 0x84, 0x32, 0x12, 0x18, 0x0a, 0x13, 0x49, 0x6e, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x52, 0x65, 0x73, + 0x6f, 0x75, 0x72, 0x63, 0x65, 0x54, 0x79, 0x70, 0x65, 0x10, 0xd9, 0x36, 0x2a, 0x17, 0x0a, 0x0a, + 0x50, 0x69, 0x65, 0x63, 0x65, 0x53, 0x74, 0x79, 0x6c, 0x65, 0x12, 0x09, 0x0a, 0x05, 0x50, 0x4c, + 0x41, 0x49, 0x4e, 0x10, 0x00, 0x2a, 0x2c, 0x0a, 0x09, 0x53, 0x69, 0x7a, 0x65, 0x53, 0x63, 0x6f, + 0x70, 0x65, 0x12, 0x0a, 0x0a, 0x06, 0x4e, 0x4f, 0x52, 0x4d, 0x41, 0x4c, 0x10, 0x00, 0x12, 0x09, + 0x0a, 0x05, 0x53, 0x4d, 0x41, 0x4c, 0x4c, 0x10, 0x01, 0x12, 0x08, 0x0a, 0x04, 0x54, 0x49, 0x4e, + 0x59, 0x10, 0x02, 0x42, 0x22, 0x5a, 0x20, 0x64, 0x37, 0x79, 0x2e, 0x69, 0x6f, 0x2f, 0x64, 0x72, + 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 ( diff --git a/pkg/rpc/base/base.proto b/pkg/rpc/base/base.proto index cce63b42e..a5e4df2f8 100644 --- a/pkg/rpc/base/base.proto +++ b/pkg/rpc/base/base.proto @@ -46,6 +46,7 @@ enum Code{ ClientPieceDownloadFail = 4005; ClientRequestLimitFail = 4006; ClientConnectionError = 4007; + ClientBackSourceError = 4008; ClientPieceNotFound = 4404; // scheduler response error 5000-5999