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/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/wdclient"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
isLocking bool
|
2021-07-05 23:30:41 +00:00
|
|
|
masterClient *wdclient.MasterClient
|
2021-09-19 19:02:23 +00:00
|
|
|
lockName 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
|
|
|
|
2020-05-26 07:03:31 +00:00
|
|
|
func (l *ExclusiveLocker) IsLocking() bool {
|
|
|
|
return l.isLocking
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
2021-04-23 06:56:35 +00:00
|
|
|
func (l *ExclusiveLocker) RequestLock(clientName string) {
|
2020-06-05 22:27:10 +00:00
|
|
|
if l.isLocking {
|
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 {
|
|
|
|
if err := l.masterClient.WithClient(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())
|
2020-04-23 10:32:18 +00:00
|
|
|
time.Sleep(InitLockInteval)
|
2020-04-23 09:31:04 +00:00
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l.isLocking = true
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
2020-04-23 09:31:04 +00:00
|
|
|
for l.isLocking {
|
|
|
|
if err := l.masterClient.WithClient(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,
|
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)
|
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() {
|
2020-04-23 09:31:04 +00:00
|
|
|
l.isLocking = false
|
2020-09-09 19:07:15 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-04-23 09:31:04 +00:00
|
|
|
l.masterClient.WithClient(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
|
|
|
}
|