Merge pull request #745 from seaweedfs/test/maintenance-probe-race

test(durable): the maintenance probe tests' fakes are safe across the loop's goroutine
This commit is contained in:
Ping Qiu authored and GitHub committed 2026-11-28 23:39:55 -08:00
commit c902fd4b7d
1 file changed
+55 -30
@@ -3,6 +3,9 @@ package durable
import (
"context"
"errors"
"maps"
"sync"
"sync/atomic"
"testing"
"testing/synctest"
"time"
@@ -11,27 +14,42 @@ import (
"github.com/seaweedfs/seaweed-block/core/storage"
)
// maintenanceProbeStore is read by the maintenance loop's goroutine and
// changed by the test's: mu guards every field, and ReadHead returns a copy.
type maintenanceProbeStore struct {
storage.ChunkRemote
mu sync.Mutex
head layout.Head
found bool
err error
reads int
}
// update changes the store under its lock.
func (s *maintenanceProbeStore) update(f func(s *maintenanceProbeStore)) {
s.mu.Lock()
defer s.mu.Unlock()
f(s)
}
func (s *maintenanceProbeStore) readCount() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.reads
}
func TestMaintenanceSnapshotProbeKeepsBusyRetryAndDirtyStartGate(t *testing.T) {
for _, dirty := range []bool{false, true} {
t.Run(map[bool]string{false: "busy", true: "dirty-start"}[dirty], func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
st := &maintenanceProbeStore{found: true, head: layout.Head{Snapshots: map[string]layout.Snapshot{"a": {}}}}
layer := &firstAttemptLayer{}
passes := 0
var passes atomic.Int32
p := &DurableProvider{cfg: ProviderConfig{
ChunkMaintenanceInterval: time.Hour,
ChunkLayoutHeadFor: func(string) (layout.StartStore, error) { return nil, nil },
ChunkMaintenance: func(context.Context, string) error {
passes++
if passes == 1 {
if passes.Add(1) == 1 {
return layout.ErrBusy
}
return nil
@@ -50,24 +68,24 @@ func TestMaintenanceSnapshotProbeKeepsBusyRetryAndDirtyStartGate(t *testing.T) {
}()
defer func() { cancel(); <-done }()
synctest.Wait()
delete(st.head.Snapshots, "a")
st.update(func(s *maintenanceProbeStore) { delete(s.head.Snapshots, "a") })
time.Sleep(30 * time.Second)
synctest.Wait()
if dirty {
if passes != 0 {
if passes.Load() != 0 {
t.Fatal("snapshot probe bypassed first store attempt")
}
layer.calls.Add(1)
time.Sleep(chunkSchedulePoll)
synctest.Wait()
}
if passes != 1 {
t.Fatalf("first pass=%d", passes)
if n := passes.Load(); n != 1 {
t.Fatalf("first pass=%d", n)
}
time.Sleep(30 * time.Second)
synctest.Wait()
if passes != 2 {
t.Fatalf("busy retry=%d; waited for ordinary interval", passes)
if n := passes.Load(); n != 2 {
t.Fatalf("busy retry=%d; waited for ordinary interval", n)
}
})
})
@@ -75,8 +93,12 @@ func TestMaintenanceSnapshotProbeKeepsBusyRetryAndDirtyStartGate(t *testing.T) {
}
func (s *maintenanceProbeStore) ReadHead(context.Context) (layout.Head, bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
s.reads++
return s.head, s.found, s.err
h := s.head
h.Snapshots = maps.Clone(s.head.Snapshots)
return h, s.found, s.err
}
func (s *maintenanceProbeStore) ProbeSnapshots(ctx context.Context) (map[string]struct{}, error) {
@@ -97,11 +119,11 @@ func (s *maintenanceProbeStore) ProbeSnapshots(ctx context.Context) (map[string]
func TestMaintenanceNoticesSnapshotDeleteWithoutNotification(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
st := &maintenanceProbeStore{found: true, head: layout.Head{Snapshots: map[string]layout.Snapshot{"a": {}, "b": {}}}}
passes := 0
var passes atomic.Int32
p := &DurableProvider{cfg: ProviderConfig{
ChunkMaintenanceInterval: time.Hour,
ChunkLayoutHeadFor: func(string) (layout.StartStore, error) { return nil, nil },
ChunkMaintenance: func(context.Context, string) error { passes++; return nil },
ChunkMaintenance: func(context.Context, string) error { passes.Add(1); return nil },
}}
p.volumes = map[string]*volHandle{"v1": {layoutRemote: st}}
ctx, cancel := context.WithCancel(context.Background())
@@ -112,45 +134,48 @@ func TestMaintenanceNoticesSnapshotDeleteWithoutNotification(t *testing.T) {
}()
defer func() { cancel(); <-done }()
synctest.Wait()
if st.reads != 1 || passes != 0 {
t.Fatalf("baseline: reads=%d passes=%d", st.reads, passes)
if st.readCount() != 1 || passes.Load() != 0 {
t.Fatalf("baseline: reads=%d passes=%d", st.readCount(), passes.Load())
}
delete(st.head.Snapshots, "a")
st.err = errors.New("filer unavailable")
st.update(func(s *maintenanceProbeStore) {
delete(s.head.Snapshots, "a")
s.err = errors.New("filer unavailable")
})
time.Sleep(30 * time.Second)
synctest.Wait()
if passes != 0 {
if passes.Load() != 0 {
t.Fatal("failed read inferred a deletion")
}
st.err = nil
st.found = false
st.update(func(s *maintenanceProbeStore) { s.err, s.found = nil, false })
time.Sleep(30 * time.Second)
synctest.Wait()
if passes != 0 {
if passes.Load() != 0 {
t.Fatal("absent HEAD inferred a deletion")
}
st.found = true
st.update(func(s *maintenanceProbeStore) { s.found = true })
time.Sleep(30 * time.Second)
synctest.Wait()
if passes != 1 || st.reads != 4 {
t.Fatalf("delete: reads=%d passes=%d", st.reads, passes)
if passes.Load() != 1 || st.readCount() != 4 {
t.Fatalf("delete: reads=%d passes=%d", st.readCount(), passes.Load())
}
time.Sleep(30 * time.Second)
synctest.Wait()
if passes != 1 || st.reads != 5 {
t.Fatalf("unchanged: reads=%d passes=%d", st.reads, passes)
if passes.Load() != 1 || st.readCount() != 5 {
t.Fatalf("unchanged: reads=%d passes=%d", st.readCount(), passes.Load())
}
st.head.Snapshots["c"] = layout.Snapshot{}
st.update(func(s *maintenanceProbeStore) { s.head.Snapshots["c"] = layout.Snapshot{} })
time.Sleep(30 * time.Second)
synctest.Wait()
if passes != 1 {
if passes.Load() != 1 {
t.Fatal("growing snapshot set triggered cleaning")
}
delete(st.head.Snapshots, "c")
st.head.Snapshots["d"] = layout.Snapshot{} // equal count still contains a deletion
st.update(func(s *maintenanceProbeStore) {
delete(s.head.Snapshots, "c")
s.head.Snapshots["d"] = layout.Snapshot{} // equal count still contains a deletion
})
time.Sleep(30 * time.Second)
synctest.Wait()
if passes != 2 {
if passes.Load() != 2 {
t.Fatal("new snapshot's deletion was not noticed")
}
})