Search in sources :

Example 1 with FileRegionBuffer

use of com.baidu.hugegraph.computer.core.network.buffer.FileRegionBuffer 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 FileRegionBuffer

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

the class NettyTransportClientTest method testDataUniformity.

@Test
public void testDataUniformity() throws IOException {
    NettyTransportClient client = (NettyTransportClient) this.oneClient();
    byte[] sourceBytes1 = StringEncoding.encode("test data message");
    byte[] sourceBytes2 = StringEncoding.encode("test data edge");
    byte[] sourceBytes3 = StringEncoding.encode("test data vertex");
    Mockito.doAnswer(invocationOnMock -> {
        MessageType type = invocationOnMock.getArgument(0);
        NetworkBuffer buffer = invocationOnMock.getArgument(2);
        byte[] sourceBytes = null;
        switch(type) {
            case MSG:
                sourceBytes = sourceBytes1;
                break;
            case EDGE:
                sourceBytes = sourceBytes2;
                break;
            case VERTEX:
                sourceBytes = sourceBytes3;
                break;
            default:
        }
        byte[] bytes;
        if (buffer instanceof FileRegionBuffer) {
            String path = ((FileRegionBuffer) buffer).path();
            File file = new File(path);
            bytes = FileUtils.readFileToByteArray(file);
            FileUtils.deleteQuietly(file);
        } else {
            bytes = buffer.copyToByteArray();
        }
        Assert.assertArrayEquals(sourceBytes, bytes);
        Assert.assertNotSame(sourceBytes, bytes);
        return null;
    }).when(serverHandler).handle(Mockito.any(), Mockito.eq(1), Mockito.any());
    client.startSession();
    client.send(MessageType.MSG, 1, ByteBuffer.wrap(sourceBytes1));
    client.send(MessageType.EDGE, 1, ByteBuffer.wrap(sourceBytes2));
    client.send(MessageType.VERTEX, 1, ByteBuffer.wrap(sourceBytes3));
    client.finishSession();
}
Also used : FileRegionBuffer(com.baidu.hugegraph.computer.core.network.buffer.FileRegionBuffer) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) File(java.io.File) MessageType(com.baidu.hugegraph.computer.core.network.message.MessageType) Test(org.junit.Test)

Example 3 with FileRegionBuffer

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

the class MockMessageHandler method handle.

@Override
public void handle(MessageType messageType, int partition, NetworkBuffer buffer) {
    LOG.info("Receive data from remote, messageType: {}, partition: {}, " + "buffer readable length: {}", messageType.name(), partition, buffer != null ? buffer.length() : null);
    if (buffer != null) {
        if (buffer instanceof FileRegionBuffer) {
            String path = ((FileRegionBuffer) buffer).path();
            LOG.info("path: {}", path);
            FileUtils.deleteQuietly(new File(path));
        } else {
            buffer.copyToByteArray();
        }
    }
}
Also used : FileRegionBuffer(com.baidu.hugegraph.computer.core.network.buffer.FileRegionBuffer) File(java.io.File)

Example 4 with FileRegionBuffer

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

the class DataMessage method parseWithFileRegion.

public static DataMessage parseWithFileRegion(MessageType type, ByteBuf buf) {
    int requestId = buf.readInt();
    int partition = buf.readInt();
    int bodyLength = buf.readInt();
    return new DataMessage(type, requestId, partition, new FileRegionBuffer(bodyLength));
}
Also used : FileRegionBuffer(com.baidu.hugegraph.computer.core.network.buffer.FileRegionBuffer)

Example 5 with FileRegionBuffer

use of com.baidu.hugegraph.computer.core.network.buffer.FileRegionBuffer 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

FileRegionBuffer (com.baidu.hugegraph.computer.core.network.buffer.FileRegionBuffer)5 NetworkBuffer (com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer)3 File (java.io.File)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 AbstractMessage (com.baidu.hugegraph.computer.core.network.message.AbstractMessage)1 AckMessage (com.baidu.hugegraph.computer.core.network.message.AckMessage)1 DataMessage (com.baidu.hugegraph.computer.core.network.message.DataMessage)1 FinishMessage (com.baidu.hugegraph.computer.core.network.message.FinishMessage)1 MessageType (com.baidu.hugegraph.computer.core.network.message.MessageType)1 StartMessage (com.baidu.hugegraph.computer.core.network.message.StartMessage)1 ServerSession (com.baidu.hugegraph.computer.core.network.session.ServerSession)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 EventLoop (io.netty.channel.EventLoop)1 SocketChannel (io.netty.channel.socket.SocketChannel)1