Skip to content

Commit

Permalink
CAMEL-21614: camel-core - Prevent cache change miss on queue swap
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo committed Jan 15, 2025
1 parent 208408b commit 72f1d16
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -49,6 +51,10 @@ public class SimpleLRUCache<K, V> extends ConcurrentHashMap<K, V> {
* The flag indicating that an eviction process is in progress.
*/
private final AtomicBoolean eviction = new AtomicBoolean();
/**
* The lock to prevent the addition of changes during the swap of queue of changes.
*/
private final ReadWriteLock swapLock = new ReentrantReadWriteLock();
/**
* The maximum cache size.
*/
Expand Down Expand Up @@ -84,7 +90,13 @@ private V addChange(OperationContext<K, V> context, Function<? super K, ? extend
if (value == null) {
return null;
}
lastChanges.get().add(Map.entry(key, value));
Entry<K, V> entry = Map.entry(key, value);
swapLock.readLock().lock();
try {
lastChanges.get().add(entry);
} finally {
swapLock.readLock().unlock();
}
return value;
}

Expand Down Expand Up @@ -269,7 +281,13 @@ private Entry<K, V> nextOldestChange() {
*/
private void compressChanges() {
Deque<Entry<K, V>> newChanges = new ConcurrentLinkedDeque<>();
Deque<Entry<K, V>> currentChanges = lastChanges.getAndSet(newChanges);
Deque<Entry<K, V>> currentChanges;
swapLock.writeLock().lock();
try {
currentChanges = lastChanges.getAndSet(newChanges);
} finally {
swapLock.writeLock().unlock();
}
Set<K> keys = new HashSet<>();
Entry<K, V> entry;
while ((entry = currentChanges.pollLast()) != null) {
Expand Down

0 comments on commit 72f1d16

Please # to comment.