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.

114 lines
3.2 KiB

2 years ago
2 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "time"
  9. )
  10. // Lock is a grpc handler to handle FilerServer's LockRequest
  11. func (fs *FilerServer) Lock(ctx context.Context, req *filer_pb.LockRequest) (resp *filer_pb.LockResponse, err error) {
  12. resp = &filer_pb.LockResponse{}
  13. var movedTo pb.ServerAddress
  14. expiredAtNs := time.Now().Add(time.Duration(req.SecondsToLock) * time.Second).UnixNano()
  15. resp.RenewToken, movedTo, err = fs.filer.Dlm.LockWithTimeout(req.Name, expiredAtNs, req.RenewToken)
  16. if !req.IsMoved && movedTo != "" {
  17. err = pb.WithFilerClient(false, 0, movedTo, fs.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  18. secondResp, err := client.Lock(context.Background(), &filer_pb.LockRequest{
  19. Name: req.Name,
  20. SecondsToLock: req.SecondsToLock,
  21. RenewToken: req.RenewToken,
  22. IsMoved: true,
  23. })
  24. if err == nil {
  25. resp.RenewToken = secondResp.RenewToken
  26. } else {
  27. resp.Error = secondResp.Error
  28. }
  29. return err
  30. })
  31. }
  32. if err != nil {
  33. resp.Error = fmt.Sprintf("%v", err)
  34. }
  35. if movedTo != "" {
  36. resp.MovedTo = string(movedTo)
  37. }
  38. return resp, nil
  39. }
  40. // Unlock is a grpc handler to handle FilerServer's UnlockRequest
  41. func (fs *FilerServer) Unlock(ctx context.Context, req *filer_pb.UnlockRequest) (resp *filer_pb.UnlockResponse, err error) {
  42. resp = &filer_pb.UnlockResponse{}
  43. var movedTo pb.ServerAddress
  44. movedTo, err = fs.filer.Dlm.Unlock(req.Name, req.RenewToken)
  45. if !req.IsMoved && movedTo != "" {
  46. err = pb.WithFilerClient(false, 0, movedTo, fs.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  47. secondResp, err := client.Unlock(context.Background(), &filer_pb.UnlockRequest{
  48. Name: req.Name,
  49. RenewToken: req.RenewToken,
  50. IsMoved: true,
  51. })
  52. resp.Error = secondResp.Error
  53. return err
  54. })
  55. }
  56. if err != nil {
  57. resp.Error = fmt.Sprintf("%v", err)
  58. }
  59. if movedTo != "" {
  60. resp.MovedTo = string(movedTo)
  61. }
  62. return resp, nil
  63. }
  64. // TransferLocks is a grpc handler to handle FilerServer's TransferLocksRequest
  65. func (fs *FilerServer) TransferLocks(ctx context.Context, req *filer_pb.TransferLocksRequest) (*filer_pb.TransferLocksResponse, error) {
  66. for _, lock := range req.Locks {
  67. fs.filer.Dlm.InsertLock(lock.Name, lock.ExpiredAtNs, lock.RenewToken)
  68. }
  69. return &filer_pb.TransferLocksResponse{}, nil
  70. }
  71. func (fs *FilerServer) OnDlmChangeSnapshot(snapshot []pb.ServerAddress) {
  72. locks := fs.filer.Dlm.SelectNotOwnedLocks(snapshot)
  73. if len(locks) == 0 {
  74. return
  75. }
  76. for _, lock := range locks {
  77. server := fs.filer.Dlm.CalculateTargetServer(lock.Key, snapshot)
  78. if err := pb.WithFilerClient(false, 0, server, fs.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  79. _, err := client.TransferLocks(context.Background(), &filer_pb.TransferLocksRequest{
  80. Locks: []*filer_pb.Lock{
  81. {
  82. Name: lock.Key,
  83. RenewToken: lock.Token,
  84. ExpiredAtNs: lock.ExpiredAtNs,
  85. },
  86. },
  87. })
  88. return err
  89. }); err != nil {
  90. // it may not be worth retrying, since the lock may have expired
  91. glog.Errorf("transfer lock %v to %v: %v", lock.Key, server, err)
  92. }
  93. }
  94. }