forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid wrapping searchers multiple times in mget
- Loading branch information
Showing
4 changed files
with
164 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
server/src/main/java/org/elasticsearch/index/shard/MultiEngineGet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.index.shard; | ||
|
||
import org.apache.lucene.index.IndexReader; | ||
import org.elasticsearch.core.Releasable; | ||
import org.elasticsearch.core.Releasables; | ||
import org.elasticsearch.index.engine.Engine; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A session that can perform multiple gets without wrapping searchers multiple times. | ||
* This must be created and used by a single thread. | ||
*/ | ||
abstract class MultiEngineGet implements Releasable { | ||
private final Map<IndexReader.CacheKey, Engine.Searcher> caches = new HashMap<>(); | ||
private final Thread creationThread; | ||
private final Function<Engine.Searcher, Engine.Searcher> wrapper; | ||
|
||
MultiEngineGet(Function<Engine.Searcher, Engine.Searcher> wrapper) { | ||
this.creationThread = Thread.currentThread(); | ||
this.wrapper = wrapper; | ||
} | ||
|
||
private boolean assertAccessingThread() { | ||
assert creationThread == Thread.currentThread() | ||
: "created by [" + creationThread + "] != current thread [" + Thread.currentThread() + "]"; | ||
return true; | ||
} | ||
|
||
abstract Engine.GetResult engineGet(Engine.Get get); | ||
|
||
Engine.Searcher wrapSearchWithCache(Engine.Searcher searcher) { | ||
assert assertAccessingThread(); | ||
final IndexReader.CacheHelper cacheHelper = searcher.getIndexReader().getReaderCacheHelper(); | ||
final IndexReader.CacheKey cacheKey = cacheHelper != null ? cacheHelper.getKey() : null; | ||
if (cacheKey == null) { | ||
return wrapper.apply(searcher); | ||
} | ||
final Engine.Searcher wrapped = caches.computeIfAbsent(cacheKey, k -> wrapper.apply(searcher)); | ||
return new Engine.Searcher( | ||
wrapped.source(), | ||
wrapped.getIndexReader(), | ||
wrapped.getSimilarity(), | ||
wrapped.getQueryCache(), | ||
wrapped.getQueryCachingPolicy(), | ||
() -> {} | ||
); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
Releasables.close(caches.values()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters