Skip to content

Commit

Permalink
fix for segmented + threadlocal deadlock
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi authored and sgup432 committed Jun 3, 2024
1 parent d0899e6 commit b340577
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ public class TieredSpilloverCache<K, V> implements ICache<K, V> {

ThreadLocal<SegmentedLock> threadLocal = new ThreadLocal<>();

private final SegmentedLock[] locks = new SegmentedLock[256];
private final int NUM_LOCKS = 1;
private final SegmentedLock[] locks = new SegmentedLock[NUM_LOCKS];
{
for (int i = 0; i < 256; i++) {
for (int i = 0; i < NUM_LOCKS; i++) {
locks[i] = new SegmentedLock();
}
}
Expand All @@ -107,8 +108,13 @@ void writeLock(ICacheKey<K> key) {
SegmentedLock segmentedLock = locks[getLockIndexForKey(key)];
if (threadLocal.get() != null) {
SegmentedLock segmentedLock1 = threadLocal.get();
segmentedLock1.writeLock.close();
threadLocal.remove();
if (segmentedLock == segmentedLock1 && segmentedLock.writeLock.isHeldByCurrentThread()) {
return;
}
if (segmentedLock1.writeLock.isHeldByCurrentThread()) {
segmentedLock1.writeLock.close();
threadLocal.remove();
}
}
threadLocal.set(segmentedLock);
segmentedLock.writeLock.acquire();
Expand All @@ -121,19 +127,24 @@ void readLock(ICacheKey<K> key) {

void unlockWriteLock(ICacheKey<K> key) {
SegmentedLock segmentedLock = locks[getLockIndexForKey(key)];
segmentedLock.writeLock.close();
if (threadLocal.get() != null) {
threadLocal.remove();
if (segmentedLock.writeLock.isHeldByCurrentThread()){
segmentedLock.writeLock.close();
if (threadLocal.get() != null) {
threadLocal.remove();
}
}

}

void unlockReadLock(ICacheKey<K> key) {
SegmentedLock segmentedLock = locks[getLockIndexForKey(key)];
segmentedLock.readLock.close();
if (segmentedLock.readLock.isHeldByCurrentThread()) {
segmentedLock.readLock.close();
}
}

private int getLockIndexForKey(ICacheKey<K> key) {
return ((key.hashCode() & 0xff00) >> 8) % 256;
return ((key.hashCode() & 0xff00) >> 8) % NUM_LOCKS;
}

TieredSpilloverCache(Builder<K, V> builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,9 @@ public void testNumLocksTiming() throws Exception {
System.out.println("Finished iter " + j);
}
}
} catch (Exception ignored) {}
} catch (Exception e) {
throw new RuntimeException(e);
}
countDownLatch.countDown();
});
threads[i].start();
Expand Down

0 comments on commit b340577

Please sign in to comment.