Search in sources :

Example 11 with NoopCircuitBreaker

use of org.elasticsearch.common.breaker.NoopCircuitBreaker 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)

Example 12 with NoopCircuitBreaker

use of org.elasticsearch.common.breaker.NoopCircuitBreaker in project crate by crate.

the class HierarchyCircuitBreakerService method registerBreaker.

/**
 * Allows to register a custom circuit breaker.
 * Warning: Will overwrite any existing custom breaker with the same name.
 */
@Override
public void registerBreaker(BreakerSettings breakerSettings) {
    if (breakerSettings.getType() == CircuitBreaker.Type.NOOP) {
        CircuitBreaker breaker = new NoopCircuitBreaker(breakerSettings.getName());
        breakers.put(breakerSettings.getName(), breaker);
    } else {
        CircuitBreaker oldBreaker;
        CircuitBreaker breaker = new ChildMemoryCircuitBreaker(breakerSettings, LogManager.getLogger(CHILD_LOGGER_PREFIX + breakerSettings.getName()), this);
        for (; ; ) {
            oldBreaker = breakers.putIfAbsent(breakerSettings.getName(), breaker);
            if (oldBreaker == null) {
                return;
            }
            breaker = new ChildMemoryCircuitBreaker(breakerSettings, (ChildMemoryCircuitBreaker) oldBreaker, LogManager.getLogger(CHILD_LOGGER_PREFIX + breakerSettings.getName()), this);
            if (breakers.replace(breakerSettings.getName(), oldBreaker, breaker)) {
                return;
            }
        }
    }
}
Also used : ChildMemoryCircuitBreaker(org.elasticsearch.common.breaker.ChildMemoryCircuitBreaker) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) ChildMemoryCircuitBreaker(org.elasticsearch.common.breaker.ChildMemoryCircuitBreaker)

Example 13 with NoopCircuitBreaker

use of org.elasticsearch.common.breaker.NoopCircuitBreaker in project crate by crate.

the class IndexWriterProjectorUnitTest method testNullPKValue.

@Test
public void testNullPKValue() throws Throwable {
    InputCollectExpression sourceInput = new InputCollectExpression(0);
    List<CollectExpression<Row, ?>> collectExpressions = Collections.<CollectExpression<Row, ?>>singletonList(sourceInput);
    TransportCreatePartitionsAction transportCreatePartitionsAction = mock(TransportCreatePartitionsAction.class);
    IndexWriterProjector indexWriter = new IndexWriterProjector(clusterService, new NodeLimits(new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)), new NoopCircuitBreaker("dummy"), RamAccounting.NO_ACCOUNTING, scheduler, executor, CoordinatorTxnCtx.systemTransactionContext(), createNodeContext(), Settings.EMPTY, 5, 1, transportCreatePartitionsAction, (request, listener) -> {
    }, IndexNameResolver.forTable(BULK_IMPORT_IDENT), RAW_SOURCE_REFERENCE, Collections.singletonList(ID_IDENT), Collections.<Symbol>singletonList(new InputColumn(1)), null, null, sourceInput, collectExpressions, 20, null, null, false, false, UUID.randomUUID(), UpsertResultContext.forRowCount(), false);
    RowN rowN = new RowN(new Object[] { new BytesRef("{\"y\": \"x\"}"), null });
    BatchIterator<Row> batchIterator = InMemoryBatchIterator.of(Collections.singletonList(rowN), SENTINEL, true);
    batchIterator = indexWriter.apply(batchIterator);
    TestingRowConsumer testingBatchConsumer = new TestingRowConsumer();
    testingBatchConsumer.accept(batchIterator, null);
    List<Object[]> result = testingBatchConsumer.getResult();
    // Zero affected rows as a NULL as a PK value will result in an exception.
    // It must never bubble up as other rows might already have been written.
    assertThat(result.get(0)[0], is(0L));
}
Also used : TransportCreatePartitionsAction(org.elasticsearch.action.admin.indices.create.TransportCreatePartitionsAction) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) CollectExpression(io.crate.execution.engine.collect.CollectExpression) InputCollectExpression(io.crate.execution.engine.collect.InputCollectExpression) RowN(io.crate.data.RowN) InputCollectExpression(io.crate.execution.engine.collect.InputCollectExpression) InputColumn(io.crate.expression.symbol.InputColumn) NodeLimits(io.crate.execution.jobs.NodeLimits) Row(io.crate.data.Row) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) BytesRef(org.apache.lucene.util.BytesRef) TestingRowConsumer(io.crate.testing.TestingRowConsumer) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 14 with NoopCircuitBreaker

use of org.elasticsearch.common.breaker.NoopCircuitBreaker in project crate by crate.

the class IndexWriterProjectorTest method testIndexWriter.

@Test
public void testIndexWriter() throws Throwable {
    execute("create table bulk_import (id int primary key, name string) with (number_of_replicas=0)");
    ensureGreen();
    InputCollectExpression sourceInput = new InputCollectExpression(1);
    List<CollectExpression<Row, ?>> collectExpressions = Collections.<CollectExpression<Row, ?>>singletonList(sourceInput);
    RelationName bulkImportIdent = new RelationName(sqlExecutor.getCurrentSchema(), "bulk_import");
    ClusterState state = clusterService().state();
    Settings tableSettings = TableSettingsResolver.get(state.getMetadata(), bulkImportIdent, false);
    ThreadPool threadPool = internalCluster().getInstance(ThreadPool.class);
    IndexWriterProjector writerProjector = new IndexWriterProjector(clusterService(), new NodeLimits(new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)), new NoopCircuitBreaker("dummy"), RamAccounting.NO_ACCOUNTING, threadPool.scheduler(), threadPool.executor(ThreadPool.Names.SEARCH), CoordinatorTxnCtx.systemTransactionContext(), new NodeContext(internalCluster().getInstance(Functions.class)), Settings.EMPTY, IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.get(tableSettings), NumberOfReplicas.fromSettings(tableSettings, state.getNodes().getSize()), internalCluster().getInstance(TransportCreatePartitionsAction.class), internalCluster().getInstance(TransportShardUpsertAction.class)::execute, IndexNameResolver.forTable(bulkImportIdent), new Reference(new ReferenceIdent(bulkImportIdent, DocSysColumns.RAW), RowGranularity.DOC, DataTypes.STRING, 0, null), Collections.singletonList(ID_IDENT), Collections.<Symbol>singletonList(new InputColumn(0)), null, null, sourceInput, collectExpressions, 20, null, null, false, false, UUID.randomUUID(), UpsertResultContext.forRowCount(), false);
    BatchIterator rowsIterator = InMemoryBatchIterator.of(IntStream.range(0, 100).mapToObj(i -> new RowN(new Object[] { i, "{\"id\": " + i + ", \"name\": \"Arthur\"}" })).collect(Collectors.toList()), SENTINEL, true);
    TestingRowConsumer consumer = new TestingRowConsumer();
    consumer.accept(writerProjector.apply(rowsIterator), null);
    Bucket objects = consumer.getBucket();
    assertThat(objects, contains(isRow(100L)));
    execute("refresh table bulk_import");
    execute("select count(*) from bulk_import");
    assertThat(response.rowCount(), is(1L));
    assertThat(response.rows()[0][0], is(100L));
}
Also used : TransportCreatePartitionsAction(org.elasticsearch.action.admin.indices.create.TransportCreatePartitionsAction) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) NodeContext(io.crate.metadata.NodeContext) Reference(io.crate.metadata.Reference) ThreadPool(org.elasticsearch.threadpool.ThreadPool) BatchIterator(io.crate.data.BatchIterator) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) CollectExpression(io.crate.execution.engine.collect.CollectExpression) InputCollectExpression(io.crate.execution.engine.collect.InputCollectExpression) ReferenceIdent(io.crate.metadata.ReferenceIdent) RowN(io.crate.data.RowN) InputCollectExpression(io.crate.execution.engine.collect.InputCollectExpression) Bucket(io.crate.data.Bucket) InputColumn(io.crate.expression.symbol.InputColumn) NodeLimits(io.crate.execution.jobs.NodeLimits) RelationName(io.crate.metadata.RelationName) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) Settings(org.elasticsearch.common.settings.Settings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) TestingRowConsumer(io.crate.testing.TestingRowConsumer) Test(org.junit.Test)

Aggregations

NoopCircuitBreaker (org.elasticsearch.common.breaker.NoopCircuitBreaker)14 Test (org.junit.Test)10 CircuitBreaker (org.elasticsearch.common.breaker.CircuitBreaker)5 User (io.crate.user.User)4 SQLOperations (io.crate.action.sql.SQLOperations)3 Tuple (io.crate.common.collections.Tuple)3 Streams (io.crate.common.io.Streams)3 TimeValue (io.crate.common.unit.TimeValue)3 TestingRowConsumer (io.crate.testing.TestingRowConsumer)3 IOException (java.io.IOException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 LongSupplier (java.util.function.LongSupplier)3 Predicate (java.util.function.Predicate)3 Supplier (java.util.function.Supplier)3 Version (org.elasticsearch.Version)3 BytesArray (org.elasticsearch.common.bytes.BytesArray)3 BytesReference (org.elasticsearch.common.bytes.BytesReference)3 ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)3 Row (io.crate.data.Row)2 RowN (io.crate.data.RowN)2