From d44bae2eea660e24313482b026f690961ae6eac1 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 13:46:33 -0800 Subject: [PATCH] authority, master: duplicate slot keys in one report must be refused (RED) Review of #134 r2: store controls (conflicting and identical duplicate volume/replica keys refused with no transition and no mutation; distinct slots still yield one transition each) and an RPC control (a duplicate-slot heartbeat is refused; status and timeline unchanged). Refs #128 Co-Authored-By: Claude Opus 5.5 --- .../observation_store_duplicate_slot_test.go | 73 +++++++++++++++++++ .../master/recovery_watchdog_master_test.go | 35 +++++++++ 2 files changed, 108 insertions(+) create mode 100644 seaweed-block/core/authority/observation_store_duplicate_slot_test.go diff --git a/seaweed-block/core/authority/observation_store_duplicate_slot_test.go b/seaweed-block/core/authority/observation_store_duplicate_slot_test.go new file mode 100644 index 0000000000..5fb4578206 --- /dev/null +++ b/seaweed-block/core/authority/observation_store_duplicate_slot_test.go @@ -0,0 +1,73 @@ +package authority + +import ( + "errors" + "testing" + "time" +) + +// Review of #134 r2: an observation carrying the same volume/replica +// slot twice is refused before any mutation, so a duplicate can never +// fabricate a health transition that does not describe the stored slot. + +func dupStore(t *testing.T) (*ObservationStore, time.Time) { + t.Helper() + base := time.Unix(1000, 0) + return NewObservationStore(FreshnessConfig{FreshnessWindow: time.Hour}, func() time.Time { return base.Add(time.Minute) }), base +} + +func slotWith(reason string) SlotFact { + return SlotFact{VolumeID: "v1", ReplicaID: "r1", DataAddr: "d", CtrlAddr: "c", Reachable: true, Eligible: reason == "", UnhealthyReason: reason} +} + +func TestObservationStore_DuplicateSlotKeysRefused(t *testing.T) { + for _, tc := range []struct { + name string + slots []SlotFact + }{ + {"conflicting", []SlotFact{slotWith(""), slotWith("recovery_timeout")}}, + {"identical", []SlotFact{slotWith("recovery_timeout"), slotWith("recovery_timeout")}}, + } { + t.Run(tc.name, func(t *testing.T) { + s, base := dupStore(t) + if _, err := s.IngestWithHealthTransitions(Observation{ServerID: "s1", ObservedAt: base, Slots: []SlotFact{slotWith("recovery_timeout")}}); err != nil { + t.Fatal(err) + } + rev := s.Snapshot().revision + tr, err := s.IngestWithHealthTransitions(Observation{ServerID: "s1", ObservedAt: base.Add(time.Second), Slots: tc.slots}) + if !errors.Is(err, ErrInvalidObservation) { + t.Fatalf("duplicate slot keys: err=%v; want ErrInvalidObservation", err) + } + if len(tr) != 0 { + t.Fatalf("refused observation produced transitions %+v", tr) + } + if got := s.Snapshot().revision; got != rev { + t.Fatalf("refused observation mutated the store: revision %d -> %d", rev, got) + } + if f, ok := s.SlotFact("v1", "r1"); !ok || f.UnhealthyReason != "recovery_timeout" { + t.Fatalf("stored slot after refusal: %+v %t; want the earlier recovery_timeout fact", f, ok) + } + }) + } +} + +// Positive: distinct slots in one observation are accepted, and each +// changed slot yields its own transition. +func TestObservationStore_UniqueMultiSlotTransitions(t *testing.T) { + s, base := dupStore(t) + a := SlotFact{VolumeID: "v1", ReplicaID: "r1", DataAddr: "d", CtrlAddr: "c", Reachable: true, Eligible: true} + b := SlotFact{VolumeID: "v2", ReplicaID: "r1", DataAddr: "d2", CtrlAddr: "c2", Reachable: true, Eligible: true} + if _, err := s.IngestWithHealthTransitions(Observation{ServerID: "s1", ObservedAt: base, Slots: []SlotFact{a, b}}); err != nil { + t.Fatal(err) + } + a2, b2 := a, b + a2.Eligible, a2.UnhealthyReason = false, "recovery_timeout" + b2.Eligible, b2.UnhealthyReason = false, "role_admission_failed" + tr, err := s.IngestWithHealthTransitions(Observation{ServerID: "s1", ObservedAt: base.Add(time.Second), Slots: []SlotFact{a2, b2}}) + if err != nil { + t.Fatal(err) + } + if len(tr) != 2 || tr[0].VolumeID != "v1" || tr[0].New != "recovery_timeout" || tr[1].VolumeID != "v2" || tr[1].New != "role_admission_failed" { + t.Fatalf("transitions %+v; want v1 -> recovery_timeout and v2 -> role_admission_failed", tr) + } +} diff --git a/seaweed-block/core/host/master/recovery_watchdog_master_test.go b/seaweed-block/core/host/master/recovery_watchdog_master_test.go index f22265d797..0d80867171 100644 --- a/seaweed-block/core/host/master/recovery_watchdog_master_test.go +++ b/seaweed-block/core/host/master/recovery_watchdog_master_test.go @@ -256,3 +256,38 @@ func TestRecoveryWatchdog_MasterRejectedHeartbeatRecordsNothing(t *testing.T) { t.Fatalf("r1 health events=%v; want only [replica_unhealthy]", got) } } + +// Review of #134 r2: a heartbeat carrying the same slot twice is +// refused at the RPC; volume status and the timeline stay unchanged. +func TestRecoveryWatchdog_MasterDuplicateSlotHeartbeatRefused(t *testing.T) { + h, svc := watchdogMaster(t) + defer closeTestMaster(t, h) + t0 := time.Now() + reportR1(t, svc, wireSlot{"r1-b", false, "recovery_timeout"}, t0) + before := r1HealthEvents(t, svc) + + slot := func(reason string) *control.HeartbeatSlot { + return &control.HeartbeatSlot{ + VolumeId: "vol-rf3", ReplicaId: "r1", DataAddr: "127.0.0.1:19101", CtrlAddr: "127.0.0.1:19102", + Reachable: true, Eligible: reason == "", Incarnation: "r1-b", UnhealthyReason: reason, + } + } + for i, slots := range [][]*control.HeartbeatSlot{ + {slot(""), slot("recovery_timeout")}, // conflicting + {slot("recovery_timeout"), slot("recovery_timeout")}, // identical + } { + _, err := svc.ReportHeartbeat(context.Background(), &control.HeartbeatReport{ + ServerId: "node-a", SentAt: timestamppb.New(t0.Add(time.Duration(i+1) * time.Millisecond)), + Reachable: true, Eligible: true, Slots: slots, + }) + if err == nil { + t.Fatalf("duplicate-slot heartbeat %d accepted", i) + } + } + if got := r1HealthEvents(t, svc); len(got) != len(before) { + t.Fatalf("health events changed by refused heartbeats: %v -> %v", before, got) + } + if got := unhealthyReplicas(t, svc); got["r1"] != "recovery_timeout" { + t.Fatalf("volume status after refusal: %v; want r1=recovery_timeout", got) + } +}