use of org.opensearch.Version 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.Version in project OpenSearch by opensearch-project.
the class InboundDecoder method headerBytesToRead.
private int headerBytesToRead(BytesReference reference) {
if (reference.length() < TcpHeader.BYTES_REQUIRED_FOR_VERSION) {
return 0;
}
Version remoteVersion = Version.fromId(reference.getInt(TcpHeader.VERSION_POSITION));
int fixedHeaderSize = TcpHeader.headerSize(remoteVersion);
if (fixedHeaderSize > reference.length()) {
return 0;
} else if (remoteVersion.before(TcpHeader.VERSION_WITH_HEADER_SIZE)) {
return fixedHeaderSize;
} else {
int variableHeaderSize = reference.getInt(TcpHeader.VARIABLE_HEADER_SIZE_POSITION);
int totalHeaderSize = fixedHeaderSize + variableHeaderSize;
if (totalHeaderSize > reference.length()) {
return 0;
} else {
return totalHeaderSize;
}
}
}
use of org.opensearch.Version 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.Version in project OpenSearch by opensearch-project.
the class ClusterSearchShardsRequestTests method testSerialization.
public void testSerialization() throws Exception {
ClusterSearchShardsRequest request = new ClusterSearchShardsRequest();
if (randomBoolean()) {
int numIndices = randomIntBetween(1, 5);
String[] indices = new String[numIndices];
for (int i = 0; i < numIndices; i++) {
indices[i] = randomAlphaOfLengthBetween(3, 10);
}
request.indices(indices);
}
if (randomBoolean()) {
request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()));
}
if (randomBoolean()) {
request.preference(randomAlphaOfLengthBetween(3, 10));
}
if (randomBoolean()) {
int numRoutings = randomIntBetween(1, 3);
String[] routings = new String[numRoutings];
for (int i = 0; i < numRoutings; i++) {
routings[i] = randomAlphaOfLengthBetween(3, 10);
}
request.routing(routings);
}
Version version = VersionUtils.randomCompatibleVersion(random(), Version.CURRENT);
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.setVersion(version);
request.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
in.setVersion(version);
ClusterSearchShardsRequest deserialized = new ClusterSearchShardsRequest(in);
assertArrayEquals(request.indices(), deserialized.indices());
// read from a prior version
if (version.onOrAfter(LegacyESVersion.V_7_7_0) || request.indicesOptions().expandWildcardsHidden()) {
assertEquals(request.indicesOptions(), deserialized.indicesOptions());
}
assertEquals(request.routing(), deserialized.routing());
assertEquals(request.preference(), deserialized.preference());
}
}
}
use of org.opensearch.Version in project OpenSearch by opensearch-project.
the class ClusterHealthResponsesTests method testVersionCompatibleSerialization.
public void testVersionCompatibleSerialization() throws IOException {
boolean hasDiscoveredMaster = false;
int indicesSize = randomInt(20);
Map<String, ClusterIndexHealth> indices = new HashMap<>(indicesSize);
if ("indices".equals(level) || "shards".equals(level)) {
for (int i = 0; i < indicesSize; i++) {
String indexName = randomAlphaOfLengthBetween(1, 5) + i;
indices.put(indexName, ClusterIndexHealthTests.randomIndexHealth(indexName, level));
}
}
ClusterStateHealth stateHealth = new ClusterStateHealth(randomInt(100), randomInt(100), randomInt(100), randomInt(100), randomInt(100), randomInt(100), randomInt(100), hasDiscoveredMaster, randomDoubleBetween(0d, 100d, true), randomFrom(ClusterHealthStatus.values()), indices);
// Create the Cluster Health Response object with discovered master as false,
// to verify serialization puts default value for the field
ClusterHealthResponse clusterHealth = new ClusterHealthResponse("test-cluster", randomInt(100), randomInt(100), randomInt(100), TimeValue.timeValueMillis(randomInt(10000)), randomBoolean(), stateHealth);
BytesStreamOutput out_lt_1_0 = new BytesStreamOutput();
Version old_version = VersionUtils.randomVersionBetween(random(), LegacyESVersion.V_7_0_0, LegacyESVersion.V_7_8_0);
out_lt_1_0.setVersion(old_version);
clusterHealth.writeTo(out_lt_1_0);
BytesStreamOutput out_gt_1_0 = new BytesStreamOutput();
Version new_version = VersionUtils.randomVersionBetween(random(), Version.V_1_0_0, Version.CURRENT);
out_gt_1_0.setVersion(new_version);
clusterHealth.writeTo(out_gt_1_0);
// The serialized output byte stream will not be same; and different by a boolean field "discovered_master"
assertNotEquals(out_lt_1_0.size(), out_gt_1_0.size());
assertThat(out_gt_1_0.size() - out_lt_1_0.size(), Matchers.equalTo(1));
// Input stream constructed from Version 6_8 or less will not have field "discovered_master";
// hence fallback to default as no value retained
StreamInput in_lt_6_8 = out_lt_1_0.bytes().streamInput();
in_lt_6_8.setVersion(old_version);
clusterHealth = ClusterHealthResponse.readResponseFrom(in_lt_6_8);
assertThat(clusterHealth.hasDiscoveredMaster(), Matchers.equalTo(true));
// Input stream constructed from Version 7_0 and above will have field "discovered_master"; hence value will be retained
StreamInput in_gt_7_0 = out_gt_1_0.bytes().streamInput();
in_gt_7_0.setVersion(new_version);
clusterHealth = ClusterHealthResponse.readResponseFrom(in_gt_7_0);
assertThat(clusterHealth.hasDiscoveredMaster(), Matchers.equalTo(hasDiscoveredMaster));
}
Aggregations