Search in sources :

Example 46 with MapperService

use of org.elasticsearch.index.mapper.MapperService in project crate by crate.

the class InternalEngineTests method testLuceneSnapshotRefreshesOnlyOnce.

@Test
public void testLuceneSnapshotRefreshesOnlyOnce() throws Exception {
    final MapperService mapperService = createMapperService("test");
    final long maxSeqNo = randomLongBetween(10, 50);
    final AtomicLong refreshCounter = new AtomicLong();
    final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(IndexMetadata.builder(defaultSettings.getIndexMetadata()).settings(Settings.builder().put(defaultSettings.getSettings()).put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true)).build());
    try (Store store = createStore();
        InternalEngine engine = createEngine(config(indexSettings, store, createTempDir(), newMergePolicy(), null, new ReferenceManager.RefreshListener() {

            @Override
            public void beforeRefresh() {
                refreshCounter.incrementAndGet();
            }

            @Override
            public void afterRefresh(boolean didRefresh) {
            }
        }, () -> SequenceNumbers.NO_OPS_PERFORMED))) {
        for (long seqNo = 0; seqNo <= maxSeqNo; seqNo++) {
            final ParsedDocument doc = testParsedDocument("id_" + seqNo, null, testDocumentWithTextField("test"), new BytesArray("{}".getBytes(Charset.defaultCharset())), null);
            engine.index(replicaIndexForDoc(doc, 1, seqNo, randomBoolean()));
        }
        final long initialRefreshCount = refreshCounter.get();
        final Thread[] snapshotThreads = new Thread[between(1, 3)];
        CountDownLatch latch = new CountDownLatch(1);
        for (int i = 0; i < snapshotThreads.length; i++) {
            final long min = randomLongBetween(0, maxSeqNo - 5);
            final long max = randomLongBetween(min, maxSeqNo);
            snapshotThreads[i] = new Thread(new AbstractRunnable() {

                @Override
                public void onFailure(Exception e) {
                    throw new AssertionError(e);
                }

                @Override
                protected void doRun() throws Exception {
                    latch.await();
                    Translog.Snapshot changes = engine.newChangesSnapshot("test", mapperService, min, max, true);
                    changes.close();
                }
            });
            snapshotThreads[i].start();
        }
        latch.countDown();
        for (Thread thread : snapshotThreads) {
            thread.join();
        }
        assertThat(refreshCounter.get(), equalTo(initialRefreshCount + 1L));
        assertThat(engine.lastRefreshedCheckpoint(), equalTo(maxSeqNo));
    }
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) BytesArray(org.elasticsearch.common.bytes.BytesArray) IndexSettings(org.elasticsearch.index.IndexSettings) Store(org.elasticsearch.index.store.Store) CountDownLatch(java.util.concurrent.CountDownLatch) LongPoint(org.apache.lucene.document.LongPoint) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ElasticsearchException(org.elasticsearch.ElasticsearchException) TestTranslog(org.elasticsearch.index.translog.TestTranslog) Translog(org.elasticsearch.index.translog.Translog) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) MapperService(org.elasticsearch.index.mapper.MapperService) Test(org.junit.Test)

Aggregations

MapperService (org.elasticsearch.index.mapper.MapperService)46 Settings (org.elasticsearch.common.settings.Settings)16 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)14 IndexSettings (org.elasticsearch.index.IndexSettings)13 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)12 IOException (java.io.IOException)10 Store (org.elasticsearch.index.store.Store)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)8 Index (org.elasticsearch.index.Index)8 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 IndexService (org.elasticsearch.index.IndexService)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 IndexAnalyzers (org.elasticsearch.index.analysis.IndexAnalyzers)6 Collections (java.util.Collections)5 HashSet (java.util.HashSet)5 List (java.util.List)5 Analyzer (org.apache.lucene.analysis.Analyzer)5