2016-04-29 04:29:09 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
2018-06-06 04:51:56 -04:00
|
|
|
var globalLockServer *lockRPCReceiver
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RWLockerSync - internal locker interface.
|
|
|
|
type RWLockerSync interface {
|
|
|
|
GetLock(timeout time.Duration) bool
|
|
|
|
Unlock()
|
|
|
|
GetRLock(timeout time.Duration) bool
|
2017-01-16 20:05:00 -05:00
|
|
|
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)
|
|
|
|
|
|
|
|
receiver := &lockRPCReceiver{
|
|
|
|
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-06-14 13:17:07 -04:00
|
|
|
logger.FatalIf(err, "Unable to parse Lock RPC Host", context.Background())
|
2018-06-06 04:51:56 -04:00
|
|
|
locker, err = NewLockRPCClient(host)
|
2018-06-14 13:17:07 -04:00
|
|
|
logger.FatalIf(err, "Unable to initialize Lock RPC Client", context.Background())
|
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,
|
|
|
|
lockMap: make(map[nsParam]*nsLock),
|
2016-12-26 13:29:55 -05:00
|
|
|
counters: &lockStat{},
|
2016-08-10 16:08:11 -04:00
|
|
|
}
|
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 {
|
2017-08-31 14:29:22 -04:00
|
|
|
RWLockerSync
|
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 {
|
2016-10-11 03:50:27 -04:00
|
|
|
// Lock counter used for lock debugging.
|
2018-08-02 13:39:42 -04:00
|
|
|
counters *lockStat
|
2016-09-19 16:14:55 -04:00
|
|
|
|
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
|
|
|
|
lockMapMutex sync.Mutex
|
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
|
|
|
|
n.lockMapMutex.Lock()
|
2016-04-29 04:29:09 -04:00
|
|
|
|
|
|
|
param := nsParam{volume, path}
|
|
|
|
nsLk, found := n.lockMap[param]
|
|
|
|
if !found {
|
|
|
|
nsLk = &nsLock{
|
2017-08-31 14:29:22 -04:00
|
|
|
RWLockerSync: func() RWLockerSync {
|
2016-12-10 19:15:12 -05:00
|
|
|
if n.isDistXL {
|
2018-01-22 13:25:10 -05:00
|
|
|
return dsync.NewDRWMutex(pathJoin(volume, path), globalDsync)
|
2016-08-10 16:08:11 -04:00
|
|
|
}
|
2017-08-31 14:29:22 -04:00
|
|
|
return &lsync.LRWMutex{}
|
2016-08-10 16:08:11 -04:00
|
|
|
}(),
|
|
|
|
ref: 0,
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|
2016-04-29 14:39:20 -04:00
|
|
|
n.lockMap[param] = nsLk
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|
2016-06-01 19:43:31 -04:00
|
|
|
nsLk.ref++ // Update ref count here to avoid multiple races.
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2016-04-29 14:39:20 -04:00
|
|
|
// Unlock map before Locking NS which might block.
|
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 {
|
2017-08-31 14:29:22 -04:00
|
|
|
locked = nsLk.GetRLock(timeout)
|
2016-04-29 14:39:20 -04:00
|
|
|
} else {
|
2017-08-31 14:29:22 -04:00
|
|
|
locked = nsLk.GetLock(timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !locked { // We failed to get the lock
|
|
|
|
n.lockMapMutex.Lock()
|
|
|
|
defer n.lockMapMutex.Unlock()
|
|
|
|
|
|
|
|
nsLk.ref-- // Decrement ref count since we failed to get the lock
|
2018-04-05 18:04:40 -04:00
|
|
|
|
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)
|
|
|
|
}
|
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-09-27 17:35:43 -04:00
|
|
|
// nsLk.Unlock() will not block, hence locking the map for the
|
|
|
|
// entire function is fine.
|
2016-08-10 16:08:11 -04:00
|
|
|
n.lockMapMutex.Lock()
|
|
|
|
defer n.lockMapMutex.Unlock()
|
2016-04-29 04:29:09 -04:00
|
|
|
|
|
|
|
param := nsParam{volume, path}
|
|
|
|
if nsLk, found := n.lockMap[param]; found {
|
2016-04-29 14:39:20 -04:00
|
|
|
if readLock {
|
2016-09-16 03:30:55 -04:00
|
|
|
nsLk.RUnlock()
|
2016-04-29 14:39:20 -04:00
|
|
|
} else {
|
2016-09-16 03:30:55 -04:00
|
|
|
nsLk.Unlock()
|
2016-04-29 14:39:20 -04:00
|
|
|
}
|
|
|
|
if nsLk.ref == 0 {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(context.Background(), errors.New("Namespace reference count cannot be 0"))
|
2016-04-29 14:39:20 -04:00
|
|
|
}
|
2016-04-29 04:29:09 -04:00
|
|
|
if nsLk.ref != 0 {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
param := nsParam{volume, path}
|
|
|
|
if _, found := n.lockMap[param]; found {
|
|
|
|
// Remove lock from the map.
|
|
|
|
delete(n.lockMap, param)
|
|
|
|
}
|
|
|
|
}
|
2016-11-09 13:58:41 -05:00
|
|
|
|
|
|
|
// lockInstance - frontend/top-level interface for namespace locks.
|
|
|
|
type lockInstance 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()
|
|
|
|
return &lockInstance{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.
|
|
|
|
func (li *lockInstance) 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.
|
|
|
|
func (li *lockInstance) Unlock() {
|
|
|
|
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.
|
|
|
|
func (li *lockInstance) 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.
|
|
|
|
func (li *lockInstance) RUnlock() {
|
|
|
|
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)
|
|
|
|
}
|