Search in sources :

Example 56 with LongSupplier

use of java.util.function.LongSupplier in project crate by crate.

the class InternalEngineTests method testTranslogCleanUpPostCommitCrash.

@Test
public void testTranslogCleanUpPostCommitCrash() throws Exception {
    IndexSettings indexSettings = new IndexSettings(defaultSettings.getIndexMetadata(), defaultSettings.getNodeSettings(), defaultSettings.getScopedSettings());
    IndexMetadata.Builder builder = IndexMetadata.builder(indexSettings.getIndexMetadata());
    builder.settings(Settings.builder().put(indexSettings.getSettings()).put(IndexSettings.INDEX_TRANSLOG_RETENTION_AGE_SETTING.getKey(), "-1").put(IndexSettings.INDEX_TRANSLOG_RETENTION_SIZE_SETTING.getKey(), "-1"));
    indexSettings.updateIndexMetadata(builder.build());
    try (Store store = createStore()) {
        AtomicBoolean throwErrorOnCommit = new AtomicBoolean();
        final Path translogPath = createTempDir();
        final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
        final LongSupplier globalCheckpointSupplier = globalCheckpoint::get;
        store.createEmpty(Version.CURRENT.luceneVersion);
        final String translogUUID = Translog.createEmptyTranslog(translogPath, globalCheckpoint.get(), shardId, primaryTerm.get());
        store.associateIndexWithNewTranslog(translogUUID);
        try (InternalEngine engine = new InternalEngine(config(indexSettings, store, translogPath, newMergePolicy(), null, null, globalCheckpointSupplier)) {

            @Override
            protected void commitIndexWriter(IndexWriter writer, Translog translog, String syncId) throws IOException {
                super.commitIndexWriter(writer, translog, syncId);
                if (throwErrorOnCommit.get()) {
                    throw new RuntimeException("power's out");
                }
            }
        }) {
            engine.recoverFromTranslog(translogHandler, Long.MAX_VALUE);
            final ParsedDocument doc1 = testParsedDocument("1", null, testDocumentWithTextField(), SOURCE, null);
            engine.index(indexForDoc(doc1));
            // to advance local checkpoint
            engine.syncTranslog();
            assertEquals(engine.getProcessedLocalCheckpoint(), engine.getPersistedLocalCheckpoint());
            globalCheckpoint.set(engine.getPersistedLocalCheckpoint());
            throwErrorOnCommit.set(true);
            FlushFailedEngineException e = expectThrows(FlushFailedEngineException.class, engine::flush);
            assertThat(e.getCause().getMessage(), equalTo("power's out"));
        }
        try (InternalEngine engine = new InternalEngine(config(indexSettings, store, translogPath, newMergePolicy(), null, null, globalCheckpointSupplier))) {
            engine.recoverFromTranslog(translogHandler, Long.MAX_VALUE);
            assertVisibleCount(engine, 1);
            final long localCheckpoint = Long.parseLong(engine.getLastCommittedSegmentInfos().userData.get(SequenceNumbers.LOCAL_CHECKPOINT_KEY));
            final long committedGen = engine.getTranslog().getMinGenerationForSeqNo(localCheckpoint + 1).translogFileGeneration;
            for (int gen = 1; gen < committedGen; gen++) {
                final Path genFile = translogPath.resolve(Translog.getFilename(gen));
                assertFalse(genFile + " wasn't cleaned up", Files.exists(genFile));
            }
        }
    }
}
Also used : Path(java.nio.file.Path) IndexSettings(org.elasticsearch.index.IndexSettings) Store(org.elasticsearch.index.store.Store) Matchers.containsString(org.hamcrest.Matchers.containsString) LongPoint(org.apache.lucene.document.LongPoint) TestTranslog(org.elasticsearch.index.translog.TestTranslog) Translog(org.elasticsearch.index.translog.Translog) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexWriter(org.apache.lucene.index.IndexWriter) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) LongSupplier(java.util.function.LongSupplier) Test(org.junit.Test)

Example 57 with LongSupplier

use of java.util.function.LongSupplier in project crate by crate.

the class InternalEngineTests method testCommitAdvancesMinTranslogForRecovery.

@Test
public void testCommitAdvancesMinTranslogForRecovery() throws IOException {
    IOUtils.close(engine, store);
    final Path translogPath = createTempDir();
    store = createStore();
    final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
    final LongSupplier globalCheckpointSupplier = () -> globalCheckpoint.get();
    engine = createEngine(config(defaultSettings, store, translogPath, newMergePolicy(), null, null, globalCheckpointSupplier));
    engine.onSettingsChanged(TimeValue.MINUS_ONE, ByteSizeValue.ZERO, randomNonNegativeLong());
    ParsedDocument doc = testParsedDocument("1", null, testDocumentWithTextField(), B_1, null);
    engine.index(indexForDoc(doc));
    boolean inSync = randomBoolean();
    if (inSync) {
        // to advance persisted local checkpoint
        engine.syncTranslog();
        globalCheckpoint.set(engine.getPersistedLocalCheckpoint());
    }
    engine.flush();
    assertThat(engine.getTranslog().currentFileGeneration(), equalTo(3L));
    assertThat(engine.getTranslog().getMinFileGeneration(), equalTo(inSync ? 3L : 2L));
    engine.flush();
    assertThat(engine.getTranslog().currentFileGeneration(), equalTo(3L));
    assertThat(engine.getTranslog().getMinFileGeneration(), equalTo(inSync ? 3L : 2L));
    engine.flush(true, true);
    assertThat(engine.getTranslog().currentFileGeneration(), equalTo(3L));
    assertThat(engine.getTranslog().getMinFileGeneration(), equalTo(inSync ? 3L : 2L));
    globalCheckpoint.set(engine.getPersistedLocalCheckpoint());
    engine.flush(true, true);
    assertThat(engine.getTranslog().currentFileGeneration(), equalTo(3L));
    assertThat(engine.getTranslog().getMinFileGeneration(), equalTo(3L));
}
Also used : Path(java.nio.file.Path) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) LongSupplier(java.util.function.LongSupplier) Test(org.junit.Test)

Example 58 with LongSupplier

use of java.util.function.LongSupplier in project crate by crate.

the class OutboundHandlerTests method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    channel = new FakeTcpChannel(randomBoolean(), buildNewFakeTransportAddress().address(), buildNewFakeTransportAddress().address());
    TransportAddress transportAddress = buildNewFakeTransportAddress();
    node = new DiscoveryNode("", transportAddress, Version.CURRENT);
    StatsTracker statsTracker = new StatsTracker();
    handler = new OutboundHandler("node", Version.CURRENT, statsTracker, threadPool, BigArrays.NON_RECYCLING_INSTANCE);
    final LongSupplier millisSupplier = () -> TimeValue.nsecToMSec(System.nanoTime());
    final InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
    final Supplier<CircuitBreaker> breaker = () -> new NoopCircuitBreaker("test");
    final InboundAggregator aggregator = new InboundAggregator(breaker, (Predicate<String>) action -> true);
    pipeline = new InboundPipeline(statsTracker, millisSupplier, decoder, aggregator, (c, m) -> {
        try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
            Streams.copy(m.openOrGetStreamInput(), streamOutput);
            message.set(new Tuple<>(m.getHeader(), streamOutput.bytes()));
        } catch (IOException e) {
            throw new AssertionError(e);
        }
    });
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) Tuple(io.crate.common.collections.Tuple) LongSupplier(java.util.function.LongSupplier) BigArrays(org.elasticsearch.common.util.BigArrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) BytesArray(org.elasticsearch.common.bytes.BytesArray) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) After(org.junit.After) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) ThreadPool(org.elasticsearch.threadpool.ThreadPool) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) ESTestCase(org.elasticsearch.test.ESTestCase) Streams(io.crate.common.io.Streams) Before(org.junit.Before) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Predicate(java.util.function.Predicate) PageCacheRecycler(org.elasticsearch.common.util.PageCacheRecycler) Test(org.junit.Test) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Version(org.elasticsearch.Version) TransportAddress(org.elasticsearch.common.transport.TransportAddress) TimeValue(io.crate.common.unit.TimeValue) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) ActionListener(org.elasticsearch.action.ActionListener) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) TransportAddress(org.elasticsearch.common.transport.TransportAddress) IOException(java.io.IOException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) LongSupplier(java.util.function.LongSupplier) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) Tuple(io.crate.common.collections.Tuple) Before(org.junit.Before)

Example 59 with LongSupplier

use of java.util.function.LongSupplier in project crate by crate.

the class InboundPipelineTests method testEnsureBodyIsNotPrematurelyReleased.

public void testEnsureBodyIsNotPrematurelyReleased() throws IOException {
    BiConsumer<TcpChannel, InboundMessage> messageHandler = (c, m) -> {
    };
    final StatsTracker statsTracker = new StatsTracker();
    final LongSupplier millisSupplier = () -> TimeValue.nsecToMSec(System.nanoTime());
    final InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
    final Supplier<CircuitBreaker> breaker = () -> new NoopCircuitBreaker("test");
    final InboundAggregator aggregator = new InboundAggregator(breaker, (Predicate<String>) action -> true);
    final InboundPipeline pipeline = new InboundPipeline(statsTracker, millisSupplier, decoder, aggregator, messageHandler);
    try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
        String actionName = "actionName";
        final Version version = Version.CURRENT;
        final String value = randomAlphaOfLength(1000);
        final boolean isRequest = randomBoolean();
        final long requestId = randomNonNegativeLong();
        OutboundMessage message;
        if (isRequest) {
            message = new OutboundMessage.Request(new TestRequest(value), version, actionName, requestId, false, false);
        } else {
            message = new OutboundMessage.Response(new TestResponse(value), version, requestId, false, false);
        }
        final BytesReference reference = message.serialize(streamOutput);
        final int fixedHeaderSize = TcpHeader.headerSize(Version.CURRENT);
        final int variableHeaderSize = reference.getInt(fixedHeaderSize - 4);
        final int totalHeaderSize = fixedHeaderSize + variableHeaderSize;
        final AtomicBoolean bodyReleased = new AtomicBoolean(false);
        for (int i = 0; i < totalHeaderSize - 1; ++i) {
            try (ReleasableBytesReference slice = ReleasableBytesReference.wrap(reference.slice(i, 1))) {
                pipeline.handleBytes(new FakeTcpChannel(), slice);
            }
        }
        final Releasable releasable = () -> bodyReleased.set(true);
        final int from = totalHeaderSize - 1;
        final BytesReference partHeaderPartBody = reference.slice(from, reference.length() - from - 1);
        try (ReleasableBytesReference slice = new ReleasableBytesReference(partHeaderPartBody, releasable)) {
            pipeline.handleBytes(new FakeTcpChannel(), slice);
        }
        assertFalse(bodyReleased.get());
        try (ReleasableBytesReference slice = new ReleasableBytesReference(reference.slice(reference.length() - 1, 1), releasable)) {
            pipeline.handleBytes(new FakeTcpChannel(), slice);
        }
        assertTrue(bodyReleased.get());
    }
}
Also used : Tuple(io.crate.common.collections.Tuple) LongSupplier(java.util.function.LongSupplier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) BytesArray(org.elasticsearch.common.bytes.BytesArray) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) BiConsumer(java.util.function.BiConsumer) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) ESTestCase(org.elasticsearch.test.ESTestCase) Streams(io.crate.common.io.Streams) Releasable(org.elasticsearch.common.lease.Releasable) Predicate(java.util.function.Predicate) PageCacheRecycler(org.elasticsearch.common.util.PageCacheRecycler) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) TestCircuitBreaker(org.elasticsearch.common.breaker.TestCircuitBreaker) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Version(org.elasticsearch.Version) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) TimeValue(io.crate.common.unit.TimeValue) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) TestCircuitBreaker(org.elasticsearch.common.breaker.TestCircuitBreaker) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Version(org.elasticsearch.Version) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) BytesReference(org.elasticsearch.common.bytes.BytesReference) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Releasable(org.elasticsearch.common.lease.Releasable) LongSupplier(java.util.function.LongSupplier)

Aggregations

LongSupplier (java.util.function.LongSupplier)59 Random (java.util.Random)21 Test (org.junit.Test)9 IOException (java.io.IOException)7 Path (java.nio.file.Path)7 ArrayList (java.util.ArrayList)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 CircuitBreaker (org.elasticsearch.common.breaker.CircuitBreaker)5 BytesArray (org.elasticsearch.common.bytes.BytesArray)5 Tuple (io.crate.common.collections.Tuple)4 Streams (io.crate.common.io.Streams)4 TimeValue (io.crate.common.unit.TimeValue)4 List (java.util.List)4 Predicate (java.util.function.Predicate)4 Supplier (java.util.function.Supplier)4 StoredField (org.apache.lucene.document.StoredField)4 Version (org.elasticsearch.Version)4 NoopCircuitBreaker (org.elasticsearch.common.breaker.NoopCircuitBreaker)4 BytesReference (org.elasticsearch.common.bytes.BytesReference)4