mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
Merge branch 'feat/oss-snap-identity' into feat/oss-snap-delete
# Conflicts: # core/frontend/chunkstore/layout/guard_test.go
This commit is contained in:
commit
a63a80d329
2 files changed
+132
-23
No files matched your search
@@ -193,21 +193,57 @@ func guardFindings(code map[string]map[string]bool, decl, tests map[string]bool)
|
||||
}
|
||||
|
||||
// removerOrderFindings checks the removal half of the time premise in the
|
||||
// sources: in RemoveUnreachable, every statement that deletes
|
||||
// (DeleteObject) sits in a block where an earlier statement checks the
|
||||
// remover's own lease (StillHeld).
|
||||
// sources: in RemoveUnreachable, every statement that calls a method named
|
||||
// Delete* or Remove* is IMMEDIATELY preceded, in the same block, by a lease
|
||||
// gate: an if statement whose init calls StillHeld and whose body returns
|
||||
// on its error. A check whose result is ignored, one check reused for a
|
||||
// second removal, and a removal method the scan does not list each fail.
|
||||
func removerOrderFindings(t *testing.T, srcs map[string]string) []string {
|
||||
t.Helper()
|
||||
uses := func(n ast.Node, name string) bool {
|
||||
removes := func(n ast.Node) bool {
|
||||
found := false
|
||||
ast.Inspect(n, func(n ast.Node) bool {
|
||||
if sel, ok := n.(*ast.SelectorExpr); ok && sel.Sel.Name == name {
|
||||
found = true
|
||||
if c, ok := n.(*ast.CallExpr); ok {
|
||||
if sel, ok := c.Fun.(*ast.SelectorExpr); ok && (strings.HasPrefix(sel.Sel.Name, "Delete") || strings.HasPrefix(sel.Sel.Name, "Remove")) {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
return !found
|
||||
})
|
||||
return found
|
||||
}
|
||||
leaseGate := func(st ast.Stmt) bool {
|
||||
is, ok := st.(*ast.IfStmt)
|
||||
if !ok || is.Init == nil {
|
||||
return false
|
||||
}
|
||||
as, ok := is.Init.(*ast.AssignStmt)
|
||||
if !ok || len(as.Rhs) != 1 || len(as.Lhs) != 1 {
|
||||
return false
|
||||
}
|
||||
call, ok := as.Rhs[0].(*ast.CallExpr)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
sel, ok := call.Fun.(*ast.SelectorExpr)
|
||||
if !ok || sel.Sel.Name != "StillHeld" {
|
||||
return false
|
||||
}
|
||||
errName, ok := as.Lhs[0].(*ast.Ident)
|
||||
cond, ok2 := is.Cond.(*ast.BinaryExpr)
|
||||
if !ok || !ok2 || cond.Op != token.NEQ {
|
||||
return false
|
||||
}
|
||||
if x, ok := cond.X.(*ast.Ident); !ok || x.Name != errName.Name {
|
||||
return false
|
||||
}
|
||||
for _, b := range is.Body.List {
|
||||
if _, ok := b.(*ast.ReturnStmt); ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
var bad []string
|
||||
for fname, src := range srcs {
|
||||
file, err := parser.ParseFile(token.NewFileSet(), fname, src, 0)
|
||||
@@ -226,24 +262,16 @@ func removerOrderFindings(t *testing.T, srcs map[string]string) []string {
|
||||
return true
|
||||
}
|
||||
for i, st := range blk.List {
|
||||
if _, isBlock := st.(*ast.BlockStmt); isBlock || !uses(st, "DeleteObject") {
|
||||
continue
|
||||
switch st.(type) {
|
||||
case *ast.BlockStmt, *ast.RangeStmt, *ast.ForStmt:
|
||||
continue // their bodies are checked as their own blocks
|
||||
}
|
||||
if _, nested := st.(*ast.RangeStmt); nested {
|
||||
continue // its body is checked as its own block
|
||||
}
|
||||
if _, nested := st.(*ast.ForStmt); nested {
|
||||
if !removes(st) {
|
||||
continue
|
||||
}
|
||||
deletes++
|
||||
checked := false
|
||||
for _, prev := range blk.List[:i] {
|
||||
if uses(prev, "StillHeld") {
|
||||
checked = true
|
||||
}
|
||||
}
|
||||
if !checked {
|
||||
bad = append(bad, "RemoveUnreachable deletes without a lease check before it in the same block")
|
||||
if i == 0 || !leaseGate(blk.List[i-1]) {
|
||||
bad = append(bad, "RemoveUnreachable removes without a lease gate immediately before it (an if on StillHeld's error that returns)")
|
||||
}
|
||||
}
|
||||
return true
|
||||
@@ -376,7 +404,7 @@ func RemoveUnreachable(ctx context.Context, x p, h l, names []string) error {
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}`, want: "deletes without a lease check"},
|
||||
}`, want: "without a lease gate immediately before it"},
|
||||
"lease check after the delete": {file: "remove.go", src: `type p interface{ DeleteObject(context.Context, string) error }
|
||||
type l interface{ StillHeld(context.Context) error }
|
||||
func RemoveUnreachable(ctx context.Context, x p, h l, names []string) error {
|
||||
@@ -389,7 +417,7 @@ func RemoveUnreachable(ctx context.Context, x p, h l, names []string) error {
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}`, want: "deletes without a lease check"},
|
||||
}`, want: "without a lease gate immediately before it"},
|
||||
"lease check once before the loop": {file: "remove.go", src: `type p interface{ DeleteObject(context.Context, string) error }
|
||||
type l interface{ StillHeld(context.Context) error }
|
||||
func RemoveUnreachable(ctx context.Context, x p, h l, names []string) error {
|
||||
@@ -402,7 +430,7 @@ func RemoveUnreachable(ctx context.Context, x p, h l, names []string) error {
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}`, want: "deletes without a lease check"},
|
||||
}`, want: "without a lease gate immediately before it"},
|
||||
"claim without lease wait": {src: `func RunClaim(ctx context.Context, st HeadStore, lease LeaseWait, me HolderID) (Head, error) {
|
||||
return runTransition(ctx, st, func(h Head) (Head, bool, error) { return h, false, nil })
|
||||
}`, want: "does not wait for the old lease", file: "claim.go"},
|
||||
@@ -443,6 +471,9 @@ func DeleteVolume(ctx context.Context, st HeadStore, id VolumeID, srcs []HeadSto
|
||||
"operator release without reading the target": {file: "delete.go", src: `func OperatorReleaseDependent(ctx context.Context, source HeadStore, id VolumeID) (Head, error) {
|
||||
return RunReleaseDependent(ctx, source, id)
|
||||
}`, want: "does not read the target's HEAD"},
|
||||
"ignored lease check": {file: "remove.go", src: removeVariant(t, "ignored"), want: "without a lease gate immediately before it"},
|
||||
"one check, two deletes": {file: "remove.go", src: removeVariant(t, "second"), want: "without a lease gate immediately before it"},
|
||||
"unlisted removal method": {file: "remove.go", src: removeVariant(t, "other"), want: "without a lease gate immediately before it"},
|
||||
"remover method value": {src: `type p interface{ RemoveName(string) }
|
||||
func w(x p) { f := x.RemoveName; f("o") }`, want: "only RemoveUnreachable (after its lease re-check) and DeleteVolume"},
|
||||
} {
|
||||
@@ -458,3 +489,28 @@ func w(x p) { f := x.RemoveName; f("o") }`, want: "only RemoveUnreachable (after
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// removeVariant returns remove.go's real source changed in one way, minus
|
||||
// its package clause and imports (the controls add their own header).
|
||||
func removeVariant(t *testing.T, mode string) string {
|
||||
t.Helper()
|
||||
b, err := os.ReadFile("remove.go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src := strings.ReplaceAll(string(b), "\r\n", "\n")
|
||||
gate := "if err := lease.StillHeld(ctx); err != nil {\n\t\t\treturn removed, fmt.Errorf(\"%w: %v\", ErrLeaseLost, err)\n\t\t}"
|
||||
switch mode {
|
||||
case "ignored":
|
||||
if !strings.Contains(src, gate) {
|
||||
t.Fatal("remove.go lease gate not found")
|
||||
}
|
||||
src = strings.Replace(src, gate, "_ = lease.StillHeld(ctx)", 1)
|
||||
case "second":
|
||||
src = strings.Replace(src, "removed = append(removed, c)", "_ = pool.DeleteObject(ctx, c.Path())\n\t\tremoved = append(removed, c)", 1)
|
||||
case "other":
|
||||
src = strings.Replace(src, "removed = append(removed, c)", "_ = pool.RemoveOther(ctx, c.Path())\n\t\tremoved = append(removed, c)", 1)
|
||||
}
|
||||
i := strings.Index(src, ")\n")
|
||||
return src[i+2:]
|
||||
}
|
||||
@@ -1,8 +1,14 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -52,3 +58,50 @@ func TestTransitions_RefuseAStaleHolder(t *testing.T) {
|
||||
t.Fatalf("transitions not in the stale-holder table: %v", missing)
|
||||
}
|
||||
}
|
||||
|
||||
// The removal refuses a stale holder too, one dimension at a time: an
|
||||
// older epoch of the same server, and the same epoch held by another
|
||||
// server; nothing is deleted. Every function of the package that takes a
|
||||
// HolderID and is not a Run transition (those go through the pure methods
|
||||
// above) must be listed here, so a new one fails until it is covered.
|
||||
func TestHolderFunctions_RefuseAStaleHolder(t *testing.T) {
|
||||
callers := map[string]HolderID{
|
||||
"older epoch, same server": {Epoch: me.Epoch - 1, Holder: me.Holder, ClaimID: me.ClaimID},
|
||||
"same epoch, another server": {Epoch: me.Epoch, Holder: "srv-b", ClaimID: me.ClaimID},
|
||||
"same epoch and server, another claim id": {Epoch: me.Epoch, Holder: me.Holder, ClaimID: "k-other"},
|
||||
}
|
||||
for who, c := range callers {
|
||||
o, h, _, orphans := removeFixture(t)
|
||||
r, err := NewReader(removeBase, "v1", o.fetch)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = RemoveUnreachable(context.Background(), &memHead{h: h}, r, o, &fakeHeld{}, c, "c1", 0, orphans)
|
||||
if !errors.Is(err, ErrNotHolder) || len(o.deletes) != 0 {
|
||||
t.Errorf("RemoveUnreachable by %s: %v, %d deletes; want ErrNotHolder and none", who, err, len(o.deletes))
|
||||
}
|
||||
}
|
||||
covered := map[string]bool{"RemoveUnreachable": true}
|
||||
var missing []string
|
||||
for fname, src := range packageSources(t, false) {
|
||||
file, err := parser.ParseFile(token.NewFileSet(), fname, src, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, d := range file.Decls {
|
||||
fn, ok := d.(*ast.FuncDecl)
|
||||
if !ok || fn.Recv != nil || strings.HasPrefix(fn.Name.Name, "Run") {
|
||||
continue
|
||||
}
|
||||
for _, p := range fn.Type.Params.List {
|
||||
if id, ok := p.Type.(*ast.Ident); ok && id.Name == "HolderID" && !covered[fn.Name.Name] {
|
||||
missing = append(missing, fn.Name.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(missing)
|
||||
if len(missing) != 0 {
|
||||
t.Fatalf("functions taking a HolderID not covered by a stale-holder case: %v", missing)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user