master_grpc_server_admin.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "math/rand"
  7. "sync"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. )
  11. /*
  12. How exclusive lock works?
  13. -----------
  14. Shell
  15. ------
  16. When shell lock,
  17. * lease an admin token (lockTime, token)
  18. * start a goroutine to renew the admin token periodically
  19. When shell unlock
  20. * stop the renewal goroutine
  21. * sends a release lock request
  22. Master
  23. ------
  24. Master maintains:
  25. * randomNumber
  26. * lastLockTime
  27. When master receives the lease/renew request from shell
  28. If lastLockTime still fresh {
  29. if is a renew and token is valid {
  30. // for renew
  31. generate the randomNumber => token
  32. return
  33. }
  34. refuse
  35. return
  36. } else {
  37. // for fresh lease request
  38. generate the randomNumber => token
  39. return
  40. }
  41. When master receives the release lock request from shell
  42. set the lastLockTime to zero
  43. The volume server does not need to verify.
  44. This makes the lock/unlock optional, similar to what golang code usually does.
  45. */
  46. const (
  47. LockDuration = 10 * time.Second
  48. )
  49. type AdminLock struct {
  50. accessSecret int64
  51. accessLockTime time.Time
  52. lastClient string
  53. lastMessage string
  54. }
  55. type AdminLocks struct {
  56. locks map[string]*AdminLock
  57. sync.RWMutex
  58. }
  59. func NewAdminLocks() *AdminLocks {
  60. return &AdminLocks{
  61. locks: make(map[string]*AdminLock),
  62. }
  63. }
  64. func (locks *AdminLocks) isLocked(lockName string) (clientName string, message string, isLocked bool) {
  65. locks.RLock()
  66. defer locks.RUnlock()
  67. adminLock, found := locks.locks[lockName]
  68. if !found {
  69. return "", "", false
  70. }
  71. glog.V(4).Infof("isLocked %v: %v", adminLock.lastClient, adminLock.lastMessage)
  72. return adminLock.lastClient, adminLock.lastMessage, adminLock.accessLockTime.Add(LockDuration).After(time.Now())
  73. }
  74. func (locks *AdminLocks) isValidToken(lockName string, ts time.Time, token int64) bool {
  75. locks.RLock()
  76. defer locks.RUnlock()
  77. adminLock, found := locks.locks[lockName]
  78. if !found {
  79. return false
  80. }
  81. return adminLock.accessLockTime.Equal(ts) && adminLock.accessSecret == token
  82. }
  83. func (locks *AdminLocks) generateToken(lockName string, clientName string) (ts time.Time, token int64) {
  84. locks.Lock()
  85. defer locks.Unlock()
  86. lock := &AdminLock{
  87. accessSecret: rand.Int63(),
  88. accessLockTime: time.Now(),
  89. lastClient: clientName,
  90. }
  91. locks.locks[lockName] = lock
  92. return lock.accessLockTime, lock.accessSecret
  93. }
  94. func (locks *AdminLocks) deleteLock(lockName string) {
  95. locks.Lock()
  96. defer locks.Unlock()
  97. delete(locks.locks, lockName)
  98. }
  99. func (ms *MasterServer) LeaseAdminToken(ctx context.Context, req *master_pb.LeaseAdminTokenRequest) (*master_pb.LeaseAdminTokenResponse, error) {
  100. resp := &master_pb.LeaseAdminTokenResponse{}
  101. if lastClient, lastMessage, isLocked := ms.adminLocks.isLocked(req.LockName); isLocked {
  102. glog.V(4).Infof("LeaseAdminToken %v", lastClient)
  103. if req.PreviousToken != 0 && ms.adminLocks.isValidToken(req.LockName, time.Unix(0, req.PreviousLockTime), req.PreviousToken) {
  104. // for renew
  105. ts, token := ms.adminLocks.generateToken(req.LockName, req.ClientName)
  106. resp.Token, resp.LockTsNs = token, ts.UnixNano()
  107. return resp, nil
  108. }
  109. // refuse since still locked
  110. return resp, fmt.Errorf("already locked by %v: %v", lastClient, lastMessage)
  111. }
  112. // for fresh lease request
  113. ts, token := ms.adminLocks.generateToken(req.LockName, req.ClientName)
  114. resp.Token, resp.LockTsNs = token, ts.UnixNano()
  115. return resp, nil
  116. }
  117. func (ms *MasterServer) ReleaseAdminToken(ctx context.Context, req *master_pb.ReleaseAdminTokenRequest) (*master_pb.ReleaseAdminTokenResponse, error) {
  118. resp := &master_pb.ReleaseAdminTokenResponse{}
  119. if ms.adminLocks.isValidToken(req.LockName, time.Unix(0, req.PreviousLockTime), req.PreviousToken) {
  120. ms.adminLocks.deleteLock(req.LockName)
  121. }
  122. return resp, nil
  123. }