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 #440 from Starnop/const-code
Browse files Browse the repository at this point in the history
refactor: extract the common const code to the common folder
  • Loading branch information
lowzj authored Mar 19, 2019
2 parents e02e3b2 + 001bc1f commit 961b500
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 81 deletions.
91 changes: 91 additions & 0 deletions common/constants/dfget_super_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package constants

// This file defines the code required for both dfget and supernode.

var cmmap = make(map[int]string)

func init() {
cmmap[Success] = "success"

cmmap[CodeSystemError] = "system error"
cmmap[CodeParamError] = "param is illegal"
cmmap[CodeTargetNotFound] = "target not found"

cmmap[CodePeerFinish] = "peer task end"
cmmap[CodePeerContinue] = "peer task go on"
cmmap[CodePeerWait] = "peer task wait"
cmmap[CodePeerLimited] = "peer down limit"
cmmap[CodeSuperFail] = "super node sync source fail"
cmmap[CodeUnknownError] = "unknown error"
cmmap[CodeTaskConflict] = "task conflict"
cmmap[CodeURLNotReachable] = "url is not reachable"
cmmap[CodeNeedAuth] = "need auth"
cmmap[CodeWaitAuth] = "wait auth"
}

// GetMsgByCode gets the description of the code.
func GetMsgByCode(code int) string {
if v, ok := cmmap[code]; ok {
return v
}
return ""
}

const (
// HTTPError represents that there is an error between client and server.
HTTPError = -100
)

/* the response code returned by supernode */
const (
// Success represents the request is success.
Success = 200

CodeSystemError = 500
CodeParamError = 501
CodeTargetNotFound = 502

CodePeerFinish = 600
CodePeerContinue = 601
CodePeerWait = 602
CodePeerLimited = 603
CodeSuperFail = 604
CodeUnknownError = 607
CodeTaskConflict = 608
CodeURLNotReachable = 609
CodeNeedAuth = 610
CodeWaitAuth = 611
CodeSourceError = 612
)

/* the code of task result that dfget will report to supernode */
const (
ResultFail = 500
ResultSuc = 501
ResultInvalid = 502
// ResultSemiSuc represents the result is partial successful.
ResultSemiSuc = 503
)

/* the code of task status that dfget will report to supernode */
const (
TaskStatusStart = 700
TaskStatusRunning = 701
TaskStatusFinish = 702
)
35 changes: 0 additions & 35 deletions dfget/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,6 @@ import (
"time"
)

/* the response code from supernode */
const (
// HTTPError represents that there is an error between client and server.
HTTPError = -100
// Success represents the request is success.
Success = 200
ResultFail = 500
ResultSuc = 501
ResultInvalid = 502
// ResultSemiSuc represents the result is partial successful.
ResultSemiSuc = 503
)

/* report status of task to supernode */
const (
TaskStatusStart = 700
TaskStatusRunning = 701
TaskStatusFinish = 702
)

/* the task code get from supernode */
const (
TaskCodeFinish = 600
TaskCodeContinue = 601
TaskCodeWait = 602
TaskCodeLimited = 603
TaskCodeSuperFail = 604
TaskCodeUnknownError = 605
TaskCodeTaskConflict = 606
TaskCodeURLNotReachable = 607
TaskCodeNeedAuth = 608
TaskCodeWaitAuth = 609
TaskCodeSourceError = 610
)

/* the reason of backing to source */
const (
BackSourceReasonNone = 0
Expand Down
9 changes: 5 additions & 4 deletions dfget/core/api/supernode_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
"fmt"
"strings"

"github.com/dragonflyoss/Dragonfly/dfget/config"
"github.com/dragonflyoss/Dragonfly/common/constants"
"github.com/dragonflyoss/Dragonfly/dfget/types"

"github.com/go-check/check"
)

Expand Down Expand Up @@ -67,20 +68,20 @@ func (s *SupernodeAPITestSuite) TestSupernodeAPI_Register(c *check.C) {
c.Assert(r, check.NotNil)
c.Assert(r.Code, check.Equals, 0)

res.Code = config.Success
res.Code = constants.Success
res.Data = &types.RegisterResponseData{FileLength: int64(32)}
s.mock.postJSON = s.mock.createPostJSONFunc(200, []byte(res.String()), nil)
r, e = s.api.Register(ip, createRegisterRequest())
c.Assert(r, check.NotNil)
c.Assert(r.Code, check.Equals, config.Success)
c.Assert(r.Code, check.Equals, constants.Success)
c.Assert(r.Data.FileLength, check.Equals, res.Data.FileLength)
}

func (s *SupernodeAPITestSuite) TestSupernodeAPI_PullPieceTask(c *check.C) {
ip := "127.0.0.1"

res := &types.PullPieceTaskResponse{BaseResponse: &types.BaseResponse{}}
res.Code = config.TaskCodeFinish
res.Code = constants.CodePeerFinish
res.Data = []byte(`{"fileLength":2}`)
s.mock.get = s.mock.createGetFunc(200, []byte(res.String()), nil)

Expand Down
3 changes: 2 additions & 1 deletion dfget/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strings"
"time"

"github.com/dragonflyoss/Dragonfly/common/constants"
cutil "github.com/dragonflyoss/Dragonfly/common/util"
"github.com/dragonflyoss/Dragonfly/dfget/config"
"github.com/dragonflyoss/Dragonfly/dfget/core/api"
Expand Down Expand Up @@ -149,7 +150,7 @@ func registerToSuperNode(cfg *config.Config, register regist.SupernodeRegister)

result, e := register.Register(cfg.RV.PeerPort)
if e != nil {
if e.Code == config.TaskCodeNeedAuth {
if e.Code == constants.CodeNeedAuth {
return nil, e
}
cfg.BackSourceReason = config.BackSourceReasonRegisterFail
Expand Down
35 changes: 18 additions & 17 deletions dfget/core/downloader/p2p_downloader/p2p_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strconv"
"time"

"github.com/dragonflyoss/Dragonfly/common/constants"
cutil "github.com/dragonflyoss/Dragonfly/common/util"
"github.com/dragonflyoss/Dragonfly/dfget/config"
"github.com/dragonflyoss/Dragonfly/dfget/core/api"
Expand Down Expand Up @@ -122,7 +123,7 @@ func (p2p *P2PDownloader) init() {
p2p.RegisterResult.PieceSize, p2p.RegisterResult.PieceSize

p2p.queue = util.NewQueue(0)
p2p.queue.Put(NewPieceSimple(p2p.taskID, p2p.node, config.TaskStatusStart))
p2p.queue.Put(NewPieceSimple(p2p.taskID, p2p.node, constants.TaskStatusStart))

p2p.clientQueue = util.NewQueue(p2p.cfg.ClientQueueSize)

Expand Down Expand Up @@ -165,14 +166,14 @@ func (p2p *P2PDownloader) Run() error {
response, err := p2p.pullPieceTask(&curItem)
if err == nil {
code := response.Code
if code == config.TaskCodeContinue {
if code == constants.CodePeerContinue {
p2p.processPiece(response, &curItem)
} else if code == config.TaskCodeFinish {
} else if code == constants.CodePeerFinish {
p2p.finishTask(response, clientWriter)
return nil
} else {
logrus.Warnf("request piece result:%v", response)
if code == config.TaskCodeSourceError {
if code == constants.CodeSourceError {
p2p.cfg.BackSourceReason = config.BackSourceReasonSourceError
}
}
Expand Down Expand Up @@ -224,7 +225,7 @@ func (p2p *P2PDownloader) pullPieceTask(item *Piece) (
for {
if res, err = p2p.API.PullPieceTask(item.SuperNode, req); err != nil {
logrus.Errorf("pull piece task error: %v", err)
} else if res.Code == config.TaskCodeWait {
} else if res.Code == constants.CodePeerWait {
sleepTime := time.Duration(rand.Intn(1400)+600) * time.Millisecond
logrus.Infof("pull piece task result:%s and sleep %.3fs",
res, sleepTime.Seconds())
Expand All @@ -234,18 +235,18 @@ func (p2p *P2PDownloader) pullPieceTask(item *Piece) (
break
}

if res == nil || (res.Code != config.TaskCodeContinue &&
res.Code != config.TaskCodeFinish &&
res.Code != config.TaskCodeLimited &&
res.Code != config.Success) {
if res == nil || (res.Code != constants.CodePeerContinue &&
res.Code != constants.CodePeerFinish &&
res.Code != constants.CodePeerLimited &&
res.Code != constants.Success) {
logrus.Errorf("pull piece task fail:%v and will migrate", res)

var registerRes *regist.RegisterResult
if registerRes, err = p2p.Register.Register(p2p.cfg.RV.PeerPort); err != nil {
return nil, err
}
p2p.pieceSizeHistory[1] = registerRes.PieceSize
item.Status = config.TaskStatusStart
item.Status = constants.TaskStatusStart
item.SuperNode = registerRes.Node
item.TaskID = registerRes.TaskID
util.Printer.Println("migrated to node:" + item.SuperNode)
Expand Down Expand Up @@ -328,8 +329,8 @@ func (p2p *P2PDownloader) getItem(latestItem *Piece) (bool, *Piece) {
logrus.Warnf("pieceRange:%s is neither running nor success", item.Range)
return false, latestItem
}
if !v && (item.Result == config.ResultSemiSuc ||
item.Result == config.ResultSuc) {
if !v && (item.Result == constants.ResultSemiSuc ||
item.Result == constants.ResultSuc) {
p2p.total += int64(item.Content.Len())
p2p.pieceSet[item.Range] = true
} else if !v {
Expand All @@ -344,9 +345,9 @@ func (p2p *P2PDownloader) getItem(latestItem *Piece) (bool, *Piece) {
if cutil.IsNil(latestItem) {
return false, latestItem
}
if latestItem.Result == config.ResultSuc ||
latestItem.Result == config.ResultFail ||
latestItem.Result == config.ResultInvalid {
if latestItem.Result == constants.ResultSuc ||
latestItem.Result == constants.ResultFail ||
latestItem.Result == constants.ResultInvalid {
needMerge = false
}
runningCount := 0
Expand Down Expand Up @@ -380,8 +381,8 @@ func (p2p *P2PDownloader) processPiece(response *types.PullPieceTaskResponse,
p2p.node,
pieceTask.Cid,
pieceRange,
config.ResultSemiSuc,
config.TaskStatusRunning))
constants.ResultSemiSuc,
constants.TaskStatusRunning))
continue
}
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions dfget/core/downloader/p2p_downloader/piece.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"bytes"
"encoding/json"

"github.com/dragonflyoss/Dragonfly/common/constants"
"github.com/dragonflyoss/Dragonfly/common/util"
"github.com/dragonflyoss/Dragonfly/dfget/config"
)

// Piece contains all information of a piece.
Expand Down Expand Up @@ -90,7 +90,7 @@ func NewPieceSimple(taskID string, node string, status int) *Piece {
TaskID: taskID,
SuperNode: node,
Status: status,
Result: config.ResultInvalid,
Result: constants.ResultInvalid,
Content: &bytes.Buffer{},
}
}
Expand Down
5 changes: 3 additions & 2 deletions dfget/core/downloader/p2p_downloader/power_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"
"time"

"github.com/dragonflyoss/Dragonfly/common/constants"
cutil "github.com/dragonflyoss/Dragonfly/common/util"
"github.com/dragonflyoss/Dragonfly/dfget/config"
"github.com/dragonflyoss/Dragonfly/dfget/core/api"
Expand Down Expand Up @@ -148,15 +149,15 @@ func (pc *PowerClient) createDownloadRequest() *api.DownloadRequest {

func (pc *PowerClient) successPiece(content *bytes.Buffer) *Piece {
piece := NewPieceContent(pc.taskID, pc.node, pc.pieceTask.Cid, pc.pieceTask.Range,
config.ResultSemiSuc, config.TaskStatusRunning, content)
constants.ResultSemiSuc, constants.TaskStatusRunning, content)
piece.PieceSize = pc.pieceTask.PieceSize
piece.PieceNum = pc.pieceTask.PieceNum
return piece
}

func (pc *PowerClient) failPiece() *Piece {
return NewPiece(pc.taskID, pc.node, pc.pieceTask.Cid, pc.pieceTask.Range,
config.ResultFail, config.TaskStatusRunning)
constants.ResultFail, constants.TaskStatusRunning)
}

func (pc *PowerClient) is2xxStatus(code int) bool {
Expand Down
9 changes: 5 additions & 4 deletions dfget/core/helper/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"path"

"github.com/dragonflyoss/Dragonfly/common/constants"
"github.com/dragonflyoss/Dragonfly/common/util"
"github.com/dragonflyoss/Dragonfly/dfget/config"
"github.com/dragonflyoss/Dragonfly/dfget/types"
Expand Down Expand Up @@ -164,13 +165,13 @@ func CreateRegisterFunc() RegisterFuncType {
case "":
return newResponse(501, "invalid source url"), nil
case "http://taobao.com":
return newResponse(config.TaskCodeNeedAuth, "need auth"), nil
return newResponse(constants.CodeNeedAuth, "need auth"), nil
case "http://github.com":
return newResponse(config.TaskCodeWaitAuth, "wait auth"), nil
return newResponse(constants.CodeWaitAuth, "wait auth"), nil
case "http://x.com":
return newResponse(config.TaskCodeURLNotReachable, "not reachable"), nil
return newResponse(constants.CodeURLNotReachable, "not reachable"), nil
case "http://lowzj.com":
resp := newResponse(config.Success, "")
resp := newResponse(constants.Success, "")
resp.Data = &types.RegisterResponseData{
TaskID: "a",
FileLength: 100,
Expand Down
11 changes: 6 additions & 5 deletions dfget/core/regist/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"time"

"github.com/dragonflyoss/Dragonfly/common/constants"
"github.com/dragonflyoss/Dragonfly/common/util"
"github.com/dragonflyoss/Dragonfly/dfget/config"
"github.com/dragonflyoss/Dragonfly/dfget/core/api"
Expand Down Expand Up @@ -68,10 +69,10 @@ func (s *supernodeRegister) Register(peerPort int) (*RegisterResult, *errors.DFG
logrus.Errorf("register to node:%s error:%v", nodes[i], e)
continue
}
if resp.Code == config.Success || resp.Code == config.TaskCodeNeedAuth {
if resp.Code == constants.Success || resp.Code == constants.CodeNeedAuth {
break
}
if resp.Code == config.TaskCodeWaitAuth && retryTimes < 3 {
if resp.Code == constants.CodeWaitAuth && retryTimes < 3 {
i--
retryTimes++
logrus.Infof("sleep 2.5s to wait auth(%d/3)...", retryTimes)
Expand All @@ -94,12 +95,12 @@ func (s *supernodeRegister) Register(peerPort int) (*RegisterResult, *errors.DFG

func (s *supernodeRegister) checkResponse(resp *types.RegisterResponse, e error) *errors.DFGetError {
if e != nil {
return errors.New(config.HTTPError, e.Error())
return errors.New(constants.HTTPError, e.Error())
}
if resp == nil {
return errors.New(config.HTTPError, "empty response, unknown error")
return errors.New(constants.HTTPError, "empty response, unknown error")
}
if resp.Code != config.Success {
if resp.Code != constants.Success {
return errors.New(resp.Code, resp.Msg)
}
return nil
Expand Down
Loading

0 comments on commit 961b500

Please sign in to comment.