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.

84 lines
2.3 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package lock_manager
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/pb"
  5. "time"
  6. )
  7. const MaxDuration = 1<<63 - 1
  8. var NoLockServerError = fmt.Errorf("no lock server found")
  9. type DistributedLockManager struct {
  10. lockManager *LockManager
  11. LockRing *LockRing
  12. Host pb.ServerAddress
  13. }
  14. func NewDistributedLockManager(host pb.ServerAddress) *DistributedLockManager {
  15. return &DistributedLockManager{
  16. lockManager: NewLockManager(),
  17. LockRing: NewLockRing(time.Second * 5),
  18. Host: host,
  19. }
  20. }
  21. func (dlm *DistributedLockManager) Lock(key string, token string) (renewToken string, movedTo pb.ServerAddress, err error) {
  22. return dlm.LockWithTimeout(key, MaxDuration, token)
  23. }
  24. func (dlm *DistributedLockManager) LockWithTimeout(key string, expiredAtNs int64, token string) (renewToken string, movedTo pb.ServerAddress, err error) {
  25. servers := dlm.LockRing.GetSnapshot()
  26. if servers == nil {
  27. err = NoLockServerError
  28. return
  29. }
  30. server := hashKeyToServer(key, servers)
  31. if server != dlm.Host {
  32. movedTo = server
  33. return
  34. }
  35. renewToken, err = dlm.lockManager.Lock(key, expiredAtNs, token)
  36. return
  37. }
  38. func (dlm *DistributedLockManager) Unlock(key string, token string) (movedTo pb.ServerAddress, err error) {
  39. servers := dlm.LockRing.GetSnapshot()
  40. if servers == nil {
  41. err = NoLockServerError
  42. return
  43. }
  44. server := hashKeyToServer(key, servers)
  45. if server != dlm.Host {
  46. movedTo = server
  47. return
  48. }
  49. _, err = dlm.lockManager.Unlock(key, token)
  50. return
  51. }
  52. // InsertLock is used to insert a lock to a server unconditionally
  53. // It is used when a server is down and the lock is moved to another server
  54. func (dlm *DistributedLockManager) InsertLock(key string, expiredAtNs int64, token string) {
  55. dlm.lockManager.InsertLock(key, expiredAtNs, token)
  56. }
  57. func (dlm *DistributedLockManager) SelectNotOwnedLocks(servers []pb.ServerAddress) (locks []*Lock) {
  58. return dlm.lockManager.SelectLocks(func(key string) bool {
  59. server := hashKeyToServer(key, servers)
  60. return server != dlm.Host
  61. })
  62. }
  63. func (dlm *DistributedLockManager) CalculateTargetServer(key string, servers []pb.ServerAddress) pb.ServerAddress {
  64. return hashKeyToServer(key, servers)
  65. }
  66. func (dlm *DistributedLockManager) IsLocal(key string) bool {
  67. servers := dlm.LockRing.GetSnapshot()
  68. if len(servers) <= 1 {
  69. return true
  70. }
  71. return hashKeyToServer(key, servers) == dlm.Host
  72. }