Search in sources :

Example 11 with CONNECTION

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION in project flink by apache.

the class CreditBasedPartitionRequestClientHandlerTest method testExceptionWrap.

@Test
public void testExceptionWrap() {
    testExceptionWrap(LocalTransportException.class, new Exception());
    testExceptionWrap(LocalTransportException.class, new Exception("some error"));
    testExceptionWrap(RemoteTransportException.class, new IOException("Connection reset by peer"));
    // Only when Epoll is available the following exception could be initiated normally
    // since it relies on the native strerror method.
    Assume.assumeTrue(Epoll.isAvailable());
    testExceptionWrap(RemoteTransportException.class, new Errors.NativeIoException("readAddress", Errors.ERRNO_ECONNRESET_NEGATIVE));
}
Also used : Errors(org.apache.flink.shaded.netty4.io.netty.channel.unix.Errors) IOException(java.io.IOException) TransportException(org.apache.flink.runtime.io.network.netty.exception.TransportException) LocalTransportException(org.apache.flink.runtime.io.network.netty.exception.LocalTransportException) PartitionNotFoundException(org.apache.flink.runtime.io.network.partition.PartitionNotFoundException) RemoteTransportException(org.apache.flink.runtime.io.network.netty.exception.RemoteTransportException) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with CONNECTION

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION in project flink by apache.

the class HandlerUtils method sendResponse.

/**
 * Sends the given response and status code to the given channel.
 *
 * @param channelHandlerContext identifying the open channel
 * @param keepAlive If the connection should be kept alive.
 * @param message which should be sent
 * @param statusCode of the message to send
 * @param headers additional header values
 */
public static CompletableFuture<Void> sendResponse(@Nonnull ChannelHandlerContext channelHandlerContext, boolean keepAlive, @Nonnull String message, @Nonnull HttpResponseStatus statusCode, @Nonnull Map<String, String> headers) {
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, statusCode);
    response.headers().set(CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);
    for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
        response.headers().set(headerEntry.getKey(), headerEntry.getValue());
    }
    if (keepAlive) {
        response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }
    byte[] buf = message.getBytes(ConfigConstants.DEFAULT_CHARSET);
    ByteBuf b = Unpooled.copiedBuffer(buf);
    HttpHeaders.setContentLength(response, buf.length);
    // write the initial line and the header.
    channelHandlerContext.write(response);
    channelHandlerContext.write(b);
    ChannelFuture lastContentFuture = channelHandlerContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    // close the connection, if no keep-alive is needed
    if (!keepAlive) {
        lastContentFuture.addListener(ChannelFutureListener.CLOSE);
    }
    return toCompletableFuture(lastContentFuture);
}
Also used : ChannelFuture(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture) DefaultHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) Map(java.util.Map)

Example 13 with CONNECTION

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION in project flink by apache.

the class HandlerUtils method transferFile.

public static void transferFile(ChannelHandlerContext ctx, File file, HttpRequest httpRequest) throws FlinkException {
    final RandomAccessFile randomAccessFile;
    try {
        randomAccessFile = new RandomAccessFile(file, "r");
    } catch (FileNotFoundException e) {
        throw new FlinkException("Can not find file " + file + ".", e);
    }
    try {
        final long fileLength = randomAccessFile.length();
        final FileChannel fileChannel = randomAccessFile.getChannel();
        try {
            HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
            response.headers().set(CONTENT_TYPE, "text/plain");
            if (HttpHeaders.isKeepAlive(httpRequest)) {
                response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
            }
            HttpHeaders.setContentLength(response, fileLength);
            // write the initial line and the header.
            ctx.write(response);
            // write the content.
            final ChannelFuture lastContentFuture;
            final GenericFutureListener<Future<? super Void>> completionListener = future -> {
                fileChannel.close();
                randomAccessFile.close();
            };
            if (ctx.pipeline().get(SslHandler.class) == null) {
                ctx.write(new DefaultFileRegion(fileChannel, 0, fileLength), ctx.newProgressivePromise()).addListener(completionListener);
                lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
            } else {
                lastContentFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(randomAccessFile, 0, fileLength, 8192)), ctx.newProgressivePromise()).addListener(completionListener);
            // HttpChunkedInput will write the end marker (LastHttpContent) for us.
            }
            // close the connection, if no keep-alive is needed
            if (!HttpHeaders.isKeepAlive(httpRequest)) {
                lastContentFuture.addListener(ChannelFutureListener.CLOSE);
            }
        } catch (IOException ex) {
            fileChannel.close();
            throw ex;
        }
    } catch (IOException ioe) {
        try {
            randomAccessFile.close();
        } catch (IOException e) {
            throw new FlinkException("Close file or channel error.", e);
        }
        throw new FlinkException("Could not transfer file " + file + " to the client.", ioe);
    }
}
Also used : ChannelFuture(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture) RestMapperUtils(org.apache.flink.runtime.rest.util.RestMapperUtils) FlinkException(org.apache.flink.util.FlinkException) RandomAccessFile(java.io.RandomAccessFile) ChannelFutureListener(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener) OK(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus.OK) LastHttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) Future(org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future) HTTP_1_1(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpVersion.HTTP_1_1) DefaultHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse) HttpResponseStatus(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus) DefaultFileRegion(org.apache.flink.shaded.netty4.io.netty.channel.DefaultFileRegion) HttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse) HttpHeaders(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders) ObjectMapper(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper) Map(java.util.Map) ConfigConstants(org.apache.flink.configuration.ConfigConstants) HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) CONNECTION(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION) Nonnull(javax.annotation.Nonnull) ChannelFuture(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture) ErrorResponseBody(org.apache.flink.runtime.rest.messages.ErrorResponseBody) ChannelHandlerContext(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext) CONTENT_TYPE(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE) HttpChunkedInput(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpChunkedInput) Logger(org.slf4j.Logger) RestConstants(org.apache.flink.runtime.rest.util.RestConstants) GenericFutureListener(org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener) StringWriter(java.io.StringWriter) Unpooled(org.apache.flink.shaded.netty4.io.netty.buffer.Unpooled) IOException(java.io.IOException) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) SslHandler(org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) ResponseBody(org.apache.flink.runtime.rest.messages.ResponseBody) ChunkedFile(org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedFile) FileChannel(java.nio.channels.FileChannel) FileChannel(java.nio.channels.FileChannel) ChunkedFile(org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedFile) FileNotFoundException(java.io.FileNotFoundException) DefaultHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse) IOException(java.io.IOException) DefaultFileRegion(org.apache.flink.shaded.netty4.io.netty.channel.DefaultFileRegion) FlinkException(org.apache.flink.util.FlinkException) SslHandler(org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler) HttpChunkedInput(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpChunkedInput) RandomAccessFile(java.io.RandomAccessFile) DefaultHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse) CompletableFuture(java.util.concurrent.CompletableFuture) Future(org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future) ChannelFuture(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture)

Example 14 with CONNECTION

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION in project flink by apache.

the class ClientTransportErrorHandlingTest method testConnectionResetByPeer.

/**
 * Verifies that "Connection reset by peer" Exceptions are special-cased and are reported as an
 * instance of {@link RemoteTransportException}.
 */
@Test
public void testConnectionResetByPeer() throws Throwable {
    EmbeddedChannel ch = createEmbeddedChannel();
    NetworkClientHandler handler = getClientHandler(ch);
    RemoteInputChannel rich = addInputChannel(handler);
    final Throwable[] error = new Throwable[1];
    // Verify the Exception
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Throwable cause = (Throwable) invocation.getArguments()[0];
            try {
                assertEquals(RemoteTransportException.class, cause.getClass());
                assertNotEquals("Connection reset by peer", cause.getMessage());
                assertEquals(IOException.class, cause.getCause().getClass());
                assertEquals("Connection reset by peer", cause.getCause().getMessage());
            } catch (Throwable t) {
                error[0] = t;
            }
            return null;
        }
    }).when(rich).onError(any(Throwable.class));
    ch.pipeline().fireExceptionCaught(new IOException("Connection reset by peer"));
    assertNull(error[0]);
}
Also used : RemoteTransportException(org.apache.flink.runtime.io.network.netty.exception.RemoteTransportException) NetworkClientHandler(org.apache.flink.runtime.io.network.NetworkClientHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) EmbeddedChannel(org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel) IOException(java.io.IOException) RemoteInputChannel(org.apache.flink.runtime.io.network.partition.consumer.RemoteInputChannel) Test(org.junit.Test)

Example 15 with CONNECTION

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION in project flink by apache.

the class ClientTransportErrorHandlingTest method testExceptionOnRemoteClose.

/**
 * Verifies that unexpected remote closes are reported as an instance of {@link
 * RemoteTransportException}.
 */
@Test
public void testExceptionOnRemoteClose() throws Exception {
    NettyProtocol protocol = new NettyProtocol(mock(ResultPartitionProvider.class), mock(TaskEventDispatcher.class)) {

        @Override
        public ChannelHandler[] getServerChannelHandlers() {
            return new ChannelHandler[] { // Close on read
            new ChannelInboundHandlerAdapter() {

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                    ctx.channel().close();
                }
            } };
        }
    };
    NettyServerAndClient serverAndClient = initServerAndClient(protocol, createConfig());
    Channel ch = connect(serverAndClient);
    NetworkClientHandler handler = getClientHandler(ch);
    // Create input channels
    RemoteInputChannel[] rich = new RemoteInputChannel[] { createRemoteInputChannel(), createRemoteInputChannel() };
    final CountDownLatch sync = new CountDownLatch(rich.length);
    Answer<Void> countDownLatch = new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            sync.countDown();
            return null;
        }
    };
    for (RemoteInputChannel r : rich) {
        doAnswer(countDownLatch).when(r).onError(any(Throwable.class));
        handler.addInputChannel(r);
    }
    // Write something to trigger close by server
    ch.writeAndFlush(Unpooled.buffer().writerIndex(16));
    // Wait for the notification
    if (!sync.await(TestingUtils.TESTING_DURATION.toMillis(), TimeUnit.MILLISECONDS)) {
        fail("Timed out after waiting for " + TestingUtils.TESTING_DURATION.toMillis() + " ms to be notified about remote connection close.");
    }
    // All the registered channels should be notified.
    for (RemoteInputChannel r : rich) {
        verify(r).onError(isA(RemoteTransportException.class));
    }
    shutdown(serverAndClient);
}
Also used : RemoteTransportException(org.apache.flink.runtime.io.network.netty.exception.RemoteTransportException) NetworkClientHandler(org.apache.flink.runtime.io.network.NetworkClientHandler) RemoteInputChannel(org.apache.flink.runtime.io.network.partition.consumer.RemoteInputChannel) EmbeddedChannel(org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) ChannelHandlerContext(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext) ChannelHandler(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler) CountDownLatch(java.util.concurrent.CountDownLatch) RemoteInputChannel(org.apache.flink.runtime.io.network.partition.consumer.RemoteInputChannel) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResultPartitionProvider(org.apache.flink.runtime.io.network.partition.ResultPartitionProvider) TaskEventDispatcher(org.apache.flink.runtime.io.network.TaskEventDispatcher) NettyServerAndClient(org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient) ChannelInboundHandlerAdapter(org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)8 Test (org.junit.Test)7 ExecutionException (java.util.concurrent.ExecutionException)4 ByteBuf (org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)4 Channel (org.apache.flink.shaded.netty4.io.netty.channel.Channel)4 ChannelFuture (org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture)4 SslHandler (org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler)4 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 RandomAccessFile (java.io.RandomAccessFile)3 TimeoutException (java.util.concurrent.TimeoutException)3 RemoteTransportException (org.apache.flink.runtime.io.network.netty.exception.RemoteTransportException)3 SocketChannel (org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel)3 DefaultHttpResponse (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse)3 HttpResponse (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse)3 InputStream (java.io.InputStream)2 ConnectException (java.net.ConnectException)2 InetSocketAddress (java.net.InetSocketAddress)2 ServerSocket (java.net.ServerSocket)2 Socket (java.net.Socket)2