2020-04-23 08:55:44 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-23 09:31:04 +00:00
|
|
|
"fmt"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/cluster"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
2020-04-23 08:55:44 +00:00
|
|
|
"math/rand"
|
2020-05-26 01:39:53 +00:00
|
|
|
"sync"
|
2020-04-23 08:55:44 +00:00
|
|
|
"time"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
2020-04-23 08:55:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
How exclusive lock works?
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Shell
|
|
|
|
------
|
|
|
|
When shell lock,
|
|
|
|
* lease an admin token (lockTime, token)
|
|
|
|
* start a goroutine to renew the admin token periodically
|
|
|
|
|
|
|
|
When shell unlock
|
|
|
|
* stop the renewal goroutine
|
|
|
|
* sends a release lock request
|
|
|
|
|
|
|
|
Master
|
|
|
|
------
|
|
|
|
Master maintains:
|
|
|
|
* randomNumber
|
|
|
|
* lastLockTime
|
|
|
|
When master receives the lease/renew request from shell
|
|
|
|
If lastLockTime still fresh {
|
|
|
|
if is a renew and token is valid {
|
|
|
|
// for renew
|
|
|
|
generate the randomNumber => token
|
|
|
|
return
|
|
|
|
}
|
|
|
|
refuse
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// for fresh lease request
|
|
|
|
generate the randomNumber => token
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
When master receives the release lock request from shell
|
|
|
|
set the lastLockTime to zero
|
|
|
|
|
|
|
|
|
2020-04-23 10:50:05 +00:00
|
|
|
The volume server does not need to verify.
|
|
|
|
This makes the lock/unlock optional, similar to what golang code usually does.
|
2020-04-23 08:55:44 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
const (
|
|
|
|
LockDuration = 10 * time.Second
|
|
|
|
)
|
|
|
|
|
2020-05-26 01:39:53 +00:00
|
|
|
type AdminLock struct {
|
|
|
|
accessSecret int64
|
|
|
|
accessLockTime time.Time
|
2021-04-23 06:56:35 +00:00
|
|
|
lastClient string
|
2021-12-10 21:24:38 +00:00
|
|
|
lastMessage string
|
2020-05-26 01:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AdminLocks struct {
|
|
|
|
locks map[string]*AdminLock
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAdminLocks() *AdminLocks {
|
|
|
|
return &AdminLocks{
|
|
|
|
locks: make(map[string]*AdminLock),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 21:24:38 +00:00
|
|
|
func (locks *AdminLocks) isLocked(lockName string) (clientName string, message string, isLocked bool) {
|
2020-05-26 01:39:53 +00:00
|
|
|
locks.RLock()
|
|
|
|
defer locks.RUnlock()
|
|
|
|
adminLock, found := locks.locks[lockName]
|
|
|
|
if !found {
|
2021-12-10 21:24:38 +00:00
|
|
|
return "", "", false
|
2020-05-26 01:39:53 +00:00
|
|
|
}
|
2021-12-10 21:24:38 +00:00
|
|
|
glog.V(4).Infof("isLocked %v: %v", adminLock.lastClient, adminLock.lastMessage)
|
|
|
|
return adminLock.lastClient, adminLock.lastMessage, adminLock.accessLockTime.Add(LockDuration).After(time.Now())
|
2020-05-26 01:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (locks *AdminLocks) isValidToken(lockName string, ts time.Time, token int64) bool {
|
|
|
|
locks.RLock()
|
|
|
|
defer locks.RUnlock()
|
|
|
|
adminLock, found := locks.locks[lockName]
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return adminLock.accessLockTime.Equal(ts) && adminLock.accessSecret == token
|
|
|
|
}
|
|
|
|
|
2021-04-23 06:56:35 +00:00
|
|
|
func (locks *AdminLocks) generateToken(lockName string, clientName string) (ts time.Time, token int64) {
|
2020-05-26 01:39:53 +00:00
|
|
|
locks.Lock()
|
|
|
|
defer locks.Unlock()
|
|
|
|
lock := &AdminLock{
|
|
|
|
accessSecret: rand.Int63(),
|
|
|
|
accessLockTime: time.Now(),
|
2021-04-23 06:56:35 +00:00
|
|
|
lastClient: clientName,
|
2020-05-26 01:39:53 +00:00
|
|
|
}
|
|
|
|
locks.locks[lockName] = lock
|
|
|
|
return lock.accessLockTime, lock.accessSecret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (locks *AdminLocks) deleteLock(lockName string) {
|
|
|
|
locks.Lock()
|
|
|
|
defer locks.Unlock()
|
|
|
|
delete(locks.locks, lockName)
|
|
|
|
}
|
|
|
|
|
2020-04-23 08:55:44 +00:00
|
|
|
func (ms *MasterServer) LeaseAdminToken(ctx context.Context, req *master_pb.LeaseAdminTokenRequest) (*master_pb.LeaseAdminTokenResponse, error) {
|
|
|
|
resp := &master_pb.LeaseAdminTokenResponse{}
|
|
|
|
|
2021-12-10 21:24:38 +00:00
|
|
|
if lastClient, lastMessage, isLocked := ms.adminLocks.isLocked(req.LockName); isLocked {
|
2021-04-23 06:56:35 +00:00
|
|
|
glog.V(4).Infof("LeaseAdminToken %v", lastClient)
|
2020-05-26 01:39:53 +00:00
|
|
|
if req.PreviousToken != 0 && ms.adminLocks.isValidToken(req.LockName, time.Unix(0, req.PreviousLockTime), req.PreviousToken) {
|
2020-04-23 08:55:44 +00:00
|
|
|
// for renew
|
2021-04-23 06:56:35 +00:00
|
|
|
ts, token := ms.adminLocks.generateToken(req.LockName, req.ClientName)
|
2020-04-23 08:55:44 +00:00
|
|
|
resp.Token, resp.LockTsNs = token, ts.UnixNano()
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
// refuse since still locked
|
2021-12-10 21:24:38 +00:00
|
|
|
return resp, fmt.Errorf("already locked by %v: %v", lastClient, lastMessage)
|
2020-04-23 08:55:44 +00:00
|
|
|
}
|
|
|
|
// for fresh lease request
|
2021-04-23 06:56:35 +00:00
|
|
|
ts, token := ms.adminLocks.generateToken(req.LockName, req.ClientName)
|
2020-04-23 08:55:44 +00:00
|
|
|
resp.Token, resp.LockTsNs = token, ts.UnixNano()
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) ReleaseAdminToken(ctx context.Context, req *master_pb.ReleaseAdminTokenRequest) (*master_pb.ReleaseAdminTokenResponse, error) {
|
|
|
|
resp := &master_pb.ReleaseAdminTokenResponse{}
|
2020-05-26 01:39:53 +00:00
|
|
|
if ms.adminLocks.isValidToken(req.LockName, time.Unix(0, req.PreviousLockTime), req.PreviousToken) {
|
|
|
|
ms.adminLocks.deleteLock(req.LockName)
|
2020-04-23 08:55:44 +00:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
2022-04-01 23:50:58 +00:00
|
|
|
|
|
|
|
func (ms *MasterServer) Ping(ctx context.Context, req *master_pb.PingRequest) (resp *master_pb.PingResponse, pingErr error) {
|
2022-04-16 19:45:49 +00:00
|
|
|
resp = &master_pb.PingResponse{
|
|
|
|
StartTimeNs: time.Now().UnixNano(),
|
|
|
|
}
|
2022-04-01 23:55:26 +00:00
|
|
|
if req.TargetType == cluster.FilerType {
|
2022-04-01 23:50:58 +00:00
|
|
|
pingErr = pb.WithFilerClient(false, pb.ServerAddress(req.Target), ms.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2022-04-16 19:45:49 +00:00
|
|
|
pingResp, err := client.Ping(ctx, &filer_pb.PingRequest{})
|
|
|
|
if pingResp != nil {
|
|
|
|
resp.RemoteTimeNs = pingResp.StartTimeNs
|
|
|
|
}
|
2022-04-01 23:50:58 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2022-04-01 23:55:26 +00:00
|
|
|
if req.TargetType == cluster.VolumeServerType {
|
2022-04-01 23:50:58 +00:00
|
|
|
pingErr = pb.WithVolumeServerClient(false, pb.ServerAddress(req.Target), ms.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
|
2022-04-16 19:45:49 +00:00
|
|
|
pingResp, err := client.Ping(ctx, &volume_server_pb.PingRequest{})
|
|
|
|
if pingResp != nil {
|
|
|
|
resp.RemoteTimeNs = pingResp.StartTimeNs
|
|
|
|
}
|
2022-04-01 23:50:58 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2022-04-01 23:55:26 +00:00
|
|
|
if req.TargetType == cluster.MasterType {
|
2022-04-01 23:50:58 +00:00
|
|
|
pingErr = pb.WithMasterClient(false, pb.ServerAddress(req.Target), ms.grpcDialOption, func(client master_pb.SeaweedClient) error {
|
2022-04-16 19:45:49 +00:00
|
|
|
pingResp, err := client.Ping(ctx, &master_pb.PingRequest{})
|
|
|
|
if pingResp != nil {
|
|
|
|
resp.RemoteTimeNs = pingResp.StartTimeNs
|
|
|
|
}
|
2022-04-01 23:50:58 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if pingErr != nil {
|
|
|
|
pingErr = fmt.Errorf("ping %s %s: %v", req.TargetType, req.Target, pingErr)
|
|
|
|
}
|
2022-04-16 19:45:49 +00:00
|
|
|
resp.StopTimeNs = time.Now().UnixNano()
|
2022-04-01 23:50:58 +00:00
|
|
|
return
|
|
|
|
}
|