Skip to content

Commit

Permalink
Fixes #2717 - Async requests are not considered when shutting down gr…
Browse files Browse the repository at this point in the history
…acefully.

Now using _requestStats instead of _dispatchedStats to check for
requests completed when shutting down StatisticsHandler.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Jul 16, 2018
1 parent 9c15796 commit cc1071f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ public Future<Void> shutdown()
FutureCallback shutdown=new FutureCallback(false);
_shutdown.compareAndSet(null,shutdown);
shutdown=_shutdown.get();
if (_dispatchedStats.getCurrent()==0)
if (_requestStats.getCurrent()==0)
shutdown.succeeded();
return shutdown;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -31,6 +32,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ConnectionStatistics;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Request;
Expand All @@ -41,6 +43,7 @@

import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -85,7 +88,7 @@ public void testRequest() throws Exception
_statsHandler.setHandler(new AbstractHandler()
{
@Override
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException
{
request.setHandled(true);
try
Expand All @@ -97,7 +100,7 @@ public void handle(String path, Request request, HttpServletRequest httpRequest,
catch (Exception x)
{
Thread.currentThread().interrupt();
throw (IOException)new IOException().initCause(x);
throw new IOException(x);
}
}
});
Expand Down Expand Up @@ -182,7 +185,7 @@ public void testTwoRequests() throws Exception
_statsHandler.setHandler(new AbstractHandler()
{
@Override
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException
{
request.setHandled(true);
try
Expand All @@ -193,7 +196,7 @@ public void handle(String path, Request request, HttpServletRequest httpRequest,
catch (Exception x)
{
Thread.currentThread().interrupt();
throw (IOException)new IOException().initCause(x);
throw new IOException(x);
}
}
});
Expand Down Expand Up @@ -245,7 +248,7 @@ public void testSuspendResume() throws Exception
_statsHandler.setHandler(new AbstractHandler()
{
@Override
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException
{
request.setHandled(true);
try
Expand Down Expand Up @@ -306,22 +309,22 @@ public void handle(String path, Request request, HttpServletRequest httpRequest,
asyncHolder.get().addListener(new AsyncListener()
{
@Override
public void onTimeout(AsyncEvent event) throws IOException
public void onTimeout(AsyncEvent event)
{
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException
public void onStartAsync(AsyncEvent event)
{
}

@Override
public void onError(AsyncEvent event) throws IOException
public void onError(AsyncEvent event)
{
}

@Override
public void onComplete(AsyncEvent event) throws IOException
public void onComplete(AsyncEvent event)
{
try
{
Expand Down Expand Up @@ -375,7 +378,7 @@ public void testSuspendExpire() throws Exception
_statsHandler.setHandler(new AbstractHandler()
{
@Override
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException
{
request.setHandled(true);
try
Expand Down Expand Up @@ -428,23 +431,23 @@ public void handle(String path, Request request, HttpServletRequest httpRequest,
asyncHolder.get().addListener(new AsyncListener()
{
@Override
public void onTimeout(AsyncEvent event) throws IOException
public void onTimeout(AsyncEvent event)
{
event.getAsyncContext().complete();
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException
public void onStartAsync(AsyncEvent event)
{
}

@Override
public void onError(AsyncEvent event) throws IOException
public void onError(AsyncEvent event)
{
}

@Override
public void onComplete(AsyncEvent event) throws IOException
public void onComplete(AsyncEvent event)
{
try
{
Expand Down Expand Up @@ -491,7 +494,7 @@ public void testSuspendComplete() throws Exception
_statsHandler.setHandler(new AbstractHandler()
{
@Override
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException
{
request.setHandled(true);
try
Expand Down Expand Up @@ -550,22 +553,22 @@ public void handle(String path, Request request, HttpServletRequest httpRequest,
asyncHolder.get().addListener(new AsyncListener()
{
@Override
public void onTimeout(AsyncEvent event) throws IOException
public void onTimeout(AsyncEvent event)
{
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException
public void onStartAsync(AsyncEvent event)
{
}

@Override
public void onError(AsyncEvent event) throws IOException
public void onError(AsyncEvent event)
{
}

@Override
public void onComplete(AsyncEvent event) throws IOException
public void onComplete(AsyncEvent event)
{
try
{
Expand Down Expand Up @@ -601,6 +604,53 @@ public void onComplete(AsyncEvent event) throws IOException
assertEquals(_statsHandler.getDispatchedTimeTotal(), _statsHandler.getDispatchedTimeMean(), 0.01);
}

@Test
public void testAsyncRequestWithShutdown() throws Exception
{
long delay = 500;
CountDownLatch serverLatch = new CountDownLatch(1);
_statsHandler.setHandler(new AbstractHandler()
{
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
{
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
new Thread(() ->
{
try
{
Thread.sleep(delay);
asyncContext.complete();
}
catch (InterruptedException e)
{
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
asyncContext.complete();
}
}).start();
serverLatch.countDown();
}
});
_server.start();

String request = "GET / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"\r\n";
_connector.executeRequest(request);

assertTrue(serverLatch.await(5, TimeUnit.SECONDS));

Future<Void> shutdown = _statsHandler.shutdown();
assertFalse(shutdown.isDone());

Thread.sleep(delay / 2);
assertFalse(shutdown.isDone());

Thread.sleep(delay);
assertTrue(shutdown.isDone());
}

/**
* This handler is external to the statistics handler and it is used to ensure that statistics handler's
* handle() is fully executed before asserting its values in the tests, to avoid race conditions with the
Expand Down

0 comments on commit cc1071f

Please sign in to comment.