fix cache race

can't use linkedhashmap concurrently like that
This commit is contained in:
2025-03-08 14:19:09 +01:00
parent b94aad2e02
commit 51a6ba10d0
2 changed files with 12 additions and 13 deletions

View File

@@ -156,7 +156,7 @@ public class JObjectManager {
if (dep.get().version() > snapshotId) {
Log.trace("Checking dependency " + read.getKey() + " - newer than");
throw new TxCommitException("Serialization hazard: " + dep.get().version() + " vs " + snapshotId);
throw new TxCommitException("Serialization hazard: " + dep.get().data().key() + " " + dep.get().version() + " vs " + snapshotId);
}
Log.trace("Checking dependency " + read.getKey() + " - ok with read");

View File

@@ -19,12 +19,12 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
@ApplicationScoped
public class CachingObjectPersistentStore {
private final LinkedHashMap<JObjectKey, CacheEntry> _cache = new LinkedHashMap<>(8, 0.75f, true);
private final LinkedHashMap<JObjectKey, CacheEntry> _cache = new LinkedHashMap<>();
private TreePMap<JObjectKey, CacheEntry> _sortedCache = TreePMap.empty();
private long _cacheVersion = 0;
private final ReentrantReadWriteLock _lock = new ReentrantReadWriteLock();
private final DataLocker _locker = new DataLocker();
private final DataLocker _readerLocker = new DataLocker();
@Inject
SerializingObjectPersistentStore delegate;
@@ -83,17 +83,16 @@ public class CachingObjectPersistentStore {
@Nonnull
public Optional<JDataVersionedWrapper> readObject(JObjectKey name) {
try (var lock = _locker.lock(name)) {
_lock.readLock().lock();
try {
var got = _cache.get(name);
if (got != null) {
return got.object().opt();
}
} finally {
_lock.readLock().unlock();
_lock.readLock().lock();
try {
var got = _cache.get(name);
if (got != null) {
return got.object().opt();
}
} finally {
_lock.readLock().unlock();
}
try (var lock = _readerLocker.lock(name)) {
// TODO: This is possibly racy
// var got = delegate.readObject(name);
// put(name, got);