diff --git a/seaweed-block/core/frontend/chunkstore/delete_sites_test.go b/seaweed-block/core/frontend/chunkstore/delete_sites_test.go new file mode 100644 index 0000000000..cae327fd80 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/delete_sites_test.go @@ -0,0 +1,102 @@ +package chunkstore + +import ( + "go/ast" + "go/parser" + "go/token" + "os" + "path/filepath" + "sort" + "strings" + "testing" +) + +// deleteSites is every non-test function in core/ and cmd/ that deletes a +// filer entry (DeleteEntry) or builds a volume directory name (VolumeDir), +// with what it deletes or where the name comes from. Nothing removes a +// deleted HEAD, a VOLUME or a volume directory, and a volume directory is +// named only from a reference (a root's owner name and id) or from the +// master's volume record, never from a name given plus an id read from +// that same directory. A new site fails until it is classified here. +var deleteSites = map[string]string{ + "core/frontend/chunkstore/catalog.go Catalog.dropIntent -> DeleteEntry": "an intent entry (old store)", + "core/frontend/chunkstore/catalog.go Catalog.DeleteSnapshot -> DeleteEntry": "a snapshot name entry, conditional on its attempt (old store)", + "core/frontend/chunkstore/chunk_remote.go ChunkRemote.DeleteIntent -> DeleteEntry": "an intent entry (old store)", + "core/frontend/chunkstore/retention.go Catalog.freeName -> DeleteEntry": "a snapshot name entry, conditional on its attempt (old store)", + "core/frontend/chunkstore/swfs.go SWFS.DeleteEntry -> DeleteEntry": "the filer client itself", + "core/frontend/chunkstore/layout/read.go Image.Chunk -> VolumeDir": "a foreign chunk's directory from the reference's owner name and id", +} + +func TestDeleteAndDirectorySites_Enumeration(t *testing.T) { + root := filepath.Join("..", "..", "..") + found := map[string]bool{} + for _, top := range []string{"core", "cmd"} { + err := filepath.Walk(filepath.Join(root, top), func(p string, fi os.FileInfo, err error) error { + if err != nil { + return err + } + if fi.IsDir() || !strings.HasSuffix(p, ".go") || strings.HasSuffix(p, "_test.go") || strings.Contains(p, "filerpb") { + return nil + } + file, err := parser.ParseFile(token.NewFileSet(), p, nil, 0) + if err != nil { + return err + } + rel, _ := filepath.Rel(root, p) + rel = filepath.ToSlash(rel) + for _, d := range file.Decls { + fn, ok := d.(*ast.FuncDecl) + if !ok || fn.Body == nil { + continue + } + name := fn.Name.Name + if fn.Recv != nil && len(fn.Recv.List) == 1 { + switch x := fn.Recv.List[0].Type.(type) { + case *ast.Ident: + name = x.Name + "." + name + case *ast.StarExpr: + name = x.X.(*ast.Ident).Name + "." + name + } + } + ast.Inspect(fn.Body, func(n ast.Node) bool { + c, ok := n.(*ast.CallExpr) + if !ok { + return true + } + var callee string + switch x := c.Fun.(type) { + case *ast.SelectorExpr: + callee = x.Sel.Name + case *ast.Ident: + callee = x.Name + } + if callee == "DeleteEntry" || callee == "VolumeDir" { + key := rel + " " + strings.TrimPrefix(name, "*") + " -> " + callee + found[key] = true + } + return true + }) + } + return nil + }) + if err != nil { + t.Fatal(err) + } + } + var added, gone []string + for k := range found { + if _, ok := deleteSites[k]; !ok { + added = append(added, k) + } + } + for k := range deleteSites { + if !found[k] { + gone = append(gone, k) + } + } + sort.Strings(added) + sort.Strings(gone) + if len(added) != 0 || len(gone) != 0 { + t.Fatalf("delete or directory-naming sites changed: classify each (it must not remove a HEAD, a VOLUME or a volume directory, and a directory name must come from a reference or the master's record).\nnew: %v\ngone: %v", added, gone) + } +} diff --git a/seaweed-block/core/frontend/chunkstore/head_readers_test.go b/seaweed-block/core/frontend/chunkstore/head_readers_test.go index 7443284957..b612cb859d 100644 --- a/seaweed-block/core/frontend/chunkstore/head_readers_test.go +++ b/seaweed-block/core/frontend/chunkstore/head_readers_test.go @@ -40,7 +40,7 @@ var headReaders = map[string]string{ "core/frontend/chunkstore/share_guard.go beginShare -> ReadHead": "refuses outcome_unknown: the HEAD to share is no longer current", "core/frontend/chunkstore/usage.go Usage -> ReadHead": "GAP: reports an empty image, no error (probe Usage)", "core/frontend/chunkstore/verify.go readHead -> LookupEntry": "finding ROOT_INVALID (probe Verify)", - "core/frontend/chunkstore/layout/delete.go OperatorReleaseDependent -> ReadHead": "reads the source's HEAD (absent: refuses ErrHeadAbsent) and the target's: with no HEAD and no VOLUME the target was never created and gets a deleted HEAD written exclusively before the release; with VOLUME and no HEAD it is being created and is refused (probe LayoutOperatorRelease)", + "core/frontend/chunkstore/layout/delete.go OperatorReleaseDependent -> ReadHead": "reads the source's HEAD (absent: refuses ErrHeadAbsent) and the target's: a target without HEAD (never created, or a restore that died between its VOLUME and its HEAD) gets a deleted HEAD written exclusively before the release (probe LayoutOperatorRelease)", "core/frontend/chunkstore/layout/remove.go RemoveUnreachable -> ReadHead": "refuses ErrHeadAbsent and writes nothing to the objects (probe LayoutRemoval)", "core/frontend/chunkstore/layout/head.go runTransition -> ReadHead": "refuses ErrHeadAbsent and writes nothing; no layout transition creates a HEAD (probe LayoutTransition)", "core/frontend/chunkstore/verify.go run -> LookupEntry": "finding ROOT_INVALID (probe Verify)", @@ -294,8 +294,9 @@ func TestHeadReaders_AbsentHeadProbes(t *testing.T) { t.Fatal(err) } src := &presentLayoutHead{} - if _, err := layout.OperatorReleaseDependent(ctx, src, &absentLayoutHead{}, r, tid); !errors.Is(err, layout.ErrTargetLive) || src.updates != 0 { - t.Fatalf("operator release of a target with VOLUME and no HEAD: %v, %d writes; want ErrTargetLive and none", err, src.updates) + withVol := &absentLayoutHead{} + if _, err := layout.OperatorReleaseDependent(ctx, src, withVol, r, tid); err != nil || withVol.created == nil || withVol.created.State != layout.StateDeleted { + t.Fatalf("operator release of a target with VOLUME and no HEAD: %v, tombstone %+v; want a deleted HEAD written, then the release", err, withVol.created) } none, _ := layout.NewReader("/swblock/chunks", "t", func(context.Context, string) ([]byte, bool, error) { return nil, false, nil }) tomb := &absentLayoutHead{} diff --git a/seaweed-block/core/frontend/chunkstore/layout/delete.go b/seaweed-block/core/frontend/chunkstore/layout/delete.go index 642423b43f..376be4a8d3 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/delete.go +++ b/seaweed-block/core/frontend/chunkstore/layout/delete.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + "strings" ) // HEAD states: a live volume, one being deleted, one deleted. @@ -138,6 +139,11 @@ func DeleteVolume(ctx context.Context, st HeadStore, gone HolderGone, id VolumeI if gone == nil || os == nil { return errors.New("layout: delete needs a holder check and an object store") } + for _, rel := range objects { + if !isObjectPath(rel) { + return fmt.Errorf("%w: %q is not a pool, segment or root object; HEAD, VOLUME and the directory stay", ErrMalformed, rel) + } + } if err := gone.NoHolder(ctx); err != nil { return fmt.Errorf("%w: %v", ErrHolderActive, err) } @@ -166,12 +172,13 @@ func DeleteVolume(ctx context.Context, st HeadStore, gone HolderGone, id VolumeI // exists and is deleted or no longer names the source, read first: its // HEAD is deleting or deleted; its directory's VOLUME carries another id; // or its HEAD is live and no root in it (current or snapshot) names a -// chunk of the source. A target never created (no VOLUME, no HEAD) first -// gets a deleted HEAD written exclusively in its never-reused directory, -// so the restore that registered it can never create it later, and is -// then released. A live target that still names the source, one being -// created (VOLUME without HEAD), and one whose HEAD appeared before the -// tombstone are refused (ErrTargetLive); the source is not written then. +// chunk of the source. A target without HEAD (never created, or a restore +// that died between its VOLUME and its HEAD) first gets a deleted HEAD +// written exclusively in its never-reused directory, so the restore that +// registered it can never create it later, and is then released. A live +// target that still names the source, and one whose HEAD appeared before +// the tombstone, are refused (ErrTargetLive); the source is not written +// then. func OperatorReleaseDependent(ctx context.Context, source HeadStore, target TargetStore, r *Reader, targetID VolumeID) (Head, error) { sh, found, err := source.ReadHead(ctx) if err != nil { @@ -192,11 +199,12 @@ func OperatorReleaseDependent(ctx context.Context, source HeadStore, target Targ switch { case hasHead && (th.State == StateDeleting || th.State == StateDeleted): case hasVolume && id != targetID: - case !hasVolume && !hasHead: - // Never created: a deleted HEAD written exclusively in the target's + case !hasHead: + // No HEAD (never created, or a restore that died between its VOLUME + // and its HEAD): a deleted HEAD written exclusively in the target's // never-reused directory makes sure the restore can never create it // later; only then is the dependent released. If a HEAD appeared - // first, the restore got there: refused, re-run decides. + // first, the restore got there: refused, a re-run decides. tomb := Head{VolumeID: targetID, Activity: Activity{Kind: ActIdle}, State: StateDeleted} if err := target.CreateDeletedHead(ctx, tomb); err != nil { if errors.Is(err, ErrExists) { @@ -204,7 +212,7 @@ func OperatorReleaseDependent(ctx context.Context, source HeadStore, target Targ } return Head{}, err } - case hasVolume && hasHead: + default: names, err := namesVolume(ctx, r, th, sh.VolumeID) if err != nil { return Head{}, err @@ -212,8 +220,6 @@ func OperatorReleaseDependent(ctx context.Context, source HeadStore, target Targ if names { return Head{}, fmt.Errorf("%w: %s still names chunks of the source", ErrTargetLive, r.vol) } - default: - return Head{}, fmt.Errorf("%w: %s is being created", ErrTargetLive, r.vol) } return RunReleaseDependent(ctx, source, targetID) } @@ -248,3 +254,15 @@ func namesVolume(ctx context.Context, r *Reader, h Head, owner VolumeID) (bool, } return false, nil } + +// isObjectPath reports whether rel names a content-named object of the +// volume (a chunk, a segment or a root): the only entries a volume delete +// removes. HEAD (which stays, deleted), VOLUME and the directory are not. +func isObjectPath(rel string) bool { + for _, dir := range []string{PoolDir + "/", SegDir + "/", RootDir + "/"} { + if strings.HasPrefix(rel, dir) && len(rel) > len(dir) && !strings.Contains(rel, "..") { + return true + } + } + return false +} diff --git a/seaweed-block/core/frontend/chunkstore/layout/delete_test.go b/seaweed-block/core/frontend/chunkstore/layout/delete_test.go index 94b56516ed..b2ac465f09 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/delete_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/delete_test.go @@ -315,9 +315,9 @@ func operatorCase(t *testing.T, tgtHead *Head, tgtVolume *VolumeID, namesSource // exists and is deleted or no longer names the source: its HEAD deleting // or deleted, its name holding another volume (another VOLUME id), or a // live HEAD none of whose roots names a chunk of the source. A live target -// that still names the source and one being created (VOLUME without HEAD) -// are refused and the source is unchanged. A target never created (no -// VOLUME, no HEAD) gets a deleted HEAD written exclusively in its +// that still names the source is refused and the source is unchanged. A +// target without HEAD (never created, or a restore that died between its +// VOLUME and its HEAD) gets a deleted HEAD written exclusively in its // never-reused directory, so the restore can never create it later, and // only then is released. func TestOperatorReleaseDependent(t *testing.T) { @@ -334,18 +334,18 @@ func TestOperatorReleaseDependent(t *testing.T) { release bool want error }{ - "live target naming the source": {&live, &tid, true, false, ErrTargetLive}, - "live target no longer naming it": {&live, &tid, false, true, nil}, - "target being created": {nil, &tid, false, false, ErrTargetLive}, - "target never created": {nil, nil, false, true, nil}, - "target deleting": {&deleting, &tid, true, true, nil}, - "name holds another volume": {nil, &other, false, true, nil}, + "live target naming the source": {&live, &tid, true, false, ErrTargetLive}, + "live target no longer naming it": {&live, &tid, false, true, nil}, + "restore died between its creates": {nil, &tid, false, true, nil}, + "target never created": {nil, nil, false, true, nil}, + "target deleting": {&deleting, &tid, true, true, nil}, + "name holds another volume": {nil, &other, false, true, nil}, } { src, tgt, r := operatorCase(t, tc.head, tc.volume, tc.namesSource) before := src.updates _, err := OperatorReleaseDependent(ctx, src, tgt, r, tid) released := len(src.h.Snapshots["daily"].Dependents) == 0 - if tc.head == nil && tc.volume == nil && (tgt.h == nil || tgt.h.State != StateDeleted || tgt.h.VolumeID != tid) { + if tc.head == nil && (tc.volume == nil || *tc.volume == tid) && (tgt.h == nil || tgt.h.State != StateDeleted || tgt.h.VolumeID != tid) { t.Errorf("%s: target HEAD %+v; want a deleted HEAD (tombstone) of the target's id written before the release", name, tgt.h) } if tc.release && (err != nil || !released) { @@ -383,3 +383,15 @@ func (r *raceTarget) CreateDeletedHead(ctx context.Context, h Head) error { } return r.memHead.CreateDeletedHead(ctx, h) } + +// A volume delete removes only content-named objects: HEAD (which stays, +// deleted), VOLUME and the directory are refused before any write. +func TestDeleteVolume_RemovesOnlyObjects(t *testing.T) { + for _, bad := range []string{HeadName, VolumeName, "", "pool/", "pool/../HEAD", "other/x"} { + w := newDeleteWorld() + o := w.objects() + if err := DeleteVolume(context.Background(), w.t, gone{}, idFor("t"), []HeadStore{w.src}, o, append(append([]string(nil), w.rels...), bad)); !errors.Is(err, ErrMalformed) || w.t.updates != 0 || len(o.writes) != 0 { + t.Errorf("delete listing %q: %v (%d HEAD writes, %d object writes); want ErrMalformed and nothing written", bad, err, w.t.updates, len(o.writes)) + } + } +} diff --git a/seaweed-block/core/frontend/chunkstore/layout/volume.go b/seaweed-block/core/frontend/chunkstore/layout/volume.go index 3da1a6fbd9..a83a913eeb 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/volume.go +++ b/seaweed-block/core/frontend/chunkstore/layout/volume.go @@ -12,6 +12,10 @@ import ( // identity: written once, never changed. const VolumeName = "VOLUME" +// HeadName is the entry, under the volume root, that holds the volume's +// HEAD; it is never removed (a deleted volume keeps its deleted HEAD). +const HeadName = "HEAD" + // VolumeID is a volume's identity: random, chosen when the volume is // created. A volume's name is a path and can be created again after a // delete; its id cannot, so a volume created again satisfies no reference diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 73b6ab0889..dd5d94576f 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -549,7 +549,7 @@ stateDiagram-v2 ## Part B. Invariant ledger -358 rows: 287 ACTIVE, 67 STUB, 4 ARCHIVED; 15 kept from the internal ledger, 343 new. +359 rows: 288 ACTIVE, 67 STUB, 4 ARCHIVED; 15 kept from the internal ledger, 344 new. The "Machine / transition" column refers to Part A. Notes under a table record what a row does not cover and, for kept rows, what was narrowed from the internal ledger. @@ -1113,11 +1113,12 @@ Notes: | `INV-SNAPK-DELETE-GATE-001` | HEAD becomes deleting only with no holder (checked immediately before), no open activity and no snapshot with dependents; from deleting on, no holder transition, claim or removal applies. | M9 idle → deleting | `core/frontend/chunkstore/layout` `delete.go` `Head.MarkDeleting`; `head.go` `holderErr` | `TestMarkDeleting_Closure`, `TestMarkDeleted_Closure`, `TestDeleting_RefusesEveryHolderStep`, `TestDeleteVolume` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-DELETE-GUARD-001` | A source scan requires DeleteVolume to run no-holder check, deleting, release, object deletes and deleted in that order, and allows it as the only remover besides RemoveUnreachable. | M9 idle → deleting → deleted | `core/frontend/chunkstore/layout` (source scan of `delete.go`) | `TestGuard_WritersTransitionsAndRemovers`, `TestGuard_Controls` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-VOLUME-DIR-001` | A volume lives in the directory -, created exclusively and never reused; a foreign chunk is read from the directory its reference's owner name and id give, after that directory's VOLUME id is checked. | Content-named layout | `core/frontend/chunkstore/layout` `volume.go` `VolumeDir`; `read.go` `Image.Chunk` | `TestRead_ForeignChunkFromSourcePool`, `TestRead_ForeignChunkNeedsTheOwnersVolumeID` | — (the snapshot kernel model is not in this repository) | ACTIVE | -| `INV-SNAPK-TOMBSTONE-001` | For a restore target never created the operator writes a deleted HEAD exclusively in its directory before releasing the dependent, so the restore cannot create it later; only that command creates a tombstone (source scan). | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent`, `TargetStore.CreateDeletedHead` | `TestOperatorReleaseDependent`, `TestOperatorReleaseDependent_TombstoneLosesToTheRestore`, `TestGuard_Controls` | — (the snapshot kernel model is not in this repository) | ACTIVE | +| `INV-SNAPK-TOMBSTONE-001` | For a restore target without HEAD (never created, or a restore died between its two creates) the operator writes a deleted HEAD exclusively in its directory before releasing, so the restore cannot create it later. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent`, `TargetStore.CreateDeletedHead` | `TestOperatorReleaseDependent`, `TestOperatorReleaseDependent_TombstoneLosesToTheRestore`, `TestGuard_Controls` | — (the snapshot kernel model is not in this repository) | ACTIVE | +| `INV-SNAPK-KEEP-HEAD-001` | Nothing removes a HEAD, a VOLUME or a volume directory: a volume delete removes only pool, segment and root objects; a source scan lists every delete and every directory-naming site in core/ and cmd/. | M9 deleting → deleted | `core/frontend/chunkstore/layout` `delete.go` `DeleteVolume`; (source scan) | `TestDeleteVolume_RemovesOnlyObjects`, `TestDeleteAndDirectorySites_Enumeration` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-RELEASE-001` | A target is released from a source's dependents by its volume id, from every snapshot, in one conditional update; a volume created again under its name releases nothing; an absent target writes nothing. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `Head.ReleaseDependent`, `RunReleaseDependent` | `TestReleaseDependent_Closure`, `TestRunReleaseDependent_InterruptAtEveryStep` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-RELEASE-CALLERS-001` | Only the target's own delete (its HEAD already deleting) and the operator's command release a dependent; a source scan restricts the callers and requires the operator's read of the target. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` (source scan of `delete.go`) | `TestGuard_Controls` | — (the snapshot kernel model is not in this repository) | ACTIVE | -| `INV-SNAPK-RELEASE-OPERATOR-001` | The operator's command releases a target that is deleted (HEAD deleting or deleted, or its directory's VOLUME of another id), one whose live roots name no chunk of the source, or one never created, after its tombstone. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent` | `TestOperatorReleaseDependent` | — (the snapshot kernel model is not in this repository) | ACTIVE | -| `INV-SNAPK-RELEASE-REFUSE-001` | The operator's command refuses a live target still naming the source, a target being created (VOLUME without HEAD) and one whose HEAD appeared before its tombstone, and then writes nothing to the source. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent` | `TestOperatorReleaseDependent` | — (the snapshot kernel model is not in this repository) | ACTIVE | +| `INV-SNAPK-RELEASE-OPERATOR-001` | The operator's command releases a target that is deleted (HEAD deleting or deleted, or its directory's VOLUME of another id), one whose live roots name no chunk of the source, or one without HEAD, after its tombstone. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent` | `TestOperatorReleaseDependent` | — (the snapshot kernel model is not in this repository) | ACTIVE | +| `INV-SNAPK-RELEASE-REFUSE-001` | The operator's command refuses a live target still naming the source and one whose HEAD appeared before its tombstone, and then writes nothing to the source. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent` | `TestOperatorReleaseDependent` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-STALE-HOLDER-001` | Every transition refuses an older epoch of the same server, the same epoch held by another server, and the same epoch and server with another claim id, in idle, snap and clean; a new transition must be added to the table. | M9 every transition | `core/frontend/chunkstore/layout` `Head.heldBy` and each transition | `TestTransitions_RefuseAStaleHolder` | — (the snapshot kernel model is not in this repository) | ACTIVE | Notes: