Skip to content

Commit 888ec80

Browse files
committed
Make DHT bucket debug node ordering stable
1 parent 6707d8c commit 888ec80

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

bucket.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dht
22

33
import (
4+
"iter"
5+
"maps"
46
"time"
57

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

24+
func (b *bucket) NodeIter() iter.Seq[*node] {
25+
return maps.Keys(b.nodes)
26+
}
27+
28+
// Returns true if f returns true for all nodes. Iteration stops if f returns false.
2229
func (b *bucket) EachNode(f func(*node) bool) bool {
23-
for n := range b.nodes {
30+
for n := range b.NodeIter() {
2431
if !f(n) {
2532
return false
2633
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/anacrolix/dht/v2
22

3-
go 1.18
3+
go 1.23
44

55
require (
66
filippo.io/edwards25519 v1.0.0-rc.1

go.sum

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ github.com/RoaringBitmap/roaring v0.4.23/go.mod h1:D0gp8kJQgE1A4LQ5wFLggQEyvDi06
4444
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
4545
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
4646
github.com/alecthomas/assert/v2 v2.0.0-alpha3 h1:pcHeMvQ3OMstAWgaeaXIAL8uzB9xMm2zlxt+/4ml8lk=
47+
github.com/alecthomas/assert/v2 v2.0.0-alpha3/go.mod h1:+zD0lmDXTeQj7TgDgCt0ePWxb0hMC1G+PGTsTCv1B9o=
4748
github.com/alecthomas/atomic v0.1.0-alpha2 h1:dqwXmax66gXvHhsOS4pGPZKqYOlTkapELkLb3MNdlH8=
4849
github.com/alecthomas/atomic v0.1.0-alpha2/go.mod h1:zD6QGEyw49HIq19caJDc2NMXAy8rNi9ROrxtMXATfyI=
4950
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
51+
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
5052
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
5153
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
5254
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
@@ -57,8 +59,6 @@ github.com/anacrolix/args v0.5.1-0.20220509024600-c3b77d0b61ac h1:XWoepbk3zgOQ8j
5759
github.com/anacrolix/args v0.5.1-0.20220509024600-c3b77d0b61ac/go.mod h1:Fj/N2PehEwTBE5t/V/9xgTcxDkuYQ+5IBoFw/8gkldI=
5860
github.com/anacrolix/backtrace v0.0.0-20221205112523-22a61db8f82e h1:A0Ty9UeyBDIo29ZMnk0AvPqWDIa4HVvCaJqWNlCrMXA=
5961
github.com/anacrolix/backtrace v0.0.0-20221205112523-22a61db8f82e/go.mod h1:4YFqy+788tLJWtin2jNliYVJi+8aDejG9zcu/2/pONw=
60-
github.com/anacrolix/bargle/v2 v2.0.0-20240804050250-9655b61917bc h1:+ZEZHmyNen2jUY2gD1YSDfmISIDLo+jKrOV3fZF7ZZw=
61-
github.com/anacrolix/bargle/v2 v2.0.0-20240804050250-9655b61917bc/go.mod h1:rKvwnOHgcXKPJTINj5RmkifgpxgEGC9bkJiv5kM4ctM=
6262
github.com/anacrolix/bargle/v2 v2.0.0-20240909020204-5265698a6040 h1:gneDM+Y60h9YY5xyG1buJss/OWjBZa+eYFO/Go3e7ro=
6363
github.com/anacrolix/bargle/v2 v2.0.0-20240909020204-5265698a6040/go.mod h1:rKvwnOHgcXKPJTINj5RmkifgpxgEGC9bkJiv5kM4ctM=
6464
github.com/anacrolix/chansync v0.3.0 h1:lRu9tbeuw3wl+PhMu/r+JJCRu5ArFXIluOgdF0ao6/U=
@@ -253,6 +253,7 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.12.0/go.mod h1:ummNFgdgLhhX7aIiy35v
253253
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
254254
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
255255
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
256+
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
256257
github.com/honeycombio/honeycomb-opentelemetry-go v0.3.0 h1:3qotL5cFNAiuLk/YZsUGNmz9ywnXqGP9hGFQoNo5PdA=
257258
github.com/honeycombio/honeycomb-opentelemetry-go v0.3.0/go.mod h1:qzzIv/RAGWhyRgyRwwRaxmn5tZMkc/bbTX3zit4sBGI=
258259
github.com/honeycombio/opentelemetry-go-contrib/launcher v0.0.0-20221031150637-a3c60ed98d54 h1:CFyJMKF0jR2dv+3Cpj/GuRa5XBXKnJqiqmWMYifTzok=
@@ -423,6 +424,7 @@ go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
423424
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
424425
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
425426
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
427+
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
426428
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
427429
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
428430
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=

server.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"net"
1010
"runtime/pprof"
11+
"slices"
1112
"strings"
1213
"text/tabwriter"
1314
"time"
@@ -99,7 +100,11 @@ func (s *Server) WriteStatus(w io.Writer) {
99100
if b.Len() > 0 {
100101
tw := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)
101102
fmt.Fprintf(tw, " node id\taddr\tlast query\tlast response\trecv\tdiscard\tflags\n")
102-
b.EachNode(func(n *node) bool {
103+
// Bucket nodes ordered by distance from server ID.
104+
nodes := slices.SortedFunc(b.NodeIter(), func(l *node, r *node) int {
105+
return l.Id.Distance(s.id).Cmp(r.Id.Distance(s.id))
106+
})
107+
for _, n := range nodes {
103108
var flags []string
104109
if s.IsQuestionable(n) {
105110
flags = append(flags, "q10e")
@@ -122,8 +127,7 @@ func (s *Server) WriteStatus(w io.Writer) {
122127
n.failedLastQuestionablePing,
123128
strings.Join(flags, ","),
124129
)
125-
return true
126-
})
130+
}
127131
tw.Flush()
128132
}
129133
}

0 commit comments

Comments
 (0)