storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/dsync/dsync-server_test.go (about) 1 /* 2 * Minio Cloud Storage, (C) 2016 Minio, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dsync_test 18 19 import ( 20 "fmt" 21 "sync" 22 23 . "storj.io/minio/pkg/dsync" 24 ) 25 26 const WriteLock = -1 27 28 type lockServer struct { 29 mutex sync.Mutex 30 // Map of locks, with negative value indicating (exclusive) write lock 31 // and positive values indicating number of read locks 32 lockMap map[string]int64 33 34 // Refresh returns lock not found if set to true 35 lockNotFound bool 36 } 37 38 func (l *lockServer) setRefreshReply(refreshed bool) { 39 l.mutex.Lock() 40 defer l.mutex.Unlock() 41 l.lockNotFound = !refreshed 42 } 43 44 func (l *lockServer) Lock(args *LockArgs, reply *bool) error { 45 l.mutex.Lock() 46 defer l.mutex.Unlock() 47 if _, *reply = l.lockMap[args.Resources[0]]; !*reply { 48 l.lockMap[args.Resources[0]] = WriteLock // No locks held on the given name, so claim write lock 49 } 50 *reply = !*reply // Negate *reply to return true when lock is granted or false otherwise 51 return nil 52 } 53 54 func (l *lockServer) Unlock(args *LockArgs, reply *bool) error { 55 l.mutex.Lock() 56 defer l.mutex.Unlock() 57 var locksHeld int64 58 if locksHeld, *reply = l.lockMap[args.Resources[0]]; !*reply { // No lock is held on the given name 59 return fmt.Errorf("Unlock attempted on an unlocked entity: %s", args.Resources[0]) 60 } 61 if *reply = locksHeld == WriteLock; !*reply { // Unless it is a write lock 62 return fmt.Errorf("Unlock attempted on a read locked entity: %s (%d read locks active)", args.Resources[0], locksHeld) 63 } 64 delete(l.lockMap, args.Resources[0]) // Remove the write lock 65 return nil 66 } 67 68 const ReadLock = 1 69 70 func (l *lockServer) RLock(args *LockArgs, reply *bool) error { 71 l.mutex.Lock() 72 defer l.mutex.Unlock() 73 var locksHeld int64 74 if locksHeld, *reply = l.lockMap[args.Resources[0]]; !*reply { 75 l.lockMap[args.Resources[0]] = ReadLock // No locks held on the given name, so claim (first) read lock 76 *reply = true 77 } else { 78 if *reply = locksHeld != WriteLock; *reply { // Unless there is a write lock 79 l.lockMap[args.Resources[0]] = locksHeld + ReadLock // Grant another read lock 80 } 81 } 82 return nil 83 } 84 85 func (l *lockServer) RUnlock(args *LockArgs, reply *bool) error { 86 l.mutex.Lock() 87 defer l.mutex.Unlock() 88 var locksHeld int64 89 if locksHeld, *reply = l.lockMap[args.Resources[0]]; !*reply { // No lock is held on the given name 90 return fmt.Errorf("RUnlock attempted on an unlocked entity: %s", args.Resources[0]) 91 } 92 if *reply = locksHeld != WriteLock; !*reply { // A write-lock is held, cannot release a read lock 93 return fmt.Errorf("RUnlock attempted on a write locked entity: %s", args.Resources[0]) 94 } 95 if locksHeld > ReadLock { 96 l.lockMap[args.Resources[0]] = locksHeld - ReadLock // Remove one of the read locks held 97 } else { 98 delete(l.lockMap, args.Resources[0]) // Remove the (last) read lock 99 } 100 return nil 101 } 102 103 func (l *lockServer) Refresh(args *LockArgs, reply *bool) error { 104 l.mutex.Lock() 105 defer l.mutex.Unlock() 106 *reply = !l.lockNotFound 107 return nil 108 } 109 110 func (l *lockServer) ForceUnlock(args *LockArgs, reply *bool) error { 111 l.mutex.Lock() 112 defer l.mutex.Unlock() 113 if len(args.UID) != 0 { 114 return fmt.Errorf("ForceUnlock called with non-empty UID: %s", args.UID) 115 } 116 delete(l.lockMap, args.Resources[0]) // Remove the lock (irrespective of write or read lock) 117 *reply = true 118 return nil 119 }