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();
}
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();
}
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;
}
}
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()));
}
}
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());
}
Aggregations