seaweedfs/weed/cluster/lock_client.go

189 lines
4.9 KiB
Go
Raw Normal View History

2023-06-26 08:38:34 +08:00
package cluster
import (
"context"
"fmt"
2023-06-26 11:30:20 +08:00
"github.com/seaweedfs/seaweedfs/weed/cluster/lock_manager"
2023-06-26 08:38:34 +08:00
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/grpc"
"time"
)
type LockClient struct {
grpcDialOption grpc.DialOption
maxLockDuration time.Duration
sleepDuration time.Duration
2023-09-17 06:05:38 +08:00
seedFiler pb.ServerAddress
2023-06-26 08:38:34 +08:00
}
2023-09-17 06:05:38 +08:00
func NewLockClient(grpcDialOption grpc.DialOption, seedFiler pb.ServerAddress) *LockClient {
2023-06-26 08:38:34 +08:00
return &LockClient{
grpcDialOption: grpcDialOption,
maxLockDuration: 5 * time.Second,
2023-09-17 06:05:38 +08:00
sleepDuration: 2473 * time.Millisecond,
seedFiler: seedFiler,
2023-06-26 08:38:34 +08:00
}
}
type LiveLock struct {
key string
renewToken string
expireAtNs int64
filer pb.ServerAddress
cancelCh chan struct{}
grpcDialOption grpc.DialOption
isLocked bool
2023-09-17 06:05:38 +08:00
owner string
lc *LockClient
2023-06-26 08:38:34 +08:00
}
2024-02-02 15:01:44 +08:00
// NewShortLivedLock creates a lock with a 5-second duration
func (lc *LockClient) NewShortLivedLock(key string, owner string) (lock *LiveLock) {
2023-09-17 06:05:38 +08:00
lock = &LiveLock{
key: key,
filer: lc.seedFiler,
cancelCh: make(chan struct{}),
2024-02-02 15:01:44 +08:00
expireAtNs: time.Now().Add(5*time.Second).UnixNano(),
2023-09-17 06:05:38 +08:00
grpcDialOption: lc.grpcDialOption,
owner: owner,
lc: lc,
}
2024-02-02 15:01:44 +08:00
lock.retryUntilLocked(5*time.Second)
2023-09-17 06:05:38 +08:00
return
2023-06-26 10:31:25 +08:00
}
2024-02-02 15:01:44 +08:00
// StartLock starts a goroutine to lock the key and returns immediately.
func (lc *LockClient) StartLock(key string, owner string) (lock *LiveLock) {
2023-06-26 08:38:34 +08:00
lock = &LiveLock{
key: key,
2023-09-17 06:05:38 +08:00
filer: lc.seedFiler,
2023-06-26 08:38:34 +08:00
cancelCh: make(chan struct{}),
2024-02-02 15:01:44 +08:00
expireAtNs: time.Now().Add(lock_manager.MaxDuration).UnixNano(),
2023-06-26 08:38:34 +08:00
grpcDialOption: lc.grpcDialOption,
2023-09-17 06:05:38 +08:00
owner: owner,
lc: lc,
2023-06-26 08:38:34 +08:00
}
2024-02-02 15:01:44 +08:00
go func() {
lock.retryUntilLocked(lock_manager.MaxDuration)
lc.keepLock(lock)
}()
2024-01-30 14:46:23 +08:00
return
}
2024-02-02 15:01:44 +08:00
func (lock *LiveLock) retryUntilLocked(lockDuration time.Duration) {
2024-01-30 14:46:23 +08:00
util.RetryUntil("create lock:"+lock.key, func() error {
2024-02-02 15:01:44 +08:00
return lock.AttemptToLock(lockDuration)
2023-06-26 08:38:34 +08:00
}, func(err error) (shouldContinue bool) {
if err != nil {
2024-01-30 14:46:23 +08:00
glog.Warningf("create lock %s: %s", lock.key, err)
2023-06-26 08:38:34 +08:00
}
return lock.renewToken == ""
})
2024-01-30 14:46:23 +08:00
}
2023-06-26 08:38:34 +08:00
2024-02-02 15:01:44 +08:00
func (lock *LiveLock) AttemptToLock(lockDuration time.Duration) error {
2024-01-30 14:46:23 +08:00
errorMessage, err := lock.doLock(lockDuration)
if err != nil {
time.Sleep(time.Second)
return err
2023-06-26 08:38:34 +08:00
}
2024-01-30 14:46:23 +08:00
if errorMessage != "" {
time.Sleep(time.Second)
return fmt.Errorf("%v", errorMessage)
}
lock.isLocked = true
return nil
2023-06-26 08:38:34 +08:00
}
func (lock *LiveLock) IsLocked() bool {
return lock!=nil && lock.isLocked
2023-06-26 08:38:34 +08:00
}
2024-02-02 15:01:44 +08:00
func (lock *LiveLock) StopShortLivedLock() error {
2023-09-17 06:05:38 +08:00
if !lock.isLocked {
return nil
}
2024-02-02 15:01:44 +08:00
defer func() {
lock.isLocked = false
}()
2023-06-26 08:38:34 +08:00
return pb.WithFilerClient(false, 0, lock.filer, lock.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
2023-09-17 06:05:38 +08:00
_, err := client.DistributedUnlock(context.Background(), &filer_pb.UnlockRequest{
2023-06-26 08:38:34 +08:00
Name: lock.key,
RenewToken: lock.renewToken,
})
return err
})
}
func (lc *LockClient) keepLock(lock *LiveLock) {
2023-09-17 06:05:38 +08:00
ticker := time.Tick(lc.sleepDuration)
2023-06-26 08:38:34 +08:00
for {
select {
2023-09-17 06:05:38 +08:00
case <-ticker:
2023-06-26 08:38:34 +08:00
// renew the lock if lock.expireAtNs is still greater than now
2023-10-02 02:33:56 +08:00
util.RetryUntil("keep lock:"+lock.key, func() error {
2023-06-26 08:38:34 +08:00
lockDuration := time.Duration(lock.expireAtNs-time.Now().UnixNano()) * time.Nanosecond
if lockDuration > lc.maxLockDuration {
lockDuration = lc.maxLockDuration
}
if lockDuration <= 0 {
return nil
}
errorMessage, err := lock.doLock(lockDuration)
if err != nil {
lock.isLocked = false
2023-09-17 06:05:38 +08:00
time.Sleep(time.Second)
2024-01-30 14:46:23 +08:00
glog.V(0).Infof("keep lock %s: %v", lock.key, err)
2023-06-26 08:38:34 +08:00
return err
}
if errorMessage != "" {
lock.isLocked = false
2023-09-17 06:05:38 +08:00
time.Sleep(time.Second)
2024-01-30 14:46:23 +08:00
glog.V(4).Infof("keep lock message %s: %v", lock.key, errorMessage)
return fmt.Errorf("keep lock error: %v", errorMessage)
2023-06-26 08:38:34 +08:00
}
return nil
}, func(err error) (shouldContinue bool) {
if err == nil {
return false
}
glog.Warningf("keep lock %s: %v", lock.key, err)
return true
})
if !lock.isLocked {
return
}
case <-lock.cancelCh:
return
}
}
}
func (lock *LiveLock) doLock(lockDuration time.Duration) (errorMessage string, err error) {
err = pb.WithFilerClient(false, 0, lock.filer, lock.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
2023-09-17 06:05:38 +08:00
resp, err := client.DistributedLock(context.Background(), &filer_pb.LockRequest{
2023-06-26 08:38:34 +08:00
Name: lock.key,
SecondsToLock: int64(lockDuration.Seconds()),
RenewToken: lock.renewToken,
IsMoved: false,
2023-09-17 06:05:38 +08:00
Owner: lock.owner,
2023-06-26 08:38:34 +08:00
})
if err == nil {
lock.renewToken = resp.RenewToken
}
if resp != nil {
errorMessage = resp.Error
if resp.MovedTo != "" {
lock.filer = pb.ServerAddress(resp.MovedTo)
2023-09-17 06:05:38 +08:00
lock.lc.seedFiler = lock.filer
2023-06-26 08:38:34 +08:00
}
}
return err
})
return
}