Skip to content

Apache Lucene open-source search software

SOLR-9524: SolrIndexSearcher.getIndexFingerprint uses dubious synchronization

Noble Paulafc57347b473
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index d1d9cfb65f2..9b34d338ce3 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -137,6 +137,8 @@ Bug Fixes
* SOLR-9512: CloudSolrClient will try and keep up with leader changes if its
state cache points to a down server (Alan Woodward, noble)
+* SOLR-9524: SolrIndexSearcher.getIndexFingerprint uses dubious synchronization (Mike Drob, noble)
+
Optimizations
----------------------
diff --git a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java
index 4c188096aaf..0dec53eb48b 100644
--- a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java
+++ b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java
@@ -36,6 +36,7 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
import com.google.common.base.Function;
import com.google.common.base.Objects;
@@ -2384,15 +2385,19 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrI
* gets a cached version of the IndexFingerprint for this searcher
**/
public IndexFingerprint getIndexFingerprint(long maxVersion) throws IOException {
- IndexFingerprint fingerprint = maxVersionFingerprintCache.get(maxVersion);
- if (fingerprint != null) return fingerprint;
- // possibly expensive, so prevent more than one thread from calculating it for this searcher
- synchronized (maxVersionFingerprintCache) {
- fingerprint = maxVersionFingerprintCache.get(maxVersionFingerprintCache);
- if (fingerprint != null) return fingerprint;
- fingerprint = IndexFingerprint.getFingerprint(this, maxVersion);
- maxVersionFingerprintCache.put(maxVersion, fingerprint);
- return fingerprint;
+ final SolrIndexSearcher searcher = this;
+ final AtomicReference<IOException> exception = new AtomicReference<>();
+ try {
+ return maxVersionFingerprintCache.computeIfAbsent(maxVersion, key -> {
+ try {
+ return IndexFingerprint.getFingerprint(searcher, key);
+ } catch (IOException e) {
+ exception.set(e);
+ return null;
+ }
+ });
+ } finally {
+ if (exception.get() != null) throw exception.get();
}
}