use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class InboundAggregatorTests method testInboundAggregation.
public void testInboundAggregation() throws IOException {
long requestId = randomNonNegativeLong();
Header header = new Header(randomInt(), requestId, TransportStatus.setRequest((byte) 0), Version.CURRENT);
header.bwcNeedsToReadVariableHeader = false;
header.actionName = "action_name";
// Initiate Message
aggregator.headerReceived(header);
BytesArray bytes = new BytesArray(randomByteArrayOfLength(10));
ArrayList<ReleasableBytesReference> references = new ArrayList<>();
if (randomBoolean()) {
final ReleasableBytesReference content = ReleasableBytesReference.wrap(bytes);
references.add(content);
aggregator.aggregate(content);
content.close();
} else {
final ReleasableBytesReference content1 = ReleasableBytesReference.wrap(bytes.slice(0, 3));
references.add(content1);
aggregator.aggregate(content1);
content1.close();
final ReleasableBytesReference content2 = ReleasableBytesReference.wrap(bytes.slice(3, 3));
references.add(content2);
aggregator.aggregate(content2);
content2.close();
final ReleasableBytesReference content3 = ReleasableBytesReference.wrap(bytes.slice(6, 4));
references.add(content3);
aggregator.aggregate(content3);
content3.close();
}
// Signal EOS
InboundMessage aggregated = aggregator.finishAggregation();
assertThat(aggregated, notNullValue());
assertFalse(aggregated.isPing());
assertTrue(aggregated.getHeader().isRequest());
assertThat(aggregated.getHeader().getRequestId(), equalTo(requestId));
assertThat(aggregated.getHeader().getVersion(), equalTo(Version.CURRENT));
for (ReleasableBytesReference reference : references) {
assertEquals(1, reference.refCount());
}
aggregated.close();
for (ReleasableBytesReference reference : references) {
assertEquals(0, reference.refCount());
}
}
use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class OutboundHandlerTests method testSendRequest.
@Test
public void testSendRequest() throws IOException {
Version version = randomFrom(Version.CURRENT, Version.CURRENT.minimumCompatibilityVersion());
String action = "handshake";
long requestId = randomLongBetween(0, 300);
boolean isHandshake = randomBoolean();
boolean compress = randomBoolean();
String value = "message";
TestRequest request = new TestRequest(value);
AtomicReference<DiscoveryNode> nodeRef = new AtomicReference<>();
AtomicLong requestIdRef = new AtomicLong();
AtomicReference<String> actionRef = new AtomicReference<>();
AtomicReference<TransportRequest> requestRef = new AtomicReference<>();
handler.setMessageListener(new TransportMessageListener() {
@Override
public void onRequestSent(DiscoveryNode node, long requestId, String action, TransportRequest request, TransportRequestOptions options) {
nodeRef.set(node);
requestIdRef.set(requestId);
actionRef.set(action);
requestRef.set(request);
}
});
handler.sendRequest(node, channel, requestId, action, request, options, version, compress, isHandshake);
BytesReference reference = channel.getMessageCaptor().get();
ActionListener<Void> sendListener = channel.getListenerCaptor().get();
if (randomBoolean()) {
sendListener.onResponse(null);
} else {
sendListener.onFailure(new IOException("failed"));
}
assertEquals(node, nodeRef.get());
assertEquals(requestId, requestIdRef.get());
assertEquals(action, actionRef.get());
assertEquals(request, requestRef.get());
pipeline.handleBytes(channel, new ReleasableBytesReference(reference, () -> {
}));
final Tuple<Header, BytesReference> tuple = message.get();
final Header header = tuple.v1();
final TestRequest message = new TestRequest(tuple.v2().streamInput());
assertEquals(version, header.getVersion());
assertEquals(requestId, header.getRequestId());
assertTrue(header.isRequest());
assertFalse(header.isResponse());
if (isHandshake) {
assertTrue(header.isHandshake());
} else {
assertFalse(header.isHandshake());
}
if (compress) {
assertTrue(header.isCompressed());
} else {
assertFalse(header.isCompressed());
}
assertEquals(value, message.value);
}
use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class OutboundHandlerTests method testSendResponse.
@Test
public void testSendResponse() throws IOException {
Version version = randomFrom(Version.CURRENT, Version.CURRENT.minimumCompatibilityVersion());
String action = "handshake";
long requestId = randomLongBetween(0, 300);
boolean isHandshake = randomBoolean();
boolean compress = randomBoolean();
String value = "message";
TestResponse response = new TestResponse(value);
AtomicLong requestIdRef = new AtomicLong();
AtomicReference<String> actionRef = new AtomicReference<>();
AtomicReference<TransportResponse> responseRef = new AtomicReference<>();
handler.setMessageListener(new TransportMessageListener() {
@Override
public void onResponseSent(long requestId, String action, TransportResponse response) {
requestIdRef.set(requestId);
actionRef.set(action);
responseRef.set(response);
}
});
handler.sendResponse(version, channel, requestId, action, response, compress, isHandshake);
BytesReference reference = channel.getMessageCaptor().get();
ActionListener<Void> sendListener = channel.getListenerCaptor().get();
if (randomBoolean()) {
sendListener.onResponse(null);
} else {
sendListener.onFailure(new IOException("failed"));
}
assertEquals(requestId, requestIdRef.get());
assertEquals(action, actionRef.get());
assertEquals(response, responseRef.get());
pipeline.handleBytes(channel, new ReleasableBytesReference(reference, () -> {
}));
final Tuple<Header, BytesReference> tuple = message.get();
final Header header = tuple.v1();
final TestResponse message = new TestResponse(tuple.v2().streamInput());
assertEquals(version, header.getVersion());
assertEquals(requestId, header.getRequestId());
assertFalse(header.isRequest());
assertTrue(header.isResponse());
if (isHandshake) {
assertTrue(header.isHandshake());
} else {
assertFalse(header.isHandshake());
}
if (compress) {
assertTrue(header.isCompressed());
} else {
assertFalse(header.isCompressed());
}
assertFalse(header.isError());
assertEquals(value, message.value);
}
use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class InboundDecoderTests method testCompressedDecode.
public void testCompressedDecode() throws IOException {
boolean isRequest = randomBoolean();
String action = "test-request";
long requestId = randomNonNegativeLong();
OutboundMessage message;
TransportMessage transportMessage;
if (isRequest) {
transportMessage = new TestRequest(randomAlphaOfLength(100));
message = new OutboundMessage.Request(transportMessage, Version.CURRENT, action, requestId, false, true);
} else {
transportMessage = new TestResponse(randomAlphaOfLength(100));
message = new OutboundMessage.Response(transportMessage, Version.CURRENT, requestId, false, true);
}
final BytesReference totalBytes = message.serialize(new BytesStreamOutput());
final BytesStreamOutput out = new BytesStreamOutput();
transportMessage.writeTo(out);
final BytesReference uncompressedBytes = out.bytes();
int totalHeaderSize = TcpHeader.headerSize(Version.CURRENT) + totalBytes.getInt(TcpHeader.VARIABLE_HEADER_SIZE_POSITION);
InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
final ArrayList<Object> fragments = new ArrayList<>();
final ReleasableBytesReference releasable1 = ReleasableBytesReference.wrap(totalBytes);
int bytesConsumed = decoder.decode(releasable1, fragments::add);
assertEquals(totalHeaderSize, bytesConsumed);
assertEquals(1, releasable1.refCount());
final Header header = (Header) fragments.get(0);
assertEquals(requestId, header.getRequestId());
assertEquals(Version.CURRENT, header.getVersion());
assertTrue(header.isCompressed());
assertFalse(header.isHandshake());
if (isRequest) {
assertEquals(action, header.getActionName());
assertTrue(header.isRequest());
} else {
assertTrue(header.isResponse());
}
assertFalse(header.needsToReadVariableHeader());
fragments.clear();
final BytesReference bytes2 = totalBytes.slice(bytesConsumed, totalBytes.length() - bytesConsumed);
final ReleasableBytesReference releasable2 = ReleasableBytesReference.wrap(bytes2);
int bytesConsumed2 = decoder.decode(releasable2, fragments::add);
assertEquals(totalBytes.length() - totalHeaderSize, bytesConsumed2);
final Object content = fragments.get(0);
final Object endMarker = fragments.get(1);
assertEquals(uncompressedBytes, content);
// Ref count is not incremented since the bytes are immediately consumed on decompression
assertEquals(1, releasable2.refCount());
assertEquals(InboundDecoder.END_CONTENT, endMarker);
}
use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class InboundDecoderTests method testCompressedDecodeHandshakeCompatibility.
public void testCompressedDecodeHandshakeCompatibility() throws IOException {
String action = "test-request";
long requestId = randomNonNegativeLong();
Version handshakeCompat = Version.CURRENT.minimumCompatibilityVersion().minimumCompatibilityVersion();
OutboundMessage message = new OutboundMessage.Request(new TestRequest(randomAlphaOfLength(100)), handshakeCompat, action, requestId, true, true);
final BytesReference bytes = message.serialize(new BytesStreamOutput());
int totalHeaderSize = TcpHeader.headerSize(handshakeCompat);
InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
final ArrayList<Object> fragments = new ArrayList<>();
final ReleasableBytesReference releasable1 = ReleasableBytesReference.wrap(bytes);
int bytesConsumed = decoder.decode(releasable1, fragments::add);
assertEquals(totalHeaderSize, bytesConsumed);
assertEquals(1, releasable1.refCount());
final Header header = (Header) fragments.get(0);
assertEquals(requestId, header.getRequestId());
assertEquals(handshakeCompat, header.getVersion());
assertTrue(header.isCompressed());
assertTrue(header.isHandshake());
assertTrue(header.isRequest());
// TODO: On 9.0 this will be true because all compatible versions with contain the variable header int
assertTrue(header.needsToReadVariableHeader());
fragments.clear();
}
Aggregations