2016-04-29 04:29:09 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2016, 2017, 2018, 2019 MinIO, Inc.
|
2016-04-29 04:29:09 -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 19:23:42 -04:00
|
|
|
package cmd
|
2016-04-29 04:29:09 -04:00
|
|
|
|
2016-04-29 14:39:20 -04:00
|
|
|
import (
|
2018-04-05 18:04:40 -04:00
|
|
|
"context"
|
2016-05-16 17:31:28 -04:00
|
|
|
"errors"
|
2016-08-23 22:19:24 -04:00
|
|
|
pathutil "path"
|
2018-01-17 10:24:46 -05:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2016-04-29 14:39:20 -04:00
|
|
|
"sync"
|
2016-08-10 16:08:11 -04:00
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
"fmt"
|
2017-10-13 06:01:15 -04:00
|
|
|
"time"
|
|
|
|
|
2016-08-10 16:08:11 -04:00
|
|
|
"github.com/minio/dsync"
|
2017-08-31 14:29:22 -04:00
|
|
|
"github.com/minio/lsync"
|
2018-01-22 13:25:10 -05:00
|
|
|
"github.com/minio/minio-go/pkg/set"
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2018-06-06 04:51:56 -04:00
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
2016-04-29 14:39:20 -04:00
|
|
|
)
|
2016-04-29 04:29:09 -04:00
|
|
|
|
2016-08-10 16:08:11 -04:00
|
|
|
// Global name space lock.
|
2016-12-10 19:15:12 -05:00
|
|
|
var globalNSMutex *nsLockMap
|
2016-08-10 16:08:11 -04:00
|
|
|
|
2018-01-22 13:25:10 -05:00
|
|
|
// Global lock server one per server.
|
2019-04-18 02:16:27 -04:00
|
|
|
var globalLockServer *lockRESTServer
|
2018-01-22 13:25:10 -05:00
|
|
|
|
|
|
|
// Instance of dsync for distributed clients.
|
|
|
|
var globalDsync *dsync.Dsync
|
2017-06-05 15:25:04 -04:00
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
// RWLocker - locker interface to introduce GetRLock, RUnlock.
|
2017-01-16 20:05:00 -05:00
|
|
|
type RWLocker interface {
|
2017-08-31 14:29:22 -04:00
|
|
|
GetLock(timeout *dynamicTimeout) (timedOutErr error)
|
|
|
|
Unlock()
|
|
|
|
GetRLock(timeout *dynamicTimeout) (timedOutErr error)
|
|
|
|
RUnlock()
|
|
|
|
}
|
|
|
|
|
2016-08-10 16:08:11 -04:00
|
|
|
// Initialize distributed locking only in case of distributed setup.
|
2017-06-05 15:25:04 -04:00
|
|
|
// Returns lock clients and the node index for the current server.
|
|
|
|
func newDsyncNodes(endpoints EndpointList) (clnts []dsync.NetLocker, myNode int) {
|
|
|
|
myNode = -1
|
2018-01-22 13:25:10 -05:00
|
|
|
seenHosts := set.NewStringSet()
|
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
if seenHosts.Contains(endpoint.Host) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seenHosts.Add(endpoint.Host)
|
2017-06-05 15:25:04 -04:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
var locker dsync.NetLocker
|
|
|
|
if endpoint.IsLocal {
|
|
|
|
myNode = len(clnts)
|
|
|
|
|
2019-04-18 02:16:27 -04:00
|
|
|
receiver := &lockRESTServer{
|
2018-06-06 04:51:56 -04:00
|
|
|
ll: localLocker{
|
|
|
|
serverAddr: endpoint.Host,
|
|
|
|
serviceEndpoint: lockServicePath,
|
|
|
|
lockMap: make(map[string][]lockRequesterInfo),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
globalLockServer = receiver
|
|
|
|
locker = &(receiver.ll)
|
|
|
|
} else {
|
|
|
|
host, err := xnet.ParseHost(endpoint.Host)
|
2018-09-21 19:04:11 -04:00
|
|
|
logger.FatalIf(err, "Unable to parse Lock RPC Host")
|
2019-04-18 02:16:27 -04:00
|
|
|
locker = newlockRESTClient(host)
|
2017-06-05 15:25:04 -04:00
|
|
|
}
|
2018-02-15 20:45:57 -05:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
clnts = append(clnts, locker)
|
2016-08-10 16:08:11 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2017-06-05 15:25:04 -04:00
|
|
|
return clnts, myNode
|
2016-08-10 16:08:11 -04:00
|
|
|
}
|
|
|
|
|
2018-01-12 23:34:52 -05:00
|
|
|
// newNSLock - return a new name space lock map.
|
|
|
|
func newNSLock(isDistXL bool) *nsLockMap {
|
|
|
|
nsMutex := nsLockMap{
|
2016-12-10 19:15:12 -05:00
|
|
|
isDistXL: isDistXL,
|
2016-08-10 16:08:11 -04:00
|
|
|
}
|
2019-04-02 15:27:20 -04:00
|
|
|
if isDistXL {
|
|
|
|
return &nsMutex
|
|
|
|
}
|
|
|
|
nsMutex.lockMap = make(map[nsParam]*nsLock)
|
2018-01-12 23:34:52 -05:00
|
|
|
return &nsMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// initNSLock - initialize name space lock map.
|
|
|
|
func initNSLock(isDistXL bool) {
|
|
|
|
globalNSMutex = newNSLock(isDistXL)
|
2016-08-31 14:39:08 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 04:29:09 -04:00
|
|
|
// nsParam - carries name space resource.
|
|
|
|
type nsParam struct {
|
|
|
|
volume string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsLock - provides primitives for locking critical namespace regions.
|
|
|
|
type nsLock struct {
|
2019-04-02 15:27:20 -04:00
|
|
|
*lsync.LRWMutex
|
2016-09-16 03:30:55 -04:00
|
|
|
ref uint
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsLockMap - namespace lock map, provides primitives to Lock,
|
|
|
|
// Unlock, RLock and RUnlock.
|
|
|
|
type nsLockMap struct {
|
2017-01-16 20:05:00 -05:00
|
|
|
// Indicates if namespace is part of a distributed setup.
|
2016-12-10 19:15:12 -05:00
|
|
|
isDistXL bool
|
2016-08-10 16:08:11 -04:00
|
|
|
lockMap map[nsParam]*nsLock
|
2018-08-05 23:25:25 -04:00
|
|
|
lockMapMutex sync.RWMutex
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 14:39:20 -04:00
|
|
|
// Lock the namespace resource.
|
2017-08-31 14:29:22 -04:00
|
|
|
func (n *nsLockMap) lock(volume, path string, lockSource, opsID string, readLock bool, timeout time.Duration) (locked bool) {
|
2016-08-10 16:08:11 -04:00
|
|
|
var nsLk *nsLock
|
2016-04-29 04:29:09 -04:00
|
|
|
|
2018-08-05 23:25:25 -04:00
|
|
|
n.lockMapMutex.Lock()
|
2016-04-29 04:29:09 -04:00
|
|
|
param := nsParam{volume, path}
|
|
|
|
nsLk, found := n.lockMap[param]
|
|
|
|
if !found {
|
2018-08-05 23:25:25 -04:00
|
|
|
n.lockMap[param] = &nsLock{
|
2019-04-02 15:27:20 -04:00
|
|
|
LRWMutex: &lsync.LRWMutex{},
|
|
|
|
ref: 1,
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|
2018-08-05 23:25:25 -04:00
|
|
|
nsLk = n.lockMap[param]
|
|
|
|
} else {
|
|
|
|
// Update ref count here to avoid multiple races.
|
|
|
|
nsLk.ref++
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|
2016-08-10 16:08:11 -04:00
|
|
|
n.lockMapMutex.Unlock()
|
2016-04-29 14:39:20 -04:00
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
// Locking here will block (until timeout).
|
2016-04-29 14:39:20 -04:00
|
|
|
if readLock {
|
2019-01-24 10:22:14 -05:00
|
|
|
locked = nsLk.GetRLock(opsID, lockSource, timeout)
|
2016-04-29 14:39:20 -04:00
|
|
|
} else {
|
2019-01-24 10:22:14 -05:00
|
|
|
locked = nsLk.GetLock(opsID, lockSource, timeout)
|
2017-08-31 14:29:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !locked { // We failed to get the lock
|
2018-04-05 18:04:40 -04:00
|
|
|
|
2018-08-05 23:25:25 -04:00
|
|
|
// Decrement ref count since we failed to get the lock
|
|
|
|
n.lockMapMutex.Lock()
|
|
|
|
nsLk.ref--
|
2017-08-31 14:29:22 -04:00
|
|
|
if nsLk.ref == 0 {
|
|
|
|
// Remove from the map if there are no more references.
|
|
|
|
delete(n.lockMap, param)
|
|
|
|
}
|
2018-08-05 23:25:25 -04:00
|
|
|
n.lockMapMutex.Unlock()
|
2016-04-29 14:39:20 -04:00
|
|
|
}
|
2017-08-31 14:29:22 -04:00
|
|
|
return
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 14:39:20 -04:00
|
|
|
// Unlock the namespace resource.
|
2016-08-31 14:39:08 -04:00
|
|
|
func (n *nsLockMap) unlock(volume, path, opsID string, readLock bool) {
|
2016-04-29 04:29:09 -04:00
|
|
|
param := nsParam{volume, path}
|
2018-08-05 23:25:25 -04:00
|
|
|
n.lockMapMutex.RLock()
|
|
|
|
nsLk, found := n.lockMap[param]
|
|
|
|
n.lockMapMutex.RUnlock()
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if readLock {
|
|
|
|
nsLk.RUnlock()
|
|
|
|
} else {
|
|
|
|
nsLk.Unlock()
|
|
|
|
}
|
|
|
|
n.lockMapMutex.Lock()
|
|
|
|
if nsLk.ref == 0 {
|
|
|
|
logger.LogIf(context.Background(), errors.New("Namespace reference count cannot be 0"))
|
|
|
|
} else {
|
|
|
|
nsLk.ref--
|
2016-04-29 14:39:20 -04:00
|
|
|
if nsLk.ref == 0 {
|
|
|
|
// Remove from the map if there are no more references.
|
|
|
|
delete(n.lockMap, param)
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|
|
|
|
}
|
2018-08-05 23:25:25 -04:00
|
|
|
n.lockMapMutex.Unlock()
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 14:39:20 -04:00
|
|
|
// Lock - locks the given resource for writes, using a previously
|
|
|
|
// allocated name space lock or initializing a new one.
|
2017-08-31 14:29:22 -04:00
|
|
|
func (n *nsLockMap) Lock(volume, path, opsID string, timeout time.Duration) (locked bool) {
|
2016-10-11 03:50:27 -04:00
|
|
|
readLock := false // This is a write lock.
|
2016-09-27 17:35:43 -04:00
|
|
|
|
2017-03-23 19:36:00 -04:00
|
|
|
lockSource := getSource() // Useful for debugging
|
2017-08-31 14:29:22 -04:00
|
|
|
return n.lock(volume, path, lockSource, opsID, readLock, timeout)
|
2016-04-29 14:39:20 -04:00
|
|
|
}
|
2016-04-29 04:29:09 -04:00
|
|
|
|
2016-04-29 14:39:20 -04:00
|
|
|
// Unlock - unlocks any previously acquired write locks.
|
2016-08-31 14:39:08 -04:00
|
|
|
func (n *nsLockMap) Unlock(volume, path, opsID string) {
|
2016-04-29 14:39:20 -04:00
|
|
|
readLock := false
|
2016-08-31 14:39:08 -04:00
|
|
|
n.unlock(volume, path, opsID, readLock)
|
2016-04-29 14:39:20 -04:00
|
|
|
}
|
2016-04-29 04:29:09 -04:00
|
|
|
|
2016-04-29 14:39:20 -04:00
|
|
|
// RLock - locks any previously acquired read locks.
|
2017-08-31 14:29:22 -04:00
|
|
|
func (n *nsLockMap) RLock(volume, path, opsID string, timeout time.Duration) (locked bool) {
|
2016-04-29 14:39:20 -04:00
|
|
|
readLock := true
|
2016-09-27 17:35:43 -04:00
|
|
|
|
2017-03-23 19:36:00 -04:00
|
|
|
lockSource := getSource() // Useful for debugging
|
2017-08-31 14:29:22 -04:00
|
|
|
return n.lock(volume, path, lockSource, opsID, readLock, timeout)
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RUnlock - unlocks any previously acquired read locks.
|
2016-08-31 14:39:08 -04:00
|
|
|
func (n *nsLockMap) RUnlock(volume, path, opsID string) {
|
2016-04-29 14:39:20 -04:00
|
|
|
readLock := true
|
2016-08-31 14:39:08 -04:00
|
|
|
n.unlock(volume, path, opsID, readLock)
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|
2016-10-19 12:27:36 -04:00
|
|
|
|
|
|
|
// ForceUnlock - forcefully unlock a lock based on name.
|
|
|
|
func (n *nsLockMap) ForceUnlock(volume, path string) {
|
|
|
|
n.lockMapMutex.Lock()
|
|
|
|
defer n.lockMapMutex.Unlock()
|
|
|
|
|
2016-10-20 16:15:28 -04:00
|
|
|
// Clarification on operation:
|
2016-12-10 19:15:12 -05:00
|
|
|
// - In case of FS or XL we call ForceUnlock on the local globalNSMutex
|
2016-10-20 16:15:28 -04:00
|
|
|
// (since there is only a single server) which will cause the 'stuck'
|
|
|
|
// mutex to be removed from the map. Existing operations for this
|
|
|
|
// will continue to be blocked (and timeout). New operations on this
|
|
|
|
// resource will use a new mutex and proceed normally.
|
|
|
|
//
|
|
|
|
// - In case of Distributed setup (using dsync), there is no need to call
|
|
|
|
// ForceUnlock on the server where the lock was acquired and is presumably
|
|
|
|
// 'stuck'. Instead dsync.ForceUnlock() will release the underlying locks
|
|
|
|
// that participated in granting the lock. Any pending dsync locks that
|
|
|
|
// are blocking can now proceed as normal and any new locks will also
|
|
|
|
// participate normally.
|
2016-12-10 19:15:12 -05:00
|
|
|
if n.isDistXL { // For distributed mode, broadcast ForceUnlock message.
|
2018-01-22 13:25:10 -05:00
|
|
|
dsync.NewDRWMutex(pathJoin(volume, path), globalDsync).ForceUnlock()
|
2016-10-19 12:27:36 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 07:59:36 -05:00
|
|
|
// Remove lock from the map.
|
|
|
|
delete(n.lockMap, nsParam{volume, path})
|
2016-10-19 12:27:36 -04:00
|
|
|
}
|
2016-11-09 13:58:41 -05:00
|
|
|
|
2019-04-02 15:27:20 -04:00
|
|
|
// dsync's distributed lock instance.
|
|
|
|
type distLockInstance struct {
|
|
|
|
rwMutex *dsync.DRWMutex
|
|
|
|
volume, path, opsID string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock - block until write lock is taken or timeout has occurred.
|
|
|
|
func (di *distLockInstance) GetLock(timeout *dynamicTimeout) (timedOutErr error) {
|
|
|
|
lockSource := getSource()
|
|
|
|
start := UTCNow()
|
|
|
|
|
|
|
|
if !di.rwMutex.GetLock(di.opsID, lockSource, timeout.Timeout()) {
|
|
|
|
timeout.LogFailure()
|
|
|
|
return OperationTimedOut{Path: di.path}
|
|
|
|
}
|
|
|
|
timeout.LogSuccess(UTCNow().Sub(start))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock - block until write lock is released.
|
|
|
|
func (di *distLockInstance) Unlock() {
|
|
|
|
di.rwMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RLock - block until read lock is taken or timeout has occurred.
|
|
|
|
func (di *distLockInstance) GetRLock(timeout *dynamicTimeout) (timedOutErr error) {
|
|
|
|
lockSource := getSource()
|
|
|
|
start := UTCNow()
|
|
|
|
if !di.rwMutex.GetRLock(di.opsID, lockSource, timeout.Timeout()) {
|
|
|
|
timeout.LogFailure()
|
|
|
|
return OperationTimedOut{Path: di.path}
|
|
|
|
}
|
|
|
|
timeout.LogSuccess(UTCNow().Sub(start))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RUnlock - block until read lock is released.
|
|
|
|
func (di *distLockInstance) RUnlock() {
|
|
|
|
di.rwMutex.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// localLockInstance - frontend/top-level interface for namespace locks.
|
|
|
|
type localLockInstance struct {
|
2017-01-16 20:05:00 -05:00
|
|
|
ns *nsLockMap
|
2016-11-09 13:58:41 -05:00
|
|
|
volume, path, opsID string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNSLock - returns a lock instance for a given volume and
|
|
|
|
// path. The returned lockInstance object encapsulates the nsLockMap,
|
|
|
|
// volume, path and operation ID.
|
2017-01-16 20:05:00 -05:00
|
|
|
func (n *nsLockMap) NewNSLock(volume, path string) RWLocker {
|
2018-08-02 13:39:42 -04:00
|
|
|
opsID := mustGetUUID()
|
2019-04-02 15:27:20 -04:00
|
|
|
if n.isDistXL {
|
|
|
|
return &distLockInstance{dsync.NewDRWMutex(pathJoin(volume, path), globalDsync), volume, path, opsID}
|
|
|
|
}
|
|
|
|
return &localLockInstance{n, volume, path, opsID}
|
2016-11-09 13:58:41 -05:00
|
|
|
}
|
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
// Lock - block until write lock is taken or timeout has occurred.
|
2019-04-02 15:27:20 -04:00
|
|
|
func (li *localLockInstance) GetLock(timeout *dynamicTimeout) (timedOutErr error) {
|
2017-03-23 19:36:00 -04:00
|
|
|
lockSource := getSource()
|
2017-08-31 14:29:22 -04:00
|
|
|
start := UTCNow()
|
2016-11-09 13:58:41 -05:00
|
|
|
readLock := false
|
2017-08-31 14:29:22 -04:00
|
|
|
if !li.ns.lock(li.volume, li.path, lockSource, li.opsID, readLock, timeout.Timeout()) {
|
|
|
|
timeout.LogFailure()
|
|
|
|
return OperationTimedOut{Path: li.path}
|
|
|
|
}
|
|
|
|
timeout.LogSuccess(UTCNow().Sub(start))
|
|
|
|
return
|
2016-11-09 13:58:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock - block until write lock is released.
|
2019-04-02 15:27:20 -04:00
|
|
|
func (li *localLockInstance) Unlock() {
|
2016-11-09 13:58:41 -05:00
|
|
|
readLock := false
|
2017-01-16 20:05:00 -05:00
|
|
|
li.ns.unlock(li.volume, li.path, li.opsID, readLock)
|
2016-11-09 13:58:41 -05:00
|
|
|
}
|
|
|
|
|
2017-08-31 14:29:22 -04:00
|
|
|
// RLock - block until read lock is taken or timeout has occurred.
|
2019-04-02 15:27:20 -04:00
|
|
|
func (li *localLockInstance) GetRLock(timeout *dynamicTimeout) (timedOutErr error) {
|
2017-03-23 19:36:00 -04:00
|
|
|
lockSource := getSource()
|
2017-08-31 14:29:22 -04:00
|
|
|
start := UTCNow()
|
2016-11-09 13:58:41 -05:00
|
|
|
readLock := true
|
2017-08-31 14:29:22 -04:00
|
|
|
if !li.ns.lock(li.volume, li.path, lockSource, li.opsID, readLock, timeout.Timeout()) {
|
|
|
|
timeout.LogFailure()
|
|
|
|
return OperationTimedOut{Path: li.path}
|
|
|
|
}
|
|
|
|
timeout.LogSuccess(UTCNow().Sub(start))
|
|
|
|
return
|
2016-11-09 13:58:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// RUnlock - block until read lock is released.
|
2019-04-02 15:27:20 -04:00
|
|
|
func (li *localLockInstance) RUnlock() {
|
2016-11-09 13:58:41 -05:00
|
|
|
readLock := true
|
2017-01-16 20:05:00 -05:00
|
|
|
li.ns.unlock(li.volume, li.path, li.opsID, readLock)
|
2016-11-09 13:58:41 -05:00
|
|
|
}
|
2018-01-17 10:24:46 -05:00
|
|
|
|
|
|
|
func getSource() string {
|
|
|
|
var funcName string
|
|
|
|
pc, filename, lineNum, ok := runtime.Caller(2)
|
|
|
|
if ok {
|
|
|
|
filename = pathutil.Base(filename)
|
|
|
|
funcName = strings.TrimPrefix(runtime.FuncForPC(pc).Name(),
|
|
|
|
"github.com/minio/minio/cmd.")
|
|
|
|
} else {
|
|
|
|
filename = "<unknown>"
|
|
|
|
lineNum = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("[%s:%d:%s()]", filename, lineNum, funcName)
|
|
|
|
}
|