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
|
|
|
|
2021-05-11 05:11:29 -04:00
|
|
|
package dsync
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2021-05-11 05:11:29 -04:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2019-11-13 15:17:45 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const WriteLock = -1
|
|
|
|
|
|
|
|
type lockServer struct {
|
|
|
|
mutex sync.Mutex
|
|
|
|
// Map of locks, with negative value indicating (exclusive) write lock
|
|
|
|
// and positive values indicating number of read locks
|
|
|
|
lockMap map[string]int64
|
2021-03-03 21:36:43 -05:00
|
|
|
|
|
|
|
// Refresh returns lock not found if set to true
|
|
|
|
lockNotFound bool
|
2021-05-11 05:11:29 -04:00
|
|
|
|
|
|
|
// Set to true if you want peers servers to do not respond
|
|
|
|
responseDelay int64
|
2021-03-03 21:36:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lockServer) setRefreshReply(refreshed bool) {
|
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
|
|
|
l.lockNotFound = !refreshed
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
2021-05-11 05:11:29 -04:00
|
|
|
func (l *lockServer) setResponseDelay(responseDelay time.Duration) {
|
|
|
|
atomic.StoreInt64(&l.responseDelay, int64(responseDelay))
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
func (l *lockServer) Lock(args *LockArgs, reply *bool) error {
|
2021-05-11 05:11:29 -04:00
|
|
|
if d := atomic.LoadInt64(&l.responseDelay); d != 0 {
|
|
|
|
time.Sleep(time.Duration(d))
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
2020-02-21 00:59:57 -05:00
|
|
|
if _, *reply = l.lockMap[args.Resources[0]]; !*reply {
|
|
|
|
l.lockMap[args.Resources[0]] = WriteLock // No locks held on the given name, so claim write lock
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
*reply = !*reply // Negate *reply to return true when lock is granted or false otherwise
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lockServer) Unlock(args *LockArgs, reply *bool) error {
|
2021-05-11 05:11:29 -04:00
|
|
|
if d := atomic.LoadInt64(&l.responseDelay); d != 0 {
|
|
|
|
time.Sleep(time.Duration(d))
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
|
|
|
var locksHeld int64
|
2020-02-21 00:59:57 -05:00
|
|
|
if locksHeld, *reply = l.lockMap[args.Resources[0]]; !*reply { // No lock is held on the given name
|
|
|
|
return fmt.Errorf("Unlock attempted on an unlocked entity: %s", args.Resources[0])
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
if *reply = locksHeld == WriteLock; !*reply { // Unless it is a write lock
|
2020-02-21 00:59:57 -05:00
|
|
|
return fmt.Errorf("Unlock attempted on a read locked entity: %s (%d read locks active)", args.Resources[0], locksHeld)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
2020-02-21 00:59:57 -05:00
|
|
|
delete(l.lockMap, args.Resources[0]) // Remove the write lock
|
2019-11-13 15:17:45 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const ReadLock = 1
|
|
|
|
|
|
|
|
func (l *lockServer) RLock(args *LockArgs, reply *bool) error {
|
2021-05-11 05:11:29 -04:00
|
|
|
if d := atomic.LoadInt64(&l.responseDelay); d != 0 {
|
|
|
|
time.Sleep(time.Duration(d))
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
|
|
|
var locksHeld int64
|
2020-02-21 00:59:57 -05:00
|
|
|
if locksHeld, *reply = l.lockMap[args.Resources[0]]; !*reply {
|
|
|
|
l.lockMap[args.Resources[0]] = ReadLock // No locks held on the given name, so claim (first) read lock
|
2019-11-13 15:17:45 -05:00
|
|
|
*reply = true
|
2021-11-16 12:28:29 -05:00
|
|
|
} else if *reply = locksHeld != WriteLock; *reply { // Unless there is a write lock
|
|
|
|
l.lockMap[args.Resources[0]] = locksHeld + ReadLock // Grant another read lock
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lockServer) RUnlock(args *LockArgs, reply *bool) error {
|
2021-05-11 05:11:29 -04:00
|
|
|
if d := atomic.LoadInt64(&l.responseDelay); d != 0 {
|
|
|
|
time.Sleep(time.Duration(d))
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
|
|
|
var locksHeld int64
|
2020-02-21 00:59:57 -05:00
|
|
|
if locksHeld, *reply = l.lockMap[args.Resources[0]]; !*reply { // No lock is held on the given name
|
|
|
|
return fmt.Errorf("RUnlock attempted on an unlocked entity: %s", args.Resources[0])
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
if *reply = locksHeld != WriteLock; !*reply { // A write-lock is held, cannot release a read lock
|
2020-02-21 00:59:57 -05:00
|
|
|
return fmt.Errorf("RUnlock attempted on a write locked entity: %s", args.Resources[0])
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
if locksHeld > ReadLock {
|
2020-02-21 00:59:57 -05:00
|
|
|
l.lockMap[args.Resources[0]] = locksHeld - ReadLock // Remove one of the read locks held
|
2019-11-13 15:17:45 -05:00
|
|
|
} else {
|
2020-02-21 00:59:57 -05:00
|
|
|
delete(l.lockMap, args.Resources[0]) // Remove the (last) read lock
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:36:43 -05:00
|
|
|
func (l *lockServer) Refresh(args *LockArgs, reply *bool) error {
|
2021-05-11 05:11:29 -04:00
|
|
|
if d := atomic.LoadInt64(&l.responseDelay); d != 0 {
|
|
|
|
time.Sleep(time.Duration(d))
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:36:43 -05:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
|
|
|
*reply = !l.lockNotFound
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
func (l *lockServer) ForceUnlock(args *LockArgs, reply *bool) error {
|
2021-05-11 05:11:29 -04:00
|
|
|
if d := atomic.LoadInt64(&l.responseDelay); d != 0 {
|
|
|
|
time.Sleep(time.Duration(d))
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
|
|
|
if len(args.UID) != 0 {
|
|
|
|
return fmt.Errorf("ForceUnlock called with non-empty UID: %s", args.UID)
|
|
|
|
}
|
2020-02-21 00:59:57 -05:00
|
|
|
delete(l.lockMap, args.Resources[0]) // Remove the lock (irrespective of write or read lock)
|
2019-11-13 15:17:45 -05:00
|
|
|
*reply = true
|
|
|
|
return nil
|
|
|
|
}
|