Search in sources :

Example 1 with TestCircuitBreaker

use of org.opensearch.common.breaker.TestCircuitBreaker in project OpenSearch by opensearch-project.

the class InboundAggregatorTests method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    Predicate<String> requestCanTripBreaker = action -> {
        if (unknownAction.equals(action)) {
            throw new ActionNotFoundTransportException(action);
        } else {
            return unBreakableAction.equals(action) == false;
        }
    };
    circuitBreaker = new TestCircuitBreaker();
    aggregator = new InboundAggregator(() -> circuitBreaker, requestCanTripBreaker);
}
Also used : CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) Predicate(java.util.function.Predicate) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Version(org.opensearch.Version) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Tuple(org.opensearch.common.collect.Tuple) ArrayList(java.util.ArrayList) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) BytesArray(org.opensearch.common.bytes.BytesArray) Collections(java.util.Collections) TestCircuitBreaker(org.opensearch.common.breaker.TestCircuitBreaker) Before(org.junit.Before) TestCircuitBreaker(org.opensearch.common.breaker.TestCircuitBreaker) Before(org.junit.Before)

Example 2 with TestCircuitBreaker

use of org.opensearch.common.breaker.TestCircuitBreaker in project OpenSearch by opensearch-project.

the class InboundPipelineTests method testPipelineHandling.

public void testPipelineHandling() throws IOException {
    final List<Tuple<MessageData, Exception>> expected = new ArrayList<>();
    final List<Tuple<MessageData, Exception>> actual = new ArrayList<>();
    final List<ReleasableBytesReference> toRelease = new ArrayList<>();
    final BiConsumer<TcpChannel, InboundMessage> messageHandler = (c, m) -> {
        try {
            final Header header = m.getHeader();
            final MessageData actualData;
            final Version version = header.getVersion();
            final boolean isRequest = header.isRequest();
            final long requestId = header.getRequestId();
            final boolean isCompressed = header.isCompressed();
            if (m.isShortCircuit()) {
                actualData = new MessageData(version, requestId, isRequest, isCompressed, header.getActionName(), null);
            } else if (isRequest) {
                final TestRequest request = new TestRequest(m.openOrGetStreamInput());
                actualData = new MessageData(version, requestId, isRequest, isCompressed, header.getActionName(), request.value);
            } else {
                final TestResponse response = new TestResponse(m.openOrGetStreamInput());
                actualData = new MessageData(version, requestId, isRequest, isCompressed, null, response.value);
            }
            actual.add(new Tuple<>(actualData, m.getException()));
        } catch (IOException e) {
            throw new AssertionError(e);
        }
    };
    final StatsTracker statsTracker = new StatsTracker();
    final LongSupplier millisSupplier = () -> TimeValue.nsecToMSec(System.nanoTime());
    final InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
    final String breakThisAction = "break_this_action";
    final String actionName = "actionName";
    final Predicate<String> canTripBreaker = breakThisAction::equals;
    final TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    circuitBreaker.startBreaking();
    final InboundAggregator aggregator = new InboundAggregator(() -> circuitBreaker, canTripBreaker);
    final InboundPipeline pipeline = new InboundPipeline(statsTracker, millisSupplier, decoder, aggregator, messageHandler);
    final FakeTcpChannel channel = new FakeTcpChannel();
    final int iterations = randomIntBetween(5, 10);
    long totalMessages = 0;
    long bytesReceived = 0;
    for (int i = 0; i < iterations; ++i) {
        actual.clear();
        expected.clear();
        toRelease.clear();
        try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
            while (streamOutput.size() < BYTE_THRESHOLD) {
                final Version version = randomFrom(Version.CURRENT, Version.CURRENT.minimumCompatibilityVersion());
                final String value = randomAlphaOfLength(randomIntBetween(10, 200));
                final boolean isRequest = randomBoolean();
                final boolean isCompressed = randomBoolean();
                final long requestId = totalMessages++;
                final MessageData messageData;
                Exception expectedExceptionClass = null;
                OutboundMessage message;
                if (isRequest) {
                    if (rarely()) {
                        messageData = new MessageData(version, requestId, true, isCompressed, breakThisAction, null);
                        message = new OutboundMessage.Request(threadContext, new String[0], new TestRequest(value), version, breakThisAction, requestId, false, isCompressed);
                        expectedExceptionClass = new CircuitBreakingException("", CircuitBreaker.Durability.PERMANENT);
                    } else {
                        messageData = new MessageData(version, requestId, true, isCompressed, actionName, value);
                        message = new OutboundMessage.Request(threadContext, new String[0], new TestRequest(value), version, actionName, requestId, false, isCompressed);
                    }
                } else {
                    messageData = new MessageData(version, requestId, false, isCompressed, null, value);
                    message = new OutboundMessage.Response(threadContext, Collections.emptySet(), new TestResponse(value), version, requestId, false, isCompressed);
                }
                expected.add(new Tuple<>(messageData, expectedExceptionClass));
                final BytesReference reference = message.serialize(new BytesStreamOutput());
                Streams.copy(reference.streamInput(), streamOutput);
            }
            final BytesReference networkBytes = streamOutput.bytes();
            int currentOffset = 0;
            while (currentOffset != networkBytes.length()) {
                final int remainingBytes = networkBytes.length() - currentOffset;
                final int bytesToRead = Math.min(randomIntBetween(1, 32 * 1024), remainingBytes);
                final BytesReference slice = networkBytes.slice(currentOffset, bytesToRead);
                try (ReleasableBytesReference reference = new ReleasableBytesReference(slice, () -> {
                })) {
                    toRelease.add(reference);
                    bytesReceived += reference.length();
                    pipeline.handleBytes(channel, reference);
                    currentOffset += bytesToRead;
                }
            }
            final int messages = expected.size();
            for (int j = 0; j < messages; ++j) {
                final Tuple<MessageData, Exception> expectedTuple = expected.get(j);
                final Tuple<MessageData, Exception> actualTuple = actual.get(j);
                final MessageData expectedMessageData = expectedTuple.v1();
                final MessageData actualMessageData = actualTuple.v1();
                assertEquals(expectedMessageData.requestId, actualMessageData.requestId);
                assertEquals(expectedMessageData.isRequest, actualMessageData.isRequest);
                assertEquals(expectedMessageData.isCompressed, actualMessageData.isCompressed);
                assertEquals(expectedMessageData.actionName, actualMessageData.actionName);
                assertEquals(expectedMessageData.value, actualMessageData.value);
                if (expectedTuple.v2() != null) {
                    assertNotNull(actualTuple.v2());
                    assertThat(actualTuple.v2(), instanceOf(expectedTuple.v2().getClass()));
                }
            }
            for (ReleasableBytesReference released : toRelease) {
                assertEquals(0, released.refCount());
            }
        }
        assertEquals(bytesReceived, statsTracker.getBytesRead());
        assertEquals(totalMessages, statsTracker.getMessagesReceived());
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) LongSupplier(java.util.function.LongSupplier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Version(org.opensearch.Version) Releasable(org.opensearch.common.lease.Releasable) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) BiConsumer(java.util.function.BiConsumer) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) TimeValue(org.opensearch.common.unit.TimeValue) Streams(org.opensearch.core.internal.io.Streams) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Tuple(org.opensearch.common.collect.Tuple) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) BytesArray(org.opensearch.common.bytes.BytesArray) Collections(java.util.Collections) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) TestCircuitBreaker(org.opensearch.common.breaker.TestCircuitBreaker) ArrayList(java.util.ArrayList) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Version(org.opensearch.Version) BytesReference(org.opensearch.common.bytes.BytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) TestCircuitBreaker(org.opensearch.common.breaker.TestCircuitBreaker) IOException(java.io.IOException) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) IOException(java.io.IOException) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) LongSupplier(java.util.function.LongSupplier) Tuple(org.opensearch.common.collect.Tuple)

Aggregations

IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 Predicate (java.util.function.Predicate)2 Version (org.opensearch.Version)2 CircuitBreakingException (org.opensearch.common.breaker.CircuitBreakingException)2 TestCircuitBreaker (org.opensearch.common.breaker.TestCircuitBreaker)2 BytesArray (org.opensearch.common.bytes.BytesArray)2 ReleasableBytesReference (org.opensearch.common.bytes.ReleasableBytesReference)2 Tuple (org.opensearch.common.collect.Tuple)2 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)2 Settings (org.opensearch.common.settings.Settings)2 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)2 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)2 List (java.util.List)1 Objects (java.util.Objects)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 BiConsumer (java.util.function.BiConsumer)1 LongSupplier (java.util.function.LongSupplier)1 Supplier (java.util.function.Supplier)1