2020-05-26 07:03:31 +00:00
|
|
|
package exclusive_locks
|
2020-04-23 09:31:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"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 (
|
2022-09-14 17:29:55 +00:00
|
|
|
RenewInterval = 4 * time.Second
|
|
|
|
SafeRenewInterval = 3 * time.Second
|
|
|
|
InitLockInterval = 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
|
2021-07-05 23:30:41 +00:00
|
|
|
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) {
|
2022-09-14 17:29:55 +00:00
|
|
|
for time.Unix(0, atomic.LoadInt64(&l.lockTsNs)).Add(SafeRenewInterval).Before(time.Now()) {
|
2020-04-23 09:31:04 +00: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 06:56:35 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-09 19:07:15 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-04-23 09:31:04 +00:00
|
|
|
// retry to get the lease
|
|
|
|
for {
|
2021-12-26 08:15:03 +00:00
|
|
|
if err := l.masterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
|
2020-09-09 19:07:15 +00:00
|
|
|
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,
|
2021-04-23 06:56:35 +00:00
|
|
|
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 {
|
2021-04-23 06:56:35 +00:00
|
|
|
println("lock:", err.Error())
|
2022-09-14 17:29:55 +00:00
|
|
|
time.Sleep(InitLockInterval)
|
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() {
|
2020-09-11 02:51:03 +00:00
|
|
|
ctx2, cancel2 := context.WithCancel(context.Background())
|
|
|
|
defer cancel2()
|
|
|
|
|
2022-08-22 21:11:13 +00:00
|
|
|
for l.isLocked {
|
2021-12-26 08:15:03 +00:00
|
|
|
if err := l.masterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
|
2020-09-11 02:51:03 +00:00
|
|
|
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,
|
2021-04-23 06:56:35 +00:00
|
|
|
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 {
|
2022-09-14 17:29:55 +00:00
|
|
|
time.Sleep(RenewInterval)
|
2020-04-23 09:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-23 10:11:07 +00:00
|
|
|
func (l *ExclusiveLocker) ReleaseLock() {
|
2022-08-22 21:11:13 +00:00
|
|
|
l.isLocked = false
|
2020-09-09 19:07:15 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2021-12-26 08:15:03 +00:00
|
|
|
l.masterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
|
2020-09-09 19:07:15 +00:00
|
|
|
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
|
|
|
|
}
|