Cast DirectoryReader only when really requested

This commit is contained in:
Simon Willnauer 2015-10-13 15:54:26 +02:00
parent ba8de12429
commit dac1799305
8 changed files with 24 additions and 15 deletions

View File

@ -579,12 +579,10 @@ public abstract class Engine implements Closeable {
private final String source; private final String source;
private final IndexSearcher searcher; private final IndexSearcher searcher;
private final DirectoryReader reader;
public Searcher(String source, IndexSearcher searcher) { public Searcher(String source, IndexSearcher searcher) {
this.source = source; this.source = source;
this.searcher = searcher; this.searcher = searcher;
this.reader = (DirectoryReader) searcher.getIndexReader();
} }
/** /**
@ -594,8 +592,15 @@ public abstract class Engine implements Closeable {
return source; return source;
} }
public DirectoryReader reader() { public IndexReader reader() {
return reader; return searcher.getIndexReader();
}
public DirectoryReader getDirectoryReader() {
if (reader() instanceof DirectoryReader) {
return (DirectoryReader) reader();
}
throw new IllegalStateException("Can't use " + reader().getClass() + " as an directory reader");
} }
public IndexSearcher searcher() { public IndexSearcher searcher() {

View File

@ -63,11 +63,11 @@ public class IndexSearcherWrapper {
* This is invoked each time a {@link Engine.Searcher} is requested to do an operation. (for example search) * This is invoked each time a {@link Engine.Searcher} is requested to do an operation. (for example search)
*/ */
public final Engine.Searcher wrap(EngineConfig engineConfig, Engine.Searcher engineSearcher) throws IOException { public final Engine.Searcher wrap(EngineConfig engineConfig, Engine.Searcher engineSearcher) throws IOException {
final ElasticsearchDirectoryReader elasticsearchDirectoryReader = ElasticsearchDirectoryReader.getElasticsearchDirectoryReader(engineSearcher.reader()); final ElasticsearchDirectoryReader elasticsearchDirectoryReader = ElasticsearchDirectoryReader.getElasticsearchDirectoryReader(engineSearcher.getDirectoryReader());
if (elasticsearchDirectoryReader == null) { if (elasticsearchDirectoryReader == null) {
throw new IllegalStateException("Can't wrap non elasticsearch directory reader"); throw new IllegalStateException("Can't wrap non elasticsearch directory reader");
} }
DirectoryReader reader = wrap(engineSearcher.reader()); DirectoryReader reader = wrap(engineSearcher.getDirectoryReader());
IndexSearcher innerIndexSearcher = new IndexSearcher(new CacheFriendlyReaderWrapper(reader, elasticsearchDirectoryReader)); IndexSearcher innerIndexSearcher = new IndexSearcher(new CacheFriendlyReaderWrapper(reader, elasticsearchDirectoryReader));
innerIndexSearcher.setQueryCache(engineConfig.getQueryCache()); innerIndexSearcher.setQueryCache(engineConfig.getQueryCache());
innerIndexSearcher.setQueryCachingPolicy(engineConfig.getQueryCachingPolicy()); innerIndexSearcher.setQueryCachingPolicy(engineConfig.getQueryCachingPolicy());

View File

@ -177,10 +177,14 @@ public final class IndicesWarmer extends AbstractComponent {
return searcher; return searcher;
} }
public DirectoryReader reader() { public IndexReader reader() {
return searcher.reader(); return searcher.reader();
} }
public DirectoryReader getDirectoryReader() {
return searcher.getDirectoryReader();
}
@Override @Override
public String toString() { public String toString() {
return "WarmerContext: " + searcher.reader(); return "WarmerContext: " + searcher.reader();

View File

@ -1016,7 +1016,7 @@ public class SearchService extends AbstractLifecycleComponent<SearchService> {
try { try {
final long start = System.nanoTime(); final long start = System.nanoTime();
IndexFieldData.Global ifd = indexFieldDataService.getForField(fieldType); IndexFieldData.Global ifd = indexFieldDataService.getForField(fieldType);
ifd.loadGlobal(context.reader()); ifd.loadGlobal(context.getDirectoryReader());
if (indexShard.warmerService().logger().isTraceEnabled()) { if (indexShard.warmerService().logger().isTraceEnabled()) {
indexShard.warmerService().logger().trace("warmed global ordinals for [{}], took [{}]", fieldType.names().fullName(), TimeValue.timeValueNanos(System.nanoTime() - start)); indexShard.warmerService().logger().trace("warmed global ordinals for [{}], took [{}]", fieldType.names().fullName(), TimeValue.timeValueNanos(System.nanoTime() - start));
} }

View File

@ -41,12 +41,12 @@ public class ContextIndexSearcher extends IndexSearcher implements Releasable {
private AggregatedDfs aggregatedDfs; private AggregatedDfs aggregatedDfs;
private final DirectoryReader directoryReader; private final Engine.Searcher engineSearcher;
public ContextIndexSearcher(SearchContext searchContext, Engine.Searcher searcher) { public ContextIndexSearcher(SearchContext searchContext, Engine.Searcher searcher) {
super(searcher.reader()); super(searcher.reader());
in = searcher.searcher(); in = searcher.searcher();
directoryReader = searcher.reader(); engineSearcher = searcher;
setSimilarity(searcher.searcher().getSimilarity(true)); setSimilarity(searcher.searcher().getSimilarity(true));
setQueryCache(searchContext.getQueryCache()); setQueryCache(searchContext.getQueryCache());
setQueryCachingPolicy(searchContext.indexShard().getQueryCachingPolicy()); setQueryCachingPolicy(searchContext.indexShard().getQueryCachingPolicy());
@ -110,6 +110,6 @@ public class ContextIndexSearcher extends IndexSearcher implements Releasable {
} }
public DirectoryReader getDirectoryReader() { public DirectoryReader getDirectoryReader() {
return directoryReader; return engineSearcher.getDirectoryReader();
} }
} }

View File

@ -1499,7 +1499,7 @@ public class InternalEngineTests extends ESTestCase {
@Test @Test
public void testExtractShardId() { public void testExtractShardId() {
try (Engine.Searcher test = this.engine.acquireSearcher("test")) { try (Engine.Searcher test = this.engine.acquireSearcher("test")) {
ShardId shardId = ShardUtils.extractShardId(test.reader()); ShardId shardId = ShardUtils.extractShardId(test.getDirectoryReader());
assertNotNull(shardId); assertNotNull(shardId);
assertEquals(shardId, engine.config().getShardId()); assertEquals(shardId, engine.config().getShardId());
} }

View File

@ -855,7 +855,7 @@ public class ShadowEngineTests extends ESTestCase {
@Test @Test
public void testExtractShardId() { public void testExtractShardId() {
try (Engine.Searcher test = replicaEngine.acquireSearcher("test")) { try (Engine.Searcher test = replicaEngine.acquireSearcher("test")) {
ShardId shardId = ShardUtils.extractShardId(test.reader()); ShardId shardId = ShardUtils.extractShardId(test.getDirectoryReader());
assertNotNull(shardId); assertNotNull(shardId);
assertEquals(shardId, replicaEngine.config().getShardId()); assertEquals(shardId, replicaEngine.config().getShardId());
} }

View File

@ -75,7 +75,7 @@ public class IndexSearcherWrapperTests extends ESTestCase {
try (Engine.Searcher engineSearcher = new Engine.Searcher("foo", searcher)) { try (Engine.Searcher engineSearcher = new Engine.Searcher("foo", searcher)) {
final Engine.Searcher wrap = wrapper.wrap(ENGINE_CONFIG, engineSearcher); final Engine.Searcher wrap = wrapper.wrap(ENGINE_CONFIG, engineSearcher);
assertEquals(1, wrap.reader().getRefCount()); assertEquals(1, wrap.reader().getRefCount());
ElasticsearchDirectoryReader.addReaderCloseListener(wrap.reader(), reader -> { ElasticsearchDirectoryReader.addReaderCloseListener(wrap.getDirectoryReader(), reader -> {
if (reader == open) { if (reader == open) {
count.incrementAndGet(); count.incrementAndGet();
} }
@ -119,7 +119,7 @@ public class IndexSearcherWrapperTests extends ESTestCase {
final ConcurrentHashMap<Object, TopDocs> cache = new ConcurrentHashMap<>(); final ConcurrentHashMap<Object, TopDocs> cache = new ConcurrentHashMap<>();
try (Engine.Searcher engineSearcher = new Engine.Searcher("foo", searcher)) { try (Engine.Searcher engineSearcher = new Engine.Searcher("foo", searcher)) {
try (final Engine.Searcher wrap = wrapper.wrap(ENGINE_CONFIG, engineSearcher)) { try (final Engine.Searcher wrap = wrapper.wrap(ENGINE_CONFIG, engineSearcher)) {
ElasticsearchDirectoryReader.addReaderCloseListener(wrap.reader(), reader -> { ElasticsearchDirectoryReader.addReaderCloseListener(wrap.getDirectoryReader(), reader -> {
cache.remove(reader.getCoreCacheKey()); cache.remove(reader.getCoreCacheKey());
}); });
TopDocs search = wrap.searcher().search(new TermQuery(new Term("field", "doc")), 1); TopDocs search = wrap.searcher().search(new TermQuery(new Term("field", "doc")), 1);