2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
package dsync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-01-10 05:35:06 -05:00
|
|
|
"errors"
|
2019-11-13 15:17:45 -05:00
|
|
|
"math/rand"
|
2021-09-30 14:53:01 -04:00
|
|
|
"sort"
|
2022-12-15 11:31:21 -05:00
|
|
|
"strconv"
|
2019-11-13 15:17:45 -05:00
|
|
|
"sync"
|
|
|
|
"time"
|
2020-06-08 14:28:40 -04:00
|
|
|
|
2024-01-28 13:04:17 -05:00
|
|
|
xioutil "github.com/minio/minio/internal/ioutil"
|
2022-12-23 22:49:07 -05:00
|
|
|
"github.com/minio/minio/internal/mcontext"
|
2024-05-24 19:05:23 -04:00
|
|
|
"github.com/minio/pkg/v3/console"
|
|
|
|
"github.com/minio/pkg/v3/env"
|
2019-11-13 15:17:45 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Indicator if logging is enabled.
|
|
|
|
var dsyncLog bool
|
|
|
|
|
2023-05-13 11:42:21 -04:00
|
|
|
// Retry unit interval
|
|
|
|
var lockRetryMinInterval time.Duration
|
|
|
|
|
|
|
|
var lockRetryBackOff func(*rand.Rand, uint) time.Duration
|
2022-12-15 11:31:21 -05:00
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
func init() {
|
|
|
|
// Check for MINIO_DSYNC_TRACE env variable, if set logging will be enabled for failed REST operations.
|
2023-07-20 10:52:49 -04:00
|
|
|
dsyncLog = env.Get("_MINIO_DSYNC_TRACE", "0") == "1"
|
2022-12-15 11:31:21 -05:00
|
|
|
|
2023-05-13 11:42:21 -04:00
|
|
|
lockRetryMinInterval = 250 * time.Millisecond
|
2023-07-20 10:52:49 -04:00
|
|
|
if lri := env.Get("_MINIO_LOCK_RETRY_INTERVAL", ""); lri != "" {
|
2022-12-15 11:31:21 -05:00
|
|
|
v, err := strconv.Atoi(lri)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-05-13 11:42:21 -04:00
|
|
|
lockRetryMinInterval = time.Duration(v) * time.Millisecond
|
2022-12-15 11:31:21 -05:00
|
|
|
}
|
2023-05-13 11:42:21 -04:00
|
|
|
|
|
|
|
lockRetryBackOff = backoffWait(
|
|
|
|
lockRetryMinInterval,
|
|
|
|
100*time.Millisecond,
|
|
|
|
5*time.Second,
|
|
|
|
)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
2020-07-12 01:19:38 -04:00
|
|
|
func log(format string, data ...interface{}) {
|
2019-11-13 15:17:45 -05:00
|
|
|
if dsyncLog {
|
2020-07-12 01:19:38 -04:00
|
|
|
console.Printf(format, data...)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 14:14:28 -05:00
|
|
|
const (
|
|
|
|
// dRWMutexAcquireTimeout - default tolerance limit to wait for lock acquisition before.
|
|
|
|
drwMutexAcquireTimeout = 1 * time.Second // 1 second.
|
|
|
|
|
|
|
|
// dRWMutexRefreshTimeout - default timeout for the refresh call
|
|
|
|
drwMutexRefreshCallTimeout = 5 * time.Second
|
|
|
|
|
|
|
|
// dRWMutexUnlockTimeout - default timeout for the unlock call
|
|
|
|
drwMutexUnlockCallTimeout = 30 * time.Second
|
|
|
|
|
|
|
|
// dRWMutexForceUnlockTimeout - default timeout for the unlock call
|
|
|
|
drwMutexForceUnlockCallTimeout = 30 * time.Second
|
2021-03-03 21:36:43 -05:00
|
|
|
|
2022-03-01 14:14:28 -05:00
|
|
|
// dRWMutexRefreshInterval - default the interval between two refresh calls
|
|
|
|
drwMutexRefreshInterval = 10 * time.Second
|
2021-05-11 05:11:29 -04:00
|
|
|
|
2022-03-01 14:14:28 -05:00
|
|
|
drwMutexInfinite = 1<<63 - 1
|
|
|
|
)
|
2021-03-03 21:36:43 -05:00
|
|
|
|
2022-03-01 14:14:28 -05:00
|
|
|
// Timeouts are timeouts for specific operations.
|
|
|
|
type Timeouts struct {
|
|
|
|
// Acquire - tolerance limit to wait for lock acquisition before.
|
|
|
|
Acquire time.Duration
|
2021-08-27 11:59:36 -04:00
|
|
|
|
2022-03-01 14:14:28 -05:00
|
|
|
// RefreshCall - timeout for the refresh call
|
|
|
|
RefreshCall time.Duration
|
2021-03-03 21:36:43 -05:00
|
|
|
|
2022-03-01 14:14:28 -05:00
|
|
|
// UnlockCall - timeout for the unlock call
|
|
|
|
UnlockCall time.Duration
|
|
|
|
|
|
|
|
// ForceUnlockCall - timeout for the force unlock call
|
|
|
|
ForceUnlockCall time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultTimeouts contains default timeouts.
|
|
|
|
var DefaultTimeouts = Timeouts{
|
|
|
|
Acquire: drwMutexAcquireTimeout,
|
2022-07-14 10:20:48 -04:00
|
|
|
RefreshCall: drwMutexRefreshCallTimeout,
|
|
|
|
UnlockCall: drwMutexUnlockCallTimeout,
|
2022-03-01 14:14:28 -05:00
|
|
|
ForceUnlockCall: drwMutexForceUnlockCallTimeout,
|
|
|
|
}
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
// A DRWMutex is a distributed mutual exclusion lock.
|
|
|
|
type DRWMutex struct {
|
2023-05-13 11:42:21 -04:00
|
|
|
Names []string
|
|
|
|
writeLocks []string // Array of nodes that granted a write lock
|
|
|
|
readLocks []string // Array of array of nodes that granted reader locks
|
|
|
|
rng *rand.Rand
|
|
|
|
m sync.Mutex // Mutex to prevent multiple simultaneous locks from this node
|
|
|
|
clnt *Dsync
|
|
|
|
cancelRefresh context.CancelFunc
|
|
|
|
refreshInterval time.Duration
|
|
|
|
lockRetryMinInterval time.Duration
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-06-14 10:43:10 -04:00
|
|
|
func NewDRWMutex(clnt *Dsync, names ...string) *DRWMutex {
|
2020-09-25 22:21:52 -04:00
|
|
|
restClnts, _ := clnt.GetLockers()
|
2021-09-30 14:53:01 -04:00
|
|
|
sort.Strings(names)
|
2019-11-13 15:17:45 -05:00
|
|
|
return &DRWMutex{
|
2023-05-13 11:42:21 -04:00
|
|
|
writeLocks: make([]string, len(restClnts)),
|
|
|
|
readLocks: make([]string, len(restClnts)),
|
|
|
|
Names: names,
|
|
|
|
clnt: clnt,
|
|
|
|
rng: rand.New(&lockedRandSource{src: rand.NewSource(time.Now().UTC().UnixNano())}),
|
|
|
|
refreshInterval: drwMutexRefreshInterval,
|
|
|
|
lockRetryMinInterval: lockRetryMinInterval,
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2021-03-03 21:36:43 -05:00
|
|
|
dm.lockBlocking(context.Background(), nil, id, source, isReadLock, Options{
|
2020-08-14 21:17:14 -04:00
|
|
|
Timeout: drwMutexInfinite,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options lock options.
|
|
|
|
type Options struct {
|
2022-08-19 19:21:05 -04:00
|
|
|
Timeout time.Duration
|
|
|
|
RetryInterval time.Duration
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2021-03-03 21:36:43 -05:00
|
|
|
func (dm *DRWMutex) GetLock(ctx context.Context, cancel context.CancelFunc, id, source string, opts Options) (locked bool) {
|
2019-11-13 15:17:45 -05:00
|
|
|
isReadLock := false
|
2021-03-03 21:36:43 -05:00
|
|
|
return dm.lockBlocking(ctx, cancel, id, source, isReadLock, opts)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2021-03-03 21:36:43 -05:00
|
|
|
dm.lockBlocking(context.Background(), nil, id, source, isReadLock, Options{
|
2020-08-14 21:17:14 -04:00
|
|
|
Timeout: drwMutexInfinite,
|
|
|
|
})
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2021-03-03 21:36:43 -05:00
|
|
|
func (dm *DRWMutex) GetRLock(ctx context.Context, cancel context.CancelFunc, id, source string, opts Options) (locked bool) {
|
2019-11-13 15:17:45 -05:00
|
|
|
isReadLock := true
|
2021-03-03 21:36:43 -05:00
|
|
|
return dm.lockBlocking(ctx, cancel, id, source, isReadLock, opts)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2021-03-03 21:36:43 -05:00
|
|
|
func (dm *DRWMutex) lockBlocking(ctx context.Context, lockLossCallback func(), id, source string, isReadLock bool, opts Options) (locked bool) {
|
2020-11-04 11:25:42 -05:00
|
|
|
restClnts, _ := dm.clnt.GetLockers()
|
2019-11-19 20:42:27 -05:00
|
|
|
|
2020-09-14 18:57:13 -04:00
|
|
|
// Create lock array to capture the successful lockers
|
|
|
|
locks := make([]string, len(restClnts))
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2020-11-04 11:25:42 -05:00
|
|
|
// Add total timeout
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, opts.Timeout)
|
2020-09-14 18:57:13 -04:00
|
|
|
defer cancel()
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2020-09-29 18:18:34 -04:00
|
|
|
// Tolerance is not set, defaults to half of the locker clients.
|
2021-02-05 22:23:48 -05:00
|
|
|
tolerance := len(restClnts) / 2
|
2020-09-29 18:18:34 -04:00
|
|
|
|
|
|
|
// Quorum is effectively = total clients subtracted with tolerance limit
|
|
|
|
quorum := len(restClnts) - tolerance
|
|
|
|
if !isReadLock {
|
|
|
|
// In situations for write locks, as a special case
|
|
|
|
// to avoid split brains we make sure to acquire
|
|
|
|
// quorum + 1 when tolerance is exactly half of the
|
|
|
|
// total locker clients.
|
|
|
|
if quorum == tolerance {
|
|
|
|
quorum++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 00:46:24 -04:00
|
|
|
log("lockBlocking %s/%s for %#v: lockType readLock(%t), additional opts: %#v, quorum: %d, tolerance: %d, lockClients: %d\n", id, source, dm.Names, isReadLock, opts, quorum, tolerance, len(restClnts))
|
|
|
|
|
2020-09-29 18:18:34 -04:00
|
|
|
tolerance = len(restClnts) - quorum
|
2023-05-13 11:42:21 -04:00
|
|
|
attempt := uint(0)
|
2020-09-29 18:18:34 -04:00
|
|
|
|
2020-09-14 18:57:13 -04:00
|
|
|
for {
|
|
|
|
select {
|
2020-11-04 11:25:42 -05:00
|
|
|
case <-ctx.Done():
|
2020-09-14 18:57:13 -04:00
|
|
|
return false
|
|
|
|
default:
|
|
|
|
// Try to acquire the lock.
|
2020-11-04 11:25:42 -05:00
|
|
|
if locked = lock(ctx, dm.clnt, &locks, id, source, isReadLock, tolerance, quorum, dm.Names...); locked {
|
2020-09-14 18:57:13 -04:00
|
|
|
dm.m.Lock()
|
|
|
|
|
|
|
|
// If success, copy array to object
|
|
|
|
if isReadLock {
|
2021-11-16 12:28:29 -05:00
|
|
|
copy(dm.readLocks, locks)
|
2020-09-14 18:57:13 -04:00
|
|
|
} else {
|
2021-11-16 12:28:29 -05:00
|
|
|
copy(dm.writeLocks, locks)
|
2020-09-14 18:57:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dm.m.Unlock()
|
2020-11-04 11:25:42 -05:00
|
|
|
log("lockBlocking %s/%s for %#v: granted\n", id, source, dm.Names)
|
2021-03-03 21:36:43 -05:00
|
|
|
|
|
|
|
// Refresh lock continuously and cancel if there is no quorum in the lock anymore
|
2024-02-22 01:26:06 -05:00
|
|
|
dm.startContinuousLockRefresh(lockLossCallback, id, source, quorum)
|
2021-03-03 21:36:43 -05:00
|
|
|
|
2020-09-14 18:57:13 -04:00
|
|
|
return locked
|
|
|
|
}
|
2020-10-08 15:32:32 -04:00
|
|
|
|
2023-05-13 11:42:21 -04:00
|
|
|
switch {
|
|
|
|
case opts.RetryInterval < 0:
|
2022-12-15 11:31:21 -05:00
|
|
|
return false
|
2023-05-13 11:42:21 -04:00
|
|
|
case opts.RetryInterval > 0:
|
|
|
|
time.Sleep(opts.RetryInterval)
|
|
|
|
default:
|
|
|
|
attempt++
|
|
|
|
time.Sleep(lockRetryBackOff(dm.rng, attempt))
|
2022-12-15 11:31:21 -05:00
|
|
|
}
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-22 01:26:06 -05:00
|
|
|
func (dm *DRWMutex) startContinuousLockRefresh(lockLossCallback func(), id, source string, quorum int) {
|
2021-03-03 21:36:43 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
dm.m.Lock()
|
|
|
|
dm.cancelRefresh = cancel
|
|
|
|
dm.m.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer cancel()
|
2021-03-17 19:37:13 -04:00
|
|
|
|
2022-03-01 14:14:28 -05:00
|
|
|
refreshTimer := time.NewTimer(dm.refreshInterval)
|
2021-03-17 19:37:13 -04:00
|
|
|
defer refreshTimer.Stop()
|
|
|
|
|
2021-03-03 21:36:43 -05:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2021-03-17 19:37:13 -04:00
|
|
|
case <-refreshTimer.C:
|
2022-01-14 13:33:08 -05:00
|
|
|
noQuorum, err := refreshLock(ctx, dm.clnt, id, source, quorum)
|
|
|
|
if err == nil && noQuorum {
|
2021-08-27 11:59:36 -04:00
|
|
|
// Clean the lock locally and in remote nodes
|
|
|
|
forceUnlock(ctx, dm.clnt, id)
|
|
|
|
// Execute the caller lock loss callback
|
2021-03-03 21:36:43 -05:00
|
|
|
if lockLossCallback != nil {
|
|
|
|
lockLossCallback()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-05-18 18:37:58 -04:00
|
|
|
|
|
|
|
refreshTimer.Reset(dm.refreshInterval)
|
2021-03-03 21:36:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-08-27 11:59:36 -04:00
|
|
|
func forceUnlock(ctx context.Context, ds *Dsync, id string) {
|
2022-03-01 14:14:28 -05:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, ds.Timeouts.ForceUnlockCall)
|
2021-08-27 11:59:36 -04:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
restClnts, _ := ds.GetLockers()
|
|
|
|
|
2021-09-23 00:46:24 -04:00
|
|
|
args := LockArgs{
|
|
|
|
UID: id,
|
|
|
|
}
|
|
|
|
|
2021-08-27 11:59:36 -04:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for index, c := range restClnts {
|
|
|
|
wg.Add(1)
|
|
|
|
// Send refresh request to all nodes
|
|
|
|
go func(index int, c NetLocker) {
|
|
|
|
defer wg.Done()
|
|
|
|
c.ForceUnlock(ctx, args)
|
|
|
|
}(index, c)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:36:43 -05:00
|
|
|
type refreshResult struct {
|
|
|
|
offline bool
|
2022-01-14 13:33:08 -05:00
|
|
|
refreshed bool
|
2021-03-03 21:36:43 -05:00
|
|
|
}
|
|
|
|
|
2022-01-14 13:33:08 -05:00
|
|
|
// Refresh the given lock in all nodes, return true to indicate if a lock
|
|
|
|
// does not exist in enough quorum nodes.
|
|
|
|
func refreshLock(ctx context.Context, ds *Dsync, id, source string, quorum int) (bool, error) {
|
2021-08-27 16:07:55 -04:00
|
|
|
restClnts, _ := ds.GetLockers()
|
2021-03-03 21:36:43 -05:00
|
|
|
|
|
|
|
// Create buffered channel of size equal to total number of nodes.
|
|
|
|
ch := make(chan refreshResult, len(restClnts))
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2021-09-23 00:46:24 -04:00
|
|
|
args := LockArgs{
|
|
|
|
UID: id,
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:36:43 -05:00
|
|
|
for index, c := range restClnts {
|
|
|
|
wg.Add(1)
|
|
|
|
// Send refresh request to all nodes
|
|
|
|
go func(index int, c NetLocker) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
ch <- refreshResult{offline: true}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-01 14:14:28 -05:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, ds.Timeouts.RefreshCall)
|
2021-03-03 21:36:43 -05:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
refreshed, err := c.Refresh(ctx, args)
|
2022-01-14 13:33:08 -05:00
|
|
|
if err != nil {
|
|
|
|
ch <- refreshResult{offline: true}
|
|
|
|
log("dsync: Unable to call Refresh failed with %s for %#v at %s\n", err, args, c)
|
2021-03-03 21:36:43 -05:00
|
|
|
} else {
|
2022-01-14 13:33:08 -05:00
|
|
|
ch <- refreshResult{refreshed: refreshed}
|
|
|
|
log("dsync: Refresh returned false for %#v at %s\n", args, c)
|
2021-03-03 21:36:43 -05:00
|
|
|
}
|
|
|
|
}(index, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until we have either
|
|
|
|
//
|
|
|
|
// a) received all refresh responses
|
|
|
|
// b) received too many refreshed for quorum to be still possible
|
|
|
|
// c) timed out
|
|
|
|
//
|
2022-01-14 13:33:08 -05:00
|
|
|
lockNotFound, lockRefreshed := 0, 0
|
2021-03-03 21:36:43 -05:00
|
|
|
done := false
|
|
|
|
|
2022-01-14 13:33:08 -05:00
|
|
|
for i := 0; i < len(restClnts); i++ {
|
2021-03-03 21:36:43 -05:00
|
|
|
select {
|
2022-01-14 13:33:08 -05:00
|
|
|
case refreshResult := <-ch:
|
|
|
|
if refreshResult.offline {
|
2021-03-03 21:36:43 -05:00
|
|
|
continue
|
|
|
|
}
|
2022-01-14 13:33:08 -05:00
|
|
|
if refreshResult.refreshed {
|
|
|
|
lockRefreshed++
|
2021-03-03 21:36:43 -05:00
|
|
|
} else {
|
2022-01-14 13:33:08 -05:00
|
|
|
lockNotFound++
|
2021-03-03 21:36:43 -05:00
|
|
|
}
|
2022-01-14 13:33:08 -05:00
|
|
|
if lockRefreshed >= quorum || lockNotFound > len(restClnts)-quorum {
|
2021-03-03 21:36:43 -05:00
|
|
|
done = true
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
// Refreshing is canceled
|
|
|
|
return false, ctx.Err()
|
|
|
|
}
|
|
|
|
if done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We may have some unused results in ch, release them async.
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
2024-01-28 13:04:17 -05:00
|
|
|
xioutil.SafeClose(ch)
|
2021-03-03 21:36:43 -05:00
|
|
|
for range ch {
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-01-14 13:33:08 -05:00
|
|
|
noQuorum := lockNotFound > len(restClnts)-quorum
|
|
|
|
return noQuorum, nil
|
2021-03-03 21:36:43 -05:00
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
// lock tries to acquire the distributed lock, returning true or false.
|
2021-09-30 14:53:01 -04:00
|
|
|
func lock(ctx context.Context, ds *Dsync, locks *[]string, id, source string, isReadLock bool, tolerance, quorum int, names ...string) bool {
|
2020-09-25 22:21:52 -04:00
|
|
|
for i := range *locks {
|
|
|
|
(*locks)[i] = ""
|
|
|
|
}
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2020-09-25 22:21:52 -04:00
|
|
|
restClnts, owner := ds.GetLockers()
|
2019-11-19 20:42:27 -05:00
|
|
|
|
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
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2021-09-23 00:46:24 -04:00
|
|
|
args := LockArgs{
|
|
|
|
Owner: owner,
|
|
|
|
UID: id,
|
2021-09-30 14:53:01 -04:00
|
|
|
Resources: names,
|
2021-09-23 00:46:24 -04:00
|
|
|
Source: source,
|
2024-07-24 06:24:01 -04:00
|
|
|
Quorum: &quorum,
|
2021-09-23 00:46:24 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 14:33:56 -05:00
|
|
|
// Combined timeout for the lock attempt.
|
2022-03-01 14:14:28 -05:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, ds.Timeouts.Acquire)
|
2020-11-04 11:25:42 -05:00
|
|
|
defer cancel()
|
2022-12-23 22:49:07 -05:00
|
|
|
|
|
|
|
// Special context for NetLockers - do not use timeouts.
|
|
|
|
// Also, pass the trace context info if found for debugging
|
|
|
|
netLockCtx := context.Background()
|
|
|
|
tc, ok := ctx.Value(mcontext.ContextTraceKey).(*mcontext.TraceCtxt)
|
|
|
|
if ok {
|
|
|
|
netLockCtx = context.WithValue(netLockCtx, mcontext.ContextTraceKey, tc)
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:25:42 -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 {
|
2020-11-04 11:25:42 -05:00
|
|
|
log("dsync: nil locker\n")
|
2020-01-10 05:35:06 -05:00
|
|
|
ch <- g
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
var locked bool
|
|
|
|
var err error
|
|
|
|
if isReadLock {
|
2022-12-23 22:49:07 -05:00
|
|
|
if locked, err = c.RLock(netLockCtx, args); err != nil {
|
2020-07-12 01:19:38 -04:00
|
|
|
log("dsync: Unable to call RLock failed with %s for %#v at %s\n", err, args, c)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-23 22:49:07 -05:00
|
|
|
if locked, err = c.Lock(netLockCtx, args); err != nil {
|
2020-07-12 01:19:38 -04:00
|
|
|
log("dsync: Unable to call Lock failed with %s for %#v at %s\n", err, args, c)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if locked {
|
|
|
|
g.lockUID = args.UID
|
|
|
|
}
|
|
|
|
ch <- g
|
|
|
|
}(index, isReadLock, c)
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:25:42 -05:00
|
|
|
// Wait until we have either
|
|
|
|
//
|
|
|
|
// a) received all lock responses
|
|
|
|
// b) received too many 'non-'locks for quorum to be still possible
|
|
|
|
// c) timed out
|
|
|
|
//
|
|
|
|
i, locksFailed := 0, 0
|
|
|
|
done := false
|
|
|
|
|
|
|
|
for ; i < len(restClnts); i++ { // Loop until we acquired all locks
|
|
|
|
select {
|
|
|
|
case grant := <-ch:
|
|
|
|
if grant.isLocked() {
|
|
|
|
// Mark that this node has acquired the lock
|
|
|
|
(*locks)[grant.index] = grant.lockUID
|
|
|
|
} else {
|
|
|
|
locksFailed++
|
|
|
|
if locksFailed > tolerance {
|
|
|
|
// 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
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
2020-11-04 11:25:42 -05:00
|
|
|
case <-ctx.Done():
|
2020-12-04 14:33:56 -05:00
|
|
|
// Capture timedout locks as failed or took too long
|
|
|
|
locksFailed++
|
|
|
|
if locksFailed > tolerance {
|
|
|
|
// 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
|
|
|
|
}
|
2020-11-04 11:25:42 -05:00
|
|
|
}
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2020-11-04 11:25:42 -05:00
|
|
|
if done {
|
|
|
|
break
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
2020-11-04 11:25:42 -05:00
|
|
|
}
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2020-11-04 11:25:42 -05:00
|
|
|
quorumLocked := checkQuorumLocked(locks, quorum) && locksFailed <= tolerance
|
|
|
|
if !quorumLocked {
|
2021-09-23 00:46:24 -04:00
|
|
|
log("dsync: Unable to acquire lock in quorum %#v\n", args)
|
|
|
|
// Release all acquired locks without quorum.
|
2022-12-23 22:49:07 -05:00
|
|
|
if !releaseAll(ctx, ds, tolerance, owner, locks, isReadLock, restClnts, names...) {
|
2021-09-23 00:46:24 -04:00
|
|
|
log("Unable to release acquired locks, these locks will expire automatically %#v\n", args)
|
2020-12-10 10:28:37 -05:00
|
|
|
}
|
2020-11-04 11:25:42 -05:00
|
|
|
}
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2020-11-04 11:25:42 -05:00
|
|
|
// We may have some unused results in ch, release them async.
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
2024-01-28 13:04:17 -05:00
|
|
|
xioutil.SafeClose(ch)
|
2020-11-04 11:25:42 -05:00
|
|
|
for grantToBeReleased := range ch {
|
2019-11-13 15:17:45 -05:00
|
|
|
if grantToBeReleased.isLocked() {
|
2020-12-10 10:28:37 -05:00
|
|
|
// release abandoned lock
|
2020-11-04 11:25:42 -05:00
|
|
|
log("Releasing abandoned lock\n")
|
2022-12-23 22:49:07 -05:00
|
|
|
sendRelease(ctx, ds, restClnts[grantToBeReleased.index],
|
2021-09-30 14:53:01 -04:00
|
|
|
owner, grantToBeReleased.lockUID, isReadLock, names...)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
2020-11-04 11:25:42 -05:00
|
|
|
}()
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2020-09-28 18:39:52 -04:00
|
|
|
return quorumLocked
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
2020-10-07 12:15:01 -04:00
|
|
|
// checkFailedUnlocks determines whether we have sufficiently unlocked all
|
|
|
|
// resources to ensure no deadlocks for future callers
|
|
|
|
func checkFailedUnlocks(locks []string, tolerance int) bool {
|
|
|
|
unlocksFailed := 0
|
|
|
|
for lockID := range locks {
|
|
|
|
if isLocked(locks[lockID]) {
|
|
|
|
unlocksFailed++
|
2020-09-28 18:39:52 -04:00
|
|
|
}
|
|
|
|
}
|
2020-10-07 12:15:01 -04:00
|
|
|
|
|
|
|
// Unlock failures are higher than tolerance limit
|
|
|
|
// for this instance of unlocker, we should let the
|
|
|
|
// caller know that lock is not successfully released
|
|
|
|
// yet.
|
|
|
|
if len(locks)-tolerance == tolerance {
|
2024-01-18 02:03:17 -05:00
|
|
|
// In case of split brain scenarios where
|
2020-10-07 12:15:01 -04:00
|
|
|
// tolerance is exactly half of the len(*locks)
|
|
|
|
// then we need to make sure we have unlocked
|
|
|
|
// upto tolerance+1 - especially for RUnlock
|
|
|
|
// to ensure that we don't end up with active
|
|
|
|
// read locks on the resource after unlocking
|
|
|
|
// only half of the lockers.
|
|
|
|
return unlocksFailed >= tolerance
|
|
|
|
}
|
|
|
|
return unlocksFailed > tolerance
|
2020-09-28 18:39:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkQuorumLocked determines whether we have locked the required quorum of underlying locks or not
|
|
|
|
func checkQuorumLocked(locks *[]string, quorum int) bool {
|
2019-11-13 15:17:45 -05:00
|
|
|
count := 0
|
|
|
|
for _, uid := range *locks {
|
|
|
|
if isLocked(uid) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-14 21:17:14 -04:00
|
|
|
return count >= quorum
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// releaseAll releases all locks that are marked as locked
|
2022-12-23 22:49:07 -05:00
|
|
|
func releaseAll(ctx context.Context, ds *Dsync, tolerance int, owner string, locks *[]string, isReadLock bool, restClnts []NetLocker, names ...string) bool {
|
2020-09-28 18:39:52 -04:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for lockID := range restClnts {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(lockID int) {
|
|
|
|
defer wg.Done()
|
2022-12-23 22:49:07 -05:00
|
|
|
if sendRelease(ctx, ds, restClnts[lockID], owner, (*locks)[lockID], isReadLock, names...) {
|
2021-10-18 11:39:59 -04:00
|
|
|
(*locks)[lockID] = ""
|
2020-09-28 18:39:52 -04:00
|
|
|
}
|
|
|
|
}(lockID)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
2020-09-28 18:39:52 -04:00
|
|
|
wg.Wait()
|
2020-10-07 12:15:01 -04:00
|
|
|
|
|
|
|
// Return true if releaseAll was successful, otherwise we return 'false'
|
|
|
|
// to indicate we haven't sufficiently unlocked lockers to avoid deadlocks.
|
|
|
|
//
|
|
|
|
// Caller may use this as an indication to call again.
|
|
|
|
return !checkFailedUnlocks(*locks, tolerance)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock unlocks the write lock.
|
|
|
|
//
|
|
|
|
// It is a run-time error if dm is not locked on entry to Unlock.
|
2022-12-23 22:49:07 -05:00
|
|
|
func (dm *DRWMutex) Unlock(ctx context.Context) {
|
2021-03-03 21:36:43 -05:00
|
|
|
dm.m.Lock()
|
|
|
|
dm.cancelRefresh()
|
|
|
|
dm.m.Unlock()
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2020-09-25 22:21:52 -04:00
|
|
|
restClnts, owner := dm.clnt.GetLockers()
|
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
|
2021-11-16 12:28:29 -05:00
|
|
|
copy(locks, dm.writeLocks)
|
2020-09-28 18:39:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tolerance is not set, defaults to half of the locker clients.
|
|
|
|
tolerance := len(restClnts) / 2
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
isReadLock := false
|
2024-06-19 10:35:19 -04:00
|
|
|
started := time.Now()
|
|
|
|
// Do async unlocking.
|
|
|
|
// This means unlock will no longer block on the network or missing quorum.
|
|
|
|
go func() {
|
2024-09-09 11:49:49 -04:00
|
|
|
ctx, done := context.WithTimeout(ctx, drwMutexUnlockCallTimeout)
|
|
|
|
defer done()
|
2024-06-19 10:35:19 -04:00
|
|
|
for !releaseAll(ctx, dm.clnt, tolerance, owner, &locks, isReadLock, restClnts, dm.Names...) {
|
|
|
|
time.Sleep(time.Duration(dm.rng.Float64() * float64(dm.lockRetryMinInterval)))
|
|
|
|
if time.Since(started) > dm.clnt.Timeouts.UnlockCall {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
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.
|
2022-12-23 22:49:07 -05:00
|
|
|
func (dm *DRWMutex) RUnlock(ctx context.Context) {
|
2021-03-03 21:36:43 -05:00
|
|
|
dm.m.Lock()
|
|
|
|
dm.cancelRefresh()
|
|
|
|
dm.m.Unlock()
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2020-09-25 22:21:52 -04:00
|
|
|
restClnts, owner := dm.clnt.GetLockers()
|
2021-10-18 11:39:59 -04:00
|
|
|
// create temp array on stack
|
2019-11-19 20:42:27 -05:00
|
|
|
locks := make([]string, len(restClnts))
|
2021-10-18 11:39:59 -04:00
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
{
|
|
|
|
dm.m.Lock()
|
|
|
|
defer dm.m.Unlock()
|
2021-10-18 11:39:59 -04:00
|
|
|
|
|
|
|
// Check if minimally a single bool is set in the writeLocks array
|
|
|
|
lockFound := false
|
|
|
|
for _, uid := range dm.readLocks {
|
|
|
|
if isLocked(uid) {
|
|
|
|
lockFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !lockFound {
|
2019-11-13 15:17:45 -05:00
|
|
|
panic("Trying to RUnlock() while no RLock() is active")
|
|
|
|
}
|
2021-10-18 11:39:59 -04:00
|
|
|
|
|
|
|
// Copy write locks to stack array
|
2021-11-16 12:28:29 -05:00
|
|
|
copy(locks, dm.readLocks)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
2020-09-28 18:39:52 -04:00
|
|
|
// Tolerance is not set, defaults to half of the locker clients.
|
|
|
|
tolerance := len(restClnts) / 2
|
|
|
|
isReadLock := true
|
2024-06-19 10:35:19 -04:00
|
|
|
started := time.Now()
|
|
|
|
// Do async unlocking.
|
|
|
|
// This means unlock will no longer block on the network or missing quorum.
|
|
|
|
go func() {
|
|
|
|
for !releaseAll(ctx, dm.clnt, tolerance, owner, &locks, isReadLock, restClnts, dm.Names...) {
|
|
|
|
time.Sleep(time.Duration(dm.rng.Float64() * float64(dm.lockRetryMinInterval)))
|
|
|
|
// If we have been waiting for more than the force unlock timeout, return
|
|
|
|
// Remotes will have canceled due to the missing refreshes anyway.
|
|
|
|
if time.Since(started) > dm.clnt.Timeouts.UnlockCall {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendRelease sends a release message to a node that previously granted a lock
|
2022-12-23 22:49:07 -05:00
|
|
|
func sendRelease(ctx context.Context, ds *Dsync, c NetLocker, owner string, uid string, isReadLock bool, names ...string) bool {
|
2020-01-10 05:35:06 -05:00
|
|
|
if c == nil {
|
2020-07-12 01:19:38 -04:00
|
|
|
log("Unable to call RUnlock failed with %s\n", errors.New("netLocker is offline"))
|
2020-09-28 18:39:52 -04:00
|
|
|
return false
|
2020-01-10 05:35:06 -05:00
|
|
|
}
|
|
|
|
|
2021-10-18 11:39:59 -04:00
|
|
|
if len(uid) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
args := LockArgs{
|
2020-09-25 22:21:52 -04:00
|
|
|
Owner: owner,
|
2020-02-21 00:59:57 -05:00
|
|
|
UID: uid,
|
|
|
|
Resources: names,
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
2020-09-28 18:39:52 -04:00
|
|
|
|
2022-12-23 22:49:07 -05:00
|
|
|
netLockCtx, cancel := context.WithTimeout(context.Background(), ds.Timeouts.UnlockCall)
|
2021-05-11 05:11:29 -04:00
|
|
|
defer cancel()
|
|
|
|
|
2022-12-23 22:49:07 -05:00
|
|
|
tc, ok := ctx.Value(mcontext.ContextTraceKey).(*mcontext.TraceCtxt)
|
|
|
|
if ok {
|
|
|
|
netLockCtx = context.WithValue(netLockCtx, mcontext.ContextTraceKey, tc)
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
if isReadLock {
|
2022-12-23 22:49:07 -05:00
|
|
|
if _, err := c.RUnlock(netLockCtx, args); err != nil {
|
2020-07-12 01:19:38 -04:00
|
|
|
log("dsync: Unable to call RUnlock failed with %s for %#v at %s\n", err, args, c)
|
2020-09-28 18:39:52 -04:00
|
|
|
return false
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-23 22:49:07 -05:00
|
|
|
if _, err := c.Unlock(netLockCtx, args); err != nil {
|
2020-07-12 01:19:38 -04:00
|
|
|
log("dsync: Unable to call Unlock failed with %s for %#v at %s\n", err, args, c)
|
2020-09-28 18:39:52 -04:00
|
|
|
return false
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 18:39:52 -04:00
|
|
|
|
|
|
|
return true
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|