p2p: anti-entropy, a periodic sync with one random peer
Repairs what gossip lost while two nodes stay connected; cheap because matching digests end the round at once. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
+4
-2
@@ -142,7 +142,9 @@ within 30 days.
|
|||||||
existed) is caught up the original way: kinds `revocations`, `listings` and
|
existed) is caught up the original way: kinds `revocations`, `listings` and
|
||||||
`messages`, paged by `(created_at, author, id)` and `(created_at, hash)`
|
`messages`, paged by `(created_at, author, id)` and `(created_at, hash)`
|
||||||
cursors, which servers still answer. Gossip lost *while connected* is only
|
cursors, which servers still answer. Gossip lost *while connected* is only
|
||||||
repaired by the next reconnect; periodic anti-entropy is future work.
|
repaired by the next reconnect, and by anti-entropy: every 30 minutes a node
|
||||||
|
runs this sync again with one random connected peer that shares a region (a
|
||||||
|
matching digest makes a round nearly free).
|
||||||
- **Reconnection**: bootstrap peers are redialed every 20 s when disconnected.
|
- **Reconnection**: bootstrap peers are redialed every 20 s when disconnected.
|
||||||
- **Re-announce**: a running node re-gossips its own live listings and
|
- **Re-announce**: a running node re-gossips its own live listings and
|
||||||
revocations every 30 minutes, so newcomers hear about them without waiting
|
revocations every 30 minutes, so newcomers hear about them without waiting
|
||||||
@@ -493,7 +495,7 @@ Proof-of-work per listing and per revocation, plus subscribable blocklists
|
|||||||
|
|
||||||
Abuse reporting for messages, reciprocity rules, anti-
|
Abuse reporting for messages, reciprocity rules, anti-
|
||||||
discrimination (AGG) content rules, blocklists, peer scoring and rate limits,
|
discrimination (AGG) content rules, blocklists, peer scoring and rate limits,
|
||||||
periodic anti-entropy while connected, a fallback bootstrap list inside the binary
|
a fallback bootstrap list inside the binary
|
||||||
for networks that block DNS, image hosting,
|
for networks that block DNS, image hosting,
|
||||||
recovering listings onto a fresh machine with an empty database, a helper to
|
recovering listings onto a fresh machine with an empty database, a helper to
|
||||||
turn a place name into a region.
|
turn a place name into a region.
|
||||||
|
|||||||
+50
-5
@@ -17,6 +17,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"math/rand/v2"
|
||||||
"slices"
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -89,10 +90,14 @@ type Config struct {
|
|||||||
// MessageDifficulty is the proof-of-work bits required of message
|
// MessageDifficulty is the proof-of-work bits required of message
|
||||||
// envelopes; it must match the store's.
|
// envelopes; it must match the store's.
|
||||||
MessageDifficulty int
|
MessageDifficulty int
|
||||||
SyncPageSize int // records per sync page (default 500)
|
SyncPageSize int // records per sync page (default 500)
|
||||||
RedialInterval time.Duration // how often lost bootstrap peers are redialed (default 20s)
|
RedialInterval time.Duration // how often lost bootstrap peers are redialed (default 20s)
|
||||||
Clock func() time.Time // default time.Now
|
// AntiEntropyInterval is how often the node syncs with one random peer
|
||||||
Logger *slog.Logger
|
// it shares a region with, to repair what gossip lost while connected
|
||||||
|
// (default 30 min).
|
||||||
|
AntiEntropyInterval time.Duration
|
||||||
|
Clock func() time.Time // default time.Now
|
||||||
|
Logger *slog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Node is a running participant.
|
// Node is a running participant.
|
||||||
@@ -140,6 +145,9 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
|
|||||||
if cfg.RedialInterval <= 0 {
|
if cfg.RedialInterval <= 0 {
|
||||||
cfg.RedialInterval = 20 * time.Second
|
cfg.RedialInterval = 20 * time.Second
|
||||||
}
|
}
|
||||||
|
if cfg.AntiEntropyInterval <= 0 {
|
||||||
|
cfg.AntiEntropyInterval = 30 * time.Minute
|
||||||
|
}
|
||||||
if cfg.RelayBootDelay <= 0 {
|
if cfg.RelayBootDelay <= 0 {
|
||||||
cfg.RelayBootDelay = 10 * time.Second
|
cfg.RelayBootDelay = 10 * time.Second
|
||||||
}
|
}
|
||||||
@@ -256,7 +264,7 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
|
|||||||
|
|
||||||
// Finding the first peers can take a while (DNS, unreachable servers), so it
|
// Finding the first peers can take a while (DNS, unreachable servers), so it
|
||||||
// does not hold up starting the node.
|
// does not hold up starting the node.
|
||||||
n.wg.Add(4)
|
n.wg.Add(5)
|
||||||
go func() {
|
go func() {
|
||||||
defer n.wg.Done()
|
defer n.wg.Done()
|
||||||
n.dialBootstrap()
|
n.dialBootstrap()
|
||||||
@@ -264,6 +272,7 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
|
|||||||
}()
|
}()
|
||||||
go n.maintain()
|
go n.maintain()
|
||||||
go n.redial()
|
go n.redial()
|
||||||
|
go n.antiEntropy()
|
||||||
go n.remember()
|
go n.remember()
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
@@ -760,6 +769,42 @@ func (n *Node) redial() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// antiEntropy repairs what gossip lost while the node stayed connected, which
|
||||||
|
// the sync on a new connection cannot: every interval it syncs with one
|
||||||
|
// random peer that shares a region. Matching digests make a round nearly free.
|
||||||
|
func (n *Node) antiEntropy() {
|
||||||
|
defer n.wg.Done()
|
||||||
|
t := time.NewTicker(n.cfg.AntiEntropyInterval)
|
||||||
|
defer t.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-n.ctx.Done():
|
||||||
|
return
|
||||||
|
case <-t.C:
|
||||||
|
n.antiEntropyOnce()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node) antiEntropyOnce() {
|
||||||
|
n.mu.Lock()
|
||||||
|
regions := append([]string(nil), n.subscribed...)
|
||||||
|
n.mu.Unlock()
|
||||||
|
seen := map[peer.ID]bool{}
|
||||||
|
var peers []peer.ID
|
||||||
|
for _, r := range regions {
|
||||||
|
for _, p := range n.RegionPeers(r) {
|
||||||
|
if !seen[p] && n.host.Network().Connectedness(p) == network.Connected {
|
||||||
|
seen[p] = true
|
||||||
|
peers = append(peers, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(peers) > 0 {
|
||||||
|
n.syncSoon(peers[rand.IntN(len(peers))])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// maintain periodically drops expired data.
|
// maintain periodically drops expired data.
|
||||||
func (n *Node) maintain() {
|
func (n *Node) maintain() {
|
||||||
defer n.wg.Done()
|
defer n.wg.Done()
|
||||||
|
|||||||
@@ -492,3 +492,20 @@ func TestSyncWithANodeThatKnowsNoDigests(t *testing.T) {
|
|||||||
connect(t, b, a)
|
connect(t, b, a)
|
||||||
eventually(t, "b caught up from an old node", func() bool { return count(b) == 4 })
|
eventually(t, "b caught up from an old node", func() bool { return count(b) == 4 })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A listing that gossip never delivered is repaired while the link stays up.
|
||||||
|
func TestAntiEntropyRepairsWhatGossipMissed(t *testing.T) {
|
||||||
|
a := startNode(t, nil, "u1x")
|
||||||
|
b := startNodeWith(t, nil, func(c *Config) { c.AntiEntropyInterval = 200 * time.Millisecond }, "u1x")
|
||||||
|
connect(t, b, a)
|
||||||
|
eventually(t, "connected and synced", func() bool { return len(b.RegionPeers("u1x")) == 1 })
|
||||||
|
|
||||||
|
l := a.listing(t, "u1xjhx")
|
||||||
|
if _, err := a.s.Put(context.Background(), l); err != nil { // stored, not published: gossip never sees it
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
eventually(t, "b repairs the gap without reconnecting", func() bool { return count(b) == 1 })
|
||||||
|
if b.PeerCount() != 1 {
|
||||||
|
t.Fatal("the link should have stayed up")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user