exclusive_locker.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package exclusive_locks
  2. import (
  3. "context"
  4. "sync/atomic"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  8. "github.com/chrislusf/seaweedfs/weed/wdclient"
  9. )
  10. const (
  11. RenewInteval = 4 * time.Second
  12. SafeRenewInteval = 3 * time.Second
  13. InitLockInteval = 1 * time.Second
  14. AdminLockName = "admin"
  15. )
  16. type ExclusiveLocker struct {
  17. masterClient *wdclient.MasterClient
  18. token int64
  19. lockTsNs int64
  20. isLocking bool
  21. }
  22. func NewExclusiveLocker(masterClient *wdclient.MasterClient) *ExclusiveLocker {
  23. return &ExclusiveLocker{
  24. masterClient: masterClient,
  25. }
  26. }
  27. func (l *ExclusiveLocker) IsLocking() bool {
  28. return l.isLocking
  29. }
  30. func (l *ExclusiveLocker) GetToken() (token int64, lockTsNs int64) {
  31. for time.Unix(0, atomic.LoadInt64(&l.lockTsNs)).Add(SafeRenewInteval).Before(time.Now()) {
  32. // wait until now is within the safe lock period, no immediate renewal to change the token
  33. time.Sleep(100 * time.Millisecond)
  34. }
  35. return atomic.LoadInt64(&l.token), atomic.LoadInt64(&l.lockTsNs)
  36. }
  37. func (l *ExclusiveLocker) RequestLock() {
  38. if l.isLocking {
  39. return
  40. }
  41. ctx, cancel := context.WithCancel(context.Background())
  42. defer cancel()
  43. // retry to get the lease
  44. for {
  45. if err := l.masterClient.WithClient(func(client master_pb.SeaweedClient) error {
  46. resp, err := client.LeaseAdminToken(ctx, &master_pb.LeaseAdminTokenRequest{
  47. PreviousToken: atomic.LoadInt64(&l.token),
  48. PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
  49. LockName: AdminLockName,
  50. })
  51. if err == nil {
  52. atomic.StoreInt64(&l.token, resp.Token)
  53. atomic.StoreInt64(&l.lockTsNs, resp.LockTsNs)
  54. }
  55. return err
  56. }); err != nil {
  57. // println("leasing problem", err.Error())
  58. time.Sleep(InitLockInteval)
  59. } else {
  60. break
  61. }
  62. }
  63. l.isLocking = true
  64. // start a goroutine to renew the lease
  65. go func() {
  66. ctx2, cancel2 := context.WithCancel(context.Background())
  67. defer cancel2()
  68. for l.isLocking {
  69. if err := l.masterClient.WithClient(func(client master_pb.SeaweedClient) error {
  70. resp, err := client.LeaseAdminToken(ctx2, &master_pb.LeaseAdminTokenRequest{
  71. PreviousToken: atomic.LoadInt64(&l.token),
  72. PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
  73. LockName: AdminLockName,
  74. })
  75. if err == nil {
  76. atomic.StoreInt64(&l.token, resp.Token)
  77. atomic.StoreInt64(&l.lockTsNs, resp.LockTsNs)
  78. // println("ts", l.lockTsNs, "token", l.token)
  79. }
  80. return err
  81. }); err != nil {
  82. glog.Errorf("failed to renew lock: %v", err)
  83. return
  84. } else {
  85. time.Sleep(RenewInteval)
  86. }
  87. }
  88. }()
  89. }
  90. func (l *ExclusiveLocker) ReleaseLock() {
  91. l.isLocking = false
  92. ctx, cancel := context.WithCancel(context.Background())
  93. defer cancel()
  94. l.masterClient.WithClient(func(client master_pb.SeaweedClient) error {
  95. client.ReleaseAdminToken(ctx, &master_pb.ReleaseAdminTokenRequest{
  96. PreviousToken: atomic.LoadInt64(&l.token),
  97. PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
  98. LockName: AdminLockName,
  99. })
  100. return nil
  101. })
  102. atomic.StoreInt64(&l.token, 0)
  103. atomic.StoreInt64(&l.lockTsNs, 0)
  104. }