Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When priority is equal, use FIFO #16

Merged
merged 3 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/ipfs/go-peertaskqueue
go 1.16

require (
github.com/benbjohnson/clock v1.1.0
github.com/ipfs/go-ipfs-pq v0.0.2
github.com/libp2p/go-libp2p-core v0.0.1
github.com/multiformats/go-multihash v0.0.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/btcsuite/btcd v0.0.0-20190213025234-306aecffea32 h1:qkOC5Gd33k54tobS36cXdAzJbeHaduLtnLQQwNoIi78=
github.com/btcsuite/btcd v0.0.0-20190213025234-306aecffea32/go.mod h1:DrZx5ec/dmnfpw9KyYoQyYo7d0KEvTkk/5M/vbZjAr8=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
Expand Down
2 changes: 1 addition & 1 deletion peertask/peertask.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var FIFOCompare = func(a, b *QueueTask) bool {
// PriorityCompare respects the target peer's task priority. For tasks involving
// different peers, the oldest task is prioritized.
var PriorityCompare = func(a, b *QueueTask) bool {
if a.Target == b.Target {
if a.Target == b.Target && a.Priority != b.Priority {
return a.Priority > b.Priority
}
return FIFOCompare(a, b)
Expand Down
6 changes: 4 additions & 2 deletions peertracker/peertracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package peertracker

import (
"sync"
"time"

"github.com/benbjohnson/clock"
pq "github.com/ipfs/go-ipfs-pq"
"github.com/ipfs/go-peertaskqueue/peertask"
peer "github.com/libp2p/go-libp2p-core/peer"
)

var clockInstance = clock.New()

// TaskMerger is an interface that is used to merge new tasks into the active
// and pending queues
type TaskMerger interface {
Expand Down Expand Up @@ -144,7 +146,7 @@ func (p *PeerTracker) SetIndex(i int) {

// PushTasks adds a group of tasks onto a peer's queue
func (p *PeerTracker) PushTasks(tasks ...peertask.Task) {
now := time.Now()
now := clockInstance.Now()

p.activelk.Lock()
defer p.activelk.Unlock()
Expand Down
59 changes: 59 additions & 0 deletions peertracker/peertracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package peertracker

import (
"testing"
"time"

"github.com/benbjohnson/clock"
"github.com/ipfs/go-peertaskqueue/peertask"
"github.com/ipfs/go-peertaskqueue/testutil"
)
Expand Down Expand Up @@ -568,3 +570,60 @@ func TestRemoveActive(t *testing.T) {
t.Fatal("Expected tasks in order")
}
}

func TestPushPopEqualTaskPriorities(t *testing.T) {
partner := testutil.GeneratePeers(1)[0]
clock := clock.NewMock()
oldClock := clockInstance
clockInstance = clock
t.Cleanup(func() {
clockInstance = oldClock
})
tracker := New(partner, &DefaultTaskMerger{}, 1)

tasks := []peertask.Task{
{
Topic: "1",
Priority: 10,
Work: 1,
},
{
Topic: "2",
Priority: 10,
Work: 1,
},
{
Topic: "3",
Priority: 10,
Work: 1,
},
}
tracker.PushTasks(tasks[0])
clock.Add(10 * time.Millisecond)
tracker.PushTasks(tasks[1])
clock.Add(10 * time.Millisecond)
tracker.PushTasks(tasks[2])
popped, _ := tracker.PopTasks(1)
if len(popped) != 1 {
t.Fatal("Expected 1 task")
}
if popped[0].Topic != "1" {
t.Fatal("Expected first task")
}
tracker.TaskDone(popped[0])
popped, _ = tracker.PopTasks(1)
if len(popped) != 1 {
t.Fatal("Expected 1 task")
}
if popped[0].Topic != "2" {
t.Fatal("Expected second task")
}
tracker.TaskDone(popped[0])
popped, _ = tracker.PopTasks(1)
if len(popped) != 1 {
t.Fatal("Expected 1 task")
}
if popped[0].Topic != "3" {
t.Fatal("Expected third task")
}
}