2023-06-25 15:58:21 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-01-30 14:46:23 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/cluster/lock_manager"
|
2023-06-25 15:58:21 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
2023-09-17 06:05:38 +08:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2023-06-25 15:58:21 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-09-17 06:05:38 +08:00
|
|
|
// DistributedLock is a grpc handler to handle FilerServer's LockRequest
|
|
|
|
func (fs *FilerServer) DistributedLock(ctx context.Context, req *filer_pb.LockRequest) (resp *filer_pb.LockResponse, err error) {
|
2023-06-25 15:58:21 +08:00
|
|
|
|
|
|
|
resp = &filer_pb.LockResponse{}
|
|
|
|
|
2023-06-26 05:14:40 +08:00
|
|
|
var movedTo pb.ServerAddress
|
|
|
|
expiredAtNs := time.Now().Add(time.Duration(req.SecondsToLock) * time.Second).UnixNano()
|
2024-02-03 07:54:57 +08:00
|
|
|
resp.LockOwner, resp.RenewToken, movedTo, err = fs.filer.Dlm.LockWithTimeout(req.Name, expiredAtNs, req.RenewToken, req.Owner)
|
2023-09-20 05:10:54 +08:00
|
|
|
glog.V(3).Infof("lock %s %v %v %v, isMoved=%v %v", req.Name, req.SecondsToLock, req.RenewToken, req.Owner, req.IsMoved, movedTo)
|
2023-09-17 06:05:38 +08:00
|
|
|
if movedTo != "" && movedTo != fs.option.Host && !req.IsMoved {
|
2023-06-26 06:28:01 +08:00
|
|
|
err = pb.WithFilerClient(false, 0, movedTo, fs.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2023-09-17 06:05:38 +08:00
|
|
|
secondResp, err := client.DistributedLock(context.Background(), &filer_pb.LockRequest{
|
2023-06-26 06:28:01 +08:00
|
|
|
Name: req.Name,
|
|
|
|
SecondsToLock: req.SecondsToLock,
|
|
|
|
RenewToken: req.RenewToken,
|
|
|
|
IsMoved: true,
|
2023-09-17 06:05:38 +08:00
|
|
|
Owner: req.Owner,
|
2023-06-26 06:28:01 +08:00
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
resp.RenewToken = secondResp.RenewToken
|
2024-02-03 07:54:57 +08:00
|
|
|
resp.LockOwner = secondResp.LockOwner
|
2023-06-26 06:28:01 +08:00
|
|
|
resp.Error = secondResp.Error
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2023-06-25 15:58:21 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
resp.Error = fmt.Sprintf("%v", err)
|
2023-06-26 05:14:40 +08:00
|
|
|
}
|
|
|
|
if movedTo != "" {
|
2024-02-03 07:54:57 +08:00
|
|
|
resp.LockHostMovedTo = string(movedTo)
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock is a grpc handler to handle FilerServer's UnlockRequest
|
2023-09-17 06:05:38 +08:00
|
|
|
func (fs *FilerServer) DistributedUnlock(ctx context.Context, req *filer_pb.UnlockRequest) (resp *filer_pb.UnlockResponse, err error) {
|
2023-06-25 15:58:21 +08:00
|
|
|
|
|
|
|
resp = &filer_pb.UnlockResponse{}
|
|
|
|
|
2023-06-26 06:28:01 +08:00
|
|
|
var movedTo pb.ServerAddress
|
2023-06-26 11:30:20 +08:00
|
|
|
movedTo, err = fs.filer.Dlm.Unlock(req.Name, req.RenewToken)
|
2023-06-26 06:28:01 +08:00
|
|
|
|
|
|
|
if !req.IsMoved && movedTo != "" {
|
|
|
|
err = pb.WithFilerClient(false, 0, movedTo, fs.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2023-09-17 06:05:38 +08:00
|
|
|
secondResp, err := client.DistributedUnlock(context.Background(), &filer_pb.UnlockRequest{
|
2023-06-26 06:28:01 +08:00
|
|
|
Name: req.Name,
|
|
|
|
RenewToken: req.RenewToken,
|
|
|
|
IsMoved: true,
|
|
|
|
})
|
|
|
|
resp.Error = secondResp.Error
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-25 15:58:21 +08:00
|
|
|
if err != nil {
|
|
|
|
resp.Error = fmt.Sprintf("%v", err)
|
|
|
|
}
|
2023-06-26 06:28:01 +08:00
|
|
|
if movedTo != "" {
|
|
|
|
resp.MovedTo = string(movedTo)
|
|
|
|
}
|
2023-06-25 15:58:21 +08:00
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-17 06:05:38 +08:00
|
|
|
func (fs *FilerServer) FindLockOwner(ctx context.Context, req *filer_pb.FindLockOwnerRequest) (*filer_pb.FindLockOwnerResponse, error) {
|
|
|
|
owner, movedTo, err := fs.filer.Dlm.FindLockOwner(req.Name)
|
2024-02-06 09:30:14 +08:00
|
|
|
if !req.IsMoved && movedTo != "" || err == lock_manager.LockNotFound {
|
2023-09-17 06:05:38 +08:00
|
|
|
err = pb.WithFilerClient(false, 0, movedTo, fs.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
secondResp, err := client.FindLockOwner(context.Background(), &filer_pb.FindLockOwnerRequest{
|
|
|
|
Name: req.Name,
|
|
|
|
IsMoved: true,
|
|
|
|
})
|
2023-09-20 05:10:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-17 06:05:38 +08:00
|
|
|
owner = secondResp.Owner
|
2023-09-20 05:10:54 +08:00
|
|
|
return nil
|
2023-09-17 06:05:38 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2024-01-30 14:46:23 +08:00
|
|
|
|
|
|
|
if owner == "" {
|
|
|
|
glog.V(0).Infof("find lock %s moved to %v: %v", req.Name, movedTo, err)
|
2024-01-31 00:21:42 +08:00
|
|
|
return nil, status.Error(codes.NotFound, fmt.Sprintf("lock %s not found", req.Name))
|
2024-01-30 14:46:23 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-09-17 06:05:38 +08:00
|
|
|
return &filer_pb.FindLockOwnerResponse{
|
|
|
|
Owner: owner,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-06-25 15:58:21 +08:00
|
|
|
// TransferLocks is a grpc handler to handle FilerServer's TransferLocksRequest
|
|
|
|
func (fs *FilerServer) TransferLocks(ctx context.Context, req *filer_pb.TransferLocksRequest) (*filer_pb.TransferLocksResponse, error) {
|
|
|
|
|
|
|
|
for _, lock := range req.Locks {
|
2023-09-17 06:05:38 +08:00
|
|
|
fs.filer.Dlm.InsertLock(lock.Name, lock.ExpiredAtNs, lock.RenewToken, lock.Owner)
|
2023-06-25 15:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return &filer_pb.TransferLocksResponse{}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FilerServer) OnDlmChangeSnapshot(snapshot []pb.ServerAddress) {
|
2023-06-26 11:30:20 +08:00
|
|
|
locks := fs.filer.Dlm.SelectNotOwnedLocks(snapshot)
|
2023-06-25 15:58:21 +08:00
|
|
|
if len(locks) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, lock := range locks {
|
2023-06-26 05:30:58 +08:00
|
|
|
server := fs.filer.Dlm.CalculateTargetServer(lock.Key, snapshot)
|
2023-06-25 15:58:21 +08:00
|
|
|
if err := pb.WithFilerClient(false, 0, server, fs.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
_, err := client.TransferLocks(context.Background(), &filer_pb.TransferLocksRequest{
|
|
|
|
Locks: []*filer_pb.Lock{
|
|
|
|
{
|
2023-06-26 05:14:40 +08:00
|
|
|
Name: lock.Key,
|
|
|
|
RenewToken: lock.Token,
|
|
|
|
ExpiredAtNs: lock.ExpiredAtNs,
|
2023-09-17 06:05:38 +08:00
|
|
|
Owner: lock.Owner,
|
2023-06-25 15:58:21 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
// it may not be worth retrying, since the lock may have expired
|
|
|
|
glog.Errorf("transfer lock %v to %v: %v", lock.Key, server, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|