~james/core

core/spindle/hydrant_pending_test.go -rw-r--r-- 7.3 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package spindle

import (
	"context"
	"errors"
	"log/slog"
	"slices"
	"testing"
	"time"

	"github.com/bluesky-social/indigo/atproto/syntax"

	"tangled.org/core/spindle/db"
)

const (
	pendingOwner = syntax.DID("did:plc:owner")
	pendingRepo  = syntax.DID("did:plc:repo")
)

func catchUpBackfill(t *testing.T, f *fakeHydrant) *backfill {
	t.Helper()
	b, _ := newTestBackfill(t, f)
	b.s.l = slog.Default()
	b.refresh = func(context.Context, syntax.DID) error { return nil }
	if err := b.s.db.AddRepo(db.Repo{Knot: "knot.test", Owner: pendingOwner, Rkey: "repo", RepoDid: pendingRepo}); err != nil {
		t.Fatal(err)
	}
	return b
}

func pendingDids(t *testing.T, b *backfill) []syntax.DID {
	t.Helper()
	dids, err := b.s.db.PendingCatchUp()
	if err != nil {
		t.Fatal(err)
	}
	return dids
}

// the pending list lives in the spindle db, so the restart has to reuse it
func restartedBackfill(t *testing.T, old *backfill, f *fakeHydrant) *backfill {
	t.Helper()
	b := newBackfill(old.s, slog.Default(), shortTempDir(t))
	b.start = f.start
	b.poll = old.poll
	b.retryBase = old.retryBase
	b.handle = old.handle
	b.refresh = old.refresh
	return b
}

func TestCatchUpSavesItsDidsBeforeAnyRoundRuns(t *testing.T) {
	b := catchUpBackfill(t, &fakeHydrant{})
	if err := b.CatchUp(); err != nil {
		t.Fatal(err)
	}
	if got, want := pendingDids(t, b), []syntax.DID{pendingOwner, pendingRepo}; !slices.Equal(got, want) {
		t.Fatalf("pending = %v, want %v", got, want)
	}
}

func failOn(f *fakeHydrant, failing syntax.DID) {
	f.onTrack = func(did syntax.DID) {
		if did == failing {
			f.setRepo(failedWith(did, "transport", 0))
		} else {
			f.setRepo(synced(did))
		}
	}
}

func TestPendingCatchUpKeepsADidHydrantCouldNotBackfill(t *testing.T) {
	f := &fakeHydrant{}
	failOn(f, pendingOwner)
	b := catchUpBackfill(t, f)
	if err := b.CatchUp(); err != nil {
		t.Fatal(err)
	}
	runRound(t, b)
	if got, want := pendingDids(t, b), []syntax.DID{pendingOwner}; !slices.Equal(got, want) {
		t.Fatalf("pending = %v, want only the failed did %v", got, want)
	}
}

// after a restart the pending list is the only thing that knows these dids are owed
func TestPendingCatchUpSurvivesARestartMidCatchUp(t *testing.T) {
	interrupted := &fakeHydrant{}
	b := catchUpBackfill(t, interrupted)
	if err := b.CatchUp(); err != nil {
		t.Fatal(err)
	}
	interrupted.failNext = errors.New("db is locked")
	if err := b.round(context.Background()); err == nil {
		t.Fatal("round succeeded although hydrant didn't start")
	}
	if len(pendingDids(t, b)) != 2 {
		t.Fatalf("pending = %v, want both dids still owed", pendingDids(t, b))
	}

	f := &fakeHydrant{}
	b2 := restartedBackfill(t, b, f)
	if err := b2.Resume(); err != nil {
		t.Fatal(err)
	}
	runRound(t, b2)
	if want := []syntax.DID{pendingOwner, pendingRepo}; !slices.Equal(slices.Sorted(slices.Values(f.erased)), want) {
		t.Fatalf("erased = %v, want the resumed catch-up to erase %v", f.erased, want)
	}
	if got := pendingDids(t, b2); len(got) != 0 {
		t.Fatalf("pending = %v after the resumed catch-up finished", got)
	}
}

func TestResumeWithNothingPendingStartsNoRound(t *testing.T) {
	f := &fakeHydrant{}
	b := catchUpBackfill(t, f)
	if err := b.Resume(); err != nil {
		t.Fatal(err)
	}
	runRound(t, b)
	if f.starts != 0 {
		t.Fatalf("hydrant started %d times with nothing to catch up", f.starts)
	}
}

func TestResumeDropsDidsThatNeverBackfilledInAWeek(t *testing.T) {
	b := catchUpBackfill(t, &fakeHydrant{})
	if err := b.s.db.AddPendingCatchUp([]syntax.DID{"did:plc:dead"}, time.Now().Add(-pendingCatchUpMaxAge-time.Hour)); err != nil {
		t.Fatal(err)
	}
	if err := b.s.db.AddPendingCatchUp([]syntax.DID{"did:plc:slow"}, time.Now().Add(-time.Hour)); err != nil {
		t.Fatal(err)
	}
	if err := b.Resume(); err != nil {
		t.Fatal(err)
	}
	if got, want := pendingDids(t, b), []syntax.DID{"did:plc:slow"}; !slices.Equal(got, want) {
		t.Fatalf("pending = %v, want %v", got, want)
	}
	add, _ := b.take()
	if !slices.Equal(add, []syntax.DID{"did:plc:slow"}) {
		t.Fatalf("queued %v, want only the did that is still pending", add)
	}
}

func TestUntrackForgetsAPendingCatchUp(t *testing.T) {
	b := catchUpBackfill(t, &fakeHydrant{})
	if err := b.CatchUp(); err != nil {
		t.Fatal(err)
	}
	b.Untrack(pendingOwner)
	if got, want := pendingDids(t, b), []syntax.DID{pendingRepo}; !slices.Equal(got, want) {
		t.Fatalf("pending = %v, want %v", got, want)
	}
}

// outcomes only count for dids in the round list, even a done one
func TestSettlePendingOnlyTouchesDidsTheRoundBackfilled(t *testing.T) {
	b := catchUpBackfill(t, &fakeHydrant{})
	if err := b.s.db.AddPendingCatchUp([]syntax.DID{"did:plc:a", "did:plc:b", "did:plc:c"}, time.Now()); err != nil {
		t.Fatal(err)
	}
	b.settlePending(
		context.Background(),
		[]syntax.DID{"did:plc:a", "did:plc:c"},
		map[syntax.DID]outcome{"did:plc:a": done, "did:plc:b": done, "did:plc:c": failed},
	)
	if got, want := pendingDids(t, b), []syntax.DID{"did:plc:b", "did:plc:c"}; !slices.Equal(got, want) {
		t.Fatalf("pending = %v, want %v", got, want)
	}
}

func recordRefreshes(b *backfill, fail error) *[]syntax.DID {
	var refreshed []syntax.DID
	b.refresh = func(_ context.Context, did syntax.DID) error {
		refreshed = append(refreshed, did)
		return fail
	}
	return &refreshed
}

// hydrant never sends deleted manifests, so only the knot can tell us a repo died in the gap
func TestCatchUpRefreshesServedReposButNotOwners(t *testing.T) {
	b := catchUpBackfill(t, &fakeHydrant{})
	refreshed := recordRefreshes(b, nil)
	if err := b.CatchUp(); err != nil {
		t.Fatal(err)
	}
	runRound(t, b)
	if !slices.Equal(*refreshed, []syntax.DID{pendingRepo}) {
		t.Fatalf("refreshed = %v, want only the served repo did", *refreshed)
	}
	if got := pendingDids(t, b); len(got) != 0 {
		t.Fatalf("pending = %v", got)
	}
}

func TestCatchUpRefreshFailureGoesToTheRetrySetAndClearsThePending(t *testing.T) {
	b := catchUpBackfill(t, &fakeHydrant{})
	recordRefreshes(b, errors.New("knot is down"))
	if err := b.CatchUp(); err != nil {
		t.Fatal(err)
	}
	runRound(t, b)
	if got := pendingRetries(t, b.s.db); !slices.Equal(got, []syntax.DID{pendingRepo}) {
		t.Fatalf("retry set = %v, want the repo whose refresh failed", got)
	}
	if got := pendingDids(t, b); len(got) != 0 {
		t.Fatalf("pending = %v, the retry set owns it now", got)
	}
}

func TestCancelledRefreshKeepsTheDidPending(t *testing.T) {
	b := catchUpBackfill(t, &fakeHydrant{})
	ctx, cancel := context.WithCancel(context.Background())
	b.refresh = func(context.Context, syntax.DID) error {
		cancel()
		return ctx.Err()
	}
	if err := b.s.db.AddPendingCatchUp([]syntax.DID{pendingRepo}, time.Now()); err != nil {
		t.Fatal(err)
	}
	b.settlePending(ctx, []syntax.DID{pendingRepo}, map[syntax.DID]outcome{pendingRepo: done})
	if got := pendingDids(t, b); !slices.Equal(got, []syntax.DID{pendingRepo}) {
		t.Fatalf("pending = %v, want the did kept for the next start", got)
	}
	if got := pendingRetries(t, b.s.db); len(got) != 0 {
		t.Fatalf("retry set = %v, a shutdown isn't a refresh failure", got)
	}
}

func TestCatchUpDoesNotRefreshARepoHydrantCouldNotBackfill(t *testing.T) {
	f := &fakeHydrant{}
	failOn(f, pendingRepo)
	b := catchUpBackfill(t, f)
	refreshed := recordRefreshes(b, nil)
	if err := b.CatchUp(); err != nil {
		t.Fatal(err)
	}
	runRound(t, b)
	if len(*refreshed) != 0 {
		t.Fatalf("refreshed %v although hydrant failed it, it has to stay pending for the next round", *refreshed)
	}
	if got := pendingDids(t, b); !slices.Equal(got, []syntax.DID{pendingRepo}) {
		t.Fatalf("pending = %v", got)
	}
}