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.

111 lines
2.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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. // retry to get the lease
  42. for {
  43. if err := l.masterClient.WithClient(func(client master_pb.SeaweedClient) error {
  44. resp, err := client.LeaseAdminToken(context.Background(), &master_pb.LeaseAdminTokenRequest{
  45. PreviousToken: atomic.LoadInt64(&l.token),
  46. PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
  47. LockName: AdminLockName,
  48. })
  49. if err == nil {
  50. atomic.StoreInt64(&l.token, resp.Token)
  51. atomic.StoreInt64(&l.lockTsNs, resp.LockTsNs)
  52. }
  53. return err
  54. }); err != nil {
  55. // println("leasing problem", err.Error())
  56. time.Sleep(InitLockInteval)
  57. } else {
  58. break
  59. }
  60. }
  61. l.isLocking = true
  62. // start a goroutine to renew the lease
  63. go func() {
  64. for l.isLocking {
  65. if err := l.masterClient.WithClient(func(client master_pb.SeaweedClient) error {
  66. resp, err := client.LeaseAdminToken(context.Background(), &master_pb.LeaseAdminTokenRequest{
  67. PreviousToken: atomic.LoadInt64(&l.token),
  68. PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
  69. LockName: AdminLockName,
  70. })
  71. if err == nil {
  72. atomic.StoreInt64(&l.token, resp.Token)
  73. atomic.StoreInt64(&l.lockTsNs, resp.LockTsNs)
  74. // println("ts", l.lockTsNs, "token", l.token)
  75. }
  76. return err
  77. }); err != nil {
  78. glog.Errorf("failed to renew lock: %v", err)
  79. return
  80. } else {
  81. time.Sleep(RenewInteval)
  82. }
  83. }
  84. }()
  85. }
  86. func (l *ExclusiveLocker) ReleaseLock() {
  87. l.isLocking = false
  88. l.masterClient.WithClient(func(client master_pb.SeaweedClient) error {
  89. client.ReleaseAdminToken(context.Background(), &master_pb.ReleaseAdminTokenRequest{
  90. PreviousToken: atomic.LoadInt64(&l.token),
  91. PreviousLockTime: atomic.LoadInt64(&l.lockTsNs),
  92. LockName: AdminLockName,
  93. })
  94. return nil
  95. })
  96. atomic.StoreInt64(&l.token, 0)
  97. atomic.StoreInt64(&l.lockTsNs, 0)
  98. }