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

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener. 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: NettyPartitionRequestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a task event backwards to an intermediate result partition producer.
 *
 * <p>Backwards task events flow between readers and writers and therefore
 * will only work when both are running at the same time, which is only
 * guaranteed to be the case when both the respective producer and
 * consumer task run pipelined.
 */
@Override
public void sendTaskEvent(ResultPartitionID partitionId, TaskEvent event, final RemoteInputChannel inputChannel) throws IOException {
	checkNotClosed();

	tcpChannel.writeAndFlush(new TaskEventRequest(event, partitionId, inputChannel.getInputChannelId()))
			.addListener(
					new ChannelFutureListener() {
						@Override
						public void operationComplete(ChannelFuture future) throws Exception {
							if (!future.isSuccess()) {
								SocketAddress remoteAddr = future.channel().remoteAddress();
								inputChannel.onError(new LocalTransportException(
									String.format("Sending the task event to '%s' failed.", remoteAddr),
									future.channel().localAddress(), future.cause()
								));
							}
						}
					});
}
 
Example #2
Source File: NettyPartitionRequestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void close(RemoteInputChannel inputChannel) throws IOException {

	clientHandler.removeInputChannel(inputChannel);

	if (closeReferenceCounter.decrement()) {
		// Close the TCP connection. Send a close request msg to ensure
		// that outstanding backwards task events are not discarded.
		tcpChannel.writeAndFlush(new NettyMessage.CloseRequest())
				.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);

		// Make sure to remove the client from the factory
		clientFactory.destroyPartitionRequestClient(connectionId, this);
	} else {
		clientHandler.cancelRequestFor(inputChannel.getInputChannelId());
	}
}
 
Example #3
Source File: NettyPartitionRequestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a task event backwards to an intermediate result partition producer.
 *
 * <p>Backwards task events flow between readers and writers and therefore
 * will only work when both are running at the same time, which is only
 * guaranteed to be the case when both the respective producer and
 * consumer task run pipelined.
 */
@Override
public void sendTaskEvent(ResultPartitionID partitionId, TaskEvent event, final RemoteInputChannel inputChannel) throws IOException {
	checkNotClosed();

	tcpChannel.writeAndFlush(new TaskEventRequest(event, partitionId, inputChannel.getInputChannelId()))
			.addListener(
					new ChannelFutureListener() {
						@Override
						public void operationComplete(ChannelFuture future) throws Exception {
							if (!future.isSuccess()) {
								SocketAddress remoteAddr = future.channel().remoteAddress();
								inputChannel.onError(new LocalTransportException(
									String.format("Sending the task event to '%s' failed.", remoteAddr),
									future.channel().localAddress(), future.cause()
								));
							}
						}
					});
}
 
Example #4
Source File: NettyPartitionRequestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void close(RemoteInputChannel inputChannel) throws IOException {

	clientHandler.removeInputChannel(inputChannel);

	if (closeReferenceCounter.decrement()) {
		// Close the TCP connection. Send a close request msg to ensure
		// that outstanding backwards task events are not discarded.
		tcpChannel.writeAndFlush(new NettyMessage.CloseRequest())
				.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);

		// Make sure to remove the client from the factory
		clientFactory.destroyPartitionRequestClient(connectionId, this);
	} else {
		clientHandler.cancelRequestFor(inputChannel.getInputChannelId());
	}
}
 
Example #5
Source File: PartitionRequestClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a task event backwards to an intermediate result partition producer.
 * <p>
 * Backwards task events flow between readers and writers and therefore
 * will only work when both are running at the same time, which is only
 * guaranteed to be the case when both the respective producer and
 * consumer task run pipelined.
 */
public void sendTaskEvent(ResultPartitionID partitionId, TaskEvent event, final RemoteInputChannel inputChannel) throws IOException {
	checkNotClosed();

	tcpChannel.writeAndFlush(new TaskEventRequest(event, partitionId, inputChannel.getInputChannelId()))
			.addListener(
					new ChannelFutureListener() {
						@Override
						public void operationComplete(ChannelFuture future) throws Exception {
							if (!future.isSuccess()) {
								SocketAddress remoteAddr = future.channel().remoteAddress();
								inputChannel.onError(new LocalTransportException(
									String.format("Sending the task event to '%s' failed.", remoteAddr),
									future.channel().localAddress(), future.cause()
								));
							}
						}
					});
}
 
Example #6
Source File: PartitionRequestClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public void close(RemoteInputChannel inputChannel) throws IOException {

		clientHandler.removeInputChannel(inputChannel);

		if (closeReferenceCounter.decrement()) {
			// Close the TCP connection. Send a close request msg to ensure
			// that outstanding backwards task events are not discarded.
			tcpChannel.writeAndFlush(new NettyMessage.CloseRequest())
					.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);

			// Make sure to remove the client from the factory
			clientFactory.destroyPartitionRequestClient(connectionId, this);
		} else {
			clientHandler.cancelRequestFor(inputChannel.getInputChannelId());
		}
	}
 
Example #7
Source File: RedirectingSslHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
	HttpRequest request = msg instanceof HttpRequest ? (HttpRequest) msg : null;
	String path = request == null ? "" : request.uri();
	String redirectAddress = getRedirectAddress(ctx);
	log.trace("Received non-SSL request, redirecting to {}{}", redirectAddress, path);
	HttpResponse response = HandlerRedirectUtils.getRedirectResponse(
		redirectAddress, path, HttpResponseStatus.MOVED_PERMANENTLY);
	if (request != null) {
		KeepAliveWrite.flush(ctx, request, response);
	} else {
		ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	}
}
 
Example #8
Source File: HandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #9
Source File: PartitionRequestQueue.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleException(Channel channel, Throwable cause) throws IOException {
	LOG.error("Encountered error while consuming partitions", cause);

	fatalError = true;
	releaseAllResources();

	if (channel.isActive()) {
		channel.writeAndFlush(new ErrorResponse(cause)).addListener(ChannelFutureListener.CLOSE);
	}
}
 
Example #10
Source File: AbstractServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
	final String msg = "Exception in server pipeline. Caused by: " + ExceptionUtils.stringifyException(cause);
	final ByteBuf err = MessageSerializer.serializeServerFailure(ctx.alloc(), new RuntimeException(msg));

	LOG.debug(msg);
	ctx.writeAndFlush(err).addListener(ChannelFutureListener.CLOSE);
}
 
Example #11
Source File: MesosArtifactServer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Send the "405 Method Not Allowed" response.
 *
 * @param ctx The channel context to write the response to.
 */
private static void sendMethodNotAllowed(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #12
Source File: MesosArtifactServer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a simple  error response message.
 *
 * @param ctx    The channel context to write the response to.
 * @param status The response status.
 */
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
	FullHttpResponse response = new DefaultFullHttpResponse(
		HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
	HttpHeaders.setHeader(response, CONTENT_TYPE, "text/plain; charset=UTF-8");

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #13
Source File: StaticFileServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Send the "304 Not Modified" response. This response can be used when the
 * file timestamp is the same as what the browser is sending up.
 *
 * @param ctx The channel context to write the response to.
 */
public static void sendNotModified(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED);
	setDateHeader(response);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #14
Source File: KeepAliveWrite.java    From flink with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) {
	if (!HttpHeaders.isKeepAlive(request)) {
		return ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	} else {
		response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ctx.writeAndFlush(response);
	}
}
 
Example #15
Source File: KeepAliveWrite.java    From flink with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(Channel ch, HttpRequest req, HttpResponse res) {
	if (!HttpHeaders.isKeepAlive(req)) {
		return ch.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
	} else {
		res.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ch.writeAndFlush(res);
	}
}
 
Example #16
Source File: HandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #17
Source File: RedirectingSslHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
	HttpRequest request = msg instanceof HttpRequest ? (HttpRequest) msg : null;
	String path = request == null ? "" : request.uri();
	String redirectAddress = getRedirectAddress(ctx);
	log.trace("Received non-SSL request, redirecting to {}{}", redirectAddress, path);
	HttpResponse response = HandlerRedirectUtils.getRedirectResponse(
		redirectAddress, path, HttpResponseStatus.MOVED_PERMANENTLY);
	if (request != null) {
		KeepAliveWrite.flush(ctx, request, response);
	} else {
		ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	}
}
 
Example #18
Source File: PartitionRequestQueue.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleException(Channel channel, Throwable cause) throws IOException {
	LOG.error("Encountered error while consuming partitions", cause);

	fatalError = true;
	releaseAllResources();

	if (channel.isActive()) {
		channel.writeAndFlush(new ErrorResponse(cause)).addListener(ChannelFutureListener.CLOSE);
	}
}
 
Example #19
Source File: AbstractServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
	final String msg = "Exception in server pipeline. Caused by: " + ExceptionUtils.stringifyException(cause);
	final ByteBuf err = MessageSerializer.serializeServerFailure(ctx.alloc(), new RuntimeException(msg));

	LOG.debug(msg);
	ctx.writeAndFlush(err).addListener(ChannelFutureListener.CLOSE);
}
 
Example #20
Source File: AbstractServerHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
	final String msg = "Exception in server pipeline. Caused by: " + ExceptionUtils.stringifyException(cause);
	final ByteBuf err = MessageSerializer.serializeServerFailure(ctx.alloc(), new RuntimeException(msg));

	LOG.debug(msg);
	ctx.writeAndFlush(err).addListener(ChannelFutureListener.CLOSE);
}
 
Example #21
Source File: MesosArtifactServer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a simple  error response message.
 *
 * @param ctx    The channel context to write the response to.
 * @param status The response status.
 */
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
	FullHttpResponse response = new DefaultFullHttpResponse(
		HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
	HttpHeaders.setHeader(response, CONTENT_TYPE, "text/plain; charset=UTF-8");

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #22
Source File: StaticFileServerHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Send the "304 Not Modified" response. This response can be used when the
 * file timestamp is the same as what the browser is sending up.
 *
 * @param ctx The channel context to write the response to.
 */
public static void sendNotModified(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED);
	setDateHeader(response);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #23
Source File: KeepAliveWrite.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) {
	if (!HttpHeaders.isKeepAlive(request)) {
		return ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	} else {
		response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ctx.writeAndFlush(response);
	}
}
 
Example #24
Source File: KeepAliveWrite.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static ChannelFuture flush(Channel ch, HttpRequest req, HttpResponse res) {
	if (!HttpHeaders.isKeepAlive(req)) {
		return ch.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
	} else {
		res.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
		return ch.writeAndFlush(res);
	}
}
 
Example #25
Source File: HandlerUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #26
Source File: RedirectingSslHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
	HttpRequest request = msg instanceof HttpRequest ? (HttpRequest) msg : null;
	String path = request == null ? "" : request.uri();
	String redirectAddress = getRedirectAddress(ctx);
	log.trace("Received non-SSL request, redirecting to {}{}", redirectAddress, path);
	HttpResponse response = HandlerRedirectUtils.getRedirectResponse(
		redirectAddress, path, HttpResponseStatus.MOVED_PERMANENTLY);
	if (request != null) {
		KeepAliveWrite.flush(ctx, request, response);
	} else {
		ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	}
}
 
Example #27
Source File: PartitionRequestQueue.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void handleException(Channel channel, Throwable cause) throws IOException {
	LOG.error("Encountered error while consuming partitions", cause);

	fatalError = true;
	releaseAllResources();

	if (channel.isActive()) {
		channel.writeAndFlush(new ErrorResponse(cause)).addListener(ChannelFutureListener.CLOSE);
	}
}
 
Example #28
Source File: MesosArtifactServer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Send the "405 Method Not Allowed" response.
 *
 * @param ctx The channel context to write the response to.
 */
private static void sendMethodNotAllowed(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #29
Source File: MesosArtifactServer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Send the "405 Method Not Allowed" response.
 *
 * @param ctx The channel context to write the response to.
 */
private static void sendMethodNotAllowed(ChannelHandlerContext ctx) {
	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #30
Source File: MesosArtifactServer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a simple  error response message.
 *
 * @param ctx    The channel context to write the response to.
 * @param status The response status.
 */
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
	FullHttpResponse response = new DefaultFullHttpResponse(
		HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
	HttpHeaders.setHeader(response, CONTENT_TYPE, "text/plain; charset=UTF-8");

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}