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.

107 lines
2.9 KiB

  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  8. )
  9. /*
  10. How exclusive lock works?
  11. -----------
  12. Shell
  13. ------
  14. When shell lock,
  15. * lease an admin token (lockTime, token)
  16. * start a goroutine to renew the admin token periodically
  17. For later volume operations, send (lockTime, token) to volume servers for exclusive access
  18. * need to pause renewal a few seconds, to prevent race condition
  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. When master receives the verfication request from volume servers
  44. return secret+lockTime == token && lockTime == lastLockTime
  45. Volume
  46. ------
  47. When receives (lockTime, token), ask the master whether this is valid
  48. */
  49. const (
  50. LockDuration = 10 * time.Second
  51. )
  52. func (ms *MasterServer) LeaseAdminToken(ctx context.Context, req *master_pb.LeaseAdminTokenRequest) (*master_pb.LeaseAdminTokenResponse, error) {
  53. resp := &master_pb.LeaseAdminTokenResponse{}
  54. if ms.adminAccessLockTime.Add(LockDuration).After(time.Now()) {
  55. if req.PreviousToken != 0 && ms.isValidToken(time.Unix(0, req.PreviousLockTime), req.PreviousToken) {
  56. // for renew
  57. ts, token := ms.generateToken()
  58. resp.Token, resp.LockTsNs = token, ts.UnixNano()
  59. return resp, nil
  60. }
  61. // refuse since still locked
  62. return resp, fmt.Errorf("already locked")
  63. }
  64. // for fresh lease request
  65. ts, token := ms.generateToken()
  66. resp.Token, resp.LockTsNs = token, ts.UnixNano()
  67. return resp, nil
  68. }
  69. func (ms *MasterServer) isValidToken(ts time.Time, token int64) bool {
  70. return ms.adminAccessLockTime.Equal(ts) && ms.adminAccessSecret == token
  71. }
  72. func (ms *MasterServer) generateToken() (ts time.Time, token int64) {
  73. ms.adminAccessLockTime = time.Now()
  74. ms.adminAccessSecret = rand.Int63()
  75. return ms.adminAccessLockTime, ms.adminAccessSecret
  76. }
  77. func (ms *MasterServer) ReleaseAdminToken(ctx context.Context, req *master_pb.ReleaseAdminTokenRequest) (*master_pb.ReleaseAdminTokenResponse, error) {
  78. resp := &master_pb.ReleaseAdminTokenResponse{}
  79. if ms.isValidToken(time.Unix(0, req.PreviousLockTime), req.PreviousToken) {
  80. ms.adminAccessSecret = 0
  81. }
  82. return resp, nil
  83. }
  84. func (ms *MasterServer) VerifyAdminToken(ctx context.Context, req *master_pb.VerifyAdminTokenRequest) (*master_pb.VerifyAdminTokenResponse, error) {
  85. resp := &master_pb.VerifyAdminTokenResponse{}
  86. if ms.isValidToken(time.Unix(0, req.LockTime), req.Token) {
  87. resp.IsValid = true
  88. }
  89. return resp, nil
  90. }