Files
upstream-seaweedfs/weed/server/filer_grpc_server_stream_mutate_scheduler.go
upstream-mirror 89f5be005d
Some checks failed
go: build dev binaries / cleanup (push) Successful in 4s
Codespell / Check for spelling errors (push) Failing after 3s
docker: build dev containers / build-rust-binaries (amd64, x86_64-unknown-linux-musl) (push) Failing after 3s
docker: build dev containers / build-dev-containers (push) Has been skipped
docker: build dev containers / build-rust-binaries (arm64, true, aarch64-unknown-linux-musl) (push) Failing after 3s
End to End / FUSE Mount (push) Failing after 2s
EC Integration Tests / EC Integration Tests (push) Failing after 30s
FUSE DLM Integration Tests / FUSE DLM Integration Tests (push) Failing after 3s
FUSE Volume Server Failover Tests / FUSE Volume Server Failover (push) Failing after 3s
FUSE Integration Tests / FUSE Integration Testing (push) Failing after 2s
FUSE P2P Peer Chunk Sharing Integration Tests / FUSE P2P Peer Chunk Sharing (push) Failing after 3s
go: build binary / Go Vet (push) Failing after 2s
go: build binary / Go Vet 32-bit (push) Failing after 3s
go: build binary / Build (push) Failing after 4s
go: build binary / Build cross-platform (freebsd/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (darwin/arm64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/arm64) (push) Failing after 3s
go: build binary / Test (push) Failing after 2s
go: build binary / Test 32-bit (push) Failing after 3s
helm: lint and test charts / lint-test (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (11) (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (17) (push) Failing after 3s
go: build dev binaries / build_dev_linux_windows (amd64, linux) (push) Failing after 2s
go: build dev binaries / build_dev_linux_windows (amd64, windows) (push) Failing after 3s
go: build dev binaries / build_dev_darwin (amd64, darwin) (push) Has been skipped
go: build dev binaries / build_dev_darwin (arm64, darwin) (push) Has been skipped
Java Client Unit Tests / Java Unit Tests (11) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (17) (push) Failing after 2s
Java Client Unit Tests / Java Unit Tests (21) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (8) (push) Failing after 3s
Kafka Quick Test (Load Test with Schema Registry) / Kafka Client Load Test (Quick) (push) Failing after 3s
Kafka Gateway Tests / Kafka Unit Tests (unit-tests-1) (push) Failing after 11s
Kafka Gateway Tests / Kafka Integration Tests (Critical) (integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka End-to-End Tests (with SMQ) (e2e-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Consumer Group Tests (Highly Isolated) (consumer-group-1) (push) Failing after 2s
Kafka Gateway Tests / Kafka Client Compatibility (with SMQ) (client-compat-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka SMQ Integration Tests (Full Stack) (smq-integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Protocol Tests (Isolated) (protocol-1) (push) Failing after 3s
KMS Tests / KMS Provider Integration Tests (push) Failing after 2s
KMS Tests / S3 KMS End-to-End Tests (push) Failing after 3s
Master Cold Start Tests / Master Cold Start Tests (push) Failing after 4s
Metadata Subscribe Integration Tests / Metadata Subscribe Integration Tests (push) Failing after 3s
Multi-Master Tests / Multi-Master Failover Tests (push) Failing after 3s
Performance / CPU and Heap Profile (push) Failing after 2s
Performance / S3 Read/Write Benchmark (rust volume) (push) Failing after 3s
pjdfstest POSIX Compliance / pjdfstest (push) Failing after 2s
Plugin Worker Integration Tests / Plugin Worker: erasure_coding (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: vacuum (push) Failing after 3s
Rust Volume Server Tests / Rust Integration Tests (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 1) (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 1) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 3) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Distributed Tests (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Performance Tests (push) Failing after 3s
Performance / Throughput Benchmark (go volume) (push) Failing after 3s
Performance / Throughput Benchmark (rust volume) (push) Failing after 2s
Performance / S3 Read/Write Benchmark (go volume) (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: volume_balance (push) Failing after 3s
PostgreSQL Gateway Tests / PostgreSQL Basic Tests (push) Failing after 3s
Rust Volume Server Tests / Rust Unit Tests (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 3) (push) Failing after 3s
Rust Plugin Worker Tests / Rust Plugin Worker Build and Unit Tests (push) Failing after 3s
rust: build dev volume server binaries / cleanup (push) Successful in 3s
S3 ETag and ACL Tests / S3 ETag + ACL Integration Tests (push) Failing after 3s
S3 IAM Integration Tests / IAM Unit Tests (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (advanced) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (basic) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (group) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (policy-enforcement) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (sts) (push) Failing after 4s
S3 Keycloak Integration Tests / S3 Keycloak Integration Tests (push) Failing after 3s
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.11) (push) Failing after 3s
S3 PyArrow Parquet Tests / Go Unit Tests (Implicit Directory) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.12) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-amd64, x86_64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-arm64, aarch64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-linux (linux-amd64, x86_64-unknown-linux-gnu) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.9) (push) Has been cancelled
S3 Proxy Signature Tests / S3 Proxy Signature Verification Tests (push) Has been cancelled
S3 SDK V2 Route Disambiguation Tests / S3 SDK V2 Routing Tests (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (quick) (push) Has been cancelled
S3 SSE Tests / S3 SSE Compatibility Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Metadata Persistence Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Copy Operations Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Multipart Upload Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Performance Test (push) Has been cancelled
S3 SSE Tests / S3 Volume Encryption Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (comprehensive) (push) Has been cancelled
Spark Integration Tests / Spark Integration Tests (push) Failing after 2s
Telemetry Integration Tests / telemetry-integration-test (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 1) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 1) (push) Failing after 3s
Ceph S3 tests / Basic S3 tests (KV store) (push) Has been cancelled
Ceph S3 tests / S3 Versioning & Object Lock tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Unit Tests (push) Failing after 3s
Ceph S3 tests / S3 CORS tests (push) Has been cancelled
Ceph S3 tests / SeaweedFS Custom S3 Copy tests (push) Has been cancelled
Ceph S3 tests / Basic S3 tests (SQL store) (push) Has been cancelled
test s3 over https using aws-cli / awscli-tests (push) Has been cancelled
TLS Rotation Integration Tests / TLS Rotation Integration Tests (push) Has been cancelled
Vacuum Integration Tests / Vacuum Integration Tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Variables Integration Tests (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (advanced-policy) (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (basic-policy) (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (contextual-policy) (push) Failing after 4s
S3 Policy Integration Tests / Trusted Proxy Detection Tests (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Shell Integration Tests (push) Failing after 3s
Samba on FUSE Integration / samba-integration (push) Failing after 2s
SFTP Integration Tests / SFTP Integration Testing (push) Failing after 3s
terraform: validate and test modules / fmt, validate, plan-level tests (push) Failing after 3s
terraform: validate and test modules / local cluster smoke test (real weed) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 3) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 2) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 2) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 3) (push) Failing after 5s
mount: windows conformance / WinFsp conformance (push) Has been cancelled
mount: windows / Mount on Windows (push) Has been cancelled
snapshot of https://github.com/seaweedfs/seaweedfs.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 03:23:42 +05:00

380 lines
12 KiB
Go

package weed_server
import (
"path"
"sync"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// mutateJobKind classifies a streamed mutation for conflict detection. This is
// the same taxonomy MetadataProcessor uses in weed/command/filer_sync_jobs.go:
// separate a "barrier" directory event (create/delete/rename) from an in-place
// attribute-only directory update so that mtime/xattr bumps do not serialize
// every file operation in the subtree.
type mutateJobKind int
const (
// kindMutateFile: a regular file event. Conflicts with any in-flight job
// on the same path and with any barrier directory on the same path or on
// an ancestor.
kindMutateFile mutateJobKind = iota
// kindMutateBarrierDir: a directory create, a directory rename, or a
// delete (target type unknown on the wire). Acts as a subtree barrier:
// must drain every active descendant, and blocks every new job under it
// until it completes.
kindMutateBarrierDir
// kindMutateNonBarrierDir: an in-place directory attribute update
// (mode / xattr / mtime with unchanged name). Conflicts with any other
// in-flight job at the same path, but never with descendants.
kindMutateNonBarrierDir
)
// mutateScheduler serializes mutations by path while allowing cross-path work
// to run in parallel. The conflict taxonomy mirrors filer.sync's
// MetadataProcessor (weed/command/filer_sync_jobs.go); admission adds
// per-path FIFO ordering so two requests arriving on the same stream on the
// same path are always processed in arrival order, even when a Cond broadcast
// would otherwise race them.
//
// Invariants:
// - pathQueue[p] contains every waiter (pending + admitted) that is
// interested in path p, in arrival order. admit dequeues nothing; Done
// dequeues the head.
// - A waiter is admissible iff it is the head of every path queue it
// joined (primary and secondary), pathConflictsLocked passes for each
// path against *active* state, and totalActive < concurrencyLimit.
type mutateScheduler struct {
concurrencyLimit int
mu sync.Mutex
totalActive int
activeFilePaths map[util.FullPath]int
activeBarrierDirPaths map[util.FullPath]int
activeNonBarrierDirPaths map[util.FullPath]int
descendantCount map[util.FullPath]int
// pathQueue maps each path to the FIFO list of waiters (pending or
// admitted) interested in it. Entries are removed by Done.
pathQueue map[util.FullPath][]*mutateWaiter
}
// mutateWaiter is one outstanding Admit call. admitted flips under mu when
// the waiter moves from pending to active; ready is closed at the same time
// so the Admit caller unblocks.
type mutateWaiter struct {
primary, secondary util.FullPath
kind mutateJobKind
admitted bool
ready chan struct{}
}
func newMutateScheduler(concurrency int) *mutateScheduler {
return &mutateScheduler{
concurrencyLimit: concurrency,
activeFilePaths: make(map[util.FullPath]int),
activeBarrierDirPaths: make(map[util.FullPath]int),
activeNonBarrierDirPaths: make(map[util.FullPath]int),
descendantCount: make(map[util.FullPath]int),
pathQueue: make(map[util.FullPath][]*mutateWaiter),
}
}
// Admit blocks until this (primary, secondary, kind) tuple can be admitted
// without violating the conflict rules and without exceeding concurrencyLimit,
// and until every earlier waiter on any of its paths has itself been admitted.
// On return the job is registered in the indexes; the caller must call Done
// with the same arguments when the work is finished.
//
// For single-path operations (create / update / delete) pass secondary="".
// For rename, pass old path as primary and new path as secondary; kind is
// kindMutateBarrierDir.
func (s *mutateScheduler) Admit(primary, secondary util.FullPath, kind mutateJobKind) {
w := &mutateWaiter{
primary: primary,
secondary: secondary,
kind: kind,
ready: make(chan struct{}),
}
s.mu.Lock()
s.pathQueue[primary] = append(s.pathQueue[primary], w)
if secondary != "" && secondary != primary {
s.pathQueue[secondary] = append(s.pathQueue[secondary], w)
}
s.tryPromoteLocked()
s.mu.Unlock()
<-w.ready
}
// Done releases the slot reserved by Admit and promotes any waiters that
// became admissible as a result. Must be called exactly once per successful
// Admit with the same arguments.
func (s *mutateScheduler) Done(primary, secondary util.FullPath, kind mutateJobKind) {
s.mu.Lock()
defer s.mu.Unlock()
s.removePathLocked(primary, kind)
if secondary != "" {
s.removePathLocked(secondary, kind)
}
s.totalActive--
s.dequeueHeadLocked(primary)
if secondary != "" && secondary != primary {
s.dequeueHeadLocked(secondary)
}
s.tryPromoteLocked()
}
// dequeueHeadLocked removes the current head of path p and deletes the map
// entry when the queue becomes empty. Must be called under s.mu.
func (s *mutateScheduler) dequeueHeadLocked(p util.FullPath) {
q := s.pathQueue[p]
if len(q) == 0 {
return
}
// Shift left without reallocating; drop the reference so the waiter can
// be garbage-collected before the tail shrinks.
q[0] = nil
copy(q, q[1:])
q = q[:len(q)-1]
if len(q) == 0 {
delete(s.pathQueue, p)
} else {
s.pathQueue[p] = q
}
}
// tryPromoteLocked admits as many queue heads as possible while respecting
// path-FIFO order, the active-state conflict rules, and concurrencyLimit.
// The admitted set can grow in one call because admitting one waiter frees
// zero or more paths whose new heads may now pass the conflict check.
// Must be called under s.mu.
func (s *mutateScheduler) tryPromoteLocked() {
for s.totalActive < s.concurrencyLimit {
promoted := false
// Walk distinct head waiters across all path queues. Map iteration
// order is randomized, which is fine: path-FIFO is preserved by the
// head-of-queue check inside admitIfHeadLocked, and cross-path order
// is not constrained.
for _, q := range s.pathQueue {
if len(q) == 0 {
continue
}
w := q[0]
if w.admitted {
continue
}
if s.admitIfHeadLocked(w) {
promoted = true
if s.totalActive >= s.concurrencyLimit {
return
}
}
}
if !promoted {
return
}
}
}
// admitIfHeadLocked admits w if w is the head of every path queue it joined
// and pathConflictsLocked passes. Returns true if admitted. Must be called
// under s.mu.
func (s *mutateScheduler) admitIfHeadLocked(w *mutateWaiter) bool {
if s.pathQueue[w.primary][0] != w {
return false
}
if w.secondary != "" && w.secondary != w.primary {
q := s.pathQueue[w.secondary]
if len(q) == 0 || q[0] != w {
return false
}
}
if s.pathConflictsLocked(w.primary, w.kind) {
return false
}
if w.secondary != "" && s.pathConflictsLocked(w.secondary, w.kind) {
return false
}
s.addPathLocked(w.primary, w.kind)
if w.secondary != "" {
s.addPathLocked(w.secondary, w.kind)
}
s.totalActive++
w.admitted = true
close(w.ready)
return true
}
// pathConflictsLocked mirrors MetadataProcessor.pathConflicts exactly.
func (s *mutateScheduler) pathConflictsLocked(p util.FullPath, kind mutateJobKind) bool {
if s.activeBarrierDirPaths[p] > 0 {
return true
}
if kind == kindMutateBarrierDir && s.activeNonBarrierDirPaths[p] > 0 {
return true
}
if s.activeFilePaths[p] > 0 && (kind == kindMutateFile || kind == kindMutateBarrierDir) {
return true
}
if kind == kindMutateBarrierDir && s.descendantCount[p] > 0 {
return true
}
for _, ancestor := range mutatePathAncestors(p) {
if s.activeBarrierDirPaths[ancestor] > 0 {
return true
}
}
return false
}
func (s *mutateScheduler) addPathLocked(p util.FullPath, kind mutateJobKind) {
switch kind {
case kindMutateFile:
s.activeFilePaths[p]++
case kindMutateBarrierDir:
s.activeBarrierDirPaths[p]++
case kindMutateNonBarrierDir:
s.activeNonBarrierDirPaths[p]++
}
for _, ancestor := range mutatePathAncestors(p) {
s.descendantCount[ancestor]++
}
}
func (s *mutateScheduler) removePathLocked(p util.FullPath, kind mutateJobKind) {
switch kind {
case kindMutateFile:
if s.activeFilePaths[p] <= 1 {
delete(s.activeFilePaths, p)
} else {
s.activeFilePaths[p]--
}
case kindMutateBarrierDir:
if s.activeBarrierDirPaths[p] <= 1 {
delete(s.activeBarrierDirPaths, p)
} else {
s.activeBarrierDirPaths[p]--
}
case kindMutateNonBarrierDir:
if s.activeNonBarrierDirPaths[p] <= 1 {
delete(s.activeNonBarrierDirPaths, p)
} else {
s.activeNonBarrierDirPaths[p]--
}
}
for _, ancestor := range mutatePathAncestors(p) {
if s.descendantCount[ancestor] <= 1 {
delete(s.descendantCount, ancestor)
} else {
s.descendantCount[ancestor]--
}
}
}
// mutatePathAncestors mirrors filer_sync_jobs.go:pathAncestors — returns the
// proper ancestor directories of p. For "/a/b/c", returns ["/a/b", "/a", "/"].
func mutatePathAncestors(p util.FullPath) []util.FullPath {
var ancestors []util.FullPath
s := string(p)
for {
parent := path.Dir(s)
if parent == s {
break
}
ancestors = append(ancestors, util.FullPath(parent))
s = parent
}
return ancestors
}
// classifyMutation extracts the admission key(s) and kind for a mutation
// request. It returns primary, secondary (empty unless rename), and kind.
//
// Malformed requests (missing oneof payload, nil Entry, missing rename fields)
// are routed to a barrier at "/" so admission still runs under the full stream
// lock; the handler will then send EINVAL to the client. This keeps the
// scheduler and Recv loop crash-free regardless of client-side validation.
//
// Deletes are always classified as kindMutateBarrierDir because a
// DeleteEntryRequest can target a directory (empty or, with IsRecursive, with
// contents) but does not carry IsDirectory on the wire. Treating every delete
// as a barrier at its target path makes it conflict with an in-flight
// non-barrier directory update on the same path (e.g. chmod), which a
// kindMutateFile classification would miss.
func classifyMutation(req *filer_pb.StreamMutateEntryRequest) (primary, secondary util.FullPath, kind mutateJobKind) {
// Default fallback for any shape we cannot classify safely.
primary = util.FullPath("/")
kind = kindMutateBarrierDir
switch r := req.Request.(type) {
case *filer_pb.StreamMutateEntryRequest_CreateRequest:
cr := r.CreateRequest
if cr == nil || cr.Entry == nil {
return
}
primary = util.FullPath(cr.Directory).Child(cr.Entry.Name)
kind = classifyEntry(cr.Entry.IsDirectory, false)
return
case *filer_pb.StreamMutateEntryRequest_UpdateRequest:
ur := r.UpdateRequest
if ur == nil || ur.Entry == nil {
return
}
primary = util.FullPath(ur.Directory).Child(ur.Entry.Name)
// UpdateEntry never changes the name, so directory updates are always
// in-place attribute updates. File updates (chunk manifests, xattrs)
// are kindMutateFile and thus serialize against same-path file ops.
kind = classifyEntry(ur.Entry.IsDirectory, true)
return
case *filer_pb.StreamMutateEntryRequest_DeleteRequest:
dr := r.DeleteRequest
if dr == nil {
return
}
primary = util.FullPath(dr.Directory).Child(dr.Name)
// Barrier regardless of IsRecursive: the request does not carry the
// target's IsDirectory, and barrier classification correctly blocks
// concurrent non-barrier dir updates at the same path. Descendant
// wait for a non-recursive delete of a non-empty dir is wasted but
// not wrong — that call fails at the store anyway.
kind = kindMutateBarrierDir
return
case *filer_pb.StreamMutateEntryRequest_RenameRequest:
rr := r.RenameRequest
if rr == nil {
return
}
primary = util.FullPath(rr.OldDirectory).Child(rr.OldName)
secondary = util.FullPath(rr.NewDirectory).Child(rr.NewName)
// Renames reshape the namespace on both sides; conservatively treat as
// a subtree barrier so any in-flight descendant drains before the move
// and no new descendant is admitted until it completes.
kind = kindMutateBarrierDir
return
default:
return
}
}
func classifyEntry(isDirectory, isAttributeUpdate bool) mutateJobKind {
if !isDirectory {
return kindMutateFile
}
if isAttributeUpdate {
return kindMutateNonBarrierDir
}
return kindMutateBarrierDir
}