Search in sources :

Example 41 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel 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);
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RPCUnderFileSystemBlockReadRequest(alluxio.network.protocol.RPCUnderFileSystemBlockReadRequest) Channel(io.netty.channel.Channel) RPCResponse(alluxio.network.protocol.RPCResponse) IOException(java.io.IOException) IOException(java.io.IOException) RPCErrorResponse(alluxio.network.protocol.RPCErrorResponse) RPCBlockReadResponse(alluxio.network.protocol.RPCBlockReadResponse)

Example 42 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel 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);
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCFileWriteRequest(alluxio.network.protocol.RPCFileWriteRequest) RPCResponse(alluxio.network.protocol.RPCResponse) IOException(java.io.IOException) IOException(java.io.IOException) DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCErrorResponse(alluxio.network.protocol.RPCErrorResponse) RPCFileWriteResponse(alluxio.network.protocol.RPCFileWriteResponse)

Example 43 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project riposte by Nike-Inc.

the class ChannelAttributesTest method getHttpProcessingStateForChannel_works_as_expected.

@Test
public void getHttpProcessingStateForChannel_works_as_expected() {
    // given
    ChannelHandlerContext ctxMock = mock(ChannelHandlerContext.class);
    Channel channelMock = mock(Channel.class);
    @SuppressWarnings("unchecked") Attribute<HttpProcessingState> magicAttrMock = mock(Attribute.class);
    // and
    doReturn(channelMock).when(ctxMock).channel();
    doReturn(magicAttrMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY);
    // expect
    assertThat(ChannelAttributes.getHttpProcessingStateForChannel(ctxMock), is(magicAttrMock));
}
Also used : Channel(io.netty.channel.Channel) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Test(org.junit.Test)

Example 44 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project riposte by Nike-Inc.

the class ConsumerWithTracingAndMdcSupportTest method beforeMethod.

@Before
public void beforeMethod() {
    channelMock = mock(Channel.class);
    ctxMock = mock(ChannelHandlerContext.class);
    stateAttributeMock = mock(Attribute.class);
    state = new HttpProcessingState();
    doReturn(channelMock).when(ctxMock).channel();
    doReturn(stateAttributeMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY);
    doReturn(state).when(stateAttributeMock).get();
    consumerMock = mock(Consumer.class);
    inObj = new Object();
    throwExceptionDuringCall = false;
    currentSpanStackWhenConsumerWasCalled = new ArrayList<>();
    currentMdcInfoWhenConsumerWasCalled = new ArrayList<>();
    doAnswer(invocation -> {
        currentSpanStackWhenConsumerWasCalled.add(Tracer.getInstance().getCurrentSpanStackCopy());
        currentMdcInfoWhenConsumerWasCalled.add(MDC.getCopyOfContextMap());
        if (throwExceptionDuringCall)
            throw new RuntimeException("kaboom");
        return null;
    }).when(consumerMock).accept(inObj);
    resetTracingAndMdc();
}
Also used : Consumer(java.util.function.Consumer) Attribute(io.netty.util.Attribute) Channel(io.netty.channel.Channel) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Before(org.junit.Before)

Example 45 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project riposte by Nike-Inc.

the class BiFunctionWithTracingAndMdcSupportTest method beforeMethod.

@Before
public void beforeMethod() {
    channelMock = mock(Channel.class);
    ctxMock = mock(ChannelHandlerContext.class);
    stateAttributeMock = mock(Attribute.class);
    state = new HttpProcessingState();
    doReturn(channelMock).when(ctxMock).channel();
    doReturn(stateAttributeMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY);
    doReturn(state).when(stateAttributeMock).get();
    biFunctionMock = mock(BiFunction.class);
    inObj1 = new Object();
    inObj2 = new Object();
    outObj = new Object();
    throwExceptionDuringCall = false;
    currentSpanStackWhenFunctionWasCalled = new ArrayList<>();
    currentMdcInfoWhenFunctionWasCalled = new ArrayList<>();
    doAnswer(invocation -> {
        currentSpanStackWhenFunctionWasCalled.add(Tracer.getInstance().getCurrentSpanStackCopy());
        currentMdcInfoWhenFunctionWasCalled.add(MDC.getCopyOfContextMap());
        if (throwExceptionDuringCall)
            throw new RuntimeException("kaboom");
        return outObj;
    }).when(biFunctionMock).apply(inObj1, inObj2);
    resetTracingAndMdc();
}
Also used : Attribute(io.netty.util.Attribute) BiFunction(java.util.function.BiFunction) Channel(io.netty.channel.Channel) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Before(org.junit.Before)

Aggregations

Channel (io.netty.channel.Channel)796 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)212 Test (org.junit.Test)195 Bootstrap (io.netty.bootstrap.Bootstrap)192 ChannelFuture (io.netty.channel.ChannelFuture)189 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)175 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)172 InetSocketAddress (java.net.InetSocketAddress)159 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)144 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)136 EventLoopGroup (io.netty.channel.EventLoopGroup)134 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)130 IOException (java.io.IOException)124 ByteBuf (io.netty.buffer.ByteBuf)108 CountDownLatch (java.util.concurrent.CountDownLatch)97 ChannelPipeline (io.netty.channel.ChannelPipeline)93 LocalChannel (io.netty.channel.local.LocalChannel)93 SocketChannel (io.netty.channel.socket.SocketChannel)91 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)90 LocalServerChannel (io.netty.channel.local.LocalServerChannel)89