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);
}
});
}
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());
}
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();
}
}
}
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();
}
Aggregations