seaweedfs/weed/wdclient/exclusive_locks/exclusive_locker.go

133 lines
3.3 KiB
Go
Raw Normal View History

2020-05-26 07:03:31 +00:00
package exclusive_locks
2020-04-23 09:31:04 +00:00
import (
"context"
"sync/atomic"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
"github.com/seaweedfs/seaweedfs/weed/wdclient"
2020-04-23 09:31:04 +00:00
)
const (
RenewInteval = 4 * time.Second
SafeRenewInteval = 3 * time.Second
2020-04-23 10:32:18 +00:00
InitLockInteval = 1 * time.Second
2020-04-23 09:31:04 +00:00
)
type ExclusiveLocker struct {
token int64
lockTsNs int64
2022-08-22 21:11:13 +00:00
isLocked bool
masterClient *wdclient.MasterClient
2021-09-19 19:02:23 +00:00
lockName string
2021-12-10 21:24:38 +00:00
message string
2020-04-23 09:31:04 +00:00
}
2021-09-19 19:02:23 +00:00
func NewExclusiveLocker(masterClient *wdclient.MasterClient, lockName string) *ExclusiveLocker {
2020-04-23 09:31:04 +00:00
return &ExclusiveLocker{
masterClient: masterClient,
2021-09-19 19:02:23 +00:00
lockName: lockName,
2020-04-23 09:31:04 +00:00
}
}
2021-09-19 19:02:23 +00:00
2022-08-22 21:11:13 +00:00
func (l *ExclusiveLocker) IsLocked() bool {
return l.isLocked
2020-05-26 07:03:31 +00:00
}
2020-04-23 09:31:04 +00:00
func (l *ExclusiveLocker) GetToken() (token int64, lockTsNs int64) {
for time.Unix(0, atomic.LoadInt64(&l.lockTsNs)).Add(SafeRenewInteval).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) {
2022-08-22 21:11:13 +00:00
if l.isLocked {
2020-05-26 07:03:31 +00:00
return
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2020-04-23 09:31:04 +00:00
// retry to get the lease
for {
if err := l.masterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
resp, err := client.LeaseAdminToken(ctx, &master_pb.LeaseAdminTokenRequest{
2020-04-23 10:35:52 +00:00
PreviousToken: atomic.LoadInt64(&l.token),
PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
2021-09-19 19:02:23 +00:00
LockName: l.lockName,
ClientName: clientName,
2020-04-23 10:35:52 +00:00
})
2020-04-23 09:31:04 +00:00
if err == nil {
atomic.StoreInt64(&l.token, resp.Token)
atomic.StoreInt64(&l.lockTsNs, resp.LockTsNs)
}
return err
}); err != nil {
println("lock:", err.Error())
2020-04-23 10:32:18 +00:00
time.Sleep(InitLockInteval)
2020-04-23 09:31:04 +00:00
} else {
break
}
}
2022-08-22 21:11:13 +00:00
l.isLocked = true
2020-04-23 09:31:04 +00:00
// start a goroutine to renew the lease
go func() {
ctx2, cancel2 := context.WithCancel(context.Background())
defer cancel2()
2022-08-22 21:11:13 +00:00
for l.isLocked {
if err := l.masterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
resp, err := client.LeaseAdminToken(ctx2, &master_pb.LeaseAdminTokenRequest{
2020-04-23 09:31:04 +00:00
PreviousToken: atomic.LoadInt64(&l.token),
PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
2021-09-19 19:02:23 +00:00
LockName: l.lockName,
ClientName: clientName,
2021-12-10 21:24:38 +00:00
Message: l.message,
2020-04-23 09:31:04 +00:00
})
if err == nil {
atomic.StoreInt64(&l.token, resp.Token)
atomic.StoreInt64(&l.lockTsNs, resp.LockTsNs)
2020-04-23 10:11:07 +00:00
// println("ts", l.lockTsNs, "token", l.token)
2020-04-23 09:31:04 +00:00
}
return err
}); err != nil {
2020-04-23 10:43:45 +00:00
glog.Errorf("failed to renew lock: %v", err)
2022-08-22 21:11:13 +00:00
l.isLocked = false
2020-04-23 09:31:04 +00:00
return
} else {
time.Sleep(RenewInteval)
}
}
}()
}
2020-04-23 10:11:07 +00:00
func (l *ExclusiveLocker) ReleaseLock() {
2022-08-22 21:11:13 +00:00
l.isLocked = false
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
l.masterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
client.ReleaseAdminToken(ctx, &master_pb.ReleaseAdminTokenRequest{
2020-04-23 09:31:04 +00:00
PreviousToken: atomic.LoadInt64(&l.token),
PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
2021-09-19 19:02:23 +00:00
LockName: l.lockName,
2020-04-23 09:31:04 +00:00
})
return nil
})
2020-04-23 10:32:18 +00:00
atomic.StoreInt64(&l.token, 0)
atomic.StoreInt64(&l.lockTsNs, 0)
2020-04-23 09:31:04 +00:00
}
2021-12-10 21:24:38 +00:00
func (l *ExclusiveLocker) SetMessage(message string) {
l.message = message
}