seaweedfs/weed/shell/exclusive_locker.go

101 lines
2.6 KiB
Go
Raw Normal View History

2020-04-23 09:31:04 +00:00
package shell
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 {
masterClient *wdclient.MasterClient
token int64
lockTsNs int64
isLocking bool
}
func NewExclusiveLocker(masterClient *wdclient.MasterClient) *ExclusiveLocker {
return &ExclusiveLocker{
masterClient: masterClient,
}
}
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-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-04-23 10:35:52 +00:00
resp, err := client.LeaseAdminToken(context.Background(), &master_pb.LeaseAdminTokenRequest{
PreviousToken: atomic.LoadInt64(&l.token),
PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
})
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() {
for l.isLocking {
if err := l.masterClient.WithClient(func(client master_pb.SeaweedClient) error {
resp, err := client.LeaseAdminToken(context.Background(), &master_pb.LeaseAdminTokenRequest{
PreviousToken: atomic.LoadInt64(&l.token),
PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
})
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
l.masterClient.WithClient(func(client master_pb.SeaweedClient) error {
client.ReleaseAdminToken(context.Background(), &master_pb.ReleaseAdminTokenRequest{
PreviousToken: atomic.LoadInt64(&l.token),
PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
})
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
}