Skip to content

Commit

Permalink
neutrino: Added ResetRanking method to PeerRanking.
Browse files Browse the repository at this point in the history
Signed-off-by: Maureen Ononiwu <[email protected]>
  • Loading branch information
Chinwendu20 committed Sep 2, 2023
1 parent 42a196f commit e84cf5b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
9 changes: 9 additions & 0 deletions query/peer_rank.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,12 @@ func (p *peerRanking) Reward(peer string) {

p.rank[peer] = score - 1
}

func (p *peerRanking) ResetRanking(peer string) {
_, ok := p.rank[peer]
if !ok {
return
}

p.rank[peer] = defaultScore
}
27 changes: 24 additions & 3 deletions query/peer_rank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,35 @@ func TestPeerRank(t *testing.T) {
}
}

// Lastly, reward the lowest scored one a bunch, which should move it
// This is the lowest scored peer after punishment.
const lowestScoredPeer = "peer0"

// Reward the lowest scored one a bunch, which should move it
// to the front.
for i := 0; i < 10; i++ {
ranking.Reward("peer0")
ranking.Reward(lowestScoredPeer)
}

ranking.Order(peers)
if peers[0] != "peer0" {
if peers[0] != lowestScoredPeer {
t.Fatalf("peer0 was not first")
}

// Punish the peer a bunch to make it the lowest scored one.
for i := 0; i < 10; i++ {
ranking.Punish(lowestScoredPeer)
}

ranking.Order(peers)
if peers[len(peers)-1] != lowestScoredPeer {
t.Fatalf("peer0 should be last")
}

// Reset its ranking. It should have the default score now
// and should not be the lowest ranked peer.
ranking.ResetRanking(lowestScoredPeer)
ranking.Order(peers)
if peers[len(peers)-1] == lowestScoredPeer {
t.Fatalf("peer0 should not be last.")
}
}
7 changes: 7 additions & 0 deletions query/workmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type PeerRanking interface {

// Order sorst the slice of peers according to their ranking.
Order(peers []string)

ResetRanking(peerAddr string)
}

// activeWorker wraps a Worker that is currently running, together with the job
Expand Down Expand Up @@ -377,6 +379,11 @@ Loop:
result.job.timeout = newTimeout
}

// Refresh peer rank on disconnect.
if result.err == ErrPeerDisconnected {
w.cfg.Ranking.ResetRanking(result.peer.Addr())
}

heap.Push(work, result.job)
currentQueries[result.job.index] = batchNum

Expand Down
3 changes: 3 additions & 0 deletions query/workmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func (p *mockPeerRanking) Punish(peer string) {
func (p *mockPeerRanking) Reward(peer string) {
}

func (p *mockPeerRanking) ResetRanking(peer string) {
}

// startWorkManager starts a new workmanager with the given number of mock
// workers.
func startWorkManager(t *testing.T, numWorkers int) (WorkManager,
Expand Down

0 comments on commit e84cf5b

Please sign in to comment.