use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class InboundPipelineTests method testDecodeExceptionIsPropagated.
public void testDecodeExceptionIsPropagated() throws IOException {
BiConsumer<TcpChannel, InboundMessage> messageHandler = (c, m) -> {
};
final StatsTracker statsTracker = new StatsTracker();
final LongSupplier millisSupplier = () -> TimeValue.nsecToMSec(System.nanoTime());
final InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
final Supplier<CircuitBreaker> breaker = () -> new NoopCircuitBreaker("test");
final InboundAggregator aggregator = new InboundAggregator(breaker, (Predicate<String>) action -> true);
final InboundPipeline pipeline = new InboundPipeline(statsTracker, millisSupplier, decoder, aggregator, messageHandler);
try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
String actionName = "actionName";
final Version invalidVersion = Version.V_3_2_0;
final String value = randomAlphaOfLength(1000);
final boolean isRequest = randomBoolean();
final long requestId = randomNonNegativeLong();
OutboundMessage message;
if (isRequest) {
message = new OutboundMessage.Request(new TestRequest(value), invalidVersion, actionName, requestId, false, false);
} else {
message = new OutboundMessage.Response(new TestResponse(value), invalidVersion, requestId, false, false);
}
final BytesReference reference = message.serialize(streamOutput);
try (ReleasableBytesReference releasable = ReleasableBytesReference.wrap(reference)) {
expectThrows(IllegalStateException.class, () -> pipeline.handleBytes(new FakeTcpChannel(), releasable));
}
// Pipeline cannot be reused after uncaught exception
final IllegalStateException ise = expectThrows(IllegalStateException.class, () -> pipeline.handleBytes(new FakeTcpChannel(), ReleasableBytesReference.wrap(BytesArray.EMPTY)));
assertEquals("Pipeline state corrupted by uncaught exception", ise.getMessage());
}
}
use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class InboundAggregatorTests method testFinishAggregationWillFinishHeader.
public void testFinishAggregationWillFinishHeader() throws IOException {
long requestId = randomNonNegativeLong();
final String actionName;
final boolean unknownAction = randomBoolean();
if (unknownAction) {
actionName = this.unknownAction;
} else {
actionName = "action_name";
}
Header header = new Header(randomInt(), requestId, TransportStatus.setRequest((byte) 0), Version.CURRENT);
// Initiate Message
aggregator.headerReceived(header);
try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
ThreadContext.bwcWriteHeaders(streamOutput);
streamOutput.writeString(actionName);
streamOutput.write(randomByteArrayOfLength(10));
final ReleasableBytesReference content = ReleasableBytesReference.wrap(streamOutput.bytes());
aggregator.aggregate(content);
content.close();
// Signal EOS
InboundMessage aggregated = aggregator.finishAggregation();
assertThat(aggregated, notNullValue());
assertFalse(header.needsToReadVariableHeader());
assertEquals(actionName, header.getActionName());
if (unknownAction) {
assertEquals(0, content.refCount());
assertTrue(aggregated.isShortCircuit());
} else {
assertEquals(1, content.refCount());
assertFalse(aggregated.isShortCircuit());
}
}
}
use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class InboundAggregatorTests method testCircuitBreak.
public void testCircuitBreak() throws IOException {
circuitBreaker.startBreaking();
// Actions are breakable
Header breakableHeader = new Header(randomInt(), randomNonNegativeLong(), TransportStatus.setRequest((byte) 0), Version.CURRENT);
breakableHeader.bwcNeedsToReadVariableHeader = false;
breakableHeader.actionName = "action_name";
// Initiate Message
aggregator.headerReceived(breakableHeader);
BytesArray bytes = new BytesArray(randomByteArrayOfLength(10));
final ReleasableBytesReference content1 = ReleasableBytesReference.wrap(bytes);
aggregator.aggregate(content1);
content1.close();
// Signal EOS
InboundMessage aggregated1 = aggregator.finishAggregation();
assertEquals(0, content1.refCount());
assertThat(aggregated1, notNullValue());
assertTrue(aggregated1.isShortCircuit());
assertThat(aggregated1.getException(), instanceOf(CircuitBreakingException.class));
// Actions marked as unbreakable are not broken
Header unbreakableHeader = new Header(randomInt(), randomNonNegativeLong(), TransportStatus.setRequest((byte) 0), Version.CURRENT);
unbreakableHeader.bwcNeedsToReadVariableHeader = false;
unbreakableHeader.actionName = unBreakableAction;
// Initiate Message
aggregator.headerReceived(unbreakableHeader);
final ReleasableBytesReference content2 = ReleasableBytesReference.wrap(bytes);
aggregator.aggregate(content2);
content2.close();
// Signal EOS
InboundMessage aggregated2 = aggregator.finishAggregation();
assertEquals(1, content2.refCount());
assertThat(aggregated2, notNullValue());
assertFalse(aggregated2.isShortCircuit());
// Handshakes are not broken
final byte handshakeStatus = TransportStatus.setHandshake(TransportStatus.setRequest((byte) 0));
Header handshakeHeader = new Header(randomInt(), randomNonNegativeLong(), handshakeStatus, Version.CURRENT);
handshakeHeader.bwcNeedsToReadVariableHeader = false;
handshakeHeader.actionName = "handshake";
// Initiate Message
aggregator.headerReceived(handshakeHeader);
final ReleasableBytesReference content3 = ReleasableBytesReference.wrap(bytes);
aggregator.aggregate(content3);
content3.close();
// Signal EOS
InboundMessage aggregated3 = aggregator.finishAggregation();
assertEquals(1, content3.refCount());
assertThat(aggregated3, notNullValue());
assertFalse(aggregated3.isShortCircuit());
}
use of org.elasticsearch.common.bytes.ReleasableBytesReference in project crate by crate.
the class InboundAggregatorTests method testCloseWillCloseContent.
public void testCloseWillCloseContent() {
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, 5));
references.add(content1);
aggregator.aggregate(content1);
content1.close();
final ReleasableBytesReference content2 = ReleasableBytesReference.wrap(bytes.slice(5, 5));
references.add(content2);
aggregator.aggregate(content2);
content2.close();
}
aggregator.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 testErrorResponse.
@Test
public void testErrorResponse() throws IOException {
Version version = randomFrom(Version.CURRENT, Version.CURRENT.minimumCompatibilityVersion());
String action = "handshake";
long requestId = randomLongBetween(0, 300);
ElasticsearchException error = new ElasticsearchException("boom");
AtomicLong requestIdRef = new AtomicLong();
AtomicReference<String> actionRef = new AtomicReference<>();
AtomicReference<Exception> responseRef = new AtomicReference<>();
handler.setMessageListener(new TransportMessageListener() {
@Override
public void onResponseSent(long requestId, String action, Exception error) {
requestIdRef.set(requestId);
actionRef.set(action);
responseRef.set(error);
}
});
handler.sendErrorResponse(version, channel, requestId, action, error);
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(error, responseRef.get());
pipeline.handleBytes(channel, new ReleasableBytesReference(reference, () -> {
}));
final Tuple<Header, BytesReference> tuple = message.get();
final Header header = tuple.v1();
assertEquals(version, header.getVersion());
assertEquals(requestId, header.getRequestId());
assertFalse(header.isRequest());
assertTrue(header.isResponse());
assertFalse(header.isCompressed());
assertFalse(header.isHandshake());
assertTrue(header.isError());
RemoteTransportException remoteException = tuple.v2().streamInput().readException();
assertThat(remoteException.getCause(), instanceOf(ElasticsearchException.class));
assertEquals(remoteException.getCause().getMessage(), "boom");
assertEquals(action, remoteException.action());
assertEquals(channel.getLocalAddress(), remoteException.address().address());
}
Aggregations