Skip to content

Commit

Permalink
Fixed log level and Append comment
Browse files Browse the repository at this point in the history
  • Loading branch information
suyanlong authored and vipwzw committed Aug 2, 2023
1 parent 9e284ed commit c909f4d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
31 changes: 21 additions & 10 deletions system/p2p/dht/protocol/download/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,64 @@ import (
"time"
)

func peersCounterKey(taskId, pid string) string {
return fmt.Sprintf("%s-%s", taskId, pid)
func peersCounterKey(taskID, pid string) string {
return fmt.Sprintf("%s-%s", taskID, pid)
}

type heightCostTime struct {
height int64
costTime int64
}

// PeerTaskCounter ...
type PeerTaskCounter struct {
pid string
latencys []time.Duration
latencies []time.Duration
taskID string
heightCostTimes []heightCostTime
}

// NewPeerTaskCounter ...
func NewPeerTaskCounter(pid, taskID string) *PeerTaskCounter {
return &PeerTaskCounter{
pid: pid,
taskID: taskID,
latencys: []time.Duration{},
latencies: []time.Duration{},
heightCostTimes: []heightCostTime{},
}
}

// Pretty print information
func (p *PeerTaskCounter) Pretty() string {
return fmt.Sprintf("pid = %s, taskID = %s, latencys = %v, counter = %d, heightCostTimes = %v", p.pid, p.taskID, p.latencys, len(p.heightCostTimes), p.heightCostTimes)
return fmt.Sprintf("pid = %s, taskID = %s, latencys = %v, counter = %d, heightCostTimes = %v", p.pid, p.taskID, p.latencies, len(p.heightCostTimes), p.heightCostTimes)
}

// Append add counter info
func (p *PeerTaskCounter) Append(height, costTime int64) {
p.heightCostTimes = append(p.heightCostTimes, heightCostTime{
height: height,
costTime: costTime,
})
}

// AppendLatency ...
func (p *PeerTaskCounter) AppendLatency(latency time.Duration) {
p.latencys = append(p.latencys, latency)
p.latencies = append(p.latencies, latency)
}

// Counter ..
func (p *PeerTaskCounter) Counter() int64 {
return int64(len(p.heightCostTimes))
}

// Counter ...
type Counter struct {
taskCounter map[string][]string //taskID:pid
peerCounter map[string]*PeerTaskCounter //taskID-pid:PeerTaskCounter
rw sync.Mutex
}

// NewCounter ...
func NewCounter() *Counter {
return &Counter{
taskCounter: map[string][]string{},
Expand All @@ -64,6 +72,7 @@ func NewCounter() *Counter {
}
}

// UpdateTaskInfo ...
func (c *Counter) UpdateTaskInfo(taskID, pid string, height, costTime int64) {
c.rw.Lock()
if counter, ok := c.peerCounter[peersCounterKey(taskID, pid)]; ok {
Expand All @@ -72,6 +81,7 @@ func (c *Counter) UpdateTaskInfo(taskID, pid string, height, costTime int64) {
c.rw.Unlock()
}

// AddTaskInfo ...
func (c *Counter) AddTaskInfo(taskID, pid string, latency time.Duration) {
c.rw.Lock()
if ps, ok := c.taskCounter[taskID]; ok {
Expand All @@ -90,17 +100,18 @@ func (c *Counter) AddTaskInfo(taskID, pid string, latency time.Duration) {
c.rw.Unlock()
}

func (c *Counter) Release(tasksId string) {
// Release task by id
func (c *Counter) Release(tasksID string) {
c.rw.Lock()
defer c.rw.Unlock()
if pids, ok := c.taskCounter[tasksId]; ok {
if pids, ok := c.taskCounter[tasksID]; ok {
for _, pid := range pids {
key := peersCounterKey(tasksId, pid)
key := peersCounterKey(tasksID, pid)
if counter, ok := c.peerCounter[key]; ok {
log.Info("Release", "Counter ", counter.Pretty())
delete(c.peerCounter, key)
}
}
delete(c.taskCounter, tasksId)
delete(c.taskCounter, tasksID)
}
}
2 changes: 1 addition & 1 deletion system/p2p/dht/protocol/download/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (p *Protocol) handleEventDownloadBlock(msg *queue.Message) {

wg.Wait()
p.checkTask(taskID, pids, reDownload)
log.Info("Download Job Complete!", "TaskID++++++++++++++", taskID,
log.Debug("Download Job Complete!", "TaskID++++++++++++++", taskID,
"height diff", req.GetEnd()-req.GetStart()+1,
"cost time", fmt.Sprintf("cost time:%d ms", time.Since(startTime).Milliseconds()),
"from", pids,
Expand Down

0 comments on commit c909f4d

Please sign in to comment.