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

Commit

Permalink
bugfix: update cdn fail when the content is being read and the source…
Browse files Browse the repository at this point in the history
… server is down

Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop committed Jan 13, 2020
1 parent 239454c commit 7d00289
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
Binary file added dfget-31028-1577713727.584.tmp-027362658
Binary file not shown.
8 changes: 4 additions & 4 deletions dfget/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ func downloadFile(cfg *config.Config, supernodeAPI api.SupernodeAPI,
}

if success {
logrus.Infof("download SUCCESS from supernode %s cost:%.3fs length:%d",
cfg.Nodes, time.Since(cfg.StartTime).Seconds(), cfg.RV.FileLength)
logrus.Infof("download SUCCESS cost:%.3fs length:%d",
time.Since(cfg.StartTime).Seconds(), cfg.RV.FileLength)
} else {
logrus.Infof("download FAIL from supernode %s cost:%.3fs length:%d reason:%d",
cfg.Nodes, time.Since(cfg.StartTime).Seconds(), cfg.RV.FileLength, cfg.BackSourceReason)
logrus.Infof("download FAIL cost:%.3fs length:%d reason:%d error:%v",
time.Since(cfg.StartTime).Seconds(), cfg.RV.FileLength, cfg.BackSourceReason, err)
}
return err
}
Expand Down
29 changes: 24 additions & 5 deletions dfget/core/regist/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ type SupernodeRegister interface {
}

type supernodeRegister struct {
api api.SupernodeAPI
cfg *config.Config
api api.SupernodeAPI
cfg *config.Config
lastRegisteredNode string
}

var _ SupernodeRegister = &supernodeRegister{}
Expand All @@ -68,6 +69,10 @@ func (s *supernodeRegister) Register(peerPort int) (*RegisterResult, *errortypes
nodes, nLen := s.cfg.Nodes, len(s.cfg.Nodes)
req := s.constructRegisterRequest(peerPort)
for i = 0; i < nLen; i++ {
if s.lastRegisteredNode == nodes[i] {
logrus.Warnf("the last registered node is the same(%s)", nodes[i])
continue
}
req.SupernodeIP = netutils.ExtractHost(nodes[i])
resp, e = s.api.Register(nodes[i], req)
logrus.Infof("do register to %s, res:%s error:%v", nodes[i], resp, e)
Expand All @@ -86,6 +91,7 @@ func (s *supernodeRegister) Register(peerPort int) (*RegisterResult, *errortypes
time.Sleep(2500 * time.Millisecond)
}
}
s.setLastRegisteredNode(i)
s.setRemainderNodes(i)
if err := s.checkResponse(resp, e); err != nil {
logrus.Errorf("register fail:%v", err)
Expand Down Expand Up @@ -113,16 +119,29 @@ func (s *supernodeRegister) checkResponse(resp *types.RegisterResponse, e error)
return nil
}

func (s *supernodeRegister) setLastRegisteredNode(idx int) {
nLen := len(s.cfg.Nodes)
if nLen <= 0 {
return
}
if idx >= nLen {
s.lastRegisteredNode = ""
return
}
s.lastRegisteredNode = s.cfg.Nodes[idx]
}

func (s *supernodeRegister) setRemainderNodes(idx int) {
nLen := len(s.cfg.Nodes)
if nLen <= 0 {
return
}
if idx < nLen {
s.cfg.Nodes = s.cfg.Nodes[idx+1:]
} else {
if idx >= nLen {
s.cfg.Nodes = []string{}
return
}

s.cfg.Nodes = s.cfg.Nodes[idx+1:]
}

func (s *supernodeRegister) constructRegisterRequest(port int) *types.RegisterRequest {
Expand Down
7 changes: 3 additions & 4 deletions dfget/core/regist/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ func (s *RegistTestSuite) TestSupernodeRegister_Register(c *check.C) {
m := new(MockSupernodeAPI)
m.RegisterFunc = CreateRegisterFunc()

register := NewSupernodeRegister(cfg, m)

var f = func(ec int, msg string, data *RegisterResult) {
register := NewSupernodeRegister(cfg, m)
resp, e := register.Register(0)
if msg == "" {
c.Assert(e, check.IsNil)
Expand All @@ -92,7 +91,7 @@ func (s *RegistTestSuite) TestSupernodeRegister_Register(c *check.C) {
}

cfg.Nodes = []string{""}
f(constants.HTTPError, "connection refused", nil)
f(constants.HTTPError, "empty response, unknown error", nil)

cfg.Nodes = []string{"x"}
f(501, "invalid source url", nil)
Expand All @@ -117,7 +116,7 @@ func (s *RegistTestSuite) TestSupernodeRegister_Register(c *check.C) {
func (s *RegistTestSuite) TestSupernodeRegister_constructRegisterRequest(c *check.C) {
buf := &bytes.Buffer{}
cfg := s.createConfig(buf)
register := &supernodeRegister{nil, cfg}
register := &supernodeRegister{nil, cfg, ""}

cfg.Identifier = "id"
req := register.constructRegisterRequest(0)
Expand Down
2 changes: 1 addition & 1 deletion supernode/daemon/mgr/cdn/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (cm *Manager) TriggerCDN(ctx context.Context, task *types.TaskInfo) (*types
downloadMetadata, err := cm.writer.startWriter(ctx, cm.cfg, reader, task, startPieceNum, httpFileLength, pieceContSize)
if err != nil {
logrus.Errorf("failed to write for task %s: %v", task.ID, err)
return nil, err
return getUpdateTaskInfoWithStatusOnly(types.TaskInfoCdnStatusFAILED), err
}

realMD5 := reader.Md5()
Expand Down

0 comments on commit 7d00289

Please sign in to comment.