diff options
author | Håkon Hallingstad <hakon@verizonmedia.com> | 2020-09-28 12:10:38 +0200 |
---|---|---|
committer | Håkon Hallingstad <hakon@verizonmedia.com> | 2020-09-28 12:10:38 +0200 |
commit | 6e284ef06a2e6dafbaeae8e486b6f68fa53d5d48 (patch) | |
tree | c3e86ef7861f3a92fe984e0d7685a466a786960d /zkfacade | |
parent | d7118a218d4704b52ed9883e4ff381a519adb7c1 (diff) |
Use deque as stack
Diffstat (limited to 'zkfacade')
3 files changed, 32 insertions, 14 deletions
diff --git a/zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java b/zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java index 4239d325ba5..b630995d6b4 100644 --- a/zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java +++ b/zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java @@ -56,10 +56,6 @@ public class Lock implements Mutex { @Override public void close() { - release(); - } - - private void release() { ThreadLockInfo.getCurrentThreadLockInfo().lockReleased(lockPath); try { mutex.release(); diff --git a/zkfacade/src/main/java/com/yahoo/vespa/curator/stats/ThreadLockInfo.java b/zkfacade/src/main/java/com/yahoo/vespa/curator/stats/ThreadLockInfo.java index 5280a049ceb..92e6eb453b6 100644 --- a/zkfacade/src/main/java/com/yahoo/vespa/curator/stats/ThreadLockInfo.java +++ b/zkfacade/src/main/java/com/yahoo/vespa/curator/stats/ThreadLockInfo.java @@ -6,9 +6,8 @@ import com.yahoo.vespa.curator.Lock; import java.time.Duration; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ConcurrentLinkedDeque; import java.util.function.Consumer; /** @@ -30,7 +29,7 @@ public class ThreadLockInfo { private final Thread thread; /** The locks are reentrant so there may be more than 1 lock for this thread. */ - private final ConcurrentLinkedQueue<LockInfo> lockInfos = new ConcurrentLinkedQueue<>(); + private final ConcurrentLinkedDeque<LockInfo> lockInfos = new ConcurrentLinkedDeque<>(); public static Map<String, LockCounters> getLockCountersByPath() { return Map.copyOf(countersByLockPath); } @@ -83,7 +82,7 @@ public class ThreadLockInfo { LockCounters lockCounters = getLockCounters(lockPath); lockCounters.invokeAcquireCount.incrementAndGet(); lockCounters.inCriticalRegionCount.incrementAndGet(); - lockInfos.add(LockInfo.invokingAcquire(this, lockPath, timeout)); + lockInfos.addLast(LockInfo.invokingAcquire(this, lockPath, timeout)); } /** Mutable method (see class doc) */ @@ -107,7 +106,11 @@ public class ThreadLockInfo { /** Mutable method (see class doc) */ public void lockAcquired(String lockPath) { getLockCounters(lockPath).lockAcquiredCount.incrementAndGet(); - getLastLockInfo().ifPresent(LockInfo::lockAcquired); + LockInfo lastLockInfo = lockInfos.peekLast(); + if (lastLockInfo == null) { + throw new IllegalStateException("lockAcquired invoked without lockInfos"); + } + lastLockInfo.lockAcquired(); } /** Mutable method (see class doc) */ @@ -121,10 +124,6 @@ public class ThreadLockInfo { return countersByLockPath.computeIfAbsent(lockPath, __ -> new LockCounters()); } - private Optional<LockInfo> getLastLockInfo() { - return lockInfos.isEmpty() ? Optional.empty() : Optional.of(lockInfos.peek()); - } - private void removeLastLockInfo(LockCounters lockCounters, Consumer<LockInfo> completeLockInfo) { lockCounters.inCriticalRegionCount.decrementAndGet(); @@ -133,7 +132,7 @@ public class ThreadLockInfo { return; } - LockInfo lockInfo = lockInfos.poll(); + LockInfo lockInfo = lockInfos.pollLast(); completeLockInfo.accept(lockInfo); completedLockInfoSamples.maybeSample(lockInfo); } diff --git a/zkfacade/src/test/java/com/yahoo/vespa/curator/stats/LockTest.java b/zkfacade/src/test/java/com/yahoo/vespa/curator/stats/LockTest.java index 4b9b6a4429b..984a04c6d4e 100644 --- a/zkfacade/src/test/java/com/yahoo/vespa/curator/stats/LockTest.java +++ b/zkfacade/src/test/java/com/yahoo/vespa/curator/stats/LockTest.java @@ -110,4 +110,27 @@ public class LockTest { expectedCounters.locksReleasedCount.set(2); assertEquals(Map.of(lockPath, expectedCounters), ThreadLockInfo.getLockCountersByPath()); } + + @Test + public void nestedLocks() throws Exception { + when(mutex.acquire(anyLong(), any())).thenReturn(true); + + String lockPath2 = "/lock/path/2"; + Lock lock2 = new Lock(lockPath2, mutex); + + lock.acquire(acquireTimeout); + lock2.acquire(acquireTimeout); + + List<ThreadLockInfo> threadLockInfos = ThreadLockInfo.getThreadLockInfos(); + assertEquals(1, threadLockInfos.size()); + List<LockInfo> lockInfos = threadLockInfos.get(0).getLockInfos(); + assertEquals(2, lockInfos.size()); + assertEquals(lockPath, lockInfos.get(0).getLockPath()); + assertEquals(LockInfo.LockState.ACQUIRED, lockInfos.get(0).getLockState()); + assertEquals(lockPath2, lockInfos.get(1).getLockPath()); + assertEquals(LockInfo.LockState.ACQUIRED, lockInfos.get(1).getLockState()); + + lock.close(); + lock.close(); + } } |