You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.6 KiB
106 lines
3.6 KiB
8 years ago
|
/*
|
||
6 years ago
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
||
8 years ago
|
*
|
||
|
* 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 (
|
||
7 years ago
|
"context"
|
||
8 years ago
|
"errors"
|
||
6 years ago
|
"path"
|
||
8 years ago
|
"time"
|
||
7 years ago
|
|
||
|
"github.com/minio/minio/cmd/logger"
|
||
8 years ago
|
)
|
||
|
|
||
6 years ago
|
const lockRESTVersion = "v1"
|
||
|
const lockRESTPath = minioReservedBucketPath + "/lock/" + lockRESTVersion
|
||
|
|
||
|
var lockServicePath = path.Join(minioReservedBucketPath, lockServiceSubPath)
|
||
|
|
||
|
const (
|
||
|
lockRESTMethodLock = "lock"
|
||
|
lockRESTMethodRLock = "rlock"
|
||
|
lockRESTMethodUnlock = "unlock"
|
||
|
lockRESTMethodRUnlock = "runlock"
|
||
|
lockRESTMethodForceUnlock = "forceunlock"
|
||
|
lockRESTMethodExpired = "expired"
|
||
|
)
|
||
|
|
||
7 years ago
|
// nameLockRequesterInfoPair is a helper type for lock maintenance
|
||
|
type nameLockRequesterInfoPair struct {
|
||
|
name string
|
||
|
lri lockRequesterInfo
|
||
|
}
|
||
|
|
||
6 years ago
|
type lockResponse struct {
|
||
|
Success bool
|
||
|
}
|
||
|
|
||
8 years ago
|
// Similar to removeEntry but only removes an entry only if the lock entry exists in map.
|
||
8 years ago
|
func (l *localLocker) removeEntryIfExists(nlrip nameLockRequesterInfoPair) {
|
||
8 years ago
|
// Check if entry is still in map (could have been removed altogether by 'concurrent' (R)Unlock of last entry)
|
||
|
if lri, ok := l.lockMap[nlrip.name]; ok {
|
||
6 years ago
|
if !l.removeEntry(nlrip.name, nlrip.lri.UID, &lri) {
|
||
8 years ago
|
// Remove failed, in case it is a:
|
||
6 years ago
|
if nlrip.lri.Writer {
|
||
8 years ago
|
// Writer: this should never happen as the whole (mapped) entry should have been deleted
|
||
7 years ago
|
reqInfo := (&logger.ReqInfo{}).AppendTags("name", nlrip.name)
|
||
6 years ago
|
reqInfo.AppendTags("uid", nlrip.lri.UID)
|
||
7 years ago
|
ctx := logger.SetReqInfo(context.Background(), reqInfo)
|
||
|
logger.LogIf(ctx, errors.New("Lock maintenance failed to remove entry for write lock (should never happen)"))
|
||
8 years ago
|
} // Reader: this can happen if multiple read locks were active and
|
||
|
// the one we are looking for has been released concurrently (so it is fine).
|
||
|
} // Removal went okay, all is fine.
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// removeEntry either, 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)
|
||
8 years ago
|
func (l *localLocker) removeEntry(name, uid string, lri *[]lockRequesterInfo) bool {
|
||
8 years ago
|
// Find correct entry to remove based on uid.
|
||
|
for index, entry := range *lri {
|
||
6 years ago
|
if entry.UID == uid {
|
||
8 years ago
|
if len(*lri) == 1 {
|
||
|
// Remove the (last) 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
|
||
|
}
|
||
|
|
||
|
// getLongLivedLocks returns locks that are older than a certain time and
|
||
|
// have not been 'checked' for validity too soon enough
|
||
|
func getLongLivedLocks(m map[string][]lockRequesterInfo, interval time.Duration) []nameLockRequesterInfoPair {
|
||
|
rslt := []nameLockRequesterInfoPair{}
|
||
|
for name, lriArray := range m {
|
||
|
for idx := range lriArray {
|
||
|
// Check whether enough time has gone by since last check
|
||
6 years ago
|
if time.Since(lriArray[idx].TimeLastCheck) >= interval {
|
||
8 years ago
|
rslt = append(rslt, nameLockRequesterInfoPair{name: name, lri: lriArray[idx]})
|
||
6 years ago
|
lriArray[idx].TimeLastCheck = UTCNow()
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
return rslt
|
||
|
}
|