Search in sources :

Example 1 with CompositeBytesReference

use of org.elasticsearch.common.bytes.CompositeBytesReference in project elasticsearch by elastic.

the class TcpTransport method sendErrorResponse.

/**
     * Sends back an error response to the caller via the given channel
     * @param nodeVersion the caller node version
     * @param channel the channel to send the response to
     * @param error the error to return
     * @param requestId the request ID this response replies to
     * @param action the action this response replies to
     */
public void sendErrorResponse(Version nodeVersion, Channel channel, final Exception error, final long requestId, final String action) throws IOException {
    try (BytesStreamOutput stream = new BytesStreamOutput()) {
        stream.setVersion(nodeVersion);
        RemoteTransportException tx = new RemoteTransportException(nodeName(), new TransportAddress(getLocalAddress(channel)), action, error);
        threadPool.getThreadContext().writeTo(stream);
        stream.writeException(tx);
        byte status = 0;
        status = TransportStatus.setResponse(status);
        status = TransportStatus.setError(status);
        final BytesReference bytes = stream.bytes();
        final BytesReference header = buildHeader(requestId, status, nodeVersion, bytes.length());
        Runnable onRequestSent = () -> transportServiceAdapter.onResponseSent(requestId, action, error);
        sendMessage(channel, new CompositeBytesReference(header, bytes), onRequestSent);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference) TransportAddress(org.elasticsearch.common.transport.TransportAddress) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) AbstractLifecycleRunnable(org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 2 with CompositeBytesReference

use of org.elasticsearch.common.bytes.CompositeBytesReference in project elasticsearch by elastic.

the class TcpTransport method buildMessage.

/**
     * Serializes the given message into a bytes representation
     */
private BytesReference buildMessage(long requestId, byte status, Version nodeVersion, TransportMessage message, StreamOutput stream, ReleasableBytesStream writtenBytes) throws IOException {
    final BytesReference zeroCopyBuffer;
    if (message instanceof BytesTransportRequest) {
        // what a shitty optimization - we should use a direct send method instead
        BytesTransportRequest bRequest = (BytesTransportRequest) message;
        assert nodeVersion.equals(bRequest.version());
        bRequest.writeThin(stream);
        zeroCopyBuffer = bRequest.bytes;
    } else {
        message.writeTo(stream);
        zeroCopyBuffer = BytesArray.EMPTY;
    }
    // we have to close the stream here - flush is not enough since we might be compressing the content
    // and if we do that the close method will write some marker bytes (EOS marker) and otherwise
    // we barf on the decompressing end when we read past EOF on purpose in the #validateRequest method.
    // this might be a problem in deflate after all but it's important to close it for now.
    stream.close();
    final BytesReference messageBody = writtenBytes.bytes();
    final BytesReference header = buildHeader(requestId, status, stream.getVersion(), messageBody.length() + zeroCopyBuffer.length());
    return new CompositeBytesReference(header, messageBody, zeroCopyBuffer);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference)

Aggregations

BytesReference (org.elasticsearch.common.bytes.BytesReference)2 CompositeBytesReference (org.elasticsearch.common.bytes.CompositeBytesReference)2 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)1 ReleasableBytesStreamOutput (org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)1 BoundTransportAddress (org.elasticsearch.common.transport.BoundTransportAddress)1 TransportAddress (org.elasticsearch.common.transport.TransportAddress)1 AbstractLifecycleRunnable (org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable)1 AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)1