-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdist_migration_hint_test.go
More file actions
319 lines (252 loc) · 10.4 KB
/
Copy pathdist_migration_hint_test.go
File metadata and controls
319 lines (252 loc) · 10.4 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package backend
import (
"context"
"log/slog"
"sync/atomic"
"testing"
"time"
"github.com/hyp3rd/ewrap"
"github.com/hyp3rd/hypercache/internal/cluster"
"github.com/hyp3rd/hypercache/internal/sentinel"
cache "github.com/hyp3rd/hypercache/pkg/cache/v2"
)
// Static sentinels for the scriptedTransport — err113 forbids defining
// dynamic errors with ewrap.New inside test bodies.
var (
errScriptedNotUsed = ewrap.New("scriptedTransport: method not exercised by this test")
errScriptedSimulate = ewrap.New("scriptedTransport: simulated transport error")
)
const migrationHintTestOrigin = "test-A"
// scriptedTransport is a DistTransport stub that returns whatever the
// test sets on its forwardSetErr field. Keeps the test focused on
// hint-source bookkeeping without involving the real HTTP or
// in-process transport paths.
type scriptedTransport struct {
forwardSetErr atomic.Value // error or nil
forwardSetCalls atomic.Int64
}
func (s *scriptedTransport) ForwardSet(_ context.Context, _ string, _ *cache.Item, _ bool) error {
s.forwardSetCalls.Add(1)
if v := s.forwardSetErr.Load(); v != nil {
if err, ok := v.(error); ok {
return err
}
}
return nil
}
func (*scriptedTransport) ForwardGet(_ context.Context, _, _ string) (*cache.Item, bool, error) {
return nil, false, nil
}
func (*scriptedTransport) ForwardRemove(_ context.Context, _, _ string, _ bool) error {
return nil
}
func (*scriptedTransport) Health(_ context.Context, _ string) error {
return nil
}
func (*scriptedTransport) IndirectHealth(_ context.Context, _, _ string) error {
return nil
}
func (*scriptedTransport) Gossip(_ context.Context, _ string, _ []GossipMember) error {
return nil
}
func (*scriptedTransport) FetchMerkle(_ context.Context, _ string) (*MerkleTree, error) {
return nil, errScriptedNotUsed
}
func (*scriptedTransport) ListKeys(_ context.Context, _, _ string) ([]string, error) {
return nil, errScriptedNotUsed
}
// newMigrationHintTestDM builds a DistMemory with just enough wiring to
// drive queueHint + processHint directly. We bypass NewDistMemory to
// avoid spinning a real cluster — the goal is to exercise the
// source-aware counters in isolation. Hint TTL is set to one minute
// (large enough that no test cares about expiry except the one that
// explicitly constructs an entry whose expire is in the past).
func newMigrationHintTestDM(t *testing.T) (*DistMemory, *scriptedTransport) {
t.Helper()
transport := &scriptedTransport{}
dm := &DistMemory{
logger: slog.New(slog.DiscardHandler),
hintTTL: time.Minute,
localNode: cluster.NewNode(migrationHintTestOrigin, "127.0.0.1:0"),
}
dm.storeTransport(transport)
return dm, transport
}
// TestMigrationHint_QueueTagsSource verifies that queueHint tags each
// entry with the source it was queued from and bumps the matching
// per-source counter alongside the aggregate. Without this contract,
// every downstream replay-time accounting decision (clean / dirty /
// migration-only metrics) would fall apart.
func TestMigrationHint_QueueTagsSource(t *testing.T) {
t.Parallel()
dm, _ := newMigrationHintTestDM(t)
item := &cache.Item{Key: "k1", Value: []byte("v"), Version: 1, Origin: migrationHintTestOrigin}
dm.queueHint("peer-B", item, hintSourceMigration)
dm.queueHint("peer-B", item, hintSourceReplication)
if got := dm.metrics.hintedQueued.Load(); got != 2 {
t.Errorf("aggregate hintedQueued: want 2, got %d", got)
}
if got := dm.metrics.migrationHintQueued.Load(); got != 1 {
t.Errorf("migrationHintQueued: want 1, got %d", got)
}
dm.hintsMu.Lock()
queue := dm.hints["peer-B"]
dm.hintsMu.Unlock()
if len(queue) != 2 {
t.Fatalf("queue length: want 2, got %d", len(queue))
}
if queue[0].source != hintSourceMigration {
t.Errorf("queue[0].source: want hintSourceMigration, got %d", queue[0].source)
}
if queue[1].source != hintSourceReplication {
t.Errorf("queue[1].source: want hintSourceReplication, got %d", queue[1].source)
}
}
// TestMigrationHint_ReplaySuccessUpdatesPerSourceCounters drives
// processHint manually through its happy path and confirms migration
// hints get tallied separately, replication hints do not bump the
// migration counter, and last_age_ns moves to a non-zero value.
func TestMigrationHint_ReplaySuccessUpdatesPerSourceCounters(t *testing.T) {
t.Parallel()
dm, _ := newMigrationHintTestDM(t)
queuedAt := time.Now().Add(-3 * time.Second) // 3s residency
item := &cache.Item{Key: "k1", Value: []byte("v"), Version: 1, Origin: migrationHintTestOrigin}
migEntry := hintedEntry{
item: item,
queuedAt: queuedAt,
expire: queuedAt.Add(time.Minute),
source: hintSourceMigration,
}
repEntry := hintedEntry{
item: item,
queuedAt: queuedAt,
expire: queuedAt.Add(time.Minute),
source: hintSourceReplication,
}
now := time.Now()
if action := dm.processHint(context.Background(), "peer-B", migEntry, now); action != 1 {
t.Errorf("migration replay: want action=1 (remove), got %d", action)
}
if action := dm.processHint(context.Background(), "peer-B", repEntry, now); action != 1 {
t.Errorf("replication replay: want action=1 (remove), got %d", action)
}
if got := dm.metrics.hintedReplayed.Load(); got != 2 {
t.Errorf("aggregate hintedReplayed: want 2, got %d", got)
}
if got := dm.metrics.migrationHintReplayed.Load(); got != 1 {
t.Errorf("migrationHintReplayed: want 1 (migration only), got %d", got)
}
ageNs := dm.metrics.migrationHintLastAgeNanos.Load()
if ageNs <= 0 {
t.Errorf("migrationHintLastAgeNanos: want > 0 after migration replay, got %d", ageNs)
}
// The recorded age should be roughly 3 seconds (the queuedAt offset).
// Allow generous slack to absorb test-machine scheduling jitter.
if ageNs < int64(2*time.Second) || ageNs > int64(10*time.Second) {
t.Errorf("migrationHintLastAgeNanos: want ~3s residency, got %v", time.Duration(ageNs))
}
}
// TestMigrationHint_ExpiredBumpsPerSourceCounter exercises the expired
// branch of processHint with a hint whose expire is in the past, and
// verifies the migration-source expired counter ticks.
func TestMigrationHint_ExpiredBumpsPerSourceCounter(t *testing.T) {
t.Parallel()
dm, _ := newMigrationHintTestDM(t)
now := time.Now()
item := &cache.Item{Key: "k1", Value: []byte("v"), Version: 1, Origin: migrationHintTestOrigin}
migEntry := hintedEntry{
item: item,
queuedAt: now.Add(-2 * time.Hour),
expire: now.Add(-time.Hour),
source: hintSourceMigration,
}
if action := dm.processHint(context.Background(), "peer-B", migEntry, now); action != 1 {
t.Errorf("expired migration: want action=1 (remove), got %d", action)
}
if got := dm.metrics.hintedExpired.Load(); got != 1 {
t.Errorf("aggregate hintedExpired: want 1, got %d", got)
}
if got := dm.metrics.migrationHintExpired.Load(); got != 1 {
t.Errorf("migrationHintExpired: want 1, got %d", got)
}
}
// TestMigrationHint_TransportErrorKeepsEntry pins the new
// retain-on-any-error contract: when a hint replay fails with any
// non-nil transport error (e.g. net.OpError, io.EOF, a scripted
// generic error like here), the hint stays queued for the next
// replay tick. Pre-fix this branch dropped the hint and bumped
// hintedDropped / migrationHintDropped, but that only matched the
// in-process ErrBackendNotFound sentinel — production HTTP/gRPC
// transports surfaced a different error class and lost the hint on
// the first replay attempt. TTL bounds total retry time, so a
// permanently-broken target still drains.
func TestMigrationHint_TransportErrorKeepsEntry(t *testing.T) {
t.Parallel()
dm, transport := newMigrationHintTestDM(t)
transport.forwardSetErr.Store(errScriptedSimulate)
now := time.Now()
item := &cache.Item{Key: "k1", Value: []byte("v"), Version: 1, Origin: migrationHintTestOrigin}
migEntry := hintedEntry{
item: item,
queuedAt: now.Add(-time.Second),
expire: now.Add(time.Minute),
source: hintSourceMigration,
}
if action := dm.processHint(context.Background(), "peer-B", migEntry, now); action != 0 {
t.Errorf("transport error: want action=0 (keep for retry), got %d", action)
}
if got := dm.metrics.hintedDropped.Load(); got != 0 {
t.Errorf("aggregate hintedDropped on retainable error: want 0 (no longer bumped on replay failures), got %d", got)
}
if got := dm.metrics.migrationHintDropped.Load(); got != 0 {
t.Errorf("migrationHintDropped on retainable error: want 0, got %d", got)
}
}
// TestMigrationHint_NotFoundKeepsEntry confirms the not-found path
// (peer still absent — typically backend restarting) keeps the entry
// in the queue rather than dropping. Migration counters must NOT
// increment on this path; the hint will be retried on a later tick.
func TestMigrationHint_NotFoundKeepsEntry(t *testing.T) {
t.Parallel()
dm, transport := newMigrationHintTestDM(t)
transport.forwardSetErr.Store(sentinel.ErrBackendNotFound)
now := time.Now()
item := &cache.Item{Key: "k1", Value: []byte("v"), Version: 1, Origin: migrationHintTestOrigin}
migEntry := hintedEntry{
item: item,
queuedAt: now.Add(-time.Second),
expire: now.Add(time.Minute),
source: hintSourceMigration,
}
if action := dm.processHint(context.Background(), "peer-B", migEntry, now); action != 0 {
t.Errorf("not-found: want action=0 (keep), got %d", action)
}
if got := dm.metrics.migrationHintDropped.Load(); got != 0 {
t.Errorf("migrationHintDropped on keep: want 0, got %d", got)
}
if got := dm.metrics.migrationHintReplayed.Load(); got != 0 {
t.Errorf("migrationHintReplayed on keep: want 0, got %d", got)
}
}
// TestMigrationHint_GlobalCapDropTagsSource confirms that even the
// global-cap rejection path (queue refused because hintMaxTotal /
// hintMaxBytes was already at the cap) routes through the
// migration-aware drop counter.
func TestMigrationHint_GlobalCapDropTagsSource(t *testing.T) {
t.Parallel()
dm, _ := newMigrationHintTestDM(t)
// Force the cap so the next queue attempt trips the global drop branch.
dm.hintMaxTotal = 1
dm.hintTotal = 1
item := &cache.Item{Key: "k1", Value: []byte("v"), Version: 1, Origin: migrationHintTestOrigin}
dm.queueHint("peer-B", item, hintSourceMigration)
if got := dm.metrics.hintedGlobalDropped.Load(); got != 1 {
t.Errorf("aggregate hintedGlobalDropped: want 1, got %d", got)
}
if got := dm.metrics.migrationHintDropped.Load(); got != 1 {
t.Errorf("migrationHintDropped on global-cap drop: want 1, got %d", got)
}
if got := dm.metrics.hintedQueued.Load(); got != 0 {
t.Errorf("hintedQueued must not increment on drop: got %d", got)
}
}