mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-11-30 23:29:02 +08:00
89a1fd1751
commit4827425146
Author: chrislu <chris.lu@gmail.com> Date: Sat Sep 16 15:05:38 2023 -0700 balancer works commit3b50139f68
Author: chrislu <chris.lu@gmail.com> Date: Fri Sep 15 22:22:32 2023 -0700 comments commit7f685ce7ba
Author: chrislu <chris.lu@gmail.com> Date: Fri Sep 15 22:20:05 2023 -0700 adjust APIs commit436d99443b
Author: chrislu <chris.lu@gmail.com> Date: Thu Sep 14 23:49:05 2023 -0700 receive broker stats commitb771fefa37
Merge:0a851ec00
890881037
Author: chrislu <chris.lu@gmail.com> Date: Wed Sep 13 00:03:47 2023 -0700 Merge branch 'master' into sub commit0a851ec00b
Author: chrislu <chris.lu@gmail.com> Date: Sun Sep 10 22:01:25 2023 -0700 Create balancer.go commit39941edc0b
Author: chrislu <chris.lu@gmail.com> Date: Thu Sep 7 23:55:19 2023 -0700 add publisher shutdown commit875f562779
Author: chrislu <chris.lu@gmail.com> Date: Wed Sep 6 23:16:41 2023 -0700 server side send response at least once per second commit984b6c54cf
Author: chrislu <chris.lu@gmail.com> Date: Wed Sep 6 23:15:29 2023 -0700 ack interval 128 commit2492a45499
Author: chrislu <chris.lu@gmail.com> Date: Wed Sep 6 22:39:46 2023 -0700 ack interval commitba67e6ca29
Author: chrislu <chris.lu@gmail.com> Date: Mon Sep 4 21:43:50 2023 -0700 api for sub commit9e4f985698
Author: chrislu <chris.lu@gmail.com> Date: Mon Sep 4 21:43:30 2023 -0700 publish, benchmark commitcb470d44df
Author: chrislu <chris.lu@gmail.com> Date: Fri Sep 1 00:36:51 2023 -0700 can pub and sub commit1eb2da46d5
Author: chrislu <chris.lu@gmail.com> Date: Mon Aug 28 09:02:12 2023 -0700 connect and publish commit504ae8383a
Author: chrislu <chris.lu@gmail.com> Date: Mon Aug 28 09:01:25 2023 -0700 protoc version commitdbcba75271
Author: chrislu <chris.lu@gmail.com> Date: Sun Aug 27 18:59:04 2023 -0700 rename to lookup commitc9caf33119
Author: chrislu <chris.lu@gmail.com> Date: Sun Aug 27 18:33:46 2023 -0700 move functions commit4d6c18d86f
Author: chrislu <chris.lu@gmail.com> Date: Sun Aug 27 17:50:59 2023 -0700 pub sub initial tests commit4eb8e8624d
Author: chrislu <chris.lu@gmail.com> Date: Sun Aug 27 13:14:39 2023 -0700 rename commit1990456670
Author: chrislu <chris.lu@gmail.com> Date: Sun Aug 27 13:13:14 2023 -0700 sub commit905911853d
Author: chrislu <chris.lu@gmail.com> Date: Sat Aug 26 13:39:21 2023 -0700 adjust proto
211 lines
5.3 KiB
Go
211 lines
5.3 KiB
Go
package cluster
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/seaweedfs/seaweedfs/weed/cluster/lock_manager"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"google.golang.org/grpc"
|
|
"time"
|
|
)
|
|
|
|
type LockClient struct {
|
|
grpcDialOption grpc.DialOption
|
|
maxLockDuration time.Duration
|
|
sleepDuration time.Duration
|
|
seedFiler pb.ServerAddress
|
|
}
|
|
|
|
func NewLockClient(grpcDialOption grpc.DialOption, seedFiler pb.ServerAddress) *LockClient {
|
|
return &LockClient{
|
|
grpcDialOption: grpcDialOption,
|
|
maxLockDuration: 5 * time.Second,
|
|
sleepDuration: 2473 * time.Millisecond,
|
|
seedFiler: seedFiler,
|
|
}
|
|
}
|
|
|
|
type LiveLock struct {
|
|
key string
|
|
renewToken string
|
|
expireAtNs int64
|
|
filer pb.ServerAddress
|
|
cancelCh chan struct{}
|
|
grpcDialOption grpc.DialOption
|
|
isLocked bool
|
|
owner string
|
|
lc *LockClient
|
|
}
|
|
|
|
// NewLock creates a lock with a very long duration
|
|
func (lc *LockClient) NewLock(key string, owner string) (lock *LiveLock) {
|
|
return lc.doNewLock(key, lock_manager.MaxDuration, owner)
|
|
}
|
|
|
|
// StartLock starts a goroutine to lock the key and returns immediately.
|
|
func (lc *LockClient) StartLock(key string, owner string) (lock *LiveLock) {
|
|
lock = &LiveLock{
|
|
key: key,
|
|
filer: lc.seedFiler,
|
|
cancelCh: make(chan struct{}),
|
|
expireAtNs: time.Now().Add(lock_manager.MaxDuration).UnixNano(),
|
|
grpcDialOption: lc.grpcDialOption,
|
|
owner: owner,
|
|
lc: lc,
|
|
}
|
|
go func() {
|
|
util.RetryForever("create lock:"+key, func() error {
|
|
errorMessage, err := lock.doLock(lock_manager.MaxDuration)
|
|
if err != nil {
|
|
glog.Infof("create lock %s: %s", key, err)
|
|
time.Sleep(time.Second)
|
|
return err
|
|
}
|
|
if errorMessage != "" {
|
|
glog.Infof("create lock %s: %s", key, errorMessage)
|
|
time.Sleep(time.Second)
|
|
return fmt.Errorf("%v", errorMessage)
|
|
}
|
|
lock.isLocked = true
|
|
return nil
|
|
}, func(err error) (shouldContinue bool) {
|
|
if err != nil {
|
|
glog.Warningf("create lock %s: %s", key, err)
|
|
time.Sleep(time.Second)
|
|
}
|
|
return lock.renewToken == ""
|
|
})
|
|
lc.keepLock(lock)
|
|
}()
|
|
return
|
|
}
|
|
|
|
func (lc *LockClient) doNewLock(key string, lockDuration time.Duration, owner string) (lock *LiveLock) {
|
|
lock = &LiveLock{
|
|
key: key,
|
|
filer: lc.seedFiler,
|
|
cancelCh: make(chan struct{}),
|
|
expireAtNs: time.Now().Add(lockDuration).UnixNano(),
|
|
grpcDialOption: lc.grpcDialOption,
|
|
owner: owner,
|
|
lc: lc,
|
|
}
|
|
var needRenewal bool
|
|
if lockDuration > lc.maxLockDuration {
|
|
lockDuration = lc.maxLockDuration
|
|
needRenewal = true
|
|
}
|
|
util.RetryForever("create lock:"+key, func() error {
|
|
errorMessage, err := lock.doLock(lockDuration)
|
|
if err != nil {
|
|
time.Sleep(time.Second)
|
|
return err
|
|
}
|
|
if errorMessage != "" {
|
|
time.Sleep(time.Second)
|
|
return fmt.Errorf("%v", errorMessage)
|
|
}
|
|
lock.isLocked = true
|
|
return nil
|
|
}, func(err error) (shouldContinue bool) {
|
|
if err != nil {
|
|
glog.Warningf("create lock %s: %s", key, err)
|
|
}
|
|
return lock.renewToken == ""
|
|
})
|
|
|
|
if needRenewal {
|
|
go lc.keepLock(lock)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (lock *LiveLock) IsLocked() bool {
|
|
return lock.isLocked
|
|
}
|
|
|
|
func (lock *LiveLock) StopLock() error {
|
|
close(lock.cancelCh)
|
|
if !lock.isLocked {
|
|
return nil
|
|
}
|
|
return pb.WithFilerClient(false, 0, lock.filer, lock.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
|
_, err := client.DistributedUnlock(context.Background(), &filer_pb.UnlockRequest{
|
|
Name: lock.key,
|
|
RenewToken: lock.renewToken,
|
|
})
|
|
return err
|
|
})
|
|
}
|
|
|
|
func (lc *LockClient) keepLock(lock *LiveLock) {
|
|
ticker := time.Tick(lc.sleepDuration)
|
|
for {
|
|
select {
|
|
case <-ticker:
|
|
// renew the lock if lock.expireAtNs is still greater than now
|
|
util.RetryForever("keep lock:"+lock.key, func() error {
|
|
lockDuration := time.Duration(lock.expireAtNs-time.Now().UnixNano()) * time.Nanosecond
|
|
if lockDuration > lc.maxLockDuration {
|
|
lockDuration = lc.maxLockDuration
|
|
}
|
|
if lockDuration <= 0 {
|
|
return nil
|
|
}
|
|
|
|
errorMessage, err := lock.doLock(lockDuration)
|
|
if err != nil {
|
|
lock.isLocked = false
|
|
time.Sleep(time.Second)
|
|
return err
|
|
}
|
|
if errorMessage != "" {
|
|
lock.isLocked = false
|
|
time.Sleep(time.Second)
|
|
return fmt.Errorf("%v", errorMessage)
|
|
}
|
|
return nil
|
|
}, func(err error) (shouldContinue bool) {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
glog.Warningf("keep lock %s: %v", lock.key, err)
|
|
return true
|
|
})
|
|
if !lock.isLocked {
|
|
return
|
|
}
|
|
case <-lock.cancelCh:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (lock *LiveLock) doLock(lockDuration time.Duration) (errorMessage string, err error) {
|
|
err = pb.WithFilerClient(false, 0, lock.filer, lock.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
|
resp, err := client.DistributedLock(context.Background(), &filer_pb.LockRequest{
|
|
Name: lock.key,
|
|
SecondsToLock: int64(lockDuration.Seconds()),
|
|
RenewToken: lock.renewToken,
|
|
IsMoved: false,
|
|
Owner: lock.owner,
|
|
})
|
|
if err == nil {
|
|
lock.renewToken = resp.RenewToken
|
|
}
|
|
if resp != nil {
|
|
errorMessage = resp.Error
|
|
if resp.MovedTo != "" {
|
|
lock.filer = pb.ServerAddress(resp.MovedTo)
|
|
lock.lc.seedFiler = lock.filer
|
|
}
|
|
}
|
|
return err
|
|
})
|
|
return
|
|
}
|