Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion internal/dcs/zk.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type zkDCS struct {
eventsChan <-chan zk.Event
disconnectCallback func() error
closeTimer *time.Timer
lockHeld sync.Map
connectedChans []chan struct{}
acl []zk.ACL
connectedLock sync.Mutex
Expand Down Expand Up @@ -222,6 +223,7 @@ func (z *zkDCS) handleSessionEvent(ev zk.Event) {
z.connectedLock.Lock()
if z.isConnected && z.closeTimer != nil {
defer z.logger.Info("Session lost")
z.lockHeld.Clear()
z.isConnected = false
err := z.disconnectCallback()
if err != nil {
Expand Down Expand Up @@ -341,6 +343,10 @@ func (z *zkDCS) retryDelete(path string, version int32) (err error) {

func (z *zkDCS) AcquireLock(path string) bool {
fullPath := z.buildFullPath(path)
_, hasLock := z.lockHeld.Load(fullPath)
if hasLock {
return true
}
self := z.getSelfLockOwner()
data, _, err := z.retryGet(fullPath)
if err != nil && err != zk.ErrNoNode {
Expand All @@ -359,14 +365,19 @@ func (z *zkDCS) AcquireLock(path string) bool {
}
return false
}
z.lockHeld.Store(fullPath, struct{}{})
return true
}
owner := LockOwner{}
if err = json.Unmarshal(data, &owner); err != nil {
z.logger.Error(fmt.Sprintf("Malformed lock data %s (%s)", fullPath, data), slog.Any("error", err))
return false
}
return owner == self
if owner == self {
z.lockHeld.Store(fullPath, struct{}{})
return true
}
return false
}

func (z *zkDCS) ReleaseLock(path string) {
Expand All @@ -378,6 +389,7 @@ func (z *zkDCS) ReleaseLock(path string) {

func (z *zkDCS) ReleaseLockOrError(path string) error {
fullPath := z.buildFullPath(path)
z.lockHeld.Delete(fullPath)
data, stat, err := z.retryGet(fullPath)
if err != nil && err != zk.ErrNoNode {
return fmt.Errorf("failed to get lock info %s: %v", fullPath, err)
Expand Down
Loading