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.

91 lines
2.5 KiB

  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. snapshot := fs.filer.LockRing.GetSnapshot()
  14. if snapshot == nil {
  15. resp.Error = "no lock server found"
  16. return
  17. }
  18. var movedTo pb.ServerAddress
  19. expiredAtNs := time.Now().Add(time.Duration(req.SecondsToLock) * time.Second).UnixNano()
  20. resp.RenewToken, movedTo, err = fs.dlm.Lock(fs.option.Host, req.Name, expiredAtNs, req.PreviousLockToken, snapshot)
  21. if err != nil {
  22. resp.Error = fmt.Sprintf("%v", err)
  23. }
  24. if movedTo != "" {
  25. resp.MovedTo = string(movedTo)
  26. }
  27. return resp, nil
  28. }
  29. // Unlock is a grpc handler to handle FilerServer's UnlockRequest
  30. func (fs *FilerServer) Unlock(ctx context.Context, req *filer_pb.UnlockRequest) (resp *filer_pb.UnlockResponse, err error) {
  31. resp = &filer_pb.UnlockResponse{}
  32. snapshot := fs.filer.LockRing.GetSnapshot()
  33. if snapshot == nil {
  34. resp.Error = "no lock server found"
  35. return
  36. }
  37. _, err = fs.dlm.Unlock(fs.option.Host, req.Name, req.LockToken, snapshot)
  38. if err != nil {
  39. resp.Error = fmt.Sprintf("%v", err)
  40. }
  41. return resp, nil
  42. }
  43. // TransferLocks is a grpc handler to handle FilerServer's TransferLocksRequest
  44. func (fs *FilerServer) TransferLocks(ctx context.Context, req *filer_pb.TransferLocksRequest) (*filer_pb.TransferLocksResponse, error) {
  45. for _, lock := range req.Locks {
  46. fs.dlm.InsertLock(lock.Name, lock.ExpiredAtNs, lock.RenewToken)
  47. }
  48. return &filer_pb.TransferLocksResponse{}, nil
  49. }
  50. func (fs *FilerServer) OnDlmChangeSnapshot(snapshot []pb.ServerAddress) {
  51. locks := fs.dlm.SelectNotOwnedLocks(fs.option.Host, snapshot)
  52. if len(locks) == 0 {
  53. return
  54. }
  55. for _, lock := range locks {
  56. server := fs.dlm.CalculateTargetServer(lock.Key, snapshot)
  57. if err := pb.WithFilerClient(false, 0, server, fs.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  58. _, err := client.TransferLocks(context.Background(), &filer_pb.TransferLocksRequest{
  59. Locks: []*filer_pb.Lock{
  60. {
  61. Name: lock.Key,
  62. RenewToken: lock.Token,
  63. ExpiredAtNs: lock.ExpiredAtNs,
  64. },
  65. },
  66. })
  67. return err
  68. }); err != nil {
  69. // it may not be worth retrying, since the lock may have expired
  70. glog.Errorf("transfer lock %v to %v: %v", lock.Key, server, err)
  71. }
  72. }
  73. }