Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions internal/dcs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type ZookeeperConfig struct {
Hostname string `config:"hostname" yaml:"hostname"`
SessionTimeout time.Duration `config:"session_timeout" yaml:"session_timeout"`
LockHeldTTL time.Duration `config:"lock_held_ttl" yaml:"lock_held_ttl"`
Namespace string `config:"namespace,required" yaml:"namespace"`
Hosts []string `config:"hosts,required" yaml:"hosts"`
BackoffInterval time.Duration `config:"backoff_interval" yaml:"backoff_interval"`
Expand Down Expand Up @@ -57,6 +58,7 @@ func DefaultZookeeperConfig() (ZookeeperConfig, error) {
config := ZookeeperConfig{
Hostname: hostname,
SessionTimeout: 2 * time.Second,
LockHeldTTL: 30 * time.Second,
BackoffInterval: backoff.DefaultInitialInterval,
BackoffRandFactor: backoff.DefaultRandomizationFactor,
BackoffMultiplier: backoff.DefaultMultiplier,
Expand Down
12 changes: 7 additions & 5 deletions internal/dcs/zk.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,11 @@ 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
if cached, hasLock := z.lockHeld.Load(fullPath); hasLock {
if time.Since(cached.(time.Time)) < z.config.LockHeldTTL {
return true
}
z.lockHeld.Delete(fullPath)
}
self := z.getSelfLockOwner()
data, _, err := z.retryGet(fullPath)
Expand All @@ -362,7 +364,7 @@ func (z *zkDCS) AcquireLock(path string) bool {
}
return false
}
z.lockHeld.Store(fullPath, struct{}{})
z.lockHeld.Store(fullPath, time.Now())
return true
}
owner := LockOwner{}
Expand All @@ -371,7 +373,7 @@ func (z *zkDCS) AcquireLock(path string) bool {
return false
}
if owner == self {
z.lockHeld.Store(fullPath, struct{}{})
z.lockHeld.Store(fullPath, time.Now())
return true
}
return false
Expand Down
Loading