2019-11-13 21:17:45 +01: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 11:35:06 +01:00
|
|
|
"errors"
|
2019-11-13 21:17:45 +01:00
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2020-06-08 20:28:40 +02:00
|
|
|
|
2020-07-12 07:19:38 +02:00
|
|
|
"github.com/minio/minio/pkg/console"
|
2019-11-13 21:17:45 +01: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())
|
|
|
|
}
|
|
|
|
|
2020-07-12 07:19:38 +02:00
|
|
|
func log(format string, data ...interface{}) {
|
2019-11-13 21:17:45 +01:00
|
|
|
if dsyncLog {
|
2020-07-12 07:19:38 +02:00
|
|
|
console.Printf(format, data...)
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DRWMutexAcquireTimeout - tolerance limit to wait for lock acquisition before.
|
|
|
|
const DRWMutexAcquireTimeout = 1 * time.Second // 1 second.
|
2020-08-24 21:11:20 +02:00
|
|
|
const drwMutexInfinite = 1<<63 - 1
|
2019-11-13 21:17:45 +01:00
|
|
|
|
|
|
|
// A DRWMutex is a distributed mutual exclusion lock.
|
|
|
|
type DRWMutex struct {
|
2020-02-21 06:59:57 +01:00
|
|
|
Names []string
|
2019-11-13 21:17:45 +01: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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 16:43:10 +02:00
|
|
|
func NewDRWMutex(clnt *Dsync, names ...string) *DRWMutex {
|
2020-09-26 04:21:52 +02:00
|
|
|
restClnts, _ := clnt.GetLockers()
|
2019-11-13 21:17:45 +01:00
|
|
|
return &DRWMutex{
|
2020-09-26 04:21:52 +02:00
|
|
|
writeLocks: make([]string, len(restClnts)),
|
2020-02-21 06:59:57 +01:00
|
|
|
Names: names,
|
2019-11-13 21:17:45 +01:00
|
|
|
clnt: clnt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-08-15 03:17:14 +02:00
|
|
|
dm.lockBlocking(context.Background(), id, source, isReadLock, Options{
|
|
|
|
Timeout: drwMutexInfinite,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options lock options.
|
|
|
|
type Options struct {
|
|
|
|
Timeout time.Duration
|
|
|
|
Tolerance int
|
2019-11-13 21:17:45 +01: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.
|
2020-08-15 03:17:14 +02:00
|
|
|
func (dm *DRWMutex) GetLock(ctx context.Context, id, source string, opts Options) (locked bool) {
|
2019-11-13 21:17:45 +01:00
|
|
|
|
|
|
|
isReadLock := false
|
2020-08-15 03:17:14 +02:00
|
|
|
return dm.lockBlocking(ctx, id, source, isReadLock, opts)
|
2019-11-13 21:17:45 +01: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
|
2020-08-15 03:17:14 +02:00
|
|
|
dm.lockBlocking(context.Background(), id, source, isReadLock, Options{
|
|
|
|
Timeout: drwMutexInfinite,
|
|
|
|
})
|
2019-11-13 21:17:45 +01: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.
|
2020-08-15 03:17:14 +02:00
|
|
|
func (dm *DRWMutex) GetRLock(ctx context.Context, id, source string, opts Options) (locked bool) {
|
2019-11-13 21:17:45 +01:00
|
|
|
|
|
|
|
isReadLock := true
|
2020-08-15 03:17:14 +02:00
|
|
|
return dm.lockBlocking(ctx, id, source, isReadLock, opts)
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
|
2020-09-15 00:57:13 +02:00
|
|
|
const (
|
|
|
|
lockRetryInterval = 50 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
2019-11-13 21:17:45 +01: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.
|
2020-08-15 03:17:14 +02:00
|
|
|
func (dm *DRWMutex) lockBlocking(ctx context.Context, id, source string, isReadLock bool, opts Options) (locked bool) {
|
2020-09-26 04:21:52 +02:00
|
|
|
restClnts, _ := dm.clnt.GetLockers()
|
2019-11-20 02:42:27 +01:00
|
|
|
|
2020-09-15 00:57:13 +02:00
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
2020-05-05 05:04:06 +02:00
|
|
|
|
2020-09-15 00:57:13 +02:00
|
|
|
// Create lock array to capture the successful lockers
|
|
|
|
locks := make([]string, len(restClnts))
|
2019-11-13 21:17:45 +01:00
|
|
|
|
2020-09-18 20:15:54 +02:00
|
|
|
log("lockBlocking %s/%s for %#v: lockType readLock(%t), additional opts: %#v\n", id, source, dm.Names, isReadLock, opts)
|
2020-09-15 00:57:13 +02:00
|
|
|
retryCtx, cancel := context.WithTimeout(ctx, opts.Timeout)
|
2020-09-18 20:15:54 +02:00
|
|
|
|
2020-09-15 00:57:13 +02:00
|
|
|
defer cancel()
|
2019-11-13 21:17:45 +01:00
|
|
|
|
2020-09-15 00:57:13 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-retryCtx.Done():
|
2020-09-18 20:15:54 +02:00
|
|
|
log("lockBlocking canceled %s/%s for %#v: lockType readLock(%t), additional opts: %#v\n", id, source, dm.Names, isReadLock, opts)
|
|
|
|
|
2020-09-15 00:57:13 +02:00
|
|
|
// Caller context canceled or we timedout,
|
|
|
|
// return false anyways for both situations.
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
// Try to acquire the lock.
|
|
|
|
if locked = lock(retryCtx, dm.clnt, &locks, id, source, isReadLock, opts.Tolerance, dm.Names...); locked {
|
|
|
|
dm.m.Lock()
|
|
|
|
|
|
|
|
// 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[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
dm.m.Unlock()
|
|
|
|
return locked
|
|
|
|
}
|
|
|
|
time.Sleep(time.Duration(r.Float64() * float64(lockRetryInterval)))
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock tries to acquire the distributed lock, returning true or false.
|
2020-08-15 03:17:14 +02:00
|
|
|
func lock(ctx context.Context, ds *Dsync, locks *[]string, id, source string, isReadLock bool, tolerance int, lockNames ...string) bool {
|
2020-09-26 04:21:52 +02:00
|
|
|
for i := range *locks {
|
|
|
|
(*locks)[i] = ""
|
|
|
|
}
|
2019-11-13 21:17:45 +01:00
|
|
|
|
2020-09-26 04:21:52 +02:00
|
|
|
restClnts, owner := ds.GetLockers()
|
2019-11-20 02:42:27 +01:00
|
|
|
|
2020-08-15 03:17:14 +02:00
|
|
|
// Tolerance is not set, defaults to half of the locker clients.
|
|
|
|
if tolerance == 0 {
|
|
|
|
tolerance = len(restClnts) / 2
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tolerance = len(restClnts) - quorum
|
|
|
|
|
2019-11-13 21:17:45 +01:00
|
|
|
// Create buffered channel of size equal to total number of nodes.
|
2019-11-20 02:42:27 +01:00
|
|
|
ch := make(chan Granted, len(restClnts))
|
2019-11-13 21:17:45 +01:00
|
|
|
defer close(ch)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
2019-11-20 02:42:27 +01:00
|
|
|
for index, c := range restClnts {
|
2019-11-13 21:17:45 +01:00
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
// broadcast lock request to all nodes
|
|
|
|
go func(index int, isReadLock bool, c NetLocker) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2020-01-10 11:35:06 +01:00
|
|
|
g := Granted{index: index}
|
|
|
|
if c == nil {
|
2020-07-12 07:19:38 +02:00
|
|
|
log("dsync: nil locker")
|
2020-01-10 11:35:06 +01:00
|
|
|
ch <- g
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-13 21:17:45 +01:00
|
|
|
args := LockArgs{
|
2020-09-26 04:21:52 +02:00
|
|
|
Owner: owner,
|
2020-02-21 06:59:57 +01:00
|
|
|
UID: id,
|
|
|
|
Resources: lockNames,
|
|
|
|
Source: source,
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var locked bool
|
|
|
|
var err error
|
|
|
|
if isReadLock {
|
2020-07-30 08:15:34 +02:00
|
|
|
if locked, err = c.RLock(ctx, args); err != nil {
|
2020-07-12 07:19:38 +02:00
|
|
|
log("dsync: Unable to call RLock failed with %s for %#v at %s\n", err, args, c)
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-30 08:15:34 +02:00
|
|
|
if locked, err = c.Lock(ctx, args); err != nil {
|
2020-07-12 07:19:38 +02:00
|
|
|
log("dsync: Unable to call Lock failed with %s for %#v at %s\n", err, args, c)
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if locked {
|
|
|
|
g.lockUID = args.UID
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- g
|
|
|
|
|
|
|
|
}(index, isReadLock, c)
|
|
|
|
}
|
|
|
|
|
2020-08-15 03:17:14 +02:00
|
|
|
quorumMet := false
|
2019-11-13 21:17:45 +01:00
|
|
|
|
|
|
|
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 20:28:40 +02:00
|
|
|
// c) timedout
|
2019-11-13 21:17:45 +01:00
|
|
|
//
|
|
|
|
i, locksFailed := 0, 0
|
|
|
|
done := false
|
|
|
|
timeout := time.After(DRWMutexAcquireTimeout)
|
|
|
|
|
2019-11-20 02:42:27 +01:00
|
|
|
for ; i < len(restClnts); i++ { // Loop until we acquired all locks
|
2019-11-13 21:17:45 +01:00
|
|
|
|
|
|
|
select {
|
|
|
|
case grant := <-ch:
|
|
|
|
if grant.isLocked() {
|
|
|
|
// Mark that this node has acquired the lock
|
|
|
|
(*locks)[grant.index] = grant.lockUID
|
|
|
|
} else {
|
|
|
|
locksFailed++
|
2020-08-15 03:17:14 +02:00
|
|
|
if locksFailed > tolerance {
|
2019-11-13 21:17:45 +01: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-09-26 04:21:52 +02:00
|
|
|
releaseAll(ds, owner, locks, isReadLock, restClnts, lockNames...)
|
2019-11-13 21:17:45 +01: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
|
2020-08-15 03:17:14 +02:00
|
|
|
if !checkQuorumMet(locks, quorum) {
|
2020-07-12 07:19:38 +02:00
|
|
|
log("Quorum not met after timeout\n")
|
2020-09-26 04:21:52 +02:00
|
|
|
releaseAll(ds, owner, locks, isReadLock, restClnts, lockNames...)
|
2020-07-10 21:08:14 +02:00
|
|
|
} else {
|
2020-07-12 07:19:38 +02:00
|
|
|
log("Quorum met after timeout\n")
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count locks in order to determine whether we have quorum or not
|
2020-08-15 03:17:14 +02:00
|
|
|
quorumMet = checkQuorumMet(locks, quorum)
|
2019-11-13 21:17:45 +01: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-20 02:42:27 +01:00
|
|
|
for ; i < len(restClnts); i++ {
|
2019-11-13 21:17:45 +01:00
|
|
|
grantToBeReleased := <-ch
|
|
|
|
if grantToBeReleased.isLocked() {
|
|
|
|
// release lock
|
2020-02-21 06:59:57 +01:00
|
|
|
sendRelease(ds, restClnts[grantToBeReleased.index],
|
2020-09-26 04:21:52 +02:00
|
|
|
owner,
|
2020-02-21 06:59:57 +01:00
|
|
|
grantToBeReleased.lockUID, isReadLock, lockNames...)
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(isReadLock)
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2020-08-15 03:17:14 +02:00
|
|
|
return quorumMet
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
|
2020-08-15 03:17:14 +02:00
|
|
|
// checkQuorumMet determines whether we have acquired the required quorum of underlying locks or not
|
|
|
|
func checkQuorumMet(locks *[]string, quorum int) bool {
|
2019-11-13 21:17:45 +01:00
|
|
|
count := 0
|
|
|
|
for _, uid := range *locks {
|
|
|
|
if isLocked(uid) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 03:17:14 +02:00
|
|
|
return count >= quorum
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// releaseAll releases all locks that are marked as locked
|
2020-09-26 04:21:52 +02:00
|
|
|
func releaseAll(ds *Dsync, owner string, locks *[]string, isReadLock bool, restClnts []NetLocker, lockNames ...string) {
|
2020-02-21 06:59:57 +01:00
|
|
|
for lock := range restClnts {
|
2019-11-13 21:17:45 +01:00
|
|
|
if isLocked((*locks)[lock]) {
|
2020-09-26 04:21:52 +02:00
|
|
|
sendRelease(ds, restClnts[lock], owner, (*locks)[lock], isReadLock, lockNames...)
|
2019-11-13 21:17:45 +01: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() {
|
|
|
|
|
2020-09-26 04:21:52 +02:00
|
|
|
restClnts, owner := dm.clnt.GetLockers()
|
2019-11-13 21:17:45 +01:00
|
|
|
// create temp array on stack
|
2019-11-20 02:42:27 +01:00
|
|
|
locks := make([]string, len(restClnts))
|
2019-11-13 21:17:45 +01: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-20 02:42:27 +01:00
|
|
|
dm.writeLocks = make([]string, len(restClnts))
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isReadLock := false
|
2020-09-26 04:21:52 +02:00
|
|
|
unlock(dm.clnt, owner, locks, isReadLock, restClnts, dm.Names...)
|
2019-11-13 21:17:45 +01: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
|
2020-09-26 04:21:52 +02:00
|
|
|
restClnts, owner := dm.clnt.GetLockers()
|
2019-11-13 21:17:45 +01:00
|
|
|
|
2019-11-20 02:42:27 +01:00
|
|
|
locks := make([]string, len(restClnts))
|
2019-11-13 21:17:45 +01: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-09-26 04:21:52 +02:00
|
|
|
unlock(dm.clnt, owner, locks, isReadLock, restClnts, dm.Names...)
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
|
2020-09-26 04:21:52 +02:00
|
|
|
func unlock(ds *Dsync, owner string, locks []string, isReadLock bool, restClnts []NetLocker, names ...string) {
|
2019-11-13 21:17:45 +01: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-20 02:42:27 +01:00
|
|
|
for index, c := range restClnts {
|
2019-11-13 21:17:45 +01:00
|
|
|
|
|
|
|
if isLocked(locks[index]) {
|
|
|
|
// broadcast lock release to all nodes that granted the lock
|
2020-09-26 04:21:52 +02:00
|
|
|
sendRelease(ds, c, owner, locks[index], isReadLock, names...)
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendRelease sends a release message to a node that previously granted a lock
|
2020-09-26 04:21:52 +02:00
|
|
|
func sendRelease(ds *Dsync, c NetLocker, owner string, uid string, isReadLock bool, names ...string) {
|
2020-01-10 11:35:06 +01:00
|
|
|
if c == nil {
|
2020-07-12 07:19:38 +02:00
|
|
|
log("Unable to call RUnlock failed with %s\n", errors.New("netLocker is offline"))
|
2020-01-10 11:35:06 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-13 21:17:45 +01:00
|
|
|
args := LockArgs{
|
2020-09-26 04:21:52 +02:00
|
|
|
Owner: owner,
|
2020-02-21 06:59:57 +01:00
|
|
|
UID: uid,
|
|
|
|
Resources: names,
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
if isReadLock {
|
|
|
|
if _, err := c.RUnlock(args); err != nil {
|
2020-07-12 07:19:38 +02:00
|
|
|
log("dsync: Unable to call RUnlock failed with %s for %#v at %s\n", err, args, c)
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := c.Unlock(args); err != nil {
|
2020-07-12 07:19:38 +02:00
|
|
|
log("dsync: Unable to call Unlock failed with %s for %#v at %s\n", err, args, c)
|
2019-11-13 21:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|