Skip to content

Commit

Permalink
Issue #5105 - change asyncGraceful to gracefulShutdownWaitsForRequests
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Aug 26, 2020
1 parent 8edb768 commit a9c90d3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion jetty-server/src/main/config/etc/jetty-stats.xml
Expand Up @@ -6,7 +6,7 @@
<Call name="insertHandler">
<Arg>
<New id="StatsHandler" class="org.eclipse.jetty.server.handler.StatisticsHandler">
<Set name="asyncGraceful"><Property name="jetty.statistics.asyncGraceful" default="true"/></Set>
<Set name="gracefulShutdownWaitsForRequests"><Property name="jetty.statistics.gracefulShutdownWaitsForRequests" default="true"/></Set>
</New>
</Arg>
</Call>
Expand Down
2 changes: 1 addition & 1 deletion jetty-server/src/main/config/modules/stats.mod
Expand Up @@ -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
Expand Up @@ -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()
{
Expand All @@ -81,25 +81,25 @@ protected FutureCallback newShutdownCallback()
private final AsyncListener _onCompletion = new AsyncListener()
{
@Override
public void onStartAsync(AsyncEvent event) throws IOException
public void onStartAsync(AsyncEvent event)
{
event.getAsyncContext().addListener(this);
}

@Override
public void onTimeout(AsyncEvent event) throws IOException
public void onTimeout(AsyncEvent event)
{
_expires.increment();
}

@Override
public void onError(AsyncEvent event) throws IOException
public void onError(AsyncEvent event)
{
_errors.increment();
}

@Override
public void onComplete(AsyncEvent event) throws IOException
public void onComplete(AsyncEvent event)
{
HttpChannelState state = ((AsyncContextEvent)event).getHttpChannelState();

Expand All @@ -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)
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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 all requests")
public boolean getGracefulShutdownWaitsForRequests()
{
this.asyncGraceful = asyncGraceful;
return _gracefulShutdownWaitsForRequests;
}

/**
Expand Down
Expand Up @@ -431,7 +431,7 @@ public void waitForSuspendedRequestTest() throws Exception
CyclicBarrier barrier = new CyclicBarrier(3);
final AtomicReference<AsyncContext> asyncHolder = new AtomicReference<>();
final CountDownLatch dispatched = new CountDownLatch(1);
_statsHandler.setAsyncGraceful(true);
_statsHandler.setGracefulShutdownWaitsForRequests(true);
_statsHandler.setHandler(new AbstractHandler()
{
@Override
Expand Down Expand Up @@ -487,7 +487,7 @@ public void doNotWaitForSuspendedRequestTest() throws Exception
CyclicBarrier barrier = new CyclicBarrier(3);
final AtomicReference<AsyncContext> asyncHolder = new AtomicReference<>();
final CountDownLatch dispatched = new CountDownLatch(1);
_statsHandler.setAsyncGraceful(false);
_statsHandler.setGracefulShutdownWaitsForRequests(false);
_statsHandler.setHandler(new AbstractHandler()
{
@Override
Expand Down

0 comments on commit a9c90d3

Please sign in to comment.