2019-11-13 15:17:45 -05: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 dsync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-01-10 05:35:06 -05:00
|
|
|
"errors"
|
2019-11-13 15:17:45 -05:00
|
|
|
golog "log"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2020-06-08 14:28:40 -04:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/retry"
|
2019-11-13 15:17:45 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Indicator if logging is enabled.
|
|
|
|
var dsyncLog bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Check for MINIO_DSYNC_TRACE env variable, if set logging will be enabled for failed REST operations.
|
|
|
|
dsyncLog = os.Getenv("MINIO_DSYNC_TRACE") == "1"
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
|
|
|
func log(msg ...interface{}) {
|
|
|
|
if dsyncLog {
|
|
|
|
golog.Println(msg...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DRWMutexAcquireTimeout - tolerance limit to wait for lock acquisition before.
|
|
|
|
const DRWMutexAcquireTimeout = 1 * time.Second // 1 second.
|
|
|
|
const drwMutexInfinite = time.Duration(1<<63 - 1)
|
|
|
|
|
|
|
|
// A DRWMutex is a distributed mutual exclusion lock.
|
|
|
|
type DRWMutex struct {
|
2020-02-21 00:59:57 -05:00
|
|
|
Names []string
|
2019-11-13 15:17:45 -05:00
|
|
|
writeLocks []string // Array of nodes that granted a write lock
|
|
|
|
readersLocks [][]string // Array of array of nodes that granted reader locks
|
|
|
|
m sync.Mutex // Mutex to prevent multiple simultaneous locks from this node
|
|
|
|
clnt *Dsync
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// Granted - represents a structure of a granted lock.
|
|
|
|
type Granted struct {
|
|
|
|
index int
|
|
|
|
lockUID string // Locked if set with UID string, unlocked if empty
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Granted) isLocked() bool {
|
|
|
|
return isLocked(g.lockUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLocked(uid string) bool {
|
|
|
|
return len(uid) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDRWMutex - initializes a new dsync RW mutex.
|
2020-02-21 00:59:57 -05:00
|
|
|
func NewDRWMutex(ctx context.Context, clnt *Dsync, names ...string) *DRWMutex {
|
2019-11-13 15:17:45 -05:00
|
|
|
return &DRWMutex{
|
2019-11-19 20:42:27 -05:00
|
|
|
writeLocks: make([]string, len(clnt.GetLockersFn())),
|
2020-02-21 00:59:57 -05:00
|
|
|
Names: names,
|
2019-11-13 15:17:45 -05:00
|
|
|
clnt: clnt,
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock holds a write lock on dm.
|
|
|
|
//
|
|
|
|
// If the lock is already in use, the calling go routine
|
|
|
|
// blocks until the mutex is available.
|
|
|
|
func (dm *DRWMutex) Lock(id, source string) {
|
|
|
|
|
|
|
|
isReadLock := false
|
|
|
|
dm.lockBlocking(drwMutexInfinite, id, source, isReadLock)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLock tries to get a write lock on dm before the timeout elapses.
|
|
|
|
//
|
|
|
|
// If the lock is already in use, the calling go routine
|
|
|
|
// blocks until either the mutex becomes available and return success or
|
|
|
|
// more time has passed than the timeout value and return false.
|
|
|
|
func (dm *DRWMutex) GetLock(id, source string, timeout time.Duration) (locked bool) {
|
|
|
|
|
|
|
|
isReadLock := false
|
|
|
|
return dm.lockBlocking(timeout, id, source, isReadLock)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RLock holds a read lock on dm.
|
|
|
|
//
|
|
|
|
// If one or more read locks are already in use, it will grant another lock.
|
|
|
|
// Otherwise the calling go routine blocks until the mutex is available.
|
|
|
|
func (dm *DRWMutex) RLock(id, source string) {
|
|
|
|
|
|
|
|
isReadLock := true
|
|
|
|
dm.lockBlocking(drwMutexInfinite, id, source, isReadLock)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRLock tries to get a read lock on dm before the timeout elapses.
|
|
|
|
//
|
|
|
|
// If one or more read locks are already in use, it will grant another lock.
|
|
|
|
// Otherwise the calling go routine blocks until either the mutex becomes
|
|
|
|
// available and return success or more time has passed than the timeout
|
|
|
|
// value and return false.
|
|
|
|
func (dm *DRWMutex) GetRLock(id, source string, timeout time.Duration) (locked bool) {
|
|
|
|
|
|
|
|
isReadLock := true
|
|
|
|
return dm.lockBlocking(timeout, id, source, isReadLock)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lockBlocking will try to acquire either a read or a write lock
|
|
|
|
//
|
|
|
|
// The function will loop using a built-in timing randomized back-off
|
|
|
|
// algorithm until either the lock is acquired successfully or more
|
|
|
|
// time has elapsed than the timeout value.
|
|
|
|
func (dm *DRWMutex) lockBlocking(timeout time.Duration, id, source string, isReadLock bool) (locked bool) {
|
2019-11-19 20:42:27 -05:00
|
|
|
restClnts := dm.clnt.GetLockersFn()
|
|
|
|
|
2020-06-08 14:28:40 -04:00
|
|
|
retryCtx, cancel := context.WithTimeout(dm.ctx, timeout)
|
2020-05-04 23:04:06 -04:00
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
// Use incremental back-off algorithm for repeated attempts to acquire the lock
|
2020-06-08 14:28:40 -04:00
|
|
|
for range retry.NewTimer(retryCtx) {
|
2019-11-13 15:17:45 -05:00
|
|
|
// Create temp array on stack.
|
2019-11-19 20:42:27 -05:00
|
|
|
locks := make([]string, len(restClnts))
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
// Try to acquire the lock.
|
2020-02-21 00:59:57 -05:00
|
|
|
success := lock(dm.clnt, &locks, id, source, isReadLock, dm.Names...)
|
2020-06-08 14:28:40 -04:00
|
|
|
if !success {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2020-06-08 14:28:40 -04:00
|
|
|
dm.m.Lock()
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2020-06-08 14:28:40 -04:00
|
|
|
// If success, copy array to object
|
|
|
|
if isReadLock {
|
|
|
|
// Append new array of strings at the end
|
|
|
|
dm.readersLocks = append(dm.readersLocks, make([]string, len(restClnts)))
|
|
|
|
// and copy stack array into last spot
|
|
|
|
copy(dm.readersLocks[len(dm.readersLocks)-1], locks[:])
|
|
|
|
} else {
|
|
|
|
copy(dm.writeLocks, locks[:])
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
2020-06-08 14:28:40 -04:00
|
|
|
|
|
|
|
dm.m.Unlock()
|
|
|
|
return true
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
2020-06-08 14:28:40 -04:00
|
|
|
|
|
|
|
// Failed to acquire the lock on this attempt, incrementally wait
|
|
|
|
// for a longer back-off time and try again afterwards.
|
2019-11-13 15:17:45 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock tries to acquire the distributed lock, returning true or false.
|
2020-02-21 00:59:57 -05:00
|
|
|
func lock(ds *Dsync, locks *[]string, id, source string, isReadLock bool, lockNames ...string) bool {
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2019-11-19 20:42:27 -05:00
|
|
|
restClnts := ds.GetLockersFn()
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
// Create buffered channel of size equal to total number of nodes.
|
2019-11-19 20:42:27 -05:00
|
|
|
ch := make(chan Granted, len(restClnts))
|
2019-11-13 15:17:45 -05:00
|
|
|
defer close(ch)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
2019-11-19 20:42:27 -05:00
|
|
|
for index, c := range restClnts {
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
// broadcast lock request to all nodes
|
|
|
|
go func(index int, isReadLock bool, c NetLocker) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2020-01-10 05:35:06 -05:00
|
|
|
g := Granted{index: index}
|
|
|
|
if c == nil {
|
|
|
|
ch <- g
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
args := LockArgs{
|
2020-02-21 00:59:57 -05:00
|
|
|
UID: id,
|
|
|
|
Resources: lockNames,
|
|
|
|
Source: source,
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var locked bool
|
|
|
|
var err error
|
|
|
|
if isReadLock {
|
|
|
|
if locked, err = c.RLock(args); err != nil {
|
|
|
|
log("Unable to call RLock", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if locked, err = c.Lock(args); err != nil {
|
|
|
|
log("Unable to call Lock", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if locked {
|
|
|
|
g.lockUID = args.UID
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- g
|
|
|
|
|
|
|
|
}(index, isReadLock, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
quorum := false
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(isReadLock bool) {
|
|
|
|
|
|
|
|
// Wait until we have either
|
|
|
|
//
|
|
|
|
// a) received all lock responses
|
|
|
|
// b) received too many 'non-'locks for quorum to be still possible
|
2020-06-08 14:28:40 -04:00
|
|
|
// c) timedout
|
2019-11-13 15:17:45 -05:00
|
|
|
//
|
|
|
|
i, locksFailed := 0, 0
|
|
|
|
done := false
|
|
|
|
timeout := time.After(DRWMutexAcquireTimeout)
|
|
|
|
|
2020-03-31 12:32:16 -04:00
|
|
|
dquorumReads := (len(restClnts) + 1) / 2
|
|
|
|
dquorum := dquorumReads + 1
|
2019-11-19 20:42:27 -05:00
|
|
|
|
|
|
|
for ; i < len(restClnts); i++ { // Loop until we acquired all locks
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
select {
|
|
|
|
case grant := <-ch:
|
|
|
|
if grant.isLocked() {
|
|
|
|
// Mark that this node has acquired the lock
|
|
|
|
(*locks)[grant.index] = grant.lockUID
|
|
|
|
} else {
|
|
|
|
locksFailed++
|
2019-11-19 20:42:27 -05:00
|
|
|
if !isReadLock && locksFailed > len(restClnts)-dquorum ||
|
|
|
|
isReadLock && locksFailed > len(restClnts)-dquorumReads {
|
2019-11-13 15:17:45 -05:00
|
|
|
// We know that we are not going to get the lock anymore,
|
|
|
|
// so exit out and release any locks that did get acquired
|
|
|
|
done = true
|
|
|
|
// Increment the number of grants received from the buffered channel.
|
|
|
|
i++
|
2020-02-21 00:59:57 -05:00
|
|
|
releaseAll(ds, locks, isReadLock, restClnts, lockNames...)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-timeout:
|
|
|
|
done = true
|
|
|
|
// timeout happened, maybe one of the nodes is slow, count
|
|
|
|
// number of locks to check whether we have quorum or not
|
2019-11-19 20:42:27 -05:00
|
|
|
if !quorumMet(locks, isReadLock, dquorum, dquorumReads) {
|
2020-02-21 00:59:57 -05:00
|
|
|
releaseAll(ds, locks, isReadLock, restClnts, lockNames...)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count locks in order to determine whether we have quorum or not
|
2019-11-19 20:42:27 -05:00
|
|
|
quorum = quorumMet(locks, isReadLock, dquorum, dquorumReads)
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
// Signal that we have the quorum
|
|
|
|
wg.Done()
|
|
|
|
|
|
|
|
// Wait for the other responses and immediately release the locks
|
|
|
|
// (do not add them to the locks array because the DRWMutex could
|
|
|
|
// already has been unlocked again by the original calling thread)
|
2019-11-19 20:42:27 -05:00
|
|
|
for ; i < len(restClnts); i++ {
|
2019-11-13 15:17:45 -05:00
|
|
|
grantToBeReleased := <-ch
|
|
|
|
if grantToBeReleased.isLocked() {
|
|
|
|
// release lock
|
2020-02-21 00:59:57 -05:00
|
|
|
sendRelease(ds, restClnts[grantToBeReleased.index],
|
|
|
|
grantToBeReleased.lockUID, isReadLock, lockNames...)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(isReadLock)
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return quorum
|
|
|
|
}
|
|
|
|
|
|
|
|
// quorumMet determines whether we have acquired the required quorum of underlying locks or not
|
|
|
|
func quorumMet(locks *[]string, isReadLock bool, quorum, quorumReads int) bool {
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
for _, uid := range *locks {
|
|
|
|
if isLocked(uid) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var metQuorum bool
|
|
|
|
if isReadLock {
|
|
|
|
metQuorum = count >= quorumReads
|
|
|
|
} else {
|
|
|
|
metQuorum = count >= quorum
|
|
|
|
}
|
|
|
|
|
|
|
|
return metQuorum
|
|
|
|
}
|
|
|
|
|
|
|
|
// releaseAll releases all locks that are marked as locked
|
2020-02-21 00:59:57 -05:00
|
|
|
func releaseAll(ds *Dsync, locks *[]string, isReadLock bool, restClnts []NetLocker, lockNames ...string) {
|
|
|
|
for lock := range restClnts {
|
2019-11-13 15:17:45 -05:00
|
|
|
if isLocked((*locks)[lock]) {
|
2020-02-21 00:59:57 -05:00
|
|
|
sendRelease(ds, restClnts[lock], (*locks)[lock], isReadLock, lockNames...)
|
2019-11-13 15:17:45 -05:00
|
|
|
(*locks)[lock] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock unlocks the write lock.
|
|
|
|
//
|
|
|
|
// It is a run-time error if dm is not locked on entry to Unlock.
|
|
|
|
func (dm *DRWMutex) Unlock() {
|
|
|
|
|
2019-11-19 20:42:27 -05:00
|
|
|
restClnts := dm.clnt.GetLockersFn()
|
2019-11-13 15:17:45 -05:00
|
|
|
// create temp array on stack
|
2019-11-19 20:42:27 -05:00
|
|
|
locks := make([]string, len(restClnts))
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
dm.m.Lock()
|
|
|
|
defer dm.m.Unlock()
|
|
|
|
|
|
|
|
// Check if minimally a single bool is set in the writeLocks array
|
|
|
|
lockFound := false
|
|
|
|
for _, uid := range dm.writeLocks {
|
|
|
|
if isLocked(uid) {
|
|
|
|
lockFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !lockFound {
|
|
|
|
panic("Trying to Unlock() while no Lock() is active")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy write locks to stack array
|
|
|
|
copy(locks, dm.writeLocks[:])
|
|
|
|
// Clear write locks array
|
2019-11-19 20:42:27 -05:00
|
|
|
dm.writeLocks = make([]string, len(restClnts))
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
isReadLock := false
|
2020-02-21 00:59:57 -05:00
|
|
|
unlock(dm.clnt, locks, isReadLock, restClnts, dm.Names...)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// RUnlock releases a read lock held on dm.
|
|
|
|
//
|
|
|
|
// It is a run-time error if dm is not locked on entry to RUnlock.
|
|
|
|
func (dm *DRWMutex) RUnlock() {
|
|
|
|
|
|
|
|
// create temp array on stack
|
2019-11-19 20:42:27 -05:00
|
|
|
restClnts := dm.clnt.GetLockersFn()
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2019-11-19 20:42:27 -05:00
|
|
|
locks := make([]string, len(restClnts))
|
2019-11-13 15:17:45 -05:00
|
|
|
{
|
|
|
|
dm.m.Lock()
|
|
|
|
defer dm.m.Unlock()
|
|
|
|
if len(dm.readersLocks) == 0 {
|
|
|
|
panic("Trying to RUnlock() while no RLock() is active")
|
|
|
|
}
|
|
|
|
// Copy out first element to release it first (FIFO)
|
|
|
|
copy(locks, dm.readersLocks[0][:])
|
|
|
|
// Drop first element from array
|
|
|
|
dm.readersLocks = dm.readersLocks[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
isReadLock := true
|
2020-02-21 00:59:57 -05:00
|
|
|
unlock(dm.clnt, locks, isReadLock, restClnts, dm.Names...)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
2020-02-21 00:59:57 -05:00
|
|
|
func unlock(ds *Dsync, locks []string, isReadLock bool, restClnts []NetLocker, names ...string) {
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
// We don't need to synchronously wait until we have released all the locks (or the quorum)
|
|
|
|
// (a subsequent lock will retry automatically in case it would fail to get quorum)
|
|
|
|
|
2019-11-19 20:42:27 -05:00
|
|
|
for index, c := range restClnts {
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
if isLocked(locks[index]) {
|
|
|
|
// broadcast lock release to all nodes that granted the lock
|
2020-02-21 00:59:57 -05:00
|
|
|
sendRelease(ds, c, locks[index], isReadLock, names...)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendRelease sends a release message to a node that previously granted a lock
|
2020-02-21 00:59:57 -05:00
|
|
|
func sendRelease(ds *Dsync, c NetLocker, uid string, isReadLock bool, names ...string) {
|
2020-01-10 05:35:06 -05:00
|
|
|
if c == nil {
|
|
|
|
log("Unable to call RUnlock", errors.New("netLocker is offline"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
args := LockArgs{
|
2020-02-21 00:59:57 -05:00
|
|
|
UID: uid,
|
|
|
|
Resources: names,
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
if isReadLock {
|
|
|
|
if _, err := c.RUnlock(args); err != nil {
|
|
|
|
log("Unable to call RUnlock", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := c.Unlock(args); err != nil {
|
|
|
|
log("Unable to call Unlock", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|