mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
* fix: retain released lock version watermarks * fix(lock_manager): scope release records to lease identities Keep a stable acquisition ID in opaque renewal tokens so release history cannot reject a valid lease from a different primary when generation counters overlap. Preserve earlier releases across new acquisitions and ownership eviction. Carry the maximum lease expiry with releases, prevent renewals from shortening that bound, reject expired inserts, and reclaim release records after expiry. Track replication and handoff before sending copies so local-only leases need no release records. Add regressions for fresh-primary failover, equal and lower generation counters, reordered releases and renewals, bounded retention, local-only workloads, ownership eviction, legacy release metadata, and gRPC release handling. * lock_manager: name the release replay tests after the behavior they check Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(lock_manager): serialize lease handoff with local mutations Hold per-key mutation admission through a fresh lease snapshot, the transfer RPC, and source demotion or removal. A release that wins first suppresses the transfer; a release arriving during handoff waits and is forwarded to the new primary afterward. Keep incoming replication outside the gate to avoid callback cycles. Retain source authority on transfer errors and apply transfer completion only to the lease that was sent. Add real gRPC regressions for releases during both handoff branches and replay after release, plus coverage for fresh snapshots, transfer errors, unrelated keys, and replacement leases. * fix(lock_manager): retry handoffs without retaining renewal authority Deliver a source-held lease to the current owner before forwarding renewal or unlock. Freeze local authority before sending a transfer, including when the receiver may have committed despite an RPC error. Retry without requiring a membership change, with backoff and shutdown cancellation. Recheck ownership after a background handoff and preserve the copy until the latest owner receives it. Replicate the effective installed expiry instead of a shorter transfer input. Replace timing assumptions with a same-key admission contention hook. Cover pre-handoff releases, lost acknowledgements, retry cleanup, owner changes, and effective expiry. Document why identityless legacy releases cannot safely delete slash-format leases with overlapping generation counters. * test(filer): set up DLM handoff before the peer lock transfer check Ring-change transfers now go through Dlm.TransferFn, which SetupDlmReplication installs; without it the lock stays pending on the old filer and the peer-auth transfer test cannot see it move. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(lock_manager): bound the handoff and unlock completion waits Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(filer): give the handoff retry expiry subtest a one-second lease Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(lock_manager): serve lock and unlock here when a handoff ends with this node as primary transferToOwnerLocked now returns the primary it observed after the handoff. LockWithTimeout and Unlock redirected to the primary read before the transfer, so a ring that moved back during the handoff sent the client away from the node that holds the lease. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(lock_manager): bound the admission probes in the handoff tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(filer): keep the holder's renew token while its lock handoff is pending A failed transfer RPC or a frozen lease answered the lock request with an error and an empty RenewToken. The client treats that as a rejection and drops its token, so it can no longer renew the lease once the handoff completes. Report transfer failures as LockHandoffPending and echo the caller's token for that error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(lock_manager): return lease expiry from Lock, Unlock and InsertLock The DLM was the only production caller of these methods and used private twins (lockWithExpiry, unlock, insertLock) to get the effective expiry. Return it from the public methods instead and drop the twins. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(lock_manager): share the copy acceptance checks between InsertLock and InsertBackupLock Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(lock_manager): drive handoff tests through TransferLockToOwner TransferLock and its helpers were only reachable from tests. Remove them so the tests exercise the production handoff path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(lock_manager): share the routing and handoff prologue of LockWithTimeout and Unlock Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Master <Master@node0.8m510.lpad-pg0.utah.cloudlab.us> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
254 lines
9.2 KiB
Go
254 lines
9.2 KiB
Go
package lock_manager
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestDLM_TransferLockUsesFreshSnapshot(t *testing.T) {
|
|
dlm := NewDistributedLockManager("handoff:8888")
|
|
dlm.LockRing.SetSnapshot([]pb.ServerAddress{dlm.Host}, 1)
|
|
expires := time.Now().Add(time.Minute).UnixNano()
|
|
_, original, _, _, err := dlm.LockWithTimeout("key", expires, "", "owner")
|
|
require.NoError(t, err)
|
|
// A topology callback may have enumerated this key before its renewal.
|
|
_, renewed, _, _, err := dlm.LockWithTimeout("key", expires, original, "owner")
|
|
require.NoError(t, err)
|
|
require.NoError(t, handOff(t, dlm, "key", true, func(current *Lock) error {
|
|
require.Equal(t, renewed, current.Token)
|
|
require.EqualValues(t, 2, current.Seq)
|
|
// The receiver can replicate the lease back before acknowledging.
|
|
within(t, "incoming replication during the handoff", func() {
|
|
dlm.InsertBackupLock(current.Key, current.ExpiredAtNs, current.Token, current.PrevToken, current.Owner, current.Generation, current.Seq)
|
|
})
|
|
return nil
|
|
}))
|
|
current, found := dlm.GetLock("key")
|
|
require.True(t, found)
|
|
require.True(t, current.IsBackup)
|
|
require.Equal(t, renewed, current.Token)
|
|
}
|
|
|
|
func TestDLM_TransferLockFailureFreezesAuthority(t *testing.T) {
|
|
dlm := NewDistributedLockManager("handoff:8888")
|
|
dlm.LockRing.SetSnapshot([]pb.ServerAddress{dlm.Host}, 1)
|
|
expires := time.Now().Add(time.Minute).UnixNano()
|
|
_, token, _, _, err := dlm.LockWithTimeout("key", expires, "", "owner")
|
|
require.NoError(t, err)
|
|
failed := errors.New("handoff failed")
|
|
target := NewDistributedLockManager("target:8888")
|
|
target.LockRing.SetSnapshot([]pb.ServerAddress{target.Host}, 1)
|
|
require.ErrorIs(t, handOff(t, dlm, "key", false, func(current *Lock) error {
|
|
// The receiver committed, but its acknowledgement was lost.
|
|
target.InsertLock(current.Key, current.ExpiredAtNs, current.Token, current.PrevToken, current.Owner, current.Generation, current.Seq)
|
|
// A stalled handoff must not hold a global mutex across its RPC.
|
|
var err error
|
|
within(t, "another key during the stalled handoff", func() {
|
|
_, _, _, _, err = dlm.LockWithTimeout("other-key", expires, "", "other-owner")
|
|
})
|
|
require.NoError(t, err)
|
|
return failed
|
|
}), failed)
|
|
// Back on the old ring, the frozen local copy itself must refuse.
|
|
dlm.LockRing.SetSnapshot([]pb.ServerAddress{dlm.Host}, dlm.LockRing.Version()+1)
|
|
current, found := dlm.GetLock("key")
|
|
require.True(t, found)
|
|
require.True(t, current.IsBackup)
|
|
_, renewed, _, _, err := dlm.LockWithTimeout("key", expires, token, "owner")
|
|
require.ErrorIs(t, err, LockHandoffPending)
|
|
require.Empty(t, renewed, "an ambiguous handoff cannot retain renewal authority")
|
|
_, renewed, _, _, err = target.LockWithTimeout("key", expires, token, "owner")
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, renewed)
|
|
require.NotEqual(t, token, renewed)
|
|
_, err = dlm.Unlock("key", token)
|
|
require.ErrorIs(t, err, LockHandoffPending)
|
|
}
|
|
|
|
func TestDLM_TransferCompletionPreservesDifferentLease(t *testing.T) {
|
|
for _, keepBackup := range []bool{false, true} {
|
|
t.Run(fmt.Sprint(keepBackup), func(t *testing.T) {
|
|
dlm := NewDistributedLockManager("handoff:8888")
|
|
expires := time.Now().Add(time.Minute).UnixNano()
|
|
dlm.InsertLock("key", expires, "original/secret", "", "owner", 1, 1)
|
|
require.NoError(t, handOff(t, dlm, "key", keepBackup, func(*Lock) error {
|
|
// Incoming replication is independent of client admission.
|
|
dlm.InsertLock("key", expires, "replacement/secret", "", "new-owner", 2, 1)
|
|
return nil
|
|
}))
|
|
current, found := dlm.GetLock("key")
|
|
require.True(t, found)
|
|
require.Equal(t, "replacement/secret", current.Token)
|
|
require.False(t, current.IsBackup, "completion must not demote a different lease")
|
|
})
|
|
}
|
|
}
|
|
|
|
// Confirm actual same-key admission contention, rather than inferring it from
|
|
// a goroutine start or a sleep in the RPC test.
|
|
func TestDLM_UnlockWaitsForHandoffAdmission(t *testing.T) {
|
|
source := NewDistributedLockManager("source:8888")
|
|
target := NewDistributedLockManager("target:8888")
|
|
source.LockRing.SetSnapshot([]pb.ServerAddress{source.Host}, 1)
|
|
expires := time.Now().Add(time.Minute).UnixNano()
|
|
_, token, _, _, err := source.LockWithTimeout("key", expires, "", "owner")
|
|
require.NoError(t, err)
|
|
source.LockRing.SetSnapshot([]pb.ServerAddress{target.Host}, 2)
|
|
entered, proceed := make(chan struct{}), make(chan struct{})
|
|
waiting := make(chan struct{})
|
|
source.onMutationWait = func(key string) {
|
|
if key == "key" {
|
|
close(waiting)
|
|
}
|
|
}
|
|
var unblockOnce sync.Once
|
|
unblock := func() { unblockOnce.Do(func() { close(proceed) }) }
|
|
defer unblock()
|
|
source.TransferFn = func(_ pb.ServerAddress, lock *Lock) error {
|
|
close(entered)
|
|
<-proceed
|
|
target.InsertLock(lock.Key, lock.ExpiredAtNs, lock.Token, lock.PrevToken, lock.Owner, lock.Generation, lock.Seq)
|
|
return nil
|
|
}
|
|
transferred := make(chan error, 1)
|
|
go func() { transferred <- source.TransferLockToOwner("key") }()
|
|
select {
|
|
case <-entered:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("handoff did not start")
|
|
}
|
|
released := make(chan pb.ServerAddress, 1)
|
|
go func() { moved, _ := source.Unlock("key", token); released <- moved }()
|
|
select {
|
|
case <-waiting:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("unlock did not reach contended admission")
|
|
}
|
|
select {
|
|
case <-released:
|
|
t.Fatal("unlock bypassed the handoff gate")
|
|
default:
|
|
}
|
|
unblock()
|
|
select {
|
|
case err := <-transferred:
|
|
require.NoError(t, err)
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("handoff did not finish")
|
|
}
|
|
select {
|
|
case moved := <-released:
|
|
require.Equal(t, target.Host, moved)
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("unlock did not finish after the handoff")
|
|
}
|
|
}
|
|
|
|
func TestDLM_HandoffCompletionRechecksOwner(t *testing.T) {
|
|
for _, next := range []pb.ServerAddress{"source:8888", "third:8888"} {
|
|
t.Run(string(next), func(t *testing.T) {
|
|
source := NewDistributedLockManager("source:8888")
|
|
source.LockRing.SetSnapshot([]pb.ServerAddress{source.Host}, 1)
|
|
_, _, _, _, err := source.LockWithTimeout("key", time.Now().Add(time.Minute).UnixNano(), "", "owner")
|
|
require.NoError(t, err)
|
|
source.LockRing.SetSnapshot([]pb.ServerAddress{"target:8888"}, 2)
|
|
var destination pb.ServerAddress
|
|
var retries int
|
|
source.RetryTransferFn = func(key string) { require.Equal(t, "key", key); retries++ }
|
|
source.TransferFn = func(to pb.ServerAddress, _ *Lock) error {
|
|
destination = to
|
|
source.LockRing.SetSnapshot([]pb.ServerAddress{next}, 3)
|
|
return nil
|
|
}
|
|
err = source.TransferLockToOwner("key")
|
|
current, found := source.GetLock("key")
|
|
require.True(t, found, "an old handoff cannot discard the newest owner's lease")
|
|
if next == source.Host {
|
|
require.NoError(t, err)
|
|
require.False(t, current.IsBackup)
|
|
} else {
|
|
require.ErrorIs(t, err, LockHandoffPending)
|
|
require.Equal(t, 1, retries)
|
|
require.NoError(t, source.TransferLockToOwner("key"))
|
|
require.Equal(t, next, destination)
|
|
_, found = source.GetLock("key")
|
|
require.False(t, found)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDLM_ServesLocallyWhenHandoffEndsHere(t *testing.T) {
|
|
source := NewDistributedLockManager("source:8888")
|
|
source.LockRing.SetSnapshot([]pb.ServerAddress{source.Host}, 1)
|
|
expires := time.Now().Add(time.Minute).UnixNano()
|
|
_, token, _, _, err := source.LockWithTimeout("key", expires, "", "owner")
|
|
require.NoError(t, err)
|
|
version := int64(1)
|
|
source.TransferFn = func(pb.ServerAddress, *Lock) error {
|
|
version++
|
|
source.LockRing.SetSnapshot([]pb.ServerAddress{source.Host}, version)
|
|
return nil
|
|
}
|
|
moveAway := func() {
|
|
version++
|
|
source.LockRing.SetSnapshot([]pb.ServerAddress{"target:8888"}, version)
|
|
}
|
|
moveAway()
|
|
_, renewed, _, movedTo, err := source.LockWithTimeout("key", expires, token, "owner")
|
|
require.NoError(t, err)
|
|
require.Empty(t, movedTo, "the ring moved back here during the handoff")
|
|
require.NotEqual(t, token, renewed)
|
|
moveAway()
|
|
movedTo, err = source.Unlock("key", renewed)
|
|
require.NoError(t, err)
|
|
require.Empty(t, movedTo)
|
|
_, found := source.GetLock("key")
|
|
require.False(t, found)
|
|
}
|
|
|
|
// handOff moves key away from dlm and sends it through transfer. With
|
|
// keepBackup, dlm stays in the ring as the key's backup.
|
|
func handOff(t *testing.T, dlm *DistributedLockManager, key string, keepBackup bool, transfer func(*Lock) error) error {
|
|
t.Helper()
|
|
moveAway(t, dlm, key, keepBackup)
|
|
dlm.TransferFn = func(_ pb.ServerAddress, lock *Lock) error { return transfer(lock) }
|
|
return dlm.TransferLockToOwner(key)
|
|
}
|
|
|
|
func moveAway(t *testing.T, dlm *DistributedLockManager, key string, keepBackup bool) {
|
|
t.Helper()
|
|
for i := 0; i < 100; i++ {
|
|
servers := []pb.ServerAddress{pb.ServerAddress(fmt.Sprintf("target-%d:8888", i))}
|
|
if keepBackup {
|
|
servers = append(servers, dlm.Host)
|
|
}
|
|
dlm.LockRing.SetSnapshot(servers, dlm.LockRing.Version()+1)
|
|
if primary, backup := dlm.LockRing.GetPrimaryAndBackup(key); primary != dlm.Host && (backup == dlm.Host) == keepBackup {
|
|
return
|
|
}
|
|
}
|
|
t.Fatalf("no ring moves %s away from %s", key, dlm.Host)
|
|
}
|
|
|
|
// within fails the test if fn has not returned after five seconds.
|
|
func within(t *testing.T, what string, fn func()) {
|
|
t.Helper()
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
fn()
|
|
}()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatalf("%s did not finish", what)
|
|
}
|
|
}
|