Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2717 - Async requests are not considered when shutting down gracefully. #2721

Merged
merged 1 commit into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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