Search in sources :

Example 26 with ParsedDocument

use of org.elasticsearch.index.mapper.ParsedDocument in project elasticsearch by elastic.

the class InternalEngineTests method testBasicCreatedFlag.

public void testBasicCreatedFlag() throws IOException {
    ParsedDocument doc = testParsedDocument("1", "test", null, testDocument(), B_1, null);
    Engine.Index index = indexForDoc(doc);
    Engine.IndexResult indexResult = engine.index(index);
    assertTrue(indexResult.isCreated());
    index = indexForDoc(doc);
    indexResult = engine.index(index);
    assertFalse(indexResult.isCreated());
    engine.delete(new Engine.Delete(null, "1", newUid(doc)));
    index = indexForDoc(doc);
    indexResult = engine.index(index);
    assertTrue(indexResult.isCreated());
}
Also used : ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument)

Example 27 with ParsedDocument

use of org.elasticsearch.index.mapper.ParsedDocument in project elasticsearch by elastic.

the class InternalEngineTests method testVersioningCreateExistsExceptionWithFlush.

public void testVersioningCreateExistsExceptionWithFlush() throws IOException {
    ParsedDocument doc = testParsedDocument("1", "test", null, testDocument(), B_1, null);
    Engine.Index create = new Engine.Index(newUid(doc), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 0, Versions.MATCH_DELETED, VersionType.INTERNAL, PRIMARY, 0, -1, false);
    Engine.IndexResult indexResult = engine.index(create);
    assertThat(indexResult.getVersion(), equalTo(1L));
    engine.flush();
    create = new Engine.Index(newUid(doc), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 0, Versions.MATCH_DELETED, VersionType.INTERNAL, PRIMARY, 0, -1, false);
    indexResult = engine.index(create);
    assertTrue(indexResult.hasFailure());
    assertThat(indexResult.getFailure(), instanceOf(VersionConflictEngineException.class));
}
Also used : ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Index(org.elasticsearch.index.Index)

Example 28 with ParsedDocument

use of org.elasticsearch.index.mapper.ParsedDocument in project elasticsearch by elastic.

the class InternalEngineTests method testSequenceNumberAdvancesToMaxSeqOnEngineOpenOnPrimary.

public void testSequenceNumberAdvancesToMaxSeqOnEngineOpenOnPrimary() throws BrokenBarrierException, InterruptedException, IOException {
    engine.close();
    final int docs = randomIntBetween(1, 32);
    InternalEngine initialEngine = null;
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        final CyclicBarrier barrier = new CyclicBarrier(2);
        final AtomicBoolean skip = new AtomicBoolean();
        final AtomicLong expectedLocalCheckpoint = new AtomicLong(SequenceNumbersService.NO_OPS_PERFORMED);
        final List<Thread> threads = new ArrayList<>();
        final SequenceNumbersService seqNoService = new SequenceNumbersService(shardId, defaultSettings, SequenceNumbersService.NO_OPS_PERFORMED, SequenceNumbersService.NO_OPS_PERFORMED, SequenceNumbersService.UNASSIGNED_SEQ_NO) {

            @Override
            public long generateSeqNo() {
                final long seqNo = super.generateSeqNo();
                if (skip.get()) {
                    try {
                        barrier.await();
                        latch.await();
                    } catch (BrokenBarrierException | InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    if (expectedLocalCheckpoint.get() + 1 == seqNo) {
                        expectedLocalCheckpoint.set(seqNo);
                    }
                }
                return seqNo;
            }
        };
        initialEngine = createEngine(defaultSettings, store, primaryTranslogDir, newMergePolicy(), null, () -> seqNoService);
        final InternalEngine finalInitialEngine = initialEngine;
        for (int i = 0; i < docs; i++) {
            final String id = Integer.toString(i);
            final ParsedDocument doc = testParsedDocument(id, "test", null, testDocumentWithTextField(), SOURCE, null);
            skip.set(randomBoolean());
            final Thread thread = new Thread(() -> {
                try {
                    finalInitialEngine.index(indexForDoc(doc));
                } catch (IOException e) {
                    throw new AssertionError(e);
                }
            });
            thread.start();
            if (skip.get()) {
                threads.add(thread);
                barrier.await();
            } else {
                thread.join();
            }
        }
        assertThat(initialEngine.seqNoService().getLocalCheckpoint(), equalTo(expectedLocalCheckpoint.get()));
        assertThat(initialEngine.seqNoService().getMaxSeqNo(), equalTo((long) (docs - 1)));
        initialEngine.flush(true, true);
        latch.countDown();
        for (final Thread thread : threads) {
            thread.join();
        }
    } finally {
        IOUtils.close(initialEngine);
    }
    try (Engine recoveringEngine = new InternalEngine(copy(initialEngine.config(), EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG))) {
        assertThat(recoveringEngine.seqNoService().getLocalCheckpoint(), greaterThanOrEqualTo((long) (docs - 1)));
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) LongPoint(org.apache.lucene.document.LongPoint) CyclicBarrier(java.util.concurrent.CyclicBarrier) SequenceNumbersService(org.elasticsearch.index.seqno.SequenceNumbersService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument)

Example 29 with ParsedDocument

use of org.elasticsearch.index.mapper.ParsedDocument in project elasticsearch by elastic.

the class InternalEngineTests method testSegmentsStatsIncludingFileSizes.

public void testSegmentsStatsIncludingFileSizes() throws Exception {
    try (Store store = createStore();
        Engine engine = createEngine(defaultSettings, store, createTempDir(), NoMergePolicy.INSTANCE)) {
        assertThat(engine.segmentsStats(true).getFileSizes().size(), equalTo(0));
        ParsedDocument doc = testParsedDocument("1", "test", null, testDocumentWithTextField(), B_1, null);
        engine.index(indexForDoc(doc));
        engine.refresh("test");
        SegmentsStats stats = engine.segmentsStats(true);
        assertThat(stats.getFileSizes().size(), greaterThan(0));
        assertThat(() -> stats.getFileSizes().valuesIt(), everyItem(greaterThan(0L)));
        ObjectObjectCursor<String, Long> firstEntry = stats.getFileSizes().iterator().next();
        ParsedDocument doc2 = testParsedDocument("2", "test", null, testDocumentWithTextField(), B_2, null);
        engine.index(indexForDoc(doc2));
        engine.refresh("test");
        assertThat(engine.segmentsStats(true).getFileSizes().get(firstEntry.key), greaterThan(firstEntry.value));
    }
}
Also used : ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) AtomicLong(java.util.concurrent.atomic.AtomicLong) Store(org.elasticsearch.index.store.Store) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 30 with ParsedDocument

use of org.elasticsearch.index.mapper.ParsedDocument in project elasticsearch by elastic.

the class InternalEngineTests method testSequenceIDs.

public void testSequenceIDs() throws Exception {
    Tuple<Long, Long> seqID = getSequenceID(engine, new Engine.Get(false, newUid("1")));
    // Non-existent doc returns no seqnum and no primary term
    assertThat(seqID.v1(), equalTo(SequenceNumbersService.UNASSIGNED_SEQ_NO));
    assertThat(seqID.v2(), equalTo(0L));
    // create a document
    Document document = testDocumentWithTextField();
    document.add(new Field(SourceFieldMapper.NAME, BytesReference.toBytes(B_1), SourceFieldMapper.Defaults.FIELD_TYPE));
    ParsedDocument doc = testParsedDocument("1", "test", null, document, B_1, null);
    engine.index(indexForDoc(doc));
    engine.refresh("test");
    seqID = getSequenceID(engine, new Engine.Get(false, newUid(doc)));
    logger.info("--> got seqID: {}", seqID);
    assertThat(seqID.v1(), equalTo(0L));
    assertThat(seqID.v2(), equalTo(0L));
    // Index the same document again
    document = testDocumentWithTextField();
    document.add(new Field(SourceFieldMapper.NAME, BytesReference.toBytes(B_1), SourceFieldMapper.Defaults.FIELD_TYPE));
    doc = testParsedDocument("1", "test", null, document, B_1, null);
    engine.index(indexForDoc(doc));
    engine.refresh("test");
    seqID = getSequenceID(engine, new Engine.Get(false, newUid(doc)));
    logger.info("--> got seqID: {}", seqID);
    assertThat(seqID.v1(), equalTo(1L));
    assertThat(seqID.v2(), equalTo(0L));
    // Index the same document for the third time, this time changing the primary term
    document = testDocumentWithTextField();
    document.add(new Field(SourceFieldMapper.NAME, BytesReference.toBytes(B_1), SourceFieldMapper.Defaults.FIELD_TYPE));
    doc = testParsedDocument("1", "test", null, document, B_1, null);
    engine.index(new Engine.Index(newUid(doc), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 1, Versions.MATCH_ANY, VersionType.INTERNAL, Engine.Operation.Origin.PRIMARY, System.nanoTime(), -1, false));
    engine.refresh("test");
    seqID = getSequenceID(engine, new Engine.Get(false, newUid(doc)));
    logger.info("--> got seqID: {}", seqID);
    assertThat(seqID.v1(), equalTo(2L));
    assertThat(seqID.v2(), equalTo(1L));
    // we can query by the _seq_no
    Engine.Searcher searchResult = engine.acquireSearcher("test");
    MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(1));
    MatcherAssert.assertThat(searchResult, EngineSearcherTotalHitsMatcher.engineSearcherTotalHits(LongPoint.newExactQuery("_seq_no", 2), 1));
    searchResult.close();
}
Also used : Searcher(org.elasticsearch.index.engine.Engine.Searcher) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) TextField(org.apache.lucene.document.TextField) IndexableField(org.apache.lucene.index.IndexableField) StoredField(org.apache.lucene.document.StoredField) Field(org.apache.lucene.document.Field) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Document(org.elasticsearch.index.mapper.ParseContext.Document)

Aggregations

ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)211 Test (org.junit.Test)85 LongPoint (org.apache.lucene.document.LongPoint)59 BytesArray (org.elasticsearch.common.bytes.BytesArray)58 Matchers.containsString (org.hamcrest.Matchers.containsString)57 Store (org.elasticsearch.index.store.Store)52 Searcher (org.elasticsearch.index.engine.Engine.Searcher)46 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)35 IOException (java.io.IOException)32 AtomicLong (java.util.concurrent.atomic.AtomicLong)31 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)31 IndexableField (org.apache.lucene.index.IndexableField)30 Term (org.apache.lucene.index.Term)28 TopDocs (org.apache.lucene.search.TopDocs)28 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)27 Index (org.elasticsearch.index.Index)27 UncheckedIOException (java.io.UncheckedIOException)26 Field (org.apache.lucene.document.Field)26 TextField (org.apache.lucene.document.TextField)26 ArrayList (java.util.ArrayList)25