2016-07-31 17:11:14 -04:00
|
|
|
/*
|
2017-03-18 14:28:41 -04:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2016-07-31 17:11:14 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 17:50:50 -04:00
|
|
|
package cmd
|
2016-07-31 17:11:14 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-05 18:04:40 -04:00
|
|
|
"context"
|
2016-07-31 17:11:14 -04:00
|
|
|
"fmt"
|
2016-09-16 03:30:55 -04:00
|
|
|
"math/rand"
|
2016-07-31 17:11:14 -04:00
|
|
|
"sync"
|
2016-08-22 14:01:21 -04:00
|
|
|
"time"
|
2016-07-31 17:11:14 -04:00
|
|
|
|
|
|
|
router "github.com/gorilla/mux"
|
2016-12-23 10:12:19 -05:00
|
|
|
"github.com/minio/dsync"
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2016-07-31 17:11:14 -04:00
|
|
|
)
|
|
|
|
|
2016-12-23 10:12:19 -05:00
|
|
|
const (
|
|
|
|
// Lock rpc server endpoint.
|
2017-04-11 13:25:21 -04:00
|
|
|
lockServicePath = "/lock"
|
|
|
|
|
|
|
|
// Lock rpc service name.
|
|
|
|
lockServiceName = "Dsync"
|
2016-07-31 17:11:14 -04:00
|
|
|
|
2016-12-23 10:12:19 -05:00
|
|
|
// Lock maintenance interval.
|
|
|
|
lockMaintenanceInterval = 1 * time.Minute // 1 minute.
|
2016-08-22 14:01:21 -04:00
|
|
|
|
2016-12-23 10:12:19 -05:00
|
|
|
// Lock validity check interval.
|
|
|
|
lockValidityCheckInterval = 2 * time.Minute // 2 minutes.
|
|
|
|
)
|
2016-08-22 14:01:21 -04:00
|
|
|
|
2017-04-11 13:25:21 -04:00
|
|
|
// lockRequesterInfo stores various info from the client for each lock that is requested.
|
2016-09-16 03:30:55 -04:00
|
|
|
type lockRequesterInfo struct {
|
2017-04-11 13:25:21 -04:00
|
|
|
writer bool // Bool whether write or read lock.
|
|
|
|
node string // Network address of client claiming lock.
|
|
|
|
serviceEndpoint string // RPC path of client claiming lock.
|
|
|
|
uid string // UID to uniquely identify request of client.
|
|
|
|
timestamp time.Time // Timestamp set at the time of initialization.
|
|
|
|
timeLastCheck time.Time // Timestamp for last check of validity of lock.
|
2016-09-16 03:30:55 -04:00
|
|
|
}
|
|
|
|
|
2017-04-11 13:25:21 -04:00
|
|
|
// isWriteLock returns whether the lock is a write or read lock.
|
2016-09-16 03:30:55 -04:00
|
|
|
func isWriteLock(lri []lockRequesterInfo) bool {
|
|
|
|
return len(lri) == 1 && lri[0].writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// lockServer is type for RPC handlers
|
2016-07-31 17:11:14 -04:00
|
|
|
type lockServer struct {
|
2016-12-23 10:12:19 -05:00
|
|
|
AuthRPCServer
|
2017-06-05 15:25:04 -04:00
|
|
|
ll localLocker
|
2016-08-22 14:01:21 -04:00
|
|
|
}
|
|
|
|
|
2016-12-23 10:12:19 -05:00
|
|
|
// Start lock maintenance from all lock servers.
|
2018-01-22 13:25:10 -05:00
|
|
|
func startLockMaintenance(lkSrv *lockServer) {
|
|
|
|
// Start loop for stale lock maintenance
|
|
|
|
go func(lk *lockServer) {
|
|
|
|
// Initialize a new ticker with a minute between each ticks.
|
|
|
|
ticker := time.NewTicker(lockMaintenanceInterval)
|
|
|
|
|
|
|
|
// Start with random sleep time, so as to avoid "synchronous checks" between servers
|
|
|
|
time.Sleep(time.Duration(rand.Float64() * float64(lockMaintenanceInterval)))
|
|
|
|
for {
|
|
|
|
// Verifies every minute for locks held more than 2minutes.
|
|
|
|
select {
|
|
|
|
case <-globalServiceDoneCh:
|
|
|
|
// Stop the timer upon service closure and cleanup the go-routine.
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
lk.lockMaintenance(lockValidityCheckInterval)
|
2016-12-23 10:12:19 -05:00
|
|
|
}
|
2018-01-22 13:25:10 -05:00
|
|
|
}
|
|
|
|
}(lkSrv)
|
2016-12-23 10:12:19 -05:00
|
|
|
}
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
// Register distributed NS lock handlers.
|
2017-04-11 18:44:27 -04:00
|
|
|
func registerDistNSLockRouter(mux *router.Router, endpoints EndpointList) error {
|
2016-12-23 10:12:19 -05:00
|
|
|
// Start lock maintenance from all lock servers.
|
2018-01-22 13:25:10 -05:00
|
|
|
startLockMaintenance(globalLockServer)
|
2016-12-23 10:12:19 -05:00
|
|
|
|
|
|
|
// Register initialized lock servers to their respective rpc endpoints.
|
2018-01-22 13:25:10 -05:00
|
|
|
return registerStorageLockers(mux, globalLockServer)
|
2016-09-19 16:14:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// registerStorageLockers - register locker rpc handlers for net/rpc library clients
|
2018-01-22 13:25:10 -05:00
|
|
|
func registerStorageLockers(mux *router.Router, lkSrv *lockServer) error {
|
|
|
|
lockRPCServer := newRPCServer()
|
|
|
|
if err := lockRPCServer.RegisterName(lockServiceName, lkSrv); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
return err
|
2016-08-23 22:19:24 -04:00
|
|
|
}
|
2018-01-22 13:25:10 -05:00
|
|
|
lockRouter := mux.PathPrefix(minioReservedBucketPath).Subrouter()
|
|
|
|
lockRouter.Path(lockServicePath).Handler(lockRPCServer)
|
2016-10-13 02:13:24 -04:00
|
|
|
return nil
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
|
|
|
|
2017-06-05 15:25:04 -04:00
|
|
|
// localLocker implements Dsync.NetLocker
|
|
|
|
type localLocker struct {
|
|
|
|
mutex sync.Mutex
|
|
|
|
serviceEndpoint string
|
|
|
|
serverAddr string
|
|
|
|
lockMap map[string][]lockRequesterInfo
|
|
|
|
}
|
2016-07-31 17:11:14 -04:00
|
|
|
|
2017-06-05 15:25:04 -04:00
|
|
|
func (l *localLocker) ServerAddr() string {
|
|
|
|
return l.serverAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *localLocker) ServiceEndpoint() string {
|
|
|
|
return l.serviceEndpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *localLocker) Lock(args dsync.LockArgs) (reply bool, err error) {
|
2016-07-31 17:11:14 -04:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
2017-06-05 15:25:04 -04:00
|
|
|
_, isLockTaken := l.lockMap[args.Resource]
|
|
|
|
if !isLockTaken { // No locks held on the given name, so claim write lock
|
|
|
|
l.lockMap[args.Resource] = []lockRequesterInfo{
|
2016-09-19 16:14:55 -04:00
|
|
|
{
|
2017-04-11 13:25:21 -04:00
|
|
|
writer: true,
|
2017-06-05 15:25:04 -04:00
|
|
|
node: args.ServerAddr,
|
|
|
|
serviceEndpoint: args.ServiceEndpoint,
|
|
|
|
uid: args.UID,
|
2017-04-11 13:25:21 -04:00
|
|
|
timestamp: UTCNow(),
|
|
|
|
timeLastCheck: UTCNow(),
|
2016-09-19 16:14:55 -04:00
|
|
|
},
|
|
|
|
}
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
// return reply=true if lock was granted.
|
|
|
|
return !isLockTaken, nil
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
|
|
|
|
2017-06-05 15:25:04 -04:00
|
|
|
func (l *localLocker) Unlock(args dsync.LockArgs) (reply bool, err error) {
|
2016-07-31 17:11:14 -04:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
2016-09-16 03:30:55 -04:00
|
|
|
var lri []lockRequesterInfo
|
2017-06-05 15:25:04 -04:00
|
|
|
if lri, reply = l.lockMap[args.Resource]; !reply {
|
|
|
|
// No lock is held on the given name
|
|
|
|
return reply, fmt.Errorf("Unlock attempted on an unlocked entity: %s", args.Resource)
|
2016-09-16 03:30:55 -04:00
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
if reply = isWriteLock(lri); !reply {
|
|
|
|
// Unless it is a write lock
|
|
|
|
return reply, fmt.Errorf("Unlock attempted on a read locked entity: %s (%d read locks active)", args.Resource, len(lri))
|
2016-09-16 03:30:55 -04:00
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
if !l.removeEntry(args.Resource, args.UID, &lri) {
|
|
|
|
return false, fmt.Errorf("Unlock unable to find corresponding lock for uid: %s", args.UID)
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
return true, nil
|
|
|
|
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
|
|
|
|
2017-06-05 15:25:04 -04:00
|
|
|
func (l *localLocker) RLock(args dsync.LockArgs) (reply bool, err error) {
|
2016-08-14 19:57:01 -04:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
2016-09-19 16:14:55 -04:00
|
|
|
lrInfo := lockRequesterInfo{
|
2017-04-11 13:25:21 -04:00
|
|
|
writer: false,
|
2017-06-05 15:25:04 -04:00
|
|
|
node: args.ServerAddr,
|
|
|
|
serviceEndpoint: args.ServiceEndpoint,
|
|
|
|
uid: args.UID,
|
2017-04-11 13:25:21 -04:00
|
|
|
timestamp: UTCNow(),
|
|
|
|
timeLastCheck: UTCNow(),
|
2016-09-19 16:14:55 -04:00
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
if lri, ok := l.lockMap[args.Resource]; ok {
|
|
|
|
if reply = !isWriteLock(lri); reply {
|
|
|
|
// Unless there is a write lock
|
|
|
|
l.lockMap[args.Resource] = append(l.lockMap[args.Resource], lrInfo)
|
2016-09-16 03:30:55 -04:00
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
} else {
|
|
|
|
// No locks held on the given name, so claim (first) read lock
|
|
|
|
l.lockMap[args.Resource] = []lockRequesterInfo{lrInfo}
|
|
|
|
reply = true
|
2016-08-14 19:57:01 -04:00
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
return reply, nil
|
2016-08-14 19:57:01 -04:00
|
|
|
}
|
|
|
|
|
2017-06-05 15:25:04 -04:00
|
|
|
func (l *localLocker) RUnlock(args dsync.LockArgs) (reply bool, err error) {
|
2016-08-14 19:57:01 -04:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
2016-09-16 03:30:55 -04:00
|
|
|
var lri []lockRequesterInfo
|
2017-06-05 15:25:04 -04:00
|
|
|
if lri, reply = l.lockMap[args.Resource]; !reply {
|
|
|
|
// No lock is held on the given name
|
|
|
|
return reply, fmt.Errorf("RUnlock attempted on an unlocked entity: %s", args.Resource)
|
2016-09-16 03:30:55 -04:00
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
if reply = !isWriteLock(lri); !reply {
|
|
|
|
// A write-lock is held, cannot release a read lock
|
|
|
|
return reply, fmt.Errorf("RUnlock attempted on a write locked entity: %s", args.Resource)
|
2016-08-14 19:57:01 -04:00
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
if !l.removeEntry(args.Resource, args.UID, &lri) {
|
|
|
|
return false, fmt.Errorf("RUnlock unable to find corresponding read lock for uid: %s", args.UID)
|
2016-09-16 03:30:55 -04:00
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
return reply, nil
|
2016-09-16 03:30:55 -04:00
|
|
|
}
|
|
|
|
|
2017-06-05 15:25:04 -04:00
|
|
|
func (l *localLocker) ForceUnlock(args dsync.LockArgs) (reply bool, err error) {
|
2016-10-17 04:53:29 -04:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
2017-06-05 15:25:04 -04:00
|
|
|
if len(args.UID) != 0 {
|
|
|
|
return false, fmt.Errorf("ForceUnlock called with non-empty UID: %s", args.UID)
|
|
|
|
}
|
|
|
|
if _, ok := l.lockMap[args.Resource]; ok {
|
|
|
|
// Only clear lock when it is taken
|
|
|
|
// Remove the lock (irrespective of write or read lock)
|
|
|
|
delete(l.lockMap, args.Resource)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Distributed lock handlers
|
|
|
|
|
|
|
|
// Lock - rpc handler for (single) write lock operation.
|
|
|
|
func (l *lockServer) Lock(args *LockArgs, reply *bool) (err error) {
|
|
|
|
if err = args.IsAuthenticated(); err != nil {
|
2016-10-17 04:53:29 -04:00
|
|
|
return err
|
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
*reply, err = l.ll.Lock(args.LockArgs)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock - rpc handler for (single) write unlock operation.
|
|
|
|
func (l *lockServer) Unlock(args *LockArgs, reply *bool) (err error) {
|
|
|
|
if err = args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-10-17 04:53:29 -04:00
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
*reply, err = l.ll.Unlock(args.LockArgs)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RLock - rpc handler for read lock operation.
|
|
|
|
func (l *lockServer) RLock(args *LockArgs, reply *bool) (err error) {
|
|
|
|
if err = args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-10-17 04:53:29 -04:00
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
*reply, err = l.ll.RLock(args.LockArgs)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RUnlock - rpc handler for read unlock operation.
|
|
|
|
func (l *lockServer) RUnlock(args *LockArgs, reply *bool) (err error) {
|
|
|
|
if err = args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*reply, err = l.ll.RUnlock(args.LockArgs)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForceUnlock - rpc handler for force unlock operation.
|
|
|
|
func (l *lockServer) ForceUnlock(args *LockArgs, reply *bool) (err error) {
|
|
|
|
if err = args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*reply, err = l.ll.ForceUnlock(args.LockArgs)
|
|
|
|
return err
|
2016-10-17 04:53:29 -04:00
|
|
|
}
|
|
|
|
|
2016-09-19 16:14:55 -04:00
|
|
|
// Expired - rpc handler for expired lock status.
|
|
|
|
func (l *lockServer) Expired(args *LockArgs, reply *bool) error {
|
2016-12-23 10:12:19 -05:00
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
2016-09-16 03:30:55 -04:00
|
|
|
return err
|
|
|
|
}
|
2017-06-05 15:25:04 -04:00
|
|
|
l.ll.mutex.Lock()
|
|
|
|
defer l.ll.mutex.Unlock()
|
2016-09-19 16:14:55 -04:00
|
|
|
// Lock found, proceed to verify if belongs to given uid.
|
2017-06-05 15:25:04 -04:00
|
|
|
if lri, ok := l.ll.lockMap[args.LockArgs.Resource]; ok {
|
2016-09-19 16:14:55 -04:00
|
|
|
// Check whether uid is still active
|
|
|
|
for _, entry := range lri {
|
2017-01-08 23:37:53 -05:00
|
|
|
if entry.uid == args.LockArgs.UID {
|
2016-09-19 16:14:55 -04:00
|
|
|
*reply = false // When uid found, lock is still active so return not expired.
|
|
|
|
return nil // When uid found *reply is set to true.
|
2016-09-16 03:30:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 23:37:53 -05:00
|
|
|
// When we get here lock is no longer active due to either args.LockArgs.Resource
|
|
|
|
// being absent from map or uid not found for given args.LockArgs.Resource
|
2016-09-19 16:14:55 -04:00
|
|
|
*reply = true
|
|
|
|
return nil
|
2016-09-16 03:30:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// nameLockRequesterInfoPair is a helper type for lock maintenance
|
|
|
|
type nameLockRequesterInfoPair struct {
|
|
|
|
name string
|
|
|
|
lri lockRequesterInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// lockMaintenance loops over locks that have been active for some time and checks back
|
|
|
|
// with the original server whether it is still alive or not
|
2016-09-19 16:14:55 -04:00
|
|
|
//
|
|
|
|
// Following logic inside ignores the errors generated for Dsync.Active operation.
|
|
|
|
// - server at client down
|
|
|
|
// - some network error (and server is up normally)
|
|
|
|
//
|
|
|
|
// We will ignore the error, and we will retry later to get a resolve on this lock
|
2016-09-16 03:30:55 -04:00
|
|
|
func (l *lockServer) lockMaintenance(interval time.Duration) {
|
2017-06-05 15:25:04 -04:00
|
|
|
l.ll.mutex.Lock()
|
2016-09-19 16:14:55 -04:00
|
|
|
// Get list of long lived locks to check for staleness.
|
2017-06-05 15:25:04 -04:00
|
|
|
nlripLongLived := getLongLivedLocks(l.ll.lockMap, interval)
|
|
|
|
l.ll.mutex.Unlock()
|
2016-09-16 03:30:55 -04:00
|
|
|
|
2017-11-29 16:12:47 -05:00
|
|
|
serverCred := globalServerConfig.GetCredential()
|
2016-09-19 16:14:55 -04:00
|
|
|
// Validate if long lived locks are indeed clean.
|
2016-09-16 03:30:55 -04:00
|
|
|
for _, nlrip := range nlripLongLived {
|
2016-09-19 16:14:55 -04:00
|
|
|
// Initialize client based on the long live locks.
|
2016-12-23 10:12:19 -05:00
|
|
|
c := newLockRPCClient(authConfig{
|
|
|
|
accessKey: serverCred.AccessKey,
|
|
|
|
secretKey: serverCred.SecretKey,
|
|
|
|
serverAddr: nlrip.lri.node,
|
2017-01-11 16:59:51 -05:00
|
|
|
secureConn: globalIsSSL,
|
2017-04-11 13:25:21 -04:00
|
|
|
serviceEndpoint: nlrip.lri.serviceEndpoint,
|
|
|
|
serviceName: lockServiceName,
|
2016-12-23 10:12:19 -05:00
|
|
|
})
|
2016-09-16 03:30:55 -04:00
|
|
|
|
|
|
|
// Call back to original server verify whether the lock is still active (based on name & uid)
|
2017-04-11 13:25:21 -04:00
|
|
|
expired, _ := c.Expired(dsync.LockArgs{
|
|
|
|
UID: nlrip.lri.uid,
|
|
|
|
Resource: nlrip.name,
|
|
|
|
})
|
2016-12-23 10:12:19 -05:00
|
|
|
|
|
|
|
// Close the connection regardless of the call response.
|
2018-02-15 20:45:57 -05:00
|
|
|
c.AuthRPCClient.Close()
|
2016-09-19 16:14:55 -04:00
|
|
|
|
|
|
|
// For successful response, verify if lock is indeed active or stale.
|
|
|
|
if expired {
|
|
|
|
// The lock is no longer active at server that originated the lock
|
|
|
|
// So remove the lock from the map.
|
2017-06-05 15:25:04 -04:00
|
|
|
l.ll.mutex.Lock()
|
|
|
|
l.ll.removeEntryIfExists(nlrip) // Purge the stale entry if it exists.
|
|
|
|
l.ll.mutex.Unlock()
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|