aboutsummaryrefslogtreecommitdiff
path: root/weed/cluster/lock_client.go
diff options
context:
space:
mode:
Diffstat (limited to 'weed/cluster/lock_client.go')
-rw-r--r--weed/cluster/lock_client.go50
1 files changed, 44 insertions, 6 deletions
diff --git a/weed/cluster/lock_client.go b/weed/cluster/lock_client.go
index 6618f5d2f..9b8ed7556 100644
--- a/weed/cluster/lock_client.go
+++ b/weed/cluster/lock_client.go
@@ -3,13 +3,14 @@ package cluster
import (
"context"
"fmt"
+ "time"
+
"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 {
@@ -109,15 +110,22 @@ func (lock *LiveLock) retryUntilLocked(lockDuration time.Duration) {
}
func (lock *LiveLock) AttemptToLock(lockDuration time.Duration) error {
+ glog.V(4).Infof("LOCK: AttemptToLock key=%s owner=%s", lock.key, lock.self)
errorMessage, err := lock.doLock(lockDuration)
if err != nil {
+ glog.V(1).Infof("LOCK: doLock failed for key=%s: %v", lock.key, err)
time.Sleep(time.Second)
return err
}
if errorMessage != "" {
+ glog.V(1).Infof("LOCK: doLock returned error message for key=%s: %s", lock.key, errorMessage)
time.Sleep(time.Second)
return fmt.Errorf("%v", errorMessage)
}
+ if !lock.isLocked {
+ // Only log when transitioning from unlocked to locked
+ glog.V(1).Infof("LOCK: Successfully acquired key=%s owner=%s", lock.key, lock.self)
+ }
lock.isLocked = true
return nil
}
@@ -138,7 +146,27 @@ func (lock *LiveLock) StopShortLivedLock() error {
})
}
+// Stop stops a long-lived lock by closing the cancel channel and releasing the lock
+func (lock *LiveLock) Stop() error {
+ // Close the cancel channel to stop the long-lived lock goroutine
+ select {
+ case <-lock.cancelCh:
+ // Already closed
+ default:
+ close(lock.cancelCh)
+ }
+
+ // Also release the lock if held
+ return lock.StopShortLivedLock()
+}
+
func (lock *LiveLock) doLock(lockDuration time.Duration) (errorMessage string, err error) {
+ glog.V(4).Infof("LOCK: doLock calling DistributedLock - key=%s filer=%s owner=%s",
+ lock.key, lock.hostFiler, lock.self)
+
+ previousHostFiler := lock.hostFiler
+ previousOwner := lock.owner
+
err = pb.WithFilerClient(false, 0, lock.hostFiler, lock.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
resp, err := client.DistributedLock(context.Background(), &filer_pb.LockRequest{
Name: lock.key,
@@ -147,23 +175,33 @@ func (lock *LiveLock) doLock(lockDuration time.Duration) (errorMessage string, e
IsMoved: false,
Owner: lock.self,
})
+ glog.V(4).Infof("LOCK: DistributedLock response - key=%s err=%v", lock.key, err)
if err == nil && resp != nil {
lock.renewToken = resp.RenewToken
+ glog.V(4).Infof("LOCK: Got renewToken for key=%s", lock.key)
} else {
//this can be retried. Need to remember the last valid renewToken
lock.renewToken = ""
+ glog.V(1).Infof("LOCK: Cleared renewToken for key=%s (err=%v)", lock.key, err)
}
if resp != nil {
errorMessage = resp.Error
- if resp.LockHostMovedTo != "" {
+ if resp.LockHostMovedTo != "" && resp.LockHostMovedTo != string(previousHostFiler) {
+ // Only log if the host actually changed
+ glog.V(1).Infof("LOCK: Host changed from %s to %s for key=%s", previousHostFiler, resp.LockHostMovedTo, lock.key)
lock.hostFiler = pb.ServerAddress(resp.LockHostMovedTo)
lock.lc.seedFiler = lock.hostFiler
+ } else if resp.LockHostMovedTo != "" {
+ lock.hostFiler = pb.ServerAddress(resp.LockHostMovedTo)
}
- if resp.LockOwner != "" {
+ if resp.LockOwner != "" && resp.LockOwner != previousOwner {
+ // Only log if the owner actually changed
+ glog.V(1).Infof("LOCK: Owner changed from %s to %s for key=%s", previousOwner, resp.LockOwner, lock.key)
lock.owner = resp.LockOwner
- // fmt.Printf("lock %s owner: %s\n", lock.key, lock.owner)
- } else {
- // fmt.Printf("lock %s has no owner\n", lock.key)
+ } else if resp.LockOwner != "" {
+ lock.owner = resp.LockOwner
+ } else if previousOwner != "" {
+ glog.V(1).Infof("LOCK: Owner cleared for key=%s", lock.key)
lock.owner = ""
}
}