Search in sources :

Example 21 with Searcher

use of org.elasticsearch.index.engine.Engine.Searcher in project elasticsearch by elastic.

the class InternalEngineTests method testRetryWithAutogeneratedIdWorksAndNoDuplicateDocs.

public void testRetryWithAutogeneratedIdWorksAndNoDuplicateDocs() throws IOException {
    final ParsedDocument doc = testParsedDocument("1", "test", null, testDocumentWithTextField(), new BytesArray("{}".getBytes(Charset.defaultCharset())), null);
    boolean isRetry = false;
    long autoGeneratedIdTimestamp = 0;
    Engine.Index index = new Engine.Index(newUid(doc), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 0, Versions.MATCH_ANY, VersionType.INTERNAL, PRIMARY, System.nanoTime(), autoGeneratedIdTimestamp, isRetry);
    Engine.IndexResult indexResult = engine.index(index);
    assertThat(indexResult.getVersion(), equalTo(1L));
    index = new Engine.Index(newUid(doc), doc, indexResult.getSeqNo(), index.primaryTerm(), indexResult.getVersion(), index.versionType().versionTypeForReplicationAndRecovery(), REPLICA, System.nanoTime(), autoGeneratedIdTimestamp, isRetry);
    indexResult = replicaEngine.index(index);
    assertThat(indexResult.getVersion(), equalTo(1L));
    isRetry = true;
    index = new Engine.Index(newUid(doc), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 0, Versions.MATCH_ANY, VersionType.INTERNAL, PRIMARY, System.nanoTime(), autoGeneratedIdTimestamp, isRetry);
    indexResult = engine.index(index);
    assertThat(indexResult.getVersion(), equalTo(1L));
    engine.refresh("test");
    try (Engine.Searcher searcher = engine.acquireSearcher("test")) {
        TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), 10);
        assertEquals(1, topDocs.totalHits);
    }
    index = new Engine.Index(newUid(doc), doc, indexResult.getSeqNo(), index.primaryTerm(), indexResult.getVersion(), index.versionType().versionTypeForReplicationAndRecovery(), REPLICA, System.nanoTime(), autoGeneratedIdTimestamp, isRetry);
    indexResult = replicaEngine.index(index);
    assertThat(indexResult.hasFailure(), equalTo(false));
    replicaEngine.refresh("test");
    try (Engine.Searcher searcher = replicaEngine.acquireSearcher("test")) {
        TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), 10);
        assertEquals(1, topDocs.totalHits);
    }
}
Also used : Searcher(org.elasticsearch.index.engine.Engine.Searcher) TopDocs(org.apache.lucene.search.TopDocs) BytesArray(org.elasticsearch.common.bytes.BytesArray) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Index(org.elasticsearch.index.Index) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 22 with Searcher

use of org.elasticsearch.index.engine.Engine.Searcher in project elasticsearch by elastic.

the class InternalEngineTests method testRetryConcurrently.

public void testRetryConcurrently() throws InterruptedException, IOException {
    Thread[] thread = new Thread[randomIntBetween(3, 5)];
    int numDocs = randomIntBetween(1000, 10000);
    List<Engine.Index> docs = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        final ParsedDocument doc = testParsedDocument(Integer.toString(i), "test", null, testDocumentWithTextField(), new BytesArray("{}".getBytes(Charset.defaultCharset())), null);
        Engine.Index originalIndex = randomAppendOnly(doc, false, i);
        Engine.Index retryIndex = randomAppendOnly(doc, true, i);
        docs.add(originalIndex);
        docs.add(retryIndex);
    }
    Collections.shuffle(docs, random());
    CountDownLatch startGun = new CountDownLatch(thread.length);
    AtomicInteger offset = new AtomicInteger(-1);
    for (int i = 0; i < thread.length; i++) {
        thread[i] = new Thread(() -> {
            startGun.countDown();
            try {
                startGun.await();
            } catch (InterruptedException e) {
                throw new AssertionError(e);
            }
            int docOffset;
            while ((docOffset = offset.incrementAndGet()) < docs.size()) {
                try {
                    engine.index(docs.get(docOffset));
                } catch (IOException e) {
                    throw new AssertionError(e);
                }
            }
        });
        thread[i].start();
    }
    for (int i = 0; i < thread.length; i++) {
        thread[i].join();
    }
    assertEquals(0, engine.getNumVersionLookups());
    assertEquals(0, engine.getNumIndexVersionsLookups());
    engine.refresh("test");
    try (Engine.Searcher searcher = engine.acquireSearcher("test")) {
        TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), 10);
        assertEquals(numDocs, topDocs.totalHits);
    }
    assertTrue(engine.indexWriterHasDeletions());
}
Also used : Searcher(org.elasticsearch.index.engine.Engine.Searcher) BytesArray(org.elasticsearch.common.bytes.BytesArray) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) LongPoint(org.apache.lucene.document.LongPoint) TopDocs(org.apache.lucene.search.TopDocs) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

Searcher (org.elasticsearch.index.engine.Engine.Searcher)22 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)11 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)11 TopDocs (org.apache.lucene.search.TopDocs)10 BytesArray (org.elasticsearch.common.bytes.BytesArray)10 Index (org.elasticsearch.index.Index)10 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)9 IndexService (org.elasticsearch.index.IndexService)9 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)9 LongPoint (org.apache.lucene.document.LongPoint)8 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)6 IOException (java.io.IOException)4 UncheckedIOException (java.io.UncheckedIOException)4 ArrayList (java.util.ArrayList)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 BytesRef (org.apache.lucene.util.BytesRef)3 SortedBinaryDocValues (org.elasticsearch.index.fielddata.SortedBinaryDocValues)3 Path (java.nio.file.Path)2 CountDownLatch (java.util.concurrent.CountDownLatch)2