use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project cradle by BingLau7.
the class EchoClient method start.
void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
// 指定 EventLoopGroup 以处理客户端时间;需要适用于 NIO 的实现
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).remoteAddress(new InetSocketAddress(host, port)).handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});
// 连接远程节点,阻塞等待直到连接完成
ChannelFuture f = b.connect().sync();
// 阻塞,直到 Channel 关闭
f.channel().closeFuture().sync();
} finally {
// 关闭线程池并释放所有的资源
group.shutdownGracefully().sync();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project cradle by BingLau7.
the class EchoServer method start.
void start() throws Exception {
// 创建 Event-LoopGroup
NioEventLoopGroup group = new NioEventLoopGroup();
try {
// 创建 Server-Bootstrap
ServerBootstrap b = new ServerBootstrap();
b.group(group).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new EchoServerHandler());
}
});
// 异步地绑定服务器;调用 sync() 方法阻塞等待直到绑定完成
ChannelFuture f = b.bind().sync();
System.out.println(EchoServer.class.getName() + " started and listen on " + f.channel().localAddress());
// 获取 Channel 的 CloseFuture,并且阻塞当前线程直到它完成
f.channel().closeFuture().sync();
} finally {
// 关闭 EventLoopGroup,释放所有的资源
group.shutdownGracefully().sync();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project alluxio by Alluxio.
the class NettyRemoteBlockWriter method write.
@Override
public void write(byte[] bytes, int offset, int length) throws IOException {
Channel channel = null;
ClientHandler clientHandler = null;
Metrics.NETTY_BLOCK_WRITE_OPS.inc();
try {
channel = mContext.acquireNettyChannel(mAddress);
if (!(channel.pipeline().last() instanceof ClientHandler)) {
channel.pipeline().addLast(new ClientHandler());
}
clientHandler = (ClientHandler) channel.pipeline().last();
SingleResponseListener listener = new SingleResponseListener();
clientHandler.addListener(listener);
ChannelFuture channelFuture = channel.writeAndFlush(new RPCBlockWriteRequest(mSessionId, mBlockId, mWrittenBytes, length, new DataByteArrayChannel(bytes, offset, length))).sync();
if (channelFuture.isDone() && !channelFuture.isSuccess()) {
LOG.error("Failed to write to %s for block %d with error %s.", mAddress.toString(), mBlockId, channelFuture.cause());
throw new IOException(channelFuture.cause());
}
RPCResponse response = listener.get(NettyClient.TIMEOUT_MS, TimeUnit.MILLISECONDS);
switch(response.getType()) {
case RPC_BLOCK_WRITE_RESPONSE:
RPCBlockWriteResponse resp = (RPCBlockWriteResponse) response;
RPCResponse.Status status = resp.getStatus();
LOG.debug("status: {} from remote machine {} received", status, mAddress);
if (status != RPCResponse.Status.SUCCESS) {
throw new IOException(ExceptionMessage.BLOCK_WRITE_ERROR.getMessage(mBlockId, mSessionId, mAddress, status.getMessage()));
}
mWrittenBytes += length;
break;
case RPC_ERROR_RESPONSE:
RPCErrorResponse error = (RPCErrorResponse) response;
throw new IOException(error.getStatus().getMessage());
default:
throw new IOException(ExceptionMessage.UNEXPECTED_RPC_RESPONSE.getMessage(response.getType(), RPCMessage.Type.RPC_BLOCK_WRITE_RESPONSE));
}
} catch (Exception e) {
Metrics.NETTY_BLOCK_WRITE_FAILURES.inc();
try {
// TODO(peis): We should not close the channel unless it is an exception caused by network.
if (channel != null) {
channel.close().sync();
}
} catch (InterruptedException ee) {
Throwables.propagate(ee);
}
throw new IOException(e);
} finally {
if (clientHandler != null) {
clientHandler.removeListeners();
}
if (channel != null) {
mContext.releaseNettyChannel(mAddress, channel);
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project alluxio by Alluxio.
the class NettyUnderFileSystemBlockReader method read.
@Override
public ByteBuffer read(InetSocketAddress address, long blockId, long offset, long length, long sessionId, boolean noCache) throws IOException {
Channel channel = null;
ClientHandler clientHandler = null;
Metrics.NETTY_UFS_BLOCK_READ_OPS.inc();
try {
channel = mContext.acquireNettyChannel(address);
if (!(channel.pipeline().last() instanceof ClientHandler)) {
channel.pipeline().addLast(new ClientHandler());
}
clientHandler = (ClientHandler) channel.pipeline().last();
SingleResponseListener listener = new SingleResponseListener();
clientHandler.addListener(listener);
ChannelFuture channelFuture = channel.writeAndFlush(new RPCUnderFileSystemBlockReadRequest(blockId, offset, length, sessionId, noCache));
channelFuture = channelFuture.sync();
if (channelFuture.isDone() && !channelFuture.isSuccess()) {
LOG.error("Failed to read from %s for block %d with error %s.", address.toString(), blockId, channelFuture.cause());
throw new IOException(channelFuture.cause());
}
RPCResponse response = listener.get(NettyClient.TIMEOUT_MS, TimeUnit.MILLISECONDS);
switch(response.getType()) {
case RPC_BLOCK_READ_RESPONSE:
RPCBlockReadResponse blockResponse = (RPCBlockReadResponse) response;
LOG.debug("Data {} from machine {} received", blockId, address);
RPCResponse.Status status = blockResponse.getStatus();
if (status == RPCResponse.Status.SUCCESS) {
// always clear the previous response before reading another one
close();
mReadResponse = blockResponse;
return blockResponse.getPayloadDataBuffer().getReadOnlyByteBuffer();
}
throw new IOException(status.getMessage() + " response: " + blockResponse);
case RPC_ERROR_RESPONSE:
RPCErrorResponse error = (RPCErrorResponse) response;
throw new IOException(error.getStatus().getMessage());
default:
throw new IOException(ExceptionMessage.UNEXPECTED_RPC_RESPONSE.getMessage(response.getType(), RPCMessage.Type.RPC_BLOCK_READ_RESPONSE));
}
} catch (Exception e) {
Metrics.NETTY_UFS_BLOCK_READ_FAILURES.inc();
try {
if (channel != null) {
channel.close().sync();
}
} catch (InterruptedException ee) {
throw new RuntimeException(ee);
}
throw new IOException(e);
} finally {
if (clientHandler != null) {
clientHandler.removeListeners();
}
if (channel != null) {
mContext.releaseNettyChannel(address, channel);
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project alluxio by Alluxio.
the class NettyUnderFileSystemFileWriter method write.
@Override
public void write(InetSocketAddress address, long ufsFileId, long fileOffset, byte[] source, int offset, int length) throws IOException {
Channel channel = null;
ClientHandler clientHandler = null;
Metrics.NETTY_UFS_WRITE_OPS.inc();
try {
channel = mContext.acquireNettyChannel(address);
if (!(channel.pipeline().last() instanceof ClientHandler)) {
channel.pipeline().addLast(new ClientHandler());
}
clientHandler = (ClientHandler) channel.pipeline().last();
SingleResponseListener listener = new SingleResponseListener();
clientHandler.addListener(listener);
ChannelFuture channelFuture = channel.writeAndFlush(new RPCFileWriteRequest(ufsFileId, fileOffset, length, new DataByteArrayChannel(source, offset, length))).sync();
if (channelFuture.isDone() && !channelFuture.isSuccess()) {
LOG.error("Failed to read ufs file from %s for ufsFilId %d with error %s.", address.toString(), ufsFileId, channelFuture.cause());
throw new IOException(channelFuture.cause());
}
RPCResponse response = listener.get(NettyClient.TIMEOUT_MS, TimeUnit.MILLISECONDS);
switch(response.getType()) {
case RPC_FILE_WRITE_RESPONSE:
RPCFileWriteResponse resp = (RPCFileWriteResponse) response;
RPCResponse.Status status = resp.getStatus();
LOG.debug("status: {} from remote machine {} received", status, address);
if (status != RPCResponse.Status.SUCCESS) {
throw new IOException(ExceptionMessage.UNDER_FILE_WRITE_ERROR.getMessage(ufsFileId, address, status.getMessage()));
}
break;
case RPC_ERROR_RESPONSE:
RPCErrorResponse error = (RPCErrorResponse) response;
throw new IOException(error.getStatus().getMessage());
default:
throw new IOException(ExceptionMessage.UNEXPECTED_RPC_RESPONSE.getMessage(response.getType(), RPCMessage.Type.RPC_FILE_WRITE_RESPONSE));
}
} catch (Exception e) {
Metrics.NETTY_UFS_WRITE_FAILURES.inc();
try {
if (channel != null) {
channel.close().sync();
}
} catch (InterruptedException ee) {
Throwables.propagate(ee);
}
throw new IOException(e);
} finally {
if (clientHandler != null) {
clientHandler.removeListeners();
}
if (channel != null) {
mContext.releaseNettyChannel(address, channel);
}
}
}
Aggregations