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