diff --git a/seaweed-block/core/frontend/durable/provider_snapshot_restart_test.go b/seaweed-block/core/frontend/durable/provider_snapshot_restart_test.go new file mode 100644 index 0000000000..5c3cf6ff4d --- /dev/null +++ b/seaweed-block/core/frontend/durable/provider_snapshot_restart_test.go @@ -0,0 +1,76 @@ +package durable_test + +import ( + "context" + "fmt" + "testing" + "testing/synctest" + "time" + + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" + "github.com/seaweedfs/seaweed-block/core/frontend/durable" + "github.com/seaweedfs/seaweed-block/core/frontend/durable/testutil" + "github.com/seaweedfs/seaweed-block/core/hostlimit" + "github.com/seaweedfs/seaweed-block/core/storage" +) + +// Each boot uses the real provider and the same persisted HEAD/local root. +// Virtual time makes a day's worth of frequent restarts deterministic. +func TestScheduledSnapshotRestartsStillTakeTheDueSnapshot(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + const period = 24 * time.Hour + f := testutil.NewLayoutFixture(t, "v1", pcBlocks, pcChunkBlocks) + root := t.TempDir() + var calls int + boot := func(on bool) *durable.DurableProvider { + var p *durable.DurableProvider + cfg := durable.ProviderConfig{Impl: durable.ImplWALStoreChunk, StorageRoot: root, + BlockSize: 4096, NumBlocks: pcBlocks, ChunkBlocks: pcChunkBlocks} + if on { + cfg.ChunkSnapshotPeriod = period + cfg.ChunkScheduledSnapshot = func(ctx context.Context, id string, at time.Time, _ func(context.Context) (storage.ChunkHead, error)) error { + _, err := p.SnapshotVolume(ctx, id, fmt.Sprintf("sched-%d", at.UnixNano()), layout.SnapshotMeta{Class: layout.ClassScheduled, CreatedNs: at.UnixNano()}) + if err == nil { + calls++ + } + return err + } + } + f.Configure(&cfg) + var err error + p, err = durable.NewDurableProvider(cfg, v1View()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = p.Close() }) + openRecovered(t, p) + return p + } + p := boot(false) + seed := time.Now() + if _, err := p.SnapshotVolume(context.Background(), "v1", "previous", layout.SnapshotMeta{Class: layout.ClassScheduled, CreatedNs: seed.UnixNano()}); err != nil { + t.Fatal(err) + } + if err := p.Close(); err != nil { + t.Fatal(err) + } + step := hostlimit.Phase("v1:snapshot", period) / 2 + if step <= 0 { + t.Fatal("test needs a positive volume offset") + } + if step > period/6 { + step = period / 6 + } + for time.Now().Before(seed.Add(period + step)) { + p = boot(true) + time.Sleep(step) + synctest.Wait() + if err := p.Close(); err != nil { + t.Fatal(err) + } + } + if calls != 1 { + t.Fatalf("%d scheduled snapshots after a period of restarts every %s (less than the offset); want 1", calls, step) + } + }) +}