org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler Java Examples

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClientTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private Channel createServerChannel(final ChannelHandler... handlers) throws UnknownHostException, InterruptedException {
	ServerBootstrap bootstrap = new ServerBootstrap()
			// Bind address and port
			.localAddress(InetAddress.getLocalHost(), 0)
			// NIO server channels
			.group(nioGroup)
			.channel(NioServerSocketChannel.class)
			// See initializer for pipeline details
			.childHandler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline()
							.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4))
							.addLast(handlers);
				}
			});

	return bootstrap.bind().sync().channel();
}
 
Example #2
Source File: RouterHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void routed(
		ChannelHandlerContext channelHandlerContext,
		RouteResult<?> routeResult,
		HttpRequest httpRequest) {
	ChannelInboundHandler handler = (ChannelInboundHandler) routeResult.target();

	// The handler may have been added (keep alive)
	ChannelPipeline pipeline     = channelHandlerContext.pipeline();
	ChannelHandler addedHandler = pipeline.get(ROUTED_HANDLER_NAME);
	if (handler != addedHandler) {
		if (addedHandler == null) {
			pipeline.addAfter(ROUTER_HANDLER_NAME, ROUTED_HANDLER_NAME, handler);
		} else {
			pipeline.replace(addedHandler, ROUTED_HANDLER_NAME, handler);
		}
	}

	RoutedRequest<?> request = new RoutedRequest<>(routeResult, httpRequest);
	channelHandlerContext.fireChannelRead(request.retain());
}
 
Example #3
Source File: ClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private Channel createServerChannel(final ChannelHandler... handlers) throws UnknownHostException, InterruptedException {
	ServerBootstrap bootstrap = new ServerBootstrap()
			// Bind address and port
			.localAddress(InetAddress.getLocalHost(), 0)
			// NIO server channels
			.group(nioGroup)
			.channel(NioServerSocketChannel.class)
			// See initializer for pipeline details
			.childHandler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline()
							.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4))
							.addLast(handlers);
				}
			});

	return bootstrap.bind().sync().channel();
}
 
Example #4
Source File: RouterHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
private void routed(
		ChannelHandlerContext channelHandlerContext,
		RouteResult<?> routeResult,
		HttpRequest httpRequest) {
	ChannelInboundHandler handler = (ChannelInboundHandler) routeResult.target();

	// The handler may have been added (keep alive)
	ChannelPipeline pipeline     = channelHandlerContext.pipeline();
	ChannelHandler addedHandler = pipeline.get(ROUTED_HANDLER_NAME);
	if (handler != addedHandler) {
		if (addedHandler == null) {
			pipeline.addAfter(ROUTER_HANDLER_NAME, ROUTED_HANDLER_NAME, handler);
		} else {
			pipeline.replace(addedHandler, ROUTED_HANDLER_NAME, handler);
		}
	}

	RoutedRequest<?> request = new RoutedRequest<>(routeResult, httpRequest);
	channelHandlerContext.fireChannelRead(request.retain());
}
 
Example #5
Source File: RouterHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
private void routed(
		ChannelHandlerContext channelHandlerContext,
		RouteResult<?> routeResult,
		HttpRequest httpRequest) {
	ChannelInboundHandler handler = (ChannelInboundHandler) routeResult.target();

	// The handler may have been added (keep alive)
	ChannelPipeline pipeline     = channelHandlerContext.pipeline();
	ChannelHandler addedHandler = pipeline.get(ROUTED_HANDLER_NAME);
	if (handler != addedHandler) {
		if (addedHandler == null) {
			pipeline.addAfter(ROUTER_HANDLER_NAME, ROUTED_HANDLER_NAME, handler);
		} else {
			pipeline.replace(addedHandler, ROUTED_HANDLER_NAME, handler);
		}
	}

	RoutedRequest<?> request = new RoutedRequest<>(routeResult, httpRequest);
	channelHandlerContext.fireChannelRead(request.retain());
}
 
Example #6
Source File: ClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private Channel createServerChannel(final ChannelHandler... handlers) throws UnknownHostException, InterruptedException {
	ServerBootstrap bootstrap = new ServerBootstrap()
			// Bind address and port
			.localAddress(InetAddress.getLocalHost(), 0)
			// NIO server channels
			.group(nioGroup)
			.channel(NioServerSocketChannel.class)
			// See initializer for pipeline details
			.childHandler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline()
							.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4))
							.addLast(handlers);
				}
			});

	return bootstrap.bind().sync().channel();
}
 
Example #7
Source File: KvStateServerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a client bootstrap.
 */
private Bootstrap createBootstrap(final ChannelHandler... handlers) {
	return new Bootstrap().group(NIO_GROUP).channel(NioSocketChannel.class)
			.handler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(handlers);
				}
			});
}
 
Example #8
Source File: KvStateServerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a client bootstrap.
 */
private Bootstrap createBootstrap(final ChannelHandler... handlers) {
	return new Bootstrap().group(NIO_GROUP).channel(NioSocketChannel.class)
			.handler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(handlers);
				}
			});
}
 
Example #9
Source File: KvStateServerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a client bootstrap.
 */
private Bootstrap createBootstrap(final ChannelHandler... handlers) {
	return new Bootstrap().group(NIO_GROUP).channel(NioSocketChannel.class)
			.handler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(handlers);
				}
			});
}
 
Example #10
Source File: NettyClientServerSslTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelHandler[] getClientChannelHandlers() {
	return new ChannelHandler[0];
}
 
Example #11
Source File: NettyClientServerSslTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelHandler[] getServerChannelHandlers() {
	return new ChannelHandler[0];
}
 
Example #12
Source File: PartitionRequestClientFactoryTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testResourceReleaseAfterInterruptedConnect() throws Exception {

	// Latch to synchronize on the connect call.
	final CountDownLatch syncOnConnect = new CountDownLatch(1);

	final Tuple2<NettyServer, NettyClient> netty = createNettyServerAndClient(
			new NettyProtocol(null, null) {

				@Override
				public ChannelHandler[] getServerChannelHandlers() {
					return new ChannelHandler[0];
				}

				@Override
				public ChannelHandler[] getClientChannelHandlers() {
					return new ChannelHandler[] {
							new CountDownLatchOnConnectHandler(syncOnConnect)};
				}
			});

	final NettyServer server = netty.f0;
	final NettyClient client = netty.f1;

	final UncaughtTestExceptionHandler exceptionHandler = new UncaughtTestExceptionHandler();

	try {
		final PartitionRequestClientFactory factory = new PartitionRequestClientFactory(client);

		final Thread connect = new Thread(new Runnable() {
			@Override
			public void run() {
				ConnectionID serverAddress = null;

				try {
					serverAddress = createServerConnectionID(0);

					// This triggers a connect
					factory.createPartitionRequestClient(serverAddress);
				}
				catch (Throwable t) {

					if (serverAddress != null) {
						factory.closeOpenChannelConnections(serverAddress);
						Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), t);
					} else {
						t.printStackTrace();
						fail("Could not create RemoteAddress for server.");
					}
				}
			}
		});

		connect.setUncaughtExceptionHandler(exceptionHandler);

		connect.start();

		// Wait on the connect
		syncOnConnect.await();

		connect.interrupt();
		connect.join();

		// Make sure that after a failed connect all resources are cleared.
		assertEquals(0, factory.getNumberOfActiveClients());

		// Make sure that the interrupt exception is not swallowed
		assertTrue(exceptionHandler.getErrors().size() > 0);
	}
	finally {
		if (server != null) {
			server.shutdown();
		}

		if (client != null) {
			client.shutdown();
		}
	}
}
 
Example #13
Source File: ClientTransportErrorHandlingTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies that failed client requests via {@link PartitionRequestClient} are correctly
 * attributed to the respective {@link RemoteInputChannel}.
 */
@Test
public void testExceptionOnWrite() throws Exception {

	NettyProtocol protocol = new NettyProtocol(
			mock(ResultPartitionProvider.class),
			mock(TaskEventDispatcher.class)) {

		@Override
		public ChannelHandler[] getServerChannelHandlers() {
			return new ChannelHandler[0];
		}
	};

	// We need a real server and client in this test, because Netty's EmbeddedChannel is
	// not failing the ChannelPromise of failed writes.
	NettyServerAndClient serverAndClient = initServerAndClient(protocol, createConfig());

	Channel ch = connect(serverAndClient);

	NetworkClientHandler handler = getClientHandler(ch);

	// Last outbound handler throws Exception after 1st write
	ch.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
		int writeNum = 0;

		@Override
		public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
				throws Exception {

			if (writeNum >= 1) {
				throw new RuntimeException("Expected test exception.");
			}

			writeNum++;
			ctx.write(msg, promise);
		}
	});

	PartitionRequestClient requestClient = new NettyPartitionRequestClient(
			ch, handler, mock(ConnectionID.class), mock(PartitionRequestClientFactory.class));

	// Create input channels
	RemoteInputChannel[] rich = new RemoteInputChannel[] {
			createRemoteInputChannel(), createRemoteInputChannel()};

	final CountDownLatch sync = new CountDownLatch(1);

	// Do this with explicit synchronization. Otherwise this is not robust against slow timings
	// of the callback (e.g. we cannot just verify that it was called once, because there is
	// a chance that we do this too early).
	doAnswer(new Answer<Void>() {
		@Override
		public Void answer(InvocationOnMock invocation) throws Throwable {
			sync.countDown();
			return null;
		}
	}).when(rich[1]).onError(isA(LocalTransportException.class));

	// First request is successful
	requestClient.requestSubpartition(new ResultPartitionID(), 0, rich[0], 0);

	// Second request is *not* successful
	requestClient.requestSubpartition(new ResultPartitionID(), 0, rich[1], 0);

	// Wait for the notification and it could confirm all the request operations are done
	if (!sync.await(TestingUtils.TESTING_DURATION().toMillis(), TimeUnit.MILLISECONDS)) {
		fail("Timed out after waiting for " + TestingUtils.TESTING_DURATION().toMillis() +
				" ms to be notified about the channel error.");
	}

	// Only the second channel should be notified about the error
	verify(rich[0], times(0)).onError(any(LocalTransportException.class));

	shutdown(serverAndClient);
}
 
Example #14
Source File: AbstractTaskManagerFileHandlerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelHandler handler() {
	return null;
}
 
Example #15
Source File: PartitionRequestClientFactoryTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testResourceReleaseAfterInterruptedConnect() throws Exception {

	// Latch to synchronize on the connect call.
	final CountDownLatch syncOnConnect = new CountDownLatch(1);

	final Tuple2<NettyServer, NettyClient> netty = createNettyServerAndClient(
			new NettyProtocol(null, null, true) {

				@Override
				public ChannelHandler[] getServerChannelHandlers() {
					return new ChannelHandler[0];
				}

				@Override
				public ChannelHandler[] getClientChannelHandlers() {
					return new ChannelHandler[] {
							new CountDownLatchOnConnectHandler(syncOnConnect)};
				}
			});

	final NettyServer server = netty.f0;
	final NettyClient client = netty.f1;

	final UncaughtTestExceptionHandler exceptionHandler = new UncaughtTestExceptionHandler();

	try {
		final PartitionRequestClientFactory factory = new PartitionRequestClientFactory(client);

		final Thread connect = new Thread(new Runnable() {
			@Override
			public void run() {
				ConnectionID serverAddress = null;

				try {
					serverAddress = createServerConnectionID(0);

					// This triggers a connect
					factory.createPartitionRequestClient(serverAddress);
				}
				catch (Throwable t) {

					if (serverAddress != null) {
						factory.closeOpenChannelConnections(serverAddress);
						Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), t);
					} else {
						t.printStackTrace();
						fail("Could not create RemoteAddress for server.");
					}
				}
			}
		});

		connect.setUncaughtExceptionHandler(exceptionHandler);

		connect.start();

		// Wait on the connect
		syncOnConnect.await();

		connect.interrupt();
		connect.join();

		// Make sure that after a failed connect all resources are cleared.
		assertEquals(0, factory.getNumberOfActiveClients());

		// Make sure that the interrupt exception is not swallowed
		assertTrue(exceptionHandler.getErrors().size() > 0);
	}
	finally {
		if (server != null) {
			server.shutdown();
		}

		if (client != null) {
			client.shutdown();
		}
	}
}
 
Example #16
Source File: ServerTransportErrorHandlingTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies remote closes trigger the release of all resources.
 */
@Test
public void testRemoteClose() throws Exception {
	final TestPooledBufferProvider outboundBuffers = new TestPooledBufferProvider(16);

	final CountDownLatch sync = new CountDownLatch(1);

	final ResultPartitionManager partitionManager = mock(ResultPartitionManager.class);

	when(partitionManager
		.createSubpartitionView(any(ResultPartitionID.class), anyInt(), any(BufferAvailabilityListener.class)))
		.thenAnswer(new Answer<ResultSubpartitionView>() {
			@Override
			public ResultSubpartitionView answer(InvocationOnMock invocationOnMock) throws Throwable {
				BufferAvailabilityListener listener = (BufferAvailabilityListener) invocationOnMock.getArguments()[2];
				listener.notifyDataAvailable();
				return new CancelPartitionRequestTest.InfiniteSubpartitionView(outboundBuffers, sync);
			}
		});

	NettyProtocol protocol = new NettyProtocol(partitionManager, mock(TaskEventDispatcher.class)) {

		@Override
		public ChannelHandler[] getClientChannelHandlers() {
			return new ChannelHandler[]{
				new NettyMessage.NettyMessageEncoder(),
				// Close on read
				new ChannelInboundHandlerAdapter() {
					@Override
					public void channelRead(ChannelHandlerContext ctx, Object msg)
						throws Exception {

						ctx.channel().close();
					}
				}
			};
		}
	};

	NettyTestUtil.NettyServerAndClient serverAndClient = null;

	try {
		serverAndClient = initServerAndClient(protocol, createConfig());

		Channel ch = connect(serverAndClient);

		// Write something to trigger close by server
		ch.writeAndFlush(new NettyMessage.PartitionRequest(new ResultPartitionID(), 0, new InputChannelID(), Integer.MAX_VALUE));

		// 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 released partition.");
		}
	} finally {
		shutdown(serverAndClient);
	}
}
 
Example #17
Source File: KvStateServerHandlerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Frame length decoder (expected by the serialized messages).
 */
private ChannelHandler getFrameDecoder() {
	return new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4);
}
 
Example #18
Source File: KvStateServerHandlerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Frame length decoder (expected by the serialized messages).
 */
private ChannelHandler getFrameDecoder() {
	return new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4);
}
 
Example #19
Source File: ServerTransportErrorHandlingTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies remote closes trigger the release of all resources.
 */
@Test
public void testRemoteClose() throws Exception {
	final TestPooledBufferProvider outboundBuffers = new TestPooledBufferProvider(16);

	final CountDownLatch sync = new CountDownLatch(1);

	final ResultPartitionManager partitionManager = mock(ResultPartitionManager.class);

	when(partitionManager
		.createSubpartitionView(any(ResultPartitionID.class), anyInt(), any(BufferAvailabilityListener.class)))
		.thenAnswer(new Answer<ResultSubpartitionView>() {
			@Override
			public ResultSubpartitionView answer(InvocationOnMock invocationOnMock) throws Throwable {
				BufferAvailabilityListener listener = (BufferAvailabilityListener) invocationOnMock.getArguments()[2];
				listener.notifyDataAvailable();
				return new CancelPartitionRequestTest.InfiniteSubpartitionView(outboundBuffers, sync);
			}
		});

	NettyProtocol protocol = new NettyProtocol(partitionManager, mock(TaskEventDispatcher.class), true) {

		@Override
		public ChannelHandler[] getClientChannelHandlers() {
			return new ChannelHandler[]{
				new NettyMessage.NettyMessageEncoder(),
				// Close on read
				new ChannelInboundHandlerAdapter() {
					@Override
					public void channelRead(ChannelHandlerContext ctx, Object msg)
						throws Exception {

						ctx.channel().close();
					}
				}
			};
		}
	};

	NettyTestUtil.NettyServerAndClient serverAndClient = null;

	try {
		serverAndClient = initServerAndClient(protocol, createConfig());

		Channel ch = connect(serverAndClient);

		// Write something to trigger close by server
		ch.writeAndFlush(new NettyMessage.PartitionRequest(new ResultPartitionID(), 0, new InputChannelID(), Integer.MAX_VALUE));

		// 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 released partition.");
		}
	} finally {
		shutdown(serverAndClient);
	}
}
 
Example #20
Source File: NettyClientServerSslTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelHandler[] getClientChannelHandlers() {
	return new ChannelHandler[0];
}
 
Example #21
Source File: ClientTransportErrorHandlingTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies that failed client requests via {@link PartitionRequestClient} are correctly
 * attributed to the respective {@link RemoteInputChannel}.
 */
@Test
public void testExceptionOnWrite() throws Exception {

	NettyProtocol protocol = new NettyProtocol(
			mock(ResultPartitionProvider.class),
			mock(TaskEventDispatcher.class),
			true) {

		@Override
		public ChannelHandler[] getServerChannelHandlers() {
			return new ChannelHandler[0];
		}
	};

	// We need a real server and client in this test, because Netty's EmbeddedChannel is
	// not failing the ChannelPromise of failed writes.
	NettyServerAndClient serverAndClient = initServerAndClient(protocol, createConfig());

	Channel ch = connect(serverAndClient);

	NetworkClientHandler handler = getClientHandler(ch);

	// Last outbound handler throws Exception after 1st write
	ch.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
		int writeNum = 0;

		@Override
		public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
				throws Exception {

			if (writeNum >= 1) {
				throw new RuntimeException("Expected test exception.");
			}

			writeNum++;
			ctx.write(msg, promise);
		}
	});

	PartitionRequestClient requestClient = new NettyPartitionRequestClient(
			ch, handler, mock(ConnectionID.class), mock(PartitionRequestClientFactory.class));

	// Create input channels
	RemoteInputChannel[] rich = new RemoteInputChannel[] {
			createRemoteInputChannel(), createRemoteInputChannel()};

	final CountDownLatch sync = new CountDownLatch(1);

	// Do this with explicit synchronization. Otherwise this is not robust against slow timings
	// of the callback (e.g. we cannot just verify that it was called once, because there is
	// a chance that we do this too early).
	doAnswer(new Answer<Void>() {
		@Override
		public Void answer(InvocationOnMock invocation) throws Throwable {
			sync.countDown();
			return null;
		}
	}).when(rich[1]).onError(isA(LocalTransportException.class));

	// First request is successful
	requestClient.requestSubpartition(new ResultPartitionID(), 0, rich[0], 0);

	// Second request is *not* successful
	requestClient.requestSubpartition(new ResultPartitionID(), 0, rich[1], 0);

	// Wait for the notification and it could confirm all the request operations are done
	if (!sync.await(TestingUtils.TESTING_DURATION().toMillis(), TimeUnit.MILLISECONDS)) {
		fail("Timed out after waiting for " + TestingUtils.TESTING_DURATION().toMillis() +
				" ms to be notified about the channel error.");
	}

	// Only the second channel should be notified about the error
	verify(rich[0], times(0)).onError(any(LocalTransportException.class));

	shutdown(serverAndClient);
}
 
Example #22
Source File: AbstractTaskManagerFileHandlerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelHandler handler() {
	return null;
}
 
Example #23
Source File: NettyServerLowAndHighWatermarkTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies that the high and low watermark are set in relation to the page size.
 *
 * <p>The high and low water marks control the data flow to the wire. If the Netty write buffer
 * has size greater or equal to the high water mark, the channel state becomes not-writable.
 * Only when the size falls below the low water mark again, the state changes to writable again.
 *
 * <p>The Channel writability state needs to be checked by the handler when writing to the
 * channel and is not enforced in the sense that you cannot write a channel, which is in
 * not-writable state.
 *
 * @param pageSize memory segment size to test with (influences high and low watermarks)
 */
private void testLowAndHighWatermarks(int pageSize) throws Throwable {
	final int expectedLowWatermark = pageSize + 1;
	final int expectedHighWatermark = 2 * pageSize;

	final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
	final NettyProtocol protocol = new NettyProtocol(null, null, true) {
		@Override
		public ChannelHandler[] getServerChannelHandlers() {
			// The channel handler implements the test
			return new ChannelHandler[] {new TestLowAndHighWatermarkHandler(
				pageSize, expectedLowWatermark, expectedHighWatermark, error)};
		}

		@Override
		public ChannelHandler[] getClientChannelHandlers() {
			return new ChannelHandler[0];
		}
	};

	final NettyConfig conf = createConfig(pageSize);

	final NettyServerAndClient serverAndClient = initServerAndClient(protocol, conf);

	try {
		// We can't just check the config of this channel as it is the client's channel. We need
		// to check the server channel, because it is doing the data transfers.
		final Channel ch = connect(serverAndClient);

		// Wait for the channel to be closed
		awaitClose(ch);

		final Throwable t = error.get();
		if (t != null) {
			throw t;
		}
	}
	finally {
		shutdown(serverAndClient);
	}
}
 
Example #24
Source File: ClientTransportErrorHandlingTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies that failed client requests via {@link PartitionRequestClient} are correctly
 * attributed to the respective {@link RemoteInputChannel}.
 */
@Test
public void testExceptionOnWrite() throws Exception {

	NettyProtocol protocol = new NettyProtocol(
			mock(ResultPartitionProvider.class),
			mock(TaskEventDispatcher.class),
			true) {

		@Override
		public ChannelHandler[] getServerChannelHandlers() {
			return new ChannelHandler[0];
		}
	};

	// We need a real server and client in this test, because Netty's EmbeddedChannel is
	// not failing the ChannelPromise of failed writes.
	NettyServerAndClient serverAndClient = initServerAndClient(protocol, createConfig());

	Channel ch = connect(serverAndClient);

	NetworkClientHandler handler = getClientHandler(ch);

	// Last outbound handler throws Exception after 1st write
	ch.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
		int writeNum = 0;

		@Override
		public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
				throws Exception {

			if (writeNum >= 1) {
				throw new RuntimeException("Expected test exception.");
			}

			writeNum++;
			ctx.write(msg, promise);
		}
	});

	PartitionRequestClient requestClient = new PartitionRequestClient(
			ch, handler, mock(ConnectionID.class), mock(PartitionRequestClientFactory.class));

	// Create input channels
	RemoteInputChannel[] rich = new RemoteInputChannel[] {
			createRemoteInputChannel(), createRemoteInputChannel()};

	final CountDownLatch sync = new CountDownLatch(1);

	// Do this with explicit synchronization. Otherwise this is not robust against slow timings
	// of the callback (e.g. we cannot just verify that it was called once, because there is
	// a chance that we do this too early).
	doAnswer(new Answer<Void>() {
		@Override
		public Void answer(InvocationOnMock invocation) throws Throwable {
			sync.countDown();
			return null;
		}
	}).when(rich[1]).onError(isA(LocalTransportException.class));

	// First request is successful
	ChannelFuture f = requestClient.requestSubpartition(new ResultPartitionID(), 0, rich[0], 0);
	assertTrue(f.await().isSuccess());

	// Second request is *not* successful
	f = requestClient.requestSubpartition(new ResultPartitionID(), 0, rich[1], 0);
	assertFalse(f.await().isSuccess());

	// Only the second channel should be notified about the error
	verify(rich[0], times(0)).onError(any(LocalTransportException.class));

	// 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 the channel error.");
	}

	shutdown(serverAndClient);
}
 
Example #25
Source File: PartitionRequestClientFactoryTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testResourceReleaseAfterInterruptedConnect() throws Exception {

	// Latch to synchronize on the connect call.
	final CountDownLatch syncOnConnect = new CountDownLatch(1);

	final Tuple2<NettyServer, NettyClient> netty = createNettyServerAndClient(
			new NettyProtocol(null, null, true) {

				@Override
				public ChannelHandler[] getServerChannelHandlers() {
					return new ChannelHandler[0];
				}

				@Override
				public ChannelHandler[] getClientChannelHandlers() {
					return new ChannelHandler[] {
							new CountDownLatchOnConnectHandler(syncOnConnect)};
				}
			});

	final NettyServer server = netty.f0;
	final NettyClient client = netty.f1;

	final UncaughtTestExceptionHandler exceptionHandler = new UncaughtTestExceptionHandler();

	try {
		final PartitionRequestClientFactory factory = new PartitionRequestClientFactory(client);

		final Thread connect = new Thread(new Runnable() {
			@Override
			public void run() {
				ConnectionID serverAddress = null;

				try {
					serverAddress = createServerConnectionID(0);

					// This triggers a connect
					factory.createPartitionRequestClient(serverAddress);
				}
				catch (Throwable t) {

					if (serverAddress != null) {
						factory.closeOpenChannelConnections(serverAddress);
						Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), t);
					} else {
						t.printStackTrace();
						fail("Could not create RemoteAddress for server.");
					}
				}
			}
		});

		connect.setUncaughtExceptionHandler(exceptionHandler);

		connect.start();

		// Wait on the connect
		syncOnConnect.await();

		connect.interrupt();
		connect.join();

		// Make sure that after a failed connect all resources are cleared.
		assertEquals(0, factory.getNumberOfActiveClients());

		// Make sure that the interrupt exception is not swallowed
		assertTrue(exceptionHandler.getErrors().size() > 0);
	}
	finally {
		if (server != null) {
			server.shutdown();
		}

		if (client != null) {
			client.shutdown();
		}
	}
}
 
Example #26
Source File: NettyClientServerSslTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelHandler[] getServerChannelHandlers() {
	return new ChannelHandler[0];
}
 
Example #27
Source File: NettyClientServerSslTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelHandler[] getClientChannelHandlers() {
	return new ChannelHandler[0];
}
 
Example #28
Source File: ServerTransportErrorHandlingTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies remote closes trigger the release of all resources.
 */
@Test
public void testRemoteClose() throws Exception {
	final TestPooledBufferProvider outboundBuffers = new TestPooledBufferProvider(16);

	final CountDownLatch sync = new CountDownLatch(1);

	final ResultPartitionManager partitionManager = mock(ResultPartitionManager.class);

	when(partitionManager
		.createSubpartitionView(any(ResultPartitionID.class), anyInt(), any(BufferAvailabilityListener.class)))
		.thenAnswer(new Answer<ResultSubpartitionView>() {
			@Override
			public ResultSubpartitionView answer(InvocationOnMock invocationOnMock) throws Throwable {
				BufferAvailabilityListener listener = (BufferAvailabilityListener) invocationOnMock.getArguments()[2];
				listener.notifyDataAvailable();
				return new CancelPartitionRequestTest.InfiniteSubpartitionView(outboundBuffers, sync);
			}
		});

	NettyProtocol protocol = new NettyProtocol(partitionManager, mock(TaskEventDispatcher.class), true) {

		@Override
		public ChannelHandler[] getClientChannelHandlers() {
			return new ChannelHandler[]{
				new NettyMessage.NettyMessageEncoder(),
				// Close on read
				new ChannelInboundHandlerAdapter() {
					@Override
					public void channelRead(ChannelHandlerContext ctx, Object msg)
						throws Exception {

						ctx.channel().close();
					}
				}
			};
		}
	};

	NettyTestUtil.NettyServerAndClient serverAndClient = null;

	try {
		serverAndClient = initServerAndClient(protocol, createConfig());

		Channel ch = connect(serverAndClient);

		// Write something to trigger close by server
		ch.writeAndFlush(new NettyMessage.PartitionRequest(new ResultPartitionID(), 0, new InputChannelID(), Integer.MAX_VALUE));

		// 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 released partition.");
		}
	} finally {
		shutdown(serverAndClient);
	}
}
 
Example #29
Source File: KvStateServerHandlerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Frame length decoder (expected by the serialized messages).
 */
private ChannelHandler getFrameDecoder() {
	return new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4);
}
 
Example #30
Source File: AbstractTaskManagerFileHandlerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelHandler handler() {
	return null;
}