2018-06-06 04:51:56 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018, 2019 MinIO, Inc.
|
2018-06-06 04:51:56 -04:00
|
|
|
*
|
|
|
|
* 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 (
|
2020-07-30 02:15:34 -04:00
|
|
|
"context"
|
2018-06-06 04:51:56 -04:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
"github.com/minio/minio/pkg/dsync"
|
2018-06-06 04:51:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// lockRequesterInfo stores various info from the client for each lock that is requested.
|
|
|
|
type lockRequesterInfo struct {
|
2021-01-25 13:01:27 -05:00
|
|
|
Name string // name of the resource lock was requested for
|
2019-11-13 15:17:45 -05:00
|
|
|
Writer bool // Bool whether write or read lock.
|
|
|
|
UID string // UID to uniquely identify request of client.
|
|
|
|
Timestamp time.Time // Timestamp set at the time of initialization.
|
|
|
|
TimeLastCheck time.Time // Timestamp for last check of validity of lock.
|
|
|
|
Source string // Contains line, function and filename reqesting the lock.
|
2021-01-25 13:01:27 -05:00
|
|
|
Group bool // indicates if it was a group lock.
|
2020-09-25 22:21:52 -04:00
|
|
|
// Owner represents the UUID of the owner who originally requested the lock
|
|
|
|
// useful in expiry.
|
|
|
|
Owner string
|
2020-10-24 16:23:16 -04:00
|
|
|
// Quorum represents the quorum required for this lock to be active.
|
|
|
|
Quorum int
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// isWriteLock returns whether the lock is a write or read lock.
|
|
|
|
func isWriteLock(lri []lockRequesterInfo) bool {
|
2019-01-24 10:22:14 -05:00
|
|
|
return len(lri) == 1 && lri[0].Writer
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
// localLocker implements Dsync.NetLocker
|
|
|
|
type localLocker struct {
|
2020-12-10 10:28:37 -05:00
|
|
|
mutex sync.Mutex
|
|
|
|
lockMap map[string][]lockRequesterInfo
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
func (l *localLocker) String() string {
|
2020-12-10 10:28:37 -05:00
|
|
|
return globalEndpoints.Localhost()
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
|
2020-02-21 00:59:57 -05:00
|
|
|
func (l *localLocker) canTakeUnlock(resources ...string) bool {
|
|
|
|
var lkCnt int
|
|
|
|
for _, resource := range resources {
|
|
|
|
isWriteLockTaken := isWriteLock(l.lockMap[resource])
|
|
|
|
if isWriteLockTaken {
|
|
|
|
lkCnt++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lkCnt == len(resources)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *localLocker) canTakeLock(resources ...string) bool {
|
|
|
|
var noLkCnt int
|
|
|
|
for _, resource := range resources {
|
|
|
|
_, lockTaken := l.lockMap[resource]
|
|
|
|
if !lockTaken {
|
|
|
|
noLkCnt++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return noLkCnt == len(resources)
|
|
|
|
}
|
|
|
|
|
2020-07-30 02:15:34 -04:00
|
|
|
func (l *localLocker) Lock(ctx context.Context, args dsync.LockArgs) (reply bool, err error) {
|
2020-09-25 22:21:52 -04:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
2020-02-21 00:59:57 -05:00
|
|
|
|
2020-09-25 22:21:52 -04:00
|
|
|
if !l.canTakeLock(args.Resources...) {
|
|
|
|
// Not all locks can be taken on resources,
|
|
|
|
// reject it completely.
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-02-21 00:59:57 -05:00
|
|
|
|
2020-09-25 22:21:52 -04:00
|
|
|
// No locks held on the all resources, so claim write
|
|
|
|
// lock on all resources at once.
|
|
|
|
for _, resource := range args.Resources {
|
|
|
|
l.lockMap[resource] = []lockRequesterInfo{
|
|
|
|
{
|
2021-01-25 13:01:27 -05:00
|
|
|
Name: resource,
|
2020-09-25 22:21:52 -04:00
|
|
|
Writer: true,
|
|
|
|
Source: args.Source,
|
|
|
|
Owner: args.Owner,
|
|
|
|
UID: args.UID,
|
|
|
|
Timestamp: UTCNow(),
|
|
|
|
TimeLastCheck: UTCNow(),
|
2021-01-25 13:01:27 -05:00
|
|
|
Group: len(args.Resources) > 1,
|
2020-10-24 16:23:16 -04:00
|
|
|
Quorum: args.Quorum,
|
2020-09-25 22:21:52 -04:00
|
|
|
},
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-25 22:21:52 -04:00
|
|
|
return true, nil
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *localLocker) Unlock(args dsync.LockArgs) (reply bool, err error) {
|
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
2020-02-21 00:59:57 -05:00
|
|
|
|
|
|
|
if !l.canTakeUnlock(args.Resources...) {
|
|
|
|
// Unless it is a write lock reject it.
|
|
|
|
return reply, fmt.Errorf("Unlock attempted on a read locked entity: %s", args.Resources)
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
2020-02-21 00:59:57 -05:00
|
|
|
for _, resource := range args.Resources {
|
2020-10-26 13:29:29 -04:00
|
|
|
lri, ok := l.lockMap[resource]
|
|
|
|
if ok {
|
|
|
|
l.removeEntry(resource, args, &lri)
|
2020-02-21 00:59:57 -05:00
|
|
|
}
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
// removeEntry based on the uid of the lock message, removes a single entry from the
|
|
|
|
// lockRequesterInfo array or the whole array from the map (in case of a write lock
|
|
|
|
// or last read lock)
|
2020-09-25 22:21:52 -04:00
|
|
|
func (l *localLocker) removeEntry(name string, args dsync.LockArgs, lri *[]lockRequesterInfo) bool {
|
2019-11-13 15:17:45 -05:00
|
|
|
// Find correct entry to remove based on uid.
|
|
|
|
for index, entry := range *lri {
|
2020-09-25 22:21:52 -04:00
|
|
|
if entry.UID == args.UID && entry.Owner == args.Owner {
|
2019-11-13 15:17:45 -05:00
|
|
|
if len(*lri) == 1 {
|
|
|
|
// Remove the write lock.
|
|
|
|
delete(l.lockMap, name)
|
|
|
|
} else {
|
|
|
|
// Remove the appropriate read lock.
|
|
|
|
*lri = append((*lri)[:index], (*lri)[index+1:]...)
|
|
|
|
l.lockMap[name] = *lri
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// None found return false, perhaps entry removed in previous run.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-30 02:15:34 -04:00
|
|
|
func (l *localLocker) RLock(ctx context.Context, args dsync.LockArgs) (reply bool, err error) {
|
2020-09-25 22:21:52 -04:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
2021-01-25 13:01:27 -05:00
|
|
|
resource := args.Resources[0]
|
2020-09-25 22:21:52 -04:00
|
|
|
lrInfo := lockRequesterInfo{
|
2021-01-25 13:01:27 -05:00
|
|
|
Name: resource,
|
2020-09-25 22:21:52 -04:00
|
|
|
Writer: false,
|
|
|
|
Source: args.Source,
|
|
|
|
Owner: args.Owner,
|
|
|
|
UID: args.UID,
|
|
|
|
Timestamp: UTCNow(),
|
|
|
|
TimeLastCheck: UTCNow(),
|
2020-10-24 16:23:16 -04:00
|
|
|
Quorum: args.Quorum,
|
2020-09-25 22:21:52 -04:00
|
|
|
}
|
|
|
|
if lri, ok := l.lockMap[resource]; ok {
|
|
|
|
if reply = !isWriteLock(lri); reply {
|
|
|
|
// Unless there is a write lock
|
|
|
|
l.lockMap[resource] = append(l.lockMap[resource], lrInfo)
|
2020-07-30 02:15:34 -04:00
|
|
|
}
|
2020-09-25 22:21:52 -04:00
|
|
|
} else {
|
|
|
|
// No locks held on the given name, so claim (first) read lock
|
|
|
|
l.lockMap[resource] = []lockRequesterInfo{lrInfo}
|
|
|
|
reply = true
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
2020-09-25 22:21:52 -04:00
|
|
|
return reply, nil
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *localLocker) RUnlock(args dsync.LockArgs) (reply bool, err error) {
|
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
|
|
|
var lri []lockRequesterInfo
|
2020-02-21 00:59:57 -05:00
|
|
|
|
|
|
|
resource := args.Resources[0]
|
|
|
|
if lri, reply = l.lockMap[resource]; !reply {
|
2018-06-06 04:51:56 -04:00
|
|
|
// No lock is held on the given name
|
2020-10-26 13:29:29 -04:00
|
|
|
return true, nil
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
if reply = !isWriteLock(lri); !reply {
|
|
|
|
// A write-lock is held, cannot release a read lock
|
2020-02-21 00:59:57 -05:00
|
|
|
return reply, fmt.Errorf("RUnlock attempted on a write locked entity: %s", resource)
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
2020-10-26 13:29:29 -04:00
|
|
|
l.removeEntry(resource, args, &lri)
|
2018-06-06 04:51:56 -04:00
|
|
|
return reply, nil
|
|
|
|
}
|
|
|
|
|
2019-01-24 10:22:14 -05:00
|
|
|
func (l *localLocker) DupLockMap() map[string][]lockRequesterInfo {
|
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
|
|
|
|
2020-12-10 10:28:37 -05:00
|
|
|
lockCopy := map[string][]lockRequesterInfo{}
|
2019-01-24 10:22:14 -05:00
|
|
|
for k, v := range l.lockMap {
|
2019-02-13 07:59:36 -05:00
|
|
|
lockCopy[k] = append(lockCopy[k], v...)
|
2019-01-24 10:22:14 -05:00
|
|
|
}
|
|
|
|
return lockCopy
|
|
|
|
}
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
func (l *localLocker) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:32:32 -04:00
|
|
|
// IsOnline - local locker is always online.
|
2019-11-19 20:42:27 -05:00
|
|
|
func (l *localLocker) IsOnline() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:32:32 -04:00
|
|
|
// IsLocal - local locker returns true.
|
|
|
|
func (l *localLocker) IsLocal() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-25 13:01:27 -05:00
|
|
|
func (l *localLocker) ForceUnlock(ctx context.Context, args dsync.LockArgs) (reply bool, err error) {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return false, ctx.Err()
|
|
|
|
default:
|
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
|
|
|
if len(args.UID) != 0 {
|
|
|
|
return false, fmt.Errorf("ForceUnlock called with non-empty UID: %s", args.UID)
|
|
|
|
}
|
|
|
|
for _, resource := range args.Resources {
|
|
|
|
delete(l.lockMap, resource) // Remove the lock (irrespective of write or read lock)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 02:15:34 -04:00
|
|
|
func (l *localLocker) Expired(ctx context.Context, args dsync.LockArgs) (expired bool, err error) {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return false, ctx.Err()
|
|
|
|
default:
|
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
2019-11-25 19:39:43 -05:00
|
|
|
|
2021-01-25 13:01:27 -05:00
|
|
|
resource := args.Resources[0] // expiry check is always per resource.
|
|
|
|
|
2020-07-30 02:15:34 -04:00
|
|
|
// Lock found, proceed to verify if belongs to given uid.
|
2021-01-25 13:01:27 -05:00
|
|
|
lri, ok := l.lockMap[resource]
|
|
|
|
if !ok {
|
2021-02-05 22:23:48 -05:00
|
|
|
// lock doesn't exist yet not reason to
|
|
|
|
// expire that doesn't exist yet - it may be
|
|
|
|
// racing with other active lock requests.
|
|
|
|
return false, nil
|
2021-01-25 13:01:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether uid is still active
|
|
|
|
for _, entry := range lri {
|
|
|
|
if entry.UID == args.UID && entry.Owner == args.Owner {
|
|
|
|
ep := globalRemoteEndpoints[args.Owner]
|
|
|
|
if !ep.IsLocal {
|
|
|
|
// check if the owner is online
|
2021-02-05 22:23:48 -05:00
|
|
|
return isServerResolvable(ep, 3*time.Second) != nil, nil
|
2020-02-21 00:59:57 -05:00
|
|
|
}
|
2021-01-25 13:01:27 -05:00
|
|
|
return false, nil
|
2019-11-25 19:39:43 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-25 13:01:27 -05:00
|
|
|
|
2020-07-30 02:15:34 -04:00
|
|
|
return true, nil
|
2019-11-25 19:39:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Similar to removeEntry but only removes an entry only if the lock entry exists in map.
|
|
|
|
// Caller must hold 'l.mutex' lock.
|
2021-01-25 13:01:27 -05:00
|
|
|
func (l *localLocker) removeEntryIfExists(lrip lockRequesterInfo) {
|
2020-10-26 13:29:29 -04:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
|
|
|
|
2019-11-25 19:39:43 -05:00
|
|
|
// Check if entry is still in map (could have been removed altogether by 'concurrent' (R)Unlock of last entry)
|
2021-01-25 13:01:27 -05:00
|
|
|
if lri, ok := l.lockMap[lrip.Name]; ok {
|
2019-11-25 19:39:43 -05:00
|
|
|
// Even if the entry exists, it may not be the same entry which was
|
|
|
|
// considered as expired, so we simply an attempt to remove it if its
|
|
|
|
// not possible there is nothing we need to do.
|
2021-01-25 13:01:27 -05:00
|
|
|
l.removeEntry(lrip.Name, dsync.LockArgs{Owner: lrip.Owner, UID: lrip.UID}, &lri)
|
2019-11-25 19:39:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 10:28:37 -05:00
|
|
|
func newLocker() *localLocker {
|
2019-11-13 15:17:45 -05:00
|
|
|
return &localLocker{
|
2020-12-10 10:28:37 -05:00
|
|
|
lockMap: make(map[string][]lockRequesterInfo),
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
}
|