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 (
|
2016-05-16 17:31:28 -04:00
|
|
|
"errors"
|
2016-08-23 22:19:24 -04:00
|
|
|
pathutil "path"
|
2016-08-10 16:08:11 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-04-29 14:39:20 -04:00
|
|
|
"sync"
|
2016-08-10 16:08:11 -04:00
|
|
|
|
|
|
|
"github.com/minio/dsync"
|
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.
|
|
|
|
var nsMutex *nsLockMap
|
|
|
|
|
|
|
|
// Initialize distributed locking only in case of distributed setup.
|
|
|
|
// Returns if the setup is distributed or not on success.
|
2016-08-23 22:19:24 -04:00
|
|
|
func initDsyncNodes(disks []string, port int) error {
|
2016-08-10 16:08:11 -04:00
|
|
|
serverPort := strconv.Itoa(port)
|
2016-08-23 22:19:24 -04:00
|
|
|
cred := serverConfig.GetCredential()
|
|
|
|
// Initialize rpc lock client information only if this instance is a distributed setup.
|
|
|
|
var clnts []dsync.RPC
|
2016-08-10 16:08:11 -04:00
|
|
|
for _, disk := range disks {
|
|
|
|
if idx := strings.LastIndex(disk, ":"); idx != -1 {
|
2016-08-24 13:14:14 -04:00
|
|
|
clnts = append(clnts, newAuthClient(&authConfig{
|
|
|
|
accessKey: cred.AccessKeyID,
|
|
|
|
secretKey: cred.SecretAccessKey,
|
|
|
|
// Construct a new dsync server addr.
|
|
|
|
address: disk[:idx] + ":" + serverPort,
|
|
|
|
// Construct a new rpc path for the disk.
|
|
|
|
path: pathutil.Join(lockRPCPath, disk[idx+1:]),
|
|
|
|
loginMethod: "Dsync.LoginHandler",
|
|
|
|
}))
|
2016-08-10 16:08:11 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-23 22:19:24 -04:00
|
|
|
return dsync.SetNodesWithClients(clnts)
|
2016-08-10 16:08:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// initNSLock - initialize name space lock map.
|
|
|
|
func initNSLock(isDist bool) {
|
|
|
|
nsMutex = &nsLockMap{
|
|
|
|
isDist: isDist,
|
|
|
|
lockMap: make(map[nsParam]*nsLock),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RWLocker - interface that any read-write locking library should implement.
|
|
|
|
type RWLocker interface {
|
|
|
|
sync.Locker
|
|
|
|
RLock()
|
|
|
|
RUnlock()
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-08-27 06:16:52 -04:00
|
|
|
writer RWLocker
|
|
|
|
readerArray []RWLocker
|
|
|
|
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-08-10 16:08:11 -04:00
|
|
|
isDist bool // indicates whether the locking service is part of a distributed setup or not.
|
|
|
|
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.
|
|
|
|
func (n *nsLockMap) lock(volume, path string, readLock 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{
|
2016-08-27 06:16:52 -04:00
|
|
|
writer: func() RWLocker {
|
2016-08-10 16:08:11 -04:00
|
|
|
if n.isDist {
|
2016-08-23 22:19:24 -04:00
|
|
|
return dsync.NewDRWMutex(pathutil.Join(volume, path))
|
2016-08-10 16:08:11 -04:00
|
|
|
}
|
|
|
|
return &sync.RWMutex{}
|
|
|
|
}(),
|
|
|
|
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-27 06:16:52 -04:00
|
|
|
rwlock := nsLk.writer
|
2016-08-29 19:13:54 -04:00
|
|
|
if readLock && n.isDist {
|
2016-08-27 06:16:52 -04:00
|
|
|
rwlock = dsync.NewDRWMutex(pathutil.Join(volume, path))
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// Locking here can block.
|
|
|
|
if readLock {
|
2016-08-27 06:16:52 -04:00
|
|
|
rwlock.RLock()
|
|
|
|
|
2016-08-29 19:13:54 -04:00
|
|
|
if n.isDist {
|
|
|
|
// Only add (for reader case) to array after RLock() succeeds
|
|
|
|
// (so that we know for sure that element in [0] can be RUnlocked())
|
|
|
|
n.lockMapMutex.Lock()
|
|
|
|
if len(nsLk.readerArray) == 0 {
|
|
|
|
nsLk.readerArray = []RWLocker{rwlock}
|
|
|
|
} else {
|
|
|
|
nsLk.readerArray = append(nsLk.readerArray, rwlock)
|
|
|
|
}
|
|
|
|
n.lockMapMutex.Unlock()
|
2016-08-27 06:16:52 -04:00
|
|
|
}
|
2016-04-29 14:39:20 -04:00
|
|
|
} else {
|
2016-08-27 06:16:52 -04:00
|
|
|
rwlock.Lock()
|
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 the namespace resource.
|
|
|
|
func (n *nsLockMap) unlock(volume, path string, readLock bool) {
|
|
|
|
// 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-08-29 19:13:54 -04:00
|
|
|
if n.isDist {
|
|
|
|
if len(nsLk.readerArray) == 0 {
|
|
|
|
errorIf(errors.New("Length of reader lock array cannot be 0."), "Invalid reader lock array length detected.")
|
|
|
|
}
|
|
|
|
// Release first lock first (FIFO)
|
|
|
|
nsLk.readerArray[0].RUnlock()
|
|
|
|
// And discard first element
|
|
|
|
nsLk.readerArray = nsLk.readerArray[1:]
|
|
|
|
} else {
|
|
|
|
nsLk.writer.RUnlock()
|
2016-08-27 06:16:52 -04:00
|
|
|
}
|
2016-04-29 14:39:20 -04:00
|
|
|
} else {
|
2016-08-27 06:16:52 -04:00
|
|
|
nsLk.writer.Unlock()
|
2016-04-29 14:39:20 -04:00
|
|
|
}
|
|
|
|
if nsLk.ref == 0 {
|
2016-05-16 17:31:28 -04:00
|
|
|
errorIf(errors.New("Namespace reference count cannot be 0."), "Invalid reference count detected.")
|
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 {
|
2016-08-29 19:13:54 -04:00
|
|
|
if len(nsLk.readerArray) != 0 && n.isDist {
|
2016-08-27 06:16:52 -04:00
|
|
|
errorIf(errors.New("Length of reader lock array should be 0 upon deleting map entry."), "Invalid reader lock array length detected.")
|
|
|
|
}
|
|
|
|
|
2016-04-29 14:39:20 -04:00
|
|
|
// 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.
|
|
|
|
func (n *nsLockMap) Lock(volume, path string) {
|
|
|
|
readLock := false
|
|
|
|
n.lock(volume, path, readLock)
|
|
|
|
}
|
2016-04-29 04:29:09 -04:00
|
|
|
|
2016-04-29 14:39:20 -04:00
|
|
|
// Unlock - unlocks any previously acquired write locks.
|
|
|
|
func (n *nsLockMap) Unlock(volume, path string) {
|
|
|
|
readLock := false
|
|
|
|
n.unlock(volume, path, readLock)
|
|
|
|
}
|
2016-04-29 04:29:09 -04:00
|
|
|
|
2016-04-29 14:39:20 -04:00
|
|
|
// RLock - locks any previously acquired read locks.
|
|
|
|
func (n *nsLockMap) RLock(volume, path string) {
|
|
|
|
readLock := true
|
|
|
|
n.lock(volume, path, readLock)
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RUnlock - unlocks any previously acquired read locks.
|
|
|
|
func (n *nsLockMap) RUnlock(volume, path string) {
|
2016-04-29 14:39:20 -04:00
|
|
|
readLock := true
|
|
|
|
n.unlock(volume, path, readLock)
|
2016-04-29 04:29:09 -04:00
|
|
|
}
|