Skip to content

Commit

Permalink
Make DHT bucket debug node ordering stable
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Oct 7, 2024
1 parent 6707d8c commit 888ec80
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
9 changes: 8 additions & 1 deletion bucket.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dht

import (
"iter"
"maps"
"time"

"github.com/anacrolix/chansync"
Expand All @@ -19,8 +21,13 @@ func (b *bucket) Len() int {
return len(b.nodes)
}

func (b *bucket) NodeIter() iter.Seq[*node] {
return maps.Keys(b.nodes)
}

// Returns true if f returns true for all nodes. Iteration stops if f returns false.
func (b *bucket) EachNode(f func(*node) bool) bool {
for n := range b.nodes {
for n := range b.NodeIter() {
if !f(n) {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/anacrolix/dht/v2

go 1.18
go 1.23

require (
filippo.io/edwards25519 v1.0.0-rc.1
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ github.com/RoaringBitmap/roaring v0.4.23/go.mod h1:D0gp8kJQgE1A4LQ5wFLggQEyvDi06
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/alecthomas/assert/v2 v2.0.0-alpha3 h1:pcHeMvQ3OMstAWgaeaXIAL8uzB9xMm2zlxt+/4ml8lk=
github.com/alecthomas/assert/v2 v2.0.0-alpha3/go.mod h1:+zD0lmDXTeQj7TgDgCt0ePWxb0hMC1G+PGTsTCv1B9o=
github.com/alecthomas/atomic v0.1.0-alpha2 h1:dqwXmax66gXvHhsOS4pGPZKqYOlTkapELkLb3MNdlH8=
github.com/alecthomas/atomic v0.1.0-alpha2/go.mod h1:zD6QGEyw49HIq19caJDc2NMXAy8rNi9ROrxtMXATfyI=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand All @@ -57,8 +59,6 @@ github.com/anacrolix/args v0.5.1-0.20220509024600-c3b77d0b61ac h1:XWoepbk3zgOQ8j
github.com/anacrolix/args v0.5.1-0.20220509024600-c3b77d0b61ac/go.mod h1:Fj/N2PehEwTBE5t/V/9xgTcxDkuYQ+5IBoFw/8gkldI=
github.com/anacrolix/backtrace v0.0.0-20221205112523-22a61db8f82e h1:A0Ty9UeyBDIo29ZMnk0AvPqWDIa4HVvCaJqWNlCrMXA=
github.com/anacrolix/backtrace v0.0.0-20221205112523-22a61db8f82e/go.mod h1:4YFqy+788tLJWtin2jNliYVJi+8aDejG9zcu/2/pONw=
github.com/anacrolix/bargle/v2 v2.0.0-20240804050250-9655b61917bc h1:+ZEZHmyNen2jUY2gD1YSDfmISIDLo+jKrOV3fZF7ZZw=
github.com/anacrolix/bargle/v2 v2.0.0-20240804050250-9655b61917bc/go.mod h1:rKvwnOHgcXKPJTINj5RmkifgpxgEGC9bkJiv5kM4ctM=
github.com/anacrolix/bargle/v2 v2.0.0-20240909020204-5265698a6040 h1:gneDM+Y60h9YY5xyG1buJss/OWjBZa+eYFO/Go3e7ro=
github.com/anacrolix/bargle/v2 v2.0.0-20240909020204-5265698a6040/go.mod h1:rKvwnOHgcXKPJTINj5RmkifgpxgEGC9bkJiv5kM4ctM=
github.com/anacrolix/chansync v0.3.0 h1:lRu9tbeuw3wl+PhMu/r+JJCRu5ArFXIluOgdF0ao6/U=
Expand Down Expand Up @@ -253,6 +253,7 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.12.0/go.mod h1:ummNFgdgLhhX7aIiy35v
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/honeycombio/honeycomb-opentelemetry-go v0.3.0 h1:3qotL5cFNAiuLk/YZsUGNmz9ywnXqGP9hGFQoNo5PdA=
github.com/honeycombio/honeycomb-opentelemetry-go v0.3.0/go.mod h1:qzzIv/RAGWhyRgyRwwRaxmn5tZMkc/bbTX3zit4sBGI=
github.com/honeycombio/opentelemetry-go-contrib/launcher v0.0.0-20221031150637-a3c60ed98d54 h1:CFyJMKF0jR2dv+3Cpj/GuRa5XBXKnJqiqmWMYifTzok=
Expand Down Expand Up @@ -423,6 +424,7 @@ go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
10 changes: 7 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net"
"runtime/pprof"
"slices"
"strings"
"text/tabwriter"
"time"
Expand Down Expand Up @@ -99,7 +100,11 @@ func (s *Server) WriteStatus(w io.Writer) {
if b.Len() > 0 {
tw := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)
fmt.Fprintf(tw, " node id\taddr\tlast query\tlast response\trecv\tdiscard\tflags\n")
b.EachNode(func(n *node) bool {
// Bucket nodes ordered by distance from server ID.
nodes := slices.SortedFunc(b.NodeIter(), func(l *node, r *node) int {
return l.Id.Distance(s.id).Cmp(r.Id.Distance(s.id))
})
for _, n := range nodes {
var flags []string
if s.IsQuestionable(n) {
flags = append(flags, "q10e")
Expand All @@ -122,8 +127,7 @@ func (s *Server) WriteStatus(w io.Writer) {
n.failedLastQuestionablePing,
strings.Join(flags, ","),
)
return true
})
}
tw.Flush()
}
}
Expand Down

0 comments on commit 888ec80

Please sign in to comment.