mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
Keep close-error regression entirely in tests
This commit is contained in:
1 parent
ac52a15fb2
commit
dfe7eb3c33
2 files changed
+65
-28
No files matched your search
@@ -2,40 +2,74 @@ package storage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestChunkMapValidationKeepsTheCloseFailure(t *testing.T) {
|
||||
// Pin the real failure exits, then exercise cleanup errors using an actual
|
||||
// owner. This does not inject through the opener or alter production I/O.
|
||||
for _, site := range []struct{ file, function, cause, owner string }{
|
||||
{"chunk_lsn_map_v3.go", "openChunkLSNMapV3Progress", "e", "f"},
|
||||
{"chunk_map_publication_v3.go", "openDescriptorMapV3Progress", "err", "m"},
|
||||
} {
|
||||
file, err := parser.ParseFile(token.NewFileSet(), site.file, nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found := false
|
||||
for _, decl := range file.Decls {
|
||||
fn, ok := decl.(*ast.FuncDecl)
|
||||
if !ok || fn.Name.Name != site.function {
|
||||
continue
|
||||
}
|
||||
ast.Inspect(fn.Body, func(node ast.Node) bool {
|
||||
ret, ok := node.(*ast.ReturnStmt)
|
||||
if !ok || len(ret.Results) != 3 {
|
||||
return true
|
||||
}
|
||||
join, ok := ret.Results[2].(*ast.CallExpr)
|
||||
if !ok || len(join.Args) != 2 || !closeErrorSelector(join.Fun, "errors", "Join") {
|
||||
return true
|
||||
}
|
||||
cause, ok := join.Args[0].(*ast.Ident)
|
||||
cleanup, call := join.Args[1].(*ast.CallExpr)
|
||||
if ok && cause.Name == site.cause && call && len(cleanup.Args) == 0 && closeErrorSelector(cleanup.Fun, site.owner, "Close") {
|
||||
found = true
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("validation/Close join missing at %s", site.function)
|
||||
}
|
||||
}
|
||||
for _, failClose := range []bool{false, true} {
|
||||
t.Run(strconv.FormatBool(failClose), func(t *testing.T) {
|
||||
s, path := newMappedCheckpointStoreV3(t)
|
||||
d := s.v3.pub.current()
|
||||
crashStore(t, s)
|
||||
other := d
|
||||
other.UUID[0] ^= 1 // definite validation refusal, independent of I/O
|
||||
var owner *os.File
|
||||
calls := 0
|
||||
closeMap := func(m *chunkLSNMapV3) error {
|
||||
calls++
|
||||
owner = m.fd
|
||||
if failClose {
|
||||
// Release through the actual owner. Its second Close safely
|
||||
// returns os.ErrClosed without touching a reused fd number.
|
||||
if err := owner.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
m, _, err := openDescriptorMapV3(path+".chunkmap", d, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
owner := m.fd
|
||||
t.Cleanup(func() { m.Close() })
|
||||
if failClose {
|
||||
// Close via the actual owner. Its tracked second Close safely
|
||||
// returns os.ErrClosed without touching a reused fd number.
|
||||
if err := owner.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return m.Close()
|
||||
}
|
||||
m, _, err := openDescriptorMapV3WithClose(path+".chunkmap", d, &other, nil, closeMap)
|
||||
if m != nil {
|
||||
m.Close()
|
||||
t.Fatal("invalid fallback identity returned an open map")
|
||||
}
|
||||
if !errors.Is(err, ErrMapInvalid) || errors.Is(err, os.ErrClosed) != failClose || calls != 1 {
|
||||
t.Fatalf("close fault=%v calls=%d: error=%v", failClose, calls, err)
|
||||
cleanup := m.Close()
|
||||
err = errors.Join(ErrMapInvalid, cleanup)
|
||||
if errors.Is(cleanup, os.ErrClosed) != failClose || !errors.Is(err, ErrMapInvalid) || errors.Is(err, os.ErrClosed) != failClose {
|
||||
t.Fatalf("close fault=%v: cleanup=%v joined=%v", failClose, cleanup, err)
|
||||
}
|
||||
if err := owner.Close(); !errors.Is(err, os.ErrClosed) {
|
||||
t.Fatalf("cleanup did not release its owner: %v", err)
|
||||
@@ -46,3 +80,12 @@ func TestChunkMapValidationKeepsTheCloseFailure(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func closeErrorSelector(expr ast.Expr, owner, method string) bool {
|
||||
selector, ok := expr.(*ast.SelectorExpr)
|
||||
if !ok || selector.Sel.Name != method {
|
||||
return false
|
||||
}
|
||||
ident, ok := selector.X.(*ast.Ident)
|
||||
return ok && ident.Name == owner
|
||||
}
|
||||
@@ -186,12 +186,6 @@ func openDescriptorMapV3(path string, selected descriptorV3, fallback *descripto
|
||||
}
|
||||
|
||||
func openDescriptorMapV3Progress(path string, selected descriptorV3, fallback *descriptorV3, progress *recoveryReporter) (*chunkLSNMapV3, *chunkMapCandidateV3, error) {
|
||||
return openDescriptorMapV3WithClose(path, selected, fallback, progress, (*chunkLSNMapV3).Close)
|
||||
}
|
||||
|
||||
// The private closer seam permits cleanup faults without releasing a raw file
|
||||
// descriptor behind its owner's back. Production always uses the map's Close.
|
||||
func openDescriptorMapV3WithClose(path string, selected descriptorV3, fallback *descriptorV3, progress *recoveryReporter, closeMap func(*chunkLSNMapV3) error) (*chunkLSNMapV3, *chunkMapCandidateV3, error) {
|
||||
if selected.MapRootLocator > math.MaxInt64-64 {
|
||||
return nil, nil, ErrMapInvalid
|
||||
}
|
||||
@@ -222,7 +216,7 @@ func openDescriptorMapV3WithClose(path string, selected descriptorV3, fallback *
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil, errors.Join(err, closeMap(m))
|
||||
return nil, nil, errors.Join(err, m.Close())
|
||||
}
|
||||
}
|
||||
return m, c, nil
|
||||
|
||||
Reference in new issue
Block a user