2023-06-25 21:14:40 +00:00
|
|
|
package lock_manager
|
|
|
|
|
|
|
|
import (
|
2023-06-25 21:30:58 +00:00
|
|
|
"fmt"
|
2023-06-25 21:14:40 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
2023-06-25 21:30:58 +00:00
|
|
|
"time"
|
2023-06-25 21:14:40 +00:00
|
|
|
)
|
|
|
|
|
2023-06-26 03:30:20 +00:00
|
|
|
const MaxDuration = 1<<63 - 1
|
|
|
|
|
2023-06-26 00:37:54 +00:00
|
|
|
var NoLockServerError = fmt.Errorf("no lock server found")
|
|
|
|
|
2023-06-25 21:14:40 +00:00
|
|
|
type DistributedLockManager struct {
|
|
|
|
lockManager *LockManager
|
2023-06-25 21:30:58 +00:00
|
|
|
LockRing *LockRing
|
2023-06-26 03:30:20 +00:00
|
|
|
Host pb.ServerAddress
|
2023-06-25 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 03:30:20 +00:00
|
|
|
func NewDistributedLockManager(host pb.ServerAddress) *DistributedLockManager {
|
2023-06-25 21:14:40 +00:00
|
|
|
return &DistributedLockManager{
|
|
|
|
lockManager: NewLockManager(),
|
2023-06-25 21:30:58 +00:00
|
|
|
LockRing: NewLockRing(time.Second * 5),
|
2023-06-26 03:30:20 +00:00
|
|
|
Host: host,
|
2023-06-25 21:14:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 22:05:38 +00:00
|
|
|
func (dlm *DistributedLockManager) LockWithTimeout(key string, expiredAtNs int64, token string, owner string) (renewToken string, movedTo pb.ServerAddress, err error) {
|
|
|
|
movedTo, err = dlm.findLockOwningFiler(key)
|
2023-09-16 05:20:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if movedTo != dlm.Host {
|
|
|
|
return
|
|
|
|
}
|
2023-09-16 22:05:38 +00:00
|
|
|
renewToken, err = dlm.lockManager.Lock(key, expiredAtNs, token, owner)
|
2023-09-16 05:20:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-16 22:05:38 +00:00
|
|
|
func (dlm *DistributedLockManager) findLockOwningFiler(key string) (movedTo pb.ServerAddress, err error) {
|
2023-06-25 21:30:58 +00:00
|
|
|
servers := dlm.LockRing.GetSnapshot()
|
|
|
|
if servers == nil {
|
2023-06-26 00:37:54 +00:00
|
|
|
err = NoLockServerError
|
2023-06-25 21:30:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-16 05:20:05 +00:00
|
|
|
movedTo = hashKeyToServer(key, servers)
|
2023-06-25 21:14:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-16 22:05:38 +00:00
|
|
|
func (dlm *DistributedLockManager) FindLockOwner(key string) (owner string, movedTo pb.ServerAddress, err error) {
|
|
|
|
movedTo, err = dlm.findLockOwningFiler(key)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if movedTo != dlm.Host {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
owner, err = dlm.lockManager.GetLockOwner(key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-26 03:30:20 +00:00
|
|
|
func (dlm *DistributedLockManager) Unlock(key string, token string) (movedTo pb.ServerAddress, err error) {
|
2023-06-25 21:30:58 +00:00
|
|
|
servers := dlm.LockRing.GetSnapshot()
|
|
|
|
if servers == nil {
|
2023-06-26 00:37:54 +00:00
|
|
|
err = NoLockServerError
|
2023-06-25 21:30:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-25 21:17:58 +00:00
|
|
|
server := hashKeyToServer(key, servers)
|
2023-06-26 03:30:20 +00:00
|
|
|
if server != dlm.Host {
|
2023-06-25 21:14:40 +00:00
|
|
|
movedTo = server
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = dlm.lockManager.Unlock(key, token)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// InsertLock is used to insert a lock to a server unconditionally
|
|
|
|
// It is used when a server is down and the lock is moved to another server
|
2023-09-16 22:05:38 +00:00
|
|
|
func (dlm *DistributedLockManager) InsertLock(key string, expiredAtNs int64, token string, owner string) {
|
|
|
|
dlm.lockManager.InsertLock(key, expiredAtNs, token, owner)
|
2023-06-25 21:14:40 +00:00
|
|
|
}
|
2023-06-26 03:30:20 +00:00
|
|
|
func (dlm *DistributedLockManager) SelectNotOwnedLocks(servers []pb.ServerAddress) (locks []*Lock) {
|
2023-06-25 21:14:40 +00:00
|
|
|
return dlm.lockManager.SelectLocks(func(key string) bool {
|
2023-06-25 21:17:58 +00:00
|
|
|
server := hashKeyToServer(key, servers)
|
2023-06-26 03:30:20 +00:00
|
|
|
return server != dlm.Host
|
2023-06-25 21:14:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
func (dlm *DistributedLockManager) CalculateTargetServer(key string, servers []pb.ServerAddress) pb.ServerAddress {
|
2023-06-25 21:17:58 +00:00
|
|
|
return hashKeyToServer(key, servers)
|
2023-06-25 21:14:40 +00:00
|
|
|
}
|
2023-06-26 03:30:20 +00:00
|
|
|
|
|
|
|
func (dlm *DistributedLockManager) IsLocal(key string) bool {
|
|
|
|
servers := dlm.LockRing.GetSnapshot()
|
|
|
|
if len(servers) <= 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return hashKeyToServer(key, servers) == dlm.Host
|
|
|
|
}
|