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