mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
Brings seaweed-block main (v1.1.1 plus the docs and a test fix after it) into seaweed-block/ as one commit. Only files under seaweed-block/ change, and the subtree equals seaweed-block main exactly. The module's full history stays in the seaweed-block repository. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
321 lines
9.7 KiB
Go
321 lines
9.7 KiB
Go
// Package timebound keeps the registry of fixed time bounds in core/ and cmd/
|
|
// (docs/time-bounds.tsv) in step with the code: every bound site is listed
|
|
// with its class and model row, and a per-call bound never wraps a loop over
|
|
// chunks or objects.
|
|
package timebound
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// A site is one fixed time bound in the source.
|
|
type site struct {
|
|
Path string // repository-relative, forward slashes
|
|
Func string // enclosing top-level function (Name or Recv.Name), or "var Name" for a package-level initializer
|
|
Kind string // what sets the bound (see kinds)
|
|
Ordinal int // 1-based, among the same Path, Func and Kind in source order
|
|
Line int
|
|
Loops []string // for loops after the site, in the same function, whose header names chunks or objects
|
|
}
|
|
|
|
// key identifies a site across edits that only move it.
|
|
func (s site) key() string {
|
|
return s.Path + "\t" + s.Func + "\t" + s.Kind + "\t" + strconv.Itoa(s.Ordinal)
|
|
}
|
|
|
|
// Package-qualified calls that set a bound, by import path. A time.Now().Add
|
|
// value is a deadline too (a loop or wait deadline), unless it lies in the
|
|
// deadline argument of a recorded WithDeadline or Set*Deadline call (then it
|
|
// is that call's deadline). One inside a callback, such as an AfterFunc's, is
|
|
// its own site.
|
|
var pkgCalls = map[string]map[string]bool{
|
|
"context": {"WithTimeout": true, "WithDeadline": true, "WithTimeoutCause": true, "WithDeadlineCause": true},
|
|
"time": {"NewTimer": true, "AfterFunc": true, "After": true},
|
|
}
|
|
|
|
// Method calls that set a connection deadline, whatever the receiver.
|
|
var deadlineMethods = map[string]bool{"SetDeadline": true, "SetReadDeadline": true, "SetWriteDeadline": true}
|
|
|
|
// net/http struct fields that are time bounds.
|
|
var httpTimeoutFields = map[string]map[string]bool{
|
|
"Client": {"Timeout": true},
|
|
"Server": {"ReadTimeout": true, "ReadHeaderTimeout": true, "WriteTimeout": true, "IdleTimeout": true},
|
|
}
|
|
|
|
// Field names whose assignment is an http time bound (client.Timeout = d).
|
|
var assignedTimeoutFields = map[string]bool{"Timeout": true, "ReadTimeout": true, "ReadHeaderTimeout": true, "WriteTimeout": true, "IdleTimeout": true}
|
|
|
|
// The product's own budget helpers: boundedPass bounds a whole pass,
|
|
// newBCommand gives each catalog CLI command its budget (which bCommand.run
|
|
// applies), and watchVerifyProgress enforces verify's no-progress window (its
|
|
// ticker only polls; the window is its argument).
|
|
var budgetCalls = map[string]bool{"boundedPass": true, "newBCommand": true, "watchVerifyProgress": true}
|
|
|
|
// sizedLoop names a loop over chunks or objects: its header mentions one.
|
|
var sizedLoop = regexp.MustCompile(`(?i)chunk|object|segment|manifest|\brefs?\b|references`)
|
|
|
|
// scan returns every bound site under core/ and cmd/ below root (non-test Go
|
|
// files, testdata excluded), sorted by path and line.
|
|
func scan(root string) ([]site, error) {
|
|
var out []site
|
|
for _, top := range []string{"core", "cmd"} {
|
|
err := filepath.WalkDir(filepath.Join(root, top), func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.IsDir() {
|
|
if d.Name() == "testdata" {
|
|
return filepath.SkipDir
|
|
}
|
|
return nil
|
|
}
|
|
if !strings.HasSuffix(p, ".go") || strings.HasSuffix(p, "_test.go") {
|
|
return nil
|
|
}
|
|
rel, err := filepath.Rel(root, p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sites, err := scanFile(p, filepath.ToSlash(rel))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
out = append(out, sites...)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
sort.Slice(out, func(i, j int) bool {
|
|
if out[i].Path != out[j].Path {
|
|
return out[i].Path < out[j].Path
|
|
}
|
|
return out[i].Line < out[j].Line
|
|
})
|
|
return out, nil
|
|
}
|
|
|
|
func scanFile(path, rel string) ([]site, error) {
|
|
src, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fset := token.NewFileSet()
|
|
f, err := parser.ParseFile(fset, path, src, parser.SkipObjectResolution)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
imports := map[string]string{} // local name -> import path
|
|
for _, im := range f.Imports {
|
|
p, _ := strconv.Unquote(im.Path.Value)
|
|
name := p[strings.LastIndex(p, "/")+1:]
|
|
if im.Name != nil {
|
|
name = im.Name.Name
|
|
}
|
|
imports[name] = p
|
|
}
|
|
text := func(n ast.Node) string {
|
|
return string(src[fset.Position(n.Pos()).Offset:fset.Position(n.End()).Offset])
|
|
}
|
|
var out []site
|
|
count := map[string]int{}
|
|
add := func(fn string, kind string, n ast.Node, body *ast.BlockStmt) {
|
|
s := site{Path: rel, Func: fn, Kind: kind, Line: fset.Position(n.Pos()).Line}
|
|
k := fn + "\t" + kind
|
|
count[k]++
|
|
s.Ordinal = count[k]
|
|
if body != nil {
|
|
ast.Inspect(body, func(m ast.Node) bool {
|
|
if m == nil || m.Pos() <= n.Pos() {
|
|
return true
|
|
}
|
|
var header string
|
|
switch l := m.(type) {
|
|
case *ast.RangeStmt:
|
|
header = text(l.X)
|
|
case *ast.ForStmt:
|
|
if l.Cond != nil {
|
|
header = text(l.Cond)
|
|
}
|
|
default:
|
|
return true
|
|
}
|
|
if sizedLoop.MatchString(header) {
|
|
s.Loops = append(s.Loops, strconv.Itoa(fset.Position(m.Pos()).Line)+": "+header)
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
out = append(out, s)
|
|
}
|
|
// visit finds the bounds under root. A bound's loop check runs over its
|
|
// innermost enclosing function body: a function literal's own body, else
|
|
// the declaration's (body; nil for an initializer outside any literal).
|
|
visit := func(fn string, root ast.Node, declBody *ast.BlockStmt) {
|
|
var lits []*ast.FuncLit // enclosing function literals, innermost last
|
|
var covered []ast.Node // deadline arguments of recorded calls: a time.Now().Add inside one is that call's
|
|
ast.Inspect(root, func(n ast.Node) bool {
|
|
if n == nil {
|
|
return true
|
|
}
|
|
for len(lits) > 0 && n.Pos() >= lits[len(lits)-1].End() {
|
|
lits = lits[:len(lits)-1] // left the innermost literal
|
|
}
|
|
if lit, ok := n.(*ast.FuncLit); ok {
|
|
lits = append(lits, lit)
|
|
}
|
|
body := declBody
|
|
if len(lits) > 0 {
|
|
body = lits[len(lits)-1].Body
|
|
}
|
|
switch x := n.(type) {
|
|
case *ast.CallExpr:
|
|
switch fun := x.Fun.(type) {
|
|
case *ast.SelectorExpr:
|
|
if id, ok := fun.X.(*ast.Ident); ok {
|
|
if names := pkgCalls[imports[id.Name]]; names[fun.Sel.Name] {
|
|
add(fn, imports[id.Name]+"."+fun.Sel.Name, x, body)
|
|
if (fun.Sel.Name == "WithDeadline" || fun.Sel.Name == "WithDeadlineCause") && len(x.Args) >= 2 {
|
|
covered = append(covered, x.Args[1]) // the deadline argument only
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
if deadlineMethods[fun.Sel.Name] {
|
|
if len(x.Args) == 1 && isZeroTime(x.Args[0], imports) {
|
|
return true // clears the deadline: no bound
|
|
}
|
|
add(fn, fun.Sel.Name, x, body)
|
|
for _, a := range x.Args { // the deadline argument only
|
|
covered = append(covered, a)
|
|
}
|
|
} else if fun.Sel.Name == "Add" && isTimeNow(fun.X, imports) && !inside(x, covered) {
|
|
add(fn, "time.Now().Add", x, body) // a deadline value (a loop or wait deadline)
|
|
} else if budgetCalls[fun.Sel.Name] {
|
|
add(fn, fun.Sel.Name, x, body)
|
|
}
|
|
case *ast.Ident:
|
|
if budgetCalls[fun.Name] {
|
|
add(fn, fun.Name, x, body)
|
|
}
|
|
}
|
|
case *ast.AssignStmt:
|
|
// client.Timeout = ...: an http time bound set by assignment (the field
|
|
// names are net/http's; the receiver's type is not resolved here).
|
|
for _, lhs := range x.Lhs {
|
|
if sel, ok := lhs.(*ast.SelectorExpr); ok && assignedTimeoutFields[sel.Sel.Name] {
|
|
add(fn, "set."+sel.Sel.Name, x, nil)
|
|
}
|
|
}
|
|
case *ast.CompositeLit:
|
|
sel, ok := x.Type.(*ast.SelectorExpr)
|
|
if !ok {
|
|
return true
|
|
}
|
|
id, ok := sel.X.(*ast.Ident)
|
|
if !ok || imports[id.Name] != "net/http" {
|
|
return true
|
|
}
|
|
fields := httpTimeoutFields[sel.Sel.Name]
|
|
for _, el := range x.Elts {
|
|
if kv, ok := el.(*ast.KeyValueExpr); ok {
|
|
if k, ok := kv.Key.(*ast.Ident); ok && fields[k.Name] {
|
|
add(fn, "http."+sel.Sel.Name+"."+k.Name, kv, nil)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
for _, decl := range f.Decls {
|
|
switch d := decl.(type) {
|
|
case *ast.FuncDecl:
|
|
if d.Body == nil {
|
|
continue
|
|
}
|
|
fn := d.Name.Name
|
|
if d.Recv != nil && len(d.Recv.List) == 1 {
|
|
fn = recvName(d.Recv.List[0].Type) + "." + fn
|
|
}
|
|
visit(fn, d.Body, d.Body)
|
|
case *ast.GenDecl: // package-level var initializers (and any func literal in them)
|
|
if d.Tok != token.VAR {
|
|
continue
|
|
}
|
|
for _, spec := range d.Specs {
|
|
vs, ok := spec.(*ast.ValueSpec)
|
|
if !ok || len(vs.Values) == 0 || len(vs.Names) == 0 {
|
|
continue
|
|
}
|
|
for _, v := range vs.Values {
|
|
visit("var "+vs.Names[0].Name, v, nil)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// isTimeNow reports a call of time.Now().
|
|
func isTimeNow(e ast.Expr, imports map[string]string) bool {
|
|
c, ok := e.(*ast.CallExpr)
|
|
if !ok || len(c.Args) != 0 {
|
|
return false
|
|
}
|
|
sel, ok := c.Fun.(*ast.SelectorExpr)
|
|
if !ok || sel.Sel.Name != "Now" {
|
|
return false
|
|
}
|
|
id, ok := sel.X.(*ast.Ident)
|
|
return ok && imports[id.Name] == "time"
|
|
}
|
|
|
|
// inside reports n lying within one of the nodes.
|
|
func inside(n ast.Node, nodes []ast.Node) bool {
|
|
for _, c := range nodes {
|
|
if n.Pos() >= c.Pos() && n.End() <= c.End() {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// isZeroTime reports time.Time{}: a deadline argument that clears the deadline.
|
|
func isZeroTime(e ast.Expr, imports map[string]string) bool {
|
|
lit, ok := e.(*ast.CompositeLit)
|
|
if !ok || len(lit.Elts) != 0 {
|
|
return false
|
|
}
|
|
sel, ok := lit.Type.(*ast.SelectorExpr)
|
|
if !ok || sel.Sel.Name != "Time" {
|
|
return false
|
|
}
|
|
id, ok := sel.X.(*ast.Ident)
|
|
return ok && imports[id.Name] == "time"
|
|
}
|
|
|
|
func recvName(e ast.Expr) string {
|
|
switch t := e.(type) {
|
|
case *ast.StarExpr:
|
|
return recvName(t.X)
|
|
case *ast.IndexExpr:
|
|
return recvName(t.X)
|
|
case *ast.IndexListExpr:
|
|
return recvName(t.X)
|
|
case *ast.Ident:
|
|
return t.Name
|
|
}
|
|
return "?"
|
|
}
|