Search in sources :

Example 96 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class TransportDecompressorTests method testIncrementalMultiPageCompression.

public void testIncrementalMultiPageCompression() throws IOException {
    try (BytesStreamOutput output = new BytesStreamOutput()) {
        try (StreamOutput deflateStream = new OutputStreamStreamOutput(CompressorFactory.COMPRESSOR.threadLocalOutputStream(Streams.flushOnCloseStream(output)))) {
            for (int i = 0; i < 10000; ++i) {
                deflateStream.writeInt(i);
            }
        }
        BytesReference bytes = output.bytes();
        TransportDecompressor decompressor = new TransportDecompressor(PageCacheRecycler.NON_RECYCLING_INSTANCE);
        int split1 = (int) (bytes.length() * 0.3);
        int split2 = (int) (bytes.length() * 0.65);
        BytesReference inbound1 = bytes.slice(0, split1);
        BytesReference inbound2 = bytes.slice(split1, split2 - split1);
        BytesReference inbound3 = bytes.slice(split2, bytes.length() - split2);
        int bytesConsumed1 = decompressor.decompress(inbound1);
        assertEquals(inbound1.length(), bytesConsumed1);
        assertFalse(decompressor.isEOS());
        int bytesConsumed2 = decompressor.decompress(inbound2);
        assertEquals(inbound2.length(), bytesConsumed2);
        assertFalse(decompressor.isEOS());
        int bytesConsumed3 = decompressor.decompress(inbound3);
        assertEquals(inbound3.length(), bytesConsumed3);
        assertTrue(decompressor.isEOS());
        ReleasableBytesReference reference1 = decompressor.pollDecompressedPage();
        ReleasableBytesReference reference2 = decompressor.pollDecompressedPage();
        ReleasableBytesReference reference3 = decompressor.pollDecompressedPage();
        assertNull(decompressor.pollDecompressedPage());
        BytesReference composite = CompositeBytesReference.of(reference1, reference2, reference3);
        assertEquals(4 * 10000, composite.length());
        StreamInput streamInput = composite.streamInput();
        for (int i = 0; i < 10000; ++i) {
            assertEquals(i, streamInput.readInt());
        }
        Releasables.close(reference1, reference2, reference3);
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) CompositeBytesReference(org.opensearch.common.bytes.CompositeBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) OutputStreamStreamOutput(org.opensearch.common.io.stream.OutputStreamStreamOutput) StreamInput(org.opensearch.common.io.stream.StreamInput) StreamOutput(org.opensearch.common.io.stream.StreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) OutputStreamStreamOutput(org.opensearch.common.io.stream.OutputStreamStreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 97 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class TransportServiceDeserializationFailureTests method testDeserializationFailureLogIdentifiesListener.

public void testDeserializationFailureLogIdentifiesListener() {
    final DiscoveryNode localNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);
    final DiscoveryNode otherNode = new DiscoveryNode("other", buildNewFakeTransportAddress(), Version.CURRENT);
    final Settings settings = Settings.builder().put(NODE_NAME_SETTING.getKey(), "local").build();
    final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue(settings, random());
    final String testActionName = "internal:test-action";
    final MockTransport transport = new MockTransport() {

        @Override
        protected void onSendRequest(long requestId, String action, TransportRequest request, DiscoveryNode node) {
            if (action.equals(TransportService.HANDSHAKE_ACTION_NAME)) {
                handleResponse(requestId, new TransportService.HandshakeResponse(otherNode, new ClusterName(""), Version.CURRENT));
            }
        }
    };
    final TransportService transportService = transport.createTransportService(Settings.EMPTY, deterministicTaskQueue.getThreadPool(), TransportService.NOOP_TRANSPORT_INTERCEPTOR, ignored -> localNode, null, Collections.emptySet());
    transportService.registerRequestHandler(testActionName, ThreadPool.Names.SAME, TransportRequest.Empty::new, (request, channel, task) -> channel.sendResponse(TransportResponse.Empty.INSTANCE));
    transportService.start();
    transportService.acceptIncomingRequests();
    final PlainActionFuture<Void> connectionFuture = new PlainActionFuture<>();
    transportService.connectToNode(otherNode, connectionFuture);
    assertTrue(connectionFuture.isDone());
    {
        // requests without a parent task are recorded directly in the response context
        transportService.sendRequest(otherNode, testActionName, TransportRequest.Empty.INSTANCE, TransportRequestOptions.EMPTY, new TransportResponseHandler<TransportResponse.Empty>() {

            @Override
            public void handleResponse(TransportResponse.Empty response) {
                fail("should not be called");
            }

            @Override
            public void handleException(TransportException exp) {
                fail("should not be called");
            }

            @Override
            public String executor() {
                return ThreadPool.Names.SAME;
            }

            @Override
            public TransportResponse.Empty read(StreamInput in) {
                throw new AssertionError("should not be called");
            }

            @Override
            public String toString() {
                return "test handler without parent";
            }
        });
        final List<Transport.ResponseContext<? extends TransportResponse>> responseContexts = transport.getResponseHandlers().prune(ignored -> true);
        assertThat(responseContexts, hasSize(1));
        final TransportResponseHandler<? extends TransportResponse> handler = responseContexts.get(0).handler();
        assertThat(handler, hasToString(containsString("test handler without parent")));
    }
    {
        // requests with a parent task get wrapped up by the transport service, including the action name
        final Task parentTask = transportService.getTaskManager().register("test", "test-action", new TaskAwareRequest() {

            @Override
            public void setParentTask(TaskId taskId) {
                fail("should not be called");
            }

            @Override
            public TaskId getParentTask() {
                return TaskId.EMPTY_TASK_ID;
            }
        });
        transportService.sendChildRequest(otherNode, testActionName, TransportRequest.Empty.INSTANCE, parentTask, TransportRequestOptions.EMPTY, new TransportResponseHandler<TransportResponse.Empty>() {

            @Override
            public void handleResponse(TransportResponse.Empty response) {
                fail("should not be called");
            }

            @Override
            public void handleException(TransportException exp) {
                fail("should not be called");
            }

            @Override
            public String executor() {
                return ThreadPool.Names.SAME;
            }

            @Override
            public TransportResponse.Empty read(StreamInput in) {
                throw new AssertionError("should not be called");
            }

            @Override
            public String toString() {
                return "test handler with parent";
            }
        });
        final List<Transport.ResponseContext<? extends TransportResponse>> responseContexts = transport.getResponseHandlers().prune(ignored -> true);
        assertThat(responseContexts, hasSize(1));
        final TransportResponseHandler<? extends TransportResponse> handler = responseContexts.get(0).handler();
        assertThat(handler, hasToString(allOf(containsString("test handler with parent"), containsString(testActionName))));
    }
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) Matchers.hasToString(org.hamcrest.Matchers.hasToString) DeterministicTaskQueue(org.opensearch.cluster.coordination.DeterministicTaskQueue) Matchers.allOf(org.hamcrest.Matchers.allOf) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) ThreadPool(org.opensearch.threadpool.ThreadPool) TaskId(org.opensearch.tasks.TaskId) Version(org.opensearch.Version) Settings(org.opensearch.common.settings.Settings) Task(org.opensearch.tasks.Task) TaskAwareRequest(org.opensearch.tasks.TaskAwareRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) List(java.util.List) NODE_NAME_SETTING(org.opensearch.node.Node.NODE_NAME_SETTING) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) MockTransport(org.opensearch.test.transport.MockTransport) ClusterName(org.opensearch.cluster.ClusterName) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Task(org.opensearch.tasks.Task) TaskId(org.opensearch.tasks.TaskId) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) TaskAwareRequest(org.opensearch.tasks.TaskAwareRequest) DeterministicTaskQueue(org.opensearch.cluster.coordination.DeterministicTaskQueue) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) MockTransport(org.opensearch.test.transport.MockTransport) StreamInput(org.opensearch.common.io.stream.StreamInput) ClusterName(org.opensearch.cluster.ClusterName) List(java.util.List) MockTransport(org.opensearch.test.transport.MockTransport) Settings(org.opensearch.common.settings.Settings)

Example 98 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class AbstractBytesReferenceTestCase method testRandomReads.

public void testRandomReads() throws IOException {
    int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20));
    BytesReference pbr = newBytesReference(length);
    StreamInput streamInput = pbr.streamInput();
    BytesRefBuilder target = new BytesRefBuilder();
    while (target.length() < pbr.length()) {
        switch(randomIntBetween(0, 10)) {
            case 6:
            case 5:
                target.append(new BytesRef(new byte[] { streamInput.readByte() }));
                break;
            case 4:
            case 3:
                BytesRef bytesRef = streamInput.readBytesRef(scaledRandomIntBetween(1, pbr.length() - target.length()));
                target.append(bytesRef);
                break;
            default:
                byte[] buffer = new byte[scaledRandomIntBetween(1, pbr.length() - target.length())];
                int offset = scaledRandomIntBetween(0, buffer.length - 1);
                int read = streamInput.read(buffer, offset, buffer.length - offset);
                target.append(new BytesRef(buffer, offset, read));
                break;
        }
    }
    assertEquals(pbr.length(), target.length());
    BytesRef targetBytes = target.get();
    assertArrayEquals(BytesReference.toBytes(pbr), Arrays.copyOfRange(targetBytes.bytes, targetBytes.offset, targetBytes.length));
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) StreamInput(org.opensearch.common.io.stream.StreamInput) BytesRef(org.apache.lucene.util.BytesRef)

Example 99 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class AbstractBytesReferenceTestCase method testSliceStreamInput.

public void testSliceStreamInput() throws IOException {
    int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20));
    BytesReference pbr = newBytesReference(length);
    // test stream input over slice (upper half of original)
    int sliceOffset = randomIntBetween(1, length / 2);
    int sliceLength = length - sliceOffset;
    BytesReference slice = pbr.slice(sliceOffset, sliceLength);
    StreamInput sliceInput = slice.streamInput();
    assertEquals(sliceInput.available(), sliceLength);
    // single reads
    assertEquals(slice.get(0), sliceInput.readByte());
    assertEquals(slice.get(1), sliceInput.readByte());
    assertEquals(slice.get(2), sliceInput.readByte());
    assertEquals(sliceInput.available(), sliceLength - 3);
    // reset the slice stream for bulk reading
    sliceInput.reset();
    assertEquals(sliceInput.available(), sliceLength);
    // bulk read
    byte[] sliceBytes = new byte[sliceLength];
    sliceInput.readFully(sliceBytes);
    assertEquals(sliceInput.available(), 0);
    // compare slice content with upper half of original
    byte[] pbrSliceBytes = Arrays.copyOfRange(BytesReference.toBytes(pbr), sliceOffset, length);
    assertArrayEquals(pbrSliceBytes, sliceBytes);
    // compare slice bytes with bytes read from slice via streamInput :D
    byte[] sliceToBytes = BytesReference.toBytes(slice);
    assertEquals(sliceBytes.length, sliceToBytes.length);
    assertArrayEquals(sliceBytes, sliceToBytes);
    sliceInput.reset();
    assertEquals(sliceInput.available(), sliceLength);
    byte[] buffer = new byte[sliceLength + scaledRandomIntBetween(1, 100)];
    int offset = scaledRandomIntBetween(0, Math.max(1, buffer.length - sliceLength - 1));
    int read = sliceInput.read(buffer, offset, sliceLength / 2);
    assertEquals(sliceInput.available(), sliceLength - read);
    sliceInput.read(buffer, offset + read, sliceLength - read);
    assertArrayEquals(sliceBytes, Arrays.copyOfRange(buffer, offset, offset + sliceLength));
    assertEquals(sliceInput.available(), 0);
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput)

Example 100 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class AbstractBytesReferenceTestCase method testInputStreamSkip.

public void testInputStreamSkip() throws IOException {
    int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20));
    BytesReference pbr = newBytesReference(length);
    final int iters = randomIntBetween(5, 50);
    for (int i = 0; i < iters; i++) {
        try (StreamInput input = pbr.streamInput()) {
            final int offset = randomIntBetween(0, length - 1);
            assertEquals(offset, input.skip(offset));
            assertEquals(pbr.get(offset), input.readByte());
            if (offset == length - 1) {
                // no more bytes to retrieve!
                continue;
            }
            final int nextOffset = randomIntBetween(offset, length - 2);
            assertEquals(nextOffset - offset, input.skip(nextOffset - offset));
            // +1 for the one byte we read above
            assertEquals(pbr.get(nextOffset + 1), input.readByte());
            assertEquals(length - (nextOffset + 2), input.skip(Long.MAX_VALUE));
            assertEquals(0, input.skip(randomIntBetween(0, Integer.MAX_VALUE)));
        }
    }
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput)

Aggregations

StreamInput (org.opensearch.common.io.stream.StreamInput)206 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)139 IOException (java.io.IOException)42 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)40 Version (org.opensearch.Version)27 OpenSearchException (org.opensearch.OpenSearchException)21 InputStreamStreamInput (org.opensearch.common.io.stream.InputStreamStreamInput)19 BytesReference (org.opensearch.common.bytes.BytesReference)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 Matchers.hasToString (org.hamcrest.Matchers.hasToString)17 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)17 UncheckedIOException (java.io.UncheckedIOException)15 CountDownLatch (java.util.concurrent.CountDownLatch)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)13 ArrayList (java.util.ArrayList)12 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 UnknownHostException (java.net.UnknownHostException)11 ExecutionException (java.util.concurrent.ExecutionException)11