Java Code Examples for org.apache.flink.shaded.netty4.io.netty.channel.Channel#close()

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.channel.Channel#close() . 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: Client.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Hands in a channel after a successful connection.
 *
 * @param channel Channel to hand in
 */
private void handInChannel(Channel channel) {
	synchronized (connectLock) {
		if (connectionShutdownFuture.get() != null || failureCause != null) {
			// Close the channel and we are done. Any queued requests
			// are removed on the close/failure call and after that no
			// new ones can be enqueued.
			channel.close();
		} else {
			established = new EstablishedConnection(serverAddress, serializer, channel);

			while (!queuedRequests.isEmpty()) {
				final PendingRequest pending = queuedRequests.poll();

				established.sendRequest(pending.request).whenComplete(
						(response, throwable) -> {
							if (throwable != null) {
								pending.completeExceptionally(throwable);
							} else {
								pending.complete(response);
							}
						});
			}

			// Publish the channel for the general public
			establishedConnections.put(serverAddress, established);
			pendingConnections.remove(serverAddress);

			// Check shut down for possible race with shut down. We
			// don't want any lingering connections after shut down,
			// which can happen if we don't check this here.
			if (clientShutdownFuture.get() != null) {
				if (establishedConnections.remove(serverAddress, established)) {
					established.close();
				}
			}
		}
	}
}
 
Example 2
Source File: Client.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Hands in a channel after a successful connection.
 *
 * @param channel Channel to hand in
 */
private void handInChannel(Channel channel) {
	synchronized (connectLock) {
		if (connectionShutdownFuture.get() != null || failureCause != null) {
			// Close the channel and we are done. Any queued requests
			// are removed on the close/failure call and after that no
			// new ones can be enqueued.
			channel.close();
		} else {
			established = new EstablishedConnection(serverAddress, serializer, channel);

			while (!queuedRequests.isEmpty()) {
				final PendingRequest pending = queuedRequests.poll();

				established.sendRequest(pending.request).whenComplete(
						(response, throwable) -> {
							if (throwable != null) {
								pending.completeExceptionally(throwable);
							} else {
								pending.complete(response);
							}
						});
			}

			// Publish the channel for the general public
			establishedConnections.put(serverAddress, established);
			pendingConnections.remove(serverAddress);

			// Check shut down for possible race with shut down. We
			// don't want any lingering connections after shut down,
			// which can happen if we don't check this here.
			if (clientShutdownFuture.get() != null) {
				if (establishedConnections.remove(serverAddress, established)) {
					established.close();
				}
			}
		}
	}
}
 
Example 3
Source File: Client.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Hands in a channel after a successful connection.
 *
 * @param channel Channel to hand in
 */
private void handInChannel(Channel channel) {
	synchronized (connectLock) {
		if (connectionShutdownFuture.get() != null || failureCause != null) {
			// Close the channel and we are done. Any queued requests
			// are removed on the close/failure call and after that no
			// new ones can be enqueued.
			channel.close();
		} else {
			established = new EstablishedConnection(serverAddress, serializer, channel);

			while (!queuedRequests.isEmpty()) {
				final PendingRequest pending = queuedRequests.poll();

				established.sendRequest(pending.request).whenComplete(
						(response, throwable) -> {
							if (throwable != null) {
								pending.completeExceptionally(throwable);
							} else {
								pending.complete(response);
							}
						});
			}

			// Publish the channel for the general public
			establishedConnections.put(serverAddress, established);
			pendingConnections.remove(serverAddress);

			// Check shut down for possible race with shut down. We
			// don't want any lingering connections after shut down,
			// which can happen if we don't check this here.
			if (clientShutdownFuture.get() != null) {
				if (establishedConnections.remove(serverAddress, established)) {
					established.close();
				}
			}
		}
	}
}
 
Example 4
Source File: CancelPartitionRequestTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testDuplicateCancel() throws Exception {

	NettyServerAndClient serverAndClient = null;

	try {
		final TestPooledBufferProvider outboundBuffers = new TestPooledBufferProvider(16);

		ResultPartitionManager partitions = mock(ResultPartitionManager.class);

		ResultPartitionID pid = new ResultPartitionID();

		final CountDownLatch sync = new CountDownLatch(1);

		final ResultSubpartitionView view = spy(new InfiniteSubpartitionView(outboundBuffers, sync));

		// Return infinite subpartition
		when(partitions.createSubpartitionView(eq(pid), eq(0), any(BufferAvailabilityListener.class)))
				.thenAnswer(new Answer<ResultSubpartitionView>() {
					@Override
					public ResultSubpartitionView answer(InvocationOnMock invocationOnMock) throws Throwable {
						BufferAvailabilityListener listener = (BufferAvailabilityListener) invocationOnMock.getArguments()[2];
						listener.notifyDataAvailable();
						return view;
					}
				});

		NettyProtocol protocol = new NettyProtocol(
				partitions, mock(TaskEventDispatcher.class), true);

		serverAndClient = initServerAndClient(protocol);

		Channel ch = connect(serverAndClient);

		// Request for non-existing input channel => results in cancel request
		InputChannelID inputChannelId = new InputChannelID();

		ch.writeAndFlush(new PartitionRequest(pid, 0, inputChannelId, Integer.MAX_VALUE)).await();

		// 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 cancelled partition.");
		}

		ch.writeAndFlush(new CancelPartitionRequest(inputChannelId)).await();

		ch.close();

		NettyTestUtil.awaitClose(ch);

		verify(view, times(1)).releaseAllResources();
		verify(view, times(0)).notifySubpartitionConsumed();
	}
	finally {
		shutdown(serverAndClient);
	}
}
 
Example 5
Source File: CancelPartitionRequestTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testDuplicateCancel() throws Exception {

	NettyServerAndClient serverAndClient = null;

	try {
		final TestPooledBufferProvider outboundBuffers = new TestPooledBufferProvider(16);

		ResultPartitionManager partitions = mock(ResultPartitionManager.class);

		ResultPartitionID pid = new ResultPartitionID();

		final CountDownLatch sync = new CountDownLatch(1);

		final ResultSubpartitionView view = spy(new InfiniteSubpartitionView(outboundBuffers, sync));

		// Return infinite subpartition
		when(partitions.createSubpartitionView(eq(pid), eq(0), any(BufferAvailabilityListener.class)))
				.thenAnswer(new Answer<ResultSubpartitionView>() {
					@Override
					public ResultSubpartitionView answer(InvocationOnMock invocationOnMock) throws Throwable {
						BufferAvailabilityListener listener = (BufferAvailabilityListener) invocationOnMock.getArguments()[2];
						listener.notifyDataAvailable();
						return view;
					}
				});

		NettyProtocol protocol = new NettyProtocol(
				partitions, mock(TaskEventDispatcher.class), true);

		serverAndClient = initServerAndClient(protocol);

		Channel ch = connect(serverAndClient);

		// Request for non-existing input channel => results in cancel request
		InputChannelID inputChannelId = new InputChannelID();

		ch.writeAndFlush(new PartitionRequest(pid, 0, inputChannelId, Integer.MAX_VALUE)).await();

		// 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 cancelled partition.");
		}

		ch.writeAndFlush(new CancelPartitionRequest(inputChannelId)).await();

		ch.close();

		NettyTestUtil.awaitClose(ch);

		verify(view, times(1)).releaseAllResources();
		verify(view, times(1)).notifySubpartitionConsumed();
	}
	finally {
		shutdown(serverAndClient);
	}
}
 
Example 6
Source File: CancelPartitionRequestTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testDuplicateCancel() throws Exception {

	NettyServerAndClient serverAndClient = null;

	try {
		final TestPooledBufferProvider outboundBuffers = new TestPooledBufferProvider(16);

		ResultPartitionManager partitions = mock(ResultPartitionManager.class);

		ResultPartitionID pid = new ResultPartitionID();

		final CountDownLatch sync = new CountDownLatch(1);

		final ResultSubpartitionView view = spy(new InfiniteSubpartitionView(outboundBuffers, sync));

		// Return infinite subpartition
		when(partitions.createSubpartitionView(eq(pid), eq(0), any(BufferAvailabilityListener.class)))
				.thenAnswer(new Answer<ResultSubpartitionView>() {
					@Override
					public ResultSubpartitionView answer(InvocationOnMock invocationOnMock) throws Throwable {
						BufferAvailabilityListener listener = (BufferAvailabilityListener) invocationOnMock.getArguments()[2];
						listener.notifyDataAvailable();
						return view;
					}
				});

		NettyProtocol protocol = new NettyProtocol(partitions, mock(TaskEventDispatcher.class));

		serverAndClient = initServerAndClient(protocol);

		Channel ch = connect(serverAndClient);

		// Request for non-existing input channel => results in cancel request
		InputChannelID inputChannelId = new InputChannelID();

		ch.writeAndFlush(new PartitionRequest(pid, 0, inputChannelId, Integer.MAX_VALUE)).await();

		// 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 cancelled partition.");
		}

		ch.writeAndFlush(new CancelPartitionRequest(inputChannelId)).await();

		ch.close();

		NettyTestUtil.awaitClose(ch);

		verify(view, times(1)).releaseAllResources();
	}
	finally {
		shutdown(serverAndClient);
	}
}