mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
* filer/posixlock: key lock owners and leases by principal A lock's owner becomes (Principal, Sid, Owner). The principal is server-side state, empty for unscoped callers, so owners with equal client-chosen sid and owner under different principals conflict and never match on unlock, release, reassertion or coalescing. The session index and leases key on (Principal, Sid). Scoped sessions are leased per key from their first acquisition: the lease ends at the earlier of TTL after the last acquisition or renewal on that key and Lease.NotAfter. Expired scoped locks are dropped before any operation on their key as well as by ReapExpired. Unscoped sessions keep their current whole-session lease, started by the first keepalive. The handler passes the unscoped session for now, so behaviour is unchanged. NewManagerWithClock lets tests drive lease boundaries. * filer: bind scoped POSIX locks to the verified token id PosixLock trusted the client-supplied sid. GET_LK reports a conflicting lock's sid and owner, so a scoped caller with a write grant on the same path could reuse them to unlock or release another holder's lock, keep it alive, or replace it through a KEEP_ALIVE reassertion. A tokenless request with the same identity could do the same. A request admitted for a scoped token now acts for that token's verified jti, derived in posixLockAuthority from the interceptor's authority. It controls only the locks it took, whatever sid or owner it names. Its lease on each key ends at the earlier of the 15s TTL after its last acquisition or renewal and the token's expiry plus the clock leeway. Each renewal passes admission for its key, so a narrower token cannot extend keys outside its grant. Tokenless and unscoped calls keep the unscoped namespace and behaviour, and can no longer match a scoped lock. Scoped TRY_LOCK and reassertion ranges must be well-formed read or write ranges, checked before any state changes. GET_LK, the protobuf and forwarding are unchanged; the principal is never returned or logged. A renewed token with a new jti is a new owner: the old locks expire and are reacquired. * filer/posixlock: release an expired session once, however many ranges it holds * posix locks: report a lock lost on re-assertion to the mount A mount whose lease lapsed, for example when its scoped token expired or was replaced by one with a new id, kept re-asserting locks that another session had since taken. The filer logged the loss but returned success, and the mount discarded the reply, so nothing on the mount side showed it. KEEP_ALIVE now returns the first conflicting lock in the existing has_conflict and conflict fields, which older mounts ignore. The mount warns once per key when it learns of a loss and forgets it once a re-assertion succeeds or the key is no longer held. Writes are not fenced. * filer/posixlock: drop every expired session's ranges in one pass * filer/posixlock: renew a scoped session's same-grant leases together Scoped leases expire per key and a keepalive renewed only the key it named. A mount renews its keys one keepalive at a time, so with many keys and slow keepalives a round could outlast the 15s lease and a healthy mount lost locks; unscoped sessions never had this problem because any keepalive renews the whole session. Each scoped lease now records a fingerprint of the verified grants it was acquired or re-asserted under: a SHA-256 over the sorted, deduplicated (access level, path) entries. A keepalive, bare or re-assertion, extends every live lease of the same session (principal and sid) held under the same fingerprint, each to the request's lease (the earlier of the TTL and the token's expiry plus leeway). Leases taken under other grants, such as a narrower token reusing the jti, are still renewed only by name, and a lease that already ended is not revived before the sweep drops it. No extra filer lookups; unscoped sessions are unchanged. * filer/posixlock: bound same-grant renewal and refuse ended-lease grants A scoped keepalive scanned every key of its session to renew those held under the same grant, so a mount renewing N keys one by one did N^2 lease visits per round under the manager's mutex. The scan now runs at most once per quarter lease TTL per grant; every key under the grant still ends no earlier than three quarters of a TTL after the latest renewal, and the named key is always renewed in full. TryLock now refuses a scoped grant whose lease has already ended, such as a request admitted just before its token expired that then waited on the manager, instead of granting a lock the next operation would drop. The filer returns Unauthenticated, which the mount reports as EIO. * filer/posixlock: skip same-grant scans by deadline floor, not by time The scan throttle remembered when a grant was last renewed, not how long that renewal made its keys last. A replacement token with the same id and grants but a later expiry could then renew one key while the others kept ending at the old token's expiry. Each session now keeps, per grant, the deadline the last scan gave every live key under it, lowered whenever a key is leased for less. A renewal skips the scan only while that floor already reaches the earlier of its own deadline and three quarters of a TTL from now, so a mount renewing keys one by one still costs linear work. A keepalive from a session holding no leases records nothing, and a session's floors go with its last lease, so lock-less keepalives with changing session ids no longer accumulate state the sweeper never sees. * filer/posixlock: never move a scoped lease's deadline earlier Renew, a re-assertion, a same-grant scan and a re-lock all set the key's deadline to the request's own bound. A request under the old token of a principal that already renews under a replacement (same id and grants, later expiry) can land after the replacement's renewal, for example one of the 32 concurrent keepalives in flight when the mount switched credentials. It then cut the key back to the old token's expiry, and the lock was dropped before the next round although the replacement had renewed it. Keep the later of the current and the requested deadline: every deadline still comes from an admitted request of the same principal, so a lease never outlives the latest of its tokens. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/posixlock: keep each session's index and leases in one map bySession, lastSeen, leases and floors were four maps keyed by Session that had to be created and pruned together. They become one sessions map whose sessionState holds the session's keys (with each key's lease), its last keepalive and its grant floors, so a session's state is dropped in one place. The release loop shared by ReapExpired and ReleaseSession becomes releaseOn. ReleaseSession now also forgets an unscoped session's last keepalive, so a session it released is not reported again by the next sweep. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
3.1 KiB
Go
83 lines
3.1 KiB
Go
package posixlock
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A mount's tracked locks round-trip through Snapshot back to a fresh owner via
|
|
// Reassert — the owner-restart / ring-change recovery path.
|
|
func TestReassertRebuildsOnFreshOwner(t *testing.T) {
|
|
const sid = uint64(7)
|
|
|
|
// Client mirror: two granted locks on one key, one on another.
|
|
client := NewManager()
|
|
client.Track("a", Range{Start: 0, End: 99, Type: Write, Sid: sid, Owner: 1})
|
|
client.Track("a", Range{Start: 200, End: 299, Type: Read, Sid: sid, Owner: 2})
|
|
client.Track("b", Range{Start: 0, End: maxEnd, Type: Write, Sid: sid, Owner: 1, IsFlock: true})
|
|
|
|
// Fresh owner (post-restart / new ring owner) knows nothing.
|
|
owner := NewManager()
|
|
for key, locks := range client.Snapshot() {
|
|
if c := owner.Reassert(key, Session{Sid: sid}, locks, Lease{}); c != nil {
|
|
t.Fatalf("unexpected conflict reasserting %s: %+v", key, c)
|
|
}
|
|
}
|
|
|
|
// The owner now reports the same conflicts a foreign session would hit.
|
|
if _, granted, _ := owner.TryLock("a", Range{Start: 50, End: 60, Type: Write, Sid: 99, Owner: 1}, Lease{}); granted {
|
|
t.Fatal("owner should block a foreign write after rebuild")
|
|
}
|
|
if _, granted, _ := owner.TryLock("b", Range{Start: 0, End: 0, Type: Read, Sid: 99, Owner: 1, IsFlock: true}, Lease{}); granted {
|
|
t.Fatal("owner should block a foreign flock read after rebuild")
|
|
}
|
|
}
|
|
|
|
// Re-asserting every tick is idempotent: the owner's view is unchanged.
|
|
func TestReassertIdempotent(t *testing.T) {
|
|
const sid = uint64(1)
|
|
m := NewManager()
|
|
m.TryLock("k", Range{Start: 0, End: 99, Type: Write, Sid: sid, Owner: 1}, Lease{})
|
|
before := append([]Range(nil), m.byKey["k"].locks...)
|
|
|
|
m.Reassert("k", Session{Sid: sid}, before, Lease{})
|
|
m.Reassert("k", Session{Sid: sid}, before, Lease{})
|
|
|
|
if !reflect.DeepEqual(m.byKey["k"].locks, before) {
|
|
t.Fatalf("reassert not idempotent:\n got %+v\nwant %+v", m.byKey["k"].locks, before)
|
|
}
|
|
}
|
|
|
|
// A lock another session grabbed in the migration window is reported as a
|
|
// conflict and not double-granted.
|
|
func TestReassertReportsConflict(t *testing.T) {
|
|
const mine, other = uint64(1), uint64(2)
|
|
m := NewManager()
|
|
// Another mount took the lock on this (new) owner during the gap.
|
|
m.TryLock("k", Range{Start: 0, End: 99, Type: Write, Sid: other, Owner: 1}, Lease{})
|
|
|
|
conflicts := m.Reassert("k", Session{Sid: mine}, []Range{{Start: 0, End: 99, Type: Write, Sid: mine, Owner: 1}}, Lease{})
|
|
if len(conflicts) != 1 {
|
|
t.Fatalf("expected 1 conflict, got %d: %+v", len(conflicts), conflicts)
|
|
}
|
|
// The other session keeps the lock; mine was not installed.
|
|
if got := len(m.byKey["k"].locks); got != 1 {
|
|
t.Fatalf("expected only the incumbent lock, got %d", got)
|
|
}
|
|
}
|
|
|
|
// Reassert renews the lease, so a re-asserting mount is not reaped.
|
|
func TestReassertRenewsLease(t *testing.T) {
|
|
const sid = uint64(1)
|
|
m := NewManager()
|
|
m.Renew("", Session{Sid: sid}, Lease{})
|
|
m.Reassert("k", Session{Sid: sid}, []Range{{Start: 0, End: 9, Type: Write, Sid: sid, Owner: 1}}, Lease{})
|
|
|
|
if reaped := m.ReapExpired(time.Hour); len(reaped) != 0 {
|
|
t.Fatalf("freshly re-asserted session should not be reaped: %v", reaped)
|
|
}
|
|
}
|
|
|
|
const maxEnd = ^uint64(0)
|