Browse Source

adjust logs

test-reverting-lock-table
chrislu 1 year ago
parent
commit
2df70ce95f
  1. 16
      weed/util/lock_table.go
  2. 5
      weed/util/lock_table_test.go

16
weed/util/lock_table.go

@ -64,13 +64,15 @@ func (lt *LockTable[T]) AcquireLock(intention string, key T, lockType LockType)
// If the lock is held exclusively, wait
entry.mu.Lock()
if len(entry.waiters) > 0 || lockType == ExclusiveLock {
glog.V(4).Infof("ActiveLock %d %s wait for %+v type=%v with waiters %d active %d.\n", lock.ID, lock.intention, key, lockType, len(entry.waiters), entry.activeLockOwnerCount)
if glog.V(4) && len(entry.waiters) > 0 {
if glog.V(4) {
fmt.Printf("ActiveLock %d %s wait for %+v type=%v with waiters %d active %d.\n", lock.ID, lock.intention, key, lockType, len(entry.waiters), entry.activeLockOwnerCount)
if len(entry.waiters) > 0 {
for _, waiter := range entry.waiters {
fmt.Printf(" %d", waiter.ID)
}
fmt.Printf("\n")
}
}
entry.waiters = append(entry.waiters, lock)
if lockType == ExclusiveLock {
for !lock.isDeleted && ((len(entry.waiters) > 0 && lock.ID != entry.waiters[0].ID) || entry.activeLockOwnerCount > 0) {
@ -91,13 +93,15 @@ func (lt *LockTable[T]) AcquireLock(intention string, key T, lockType LockType)
// Otherwise, grant the lock
entry.lockType = lockType
glog.V(4).Infof("ActiveLock %d %s locked %+v type=%v with waiters %d active %d.\n", lock.ID, lock.intention, key, lockType, len(entry.waiters), entry.activeLockOwnerCount)
if glog.V(4) && len(entry.waiters) > 0 {
if glog.V(4) {
fmt.Printf("ActiveLock %d %s locked %+v type=%v with waiters %d active %d.\n", lock.ID, lock.intention, key, lockType, len(entry.waiters), entry.activeLockOwnerCount)
if len(entry.waiters) > 0 {
for _, waiter := range entry.waiters {
fmt.Printf(" %d", waiter.ID)
}
fmt.Printf("\n")
}
}
entry.mu.Unlock()
return lock
@ -129,13 +133,15 @@ func (lt *LockTable[T]) ReleaseLock(key T, lock *ActiveLock) {
delete(lt.locks, key)
}
glog.V(4).Infof("ActiveLock %d %s unlocked %+v type=%v with waiters %d active %d.\n", lock.ID, lock.intention, key, entry.lockType, len(entry.waiters), entry.activeLockOwnerCount)
if glog.V(4) {
fmt.Printf("ActiveLock %d %s unlocked %+v type=%v with waiters %d active %d.\n", lock.ID, lock.intention, key, entry.lockType, len(entry.waiters), entry.activeLockOwnerCount)
if len(entry.waiters) > 0 {
for _, waiter := range entry.waiters {
fmt.Printf(" %d", waiter.ID)
}
fmt.Printf("\n")
}
}
entry.activeLockOwnerCount--
// Notify the next waiter

5
weed/util/lock_table_test.go

@ -1,6 +1,7 @@
package util
import (
"fmt"
"math/rand"
"sync"
"testing"
@ -26,14 +27,14 @@ func TestOrderedLock(t *testing.T) {
lock := lt.AcquireLock("", key, lockType)
// Lock acquired, perform some work
glog.V(4).Infof("ActiveLock %d acquired the lock.\n", lock.ID)
fmt.Printf("ActiveLock %d acquired lock %v\n", lock.ID, lockType)
// Simulate some work
time.Sleep(time.Duration(rand.Int31n(10)*10) * time.Millisecond)
// Release the lock
lt.ReleaseLock(key, lock)
glog.V(4).Infof("ActiveLock %d released the lock.\n", lock.ID)
fmt.Printf("ActiveLock %d released lock %v\n", lock.ID, lockType)
}(i)
}

Loading…
Cancel
Save