Search in sources :

Example 91 with StreamInput

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

the class TransportLogger method format.

private static String format(TcpChannel channel, InboundMessage message, String event) throws IOException {
    final StringBuilder sb = new StringBuilder();
    sb.append(channel);
    if (message.isPing()) {
        sb.append(" [ping]").append(' ').append(event).append(": ").append(6).append('B');
    } else {
        boolean success = false;
        Header header = message.getHeader();
        int networkMessageSize = header.getNetworkMessageSize();
        int messageLengthWithHeader = HEADER_SIZE + networkMessageSize;
        StreamInput streamInput = message.openOrGetStreamInput();
        try {
            final long requestId = header.getRequestId();
            final boolean isRequest = header.isRequest();
            final String type = isRequest ? "request" : "response";
            final String version = header.getVersion().toString();
            sb.append(" [length: ").append(messageLengthWithHeader);
            sb.append(", request id: ").append(requestId);
            sb.append(", type: ").append(type);
            sb.append(", version: ").append(version);
            // TODO: Maybe Fix for BWC
            if (header.needsToReadVariableHeader() == false && isRequest) {
                sb.append(", action: ").append(header.getActionName());
            }
            sb.append(']');
            sb.append(' ').append(event).append(": ").append(messageLengthWithHeader).append('B');
            success = true;
        } finally {
            if (success) {
                IOUtils.close(streamInput);
            } else {
                IOUtils.closeWhileHandlingException(streamInput);
            }
        }
    }
    return sb.toString();
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput)

Example 92 with StreamInput

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

the class TransportLogger method format.

private static String format(TcpChannel channel, BytesReference message, String event) throws IOException {
    final StringBuilder sb = new StringBuilder();
    sb.append(channel);
    int messageLengthWithHeader = HEADER_SIZE + message.length();
    // This is a ping
    if (message.length() == 0) {
        sb.append(" [ping]").append(' ').append(event).append(": ").append(messageLengthWithHeader).append('B');
    } else {
        boolean success = false;
        StreamInput streamInput = message.streamInput();
        try {
            final long requestId = streamInput.readLong();
            final byte status = streamInput.readByte();
            final boolean isRequest = TransportStatus.isRequest(status);
            final String type = isRequest ? "request" : "response";
            final Version version = Version.fromId(streamInput.readInt());
            streamInput.setVersion(version);
            sb.append(" [length: ").append(messageLengthWithHeader);
            sb.append(", request id: ").append(requestId);
            sb.append(", type: ").append(type);
            sb.append(", version: ").append(version);
            if (version.onOrAfter(TcpHeader.VERSION_WITH_HEADER_SIZE)) {
                sb.append(", header size: ").append(streamInput.readInt()).append('B');
            } else {
                streamInput = decompressingStream(status, streamInput);
                InboundHandler.assertRemoteVersion(streamInput, version);
            }
            // read and discard headers
            ThreadContext.readHeadersFromStream(streamInput);
            if (isRequest) {
                // discard features
                streamInput.readStringArray();
                sb.append(", action: ").append(streamInput.readString());
            }
            sb.append(']');
            sb.append(' ').append(event).append(": ").append(messageLengthWithHeader).append('B');
            success = true;
        } finally {
            if (success) {
                IOUtils.close(streamInput);
            } else {
                IOUtils.closeWhileHandlingException(streamInput);
            }
        }
    }
    return sb.toString();
}
Also used : Version(org.opensearch.Version) StreamInput(org.opensearch.common.io.stream.StreamInput) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput)

Example 93 with StreamInput

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

the class InboundDecoder method readHeader.

// exposed for use in tests
static Header readHeader(Version version, int networkMessageSize, BytesReference bytesReference) throws IOException {
    try (StreamInput streamInput = bytesReference.streamInput()) {
        streamInput.skip(TcpHeader.BYTES_REQUIRED_FOR_MESSAGE_SIZE);
        long requestId = streamInput.readLong();
        byte status = streamInput.readByte();
        Version remoteVersion = Version.fromId(streamInput.readInt());
        Header header = new Header(networkMessageSize, requestId, status, remoteVersion);
        final IllegalStateException invalidVersion = ensureVersionCompatibility(remoteVersion, version, header.isHandshake());
        if (invalidVersion != null) {
            throw invalidVersion;
        } else {
            if (remoteVersion.onOrAfter(TcpHeader.VERSION_WITH_HEADER_SIZE)) {
                // Skip since we already have ensured enough data available
                streamInput.readInt();
                header.finishParsingHeader(streamInput);
            }
        }
        return header;
    }
}
Also used : Version(org.opensearch.Version) StreamInput(org.opensearch.common.io.stream.StreamInput)

Example 94 with StreamInput

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

the class ClusterHealthRequestTests method testBwcSerialization.

public void testBwcSerialization() throws Exception {
    for (int runs = 0; runs < randomIntBetween(5, 20); runs++) {
        // Generate a random cluster health request in version < 7.2.0 and serializes it
        final BytesStreamOutput out = new BytesStreamOutput();
        out.setVersion(randomVersionBetween(random(), VersionUtils.getFirstVersion(), getPreviousVersion(LegacyESVersion.V_7_2_0)));
        final ClusterHealthRequest expected = randomRequest();
        {
            expected.getParentTask().writeTo(out);
            out.writeTimeValue(expected.masterNodeTimeout());
            out.writeBoolean(expected.local());
            if (expected.indices() == null) {
                out.writeVInt(0);
            } else {
                out.writeVInt(expected.indices().length);
                for (String index : expected.indices()) {
                    out.writeString(index);
                }
            }
            out.writeTimeValue(expected.timeout());
            if (expected.waitForStatus() == null) {
                out.writeBoolean(false);
            } else {
                out.writeBoolean(true);
                out.writeByte(expected.waitForStatus().value());
            }
            out.writeBoolean(expected.waitForNoRelocatingShards());
            expected.waitForActiveShards().writeTo(out);
            out.writeString(expected.waitForNodes());
            if (expected.waitForEvents() == null) {
                out.writeBoolean(false);
            } else {
                out.writeBoolean(true);
                Priority.writeTo(expected.waitForEvents(), out);
            }
            out.writeBoolean(expected.waitForNoInitializingShards());
        }
        // Deserialize and check the cluster health request
        final StreamInput in = out.bytes().streamInput();
        in.setVersion(out.getVersion());
        final ClusterHealthRequest actual = new ClusterHealthRequest(in);
        assertThat(actual.waitForStatus(), equalTo(expected.waitForStatus()));
        assertThat(actual.waitForNodes(), equalTo(expected.waitForNodes()));
        assertThat(actual.waitForNoInitializingShards(), equalTo(expected.waitForNoInitializingShards()));
        assertThat(actual.waitForNoRelocatingShards(), equalTo(expected.waitForNoRelocatingShards()));
        assertThat(actual.waitForActiveShards(), equalTo(expected.waitForActiveShards()));
        assertThat(actual.waitForEvents(), equalTo(expected.waitForEvents()));
        assertIndicesEquals(actual.indices(), expected.indices());
        assertThat(actual.indicesOptions(), equalTo(IndicesOptions.lenientExpandOpen()));
    }
    for (int runs = 0; runs < randomIntBetween(5, 20); runs++) {
        // Generate a random cluster health request in current version
        final ClusterHealthRequest expected = randomRequest();
        // Serialize to node in version < 7.2.0
        final BytesStreamOutput out = new BytesStreamOutput();
        out.setVersion(randomVersionBetween(random(), VersionUtils.getFirstVersion(), getPreviousVersion(LegacyESVersion.V_7_2_0)));
        expected.writeTo(out);
        // Deserialize and check the cluster health request
        final StreamInput in = out.bytes().streamInput();
        in.setVersion(out.getVersion());
        final ClusterHealthRequest actual = new ClusterHealthRequest(in);
        assertThat(actual.waitForStatus(), equalTo(expected.waitForStatus()));
        assertThat(actual.waitForNodes(), equalTo(expected.waitForNodes()));
        assertThat(actual.waitForNoInitializingShards(), equalTo(expected.waitForNoInitializingShards()));
        assertThat(actual.waitForNoRelocatingShards(), equalTo(expected.waitForNoRelocatingShards()));
        assertThat(actual.waitForActiveShards(), equalTo(expected.waitForActiveShards()));
        assertThat(actual.waitForEvents(), equalTo(expected.waitForEvents()));
        assertIndicesEquals(actual.indices(), expected.indices());
        assertThat(actual.indicesOptions(), equalTo(IndicesOptions.lenientExpandOpen()));
    }
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 95 with StreamInput

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

the class ClusterRerouteTests method testSerializeRequest.

public void testSerializeRequest() throws IOException {
    ClusterRerouteRequest req = new ClusterRerouteRequest();
    req.setRetryFailed(randomBoolean());
    req.dryRun(randomBoolean());
    req.explain(randomBoolean());
    req.add(new AllocateEmptyPrimaryAllocationCommand("foo", 1, "bar", randomBoolean()));
    req.timeout(TimeValue.timeValueMillis(randomIntBetween(0, 100)));
    BytesStreamOutput out = new BytesStreamOutput();
    req.writeTo(out);
    BytesReference bytes = out.bytes();
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(NetworkModule.getNamedWriteables());
    StreamInput wrap = new NamedWriteableAwareStreamInput(bytes.streamInput(), namedWriteableRegistry);
    ClusterRerouteRequest deserializedReq = new ClusterRerouteRequest(wrap);
    assertEquals(req.isRetryFailed(), deserializedReq.isRetryFailed());
    assertEquals(req.dryRun(), deserializedReq.dryRun());
    assertEquals(req.explain(), deserializedReq.explain());
    assertEquals(req.timeout(), deserializedReq.timeout());
    // allocation commands have their own tests
    assertEquals(1, deserializedReq.getCommands().commands().size());
    assertEquals(req.getCommands().commands().size(), deserializedReq.getCommands().commands().size());
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) AllocateEmptyPrimaryAllocationCommand(org.opensearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Aggregations

StreamInput (org.opensearch.common.io.stream.StreamInput)266 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)179 IOException (java.io.IOException)48 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)43 Version (org.opensearch.Version)30 Test (org.junit.Test)27 OpenSearchException (org.opensearch.OpenSearchException)22 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)19 InputStreamStreamInput (org.opensearch.common.io.stream.InputStreamStreamInput)19 Matchers.containsString (org.hamcrest.Matchers.containsString)18 BytesReference (org.opensearch.common.bytes.BytesReference)18 HashMap (java.util.HashMap)17 Matchers.hasToString (org.hamcrest.Matchers.hasToString)17 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)17 UncheckedIOException (java.io.UncheckedIOException)15 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)15 CountDownLatch (java.util.concurrent.CountDownLatch)14 TransportException (org.opensearch.transport.TransportException)14 ArrayList (java.util.ArrayList)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13