use of java.util.concurrent.CyclicBarrier in project jetty.project by eclipse.
the class StatisticsHandlerTest method testSuspendResume.
@Test
public void testSuspendResume() throws Exception {
final long dispatchTime = 10;
final long requestTime = 50;
final AtomicReference<AsyncContext> asyncHolder = new AtomicReference<>();
final CyclicBarrier[] barrier = { new CyclicBarrier(2), new CyclicBarrier(2), new CyclicBarrier(2) };
_statsHandler.setHandler(new AbstractHandler() {
@Override
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
request.setHandled(true);
try {
barrier[0].await();
Thread.sleep(dispatchTime);
if (asyncHolder.get() == null)
asyncHolder.set(request.startAsync());
} catch (Exception x) {
throw new ServletException(x);
} finally {
try {
barrier[1].await();
} catch (Exception ignored) {
}
}
}
});
_server.start();
String request = "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
_connector.executeRequest(request);
barrier[0].await();
assertEquals(1, _statistics.getConnections());
assertEquals(1, _statsHandler.getRequests());
assertEquals(1, _statsHandler.getRequestsActive());
assertEquals(1, _statsHandler.getDispatched());
assertEquals(1, _statsHandler.getDispatchedActive());
barrier[1].await();
assertTrue(_latchHandler.await());
assertNotNull(asyncHolder.get());
assertEquals(1, _statsHandler.getRequests());
assertEquals(1, _statsHandler.getRequestsActive());
assertEquals(1, _statsHandler.getDispatched());
assertEquals(0, _statsHandler.getDispatchedActive());
_latchHandler.reset();
barrier[0].reset();
barrier[1].reset();
Thread.sleep(requestTime);
asyncHolder.get().addListener(new AsyncListener() {
@Override
public void onTimeout(AsyncEvent event) throws IOException {
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
}
@Override
public void onError(AsyncEvent event) throws IOException {
}
@Override
public void onComplete(AsyncEvent event) throws IOException {
try {
barrier[2].await();
} catch (Exception ignored) {
}
}
});
asyncHolder.get().dispatch();
// entered app handler
barrier[0].await();
assertEquals(1, _statistics.getConnections());
assertEquals(1, _statsHandler.getRequests());
assertEquals(1, _statsHandler.getRequestsActive());
assertEquals(2, _statsHandler.getDispatched());
assertEquals(1, _statsHandler.getDispatchedActive());
// exiting app handler
barrier[1].await();
// exited stats handler
assertTrue(_latchHandler.await());
// onComplete called
barrier[2].await();
assertEquals(1, _statsHandler.getRequests());
assertEquals(0, _statsHandler.getRequestsActive());
assertEquals(2, _statsHandler.getDispatched());
assertEquals(0, _statsHandler.getDispatchedActive());
assertEquals(1, _statsHandler.getAsyncRequests());
assertEquals(1, _statsHandler.getAsyncDispatches());
assertEquals(0, _statsHandler.getExpires());
assertEquals(1, _statsHandler.getResponses2xx());
assertThat(_statsHandler.getRequestTimeTotal(), greaterThanOrEqualTo(requestTime * 3 / 4));
assertEquals(_statsHandler.getRequestTimeTotal(), _statsHandler.getRequestTimeMax());
assertEquals(_statsHandler.getRequestTimeTotal(), _statsHandler.getRequestTimeMean(), 0.01);
assertThat(_statsHandler.getDispatchedTimeTotal(), greaterThanOrEqualTo(dispatchTime * 2 * 3 / 4));
assertTrue(_statsHandler.getDispatchedTimeMean() + dispatchTime <= _statsHandler.getDispatchedTimeTotal());
assertTrue(_statsHandler.getDispatchedTimeMax() + dispatchTime <= _statsHandler.getDispatchedTimeTotal());
}
use of java.util.concurrent.CyclicBarrier in project jetty.project by eclipse.
the class StatisticsHandlerTest method testTwoRequests.
@Test
public void testTwoRequests() throws Exception {
final CyclicBarrier[] barrier = { new CyclicBarrier(3), new CyclicBarrier(3) };
_latchHandler.reset(2);
_statsHandler.setHandler(new AbstractHandler() {
@Override
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
request.setHandled(true);
try {
barrier[0].await();
barrier[1].await();
} catch (Exception x) {
Thread.currentThread().interrupt();
throw (IOException) new IOException().initCause(x);
}
}
});
_server.start();
String request = "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
_connector.executeRequest(request);
_connector.executeRequest(request);
barrier[0].await();
assertEquals(2, _statistics.getConnections());
assertEquals(2, _statsHandler.getRequests());
assertEquals(2, _statsHandler.getRequestsActive());
assertEquals(2, _statsHandler.getRequestsActiveMax());
assertEquals(2, _statsHandler.getDispatched());
assertEquals(2, _statsHandler.getDispatchedActive());
assertEquals(2, _statsHandler.getDispatchedActiveMax());
barrier[1].await();
assertTrue(_latchHandler.await());
assertEquals(2, _statsHandler.getRequests());
assertEquals(0, _statsHandler.getRequestsActive());
assertEquals(2, _statsHandler.getRequestsActiveMax());
assertEquals(2, _statsHandler.getDispatched());
assertEquals(0, _statsHandler.getDispatchedActive());
assertEquals(2, _statsHandler.getDispatchedActiveMax());
assertEquals(0, _statsHandler.getAsyncRequests());
assertEquals(0, _statsHandler.getAsyncDispatches());
assertEquals(0, _statsHandler.getExpires());
assertEquals(2, _statsHandler.getResponses2xx());
}
use of java.util.concurrent.CyclicBarrier in project jetty.project by eclipse.
the class StatisticsHandlerTest method testSuspendComplete.
@Test
public void testSuspendComplete() throws Exception {
final long dispatchTime = 10;
final AtomicReference<AsyncContext> asyncHolder = new AtomicReference<>();
final CyclicBarrier[] barrier = { new CyclicBarrier(2), new CyclicBarrier(2) };
final CountDownLatch latch = new CountDownLatch(1);
_statsHandler.setHandler(new AbstractHandler() {
@Override
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
request.setHandled(true);
try {
barrier[0].await();
Thread.sleep(dispatchTime);
if (asyncHolder.get() == null) {
AsyncContext async = request.startAsync();
asyncHolder.set(async);
}
} catch (Exception x) {
throw new ServletException(x);
} finally {
try {
barrier[1].await();
} catch (Exception ignored) {
}
}
}
});
_server.start();
String request = "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
_connector.executeRequest(request);
barrier[0].await();
assertEquals(1, _statistics.getConnections());
assertEquals(1, _statsHandler.getRequests());
assertEquals(1, _statsHandler.getRequestsActive());
assertEquals(1, _statsHandler.getDispatched());
assertEquals(1, _statsHandler.getDispatchedActive());
barrier[1].await();
assertTrue(_latchHandler.await());
assertNotNull(asyncHolder.get());
assertEquals(1, _statsHandler.getRequests());
assertEquals(1, _statsHandler.getRequestsActive());
assertEquals(1, _statsHandler.getDispatched());
assertEquals(0, _statsHandler.getDispatchedActive());
asyncHolder.get().addListener(new AsyncListener() {
@Override
public void onTimeout(AsyncEvent event) throws IOException {
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
}
@Override
public void onError(AsyncEvent event) throws IOException {
}
@Override
public void onComplete(AsyncEvent event) throws IOException {
try {
latch.countDown();
} catch (Exception ignored) {
}
}
});
long requestTime = 20;
Thread.sleep(requestTime);
asyncHolder.get().complete();
latch.await();
assertEquals(1, _statsHandler.getRequests());
assertEquals(0, _statsHandler.getRequestsActive());
assertEquals(1, _statsHandler.getDispatched());
assertEquals(0, _statsHandler.getDispatchedActive());
assertEquals(1, _statsHandler.getAsyncRequests());
assertEquals(0, _statsHandler.getAsyncDispatches());
assertEquals(0, _statsHandler.getExpires());
assertEquals(1, _statsHandler.getResponses2xx());
assertTrue(_statsHandler.getRequestTimeTotal() >= (dispatchTime + requestTime) * 3 / 4);
assertEquals(_statsHandler.getRequestTimeTotal(), _statsHandler.getRequestTimeMax());
assertEquals(_statsHandler.getRequestTimeTotal(), _statsHandler.getRequestTimeMean(), 0.01);
assertTrue(_statsHandler.getDispatchedTimeTotal() >= dispatchTime * 3 / 4);
assertTrue(_statsHandler.getDispatchedTimeTotal() < _statsHandler.getRequestTimeTotal());
assertEquals(_statsHandler.getDispatchedTimeTotal(), _statsHandler.getDispatchedTimeMax());
assertEquals(_statsHandler.getDispatchedTimeTotal(), _statsHandler.getDispatchedTimeMean(), 0.01);
}
use of java.util.concurrent.CyclicBarrier in project jetty.project by eclipse.
the class CounterStatisticTest method testCounterContended.
@Test
public void testCounterContended() throws Exception {
final CounterStatistic counter = new CounterStatistic();
final int N = 100;
final int L = 1000;
final Thread[] threads = new Thread[N];
final CyclicBarrier incBarrier = new CyclicBarrier(N);
final CountDownLatch decBarrier = new CountDownLatch(N / 2);
for (int i = N; i-- > 0; ) {
final int I = i;
threads[i] = (i >= N / 2) ? new Thread() {
@Override
public void run() {
try {
incBarrier.await();
decBarrier.await();
} catch (Exception e) {
throw new RuntimeException(e);
}
Random random = new Random();
for (int l = L; l-- > 0; ) {
counter.decrement();
if (random.nextInt(5) == 0)
Thread.yield();
}
}
} : new Thread() {
@Override
public void run() {
try {
incBarrier.await();
} catch (Exception e) {
throw new RuntimeException(e);
}
Random random = new Random();
for (int l = L; l-- > 0; ) {
counter.increment();
if (l == L / 2)
decBarrier.countDown();
if (random.nextInt(5) == 0)
Thread.yield();
}
}
};
threads[i].start();
}
for (int i = N; i-- > 0; ) threads[i].join();
assertThat(counter.getCurrent(), equalTo(0L));
assertThat(counter.getTotal(), equalTo(N * L / 2L));
assertThat(counter.getMax(), greaterThanOrEqualTo((N / 2) * (L / 2L)));
}
use of java.util.concurrent.CyclicBarrier in project elasticsearch by elastic.
the class AbstractSimpleTransportTestCase method testConcurrentSendRespondAndDisconnect.
public void testConcurrentSendRespondAndDisconnect() throws BrokenBarrierException, InterruptedException {
Set<Exception> sendingErrors = ConcurrentCollections.newConcurrentSet();
Set<Exception> responseErrors = ConcurrentCollections.newConcurrentSet();
serviceA.registerRequestHandler("test", TestRequest::new, randomBoolean() ? ThreadPool.Names.SAME : ThreadPool.Names.GENERIC, (request, channel) -> {
try {
channel.sendResponse(new TestResponse());
} catch (Exception e) {
logger.info("caught exception while responding", e);
responseErrors.add(e);
}
});
final TransportRequestHandler<TestRequest> ignoringRequestHandler = (request, channel) -> {
try {
channel.sendResponse(new TestResponse());
} catch (Exception e) {
// we don't really care what's going on B, we're testing through A
logger.trace("caught exception while responding from node B", e);
}
};
serviceB.registerRequestHandler("test", TestRequest::new, ThreadPool.Names.SAME, ignoringRequestHandler);
int halfSenders = scaledRandomIntBetween(3, 10);
final CyclicBarrier go = new CyclicBarrier(halfSenders * 2 + 1);
final CountDownLatch done = new CountDownLatch(halfSenders * 2);
for (int i = 0; i < halfSenders; i++) {
// B senders just generated activity so serciveA can respond, we don't test what's going on there
final int sender = i;
threadPool.executor(ThreadPool.Names.GENERIC).execute(new AbstractRunnable() {
@Override
public void onFailure(Exception e) {
logger.trace("caught exception while sending from B", e);
}
@Override
protected void doRun() throws Exception {
go.await();
for (int iter = 0; iter < 10; iter++) {
PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
final String info = sender + "_B_" + iter;
serviceB.sendRequest(nodeA, "test", new TestRequest(info), new ActionListenerResponseHandler<>(listener, TestResponse::new));
try {
listener.actionGet();
} catch (Exception e) {
logger.trace((Supplier<?>) () -> new ParameterizedMessage("caught exception while sending to node {}", nodeA), e);
}
}
}
@Override
public void onAfter() {
done.countDown();
}
});
}
for (int i = 0; i < halfSenders; i++) {
final int sender = i;
threadPool.executor(ThreadPool.Names.GENERIC).execute(new AbstractRunnable() {
@Override
public void onFailure(Exception e) {
logger.error("unexpected error", e);
sendingErrors.add(e);
}
@Override
protected void doRun() throws Exception {
go.await();
for (int iter = 0; iter < 10; iter++) {
PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
final String info = sender + "_" + iter;
// capture now
final DiscoveryNode node = nodeB;
try {
serviceA.sendRequest(node, "test", new TestRequest(info), new ActionListenerResponseHandler<>(listener, TestResponse::new));
try {
listener.actionGet();
} catch (ConnectTransportException e) {
// ok!
} catch (Exception e) {
logger.error((Supplier<?>) () -> new ParameterizedMessage("caught exception while sending to node {}", node), e);
sendingErrors.add(e);
}
} catch (NodeNotConnectedException ex) {
// ok
}
}
}
@Override
public void onAfter() {
done.countDown();
}
});
}
go.await();
for (int i = 0; i <= 10; i++) {
if (i % 3 == 0) {
// simulate restart of nodeB
serviceB.close();
MockTransportService newService = buildService("TS_B_" + i, version1, null);
newService.registerRequestHandler("test", TestRequest::new, ThreadPool.Names.SAME, ignoringRequestHandler);
serviceB = newService;
nodeB = newService.getLocalDiscoNode();
serviceB.connectToNode(nodeA);
serviceA.connectToNode(nodeB);
} else if (serviceA.nodeConnected(nodeB)) {
serviceA.disconnectFromNode(nodeB);
} else {
serviceA.connectToNode(nodeB);
}
}
done.await();
assertThat("found non connection errors while sending", sendingErrors, empty());
assertThat("found non connection errors while responding", responseErrors, empty());
}
Aggregations