From d09055b3350ce047fee107c8ca1ee9bfed4ef9b4 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Wed, 26 Aug 2020 14:45:12 +1000 Subject: [PATCH] Issue #5105 - change asyncGraceful to gracefulShutdownWaitsForRequests Signed-off-by: Lachlan Roberts --- .../src/main/config/etc/jetty-stats.xml | 2 +- .../src/main/config/modules/stats.mod | 2 +- .../server/handler/StatisticsHandler.java | 27 ++++++++++++------- .../server/handler/StatisticsHandlerTest.java | 4 +-- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/jetty-server/src/main/config/etc/jetty-stats.xml b/jetty-server/src/main/config/etc/jetty-stats.xml index f8fd0ba9dff9..8ba66e60d701 100644 --- a/jetty-server/src/main/config/etc/jetty-stats.xml +++ b/jetty-server/src/main/config/etc/jetty-stats.xml @@ -6,7 +6,7 @@ - + diff --git a/jetty-server/src/main/config/modules/stats.mod b/jetty-server/src/main/config/modules/stats.mod index 23b0f16237ad..a6289c4174bb 100644 --- a/jetty-server/src/main/config/modules/stats.mod +++ b/jetty-server/src/main/config/modules/stats.mod @@ -19,4 +19,4 @@ jetty.webapp.addServerClasses+=,-org.eclipse.jetty.servlet.StatisticsServlet [ini-template] ## If the Graceful shutdown should wait for async requests as well as the currently dispatched ones. -# jetty.statistics.asyncGraceful=true +# jetty.statistics.gracefulShutdownWaitsForRequests=true diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/StatisticsHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/StatisticsHandler.java index 70650b7a04cc..0f9c0e5076a4 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/StatisticsHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/StatisticsHandler.java @@ -67,7 +67,7 @@ public class StatisticsHandler extends HandlerWrapper implements Graceful private final LongAdder _responses5xx = new LongAdder(); private final LongAdder _responsesTotalBytes = new LongAdder(); - private boolean asyncGraceful = true; + private boolean _gracefulShutdownWaitsForRequests = true; private final Graceful.Shutdown _shutdown = new Graceful.Shutdown() { @@ -113,8 +113,7 @@ public void onComplete(AsyncEvent event) throws IOException _asyncWaitStats.decrement(); - // If we have no more dispatches, should we signal shutdown? - if (numRequests == 0 && asyncGraceful) + if (numRequests == 0 && _gracefulShutdownWaitsForRequests) { FutureCallback shutdown = _shutdown.get(); if (shutdown != null) @@ -207,9 +206,7 @@ public void handle(String path, Request baseRequest, HttpServletRequest request, if (shutdown != null) { response.flushBuffer(); - - // If we either have no more requests or dispatches, we can complete shutdown. - if (asyncGraceful ? (numRequests == 0) : (numDispatches == 0)) + if (_gracefulShutdownWaitsForRequests ? (numRequests == 0) : (numDispatches == 0)) shutdown.succeeded(); } } @@ -268,11 +265,23 @@ protected void doStop() throws Exception * Set whether the graceful shutdown should wait for all requests to complete including * async requests which are not currently dispatched, or whether it should only wait for all the * actively dispatched requests to complete. - * @param asyncGraceful true to wait for async requests on graceful shutdown. + * @param gracefulShutdownWaitsForRequests true to wait for async requests on graceful shutdown. + */ + public void setGracefulShutdownWaitsForRequests(boolean gracefulShutdownWaitsForRequests) + { + _gracefulShutdownWaitsForRequests = gracefulShutdownWaitsForRequests; + } + + /** + * @return whether the graceful shutdown will wait for all requests to complete including + * async requests which are not currently dispatched, or whether it will only wait for all the + * actively dispatched requests to complete. + * @see #getAsyncDispatches() */ - public void setAsyncGraceful(boolean asyncGraceful) + @ManagedAttribute("if graceful shutdown will wait for async requests") + public boolean getGracefulShutdownWaitsForRequests() { - this.asyncGraceful = asyncGraceful; + return _gracefulShutdownWaitsForRequests; } /** diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/StatisticsHandlerTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/StatisticsHandlerTest.java index c4bbc702cf9a..7ce2b5b0718c 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/StatisticsHandlerTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/StatisticsHandlerTest.java @@ -431,7 +431,7 @@ public void waitForSuspendedRequestTest() throws Exception CyclicBarrier barrier = new CyclicBarrier(3); final AtomicReference asyncHolder = new AtomicReference<>(); final CountDownLatch dispatched = new CountDownLatch(1); - _statsHandler.setAsyncGraceful(true); + _statsHandler.setGracefulShutdownWaitsForRequests(true); _statsHandler.setHandler(new AbstractHandler() { @Override @@ -487,7 +487,7 @@ public void doNotWaitForSuspendedRequestTest() throws Exception CyclicBarrier barrier = new CyclicBarrier(3); final AtomicReference asyncHolder = new AtomicReference<>(); final CountDownLatch dispatched = new CountDownLatch(1); - _statsHandler.setAsyncGraceful(false); + _statsHandler.setGracefulShutdownWaitsForRequests(false); _statsHandler.setHandler(new AbstractHandler() { @Override