add internal source to acquire searcher

add a source indicating why the searcher was acquired
This commit is contained in:
Shay Banon 2013-09-22 18:29:23 +02:00
parent 167538ef0d
commit 78af818d72
16 changed files with 101 additions and 70 deletions

View File

@ -157,7 +157,7 @@ public class TransportIndicesStatusAction extends TransportBroadcastOperationAct
// shardStatus.estimatedFlushableMemorySize = indexShard.estimateFlushableMemorySize(); // shardStatus.estimatedFlushableMemorySize = indexShard.estimateFlushableMemorySize();
shardStatus.translogId = indexShard.translog().currentId(); shardStatus.translogId = indexShard.translog().currentId();
shardStatus.translogOperations = indexShard.translog().estimatedNumberOfOperations(); shardStatus.translogOperations = indexShard.translog().estimatedNumberOfOperations();
Engine.Searcher searcher = indexShard.acquireSearcher(); Engine.Searcher searcher = indexShard.acquireSearcher("indices_status");
try { try {
shardStatus.docs = new DocsStatus(); shardStatus.docs = new DocsStatus();
shardStatus.docs.numDocs = searcher.reader().numDocs(); shardStatus.docs.numDocs = searcher.reader().numDocs();

View File

@ -179,7 +179,7 @@ public class TransportValidateQueryAction extends TransportBroadcastOperationAct
} else { } else {
SearchContext.setCurrent(new DefaultSearchContext(0, SearchContext.setCurrent(new DefaultSearchContext(0,
new ShardSearchRequest().types(request.types()).nowInMillis(request.nowInMillis()), new ShardSearchRequest().types(request.types()).nowInMillis(request.nowInMillis()),
null, indexShard.acquireSearcher(), indexService, indexShard, null, indexShard.acquireSearcher("validate_query"), indexService, indexShard,
scriptService, cacheRecycler)); scriptService, cacheRecycler));
try { try {
ParsedQuery parsedQuery = queryParserService.parse(request.querySource()); ParsedQuery parsedQuery = queryParserService.parse(request.querySource());

View File

@ -163,7 +163,7 @@ public class TransportCountAction extends TransportBroadcastOperationAction<Coun
new ShardSearchRequest().types(request.types()) new ShardSearchRequest().types(request.types())
.filteringAliases(request.filteringAliases()) .filteringAliases(request.filteringAliases())
.nowInMillis(request.nowInMillis()), .nowInMillis(request.nowInMillis()),
shardTarget, indexShard.acquireSearcher(), indexService, indexShard, shardTarget, indexShard.acquireSearcher("count"), indexService, indexShard,
scriptService, cacheRecycler); scriptService, cacheRecycler);
SearchContext.setCurrent(context); SearchContext.setCurrent(context);

View File

@ -149,7 +149,7 @@ public class TransportSuggestAction extends TransportBroadcastOperationAction<Su
protected ShardSuggestResponse shardOperation(ShardSuggestRequest request) throws ElasticSearchException { protected ShardSuggestResponse shardOperation(ShardSuggestRequest request) throws ElasticSearchException {
IndexService indexService = indicesService.indexServiceSafe(request.index()); IndexService indexService = indicesService.indexServiceSafe(request.index());
IndexShard indexShard = indexService.shardSafe(request.shardId()); IndexShard indexShard = indexService.shardSafe(request.shardId());
final Engine.Searcher searcher = indexShard.acquireSearcher(); final Engine.Searcher searcher = indexShard.acquireSearcher("suggest");
XContentParser parser = null; XContentParser parser = null;
try { try {
BytesReference suggest = request.suggest(); BytesReference suggest = request.suggest();

View File

@ -82,13 +82,13 @@ public interface Engine extends IndexShardComponent, CloseableComponent {
GetResult get(Get get) throws EngineException; GetResult get(Get get) throws EngineException;
/** /**
* Retruns a new searcher instance. The consumer of this * Returns a new searcher instance. The consumer of this
* API is responsible for releasing the returned seacher in a * API is responsible for releasing the returned seacher in a
* safe manner, preferrablly in a try/finally block. * safe manner, preferably in a try/finally block.
* *
* @see Searcher#release() * @see Searcher#release()
*/ */
Searcher acquireSearcher() throws EngineException; Searcher acquireSearcher(String source) throws EngineException;
List<Segment> segments(); List<Segment> segments();
@ -160,6 +160,11 @@ public interface Engine extends IndexShardComponent, CloseableComponent {
static interface Searcher extends Releasable { static interface Searcher extends Releasable {
/**
* The source that caused this searcher to be acquired.
*/
String source();
IndexReader reader(); IndexReader reader();
IndexSearcher searcher(); IndexSearcher searcher();
@ -167,12 +172,19 @@ public interface Engine extends IndexShardComponent, CloseableComponent {
static class SimpleSearcher implements Searcher { static class SimpleSearcher implements Searcher {
private final String source;
private final IndexSearcher searcher; private final IndexSearcher searcher;
public SimpleSearcher(IndexSearcher searcher) { public SimpleSearcher(String source, IndexSearcher searcher) {
this.source = source;
this.searcher = searcher; this.searcher = searcher;
} }
@Override
public String source() {
return this.source();
}
@Override @Override
public IndexReader reader() { public IndexReader reader() {
return searcher.getIndexReader(); return searcher.getIndexReader();

View File

@ -337,7 +337,7 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine {
} }
// no version, get the version from the index, we know that we refresh on flush // no version, get the version from the index, we know that we refresh on flush
Searcher searcher = acquireSearcher(); Searcher searcher = acquireSearcher("get");
final Versions.DocIdAndVersion docIdAndVersion; final Versions.DocIdAndVersion docIdAndVersion;
try { try {
docIdAndVersion = Versions.loadDocIdAndVersion(searcher.reader(), get.uid()); docIdAndVersion = Versions.loadDocIdAndVersion(searcher.reader(), get.uid());
@ -676,19 +676,19 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine {
} }
@Override @Override
public final Searcher acquireSearcher() throws EngineException { public final Searcher acquireSearcher(String source) throws EngineException {
SearcherManager manager = this.searcherManager; SearcherManager manager = this.searcherManager;
try { try {
IndexSearcher searcher = manager.acquire(); IndexSearcher searcher = manager.acquire();
return newSearcher(searcher, manager); return newSearcher(source, searcher, manager);
} catch (IOException ex) { } catch (IOException ex) {
logger.error("failed to accquire searcher for shard [{}]", ex, shardId); logger.error("failed to acquire searcher, source {}", ex, source);
throw new EngineException(shardId, ex.getMessage()); throw new EngineException(shardId, ex.getMessage());
} }
} }
protected Searcher newSearcher(IndexSearcher searcher, SearcherManager manager) { protected Searcher newSearcher(String source, IndexSearcher searcher, SearcherManager manager) {
return new RobinSearcher(searcher, manager); return new RobinSearcher(source, searcher, manager);
} }
@Override @Override
@ -1128,7 +1128,7 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine {
Map<String, Segment> segments = new HashMap<String, Segment>(); Map<String, Segment> segments = new HashMap<String, Segment>();
// first, go over and compute the search ones... // first, go over and compute the search ones...
Searcher searcher = acquireSearcher(); Searcher searcher = acquireSearcher("segments");
try { try {
for (AtomicReaderContext reader : searcher.reader().leaves()) { for (AtomicReaderContext reader : searcher.reader().leaves()) {
assert reader.reader() instanceof SegmentReader; assert reader.reader() instanceof SegmentReader;
@ -1279,7 +1279,7 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine {
} }
private long loadCurrentVersionFromIndex(Term uid) throws IOException { private long loadCurrentVersionFromIndex(Term uid) throws IOException {
Searcher searcher = acquireSearcher(); Searcher searcher = acquireSearcher("load_version");
try { try {
return Versions.loadVersion(searcher.reader(), uid); return Versions.loadVersion(searcher.reader(), uid);
} finally { } finally {
@ -1402,14 +1402,21 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine {
static class RobinSearcher implements Searcher { static class RobinSearcher implements Searcher {
private final String source;
private final IndexSearcher searcher; private final IndexSearcher searcher;
private final SearcherManager manager; private final SearcherManager manager;
private RobinSearcher(IndexSearcher searcher, SearcherManager manager) { private RobinSearcher(String source, IndexSearcher searcher, SearcherManager manager) {
this.source = source;
this.searcher = searcher; this.searcher = searcher;
this.manager = manager; this.manager = manager;
} }
@Override
public String source() {
return this.source;
}
@Override @Override
public IndexReader reader() { public IndexReader reader() {
return searcher.getIndexReader(); return searcher.getIndexReader();
@ -1478,7 +1485,7 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine {
// fresh index writer, just do on all of it // fresh index writer, just do on all of it
newSearcher = searcher; newSearcher = searcher;
} else { } else {
currentSearcher = acquireSearcher(); currentSearcher = acquireSearcher("search_factory");
// figure out the newSearcher, with only the new readers that are relevant for us // figure out the newSearcher, with only the new readers that are relevant for us
List<IndexReader> readers = Lists.newArrayList(); List<IndexReader> readers = Lists.newArrayList();
for (AtomicReaderContext newReaderContext : searcher.getIndexReader().leaves()) { for (AtomicReaderContext newReaderContext : searcher.getIndexReader().leaves()) {
@ -1502,8 +1509,8 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine {
if (newSearcher != null) { if (newSearcher != null) {
IndicesWarmer.WarmerContext context = new IndicesWarmer.WarmerContext(shardId, IndicesWarmer.WarmerContext context = new IndicesWarmer.WarmerContext(shardId,
new SimpleSearcher(searcher), new SimpleSearcher("warmer", searcher),
new SimpleSearcher(newSearcher)); new SimpleSearcher("warmer", newSearcher));
warmer.warm(context); warmer.warm(context);
} }
} catch (Throwable e) { } catch (Throwable e) {

View File

@ -227,7 +227,7 @@ public class PercolatorQueriesRegistry extends AbstractIndexShardComponent {
private void loadQueries(IndexShard shard) { private void loadQueries(IndexShard shard) {
try { try {
shard.refresh(new Engine.Refresh().force(true).source("percolator_load_queries")); shard.refresh(new Engine.Refresh().force(true).source("percolator_load_queries"));
Engine.Searcher searcher = shard.acquireSearcher(); Engine.Searcher searcher = shard.acquireSearcher("percolator_load_queries");
try { try {
Query query = new XConstantScoreQuery( Query query = new XConstantScoreQuery(
indexCache.filter().cache( indexCache.filter().cache(

View File

@ -98,7 +98,7 @@ public interface IndexShard extends IndexShardComponent {
FieldDataStats fieldDataStats(String... fields); FieldDataStats fieldDataStats(String... fields);
CompletionStats completionStats(String ... fields); CompletionStats completionStats(String... fields);
PercolatorQueriesRegistry percolateRegistry(); PercolatorQueriesRegistry percolateRegistry();
@ -136,7 +136,7 @@ public interface IndexShard extends IndexShardComponent {
void recover(Engine.RecoveryHandler recoveryHandler) throws EngineException; void recover(Engine.RecoveryHandler recoveryHandler) throws EngineException;
Engine.Searcher acquireSearcher(); Engine.Searcher acquireSearcher(String source);
/** /**
* Returns <tt>true</tt> if this shard can ignore a recovery attempt made to it (since the already doing/done it) * Returns <tt>true</tt> if this shard can ignore a recovery attempt made to it (since the already doing/done it)

View File

@ -457,7 +457,7 @@ public class InternalIndexShard extends AbstractIndexShardComponent implements I
@Override @Override
public DocsStats docStats() { public DocsStats docStats() {
try { try {
final Engine.Searcher searcher = acquireSearcher(); final Engine.Searcher searcher = acquireSearcher("doc_stats");
try { try {
return new DocsStats(searcher.reader().numDocs(), searcher.reader().numDeletedDocs()); return new DocsStats(searcher.reader().numDocs(), searcher.reader().numDeletedDocs());
} finally { } finally {
@ -533,7 +533,7 @@ public class InternalIndexShard extends AbstractIndexShardComponent implements I
public CompletionStats completionStats(String... fields) { public CompletionStats completionStats(String... fields) {
CompletionStats completionStats = new CompletionStats(); CompletionStats completionStats = new CompletionStats();
try { try {
final Engine.Searcher currentSearcher = acquireSearcher(); final Engine.Searcher currentSearcher = acquireSearcher("completion_stats");
try { try {
PostingsFormat postingsFormat = this.codecService.postingsFormatService().get(Completion090PostingsFormat.CODEC_NAME).get(); PostingsFormat postingsFormat = this.codecService.postingsFormatService().get(Completion090PostingsFormat.CODEC_NAME).get();
if (postingsFormat instanceof Completion090PostingsFormat) { if (postingsFormat instanceof Completion090PostingsFormat) {
@ -591,9 +591,9 @@ public class InternalIndexShard extends AbstractIndexShardComponent implements I
} }
@Override @Override
public Engine.Searcher acquireSearcher() { public Engine.Searcher acquireSearcher(String source) {
readAllowed(); readAllowed();
return engine.acquireSearcher(); return engine.acquireSearcher(source);
} }
public void close(String reason) { public void close(String reason) {

View File

@ -58,7 +58,7 @@ public class ShardTermVectorService extends AbstractIndexShardComponent {
} }
public TermVectorResponse getTermVector(TermVectorRequest request) { public TermVectorResponse getTermVector(TermVectorRequest request) {
final Engine.Searcher searcher = indexShard.acquireSearcher(); final Engine.Searcher searcher = indexShard.acquireSearcher("term_vector");
IndexReader topLevelReader = searcher.reader(); IndexReader topLevelReader = searcher.reader();
final TermVectorResponse termVectorResponse = new TermVectorResponse(request.index(), request.type(), request.id()); final TermVectorResponse termVectorResponse = new TermVectorResponse(request.index(), request.type(), request.id());
final Term uidTerm = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(request.type(), request.id())); final Term uidTerm = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(request.type(), request.id()));

View File

@ -179,7 +179,7 @@ public class IndicesTTLService extends AbstractLifecycleComponent<IndicesTTLServ
private void purgeShards(List<IndexShard> shardsToPurge) { private void purgeShards(List<IndexShard> shardsToPurge) {
for (IndexShard shardToPurge : shardsToPurge) { for (IndexShard shardToPurge : shardsToPurge) {
Query query = NumericRangeQuery.newLongRange(TTLFieldMapper.NAME, null, System.currentTimeMillis(), false, true); Query query = NumericRangeQuery.newLongRange(TTLFieldMapper.NAME, null, System.currentTimeMillis(), false, true);
Engine.Searcher searcher = shardToPurge.acquireSearcher(); Engine.Searcher searcher = shardToPurge.acquireSearcher("indices_ttl");
try { try {
logger.debug("[{}][{}] purging shard", shardToPurge.routingEntry().index(), shardToPurge.routingEntry().id()); logger.debug("[{}][{}] purging shard", shardToPurge.routingEntry().index(), shardToPurge.routingEntry().id());
ExpiredDocsCollector expiredDocsCollector = new ExpiredDocsCollector(shardToPurge.routingEntry().index()); ExpiredDocsCollector expiredDocsCollector = new ExpiredDocsCollector(shardToPurge.routingEntry().index());

View File

@ -117,6 +117,11 @@ public class PercolateContext extends SearchContext {
final IndexReader topLevelReader = docSearcher.getIndexReader(); final IndexReader topLevelReader = docSearcher.getIndexReader();
AtomicReaderContext readerContext = topLevelReader.leaves().get(0); AtomicReaderContext readerContext = topLevelReader.leaves().get(0);
docEngineSearcher = new Engine.Searcher() { docEngineSearcher = new Engine.Searcher() {
@Override
public String source() {
return "percolate";
}
@Override @Override
public IndexReader reader() { public IndexReader reader() {
return topLevelReader; return topLevelReader;

View File

@ -112,7 +112,7 @@ public class PercolatorService extends AbstractComponent {
return new ExtendedMemoryIndex(true, maxReuseBytes); return new ExtendedMemoryIndex(true, maxReuseBytes);
} }
}; };
percolatorTypes = new TByteObjectHashMap<PercolatorType>(6); percolatorTypes = new TByteObjectHashMap<PercolatorType>(6);
percolatorTypes.put(countPercolator.id(), countPercolator); percolatorTypes.put(countPercolator.id(), countPercolator);
percolatorTypes.put(queryCountPercolator.id(), queryCountPercolator); percolatorTypes.put(queryCountPercolator.id(), queryCountPercolator);
@ -416,7 +416,7 @@ public class PercolatorService extends AbstractComponent {
@Override @Override
public PercolateShardResponse doPercolate(PercolateShardRequest request, PercolateContext context) { public PercolateShardResponse doPercolate(PercolateShardRequest request, PercolateContext context) {
long count = 0; long count = 0;
Engine.Searcher percolatorSearcher = context.indexShard().acquireSearcher(); Engine.Searcher percolatorSearcher = context.indexShard().acquireSearcher("percolate");
try { try {
Count countCollector = count(logger, context); Count countCollector = count(logger, context);
queryBasedPercolating(percolatorSearcher, context, countCollector); queryBasedPercolating(percolatorSearcher, context, countCollector);
@ -450,7 +450,8 @@ public class PercolatorService extends AbstractComponent {
// Use a custom impl of AbstractBigArray for Object[]? // Use a custom impl of AbstractBigArray for Object[]?
List<PercolateResponse.Match> finalMatches = new ArrayList<PercolateResponse.Match>(requestedSize == 0 ? numMatches : requestedSize); List<PercolateResponse.Match> finalMatches = new ArrayList<PercolateResponse.Match>(requestedSize == 0 ? numMatches : requestedSize);
outer: for (PercolateShardResponse response : shardResults) { outer:
for (PercolateShardResponse response : shardResults) {
Text index = new StringText(response.getIndex()); Text index = new StringText(response.getIndex());
for (int i = 0; i < response.matches().length; i++) { for (int i = 0; i < response.matches().length; i++) {
float score = response.scores().length == 0 ? NO_SCORE : response.scores()[i]; float score = response.scores().length == 0 ? NO_SCORE : response.scores()[i];
@ -515,7 +516,7 @@ public class PercolatorService extends AbstractComponent {
@Override @Override
public PercolateShardResponse doPercolate(PercolateShardRequest request, PercolateContext context) { public PercolateShardResponse doPercolate(PercolateShardRequest request, PercolateContext context) {
Engine.Searcher percolatorSearcher = context.indexShard().acquireSearcher(); Engine.Searcher percolatorSearcher = context.indexShard().acquireSearcher("percolate");
try { try {
Match match = match(logger, context, highlightPhase); Match match = match(logger, context, highlightPhase);
queryBasedPercolating(percolatorSearcher, context, match); queryBasedPercolating(percolatorSearcher, context, match);
@ -548,7 +549,7 @@ public class PercolatorService extends AbstractComponent {
@Override @Override
public PercolateShardResponse doPercolate(PercolateShardRequest request, PercolateContext context) { public PercolateShardResponse doPercolate(PercolateShardRequest request, PercolateContext context) {
Engine.Searcher percolatorSearcher = context.indexShard().acquireSearcher(); Engine.Searcher percolatorSearcher = context.indexShard().acquireSearcher("percolate");
try { try {
MatchAndScore matchAndScore = matchAndScore(logger, context, highlightPhase); MatchAndScore matchAndScore = matchAndScore(logger, context, highlightPhase);
queryBasedPercolating(percolatorSearcher, context, matchAndScore); queryBasedPercolating(percolatorSearcher, context, matchAndScore);
@ -658,7 +659,7 @@ public class PercolatorService extends AbstractComponent {
@Override @Override
public PercolateShardResponse doPercolate(PercolateShardRequest request, PercolateContext context) { public PercolateShardResponse doPercolate(PercolateShardRequest request, PercolateContext context) {
Engine.Searcher percolatorSearcher = context.indexShard().acquireSearcher(); Engine.Searcher percolatorSearcher = context.indexShard().acquireSearcher("percolate");
try { try {
MatchAndSort matchAndSort = QueryCollector.matchAndSort(logger, context); MatchAndSort matchAndSort = QueryCollector.matchAndSort(logger, context);
queryBasedPercolating(percolatorSearcher, context, matchAndSort); queryBasedPercolating(percolatorSearcher, context, matchAndSort);

View File

@ -456,7 +456,7 @@ public class SearchService extends AbstractLifecycleComponent<SearchService> {
SearchShardTarget shardTarget = new SearchShardTarget(clusterService.localNode().id(), request.index(), request.shardId()); SearchShardTarget shardTarget = new SearchShardTarget(clusterService.localNode().id(), request.index(), request.shardId());
Engine.Searcher engineSearcher = searcher == null ? indexShard.acquireSearcher() : searcher; Engine.Searcher engineSearcher = searcher == null ? indexShard.acquireSearcher("search") : searcher;
SearchContext context = new DefaultSearchContext(idGenerator.incrementAndGet(), request, shardTarget, engineSearcher, indexService, indexShard, scriptService, cacheRecycler); SearchContext context = new DefaultSearchContext(idGenerator.incrementAndGet(), request, shardTarget, engineSearcher, indexService, indexShard, scriptService, cacheRecycler);
SearchContext.setCurrent(context); SearchContext.setCurrent(context);
try { try {
@ -566,7 +566,7 @@ public class SearchService extends AbstractLifecycleComponent<SearchService> {
} }
} }
} }
private static final int[] EMPTY_DOC_IDS = new int[0]; private static final int[] EMPTY_DOC_IDS = new int[0];
/** /**

View File

@ -299,7 +299,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
@Test @Test
public void testSimpleOperations() throws Exception { public void testSimpleOperations() throws Exception {
Engine.Searcher searchResult = engine.acquireSearcher(); Engine.Searcher searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0));
searchResult.release(); searchResult.release();
@ -310,7 +310,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
engine.create(new Engine.Create(null, newUid("1"), doc)); engine.create(new Engine.Create(null, newUid("1"), doc));
// its not there... // its not there...
searchResult = engine.acquireSearcher(); searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0));
searchResult.release(); searchResult.release();
@ -321,7 +321,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
assertThat(getResult.source().source.toBytesArray(), equalTo(B_1.toBytesArray())); assertThat(getResult.source().source.toBytesArray(), equalTo(B_1.toBytesArray()));
assertThat(getResult.docIdAndVersion(), nullValue()); assertThat(getResult.docIdAndVersion(), nullValue());
getResult.release(); getResult.release();
// but, not there non realtime // but, not there non realtime
getResult = engine.get(new Engine.Get(false, newUid("1"))); getResult = engine.get(new Engine.Get(false, newUid("1")));
assertThat(getResult.exists(), equalTo(false)); assertThat(getResult.exists(), equalTo(false));
@ -330,7 +330,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
engine.refresh(new Engine.Refresh().force(false)); engine.refresh(new Engine.Refresh().force(false));
// now its there... // now its there...
searchResult = engine.acquireSearcher(); searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 1));
searchResult.release(); searchResult.release();
@ -340,7 +340,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
assertThat(getResult.exists(), equalTo(true)); assertThat(getResult.exists(), equalTo(true));
assertThat(getResult.docIdAndVersion(), notNullValue()); assertThat(getResult.docIdAndVersion(), notNullValue());
getResult.release(); getResult.release();
// now do an update // now do an update
document = testDocument(); document = testDocument();
document.add(new TextField("value", "test1", Field.Store.YES)); document.add(new TextField("value", "test1", Field.Store.YES));
@ -349,7 +349,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
engine.index(new Engine.Index(null, newUid("1"), doc)); engine.index(new Engine.Index(null, newUid("1"), doc));
// its not updated yet... // its not updated yet...
searchResult = engine.acquireSearcher(); searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 1));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 0));
@ -361,11 +361,11 @@ public class RobinEngineTests extends ElasticSearchTestCase {
assertThat(getResult.source().source.toBytesArray(), equalTo(B_2.toBytesArray())); assertThat(getResult.source().source.toBytesArray(), equalTo(B_2.toBytesArray()));
assertThat(getResult.docIdAndVersion(), nullValue()); assertThat(getResult.docIdAndVersion(), nullValue());
getResult.release(); getResult.release();
// refresh and it should be updated // refresh and it should be updated
engine.refresh(new Engine.Refresh().force(false)); engine.refresh(new Engine.Refresh().force(false));
searchResult = engine.acquireSearcher(); searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 1));
@ -375,7 +375,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
engine.delete(new Engine.Delete("test", "1", newUid("1"))); engine.delete(new Engine.Delete("test", "1", newUid("1")));
// its not deleted yet // its not deleted yet
searchResult = engine.acquireSearcher(); searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 1));
@ -385,11 +385,11 @@ public class RobinEngineTests extends ElasticSearchTestCase {
getResult = engine.get(new Engine.Get(true, newUid("1"))); getResult = engine.get(new Engine.Get(true, newUid("1")));
assertThat(getResult.exists(), equalTo(false)); assertThat(getResult.exists(), equalTo(false));
getResult.release(); getResult.release();
// refresh and it should be deleted // refresh and it should be deleted
engine.refresh(new Engine.Refresh().force(false)); engine.refresh(new Engine.Refresh().force(false));
searchResult = engine.acquireSearcher(); searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 0));
@ -402,7 +402,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
engine.create(new Engine.Create(null, newUid("1"), doc)); engine.create(new Engine.Create(null, newUid("1"), doc));
// its not there... // its not there...
searchResult = engine.acquireSearcher(); searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 0));
@ -412,7 +412,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
engine.refresh(new Engine.Refresh().force(false)); engine.refresh(new Engine.Refresh().force(false));
// now its there... // now its there...
searchResult = engine.acquireSearcher(); searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 1));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 0));
@ -436,7 +436,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
engine.index(new Engine.Index(null, newUid("1"), doc)); engine.index(new Engine.Index(null, newUid("1"), doc));
// its not updated yet... // its not updated yet...
searchResult = engine.acquireSearcher(); searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 1));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 0));
@ -445,7 +445,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
// refresh and it should be updated // refresh and it should be updated
engine.refresh(new Engine.Refresh().force(false)); engine.refresh(new Engine.Refresh().force(false));
searchResult = engine.acquireSearcher(); searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 1));
@ -456,7 +456,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
@Test @Test
public void testSearchResultRelease() throws Exception { public void testSearchResultRelease() throws Exception {
Engine.Searcher searchResult = engine.acquireSearcher(); Engine.Searcher searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0));
searchResult.release(); searchResult.release();
@ -465,7 +465,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
engine.create(new Engine.Create(null, newUid("1"), doc)); engine.create(new Engine.Create(null, newUid("1"), doc));
// its not there... // its not there...
searchResult = engine.acquireSearcher(); searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0));
searchResult.release(); searchResult.release();
@ -474,7 +474,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
engine.refresh(new Engine.Refresh().force(false)); engine.refresh(new Engine.Refresh().force(false));
// now its there... // now its there...
searchResult = engine.acquireSearcher(); searchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1));
MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 1)); MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 1));
// don't release the search result yet... // don't release the search result yet...
@ -482,7 +482,7 @@ public class RobinEngineTests extends ElasticSearchTestCase {
// delete, refresh and do a new search, it should not be there // delete, refresh and do a new search, it should not be there
engine.delete(new Engine.Delete("test", "1", newUid("1"))); engine.delete(new Engine.Delete("test", "1", newUid("1")));
engine.refresh(new Engine.Refresh().force(false)); engine.refresh(new Engine.Refresh().force(false));
Engine.Searcher updateSearchResult = engine.acquireSearcher(); Engine.Searcher updateSearchResult = engine.acquireSearcher("test");
MatcherAssert.assertThat(updateSearchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0)); MatcherAssert.assertThat(updateSearchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(0));
updateSearchResult.release(); updateSearchResult.release();

View File

@ -54,16 +54,17 @@ import java.util.concurrent.ConcurrentMap;
public final class MockRobinEngine extends RobinEngine implements Engine { public final class MockRobinEngine extends RobinEngine implements Engine {
public static final ConcurrentMap<AssertingSearcher, RuntimeException> INFLIGHT_ENGINE_SEARCHERS = new ConcurrentHashMap<AssertingSearcher, RuntimeException>(); public static final ConcurrentMap<AssertingSearcher, RuntimeException> INFLIGHT_ENGINE_SEARCHERS = new ConcurrentHashMap<AssertingSearcher, RuntimeException>();
private final Random random; private final Random random;
@Inject @Inject
public MockRobinEngine(ShardId shardId, @IndexSettings Settings indexSettings, ThreadPool threadPool, public MockRobinEngine(ShardId shardId, @IndexSettings Settings indexSettings, ThreadPool threadPool,
IndexSettingsService indexSettingsService, ShardIndexingService indexingService, @Nullable IndicesWarmer warmer, Store store, IndexSettingsService indexSettingsService, ShardIndexingService indexingService, @Nullable IndicesWarmer warmer, Store store,
SnapshotDeletionPolicy deletionPolicy, Translog translog, MergePolicyProvider mergePolicyProvider, SnapshotDeletionPolicy deletionPolicy, Translog translog, MergePolicyProvider mergePolicyProvider,
MergeSchedulerProvider mergeScheduler, AnalysisService analysisService, SimilarityService similarityService, MergeSchedulerProvider mergeScheduler, AnalysisService analysisService, SimilarityService similarityService,
CodecService codecService) throws EngineException { CodecService codecService) throws EngineException {
super(shardId, indexSettings, threadPool, indexSettingsService, indexingService, warmer, store, super(shardId, indexSettings, threadPool, indexSettingsService, indexingService, warmer, store,
deletionPolicy, translog, mergePolicyProvider, mergeScheduler, analysisService, similarityService, codecService); deletionPolicy, translog, mergePolicyProvider, mergeScheduler, analysisService, similarityService, codecService);
final long seed = indexSettings.getAsLong(ElasticSearchTestCase.INDEX_SEED_SETTING, 0l); final long seed = indexSettings.getAsLong(ElasticSearchTestCase.INDEX_SEED_SETTING, 0l);
if (logger.isTraceEnabled()){ if (logger.isTraceEnabled()) {
logger.trace("Using [{}] for shard [{}] seed: [{}]", this.getClass().getName(), shardId, seed); logger.trace("Using [{}] for shard [{}] seed: [{}]", this.getClass().getName(), shardId, seed);
} }
random = new Random(seed); random = new Random(seed);
@ -82,30 +83,35 @@ public final class MockRobinEngine extends RobinEngine implements Engine {
} }
} }
} }
@Override @Override
protected Searcher newSearcher(IndexSearcher searcher, SearcherManager manager) throws EngineException { protected Searcher newSearcher(String source, IndexSearcher searcher, SearcherManager manager) throws EngineException {
// this executes basic query checks and asserts that weights are normalized only once etc. // this executes basic query checks and asserts that weights are normalized only once etc.
final AssertingIndexSearcher assertingIndexSearcher = new AssertingIndexSearcher(random, searcher.getTopReaderContext()); final AssertingIndexSearcher assertingIndexSearcher = new AssertingIndexSearcher(random, searcher.getTopReaderContext());
assertingIndexSearcher.setSimilarity(searcher.getSimilarity()); assertingIndexSearcher.setSimilarity(searcher.getSimilarity());
return new AssertingSearcher(super.newSearcher(assertingIndexSearcher, manager), shardId); return new AssertingSearcher(super.newSearcher(source, assertingIndexSearcher, manager), shardId);
} }
public static final class AssertingSearcher implements Searcher { public static final class AssertingSearcher implements Searcher {
private final Searcher searcher; private final Searcher searcher;
private final ShardId shardId; private final ShardId shardId;
public AssertingSearcher(Searcher searcher, ShardId shardId) { public AssertingSearcher(Searcher searcher, ShardId shardId) {
this.searcher = searcher; this.searcher = searcher;
this.shardId = shardId; this.shardId = shardId;
INFLIGHT_ENGINE_SEARCHERS.put(this, new RuntimeException("Unreleased Searcher")); INFLIGHT_ENGINE_SEARCHERS.put(this, new RuntimeException("Unreleased Searcher, source [" + searcher.source() + "]"));
}
@Override
public String source() {
return searcher.source();
} }
@Override @Override
public boolean release() throws ElasticSearchException { public boolean release() throws ElasticSearchException {
RuntimeException remove = INFLIGHT_ENGINE_SEARCHERS.remove(this); RuntimeException remove = INFLIGHT_ENGINE_SEARCHERS.remove(this);
assert remove != null : "Released Searcher more than once"; assert remove != null : "Released Searcher more than once, source [" + searcher.source() + "]";
return searcher.release(); return searcher.release();
} }
@Override @Override
@ -117,7 +123,7 @@ public final class MockRobinEngine extends RobinEngine implements Engine {
public IndexSearcher searcher() { public IndexSearcher searcher() {
return searcher.searcher(); return searcher.searcher();
} }
public ShardId shardId() { public ShardId shardId() {
return shardId; return shardId;
} }