mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-09-20 13:30:46 +02:00
* wdclient: bound the wait for a master leader by the caller's context WithClient waited on GetMaster with context.Background(), so a caller that arrived while no master leader was known parked in a 200ms poll loop until one appeared, whatever deadline it had already set on the RPC. Each retry above it then left another goroutine in the same wait. Take the context in WithClient and WithClientCustomGetMaster and hand it to GetMaster, and stop the retry loop once it is done. The dial keeps context.Background(): fn brings its own RPC context, so a cancellation seen here cannot be attributed to the shared connection. Call sites pass whatever they hold: the request context in the filer's CollectionList, DeleteCollection and Statistics handlers and in the credential store's propagation, the operation context in the shell's s3.bucket.delete and the kafka gateway's broker and filer discovery, and context.Background() where there is none - the shell commands, the admin dashboard wrapper, and the exclusive locker's initial lease. The locker's release keeps its own uncancelled context so a slow unlock cannot turn into a ghost lock. Claude-Session: https://claude.ai/code/session_01BjDWtZsCoZY6x4pdDmGWxU * wdclient: test that WithClient gives up with the caller's context Claude-Session: https://claude.ai/code/session_01BjDWtZsCoZY6x4pdDmGWxU * wdclient: cut the master retry backoff short when the caller gives up util.Retry sleeps unconditionally between attempts, so a transient error arriving just before the caller's deadline still cost it a full backoff step. Use the context-aware util.RetryWithBackoff, the same helper the volume lookup in this file already uses. Two call sites went with it: the shell's lock-holder lookup builds its three second bound before WithClient so it also covers finding the leader, as its comment already promised, and the filer's post-delete collection cleanup goes back to an uncancelled context - the entry is already gone, so a caller that hung up must not leave the collection behind. Claude-Session: https://claude.ai/code/session_01BjDWtZsCoZY6x4pdDmGWxU * wdclient: test that a cancel during backoff ends the retry Claude-Session: https://claude.ai/code/session_01BjDWtZsCoZY6x4pdDmGWxU
172 lines
4.9 KiB
Go
172 lines
4.9 KiB
Go
package exclusive_locks
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
|
)
|
|
|
|
const (
|
|
RenewInterval = 4 * time.Second
|
|
SafeRenewInterval = 3 * time.Second
|
|
InitLockInterval = 1 * time.Second
|
|
// bounds each lease and renew RPC attempt so an unresponsive master
|
|
// cannot hold l.mu (and block ReleaseLock) indefinitely
|
|
rpcTimeout = 3 * time.Second
|
|
)
|
|
|
|
type ExclusiveLocker struct {
|
|
token int64
|
|
lockTsNs int64
|
|
isLocked atomic.Bool
|
|
masterClient *wdclient.MasterClient
|
|
lockName string
|
|
message string
|
|
clientName string
|
|
// serializes renew and release RPCs: a renewal in flight during a release
|
|
// would re-create the lock on the master and leave it held until expiry
|
|
mu sync.Mutex
|
|
// Each lock has and only has one goroutine
|
|
renewGoroutineRunning atomic.Bool
|
|
}
|
|
|
|
func NewExclusiveLocker(masterClient *wdclient.MasterClient, lockName string) *ExclusiveLocker {
|
|
return &ExclusiveLocker{
|
|
masterClient: masterClient,
|
|
lockName: lockName,
|
|
}
|
|
}
|
|
|
|
func (l *ExclusiveLocker) IsLocked() bool {
|
|
return l.isLocked.Load()
|
|
}
|
|
|
|
func (l *ExclusiveLocker) GetToken() (token int64, lockTsNs int64) {
|
|
for time.Unix(0, atomic.LoadInt64(&l.lockTsNs)).Add(SafeRenewInterval).Before(time.Now()) {
|
|
// wait until now is within the safe lock period, no immediate renewal to change the token
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
return atomic.LoadInt64(&l.token), atomic.LoadInt64(&l.lockTsNs)
|
|
}
|
|
|
|
func (l *ExclusiveLocker) RequestLock(clientName string) {
|
|
if l.isLocked.Load() {
|
|
return
|
|
}
|
|
|
|
// retry to get the lease
|
|
for {
|
|
if err := l.masterClient.WithClient(context.Background(), false, func(client master_pb.SeaweedClient) error {
|
|
attemptCtx, cancel := context.WithTimeout(context.Background(), rpcTimeout)
|
|
defer cancel()
|
|
resp, err := client.LeaseAdminToken(attemptCtx, &master_pb.LeaseAdminTokenRequest{
|
|
PreviousToken: atomic.LoadInt64(&l.token),
|
|
PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
|
|
LockName: l.lockName,
|
|
ClientName: clientName,
|
|
})
|
|
if err == nil {
|
|
atomic.StoreInt64(&l.token, resp.Token)
|
|
atomic.StoreInt64(&l.lockTsNs, resp.LockTsNs)
|
|
}
|
|
return err
|
|
}); err != nil {
|
|
glog.V(2).Infof("Failed to acquire lock %s: %v", l.lockName, err)
|
|
time.Sleep(InitLockInterval)
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
|
|
l.mu.Lock()
|
|
l.clientName = clientName
|
|
l.isLocked.Store(true)
|
|
l.mu.Unlock()
|
|
glog.V(1).Infof("Acquired lock %s", l.lockName)
|
|
|
|
// Each lock has and only has one goroutine
|
|
if l.renewGoroutineRunning.CompareAndSwap(false, true) {
|
|
// start a goroutine to renew the lease
|
|
go func() {
|
|
ctx2, cancel2 := context.WithCancel(context.Background())
|
|
defer cancel2()
|
|
|
|
for {
|
|
if err := l.renewLease(ctx2); err != nil {
|
|
glog.Warningf("Failed to renew lock %s: %v", l.lockName, err)
|
|
// clear the running flag before isLocked, so a RequestLock that
|
|
// reacquires (once isLocked is false) starts a replacement renewer
|
|
l.renewGoroutineRunning.Store(false)
|
|
l.isLocked.Store(false)
|
|
return
|
|
}
|
|
time.Sleep(RenewInterval)
|
|
}
|
|
}()
|
|
}
|
|
|
|
}
|
|
|
|
func (l *ExclusiveLocker) renewLease(ctx context.Context) error {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
if !l.isLocked.Load() {
|
|
return nil
|
|
}
|
|
return l.masterClient.WithClient(ctx, false, func(client master_pb.SeaweedClient) error {
|
|
attemptCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
|
|
defer cancel()
|
|
resp, err := client.LeaseAdminToken(attemptCtx, &master_pb.LeaseAdminTokenRequest{
|
|
PreviousToken: atomic.LoadInt64(&l.token),
|
|
PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
|
|
LockName: l.lockName,
|
|
ClientName: l.clientName,
|
|
Message: l.message,
|
|
})
|
|
if err == nil {
|
|
atomic.StoreInt64(&l.token, resp.Token)
|
|
atomic.StoreInt64(&l.lockTsNs, resp.LockTsNs)
|
|
glog.V(2).Infof("Renewed lock %s: ts %d token %d", l.lockName, l.lockTsNs, l.token)
|
|
}
|
|
return err
|
|
})
|
|
}
|
|
|
|
func (l *ExclusiveLocker) ReleaseLock() {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
|
|
l.isLocked.Store(false)
|
|
l.clientName = ""
|
|
|
|
prevToken := atomic.LoadInt64(&l.token)
|
|
prevLockTsNs := atomic.LoadInt64(&l.lockTsNs)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
// single unbounded attempt: a release cut short by a deadline leaves the
|
|
// lock held until it expires, turning a slow unlock into a ghost lock
|
|
l.masterClient.WithClient(ctx, false, func(client master_pb.SeaweedClient) error {
|
|
client.ReleaseAdminToken(ctx, &master_pb.ReleaseAdminTokenRequest{
|
|
PreviousToken: prevToken,
|
|
PreviousLockTime: prevLockTsNs,
|
|
LockName: l.lockName,
|
|
})
|
|
return nil
|
|
})
|
|
// compare on clear: a RequestLock racing a slow release must not have its
|
|
// fresh token zeroed
|
|
atomic.CompareAndSwapInt64(&l.token, prevToken, 0)
|
|
atomic.CompareAndSwapInt64(&l.lockTsNs, prevLockTsNs, 0)
|
|
}
|
|
|
|
func (l *ExclusiveLocker) SetMessage(message string) {
|
|
l.message = message
|
|
}
|