Skip to content

Commit

Permalink
Reduce synchronization on field data cache
Browse files Browse the repository at this point in the history
The field data cache can come under heavy contention in cases when lots
of search threads are hitting it for doc values. This commit reduces the
amount of contention here by using a double-checked locking strategy to
only lock when the cache needs to be initialized.

Relates #27365
  • Loading branch information
tinder-xli authored and jasontedor committed Nov 14, 2017
1 parent ea98113 commit bda9257
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,24 @@ public <IFD extends IndexFieldData<?>> IFD getForField(MappedFieldType fieldType
final String fieldName = fieldType.name();
IndexFieldData.Builder builder = fieldType.fielddataBuilder();

IndexFieldDataCache cache;
synchronized (this) {
cache = fieldDataCaches.get(fieldName);
if (cache == null) {
String cacheType = indexSettings.getValue(INDEX_FIELDDATA_CACHE_KEY);
if (FIELDDATA_CACHE_VALUE_NODE.equals(cacheType)) {
cache = indicesFieldDataCache.buildIndexFieldDataCache(listener, index(), fieldName);
} else if ("none".equals(cacheType)){
cache = new IndexFieldDataCache.None();
} else {
throw new IllegalArgumentException("cache type not supported [" + cacheType + "] for field [" + fieldName + "]");
IndexFieldDataCache cache = fieldDataCaches.get(fieldName);
if (cache == null) {
//for perf reason, only synchronize when cache is null
synchronized (this) {
cache = fieldDataCaches.get(fieldName);
//double checked locking to make sure it is thread safe
//especially when other threads calling clear() or clearField()
if (cache == null) {
String cacheType = indexSettings.getValue(INDEX_FIELDDATA_CACHE_KEY);
if (FIELDDATA_CACHE_VALUE_NODE.equals(cacheType)) {
cache = indicesFieldDataCache.buildIndexFieldDataCache(listener, index(), fieldName);
} else if ("none".equals(cacheType)){
cache = new IndexFieldDataCache.None();
} else {
throw new IllegalArgumentException("cache type not supported [" + cacheType + "] for field [" + fieldName + "]");
}
fieldDataCaches.put(fieldName, cache);
}
fieldDataCaches.put(fieldName, cache);
}
}

Expand Down

0 comments on commit bda9257

Please sign in to comment.