Search in sources :

Example 1 with SequenceNumbersService

use of org.elasticsearch.index.seqno.SequenceNumbersService 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 2 with SequenceNumbersService

use of org.elasticsearch.index.seqno.SequenceNumbersService in project elasticsearch by elastic.

the class InternalEngineTests method testCommitStats.

public void testCommitStats() throws IOException {
    InternalEngine engine = null;
    try {
        this.engine.close();
        final AtomicLong maxSeqNo = new AtomicLong(SequenceNumbersService.NO_OPS_PERFORMED);
        final AtomicLong localCheckpoint = new AtomicLong(SequenceNumbersService.NO_OPS_PERFORMED);
        final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbersService.UNASSIGNED_SEQ_NO);
        engine = new InternalEngine(copy(this.engine.config(), this.engine.config().getOpenMode())) {

            @Override
            public SequenceNumbersService seqNoService() {
                return new SequenceNumbersService(this.config().getShardId(), this.config().getIndexSettings(), maxSeqNo.get(), localCheckpoint.get(), globalCheckpoint.get());
            }
        };
        CommitStats stats1 = engine.commitStats();
        assertThat(stats1.getGeneration(), greaterThan(0L));
        assertThat(stats1.getId(), notNullValue());
        assertThat(stats1.getUserData(), hasKey(Translog.TRANSLOG_GENERATION_KEY));
        assertThat(stats1.getUserData(), hasKey(SequenceNumbers.LOCAL_CHECKPOINT_KEY));
        assertThat(Long.parseLong(stats1.getUserData().get(SequenceNumbers.LOCAL_CHECKPOINT_KEY)), equalTo(SequenceNumbersService.NO_OPS_PERFORMED));
        assertThat(stats1.getUserData(), hasKey(SequenceNumbers.MAX_SEQ_NO));
        assertThat(Long.parseLong(stats1.getUserData().get(SequenceNumbers.MAX_SEQ_NO)), equalTo(SequenceNumbersService.NO_OPS_PERFORMED));
        maxSeqNo.set(rarely() ? SequenceNumbersService.NO_OPS_PERFORMED : randomIntBetween(0, 1024));
        localCheckpoint.set(rarely() || maxSeqNo.get() == SequenceNumbersService.NO_OPS_PERFORMED ? SequenceNumbersService.NO_OPS_PERFORMED : randomIntBetween(0, 1024));
        globalCheckpoint.set(rarely() || localCheckpoint.get() == SequenceNumbersService.NO_OPS_PERFORMED ? SequenceNumbersService.UNASSIGNED_SEQ_NO : randomIntBetween(0, (int) localCheckpoint.get()));
        engine.flush(true, true);
        CommitStats stats2 = engine.commitStats();
        assertThat(stats2.getGeneration(), greaterThan(stats1.getGeneration()));
        assertThat(stats2.getId(), notNullValue());
        assertThat(stats2.getId(), not(equalTo(stats1.getId())));
        assertThat(stats2.getUserData(), hasKey(Translog.TRANSLOG_GENERATION_KEY));
        assertThat(stats2.getUserData(), hasKey(Translog.TRANSLOG_UUID_KEY));
        assertThat(stats2.getUserData().get(Translog.TRANSLOG_GENERATION_KEY), not(equalTo(stats1.getUserData().get(Translog.TRANSLOG_GENERATION_KEY))));
        assertThat(stats2.getUserData().get(Translog.TRANSLOG_UUID_KEY), equalTo(stats1.getUserData().get(Translog.TRANSLOG_UUID_KEY)));
        assertThat(Long.parseLong(stats2.getUserData().get(SequenceNumbers.LOCAL_CHECKPOINT_KEY)), equalTo(localCheckpoint.get()));
        assertThat(stats2.getUserData(), hasKey(SequenceNumbers.MAX_SEQ_NO));
        assertThat(Long.parseLong(stats2.getUserData().get(SequenceNumbers.MAX_SEQ_NO)), equalTo(maxSeqNo.get()));
    } finally {
        IOUtils.close(engine);
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) SequenceNumbersService(org.elasticsearch.index.seqno.SequenceNumbersService)

Example 3 with SequenceNumbersService

use of org.elasticsearch.index.seqno.SequenceNumbersService in project elasticsearch by elastic.

the class InternalEngineTests method testNoOps.

/*
     * This test tests that a no-op does not generate a new sequence number, that no-ops can advance the local checkpoint, and that no-ops
     * are correctly added to the translog.
     */
public void testNoOps() throws IOException {
    engine.close();
    InternalEngine noOpEngine = null;
    final int maxSeqNo = randomIntBetween(0, 128);
    final int localCheckpoint = randomIntBetween(0, maxSeqNo);
    final int globalCheckpoint = randomIntBetween(0, localCheckpoint);
    try {
        final SequenceNumbersService seqNoService = new SequenceNumbersService(shardId, defaultSettings, maxSeqNo, localCheckpoint, globalCheckpoint) {

            @Override
            public long generateSeqNo() {
                throw new UnsupportedOperationException();
            }
        };
        noOpEngine = createEngine(defaultSettings, store, primaryTranslogDir, newMergePolicy(), null, () -> seqNoService);
        final long primaryTerm = randomNonNegativeLong();
        final String reason = randomAsciiOfLength(16);
        noOpEngine.noOp(new Engine.NoOp(null, maxSeqNo + 1, primaryTerm, 0, VersionType.INTERNAL, randomFrom(PRIMARY, REPLICA, PEER_RECOVERY, LOCAL_TRANSLOG_RECOVERY), System.nanoTime(), reason));
        assertThat(noOpEngine.seqNoService().getLocalCheckpoint(), equalTo((long) (maxSeqNo + 1)));
        assertThat(noOpEngine.getTranslog().totalOperations(), equalTo(1));
        final Translog.Operation op = noOpEngine.getTranslog().newSnapshot().next();
        assertThat(op, instanceOf(Translog.NoOp.class));
        final Translog.NoOp noOp = (Translog.NoOp) op;
        assertThat(noOp.seqNo(), equalTo((long) (maxSeqNo + 1)));
        assertThat(noOp.primaryTerm(), equalTo(primaryTerm));
        assertThat(noOp.reason(), equalTo(reason));
    } finally {
        IOUtils.close(noOpEngine);
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) LongPoint(org.apache.lucene.document.LongPoint) SequenceNumbersService(org.elasticsearch.index.seqno.SequenceNumbersService) Translog(org.elasticsearch.index.translog.Translog)

Aggregations

SequenceNumbersService (org.elasticsearch.index.seqno.SequenceNumbersService)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 LongPoint (org.apache.lucene.document.LongPoint)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)1 Translog (org.elasticsearch.index.translog.Translog)1