2023-06-25 15:58:21 +08:00
|
|
|
package lock_manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
2024-01-30 14:46:23 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2024-02-05 01:20:21 +08:00
|
|
|
"sync"
|
2023-06-25 15:58:21 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-06-26 08:37:54 +08:00
|
|
|
var LockErrorNonEmptyTokenOnNewLock = fmt.Errorf("lock: non-empty token on a new lock")
|
|
|
|
var LockErrorNonEmptyTokenOnExpiredLock = fmt.Errorf("lock: non-empty token on an expired lock")
|
|
|
|
var LockErrorTokenMismatch = fmt.Errorf("lock: token mismatch")
|
|
|
|
var UnlockErrorTokenMismatch = fmt.Errorf("unlock: token mismatch")
|
2024-01-30 14:46:23 +08:00
|
|
|
var LockNotFound = fmt.Errorf("lock not found")
|
2023-06-26 08:37:54 +08:00
|
|
|
|
2023-09-17 06:06:16 +08:00
|
|
|
// LockManager local lock manager, used by distributed lock manager
|
2023-06-25 15:58:21 +08:00
|
|
|
type LockManager struct {
|
2024-02-05 01:20:21 +08:00
|
|
|
locks map[string]*Lock
|
|
|
|
accessLock sync.RWMutex
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
|
|
|
type Lock struct {
|
2023-06-26 05:14:40 +08:00
|
|
|
Token string
|
|
|
|
ExpiredAtNs int64
|
|
|
|
Key string // only used for moving locks
|
2023-09-17 06:06:16 +08:00
|
|
|
Owner string
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLockManager() *LockManager {
|
|
|
|
t := &LockManager{
|
2024-02-05 01:20:21 +08:00
|
|
|
locks: make(map[string]*Lock),
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
|
|
|
go t.CleanUp()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2024-02-03 07:54:57 +08:00
|
|
|
func (lm *LockManager) Lock(path string, expiredAtNs int64, token string, owner string) (lockOwner, renewToken string, err error) {
|
2024-02-05 01:20:21 +08:00
|
|
|
lm.accessLock.Lock()
|
|
|
|
defer lm.accessLock.Unlock()
|
|
|
|
|
|
|
|
glog.V(4).Infof("lock %s %v %v %v", path, time.Unix(0, expiredAtNs), token, owner)
|
|
|
|
|
|
|
|
if oldValue, found := lm.locks[path]; found {
|
|
|
|
if oldValue.ExpiredAtNs > 0 && oldValue.ExpiredAtNs < time.Now().UnixNano() {
|
|
|
|
// lock is expired, set to a new lock
|
|
|
|
if token != "" {
|
|
|
|
glog.V(4).Infof("lock expired key %s non-empty token %v owner %v ts %s", path, token, owner, time.Unix(0, oldValue.ExpiredAtNs))
|
|
|
|
err = LockErrorNonEmptyTokenOnExpiredLock
|
|
|
|
return
|
2023-06-25 15:58:21 +08:00
|
|
|
} else {
|
2024-02-05 01:20:21 +08:00
|
|
|
// new lock
|
|
|
|
renewToken = uuid.New().String()
|
|
|
|
glog.V(4).Infof("key %s new token %v owner %v", path, renewToken, owner)
|
|
|
|
lm.locks[path] = &Lock{Token: renewToken, ExpiredAtNs: expiredAtNs, Owner: owner}
|
|
|
|
return
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
2024-02-05 01:20:21 +08:00
|
|
|
}
|
|
|
|
// not expired
|
|
|
|
lockOwner = oldValue.Owner
|
|
|
|
if oldValue.Token == token {
|
|
|
|
// token matches, renew the lock
|
|
|
|
renewToken = uuid.New().String()
|
|
|
|
glog.V(4).Infof("key %s old token %v owner %v => %v owner %v", path, oldValue.Token, oldValue.Owner, renewToken, owner)
|
|
|
|
lm.locks[path] = &Lock{Token: renewToken, ExpiredAtNs: expiredAtNs, Owner: owner}
|
|
|
|
return
|
2023-06-25 15:58:21 +08:00
|
|
|
} else {
|
|
|
|
if token == "" {
|
2023-06-26 05:14:40 +08:00
|
|
|
// new lock
|
2024-02-05 01:20:21 +08:00
|
|
|
glog.V(4).Infof("key %s locked by %v", path, oldValue.Owner)
|
|
|
|
err = fmt.Errorf("lock already owned by %v", oldValue.Owner)
|
|
|
|
return
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
2024-02-05 01:20:21 +08:00
|
|
|
glog.V(4).Infof("key %s expected token %v owner %v received %v from %v", path, oldValue.Token, oldValue.Owner, token, owner)
|
|
|
|
err = fmt.Errorf("lock: token mismatch")
|
|
|
|
return
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
2024-02-05 01:20:21 +08:00
|
|
|
} else {
|
|
|
|
glog.V(4).Infof("key %s no lock owner %v", path, owner)
|
|
|
|
if token == "" {
|
|
|
|
// new lock
|
|
|
|
glog.V(4).Infof("key %s new token %v owner %v", path, token, owner)
|
|
|
|
renewToken = uuid.New().String()
|
|
|
|
lm.locks[path] = &Lock{Token: renewToken, ExpiredAtNs: expiredAtNs, Owner: owner}
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
glog.V(4).Infof("key %s non-empty token %v owner %v", path, token, owner)
|
|
|
|
err = LockErrorNonEmptyTokenOnNewLock
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lm *LockManager) Unlock(path string, token string) (isUnlocked bool, err error) {
|
2024-02-05 01:20:21 +08:00
|
|
|
lm.accessLock.Lock()
|
|
|
|
defer lm.accessLock.Unlock()
|
|
|
|
|
|
|
|
if oldValue, found := lm.locks[path]; found {
|
|
|
|
now := time.Now()
|
|
|
|
if oldValue.ExpiredAtNs > 0 && oldValue.ExpiredAtNs < now.UnixNano() {
|
|
|
|
// lock is expired, delete it
|
|
|
|
isUnlocked = true
|
|
|
|
glog.V(4).Infof("key %s expired at %v", path, time.Unix(0, oldValue.ExpiredAtNs))
|
|
|
|
delete(lm.locks, path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if oldValue.Token == token {
|
2023-06-25 15:58:21 +08:00
|
|
|
isUnlocked = true
|
2024-02-05 01:20:21 +08:00
|
|
|
glog.V(4).Infof("key %s unlocked with %v", path, token)
|
|
|
|
delete(lm.locks, path)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
isUnlocked = false
|
|
|
|
err = UnlockErrorTokenMismatch
|
|
|
|
return
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
2024-02-05 01:20:21 +08:00
|
|
|
}
|
|
|
|
err = LockNotFound
|
2023-06-25 15:58:21 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lm *LockManager) CleanUp() {
|
2024-02-05 01:20:21 +08:00
|
|
|
|
2023-06-25 15:58:21 +08:00
|
|
|
for {
|
|
|
|
time.Sleep(1 * time.Minute)
|
|
|
|
now := time.Now().UnixNano()
|
2024-02-05 01:20:21 +08:00
|
|
|
|
|
|
|
lm.accessLock.Lock()
|
|
|
|
for key, value := range lm.locks {
|
2023-06-26 06:28:01 +08:00
|
|
|
if value == nil {
|
2024-02-05 01:20:21 +08:00
|
|
|
continue
|
2023-06-26 06:28:01 +08:00
|
|
|
}
|
2023-06-26 05:14:40 +08:00
|
|
|
if now > value.ExpiredAtNs {
|
2024-02-05 01:20:21 +08:00
|
|
|
glog.V(4).Infof("key %s expired at %v", key, time.Unix(0, value.ExpiredAtNs))
|
|
|
|
delete(lm.locks, key)
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
2024-02-05 01:20:21 +08:00
|
|
|
}
|
|
|
|
lm.accessLock.Unlock()
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 05:14:40 +08:00
|
|
|
// SelectLocks takes out locks by key
|
2023-06-25 15:58:21 +08:00
|
|
|
// if keyFn return true, the lock will be taken out
|
2023-06-26 05:14:40 +08:00
|
|
|
func (lm *LockManager) SelectLocks(selectFn func(key string) bool) (locks []*Lock) {
|
2024-02-05 01:20:21 +08:00
|
|
|
lm.accessLock.RLock()
|
|
|
|
defer lm.accessLock.RUnlock()
|
|
|
|
|
2023-06-25 15:58:21 +08:00
|
|
|
now := time.Now().UnixNano()
|
2024-02-05 01:20:21 +08:00
|
|
|
|
|
|
|
for key, lock := range lm.locks {
|
2023-06-26 05:14:40 +08:00
|
|
|
if now > lock.ExpiredAtNs {
|
2024-02-05 01:20:21 +08:00
|
|
|
glog.V(4).Infof("key %s expired at %v", key, time.Unix(0, lock.ExpiredAtNs))
|
|
|
|
delete(lm.locks, key)
|
|
|
|
continue
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
2023-06-26 05:14:40 +08:00
|
|
|
if selectFn(key) {
|
2024-02-05 01:20:21 +08:00
|
|
|
glog.V(4).Infof("key %s selected and deleted", key)
|
|
|
|
delete(lm.locks, key)
|
2023-06-25 15:58:21 +08:00
|
|
|
lock.Key = key
|
|
|
|
locks = append(locks, lock)
|
|
|
|
}
|
2024-02-05 01:20:21 +08:00
|
|
|
}
|
2023-06-25 15:58:21 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-26 05:14:40 +08:00
|
|
|
|
|
|
|
// InsertLock inserts a lock unconditionally
|
2023-09-17 06:06:16 +08:00
|
|
|
func (lm *LockManager) InsertLock(path string, expiredAtNs int64, token string, owner string) {
|
2024-02-05 01:20:21 +08:00
|
|
|
lm.accessLock.Lock()
|
|
|
|
defer lm.accessLock.Unlock()
|
|
|
|
|
|
|
|
lm.locks[path] = &Lock{Token: token, ExpiredAtNs: expiredAtNs, Owner: owner}
|
2023-09-17 06:06:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lm *LockManager) GetLockOwner(key string) (owner string, err error) {
|
2024-02-05 01:20:21 +08:00
|
|
|
lm.accessLock.RLock()
|
|
|
|
defer lm.accessLock.RUnlock()
|
|
|
|
|
|
|
|
if lock, found := lm.locks[key]; found {
|
2024-01-30 14:46:23 +08:00
|
|
|
return lock.Owner, nil
|
|
|
|
}
|
|
|
|
err = LockNotFound
|
2023-09-17 06:06:16 +08:00
|
|
|
return
|
2023-06-26 05:14:40 +08:00
|
|
|
}
|