diff --git a/seaweed-block/core/frontend/chunkstore/catalog.go b/seaweed-block/core/frontend/chunkstore/catalog.go index a3b93e5e09..c4ca9e9b8b 100644 --- a/seaweed-block/core/frontend/chunkstore/catalog.go +++ b/seaweed-block/core/frontend/chunkstore/catalog.go @@ -864,6 +864,16 @@ func (c *Catalog) CreateSnapshot(ctx context.Context, vol string, req SnapshotRe if !ok { return SnapshotInfo{}, catErr(CodeNotChunkVolume, false, nil, "volume %s has no VOLUME entry", vol) } + if v.Origin == OriginRestore { + // A restore is independent only once its marker exists: a snapshot + // taken before would keep naming the source's chunks for its whole + // retention, so it is refused until then (DESIGN-r2 open question a). + if _, independent, err := c.ReadIndependence(ctx, vol); err != nil { + return SnapshotInfo{}, err + } else if !independent { + return SnapshotInfo{}, catErr(CodeRestoreIncomplete, false, nil, "volume %s is still being restored: snapshots are refused until it is independent", vol) + } + } // Take the name: this request's record, or an earlier attempt's of it. attempt, err := c.claimName(ctx, vol, root, req, head.Gen) diff --git a/seaweed-block/core/frontend/chunkstore/restore_test.go b/seaweed-block/core/frontend/chunkstore/restore_test.go index f844827032..b30af27438 100644 --- a/seaweed-block/core/frontend/chunkstore/restore_test.go +++ b/seaweed-block/core/frontend/chunkstore/restore_test.go @@ -263,3 +263,31 @@ func TestRestore_WithoutLiveHeadNeverAttaches(t *testing.T) { t.Fatalf("marker on an aborted restore: %v, want %s", err, CodeRestoreIncomplete) } } + +// DESIGN-r2 open question (a), ruled: snapshots of a restore are refused until it is independent. +func TestRestore_SnapshotsRefusedUntilIndependent(t *testing.T) { + f, b, c, _ := restoreFixture(t) + ctx := context.Background() + r2, _ := NewChunkRemote(f, b, catBase+"/v2") + h2, _ := r2.GetHead(ctx) + if _, err := c.CreateSnapshot(ctx, "v2", SnapshotRequest{Name: "early", RequestID: "e"}, h2); CodeOf(err) != CodeRestoreIncomplete { + t.Fatalf("snapshot of a restore in progress: %v, want %s", err, CodeRestoreIncomplete) + } + if _, ok, err := c.SnapshotReplay(ctx, "v2", SnapshotRequest{Name: "early", RequestID: "e"}); err != nil || ok { + t.Fatalf("a refused snapshot must leave nothing: ok=%v err=%v", ok, err) + } + h := ownAll(t, f, b, "v2") + if _, err := c.CreateSnapshot(ctx, "v2", SnapshotRequest{Name: "late", RequestID: "l"}, h); CodeOf(err) != CodeRestoreIncomplete { + t.Fatalf("own chunks but no marker yet: %v, want %s", err, CodeRestoreIncomplete) + } + if _, err := c.MarkIndependent(ctx, "v2"); err != nil { + t.Fatal(err) + } + s, err := c.CreateSnapshot(ctx, "v2", SnapshotRequest{Name: "late", RequestID: "l"}, h) + if err != nil { + t.Fatalf("snapshot after the marker: %v", err) + } + if ForeignRefs(s.Head(), "v2") != 0 { + t.Fatalf("a snapshot of an independent restore names %d foreign refs", ForeignRefs(s.Head(), "v2")) + } +}