|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hyp3rd/hypercache/internal/cluster" |
| 9 | + "github.com/hyp3rd/hypercache/pkg/backend" |
| 10 | + cache "github.com/hyp3rd/hypercache/pkg/cache/v2" |
| 11 | +) |
| 12 | + |
| 13 | +// TestDistRemove_PromotesOnGenericForwardError pins the resilience |
| 14 | +// contract for the Remove path symmetric to the Set path: a |
| 15 | +// `ForwardRemove` that fails for any reason (not just the in-process |
| 16 | +// `ErrBackendNotFound` sentinel) must promote to a local replica |
| 17 | +// owner and apply the remove locally + fan out to peer replicas. |
| 18 | +// Pre-fix the error was blackholed via `_ = transport.ForwardRemove(...)`, |
| 19 | +// so Remove silently succeeded on a downed primary while leaving the |
| 20 | +// stale value on every owner. |
| 21 | +func TestDistRemove_PromotesOnGenericForwardError(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + |
| 24 | + chaos := backend.NewChaos() |
| 25 | + |
| 26 | + dc := SetupInProcessClusterRF( |
| 27 | + t, 3, 3, |
| 28 | + backend.WithDistChaos(chaos), |
| 29 | + backend.WithDistWriteConsistency(backend.ConsistencyOne), |
| 30 | + ) |
| 31 | + |
| 32 | + a := dc.Nodes[0] |
| 33 | + b := dc.Nodes[1] |
| 34 | + c := dc.Nodes[2] |
| 35 | + |
| 36 | + desired := []cluster.NodeID{b.LocalNodeID(), a.LocalNodeID(), c.LocalNodeID()} |
| 37 | + |
| 38 | + key, ok := FindOwnerKey(a, "remove-promote-", desired, 5000) |
| 39 | + if !ok { |
| 40 | + t.Fatalf("could not find key with owner ordering [B, A, C]") |
| 41 | + } |
| 42 | + |
| 43 | + // Seed: write the key while chaos is off so it lands on every |
| 44 | + // owner via the normal replication path. |
| 45 | + err := a.Set(context.Background(), &cache.Item{Key: key, Value: "v1"}) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("seed Set: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + if !a.LocalContains(key) { |
| 51 | + t.Fatalf("seed: A.LocalContains is false; replication failed") |
| 52 | + } |
| 53 | + |
| 54 | + // Now block every forward and call Remove from A. The Remove |
| 55 | + // must NOT silently succeed; it must promote and clear the |
| 56 | + // local copy. The promotion counter (shared with the Set path |
| 57 | + // fix) bumps once. |
| 58 | + chaos.SetDropRate(1.0) |
| 59 | + |
| 60 | + rerr := a.Remove(context.Background(), key) |
| 61 | + if rerr != nil { |
| 62 | + t.Fatalf("Remove: got %v, want nil (promotion should have succeeded)", rerr) |
| 63 | + } |
| 64 | + |
| 65 | + if a.LocalContains(key) { |
| 66 | + t.Errorf("LocalContains(%q) after promoted Remove: got true, want false (local applyRemove didn't fire)", |
| 67 | + key) |
| 68 | + } |
| 69 | + |
| 70 | + if got := a.Metrics().WriteForwardPromotion; got == 0 { |
| 71 | + t.Errorf("WriteForwardPromotion: got 0, want > 0 (Remove promotion didn't bump the counter)") |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// TestDistHintReplay_RetainsOnGenericReplayError pins the hint-queue |
| 76 | +// recovery contract: a replay attempt that fails with anything other |
| 77 | +// than the in-process `ErrBackendNotFound` sentinel — including the |
| 78 | +// network errors production transports surface — must keep the hint |
| 79 | +// queued for a later retry, not abandon it on the first failure. |
| 80 | +// Pre-fix the hint was dropped on `net.OpError` / `io.EOF` etc., |
| 81 | +// meaning a peer that was briefly unreachable during replay lost the |
| 82 | +// queued writes entirely. |
| 83 | +func TestDistHintReplay_RetainsOnGenericReplayError(t *testing.T) { |
| 84 | + t.Parallel() |
| 85 | + |
| 86 | + chaos := backend.NewChaos() |
| 87 | + |
| 88 | + dc := SetupInProcessClusterRF( |
| 89 | + t, 3, 3, |
| 90 | + backend.WithDistChaos(chaos), |
| 91 | + backend.WithDistWriteConsistency(backend.ConsistencyOne), |
| 92 | + backend.WithDistHintTTL(time.Minute), |
| 93 | + backend.WithDistHintReplayInterval(20*time.Millisecond), |
| 94 | + ) |
| 95 | + |
| 96 | + a := dc.Nodes[0] |
| 97 | + b := dc.Nodes[1] |
| 98 | + c := dc.Nodes[2] |
| 99 | + |
| 100 | + desired := []cluster.NodeID{b.LocalNodeID(), a.LocalNodeID(), c.LocalNodeID()} |
| 101 | + |
| 102 | + key, ok := FindOwnerKey(a, "hint-retain-", desired, 5000) |
| 103 | + if !ok { |
| 104 | + t.Fatalf("could not find key with owner ordering [B, A, C]") |
| 105 | + } |
| 106 | + |
| 107 | + // Drop every forward, then write. The Set promotes locally and |
| 108 | + // queues a hint for the dead primary B; the replay tick fires |
| 109 | + // shortly after, also fails (chaos still on), and pre-fix would |
| 110 | + // have dropped the hint. |
| 111 | + chaos.SetDropRate(1.0) |
| 112 | + |
| 113 | + err := a.Set(context.Background(), &cache.Item{Key: key, Value: "v1"}) |
| 114 | + if err != nil { |
| 115 | + t.Fatalf("Set: %v", err) |
| 116 | + } |
| 117 | + |
| 118 | + queued := a.Metrics().HintedQueued |
| 119 | + if queued == 0 { |
| 120 | + t.Fatalf("HintedQueued: got 0, want > 0 (promotion didn't queue a hint)") |
| 121 | + } |
| 122 | + |
| 123 | + // Let the replay loop tick a few times against the still-chaotic |
| 124 | + // transport. Pre-fix: the very first tick drops the hint and |
| 125 | + // HintedReplayed stays 0 forever even after chaos clears. |
| 126 | + time.Sleep(150 * time.Millisecond) |
| 127 | + |
| 128 | + // Now heal chaos. The retained hint replays on the next tick |
| 129 | + // and lands on B. |
| 130 | + chaos.SetDropRate(0) |
| 131 | + |
| 132 | + if !waitForLocalContains(b, key, 2*time.Second) { |
| 133 | + t.Errorf("primary did not receive the write after chaos cleared (hint was dropped on the failed replay attempts instead of being retained)") |
| 134 | + } |
| 135 | + |
| 136 | + if got := a.Metrics().HintedReplayed; got == 0 { |
| 137 | + t.Errorf("HintedReplayed: got 0, want > 0 (hint was abandoned before chaos cleared)") |
| 138 | + } |
| 139 | +} |
0 commit comments