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