diff --git a/common/constants/dfget_super_code.go b/common/constants/dfget_super_code.go index b59c3fc75..870045b4a 100644 --- a/common/constants/dfget_super_code.go +++ b/common/constants/dfget_super_code.go @@ -72,6 +72,8 @@ const ( CodeNeedAuth = 608 CodeWaitAuth = 609 CodeSourceError = 610 + CodeGetPieceReport = 611 + CodeGetPeerDown = 612 ) /* the code of task result that dfget will report to supernode */ diff --git a/dfget/core/api/supernode_api.go b/dfget/core/api/supernode_api.go index 674e1bb40..a1f934a49 100644 --- a/dfget/core/api/supernode_api.go +++ b/dfget/core/api/supernode_api.go @@ -21,8 +21,11 @@ import ( "fmt" "time" + "github.com/dragonflyoss/Dragonfly/common/constants" "github.com/dragonflyoss/Dragonfly/common/util" "github.com/dragonflyoss/Dragonfly/dfget/types" + + "github.com/sirupsen/logrus" ) /* the url paths of supernode APIs*/ @@ -90,7 +93,9 @@ func (api *supernodeAPI) PullPieceTask(node string, req *types.PullPieceTaskRequ api.Scheme, node, peerPullPieceTaskPath, util.ParseQuery(req)) resp = new(types.PullPieceTaskResponse) - e = api.get(url, resp) + if e = api.get(url, resp); e != nil { + return nil, e + } return } @@ -100,9 +105,15 @@ func (api *supernodeAPI) ReportPiece(node string, req *types.ReportPieceRequest) url := fmt.Sprintf("%s://%s%s?%s", api.Scheme, node, peerReportPiecePath, util.ParseQuery(req)) - resp = new(types.BaseResponse) e = api.get(url, resp) + if e != nil { + logrus.Errorf("failed to report piece{taskid:%s,range:%s},err: %v", req.TaskID, req.PieceRange, e) + return nil, e + } + if resp != nil && resp.Code != constants.CodeGetPieceReport { + logrus.Errorf("failed to report piece{taskid:%s,range:%s} to supernode: api response code is %d not equal to %d", req.TaskID, req.PieceRange, resp.Code, constants.CodeGetPieceReport) + } return } @@ -115,6 +126,13 @@ func (api *supernodeAPI) ServiceDown(node string, taskID string, cid string) ( resp = new(types.BaseResponse) e = api.get(url, resp) + if e != nil { + logrus.Errorf("failed to send service down,err: %v", e) + return nil, e + } + if resp != nil && resp.Code != constants.CodeGetPeerDown { + logrus.Errorf("failed to send service down to supernode: api response code is %d not equal to %d", resp.Code, constants.CodeGetPeerDown) + } return } diff --git a/dfget/core/api/supernode_api_test.go b/dfget/core/api/supernode_api_test.go index 1de6fb6ec..5f476541e 100644 --- a/dfget/core/api/supernode_api_test.go +++ b/dfget/core/api/supernode_api_test.go @@ -100,9 +100,12 @@ func (s *SupernodeAPITestSuite) TestSupernodeAPI_PullPieceTask(c *check.C) { func (s *SupernodeAPITestSuite) TestSupernodeAPI_ReportPiece(c *check.C) { ip := "127.0.0.1" - + req := &types.ReportPieceRequest{ + TaskID: "sssss", + PieceRange: "0-11", + } s.mock.GetFunc = s.mock.CreateGetFunc(200, []byte(`{"Code":700}`), nil) - r, e := s.api.ReportPiece(ip, nil) + r, e := s.api.ReportPiece(ip, req) c.Check(e, check.IsNil) c.Check(r.Code, check.Equals, 700) } diff --git a/supernode/server/0.3_bridge.go b/supernode/server/0.3_bridge.go index ab524a6ae..19ad2b867 100644 --- a/supernode/server/0.3_bridge.go +++ b/supernode/server/0.3_bridge.go @@ -198,8 +198,9 @@ func (s *Server) reportPiece(ctx context.Context, rw http.ResponseWriter, req *h return err } - rw.WriteHeader(http.StatusOK) - return nil + return EncodeResponse(rw, http.StatusOK, &types.ResultInfo{ + Code: constants.CodeGetPieceReport, + }) } func (s *Server) reportServiceDown(ctx context.Context, rw http.ResponseWriter, req *http.Request) (err error) { @@ -228,6 +229,7 @@ func (s *Server) reportServiceDown(ctx context.Context, rw http.ResponseWriter, return err } - rw.WriteHeader(http.StatusOK) - return nil + return EncodeResponse(rw, http.StatusOK, &types.ResultInfo{ + Code: constants.CodeGetPeerDown, + }) }