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

Reduce Routing Table churn #90

Merged
merged 3 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 15 additions & 7 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,26 @@ func (b *bucket) peers() []PeerInfo {
return ps
}

// findFirst returns the first peer in the bucket that satisfies the given predicate.
// It is NOT safe for the predicate to mutate the given `PeerInfo` as we pass in a pointer to it.
// returns the "minimum" peer in the bucket based on the `lessThan` comparator passed to it.
// It is NOT safe for the comparator to mutate the given `PeerInfo`
// as we pass in a pointer to it.
// It is NOT safe to modify the returned value.
func (b *bucket) findFirst(p func(p *PeerInfo) bool) *PeerInfo {
for e := b.list.Front(); e != nil; e = e.Next() {
func (b *bucket) min(lessThan func(p1 *PeerInfo, p2 *PeerInfo) bool) *PeerInfo {
if b.list.Len() == 0 {
return nil
}

minVal := b.list.Front().Value.(*PeerInfo)

for e := b.list.Front().Next(); e != nil; e = e.Next() {
val := e.Value.(*PeerInfo)
if p(val) {
return val

if lessThan(val, minVal) {
minVal = val
}
}

return nil
return minVal
}

// updateAllWith updates all the peers in the bucket by applying the given update function.
Expand Down
33 changes: 18 additions & 15 deletions bucket_test.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
package kbucket

import (
"testing"
"time"

"github.com/libp2p/go-libp2p-core/test"

"github.com/stretchr/testify/require"
"testing"
)

func TestBucketFindFirst(t *testing.T) {
func TestBucketMinimum(t *testing.T) {
t.Parallel()

b := newBucket()
require.Nil(t, b.findFirst(func(p1 *PeerInfo) bool { return true }))
require.Nil(t, b.min(func(p1 *PeerInfo, p2 *PeerInfo) bool { return true }))

pid1 := test.RandPeerIDFatal(t)
pid2 := test.RandPeerIDFatal(t)
pid3 := test.RandPeerIDFatal(t)

// first is replacable
b.pushFront(&PeerInfo{Id: pid1, replaceable: true})
require.Equal(t, pid1, b.findFirst(func(p *PeerInfo) bool {
return p.replaceable
// first is min
b.pushFront(&PeerInfo{Id: pid1, LastUsefulAt: time.Now()})
require.Equal(t, pid1, b.min(func(first *PeerInfo, second *PeerInfo) bool {
return first.LastUsefulAt.Before(second.LastUsefulAt)
}).Id)

// above peer is stll the replacable one
b.pushFront(&PeerInfo{Id: pid2, replaceable: false})
require.Equal(t, pid1, b.findFirst(func(p *PeerInfo) bool {
return p.replaceable
// first is till min
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// first is till min
// first is still min

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

b.pushFront(&PeerInfo{Id: pid2, LastUsefulAt: time.Now().AddDate(1, 0, 0)})
require.Equal(t, pid1, b.min(func(first *PeerInfo, second *PeerInfo) bool {
return first.LastUsefulAt.Before(second.LastUsefulAt)
}).Id)

// new peer is replacable.
b.pushFront(&PeerInfo{Id: pid3, replaceable: true})
require.Equal(t, pid3, b.findFirst(func(p *PeerInfo) bool {
return p.replaceable
// second is the min
b.pushFront(&PeerInfo{Id: pid3, LastUsefulAt: time.Now().AddDate(-1, 0, 0)})
require.Equal(t, pid3, b.min(func(first *PeerInfo, second *PeerInfo) bool {
return first.LastUsefulAt.Before(second.LastUsefulAt)
}).Id)
}

Expand Down
6 changes: 3 additions & 3 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool, isReplaceable bool) (

// the bucket to which the peer belongs is full. Let's try to find a peer
// in that bucket which is replaceable.
replaceablePeer := bucket.findFirst(func(p *PeerInfo) bool {
return p.replaceable
replaceablePeer := bucket.min(func(p1 *PeerInfo, p2 *PeerInfo) bool {
return p1.replaceable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a comment here about us not needing a stable sort here just to call it out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

})

if replaceablePeer != nil {
if replaceablePeer != nil && replaceablePeer.replaceable {
// let's evict it and add the new peer
if rt.removePeer(replaceablePeer.Id) {
bucket.pushFront(&PeerInfo{
Expand Down