Search in sources :

Example 6 with NetworkBuffer

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

the class NettyEncodeDecodeHandlerTest method testSendMsgWithFrameDecode.

@Test
public void testSendMsgWithFrameDecode() {
    FrameDecoder frameDecoder = new FrameDecoder();
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(frameDecoder);
    NetworkBuffer buffer = new NettyBuffer(Unpooled.buffer());
    ByteBuf buf = buffer.nettyByteBuf();
    StartMessage.INSTANCE.encode(buf);
    boolean writeInbound = embeddedChannel.writeInbound(buf);
    Assert.assertTrue(writeInbound);
    Assert.assertTrue(embeddedChannel.finish());
    buffer.release();
}
Also used : FrameDecoder(com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder) NettyBuffer(com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 7 with NetworkBuffer

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

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

the class NettyEncodeDecodeHandlerTest method testSendMsgWithFrameDecodeMagicError.

@Test
public void testSendMsgWithFrameDecodeMagicError() {
    FrameDecoder frameDecoder = new FrameDecoder();
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(frameDecoder);
    NetworkBuffer buffer = new NettyBuffer(Unpooled.buffer());
    short magicError = 10;
    ByteBuf buf = buffer.nettyByteBuf();
    StartMessage.INSTANCE.encode(buf);
    buf.setShort(0, magicError);
    embeddedChannel.writeInbound(buf);
    Assert.assertFalse(embeddedChannel.finish());
    Assert.assertNull(embeddedChannel.readInbound());
}
Also used : FrameDecoder(com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder) NettyBuffer(com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 9 with NetworkBuffer

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

the class NettyEncodeDecodeHandlerTest method testSendMsgWithFrameDecodeVersionError.

@Test
public void testSendMsgWithFrameDecodeVersionError() {
    FrameDecoder frameDecoder = new FrameDecoder();
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(frameDecoder);
    NetworkBuffer buffer = new NettyBuffer(Unpooled.buffer());
    byte versionError = 10;
    ByteBuf buf = buffer.nettyByteBuf();
    StartMessage.INSTANCE.encode(buf);
    buf.setByte(2, versionError);
    embeddedChannel.writeInbound(buf);
    Assert.assertFalse(embeddedChannel.finish());
    Assert.assertNull(embeddedChannel.readInbound());
}
Also used : FrameDecoder(com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder) NettyBuffer(com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 10 with NetworkBuffer

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

the class DataMessage method parseWithMemoryBuffer.

/**
 * Decoding uses the given ByteBuf as our data.
 */
public static DataMessage parseWithMemoryBuffer(MessageType type, ByteBuf buf) {
    int requestId = buf.readInt();
    int partition = buf.readInt();
    int bodyLength = buf.readInt();
    // Slice body and retain it, the readIndex of buf will auto to body end
    ByteBuf bodySlice = buf.readRetainedSlice(bodyLength);
    NetworkBuffer networkBuffer = new NettyBuffer(bodySlice);
    return new DataMessage(type, requestId, partition, networkBuffer);
}
Also used : NettyBuffer(com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) ByteBuf(io.netty.buffer.ByteBuf)

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