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

fix: try to re-add existing peers when the routing table is empty #420

Merged
merged 1 commit into from
Dec 11, 2019
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
73 changes: 73 additions & 0 deletions dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,79 @@ func TestRefreshBelowMinRTThreshold(t *testing.T) {
assert.Equal(t, dhtE.self, dhtA.routingTable.Find(dhtE.self), "A's routing table should have peer E!")
}

// Check to make sure we re-fill the routing table from connected peers when it
// completely empties.
func TestEmptyTable(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

nDHTs := 50 // needs more than 40 peers so we don't add all of them to our routing table.
dhts := setupDHTS(t, ctx, nDHTs)
defer func() {
for _, dht := range dhts {
dht.Close()
defer dht.host.Close()
}
}()

t.Logf("dhts are not connected. %d", nDHTs)
for _, dht := range dhts {
rtlen := dht.routingTable.Size()
if rtlen > 0 {
t.Errorf("routing table for %s should have 0 peers. has %d", dht.self, rtlen)
}
}

for i := 1; i < nDHTs; i++ {
connectNoSync(t, ctx, dhts[0], dhts[i])
}

// Wait till the routing table stabilizes.
oldSize := dhts[0].routingTable.Size()
for {
time.Sleep(time.Millisecond)
newSize := dhts[0].routingTable.Size()
if oldSize == newSize {
break
}
oldSize = newSize
}

if u.Debug {
printRoutingTables(dhts[:1])
}

// Disconnect from all peers that _were_ in the routing table.
routingTablePeers := make(map[peer.ID]bool, nDHTs)
for _, p := range dhts[0].RoutingTable().ListPeers() {
routingTablePeers[p] = true
}

oldDHTs := dhts[1:]
dhts = dhts[:1]
for _, dht := range oldDHTs {
if routingTablePeers[dht.Host().ID()] {
dhts[0].Host().Network().ClosePeer(dht.host.ID())
dht.Close()
dht.host.Close()
} else {
dhts = append(dhts, dht)
}
}

// we should now _re-add_ some peers to the routing table
for i := 0; i < 100; i++ {
Copy link
Member

Choose a reason for hiding this comment

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

Timing conditions in tests make me sad :-( But we don't have a better way right now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not yet, unfortunately.

if dhts[0].routingTable.Size() > 0 {
return
}
time.Sleep(time.Millisecond)
}
if u.Debug {
printRoutingTables(dhts[:1])
}
t.Fatal("routing table shouldn't have been empty")
}

func TestPeriodicRefresh(t *testing.T) {
if ci.IsRunning() {
t.Skip("skipping on CI. highly timing dependent")
Expand Down
10 changes: 10 additions & 0 deletions notif.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ func (nn *netNotifiee) Disconnected(n network.Network, v network.Conn) {
}

dht.routingTable.Remove(p)
if dht.routingTable.Size() < minRTRefreshThreshold {
// TODO: Actively bootstrap. For now, just try to add the currently connected peers.
for _, p := range dht.host.Network().Peers() {
// Don't bother probing, we do that on connect.
protos, err := dht.peerstore.SupportsProtocols(p, dht.protocolStrs()...)
if err == nil && len(protos) != 0 {
dht.Update(dht.Context(), p)
}
}
}

dht.smlk.Lock()
defer dht.smlk.Unlock()
Expand Down