Search in sources :

Example 11 with NetworkBuffer

use of com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer in project hugegraph-computer by hugegraph.

the class MessageEncoder method writeMessage.

private void writeMessage(ChannelHandlerContext ctx, Message message, ChannelPromise promise, ByteBufAllocator allocator) throws TransportException {
    ByteBuf bufHeader = null;
    try {
        PromiseCombiner combiner = new PromiseCombiner(ctx.executor());
        bufHeader = allocator.directBuffer(AbstractMessage.HEADER_LENGTH);
        NetworkBuffer bodyBuffer = message.encode(bufHeader);
        ChannelFuture headerWriteFuture = ctx.write(bufHeader);
        /*
             * Released bufHeader after in ctx.write(), set bufHeader = null
             * to not release again
             */
        bufHeader = null;
        combiner.add(headerWriteFuture);
        if (bodyBuffer != null) {
            ByteBuf bodyBuf = bodyBuffer.nettyByteBuf();
            // Will call bodyBuf.release() in ctx.write(), retain() first
            bodyBuffer.retain();
            combiner.add(ctx.write(bodyBuf));
        }
        combiner.finish(promise);
    } catch (Throwable e) {
        throw new TransportException("Failed to encode message, " + "message type: %s", e, message.type());
    } finally {
        if (bufHeader != null) {
            bufHeader.release();
        }
        message.release();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) PromiseCombiner(io.netty.util.concurrent.PromiseCombiner) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) ByteBuf(io.netty.buffer.ByteBuf) TransportException(com.baidu.hugegraph.computer.core.common.exception.TransportException)

Example 12 with NetworkBuffer

use of com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer in project hugegraph-computer by hugegraph.

the class ClientSession method sendAsync.

public synchronized void sendAsync(MessageType messageType, int partition, ByteBuffer buffer) {
    E.checkArgument(this.state == TransportState.ESTABLISHED, "The state must be ESTABLISHED instead of %s " + "at sendAsync()", this.state);
    int requestId = this.nextRequestId();
    NetworkBuffer networkBuffer = new NioBuffer(buffer);
    DataMessage dataMessage = new DataMessage(messageType, requestId, partition, networkBuffer);
    this.sendFunction.apply(dataMessage);
    this.updateFlowBlocking();
}
Also used : DataMessage(com.baidu.hugegraph.computer.core.network.message.DataMessage) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) NioBuffer(com.baidu.hugegraph.computer.core.network.buffer.NioBuffer)

Example 13 with NetworkBuffer

use of com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer in project hugegraph-computer by hugegraph.

the class NettyServerHandler method processDataMessage.

@Override
protected void processDataMessage(ChannelHandlerContext ctx, Channel channel, DataMessage dataMessage) {
    NetworkBuffer body = dataMessage.body();
    try {
        int requestId = dataMessage.requestId();
        this.serverSession.onRecvData(requestId);
        if (body instanceof FileRegionBuffer) {
            this.processFileRegionBuffer(ctx, channel, dataMessage, (FileRegionBuffer) body);
        } else {
            this.handler.handle(dataMessage.type(), dataMessage.partition(), dataMessage.body());
            this.serverSession.onHandledData(requestId);
        }
    } finally {
        body.release();
    }
}
Also used : FileRegionBuffer(com.baidu.hugegraph.computer.core.network.buffer.FileRegionBuffer) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer)

Aggregations

NetworkBuffer (com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer)13 Test (org.junit.Test)7 ByteBuf (io.netty.buffer.ByteBuf)5 NettyBuffer (com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer)4 FrameDecoder (com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder)3 MessageRecvManager (com.baidu.hugegraph.computer.core.receiver.MessageRecvManager)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 ConnectionId (com.baidu.hugegraph.computer.core.network.ConnectionId)2 FileRegionBuffer (com.baidu.hugegraph.computer.core.network.buffer.FileRegionBuffer)2 NioBuffer (com.baidu.hugegraph.computer.core.network.buffer.NioBuffer)2 DataMessage (com.baidu.hugegraph.computer.core.network.message.DataMessage)2 File (java.io.File)2 TransportException (com.baidu.hugegraph.computer.core.common.exception.TransportException)1 FileGraphPartition (com.baidu.hugegraph.computer.core.compute.FileGraphPartition)1 MockMessageSender (com.baidu.hugegraph.computer.core.compute.MockMessageSender)1 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)1 Id (com.baidu.hugegraph.computer.core.graph.id.Id)1 IdList (com.baidu.hugegraph.computer.core.graph.value.IdList)1 IdListList (com.baidu.hugegraph.computer.core.graph.value.IdListList)1 Managers (com.baidu.hugegraph.computer.core.manager.Managers)1