Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #682 from yunfeiyangbuaa/supernodeAPI
Browse files Browse the repository at this point in the history
bugfix: modify two supernode api's bug
  • Loading branch information
starnop authored Jul 31, 2019
2 parents 2dcc858 + cbe57dc commit c86ccf5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
2 changes: 2 additions & 0 deletions common/constants/dfget_super_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
28 changes: 23 additions & 5 deletions dfget/core/api/supernode_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down Expand Up @@ -77,7 +80,9 @@ func (api *supernodeAPI) Register(node string, req *types.RegisterRequest) (
return nil, fmt.Errorf("%d:%s", code, body)
}
resp = new(types.RegisterResponse)
e = json.Unmarshal(body, resp)
if e = json.Unmarshal(body, resp); e != nil {
return nil, e
}
return resp, e
}

Expand All @@ -90,7 +95,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
}

Expand All @@ -100,9 +107,14 @@ 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 = api.get(url, resp); 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
}

Expand All @@ -114,7 +126,13 @@ func (api *supernodeAPI) ServiceDown(node string, taskID string, cid string) (
api.Scheme, node, peerServiceDownPath, taskID, cid)

resp = new(types.BaseResponse)
e = api.get(url, resp)
if e = api.get(url, resp); 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
}

Expand Down
7 changes: 5 additions & 2 deletions dfget/core/api/supernode_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 6 additions & 4 deletions supernode/server/0.3_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,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) {
Expand Down Expand Up @@ -230,6 +231,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,
})
}

0 comments on commit c86ccf5

Please sign in to comment.