You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.6 KiB

  1. package lock_manager
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb"
  4. )
  5. type DistributedLockManager struct {
  6. lockManager *LockManager
  7. }
  8. func NewDistributedLockManager() *DistributedLockManager {
  9. return &DistributedLockManager{
  10. lockManager: NewLockManager(),
  11. }
  12. }
  13. func (dlm *DistributedLockManager) Lock(host pb.ServerAddress, key string, expiredAtNs int64, token string, servers []pb.ServerAddress) (renewToken string, movedTo pb.ServerAddress, err error) {
  14. server := HashKeyToServer(key, servers)
  15. if server != host {
  16. movedTo = server
  17. return
  18. }
  19. renewToken, err = dlm.lockManager.Lock(key, expiredAtNs, token)
  20. return
  21. }
  22. func (dlm *DistributedLockManager) Unlock(host pb.ServerAddress, key string, token string, servers []pb.ServerAddress) (movedTo pb.ServerAddress, err error) {
  23. server := HashKeyToServer(key, servers)
  24. if server != host {
  25. movedTo = server
  26. return
  27. }
  28. _, err = dlm.lockManager.Unlock(key, token)
  29. return
  30. }
  31. // InsertLock is used to insert a lock to a server unconditionally
  32. // It is used when a server is down and the lock is moved to another server
  33. func (dlm *DistributedLockManager) InsertLock(key string, expiredAtNs int64, token string) {
  34. dlm.lockManager.InsertLock(key, expiredAtNs, token)
  35. }
  36. func (dlm *DistributedLockManager) SelectNotOwnedLocks(host pb.ServerAddress, servers []pb.ServerAddress) (locks []*Lock) {
  37. return dlm.lockManager.SelectLocks(func(key string) bool {
  38. server := HashKeyToServer(key, servers)
  39. return server != host
  40. })
  41. }
  42. func (dlm *DistributedLockManager) CalculateTargetServer(key string, servers []pb.ServerAddress) pb.ServerAddress {
  43. return HashKeyToServer(key, servers)
  44. }