From a6abb4fcddfc0d2ea881491f0ca16551680cde5a Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Thu, 8 Oct 2015 18:15:52 -0400 Subject: [PATCH] Comment regarding synchronization in Cache#computeIfAbsent --- core/src/main/java/org/elasticsearch/common/cache/Cache.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/common/cache/Cache.java b/core/src/main/java/org/elasticsearch/common/cache/Cache.java index 225b8fa5ea5..624227383a5 100644 --- a/core/src/main/java/org/elasticsearch/common/cache/Cache.java +++ b/core/src/main/java/org/elasticsearch/common/cache/Cache.java @@ -300,6 +300,10 @@ public class Cache { V value = get(key, now); if (value == null) { CacheSegment segment = getCacheSegment(key); + // we synchronize against the segment lock; this is to avoid a scenario where another thread is inserting + // a value for the same key via put which would not be observed on this thread without a mechanism + // synchronizing the two threads; it is possible that the segment lock will be too expensive here (it blocks + // readers too!) so consider this as a possible place to optimize should contention be observed try (ReleasableLock ignored = segment.writeLock.acquire()) { value = get(key, now); if (value == null) {