mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
refactoring
This commit is contained in:
parent
d4235afe4d
commit
5d3ec22975
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
||||||
"github.com/chrislusf/seaweedfs/weed/util"
|
"github.com/chrislusf/seaweedfs/weed/util"
|
||||||
"github.com/chrislusf/seaweedfs/weed/wdclient"
|
"github.com/chrislusf/seaweedfs/weed/wdclient"
|
||||||
|
"github.com/chrislusf/seaweedfs/weed/wdclient/exclusive_locks"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ShellOptions struct {
|
type ShellOptions struct {
|
||||||
|
@ -28,7 +29,7 @@ type CommandEnv struct {
|
||||||
env map[string]string
|
env map[string]string
|
||||||
MasterClient *wdclient.MasterClient
|
MasterClient *wdclient.MasterClient
|
||||||
option ShellOptions
|
option ShellOptions
|
||||||
locker *ExclusiveLocker
|
locker *exclusive_locks.ExclusiveLocker
|
||||||
}
|
}
|
||||||
|
|
||||||
type command interface {
|
type command interface {
|
||||||
|
@ -47,7 +48,7 @@ func NewCommandEnv(options ShellOptions) *CommandEnv {
|
||||||
MasterClient: wdclient.NewMasterClient(options.GrpcDialOption, pb.AdminShellClient, "", 0, strings.Split(*options.Masters, ",")),
|
MasterClient: wdclient.NewMasterClient(options.GrpcDialOption, pb.AdminShellClient, "", 0, strings.Split(*options.Masters, ",")),
|
||||||
option: options,
|
option: options,
|
||||||
}
|
}
|
||||||
ce.locker = NewExclusiveLocker(ce.MasterClient)
|
ce.locker = exclusive_locks.NewExclusiveLocker(ce.MasterClient)
|
||||||
return ce
|
return ce
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +71,7 @@ func (ce *CommandEnv) isDirectory(path string) bool {
|
||||||
|
|
||||||
func (ce *CommandEnv) confirmIsLocked() error {
|
func (ce *CommandEnv) confirmIsLocked() error {
|
||||||
|
|
||||||
if ce.locker.isLocking {
|
if ce.locker.IsLocking() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package shell
|
package exclusive_locks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
@ -29,6 +29,9 @@ func NewExclusiveLocker(masterClient *wdclient.MasterClient) *ExclusiveLocker {
|
||||||
masterClient: masterClient,
|
masterClient: masterClient,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func (l *ExclusiveLocker) IsLocking() bool {
|
||||||
|
return l.isLocking
|
||||||
|
}
|
||||||
|
|
||||||
func (l *ExclusiveLocker) GetToken() (token int64, lockTsNs int64) {
|
func (l *ExclusiveLocker) GetToken() (token int64, lockTsNs int64) {
|
||||||
for time.Unix(0, atomic.LoadInt64(&l.lockTsNs)).Add(SafeRenewInteval).Before(time.Now()) {
|
for time.Unix(0, atomic.LoadInt64(&l.lockTsNs)).Add(SafeRenewInteval).Before(time.Now()) {
|
||||||
|
@ -39,6 +42,10 @@ func (l *ExclusiveLocker) GetToken() (token int64, lockTsNs int64) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ExclusiveLocker) RequestLock() {
|
func (l *ExclusiveLocker) RequestLock() {
|
||||||
|
if l.isLocking {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// retry to get the lease
|
// retry to get the lease
|
||||||
for {
|
for {
|
||||||
if err := l.masterClient.WithClient(func(client master_pb.SeaweedClient) error {
|
if err := l.masterClient.WithClient(func(client master_pb.SeaweedClient) error {
|
Loading…
Reference in a new issue