mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
chunkstore: independent restore, catalog part (Restore, OriginRestore, independence marker)
Restore is Fork recorded with Origin restore (same claim semantics: live until the target has a HEAD). Every reader of the fork record accepts OriginRestore; a replay onto a volume of the other origin is refused. MarkIndependent writes the write-once RESTORE-INDEPENDENT entry only when the resolved HEAD holds no ref to another volume (ForeignRefs: owner-qualified keys whose owner is not the volume). Tests: restore records origin/fork record and attaches; replay and origin mismatch; marker refused while foreign refs remain, write-once replay; source snapshot and source volume deleted mid-restore (attach, replay and finish stay normal); GC keeps every chunk the restored HEAD names with the snapshot and the source gone; a restore without a live HEAD never attaches. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2d345a3663
commit
a4de2484f9
4 files changed
+456
-11
No files matched your search
@@ -70,9 +70,12 @@ const (
|
||||
// SnapAbsent is DeleteSnapshot's answer for a snapshot that is not there.
|
||||
SnapAbsent = "absent"
|
||||
|
||||
// OriginCreate and OriginFork are a volume's origins.
|
||||
OriginCreate = "create"
|
||||
OriginFork = "fork"
|
||||
// OriginCreate, OriginFork and OriginRestore are a volume's origins. A
|
||||
// restore is a fork whose volume later becomes independent of its source
|
||||
// (own chunks, restoreIndependentName written); until then it is a fork.
|
||||
OriginCreate = "create"
|
||||
OriginFork = "fork"
|
||||
OriginRestore = "restore"
|
||||
|
||||
// DefaultOpDeadline bounds a snapshot or fork from reading its source to
|
||||
// its final write (spec §2.3).
|
||||
@@ -395,10 +398,10 @@ func decodeVolume(p string, e *filerpb.Entry) (VolumeInfo, error) {
|
||||
if v.Format != volumeFormat {
|
||||
return VolumeInfo{}, fmt.Errorf("chunkstore: VOLUME %s: format %d not supported", p, v.Format)
|
||||
}
|
||||
if v.Origin != OriginCreate && v.Origin != OriginFork {
|
||||
if v.Origin != OriginCreate && v.Origin != OriginFork && v.Origin != OriginRestore {
|
||||
return VolumeInfo{}, fmt.Errorf("chunkstore: VOLUME %s: origin %q", p, v.Origin)
|
||||
}
|
||||
if v.Origin == OriginFork && (v.Fork == nil || v.Fork.RequestID == "") {
|
||||
if isForked(v.Origin) && (v.Fork == nil || v.Fork.RequestID == "") {
|
||||
return VolumeInfo{}, fmt.Errorf("chunkstore: VOLUME %s: a fork without its fork record", p)
|
||||
}
|
||||
v.State = string(e.Extended[extVolState])
|
||||
@@ -527,8 +530,10 @@ func (c *Catalog) EnsureVolume(ctx context.Context, vol string, g Geometry) (Vol
|
||||
return VolumeInfo{}, err
|
||||
}
|
||||
}
|
||||
if v.Origin == OriginFork {
|
||||
if isForked(v.Origin) {
|
||||
// One read of HEAD decides both: absent and aborted never attach.
|
||||
// A restore reads only its own HEAD here, never its source: a source
|
||||
// deleted meanwhile is a normal state, not a mismatch.
|
||||
h, has, err := c.ReadHead(ctx, vol)
|
||||
switch {
|
||||
case err != nil:
|
||||
@@ -1169,6 +1174,24 @@ func (c *Catalog) ListVolumes(ctx context.Context) ([]string, error) {
|
||||
// unsettled keeps its own intent (that create may still land); no attempt
|
||||
// or replay deletes another attempt's intent.
|
||||
func (c *Catalog) Fork(ctx context.Context, req ForkRequest) (ForkResult, error) {
|
||||
return c.fork(ctx, req, OriginFork)
|
||||
}
|
||||
|
||||
// Restore starts an independent restore of req.Source's snapshot into
|
||||
// req.Target: the same steps as Fork (claim, qualified refs, HEAD), recorded
|
||||
// with Origin restore. The target serves at once (lazy fill); the restore
|
||||
// driver then hydrates every chunk, re-uploads the qualified ones under the
|
||||
// target's own keys in bounded batch cuts, and MarkIndependent records the
|
||||
// end of the lineage once HEAD holds no qualified ref (DESIGN-r2, O-REC-2).
|
||||
func (c *Catalog) Restore(ctx context.Context, req ForkRequest) (ForkResult, error) {
|
||||
return c.fork(ctx, req, OriginRestore)
|
||||
}
|
||||
|
||||
// isForked reports whether origin made the volume from another volume's
|
||||
// snapshot (a fork record exists and HEAD may hold qualified refs).
|
||||
func isForked(origin string) bool { return origin == OriginFork || origin == OriginRestore }
|
||||
|
||||
func (c *Catalog) fork(ctx context.Context, req ForkRequest, origin string) (ForkResult, error) {
|
||||
if _, err := c.volRoot(req.Source); err != nil {
|
||||
return ForkResult{}, err
|
||||
}
|
||||
@@ -1199,7 +1222,7 @@ func (c *Catalog) Fork(ctx context.Context, req ForkRequest) (ForkResult, error)
|
||||
return ForkResult{}, refuseDeleted(req.Target, "fork to a new volume id")
|
||||
}
|
||||
if ok {
|
||||
if err := c.checkForkRecord(existing, req); err != nil {
|
||||
if err := c.checkForkRecord(existing, req, origin); err != nil {
|
||||
return ForkResult{}, err
|
||||
}
|
||||
h, has, err := c.ReadHead(ctx, req.Target)
|
||||
@@ -1271,7 +1294,7 @@ func (c *Catalog) Fork(ctx context.Context, req ForkRequest) (ForkResult, error)
|
||||
req.Target, req.Source, req.Snapshot, existing.Fork.SourceGen, snap.HeadGen)
|
||||
}
|
||||
} else {
|
||||
vol := VolumeInfo{Format: volumeFormat, VolumeID: req.Target, Geometry: snap.geo, Origin: OriginFork, CreatedNs: c.now().UnixNano(),
|
||||
vol := VolumeInfo{Format: volumeFormat, VolumeID: req.Target, Geometry: snap.geo, Origin: origin, CreatedNs: c.now().UnixNano(),
|
||||
Parent: &Parent{VolumeID: req.Source, Snapshot: req.Snapshot, Gen: snap.HeadGen}, Fork: record}
|
||||
ve, err := volumeEntry(vol, req.RequestID)
|
||||
if err != nil {
|
||||
@@ -1285,7 +1308,7 @@ func (c *Catalog) Fork(ctx context.Context, req ForkRequest) (ForkResult, error)
|
||||
return ForkResult{}, catErr(CodeOutcomeUnknown, true, cerr, "fork %s: VOLUME create outcome unknown; retry with the same request id", req.Target)
|
||||
}
|
||||
v, err := decodeVolume(path.Join(dstRoot, volumeName), got)
|
||||
if err != nil || c.checkForkRecord(v, req) != nil || v.Fork.HeadSHA256 != digest {
|
||||
if err != nil || c.checkForkRecord(v, req, origin) != nil || v.Fork.HeadSHA256 != digest {
|
||||
return ForkResult{}, catErr(CodeTargetExists, false, nil, "volume %s exists (another request, or another image)", req.Target)
|
||||
}
|
||||
}
|
||||
@@ -1477,7 +1500,10 @@ func (c *Catalog) AbortFork(ctx context.Context, target string) (claimed bool, e
|
||||
}
|
||||
|
||||
// checkForkRecord refuses a target VOLUME that is not this fork request's.
|
||||
func (c *Catalog) checkForkRecord(v VolumeInfo, req ForkRequest) error {
|
||||
func (c *Catalog) checkForkRecord(v VolumeInfo, req ForkRequest, origin string) error {
|
||||
if v.Origin != origin {
|
||||
return catErr(CodeTargetExists, false, nil, "volume %s exists (origin %s, not %s)", req.Target, v.Origin, origin)
|
||||
}
|
||||
if v.Fork == nil || v.Fork.RequestID != req.RequestID {
|
||||
return catErr(CodeTargetExists, false, nil, "volume %s exists (another request)", req.Target)
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ func (g *Collector) SweepCopies(ctx context.Context) (settled int, err error) {
|
||||
if readErr != nil {
|
||||
return settled, readErr
|
||||
}
|
||||
if !ok || v.Origin != OriginFork || v.Fork == nil || v.Fork.RequestID != parts[1] {
|
||||
if !ok || !isForked(v.Origin) || v.Fork == nil || v.Fork.RequestID != parts[1] {
|
||||
return settled, errors.New("chunkstore: fork intent has no matching target VOLUME")
|
||||
}
|
||||
if _, err = g.cat.AbortFork(ctx, a.vol); err != nil {
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
package chunkstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb"
|
||||
"github.com/seaweedfs/seaweed-block/core/storage"
|
||||
)
|
||||
|
||||
// restoreIndependentName is the write-once entry that ends a restore's
|
||||
// lineage: written only when the restored volume's HEAD holds no qualified
|
||||
// ref, so from then on nothing of the volume reaches its source's chunks.
|
||||
const (
|
||||
restoreIndependentName = "RESTORE-INDEPENDENT"
|
||||
kindRestoreIndependent = "chunkrestoreindependent"
|
||||
)
|
||||
|
||||
// Restore error codes.
|
||||
const (
|
||||
// CodeNotRestore: the volume was not made by Restore.
|
||||
CodeNotRestore = "not_restore"
|
||||
// CodeRestoreIncomplete: HEAD still holds qualified refs (or no HEAD).
|
||||
CodeRestoreIncomplete = "restore_incomplete"
|
||||
)
|
||||
|
||||
// RestoreIndependence is the content of restoreIndependentName.
|
||||
type RestoreIndependence struct {
|
||||
Format int `json:"format"`
|
||||
VolumeID string `json:"volume_id"`
|
||||
HeadGen uint64 `json:"head_gen"`
|
||||
MarkedNs int64 `json:"marked_ns"`
|
||||
}
|
||||
|
||||
// ForeignRefs counts the refs of vol's HEAD h that name ANOTHER volume's
|
||||
// chunks or segments: a "@<owner>/" key whose owner is not vol. A resolved
|
||||
// HEAD writes every chunk key in owner-qualified form, vol's own included,
|
||||
// so a bare "@" prefix does not make a ref foreign; an unqualified key is
|
||||
// always vol's own.
|
||||
func ForeignRefs(h storage.ChunkHead, vol string) int {
|
||||
foreign := func(key string) bool {
|
||||
if !strings.HasPrefix(key, "@") {
|
||||
return false
|
||||
}
|
||||
owner, _, ok := strings.Cut(key[1:], "/")
|
||||
return !ok || owner != vol
|
||||
}
|
||||
n := 0
|
||||
for _, ref := range h.Chunks {
|
||||
if foreign(ref.Key) {
|
||||
n++
|
||||
}
|
||||
}
|
||||
for _, seg := range h.Segments {
|
||||
if foreign(seg.Key) {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// MarkIndependent records that restored volume vol no longer depends on its
|
||||
// source: refused unless vol was made by Restore and its current HEAD holds
|
||||
// no qualified ref. Write-once: an existing marker is a replay (ok).
|
||||
func (c *Catalog) MarkIndependent(ctx context.Context, vol string) (RestoreIndependence, error) {
|
||||
root, err := c.volRoot(vol)
|
||||
if err != nil {
|
||||
return RestoreIndependence{}, err
|
||||
}
|
||||
if m, ok, err := c.ReadIndependence(ctx, vol); err != nil || ok {
|
||||
return m, err
|
||||
}
|
||||
v, ok, err := c.ReadVolume(ctx, vol)
|
||||
if err != nil {
|
||||
return RestoreIndependence{}, err
|
||||
}
|
||||
if !ok || v.Origin != OriginRestore {
|
||||
return RestoreIndependence{}, catErr(CodeNotRestore, false, nil, "volume %s was not made by restore", vol)
|
||||
}
|
||||
h, has, err := c.ReadHead(ctx, vol)
|
||||
if err != nil {
|
||||
return RestoreIndependence{}, err
|
||||
}
|
||||
if !has || h.State == storage.ChunkHeadAborted || headDeletingOrDeleted(h) {
|
||||
return RestoreIndependence{}, catErr(CodeRestoreIncomplete, false, nil, "volume %s has no live HEAD", vol)
|
||||
}
|
||||
// The manifest segments name chunk refs too: check the resolved HEAD (every
|
||||
// segment key and every chunk ref inside), as GC and usage resolve it.
|
||||
if c.blobs == nil {
|
||||
return RestoreIndependence{}, errors.New("chunkstore: restore marker needs the segment reader")
|
||||
}
|
||||
r, err := NewChunkRemote(c.f, c.blobs, path.Join(c.base, vol))
|
||||
if err != nil {
|
||||
return RestoreIndependence{}, err
|
||||
}
|
||||
resolved, err := r.resolveManifest(ctx, h)
|
||||
if err != nil {
|
||||
return RestoreIndependence{}, remoteErr(err, "resolve HEAD of %s", vol)
|
||||
}
|
||||
if q := ForeignRefs(resolved, vol); q > 0 {
|
||||
return RestoreIndependence{}, catErr(CodeRestoreIncomplete, false, nil, "volume %s: HEAD gen %d still holds %d refs to another volume", vol, h.Gen, q)
|
||||
}
|
||||
m := RestoreIndependence{Format: volumeFormat, VolumeID: vol, HeadGen: h.Gen, MarkedNs: c.now().UnixNano()}
|
||||
body, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return RestoreIndependence{}, err
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
e := &filerpb.Entry{
|
||||
Name: restoreIndependentName,
|
||||
Attributes: &filerpb.FuseAttributes{FileSize: uint64(len(body)), Mtime: now, Crtime: now, FileMode: 0o644, Mime: "application/json"},
|
||||
Extended: map[string][]byte{extKind: []byte(kindRestoreIndependent)},
|
||||
Content: body,
|
||||
}
|
||||
if err := c.f.CreateEntry(ctx, root, e, true); err != nil {
|
||||
if !errors.Is(err, ErrExists) {
|
||||
return RestoreIndependence{}, remoteErr(err, "create %s of %s", restoreIndependentName, vol)
|
||||
}
|
||||
m, ok, err := c.ReadIndependence(ctx, vol)
|
||||
if err == nil && !ok {
|
||||
err = remoteErr(nil, "re-read %s of %s", restoreIndependentName, vol)
|
||||
}
|
||||
return m, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// ReadIndependence returns vol's independence marker; ok is false when the
|
||||
// restore has not ended (or vol is not a restore).
|
||||
func (c *Catalog) ReadIndependence(ctx context.Context, vol string) (RestoreIndependence, bool, error) {
|
||||
root, err := c.volRoot(vol)
|
||||
if err != nil {
|
||||
return RestoreIndependence{}, false, err
|
||||
}
|
||||
e, err := c.f.LookupEntry(ctx, root, restoreIndependentName)
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return RestoreIndependence{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return RestoreIndependence{}, false, remoteErr(err, "read %s of %s", restoreIndependentName, vol)
|
||||
}
|
||||
if k := string(e.Extended[extKind]); k != kindRestoreIndependent {
|
||||
return RestoreIndependence{}, false, remoteErr(nil, "%s of %s: kind %q", restoreIndependentName, vol, k)
|
||||
}
|
||||
var m RestoreIndependence
|
||||
if err := json.Unmarshal(e.GetContent(), &m); err != nil || m.VolumeID != vol {
|
||||
return RestoreIndependence{}, false, remoteErr(err, "decode %s of %s", restoreIndependentName, vol)
|
||||
}
|
||||
return m, true, nil
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
package chunkstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb"
|
||||
"github.com/seaweedfs/seaweed-block/core/storage"
|
||||
)
|
||||
|
||||
// restoreFixture: v1 with snapshot s1, restored into v2.
|
||||
func restoreFixture(t *testing.T) (*fakeFiler, *fakeBlobs, *Catalog, storage.ChunkHead) {
|
||||
t.Helper()
|
||||
f, b, c, h := catalogFixture(t)
|
||||
ctx := context.Background()
|
||||
if _, err := c.CreateSnapshot(ctx, "v1", SnapshotRequest{Name: "s1", RequestID: "r"}, h); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := c.Restore(ctx, ForkRequest{Source: "v1", Snapshot: "s1", Target: "v2", RequestID: "rs1"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return f, b, c, h
|
||||
}
|
||||
|
||||
// ownAll re-uploads every qualified chunk of vol under vol's own keys and
|
||||
// publishes a HEAD with no qualified ref (what the restore's batch cuts do).
|
||||
func ownAll(t *testing.T, f *fakeFiler, b *fakeBlobs, vol string) storage.ChunkHead {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
r, err := NewChunkRemote(f, b, catBase+"/"+vol)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h, err := r.GetHead(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The batch cut's result in one step: every chunk under a fresh own key
|
||||
// (chunks/e2/...), a new manifest, one HEAD CAS, intents dropped.
|
||||
next := storage.ChunkHead{Epoch: 1, Holder: "h2", RemoteLSN: h.RemoteLSN, Chunks: map[uint64]storage.ChunkRef{}}
|
||||
for idx, ref := range h.Chunks {
|
||||
data, err := r.Get(ctx, ref.Key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key := fmt.Sprintf("chunks/e2/c%d/k", idx)
|
||||
if err := r.Put(ctx, key, data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
next.Chunks[idx] = storage.ChunkRef{Key: key, SHA256: sha256.Sum256(data)}
|
||||
}
|
||||
next, intents, err := r.PrepareManifest(ctx, h, next, uint64(catGeo.BlockSize)*uint64(catGeo.ChunkBlocks))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.CASHead(ctx, storage.ChunkHeadVersion{Gen: h.Gen, Epoch: h.Epoch}, next); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, id := range intents {
|
||||
if err := r.DeleteIntent(ctx, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
got, err := r.GetHead(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return got
|
||||
}
|
||||
|
||||
// allQualified reports whether every chunk ref and segment key of h names another volume.
|
||||
func allQualified(h storage.ChunkHead) bool {
|
||||
for _, ref := range h.Chunks {
|
||||
if !strings.HasPrefix(ref.Key, "@") {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for _, seg := range h.Segments {
|
||||
if !strings.HasPrefix(seg.Key, "@") {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return len(h.Chunks) > 0
|
||||
}
|
||||
|
||||
func TestRestore_RecordsOriginForkRecordAndQualifiedHead(t *testing.T) {
|
||||
f, b, c, h := restoreFixture(t)
|
||||
ctx := context.Background()
|
||||
v, ok, err := c.ReadVolume(ctx, "v2")
|
||||
if err != nil || !ok || v.Origin != OriginRestore || v.Fork == nil || v.Fork.Source != "v1" || v.Parent == nil {
|
||||
t.Fatalf("restored VOLUME %+v ok=%v err=%v: want Origin restore with a fork record and Parent", v, ok, err)
|
||||
}
|
||||
r2, _ := NewChunkRemote(f, b, catBase+"/v2")
|
||||
h2, _ := r2.GetHead(ctx)
|
||||
if !allQualified(h2) || len(h2.Chunks) != len(h.Chunks) {
|
||||
t.Fatalf("restored HEAD %v: want all %d chunk refs (and segments) qualified", h2.Chunks, len(h.Chunks))
|
||||
}
|
||||
if _, err := c.EnsureVolume(ctx, "v2", v.Geometry); err != nil {
|
||||
t.Fatalf("a restored volume attaches like a fork: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestore_ReplayAndOriginMismatchRefused(t *testing.T) {
|
||||
_, _, c, _ := restoreFixture(t)
|
||||
ctx := context.Background()
|
||||
if r, err := c.Restore(ctx, ForkRequest{Source: "v1", Snapshot: "s1", Target: "v2", RequestID: "rs1"}); err != nil || !r.Replayed {
|
||||
t.Fatalf("restore replay: %+v %v", r, err)
|
||||
}
|
||||
if _, err := c.Fork(ctx, ForkRequest{Source: "v1", Snapshot: "s1", Target: "v2", RequestID: "rs1"}); CodeOf(err) != CodeTargetExists {
|
||||
t.Fatalf("a fork onto a restored volume with the same request id must be refused: %v", err)
|
||||
}
|
||||
if _, err := c.Fork(ctx, ForkRequest{Source: "v1", Snapshot: "s1", Target: "v3", RequestID: "f1"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := c.Restore(ctx, ForkRequest{Source: "v1", Snapshot: "s1", Target: "v3", RequestID: "f1"}); CodeOf(err) != CodeTargetExists {
|
||||
t.Fatalf("a restore onto a fork volume with the same request id must be refused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestore_MarkIndependentOnlyWithoutQualifiedRefs(t *testing.T) {
|
||||
f, b, c, _ := restoreFixture(t)
|
||||
ctx := context.Background()
|
||||
if _, err := c.MarkIndependent(ctx, "v2"); CodeOf(err) != CodeRestoreIncomplete {
|
||||
t.Fatalf("marker while HEAD holds qualified refs: %v, want %s", err, CodeRestoreIncomplete)
|
||||
}
|
||||
if _, ok, err := c.ReadIndependence(ctx, "v2"); err != nil || ok {
|
||||
t.Fatalf("no marker may exist after a refused mark: ok=%v err=%v", ok, err)
|
||||
}
|
||||
if _, err := c.Fork(ctx, ForkRequest{Source: "v1", Snapshot: "s1", Target: "v3", RequestID: "f1"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := c.MarkIndependent(ctx, "v3"); CodeOf(err) != CodeNotRestore {
|
||||
t.Fatalf("marker on a fork: %v, want %s", err, CodeNotRestore)
|
||||
}
|
||||
h := ownAll(t, f, b, "v2")
|
||||
if ForeignRefs(h, "v2") != 0 {
|
||||
t.Fatalf("fixture: %d foreign refs left", ForeignRefs(h, "v2"))
|
||||
}
|
||||
m, err := c.MarkIndependent(ctx, "v2")
|
||||
if err != nil || m.HeadGen != h.Gen || m.VolumeID != "v2" {
|
||||
t.Fatalf("marker: %+v %v (HEAD gen %d)", m, err, h.Gen)
|
||||
}
|
||||
if again, err := c.MarkIndependent(ctx, "v2"); err != nil || again != m {
|
||||
t.Fatalf("marker is write-once (a replay returns it): %+v %v", again, err)
|
||||
}
|
||||
if got, ok, err := c.ReadIndependence(ctx, "v2"); err != nil || !ok || got != m {
|
||||
t.Fatalf("read marker: %+v %v %v", got, ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
// claude06 (verify side): deleting the source mid-restore is a normal state for every reader of T's fork record.
|
||||
func TestRestore_SourceGoneIsNormal(t *testing.T) {
|
||||
f, b, c, _ := restoreFixture(t)
|
||||
ctx := context.Background()
|
||||
r2, _ := NewChunkRemote(f, b, catBase+"/v2")
|
||||
h2, _ := r2.GetHead(ctx)
|
||||
before := map[uint64][]byte{}
|
||||
for idx, ref := range h2.Chunks {
|
||||
data, err := r2.Get(ctx, ref.Key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before[idx] = data
|
||||
}
|
||||
if _, err := c.DeleteSnapshot(ctx, "v1", "s1"); err != nil {
|
||||
t.Fatalf("after T has its HEAD the source snapshot may go: %v", err)
|
||||
}
|
||||
detachHead(t, f, b, "v1")
|
||||
if _, err := c.DeleteVolume(ctx, "v1", nil); err != nil {
|
||||
t.Fatalf("delete the source volume: %v", err)
|
||||
}
|
||||
v, _, _ := c.ReadVolume(ctx, "v2")
|
||||
if _, err := c.EnsureVolume(ctx, "v2", v.Geometry); err != nil {
|
||||
t.Fatalf("attach of a restore whose source is gone must be normal: %v", err)
|
||||
}
|
||||
if r, err := c.Restore(ctx, ForkRequest{Source: "v1", Snapshot: "s1", Target: "v2", RequestID: "rs1"}); err != nil || !r.Replayed {
|
||||
t.Fatalf("replay of a restore whose source is gone: %+v %v", r, err)
|
||||
}
|
||||
for idx, ref := range h2.Chunks {
|
||||
data, err := r2.Get(ctx, ref.Key)
|
||||
if err != nil || sha256.Sum256(data) != sha256.Sum256(before[idx]) || sha256.Sum256(data) != ref.SHA256 {
|
||||
t.Fatalf("chunk %d unreadable or changed after the source was deleted: %v", idx, err)
|
||||
}
|
||||
}
|
||||
h := ownAll(t, f, b, "v2")
|
||||
if _, err := c.MarkIndependent(ctx, "v2"); err != nil {
|
||||
t.Fatalf("the restore finishes with its source gone: %v (HEAD gen %d)", err, h.Gen)
|
||||
}
|
||||
}
|
||||
|
||||
// claude05/claude06 load-bearing invariant for O-REC-2 option 1: after the restore's HEAD exists, the
|
||||
// source snapshot and the source volume may both go; GC must keep every chunk T's HEAD names (qualified
|
||||
// refs resolve to the owner's bindings) until T's own batch cuts replace them.
|
||||
func TestRestore_GCRootsQualifiedRefsWithSnapshotAndSourceGone(t *testing.T) {
|
||||
f, b, c, g, now := gcFixture(t)
|
||||
ctx := context.Background()
|
||||
h := publishSource(t, f, b.fakeBlobs, "v1", map[uint64][]byte{0: chunkBytes(0x31), 3: chunkBytes(0x33)})
|
||||
if _, e := c.CreateSnapshot(ctx, "v1", SnapshotRequest{Name: "s1", RequestID: "req"}, h); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e := f.CreateEntry(ctx, catBase, &filerpb.Entry{Name: "v2", IsDirectory: true}, true); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e := c.Restore(ctx, ForkRequest{Source: "v1", Snapshot: "s1", Target: "v2", RequestID: "rs"}); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e := c.DeleteSnapshot(ctx, "v1", "s1"); e != nil {
|
||||
t.Fatalf("retention/delete may retire S once T has a HEAD: %v", e)
|
||||
}
|
||||
detachHead(t, f, b.fakeBlobs, "v1")
|
||||
if _, e := c.DeleteVolume(ctx, "v1", nil); e != nil {
|
||||
t.Fatalf("delete the source volume: %v", e)
|
||||
}
|
||||
r2, _ := NewChunkRemote(f, b, path.Join(catBase, "v2"))
|
||||
h2, e := r2.GetHead(ctx)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
for i := 0; i < 3; i++ {
|
||||
if _, e := g.Run(ctx); e != nil {
|
||||
t.Fatalf("gc: %v", e)
|
||||
}
|
||||
*now = now.Add(2 * time.Hour)
|
||||
}
|
||||
want := map[uint64][]byte{0: chunkBytes(0x31), 3: chunkBytes(0x33)}
|
||||
for idx, ref := range h2.Chunks {
|
||||
got, e := r2.Get(ctx, ref.Key)
|
||||
if e != nil || !bytes.Equal(got, want[idx]) {
|
||||
t.Fatalf("restored chunk %d lost after S and V were deleted and GC ran: %v", idx, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// claude06 condition (3): a restore whose HEAD is absent or an aborted tombstone never attaches, like a fork.
|
||||
func TestRestore_WithoutLiveHeadNeverAttaches(t *testing.T) {
|
||||
f, _, c, _ := catalogFixture(t)
|
||||
ctx := context.Background()
|
||||
v := VolumeInfo{Format: volumeFormat, VolumeID: "v9", Geometry: catGeo, Origin: OriginRestore, CreatedNs: 1,
|
||||
Parent: &Parent{VolumeID: "v1", Snapshot: "s1", Gen: 1}, Fork: &ForkRecord{RequestID: "rq", Source: "v1", Snapshot: "s1"}}
|
||||
e, err := volumeEntry(v, "rq")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.CreateEntry(ctx, catBase+"/v9", e, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := c.EnsureVolume(ctx, "v9", catGeo); CodeOf(err) != CodeForkIncomplete {
|
||||
t.Fatalf("restore without a HEAD attached: %v, want %s", err, CodeForkIncomplete)
|
||||
}
|
||||
if _, err := c.AbortFork(ctx, "v9"); err != nil {
|
||||
t.Fatalf("abort the unpublished restore (sweep): %v", err)
|
||||
}
|
||||
if _, err := c.EnsureVolume(ctx, "v9", catGeo); CodeOf(err) != CodeForkIncomplete {
|
||||
t.Fatalf("restore with an aborted HEAD attached: %v, want %s", err, CodeForkIncomplete)
|
||||
}
|
||||
if _, err := c.MarkIndependent(ctx, "v9"); CodeOf(err) != CodeRestoreIncomplete {
|
||||
t.Fatalf("marker on an aborted restore: %v, want %s", err, CodeRestoreIncomplete)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user