exclusive_locker.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. )
  15. type ExclusiveLocker struct {
  16. token int64
  17. lockTsNs int64
  18. isLocking bool
  19. masterClient *wdclient.MasterClient
  20. lockName string
  21. message string
  22. }
  23. func NewExclusiveLocker(masterClient *wdclient.MasterClient, lockName string) *ExclusiveLocker {
  24. return &ExclusiveLocker{
  25. masterClient: masterClient,
  26. lockName: lockName,
  27. }
  28. }
  29. func (l *ExclusiveLocker) IsLocking() bool {
  30. return l.isLocking
  31. }
  32. func (l *ExclusiveLocker) GetToken() (token int64, lockTsNs int64) {
  33. for time.Unix(0, atomic.LoadInt64(&l.lockTsNs)).Add(SafeRenewInteval).Before(time.Now()) {
  34. // wait until now is within the safe lock period, no immediate renewal to change the token
  35. time.Sleep(100 * time.Millisecond)
  36. }
  37. return atomic.LoadInt64(&l.token), atomic.LoadInt64(&l.lockTsNs)
  38. }
  39. func (l *ExclusiveLocker) RequestLock(clientName string) {
  40. if l.isLocking {
  41. return
  42. }
  43. ctx, cancel := context.WithCancel(context.Background())
  44. defer cancel()
  45. // retry to get the lease
  46. for {
  47. if err := l.masterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  48. resp, err := client.LeaseAdminToken(ctx, &master_pb.LeaseAdminTokenRequest{
  49. PreviousToken: atomic.LoadInt64(&l.token),
  50. PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
  51. LockName: l.lockName,
  52. ClientName: clientName,
  53. })
  54. if err == nil {
  55. atomic.StoreInt64(&l.token, resp.Token)
  56. atomic.StoreInt64(&l.lockTsNs, resp.LockTsNs)
  57. }
  58. return err
  59. }); err != nil {
  60. println("lock:", err.Error())
  61. time.Sleep(InitLockInteval)
  62. } else {
  63. break
  64. }
  65. }
  66. l.isLocking = true
  67. // start a goroutine to renew the lease
  68. go func() {
  69. ctx2, cancel2 := context.WithCancel(context.Background())
  70. defer cancel2()
  71. for l.isLocking {
  72. if err := l.masterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  73. resp, err := client.LeaseAdminToken(ctx2, &master_pb.LeaseAdminTokenRequest{
  74. PreviousToken: atomic.LoadInt64(&l.token),
  75. PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
  76. LockName: l.lockName,
  77. ClientName: clientName,
  78. Message: l.message,
  79. })
  80. if err == nil {
  81. atomic.StoreInt64(&l.token, resp.Token)
  82. atomic.StoreInt64(&l.lockTsNs, resp.LockTsNs)
  83. // println("ts", l.lockTsNs, "token", l.token)
  84. }
  85. return err
  86. }); err != nil {
  87. glog.Errorf("failed to renew lock: %v", err)
  88. return
  89. } else {
  90. time.Sleep(RenewInteval)
  91. }
  92. }
  93. }()
  94. }
  95. func (l *ExclusiveLocker) ReleaseLock() {
  96. l.isLocking = false
  97. ctx, cancel := context.WithCancel(context.Background())
  98. defer cancel()
  99. l.masterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  100. client.ReleaseAdminToken(ctx, &master_pb.ReleaseAdminTokenRequest{
  101. PreviousToken: atomic.LoadInt64(&l.token),
  102. PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
  103. LockName: l.lockName,
  104. })
  105. return nil
  106. })
  107. atomic.StoreInt64(&l.token, 0)
  108. atomic.StoreInt64(&l.lockTsNs, 0)
  109. }
  110. func (l *ExclusiveLocker) SetMessage(message string) {
  111. l.message = message
  112. }