Files
seaweedfs/weed/server/filer_grpc_server_dlm_handoff_test.go
T
5b7e2d9e9e fix: retain released lock version watermarks (#13083)
* 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>
2026-11-28 15:31:06 -08:00

291 lines
11 KiB
Go

package weed_server
import (
"context"
"fmt"
"net"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/cluster/lock_manager"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
)
func newDLMHandoffServer(t *testing.T, interceptor grpc.UnaryServerInterceptor) (*FilerServer, pb.ServerAddress) {
t.Helper()
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
addr := pb.NewServerAddressWithGrpcPort(listener.Addr().String(), listener.Addr().(*net.TCPAddr).Port)
fs := &FilerServer{
filer: &filer.Filer{Dlm: lock_manager.NewDistributedLockManager(addr)},
option: &FilerOption{Host: addr}, grpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()),
}
var options []grpc.ServerOption
if interceptor != nil {
options = append(options, grpc.UnaryInterceptor(interceptor))
}
server := grpc.NewServer(options...)
filer_pb.RegisterSeaweedFilerServer(server, fs)
go server.Serve(listener)
fs.SetupDlmReplication()
t.Cleanup(func() { fs.dlmHandoffCancel(); server.Stop(); pb.InvalidateGrpcConnection(addr.ToGrpcAddress()) })
return fs, addr
}
// Delay a real transfer RPC after its source snapshot is taken, then release
// through the old primary while the request is in flight. Exercise both the
// demotion-to-backup and removal-after-transfer topology branches.
func TestDLM_ReleaseDuringHandoff(t *testing.T) {
for _, keepBackup := range []bool{false, true} {
t.Run(fmt.Sprintf("keep_backup=%v", keepBackup), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
captured := make(chan *filer_pb.TransferLocksRequest, 1)
proceed := make(chan struct{})
var once sync.Once
unblock := func() { once.Do(func() { close(proceed) }) }
defer unblock()
serve := func(delay bool) (*FilerServer, pb.ServerAddress) {
return newDLMHandoffServer(t, func(c context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
if transfer, ok := req.(*filer_pb.TransferLocksRequest); ok && delay {
captured <- transfer
select {
case <-proceed:
case <-c.Done():
return nil, c.Err()
}
}
return handler(c, req)
})
}
source, a := serve(false)
target, b := serve(true)
members := []pb.ServerAddress{a, b}
if !keepBackup {
_, c := serve(false)
members = append(members, c)
}
source.filer.Dlm.LockRing.SetSnapshot([]pb.ServerAddress{a}, 1)
target.filer.Dlm.LockRing.SetSnapshot(members, 2)
key := ""
for i := 0; i < 10000; i++ {
candidate := fmt.Sprintf("handoff-%d", i)
primary, backup := target.filer.Dlm.LockRing.GetPrimaryAndBackup(candidate)
if primary == b && (backup == a) == keepBackup {
key = candidate
break
}
}
require.NotEmpty(t, key)
acquired, err := source.DistributedLock(ctx, &filer_pb.LockRequest{Name: key, SecondsToLock: 60, Owner: "alice"})
require.NoError(t, err)
require.Empty(t, acquired.Error)
require.NotEmpty(t, acquired.RenewToken)
source.filer.Dlm.LockRing.SetTakeSnapshotCallback(source.OnDlmChangeSnapshot)
transferred := make(chan struct{})
go func() { source.filer.Dlm.LockRing.SetSnapshot(members, 2); close(transferred) }()
var request *filer_pb.TransferLocksRequest
select {
case request = <-captured:
case <-ctx.Done():
t.Fatal("handoff did not start")
}
type unlockResult struct {
response *filer_pb.UnlockResponse
err error
}
released := make(chan unlockResult, 1)
go func() {
response, err := source.DistributedUnlock(ctx, &filer_pb.UnlockRequest{Name: key, RenewToken: acquired.RenewToken})
released <- unlockResult{response, err}
}()
// Same-key admission ordering is established with a contention hook
// in TestDLM_UnlockWaitsForHandoffAdmission. Here test the real RPCs.
unblock()
select {
case <-transferred:
case <-ctx.Done():
t.Fatal("handoff did not complete")
}
var result unlockResult
select {
case result = <-released:
case <-ctx.Done():
t.Fatal("release did not complete after handoff")
}
require.NoError(t, result.err)
require.NotNil(t, result.response)
require.Empty(t, result.response.Error)
_, found := target.filer.Dlm.GetLock(key)
require.False(t, found, "completed release was undone by the delayed transfer")
// Replay the captured transfer after the release has completed.
_, err = target.TransferLocks(ctx, request)
require.NoError(t, err)
_, found = target.filer.Dlm.GetLock(key)
require.False(t, found)
next, err := target.DistributedLock(ctx, &filer_pb.LockRequest{Name: key, SecondsToLock: 60, Owner: "bob"})
require.NoError(t, err)
require.Empty(t, next.Error)
require.NotEmpty(t, next.RenewToken)
})
}
}
func TestDLM_ReleaseBeforeHandoffReachesPromotedBackup(t *testing.T) {
source, a := newDLMHandoffServer(t, nil)
target, b := newDLMHandoffServer(t, nil)
backup, c := newDLMHandoffServer(t, nil)
initial, next := []pb.ServerAddress{a, b}, []pb.ServerAddress{b, c}
source.filer.Dlm.LockRing.SetSnapshot(initial, 1)
target.filer.Dlm.LockRing.SetSnapshot(initial, 1)
nextRing := lock_manager.NewHashRing(lock_manager.DefaultVnodeCount)
nextRing.SetServers(next)
key := ""
for i := 0; i < 10000; i++ {
candidate := fmt.Sprintf("pre-handoff-%d", i)
if source.filer.Dlm.LockRing.GetPrimary(candidate) == a && nextRing.GetPrimary(candidate) == b {
key = candidate
break
}
}
require.NotEmpty(t, key)
ctx := context.Background()
lease, err := source.DistributedLock(ctx, &filer_pb.LockRequest{Name: key, Owner: "alice", SecondsToLock: 60})
require.NoError(t, err)
require.Empty(t, lease.Error)
require.Eventually(t, func() bool { _, ok := target.filer.Dlm.GetLock(key); return ok }, 5*time.Second, time.Millisecond)
backup.filer.Dlm.LockRing.SetSnapshot(next, 2)
target.filer.Dlm.LockRing.SetTakeSnapshotCallback(target.OnDlmChangeSnapshot)
target.filer.Dlm.LockRing.SetSnapshot(next, 2)
// Move routing before the source's topology callback starts: unlock wins
// admission while B already has the old lease and C is its new backup.
source.filer.Dlm.LockRing.SetSnapshot(next, 2)
response, err := source.DistributedUnlock(ctx, &filer_pb.UnlockRequest{Name: key, RenewToken: lease.RenewToken})
require.NoError(t, err)
require.Empty(t, response.Error)
source.OnDlmChangeSnapshot(next)
_, found := target.filer.Dlm.GetLock(key)
require.False(t, found)
acquired, err := target.DistributedLock(ctx, &filer_pb.LockRequest{Name: key, Owner: "bob", SecondsToLock: 60})
require.NoError(t, err)
require.Empty(t, acquired.Error)
require.NotEmpty(t, acquired.RenewToken)
}
func TestDLM_HandoffRetriesWithoutMembershipChange(t *testing.T) {
for _, installed := range []bool{false, true} {
t.Run(fmt.Sprintf("installed_before_error=%v", installed), func(t *testing.T) {
source, a := newDLMHandoffServer(t, nil)
var attempts atomic.Int32
target, b := newDLMHandoffServer(t, func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
if _, transfer := req.(*filer_pb.TransferLocksRequest); transfer && attempts.Add(1) == 1 {
if installed {
if _, err := handler(ctx, req); err != nil {
return nil, err
}
}
return nil, status.Error(codes.Unavailable, "injected handoff failure")
}
return handler(ctx, req)
})
next := []pb.ServerAddress{a, b}
source.filer.Dlm.LockRing.SetSnapshot([]pb.ServerAddress{a}, 1)
target.filer.Dlm.LockRing.SetSnapshot(next, 2)
key := ""
for i := 0; i < 10000; i++ {
candidate := fmt.Sprintf("retry-%d", i)
if target.filer.Dlm.LockRing.GetPrimary(candidate) == b {
key = candidate
break
}
}
require.NotEmpty(t, key)
ctx := context.Background()
lease, err := source.DistributedLock(ctx, &filer_pb.LockRequest{Name: key, Owner: "alice", SecondsToLock: 60})
require.NoError(t, err)
require.Empty(t, lease.Error)
source.filer.Dlm.LockRing.SetTakeSnapshotCallback(source.OnDlmChangeSnapshot)
source.filer.Dlm.LockRing.SetSnapshot(next, 2)
require.Eventually(t, func() bool {
lock, ok := target.filer.Dlm.GetLock(key)
return attempts.Load() >= 2 && ok && lock.Token == lease.RenewToken
}, 5*time.Second, time.Millisecond)
require.EqualValues(t, 2, source.filer.Dlm.LockRing.Version())
renewed, err := source.DistributedLock(ctx, &filer_pb.LockRequest{Name: key, Owner: "alice", RenewToken: lease.RenewToken, SecondsToLock: 60})
require.NoError(t, err)
require.Empty(t, renewed.Error)
require.Equal(t, string(b), renewed.LockHostMovedTo, "renewal authority stays at the new primary")
require.NotEqual(t, lease.RenewToken, renewed.RenewToken)
})
}
}
func TestDLM_PendingHandoffKeepsRenewToken(t *testing.T) {
source, a := newDLMHandoffServer(t, nil)
var failing atomic.Bool
failing.Store(true)
target, b := newDLMHandoffServer(t, func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
if _, transfer := req.(*filer_pb.TransferLocksRequest); transfer && failing.Load() {
return nil, status.Error(codes.Unavailable, "injected handoff failure")
}
return handler(ctx, req)
})
source.filer.Dlm.LockRing.SetSnapshot([]pb.ServerAddress{a}, 1)
target.filer.Dlm.LockRing.SetSnapshot([]pb.ServerAddress{b}, 2)
ctx := context.Background()
lease, err := source.DistributedLock(ctx, &filer_pb.LockRequest{Name: "key", Owner: "alice", SecondsToLock: 60})
require.NoError(t, err)
require.Empty(t, lease.Error)
source.filer.Dlm.LockRing.SetSnapshot([]pb.ServerAddress{b}, 2)
pending, err := source.DistributedLock(ctx, &filer_pb.LockRequest{Name: "key", Owner: "alice", RenewToken: lease.RenewToken, SecondsToLock: 60})
require.NoError(t, err)
require.Contains(t, pending.Error, lock_manager.LockHandoffPending.Error())
require.Equal(t, lease.RenewToken, pending.RenewToken, "a failed handoff is not a rejection")
failing.Store(false)
renewed, err := source.DistributedLock(ctx, &filer_pb.LockRequest{Name: "key", Owner: "alice", RenewToken: pending.RenewToken, SecondsToLock: 60})
require.NoError(t, err)
require.Empty(t, renewed.Error)
require.Equal(t, string(b), renewed.LockHostMovedTo)
require.NotEqual(t, lease.RenewToken, renewed.RenewToken)
}
func TestDLM_HandoffRetryStops(t *testing.T) {
for _, shutdown := range []bool{false, true} {
t.Run(fmt.Sprintf("shutdown=%v", shutdown), func(t *testing.T) {
fs, addr := newDLMHandoffServer(t, nil)
fs.filer.Dlm.LockRing.SetSnapshot([]pb.ServerAddress{addr}, 1)
expiry := time.Now().Add(time.Second).UnixNano()
if shutdown {
expiry = time.Now().Add(time.Minute).UnixNano()
}
_, _, _, _, err := fs.filer.Dlm.LockWithTimeout("key", expiry, "", "owner")
require.NoError(t, err)
fs.filer.Dlm.LockRing.SetSnapshot([]pb.ServerAddress{"target:8888"}, 2)
fs.filer.Dlm.TransferFn = func(pb.ServerAddress, *lock_manager.Lock) error {
return status.Error(codes.Unavailable, "target unavailable")
}
require.Error(t, fs.filer.Dlm.TransferLockToOwner("key"))
fs.retryDlmHandoff("key")
if shutdown {
fs.dlmHandoffCancel()
}
require.Eventually(t, func() bool {
fs.dlmHandoffMu.Lock()
defer fs.dlmHandoffMu.Unlock()
return len(fs.dlmHandoffRetries) == 0
}, 5*time.Second, time.Millisecond)
})
}
}