Search in sources :

Example 1 with DataMessage

use of com.baidu.hugegraph.computer.core.network.message.DataMessage in project hugegraph-computer by hugegraph.

the class NettyServerHandler method processFileRegionBuffer.

private void processFileRegionBuffer(ChannelHandlerContext ctx, Channel channel, DataMessage dataMessage, FileRegionBuffer fileRegionBuffer) {
    // Optimize Value of max bytes of next read
    TransportUtil.setMaxBytesPerRead(channel, fileRegionBuffer.length());
    String outputPath = this.handler.genOutputPath(dataMessage.type(), dataMessage.partition());
    /*
         * Submit zero-copy task to EventLoop, it will be executed next time
         * network data is received.
         */
    ChannelFuture channelFuture = fileRegionBuffer.transformFromChannel((SocketChannel) channel, outputPath);
    channelFuture.addListener((ChannelFutureListener) future -> {
        try {
            if (future.isSuccess()) {
                this.handler.handle(dataMessage.type(), dataMessage.partition(), dataMessage.body());
                this.serverSession.onHandledData(dataMessage.requestId());
            } else {
                this.exceptionCaught(ctx, future.cause());
            }
            TransportUtil.setMaxBytesPerRead(future.channel(), AbstractMessage.HEADER_LENGTH);
            future.channel().unsafe().recvBufAllocHandle().reset(future.channel().config());
            dataMessage.release();
        } catch (Throwable throwable) {
            this.exceptionCaught(ctx, throwable);
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) StartMessage(com.baidu.hugegraph.computer.core.network.message.StartMessage) ServerSession(com.baidu.hugegraph.computer.core.network.session.ServerSession) ConnectionId(com.baidu.hugegraph.computer.core.network.ConnectionId) AckMessage(com.baidu.hugegraph.computer.core.network.message.AckMessage) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) MessageHandler(com.baidu.hugegraph.computer.core.network.MessageHandler) EventLoop(io.netty.channel.EventLoop) FinishMessage(com.baidu.hugegraph.computer.core.network.message.FinishMessage) ChannelFuture(io.netty.channel.ChannelFuture) TimeUnit(java.util.concurrent.TimeUnit) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) TransportUtil(com.baidu.hugegraph.computer.core.network.TransportUtil) DataMessage(com.baidu.hugegraph.computer.core.network.message.DataMessage) ChannelFutureListener(io.netty.channel.ChannelFutureListener) FileRegionBuffer(com.baidu.hugegraph.computer.core.network.buffer.FileRegionBuffer) TransportException(com.baidu.hugegraph.computer.core.common.exception.TransportException) SocketChannel(io.netty.channel.socket.SocketChannel) AbstractMessage(com.baidu.hugegraph.computer.core.network.message.AbstractMessage)

Example 2 with DataMessage

use of com.baidu.hugegraph.computer.core.network.message.DataMessage in project hugegraph-computer by hugegraph.

the class NettyEncodeDecodeHandlerTest method testSendMsgWithEncoderExceptionMock.

@Test
public void testSendMsgWithEncoderExceptionMock() throws IOException {
    NettyTransportClient client = (NettyTransportClient) this.oneClient();
    client.startSession();
    int requestId = 1;
    int partition = 1;
    byte[] bytes = StringEncoding.encode("mock msg");
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    NetworkBuffer body = new NioBuffer(buffer);
    DataMessage dataMessage = new DataMessage(null, requestId, partition, body);
    ChannelFutureListenerOnWrite listener = new ChannelFutureListenerOnWrite(clientHandler);
    ChannelFutureListenerOnWrite spyListener = Mockito.spy(listener);
    client.channel().writeAndFlush(dataMessage).addListener(spyListener);
    Mockito.verify(clientHandler, Mockito.timeout(3000L).times(1)).onChannelActive(Mockito.any());
    Mockito.verify(clientHandler, Mockito.timeout(3000L).times(1)).exceptionCaught(Mockito.any(), Mockito.any());
    Mockito.verify(spyListener, Mockito.timeout(3000L).times(1)).onFailure(Mockito.any(), Mockito.any());
}
Also used : DataMessage(com.baidu.hugegraph.computer.core.network.message.DataMessage) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) ByteBuffer(java.nio.ByteBuffer) NioBuffer(com.baidu.hugegraph.computer.core.network.buffer.NioBuffer) Test(org.junit.Test)

Example 3 with DataMessage

use of com.baidu.hugegraph.computer.core.network.message.DataMessage in project hugegraph-computer by hugegraph.

the class NettyEncodeDecodeHandlerTest method testMessageRelease.

@Test
public void testMessageRelease() {
    int requestId = 99;
    int partition = 1;
    byte[] bytes = StringEncoding.encode("mock msg");
    ByteBuf buf = Unpooled.directBuffer().writeBytes(bytes);
    try {
        NettyBuffer managedBuffer = new NettyBuffer(buf);
        DataMessage dataMessage = new DataMessage(MessageType.MSG, requestId, partition, managedBuffer);
        Assert.assertEquals("DataMessage[messageType=MSG," + "sequenceNumber=99,partition=1,hasBody=true," + "bodyLength=8]", dataMessage.toString());
        Assert.assertEquals(1, managedBuffer.referenceCount());
        dataMessage.release();
        Assert.assertEquals(0, managedBuffer.referenceCount());
    } finally {
        if (buf.refCnt() > 0) {
            buf.release();
        }
    }
}
Also used : DataMessage(com.baidu.hugegraph.computer.core.network.message.DataMessage) NettyBuffer(com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 4 with DataMessage

use of com.baidu.hugegraph.computer.core.network.message.DataMessage 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)

Aggregations

DataMessage (com.baidu.hugegraph.computer.core.network.message.DataMessage)4 NetworkBuffer (com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer)3 NioBuffer (com.baidu.hugegraph.computer.core.network.buffer.NioBuffer)2 Test (org.junit.Test)2 TransportException (com.baidu.hugegraph.computer.core.common.exception.TransportException)1 ConnectionId (com.baidu.hugegraph.computer.core.network.ConnectionId)1 MessageHandler (com.baidu.hugegraph.computer.core.network.MessageHandler)1 TransportUtil (com.baidu.hugegraph.computer.core.network.TransportUtil)1 FileRegionBuffer (com.baidu.hugegraph.computer.core.network.buffer.FileRegionBuffer)1 NettyBuffer (com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer)1 AbstractMessage (com.baidu.hugegraph.computer.core.network.message.AbstractMessage)1 AckMessage (com.baidu.hugegraph.computer.core.network.message.AckMessage)1 FinishMessage (com.baidu.hugegraph.computer.core.network.message.FinishMessage)1 StartMessage (com.baidu.hugegraph.computer.core.network.message.StartMessage)1 ServerSession (com.baidu.hugegraph.computer.core.network.session.ServerSession)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1