Search in sources :

Example 56 with ChannelFuture

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project alluxio by Alluxio.

the class RPCMessageIntegrationTest method beforeClass.

@BeforeClass
public static void beforeClass() {
    sEventClient = new NioEventLoopGroup(1);
    sEventServer = new NioEventLoopGroup(1);
    sIncomingHandler = new MessageSavingHandler();
    // Setup the server.
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(sEventServer);
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.childHandler(new PipelineInitializer(sIncomingHandler));
    InetSocketAddress address = new InetSocketAddress(NetworkAddressUtils.getLocalHostName(100), Integer.parseInt(PropertyKey.MASTER_RPC_PORT.getDefaultValue()));
    ChannelFuture cf = bootstrap.bind(address).syncUninterruptibly();
    sLocalAddress = cf.channel().localAddress();
    // Setup the client.
    sBootstrapClient = new Bootstrap();
    sBootstrapClient.group(sEventClient);
    sBootstrapClient.channel(NioSocketChannel.class);
    sBootstrapClient.handler(new PipelineInitializer(new MessageSavingHandler()));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) BeforeClass(org.junit.BeforeClass)

Example 57 with ChannelFuture

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project alluxio by Alluxio.

the class RPCMessageIntegrationTest method before.

@Before
public final void before() {
    sIncomingHandler.reset();
    // Connect to the server.
    ChannelFuture cf = sBootstrapClient.connect(sLocalAddress).syncUninterruptibly();
    mOutgoingChannel = cf.channel();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Before(org.junit.Before)

Example 58 with ChannelFuture

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project alluxio by Alluxio.

the class BlockDataServerHandler method handleUnderFileSystemBlockReadRequest.

/**
   * Handles a {@link RPCUnderFileSystemBlockReadRequest} by reading the data through a
   * {@link BlockReader} provided by the block worker. This method assumes the data is available
   * in the UFS returns an error status if the data is not available.
   *
   * @param ctx The context of this request which handles the result of this operation
   * @param req The initiating {@link RPCBlockReadRequest}
   * @throws IOException if an I/O error occurs when reading the data requested
   */
public void handleUnderFileSystemBlockReadRequest(final ChannelHandlerContext ctx, final RPCUnderFileSystemBlockReadRequest req) throws IOException {
    final long blockId = req.getBlockId();
    final long offset = req.getOffset();
    final long len = req.getLength();
    final long sessionId = req.getSessionId();
    final boolean noCache = req.getNoCache();
    try {
        DataBuffer buffer = null;
        req.validate();
        BlockReader reader = mWorker.readUfsBlock(sessionId, blockId, offset, noCache);
        ByteBuffer data = reader.read(offset, len);
        if (data != null && data.remaining() > 0) {
            buffer = new DataByteBuffer(data, data.remaining());
            Metrics.BYTES_READ_UFS.inc(buffer.getLength());
        }
        RPCBlockReadResponse resp = new RPCBlockReadResponse(blockId, offset, data.remaining(), buffer, RPCResponse.Status.SUCCESS);
        ChannelFuture future = ctx.writeAndFlush(resp);
        if (buffer != null) {
            future.addListener(new ReleasableResourceChannelListener(buffer));
        }
        LOG.debug("Preparation for responding to remote block request for: {} done.", blockId);
    } catch (Exception e) {
        LOG.error("Exception reading block {}", blockId, e);
        RPCBlockReadResponse resp;
        if (e instanceof BlockDoesNotExistException) {
            resp = RPCBlockReadResponse.createErrorResponse(req, RPCResponse.Status.FILE_DNE);
        } else {
            resp = RPCBlockReadResponse.createErrorResponse(req, RPCResponse.Status.UFS_READ_FAILED);
        }
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) BlockReader(alluxio.worker.block.io.BlockReader) DataByteBuffer(alluxio.network.protocol.databuffer.DataByteBuffer) RPCBlockReadResponse(alluxio.network.protocol.RPCBlockReadResponse) ByteBuffer(java.nio.ByteBuffer) DataByteBuffer(alluxio.network.protocol.databuffer.DataByteBuffer) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) IOException(java.io.IOException) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 59 with ChannelFuture

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project alluxio by Alluxio.

the class DataServerWriteHandler method replySuccess.

/**
   * Writes a response to signify the success of the block write. Also resets the channel.
   *
   * @param channel the channel
   */
private void replySuccess(Channel channel) {
    channel.writeAndFlush(RPCProtoMessage.createOkResponse(null)).addListeners(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE, new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            reset();
        }
    });
    if (!channel.config().isAutoRead()) {
        channel.config().setAutoRead(true);
        channel.read();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener) IOException(java.io.IOException)

Example 60 with ChannelFuture

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project alluxio by Alluxio.

the class UnderFileSystemDataServerHandler method handleFileWriteRequest.

/**
   * Handles a {@link RPCFileWriteRequest} by writing the data through an output stream provided
   * by the file worker. This method only allows appending data to the file and does not support
   * writing at arbitrary offsets.
   *
   * @param ctx The context of this request which handles the result of this operation
   * @param req The initiating {@link RPCFileWriteRequest}
   * @throws IOException if an I/O error occurs when interacting with the UFS
   */
public void handleFileWriteRequest(ChannelHandlerContext ctx, RPCFileWriteRequest req) throws IOException {
    long ufsFileId = req.getTempUfsFileId();
    // Currently unused as only sequential write is supported
    long offset = req.getOffset();
    long length = req.getLength();
    final DataBuffer data = req.getPayloadDataBuffer();
    try {
        OutputStream out = mWorker.getUfsOutputStream(ufsFileId);
        // This channel will not be closed because the underlying stream should not be closed, the
        // channel will be cleaned up when the underlying stream is closed.
        WritableByteChannel channel = Channels.newChannel(out);
        channel.write(data.getReadOnlyByteBuffer());
        RPCFileWriteResponse resp = new RPCFileWriteResponse(ufsFileId, offset, length, RPCResponse.Status.SUCCESS);
        ctx.writeAndFlush(resp);
    } catch (Exception e) {
        // TODO(peis): Fix this. The exception here should never be caused netty related issue.
        LOG.error("Failed to write ufs file.", e);
        RPCFileWriteResponse resp = RPCFileWriteResponse.createErrorResponse(req, RPCResponse.Status.UFS_WRITE_FAILED);
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) OutputStream(java.io.OutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) RPCFileWriteResponse(alluxio.network.protocol.RPCFileWriteResponse) IOException(java.io.IOException) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Aggregations

ChannelFuture (io.netty.channel.ChannelFuture)936 Channel (io.netty.channel.Channel)271 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)246 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)239 Bootstrap (io.netty.bootstrap.Bootstrap)219 InetSocketAddress (java.net.InetSocketAddress)214 ChannelFutureListener (io.netty.channel.ChannelFutureListener)206 Test (org.junit.Test)195 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)194 ByteBuf (io.netty.buffer.ByteBuf)192 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)178 EventLoopGroup (io.netty.channel.EventLoopGroup)167 IOException (java.io.IOException)155 ChannelPipeline (io.netty.channel.ChannelPipeline)151 SocketChannel (io.netty.channel.socket.SocketChannel)118 ArrayList (java.util.ArrayList)118 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)116 ChannelInitializer (io.netty.channel.ChannelInitializer)115 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)112 AtomicReference (java.util.concurrent.atomic.AtomicReference)111