Search in sources :

Example 51 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateRequestSerializer method serializeKvStateRequestFailure.

/**
	 * Allocates a buffer and serializes the KvState request failure into it.
	 *
	 * @param alloc ByteBuf allocator for the buffer to serialize message into
	 * @param requestId ID of the request responding to
	 * @param cause Failure cause
	 * @return Serialized KvState request failure message
	 * @throws IOException Serialization failures are forwarded
	 */
public static ByteBuf serializeKvStateRequestFailure(ByteBufAllocator alloc, long requestId, Throwable cause) throws IOException {
    ByteBuf buf = alloc.ioBuffer();
    // Frame length is set at the end
    buf.writeInt(0);
    writeHeader(buf, KvStateRequestType.REQUEST_FAILURE);
    // Message
    buf.writeLong(requestId);
    try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
        ObjectOutputStream out = new ObjectOutputStream(bbos)) {
        out.writeObject(cause);
    }
    // Set frame length
    int frameLength = buf.readableBytes() - 4;
    buf.setInt(0, frameLength);
    return buf;
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf) ObjectOutputStream(java.io.ObjectOutputStream)

Example 52 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateRequestSerializer method serializeKvStateRequest.

// ------------------------------------------------------------------------
// Serialization
// ------------------------------------------------------------------------
/**
	 * Allocates a buffer and serializes the KvState request into it.
	 *
	 * @param alloc                     ByteBuf allocator for the buffer to
	 *                                  serialize message into
	 * @param requestId                 ID for this request
	 * @param kvStateId                 ID of the requested KvState instance
	 * @param serializedKeyAndNamespace Serialized key and namespace to request
	 *                                  from the KvState instance.
	 * @return Serialized KvState request message
	 */
public static ByteBuf serializeKvStateRequest(ByteBufAllocator alloc, long requestId, KvStateID kvStateId, byte[] serializedKeyAndNamespace) {
    // Header + request ID + KvState ID + Serialized namespace
    int frameLength = HEADER_LENGTH + 8 + (8 + 8) + (4 + serializedKeyAndNamespace.length);
    // +4 for frame length
    ByteBuf buf = alloc.ioBuffer(frameLength + 4);
    buf.writeInt(frameLength);
    writeHeader(buf, KvStateRequestType.REQUEST);
    buf.writeLong(requestId);
    buf.writeLong(kvStateId.getLowerPart());
    buf.writeLong(kvStateId.getUpperPart());
    buf.writeInt(serializedKeyAndNamespace.length);
    buf.writeBytes(serializedKeyAndNamespace);
    return buf;
}
Also used : ByteBuf(io.netty.buffer.ByteBuf)

Example 53 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateRequestSerializer method serializeKvStateRequestResult.

/**
	 * Allocates a buffer and serializes the KvState request result into it.
	 *
	 * @param alloc             ByteBuf allocator for the buffer to serialize message into
	 * @param requestId         ID for this request
	 * @param serializedResult  Serialized Result
	 * @return Serialized KvState request result message
	 */
public static ByteBuf serializeKvStateRequestResult(ByteBufAllocator alloc, long requestId, byte[] serializedResult) {
    Preconditions.checkNotNull(serializedResult, "Serialized result");
    // Header + request ID + serialized result
    int frameLength = HEADER_LENGTH + 8 + 4 + serializedResult.length;
    ByteBuf buf = alloc.ioBuffer(frameLength);
    buf.writeInt(frameLength);
    writeHeader(buf, KvStateRequestType.REQUEST_RESULT);
    buf.writeLong(requestId);
    buf.writeInt(serializedResult.length);
    buf.writeBytes(serializedResult);
    return buf;
}
Also used : ByteBuf(io.netty.buffer.ByteBuf)

Example 54 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateRequestSerializer method serializeServerFailure.

/**
	 * Allocates a buffer and serializes the server failure into it.
	 *
	 * <p>The cause must not be or contain any user types as causes.
	 *
	 * @param alloc ByteBuf allocator for the buffer to serialize message into
	 * @param cause Failure cause
	 * @return Serialized server failure message
	 * @throws IOException Serialization failures are forwarded
	 */
public static ByteBuf serializeServerFailure(ByteBufAllocator alloc, Throwable cause) throws IOException {
    ByteBuf buf = alloc.ioBuffer();
    // Frame length is set at end
    buf.writeInt(0);
    writeHeader(buf, KvStateRequestType.SERVER_FAILURE);
    try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
        ObjectOutputStream out = new ObjectOutputStream(bbos)) {
        out.writeObject(cause);
    }
    // Set frame length
    int frameLength = buf.readableBytes() - 4;
    buf.setInt(0, frameLength);
    return buf;
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf) ObjectOutputStream(java.io.ObjectOutputStream)

Example 55 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateClientHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {
        ByteBuf buf = (ByteBuf) msg;
        KvStateRequestType msgType = KvStateRequestSerializer.deserializeHeader(buf);
        if (msgType == KvStateRequestType.REQUEST_RESULT) {
            KvStateRequestResult result = KvStateRequestSerializer.deserializeKvStateRequestResult(buf);
            callback.onRequestResult(result.getRequestId(), result.getSerializedResult());
        } else if (msgType == KvStateRequestType.REQUEST_FAILURE) {
            KvStateRequestFailure failure = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf);
            callback.onRequestFailure(failure.getRequestId(), failure.getCause());
        } else if (msgType == KvStateRequestType.SERVER_FAILURE) {
            throw KvStateRequestSerializer.deserializeServerFailure(buf);
        } else {
            throw new IllegalStateException("Unexpected response type '" + msgType + "'");
        }
    } catch (Throwable t1) {
        try {
            callback.onFailure(t1);
        } catch (Throwable t2) {
            LOG.error("Failed to notify callback about failure", t2);
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}
Also used : KvStateRequestResult(org.apache.flink.runtime.query.netty.message.KvStateRequestResult) KvStateRequestType(org.apache.flink.runtime.query.netty.message.KvStateRequestType) KvStateRequestFailure(org.apache.flink.runtime.query.netty.message.KvStateRequestFailure) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)5080 Test (org.junit.Test)1813 Test (org.junit.jupiter.api.Test)680 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)377 ArrayList (java.util.ArrayList)301 IOException (java.io.IOException)297 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)200 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)182 ByteBuffer (java.nio.ByteBuffer)167 InetSocketAddress (java.net.InetSocketAddress)145 ByteBuf (org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)144 Test (org.testng.annotations.Test)140 Channel (io.netty.channel.Channel)137 List (java.util.List)134 ChannelFuture (io.netty.channel.ChannelFuture)128 Map (java.util.Map)118 MatchEntryBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder)107 Position (org.traccar.model.Position)105 DeviceSession (org.traccar.DeviceSession)100 NetworkMessage (org.traccar.NetworkMessage)93