List of usage examples for io.netty.channel ChannelFutureListener CLOSE
ChannelFutureListener CLOSE
To view the source code for io.netty.channel ChannelFutureListener CLOSE.
Click Source Link
From source file:org.apache.flink.runtime.query.netty.KvStateServerHandler.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { String stringifiedCause = ExceptionUtils.stringifyException(cause); String msg = "Exception in server pipeline. Caused by: " + stringifiedCause; ByteBuf err = KvStateRequestSerializer.serializeServerFailure(ctx.alloc(), new RuntimeException(msg)); ctx.writeAndFlush(err).addListener(ChannelFutureListener.CLOSE); }
From source file:org.apache.flink.runtime.webmonitor.files.StaticFileServerHandler.java
License:Apache License
/** * Response when running with leading JobManager. */// w w w .java2 s . co m private void respondAsLeader(ChannelHandlerContext ctx, HttpRequest request, String requestPath) throws IOException, ParseException, URISyntaxException { // convert to absolute path final File file = new File(rootPath, requestPath); if (!file.exists()) { // file does not exist. Try to load it with the classloader ClassLoader cl = StaticFileServerHandler.class.getClassLoader(); try (InputStream resourceStream = cl.getResourceAsStream("web" + requestPath)) { boolean success = false; try { if (resourceStream != null) { URL root = cl.getResource("web"); URL requested = cl.getResource("web" + requestPath); if (root != null && requested != null) { URI rootURI = new URI(root.getPath()).normalize(); URI requestedURI = new URI(requested.getPath()).normalize(); // Check that we don't load anything from outside of the // expected scope. if (!rootURI.relativize(requestedURI).equals(requestedURI)) { logger.debug("Loading missing file from classloader: {}", requestPath); // ensure that directory to file exists. file.getParentFile().mkdirs(); Files.copy(resourceStream, file.toPath()); success = true; } } } } catch (Throwable t) { logger.error("error while responding", t); } finally { if (!success) { logger.debug("Unable to load requested file {} from classloader", requestPath); sendError(ctx, NOT_FOUND); return; } } } } if (!file.exists() || file.isHidden() || file.isDirectory() || !file.isFile()) { sendError(ctx, NOT_FOUND); return; } if (!file.getCanonicalFile().toPath().startsWith(rootPath.toPath())) { sendError(ctx, NOT_FOUND); return; } // cache validation final String ifModifiedSince = request.headers().get(IF_MODIFIED_SINCE); if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) { SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US); Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince); // Only compare up to the second because the datetime format we send to the client // does not have milliseconds long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000; long fileLastModifiedSeconds = file.lastModified() / 1000; if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) { if (logger.isDebugEnabled()) { logger.debug("Responding 'NOT MODIFIED' for file '" + file.getAbsolutePath() + '\''); } sendNotModified(ctx); return; } } if (logger.isDebugEnabled()) { logger.debug("Responding with file '" + file.getAbsolutePath() + '\''); } // Don't need to close this manually. Netty's DefaultFileRegion will take care of it. final RandomAccessFile raf; try { raf = new RandomAccessFile(file, "r"); } catch (FileNotFoundException e) { sendError(ctx, NOT_FOUND); return; } long fileLength = raf.length(); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); setContentTypeHeader(response, file); // since the log and out files are rapidly changing, we don't want to browser to cache them if (!(requestPath.contains("log") || requestPath.contains("out"))) { setDateAndCacheHeaders(response, file); } if (HttpHeaders.isKeepAlive(request)) { 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. ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise()); ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); // close the connection, if no keep-alive is needed if (!HttpHeaders.isKeepAlive(request)) { lastContentFuture.addListener(ChannelFutureListener.CLOSE); } }
From source file:org.apache.flink.runtime.webmonitor.files.StaticFileServerHandler.java
License:Apache License
/** * Writes a simple error response message. * * @param ctx The channel context to write the response to. * @param status The response status./*w w w. j av a 2s. com*/ */ 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)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); // close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:org.apache.flink.runtime.webmonitor.files.StaticFileServerHandler.java
License:Apache License
/** * 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. *//*from w w w .j a v a 2s. c om*/ private 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); }
From source file:org.apache.flink.runtime.webmonitor.handlers.TaskManagerLogHandler.java
License:Apache License
/** * Response when running with leading JobManager. *///from w ww .ja v a2s . co m @Override protected void respondAsLeader(final ChannelHandlerContext ctx, final Routed routed, final ActorGateway jobManager) { if (cache == null) { Future<Object> portFuture = jobManager.ask(JobManagerMessages.getRequestBlobManagerPort(), timeout); cache = portFuture.map(new Mapper<Object, BlobCache>() { @Override public BlobCache apply(Object result) { Option<String> hostOption = jobManager.actor().path().address().host(); String host = hostOption.isDefined() ? hostOption.get() : "localhost"; int port = (int) result; return new BlobCache(new InetSocketAddress(host, port), config); } }, executor); } final String taskManagerID = routed.pathParams().get(TaskManagersHandler.TASK_MANAGER_ID_KEY); final HttpRequest request = routed.request(); //fetch TaskManager logs if no other process is currently doing it if (lastRequestPending.putIfAbsent(taskManagerID, true) == null) { try { InstanceID instanceID = new InstanceID(StringUtils.hexStringToByte(taskManagerID)); Future<Object> taskManagerFuture = jobManager .ask(new JobManagerMessages.RequestTaskManagerInstance(instanceID), timeout); Future<Object> blobKeyFuture = taskManagerFuture.flatMap(new Mapper<Object, Future<Object>>() { @Override public Future<Object> apply(Object instance) { Instance taskManager = ((JobManagerMessages.TaskManagerInstance) instance).instance().get(); return taskManager.getActorGateway() .ask(serveLogFile ? TaskManagerMessages.getRequestTaskManagerLog() : TaskManagerMessages.getRequestTaskManagerStdout(), timeout); } }, executor); Future<Object> logPathFuture = cache.zip(blobKeyFuture) .map(new Mapper<Tuple2<BlobCache, Object>, Object>() { @Override public Object checkedApply(Tuple2<BlobCache, Object> instance) throws Exception { BlobCache cache = instance._1(); if (instance._2() instanceof Exception) { throw (Exception) instance._2(); } BlobKey blobKey = (BlobKey) instance._2(); //delete previous log file, if it is different than the current one HashMap<String, BlobKey> lastSubmittedFile = serveLogFile ? lastSubmittedLog : lastSubmittedStdout; if (lastSubmittedFile.containsKey(taskManagerID)) { if (!blobKey.equals(lastSubmittedFile.get(taskManagerID))) { cache.deleteGlobal(lastSubmittedFile.get(taskManagerID)); lastSubmittedFile.put(taskManagerID, blobKey); } } else { lastSubmittedFile.put(taskManagerID, blobKey); } return cache.getURL(blobKey).getFile(); } }, executor); logPathFuture.onFailure(new OnFailure() { @Override public void onFailure(Throwable failure) throws Throwable { display(ctx, request, "Fetching TaskManager log failed."); LOG.error("Fetching TaskManager log failed.", failure); lastRequestPending.remove(taskManagerID); } }, executor); logPathFuture.onSuccess(new OnSuccess<Object>() { @Override public void onSuccess(Object filePathOption) throws Throwable { String filePath = (String) filePathOption; File file = new File(filePath); final RandomAccessFile raf; try { raf = new RandomAccessFile(file, "r"); } catch (FileNotFoundException e) { display(ctx, request, "Displaying TaskManager log failed."); LOG.error("Displaying TaskManager log failed.", e); return; } long fileLength = raf.length(); final FileChannel fc = raf.getChannel(); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.headers().set(CONTENT_TYPE, "text/plain"); if (HttpHeaders.isKeepAlive(request)) { 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. ctx.write(new DefaultFileRegion(fc, 0, fileLength), ctx.newProgressivePromise()) .addListener( new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() { @Override public void operationComplete( io.netty.util.concurrent.Future<? super Void> future) throws Exception { lastRequestPending.remove(taskManagerID); fc.close(); raf.close(); } }); ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); // close the connection, if no keep-alive is needed if (!HttpHeaders.isKeepAlive(request)) { lastContentFuture.addListener(ChannelFutureListener.CLOSE); } } }, executor); } catch (Exception e) { display(ctx, request, "Error: " + e.getMessage()); LOG.error("Fetching TaskManager log failed.", e); lastRequestPending.remove(taskManagerID); } } else { display(ctx, request, "loading..."); } }
From source file:org.apache.flink.runtime.webmonitor.handlers.TaskManagerLogHandler.java
License:Apache License
private void display(ChannelHandlerContext ctx, HttpRequest request, String message) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.headers().set(CONTENT_TYPE, "text/plain"); if (HttpHeaders.isKeepAlive(request)) { response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); }//from ww w. java 2 s .c o m byte[] buf = message.getBytes(); ByteBuf b = Unpooled.copiedBuffer(buf); HttpHeaders.setContentLength(response, buf.length); // write the initial line and the header. ctx.write(response); ctx.write(b); ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); // close the connection, if no keep-alive is needed if (!HttpHeaders.isKeepAlive(request)) { lastContentFuture.addListener(ChannelFutureListener.CLOSE); } }
From source file:org.apache.hadoop.hbase.ipc.AsyncRpcChannel.java
License:Apache License
/** * Close connection/*from w w w . ja va 2 s.c om*/ * * @param e exception on close */ public void close(final Throwable e) { client.removeConnection(this); // Move closing from the requesting thread to the channel thread channel.eventLoop().execute(new Runnable() { @Override public void run() { List<AsyncCall> toCleanup; synchronized (pendingCalls) { if (closed) { return; } closed = true; toCleanup = new ArrayList<AsyncCall>(pendingCalls.values()); pendingCalls.clear(); } IOException closeException = null; if (e != null) { if (e instanceof IOException) { closeException = (IOException) e; } else { closeException = new IOException(e); } } // log the info if (LOG.isDebugEnabled() && closeException != null) { LOG.debug(name + ": closing ipc connection to " + address, closeException); } if (cleanupTimer != null) { cleanupTimer.cancel(); cleanupTimer = null; } for (AsyncCall call : toCleanup) { call.setFailed(closeException != null ? closeException : new ConnectionClosingException("Call id=" + call.id + " on server " + address + " aborted: connection is closing")); } channel.disconnect().addListener(ChannelFutureListener.CLOSE); if (LOG.isDebugEnabled()) { LOG.debug(name + ": closed"); } } }); }
From source file:org.apache.hadoop.hbase.ipc.AsyncRpcChannelImpl.java
License:Apache License
private void close0(Throwable e) { List<AsyncCall> toCleanup; synchronized (pendingCalls) { if (closed) { return; }//from ww w .ja v a 2 s . c o m closed = true; toCleanup = new ArrayList<AsyncCall>(pendingCalls.values()); pendingCalls.clear(); } IOException closeException = null; if (e != null) { if (e instanceof IOException) { closeException = (IOException) e; } else { closeException = new IOException(e); } } // log the info if (LOG.isDebugEnabled() && closeException != null) { LOG.debug(name + ": closing ipc connection to " + address, closeException); } if (cleanupTimer != null) { cleanupTimer.cancel(); cleanupTimer = null; } for (AsyncCall call : toCleanup) { call.setFailed(closeException != null ? closeException : new ConnectionClosingException( "Call id=" + call.id + " on server " + address + " aborted: connection is closing")); } channel.disconnect().addListener(ChannelFutureListener.CLOSE); if (LOG.isDebugEnabled()) { LOG.debug(name + ": closed"); } }
From source file:org.apache.hadoop.hdfs.server.datanode.web.RestCsrfPreventionFilterHandler.java
License:Apache License
/** * Finish handling this pipeline by writing a response with the * "Connection: close" header, flushing, and scheduling a close of the * connection.// ww w.j a v a2 s . com * * @param ctx context to receive the response * @param resp response to send */ private static void sendResponseAndClose(ChannelHandlerContext ctx, DefaultHttpResponse resp) { resp.headers().set(CONNECTION, CLOSE); ctx.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE); }
From source file:org.apache.hadoop.hdfs.server.datanode.web.SimpleHttpProxyHandler.java
License:Apache License
@Override public void channelRead0(final ChannelHandlerContext ctx, final HttpRequest req) { uri = req.getUri();/*from ww w . j ava2 s .c om*/ final Channel client = ctx.channel(); Bootstrap proxiedServer = new Bootstrap().group(client.eventLoop()).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpRequestEncoder(), new Forwarder(uri, client)); } }); ChannelFuture f = proxiedServer.connect(host); proxiedChannel = f.channel(); f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { ctx.channel().pipeline().remove(HttpResponseEncoder.class); HttpRequest newReq = new DefaultFullHttpRequest(HTTP_1_1, req.getMethod(), req.getUri()); newReq.headers().add(req.headers()); newReq.headers().set(CONNECTION, Values.CLOSE); future.channel().writeAndFlush(newReq); } else { DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1, INTERNAL_SERVER_ERROR); resp.headers().set(CONNECTION, Values.CLOSE); LOG.info("Proxy " + uri + " failed. Cause: ", future.cause()); ctx.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE); client.close(); } } }); }