2016-08-31 20:39:08 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2016-09-30 23:32:13 +02:00
|
|
|
"errors"
|
2016-08-31 20:39:08 +02:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-10-11 09:50:27 +02:00
|
|
|
type statusType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
runningStatus statusType = "Running"
|
|
|
|
blockedStatus statusType = "Blocked"
|
|
|
|
)
|
|
|
|
|
|
|
|
type lockType string
|
|
|
|
|
2016-08-31 20:39:08 +02:00
|
|
|
const (
|
2016-10-11 09:50:27 +02:00
|
|
|
debugRLockStr lockType = "RLock"
|
|
|
|
debugWLockStr lockType = "WLock"
|
2016-08-31 20:39:08 +02:00
|
|
|
)
|
|
|
|
|
2016-10-11 09:50:27 +02:00
|
|
|
// Struct containing information of status (ready/running/blocked) of an operation with given operation ID.
|
2016-08-31 20:39:08 +02:00
|
|
|
type debugLockInfo struct {
|
2016-10-11 09:50:27 +02:00
|
|
|
// "RLock" or "WLock".
|
|
|
|
lType lockType
|
|
|
|
// Contains the trace of the function which invoked the lock, obtained from runtime.
|
|
|
|
lockOrigin string
|
|
|
|
// Status can be running/ready/blocked.
|
|
|
|
status statusType
|
|
|
|
// Time info of the since how long the status holds true.
|
|
|
|
since time.Time
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 09:50:27 +02:00
|
|
|
// debugLockInfo - container for storing locking information for unique copy
|
|
|
|
// (volume,path) pair. ref variable holds the reference count for locks held for.
|
2016-08-31 20:39:08 +02:00
|
|
|
// `ref` values helps us understand the n locks held for given <volume, path> pair.
|
2016-10-11 09:50:27 +02:00
|
|
|
// `running` value helps us understand the total successful locks held (not blocked)
|
|
|
|
// for given <volume, path> pair and the operation is under execution. `blocked`
|
|
|
|
// value helps us understand the total number of operations blocked waiting on
|
|
|
|
// locks for given <volume,path> pair.
|
2016-08-31 20:39:08 +02:00
|
|
|
type debugLockInfoPerVolumePath struct {
|
2016-10-11 09:50:27 +02:00
|
|
|
ref int64 // running + blocked operations.
|
|
|
|
running int64 // count of successful lock acquire and running operations.
|
|
|
|
blocked int64 // count of number of operations blocked waiting on lock.
|
|
|
|
lockInfo map[string]debugLockInfo // map of [opsID] debugLockInfo{operation, status, since} .
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns an instance of debugLockInfo.
|
|
|
|
// need to create this for every unique pair of {volume,path}.
|
|
|
|
// total locks, number of calls blocked on locks, and number of successful locks held but not unlocked yet.
|
|
|
|
func newDebugLockInfoPerVolumePath() *debugLockInfoPerVolumePath {
|
|
|
|
return &debugLockInfoPerVolumePath{
|
|
|
|
lockInfo: make(map[string]debugLockInfo),
|
|
|
|
ref: 0,
|
|
|
|
blocked: 0,
|
|
|
|
running: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LockInfoOriginNotFound - While changing the state of the lock info its important that the entry for
|
|
|
|
// lock at a given origin exists, if not `LockInfoOriginNotFound` is returned.
|
|
|
|
type LockInfoOriginNotFound struct {
|
2016-10-11 09:50:27 +02:00
|
|
|
volume string
|
|
|
|
path string
|
|
|
|
opsID string
|
|
|
|
lockOrigin string
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l LockInfoOriginNotFound) Error() string {
|
2016-11-16 03:14:23 +01:00
|
|
|
return fmt.Sprintf("No lock state stored for the lock origined at \"%s\", for <volume> %s, <path> %s, <opsID> %s",
|
2016-10-11 09:50:27 +02:00
|
|
|
l.lockOrigin, l.volume, l.path, l.opsID)
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
|
|
|
|
2016-10-31 18:17:14 +01:00
|
|
|
// LockInfoVolPathMissing - Error interface. Returned when the info the
|
|
|
|
type LockInfoVolPathMissing struct {
|
2016-08-31 20:39:08 +02:00
|
|
|
volume string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2016-10-31 18:17:14 +01:00
|
|
|
func (l LockInfoVolPathMissing) Error() string {
|
2016-11-16 03:14:23 +01:00
|
|
|
return fmt.Sprintf("No entry in debug Lock Map for Volume: %s, path: %s", l.volume, l.path)
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LockInfoOpsIDNotFound - Returned when the lock state info exists, but the entry for
|
|
|
|
// given operation ID doesn't exist.
|
|
|
|
type LockInfoOpsIDNotFound struct {
|
2016-10-11 09:50:27 +02:00
|
|
|
volume string
|
|
|
|
path string
|
|
|
|
opsID string
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l LockInfoOpsIDNotFound) Error() string {
|
2016-11-16 03:14:23 +01:00
|
|
|
return fmt.Sprintf("No entry in lock info for <Operation ID> %s, <volume> %s, <path> %s", l.opsID, l.volume, l.path)
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LockInfoStateNotBlocked - When an attempt to change the state of the lock form `blocked` to `running` is done,
|
|
|
|
// its necessary that the state before the transsition is "blocked", otherwise LockInfoStateNotBlocked returned.
|
|
|
|
type LockInfoStateNotBlocked struct {
|
2016-10-11 09:50:27 +02:00
|
|
|
volume string
|
|
|
|
path string
|
|
|
|
opsID string
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l LockInfoStateNotBlocked) Error() string {
|
2016-11-16 03:14:23 +01:00
|
|
|
return fmt.Sprintf("Lock state should be \"Blocked\" for <volume> %s, <path> %s, <opsID> %s", l.volume, l.path, l.opsID)
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
|
|
|
|
2016-11-16 03:14:23 +01:00
|
|
|
var errLockNotInitialized = errors.New("Debug lockMap not initialized")
|
2016-09-30 23:32:13 +02:00
|
|
|
|
2016-10-11 09:50:27 +02:00
|
|
|
// Initialize lock info volume path.
|
|
|
|
func (n *nsLockMap) initLockInfoForVolumePath(param nsParam) {
|
|
|
|
n.debugLockMap[param] = newDebugLockInfoPerVolumePath()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the state of the lock from Blocked to Running.
|
|
|
|
func (n *nsLockMap) statusBlockedToRunning(param nsParam, lockOrigin, opsID string, readLock bool) error {
|
2016-08-31 20:39:08 +02:00
|
|
|
// This operation is not executed under the scope nsLockMap.mutex.Lock(), lock has to be explicitly held here.
|
|
|
|
n.lockMapMutex.Lock()
|
|
|
|
defer n.lockMapMutex.Unlock()
|
|
|
|
// new state info to be set for the lock.
|
|
|
|
newLockInfo := debugLockInfo{
|
|
|
|
lockOrigin: lockOrigin,
|
2016-10-11 09:50:27 +02:00
|
|
|
status: runningStatus,
|
2016-08-31 20:39:08 +02:00
|
|
|
since: time.Now().UTC(),
|
|
|
|
}
|
|
|
|
|
2016-10-11 09:50:27 +02:00
|
|
|
// Set lock type.
|
2016-08-31 20:39:08 +02:00
|
|
|
if readLock {
|
2016-10-11 09:50:27 +02:00
|
|
|
newLockInfo.lType = debugRLockStr
|
2016-08-31 20:39:08 +02:00
|
|
|
} else {
|
2016-10-11 09:50:27 +02:00
|
|
|
newLockInfo.lType = debugWLockStr
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 09:50:27 +02:00
|
|
|
// Check whether the lock info entry for <volume, path> pair already exists and its not `nil`.
|
|
|
|
debugLockMap, ok := n.debugLockMap[param]
|
2016-10-05 21:48:07 +02:00
|
|
|
if !ok {
|
2016-10-11 09:50:27 +02:00
|
|
|
// The lock state info foe given <volume, path> pair should already exist.
|
2016-10-31 18:17:14 +01:00
|
|
|
// If not return `LockInfoVolPathMissing`.
|
|
|
|
return traceError(LockInfoVolPathMissing{param.volume, param.path})
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
2016-10-11 09:50:27 +02:00
|
|
|
// ``debugLockMap`` entry containing lock info for `param <volume, path>` is `nil`.
|
|
|
|
if debugLockMap == nil {
|
2016-10-31 18:17:14 +01:00
|
|
|
return traceError(errLockNotInitialized)
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
2016-10-11 09:50:27 +02:00
|
|
|
lockInfo, ok := n.debugLockMap[param].lockInfo[opsID]
|
2016-10-05 21:48:07 +02:00
|
|
|
if !ok {
|
2016-08-31 20:39:08 +02:00
|
|
|
// The lock info entry for given `opsID` should already exist for given <volume, path> pair.
|
|
|
|
// If not return `LockInfoOpsIDNotFound`.
|
2016-10-31 18:17:14 +01:00
|
|
|
return traceError(LockInfoOpsIDNotFound{param.volume, param.path, opsID})
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
2016-10-11 09:50:27 +02:00
|
|
|
// The entry for the lock origined at `lockOrigin` should already exist. If not return `LockInfoOriginNotFound`.
|
|
|
|
if lockInfo.lockOrigin != lockOrigin {
|
2016-10-31 18:17:14 +01:00
|
|
|
return traceError(LockInfoOriginNotFound{param.volume, param.path, opsID, lockOrigin})
|
2016-10-05 21:48:07 +02:00
|
|
|
}
|
2016-10-11 09:50:27 +02:00
|
|
|
// Status of the lock should already be set to "Blocked". If not return `LockInfoStateNotBlocked`.
|
|
|
|
if lockInfo.status != blockedStatus {
|
2016-10-31 18:17:14 +01:00
|
|
|
return traceError(LockInfoStateNotBlocked{param.volume, param.path, opsID})
|
2016-10-05 21:48:07 +02:00
|
|
|
}
|
2016-10-11 09:50:27 +02:00
|
|
|
// All checks finished. Changing the status of the operation from blocked to running and updating the time.
|
|
|
|
n.debugLockMap[param].lockInfo[opsID] = newLockInfo
|
2016-08-31 20:39:08 +02:00
|
|
|
|
|
|
|
// After locking unblocks decrease the blocked counter.
|
|
|
|
n.blockedCounter--
|
|
|
|
// Increase the running counter.
|
|
|
|
n.runningLockCounter++
|
|
|
|
n.debugLockMap[param].blocked--
|
|
|
|
n.debugLockMap[param].running++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-11 09:50:27 +02:00
|
|
|
// Change the state of the lock from Ready to Blocked.
|
|
|
|
func (n *nsLockMap) statusNoneToBlocked(param nsParam, lockOrigin, opsID string, readLock bool) error {
|
2016-08-31 20:39:08 +02:00
|
|
|
newLockInfo := debugLockInfo{
|
|
|
|
lockOrigin: lockOrigin,
|
2016-10-11 09:50:27 +02:00
|
|
|
status: blockedStatus,
|
2016-08-31 20:39:08 +02:00
|
|
|
since: time.Now().UTC(),
|
|
|
|
}
|
|
|
|
if readLock {
|
2016-10-11 09:50:27 +02:00
|
|
|
newLockInfo.lType = debugRLockStr
|
2016-08-31 20:39:08 +02:00
|
|
|
} else {
|
2016-10-11 09:50:27 +02:00
|
|
|
newLockInfo.lType = debugWLockStr
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 21:48:07 +02:00
|
|
|
lockInfo, ok := n.debugLockMap[param]
|
|
|
|
if !ok {
|
2016-08-31 20:39:08 +02:00
|
|
|
// State info entry for the given <volume, pair> doesn't exist, initializing it.
|
|
|
|
n.initLockInfoForVolumePath(param)
|
|
|
|
}
|
2016-10-05 21:48:07 +02:00
|
|
|
if lockInfo == nil {
|
2016-10-11 09:50:27 +02:00
|
|
|
// *lockInfo is nil, initialize here.
|
2016-10-05 21:48:07 +02:00
|
|
|
n.initLockInfoForVolumePath(param)
|
|
|
|
}
|
2016-10-11 09:50:27 +02:00
|
|
|
|
2016-08-31 20:39:08 +02:00
|
|
|
// lockInfo is a map[string]debugLockInfo, which holds map[OperationID]{status,time, origin} of the lock.
|
|
|
|
if n.debugLockMap[param].lockInfo == nil {
|
|
|
|
n.debugLockMap[param].lockInfo = make(map[string]debugLockInfo)
|
|
|
|
}
|
|
|
|
// The status of the operation with the given operation ID is marked blocked till its gets unblocked from the lock.
|
2016-10-11 09:50:27 +02:00
|
|
|
n.debugLockMap[param].lockInfo[opsID] = newLockInfo
|
2016-08-31 20:39:08 +02:00
|
|
|
// Increment the Global lock counter.
|
|
|
|
n.globalLockCounter++
|
|
|
|
// Increment the counter for number of blocked opertions, decrement it after the locking unblocks.
|
|
|
|
n.blockedCounter++
|
|
|
|
// increment the reference of the lock for the given <volume,path> pair.
|
|
|
|
n.debugLockMap[param].ref++
|
|
|
|
// increment the blocked counter for the given <volume, path> pair.
|
|
|
|
n.debugLockMap[param].blocked++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-11 09:50:27 +02:00
|
|
|
// deleteLockInfoEntry - Deletes the lock state information for given
|
|
|
|
// <volume, path> pair. Called when nsLk.ref count is 0.
|
2016-08-31 20:39:08 +02:00
|
|
|
func (n *nsLockMap) deleteLockInfoEntryForVolumePath(param nsParam) error {
|
|
|
|
// delete the lock info for the given operation.
|
2016-10-05 21:48:07 +02:00
|
|
|
if _, found := n.debugLockMap[param]; !found {
|
2016-10-31 18:17:14 +01:00
|
|
|
return traceError(LockInfoVolPathMissing{param.volume, param.path})
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
2016-10-05 21:48:07 +02:00
|
|
|
// Remove from the map if there are no more references for the given (volume,path) pair.
|
|
|
|
delete(n.debugLockMap, param)
|
2016-08-31 20:39:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-11 09:50:27 +02:00
|
|
|
// deleteLockInfoEntry - Deletes the entry for given opsID in the lock state information
|
|
|
|
// of given <volume, path> pair. Called when the nsLk ref count for the given
|
|
|
|
// <volume, path> pair is not 0.
|
|
|
|
func (n *nsLockMap) deleteLockInfoEntryForOps(param nsParam, opsID string) error {
|
2016-08-31 20:39:08 +02:00
|
|
|
// delete the lock info for the given operation.
|
2016-10-05 21:48:07 +02:00
|
|
|
infoMap, found := n.debugLockMap[param]
|
|
|
|
if !found {
|
2016-10-31 18:17:14 +01:00
|
|
|
return traceError(LockInfoVolPathMissing{param.volume, param.path})
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|
2016-10-11 09:50:27 +02:00
|
|
|
// The opertion finished holding the lock on the resource, remove
|
|
|
|
// the entry for the given operation with the operation ID.
|
|
|
|
_, foundInfo := infoMap.lockInfo[opsID]
|
|
|
|
if !foundInfo {
|
2016-10-05 21:48:07 +02:00
|
|
|
// Unlock request with invalid opertion ID not accepted.
|
2016-10-31 18:17:14 +01:00
|
|
|
return traceError(LockInfoOpsIDNotFound{param.volume, param.path, opsID})
|
2016-10-05 21:48:07 +02:00
|
|
|
}
|
2016-10-11 09:50:27 +02:00
|
|
|
// Decrease the global running and lock reference counter.
|
2016-10-05 21:48:07 +02:00
|
|
|
n.runningLockCounter--
|
|
|
|
n.globalLockCounter--
|
2016-10-11 09:50:27 +02:00
|
|
|
// Decrease the lock referee counter for the lock info for given <volume,path> pair.
|
|
|
|
// Decrease the running operation number. Its assumed that the operation is over
|
|
|
|
// once an attempt to release the lock is made.
|
2016-10-05 21:48:07 +02:00
|
|
|
infoMap.running--
|
2016-10-11 09:50:27 +02:00
|
|
|
// Decrease the total reference count of locks jeld on <volume,path> pair.
|
2016-10-05 21:48:07 +02:00
|
|
|
infoMap.ref--
|
2016-10-11 09:50:27 +02:00
|
|
|
delete(infoMap.lockInfo, opsID)
|
2016-08-31 20:39:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-11 09:50:27 +02:00
|
|
|
// Return randomly generated string ID
|
2016-09-27 23:35:43 +02:00
|
|
|
func getOpsID() string {
|
|
|
|
return string(generateRequestID())
|
2016-08-31 20:39:08 +02:00
|
|
|
}
|