2020-05-26 07:03:31 +00:00
|
|
|
package exclusive_locks
|
2020-04-23 09:31:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2020-11-17 06:26:58 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util/log"
|
2020-04-23 09:31:04 +00:00
|
|
|
"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-05-26 01:39:53 +00:00
|
|
|
AdminLockName = "admin"
|
2020-04-23 09:31:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ExclusiveLocker struct {
|
|
|
|
masterClient *wdclient.MasterClient
|
|
|
|
token int64
|
|
|
|
lockTsNs int64
|
|
|
|
isLocking bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExclusiveLocker(masterClient *wdclient.MasterClient) *ExclusiveLocker {
|
|
|
|
return &ExclusiveLocker{
|
|
|
|
masterClient: masterClient,
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
2020-04-23 10:11:07 +00:00
|
|
|
func (l *ExclusiveLocker) RequestLock() {
|
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),
|
2020-05-26 01:39:53 +00:00
|
|
|
LockName: AdminLockName,
|
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 {
|
2020-04-23 10:32:18 +00:00
|
|
|
// println("leasing problem", err.Error())
|
|
|
|
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),
|
2020-05-26 01:39:53 +00:00
|
|
|
LockName: AdminLockName,
|
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-11-17 06:26:58 +00:00
|
|
|
log.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),
|
2020-05-26 01:39:53 +00:00
|
|
|
LockName: AdminLockName,
|
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
|
|
|
}
|