Search in sources :

Example 6 with ReleasableBytesReference

use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.

the class InboundPipelineTests method testDecodeExceptionIsPropagated.

public void testDecodeExceptionIsPropagated() 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 invalidVersion = Version.V_3_2_0;
        final String value = randomAlphaOfLength(1000);
        final boolean isRequest = randomBoolean();
        final long requestId = randomNonNegativeLong();
        OutboundMessage message;
        if (isRequest) {
            message = new OutboundMessage.Request(new TestRequest(value), invalidVersion, actionName, requestId, false, false);
        } else {
            message = new OutboundMessage.Response(new TestResponse(value), invalidVersion, requestId, false, false);
        }
        final BytesReference reference = message.serialize(streamOutput);
        try (ReleasableBytesReference releasable = ReleasableBytesReference.wrap(reference)) {
            expectThrows(IllegalStateException.class, () -> pipeline.handleBytes(new FakeTcpChannel(), releasable));
        }
        // Pipeline cannot be reused after uncaught exception
        final IllegalStateException ise = expectThrows(IllegalStateException.class, () -> pipeline.handleBytes(new FakeTcpChannel(), ReleasableBytesReference.wrap(BytesArray.EMPTY)));
        assertEquals("Pipeline state corrupted by uncaught exception", ise.getMessage());
    }
}
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) LongSupplier(java.util.function.LongSupplier)

Example 7 with ReleasableBytesReference

use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.

the class InboundAggregatorTests method testFinishAggregationWillFinishHeader.

public void testFinishAggregationWillFinishHeader() throws IOException {
    long requestId = randomNonNegativeLong();
    final String actionName;
    final boolean unknownAction = randomBoolean();
    if (unknownAction) {
        actionName = this.unknownAction;
    } else {
        actionName = "action_name";
    }
    Header header = new Header(randomInt(), requestId, TransportStatus.setRequest((byte) 0), Version.CURRENT);
    // Initiate Message
    aggregator.headerReceived(header);
    try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
        ThreadContext.bwcWriteHeaders(streamOutput);
        streamOutput.writeString(actionName);
        streamOutput.write(randomByteArrayOfLength(10));
        final ReleasableBytesReference content = ReleasableBytesReference.wrap(streamOutput.bytes());
        aggregator.aggregate(content);
        content.close();
        // Signal EOS
        InboundMessage aggregated = aggregator.finishAggregation();
        assertThat(aggregated, notNullValue());
        assertFalse(header.needsToReadVariableHeader());
        assertEquals(actionName, header.getActionName());
        if (unknownAction) {
            assertEquals(0, content.refCount());
            assertTrue(aggregated.isShortCircuit());
        } else {
            assertEquals(1, content.refCount());
            assertFalse(aggregated.isShortCircuit());
        }
    }
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 8 with ReleasableBytesReference

use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.

the class InboundAggregatorTests method testCircuitBreak.

public void testCircuitBreak() throws IOException {
    circuitBreaker.startBreaking();
    // Actions are breakable
    Header breakableHeader = new Header(randomInt(), randomNonNegativeLong(), TransportStatus.setRequest((byte) 0), Version.CURRENT);
    breakableHeader.bwcNeedsToReadVariableHeader = false;
    breakableHeader.actionName = "action_name";
    // Initiate Message
    aggregator.headerReceived(breakableHeader);
    BytesArray bytes = new BytesArray(randomByteArrayOfLength(10));
    final ReleasableBytesReference content1 = ReleasableBytesReference.wrap(bytes);
    aggregator.aggregate(content1);
    content1.close();
    // Signal EOS
    InboundMessage aggregated1 = aggregator.finishAggregation();
    assertEquals(0, content1.refCount());
    assertThat(aggregated1, notNullValue());
    assertTrue(aggregated1.isShortCircuit());
    assertThat(aggregated1.getException(), instanceOf(CircuitBreakingException.class));
    // Actions marked as unbreakable are not broken
    Header unbreakableHeader = new Header(randomInt(), randomNonNegativeLong(), TransportStatus.setRequest((byte) 0), Version.CURRENT);
    unbreakableHeader.bwcNeedsToReadVariableHeader = false;
    unbreakableHeader.actionName = unBreakableAction;
    // Initiate Message
    aggregator.headerReceived(unbreakableHeader);
    final ReleasableBytesReference content2 = ReleasableBytesReference.wrap(bytes);
    aggregator.aggregate(content2);
    content2.close();
    // Signal EOS
    InboundMessage aggregated2 = aggregator.finishAggregation();
    assertEquals(1, content2.refCount());
    assertThat(aggregated2, notNullValue());
    assertFalse(aggregated2.isShortCircuit());
    // Handshakes are not broken
    final byte handshakeStatus = TransportStatus.setHandshake(TransportStatus.setRequest((byte) 0));
    Header handshakeHeader = new Header(randomInt(), randomNonNegativeLong(), handshakeStatus, Version.CURRENT);
    handshakeHeader.bwcNeedsToReadVariableHeader = false;
    handshakeHeader.actionName = "handshake";
    // Initiate Message
    aggregator.headerReceived(handshakeHeader);
    final ReleasableBytesReference content3 = ReleasableBytesReference.wrap(bytes);
    aggregator.aggregate(content3);
    content3.close();
    // Signal EOS
    InboundMessage aggregated3 = aggregator.finishAggregation();
    assertEquals(1, content3.refCount());
    assertThat(aggregated3, notNullValue());
    assertFalse(aggregated3.isShortCircuit());
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException)

Example 9 with ReleasableBytesReference

use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.

the class InboundAggregatorTests method testCloseWillCloseContent.

public void testCloseWillCloseContent() {
    long requestId = randomNonNegativeLong();
    Header header = new Header(randomInt(), requestId, TransportStatus.setRequest((byte) 0), Version.CURRENT);
    header.bwcNeedsToReadVariableHeader = false;
    header.actionName = "action_name";
    // Initiate Message
    aggregator.headerReceived(header);
    BytesArray bytes = new BytesArray(randomByteArrayOfLength(10));
    ArrayList<ReleasableBytesReference> references = new ArrayList<>();
    if (randomBoolean()) {
        final ReleasableBytesReference content = ReleasableBytesReference.wrap(bytes);
        references.add(content);
        aggregator.aggregate(content);
        content.close();
    } else {
        final ReleasableBytesReference content1 = ReleasableBytesReference.wrap(bytes.slice(0, 5));
        references.add(content1);
        aggregator.aggregate(content1);
        content1.close();
        final ReleasableBytesReference content2 = ReleasableBytesReference.wrap(bytes.slice(5, 5));
        references.add(content2);
        aggregator.aggregate(content2);
        content2.close();
    }
    aggregator.close();
    for (ReleasableBytesReference reference : references) {
        assertEquals(0, reference.refCount());
    }
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) ArrayList(java.util.ArrayList)

Example 10 with ReleasableBytesReference

use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.

the class OutboundHandlerTests method testErrorResponse.

@Test
public void testErrorResponse() throws IOException {
    Version version = randomFrom(Version.CURRENT, Version.CURRENT.minimumCompatibilityVersion());
    String action = "handshake";
    long requestId = randomLongBetween(0, 300);
    ElasticsearchException error = new ElasticsearchException("boom");
    AtomicLong requestIdRef = new AtomicLong();
    AtomicReference<String> actionRef = new AtomicReference<>();
    AtomicReference<Exception> responseRef = new AtomicReference<>();
    handler.setMessageListener(new TransportMessageListener() {

        @Override
        public void onResponseSent(long requestId, String action, Exception error) {
            requestIdRef.set(requestId);
            actionRef.set(action);
            responseRef.set(error);
        }
    });
    handler.sendErrorResponse(version, channel, requestId, action, error);
    BytesReference reference = channel.getMessageCaptor().get();
    ActionListener<Void> sendListener = channel.getListenerCaptor().get();
    if (randomBoolean()) {
        sendListener.onResponse(null);
    } else {
        sendListener.onFailure(new IOException("failed"));
    }
    assertEquals(requestId, requestIdRef.get());
    assertEquals(action, actionRef.get());
    assertEquals(error, responseRef.get());
    pipeline.handleBytes(channel, new ReleasableBytesReference(reference, () -> {
    }));
    final Tuple<Header, BytesReference> tuple = message.get();
    final Header header = tuple.v1();
    assertEquals(version, header.getVersion());
    assertEquals(requestId, header.getRequestId());
    assertFalse(header.isRequest());
    assertTrue(header.isResponse());
    assertFalse(header.isCompressed());
    assertFalse(header.isHandshake());
    assertTrue(header.isError());
    RemoteTransportException remoteException = tuple.v2().streamInput().readException();
    assertThat(remoteException.getCause(), instanceOf(ElasticsearchException.class));
    assertEquals(remoteException.getCause().getMessage(), "boom");
    assertEquals(action, remoteException.action());
    assertEquals(channel.getLocalAddress(), remoteException.address().address());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) AtomicReference(java.util.concurrent.atomic.AtomicReference) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Version(org.elasticsearch.Version) Test(org.junit.Test)

Aggregations

ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)29 BytesReference (org.elasticsearch.common.bytes.BytesReference)16 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)13 ArrayList (java.util.ArrayList)11 Version (org.elasticsearch.Version)10 IOException (java.io.IOException)8 BytesArray (org.elasticsearch.common.bytes.BytesArray)7 CompositeBytesReference (org.elasticsearch.common.bytes.CompositeBytesReference)5 Releasable (org.elasticsearch.common.lease.Releasable)5 CircuitBreakingException (org.elasticsearch.common.breaker.CircuitBreakingException)4 Tuple (io.crate.common.collections.Tuple)3 Streams (io.crate.common.io.Streams)3 TimeValue (io.crate.common.unit.TimeValue)3 List (java.util.List)3 Objects (java.util.Objects)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 BiConsumer (java.util.function.BiConsumer)3 LongSupplier (java.util.function.LongSupplier)3