use of java.util.concurrent.CyclicBarrier in project elasticsearch by elastic.
the class RemoteClusterConnectionTests method testTriggerUpdatesConcurrently.
public void testTriggerUpdatesConcurrently() throws IOException, InterruptedException {
List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>();
try (MockTransportService seedTransport = startTransport("seed_node", knownNodes, Version.CURRENT);
MockTransportService seedTransport1 = startTransport("seed_node_1", knownNodes, Version.CURRENT);
MockTransportService discoverableTransport = startTransport("discoverable_node", knownNodes, Version.CURRENT)) {
DiscoveryNode seedNode = seedTransport.getLocalDiscoNode();
DiscoveryNode discoverableNode = discoverableTransport.getLocalDiscoNode();
DiscoveryNode seedNode1 = seedTransport1.getLocalDiscoNode();
knownNodes.add(seedTransport.getLocalDiscoNode());
knownNodes.add(discoverableTransport.getLocalDiscoNode());
knownNodes.add(seedTransport1.getLocalDiscoNode());
Collections.shuffle(knownNodes, random());
List<DiscoveryNode> seedNodes = Arrays.asList(seedNode1, seedNode);
Collections.shuffle(seedNodes, random());
try (MockTransportService service = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool, null)) {
service.start();
service.acceptIncomingRequests();
try (RemoteClusterConnection connection = new RemoteClusterConnection(Settings.EMPTY, "test-cluster", seedNodes, service, Integer.MAX_VALUE, n -> true)) {
int numThreads = randomIntBetween(4, 10);
Thread[] threads = new Thread[numThreads];
CyclicBarrier barrier = new CyclicBarrier(numThreads);
for (int i = 0; i < threads.length; i++) {
final int numConnectionAttempts = randomIntBetween(10, 200);
threads[i] = new Thread() {
@Override
public void run() {
try {
barrier.await();
CountDownLatch latch = new CountDownLatch(numConnectionAttempts);
for (int i = 0; i < numConnectionAttempts; i++) {
AtomicBoolean executed = new AtomicBoolean(false);
ActionListener<Void> listener = ActionListener.wrap(x -> {
assertTrue(executed.compareAndSet(false, true));
latch.countDown();
}, x -> {
assertTrue(executed.compareAndSet(false, true));
latch.countDown();
if (x instanceof RejectedExecutionException) {
} else {
throw new AssertionError(x);
}
});
connection.updateSeedNodes(seedNodes, listener);
}
latch.await();
} catch (Exception ex) {
throw new AssertionError(ex);
}
}
};
threads[i].start();
}
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
assertTrue(service.nodeConnected(seedNode));
assertTrue(service.nodeConnected(discoverableNode));
assertTrue(service.nodeConnected(seedNode1));
assertTrue(connection.assertNoRunningConnections());
}
}
}
}
use of java.util.concurrent.CyclicBarrier in project elasticsearch by elastic.
the class AdapterActionFutureTests method testInterruption.
public void testInterruption() throws Exception {
final AdapterActionFuture<String, Integer> adapter = new AdapterActionFuture<String, Integer>() {
@Override
protected String convert(final Integer listenerResponse) {
return Objects.toString(listenerResponse);
}
};
// test all possible methods that can be interrupted
final Runnable runnable = () -> {
final int method = randomIntBetween(0, 4);
switch(method) {
case 0:
adapter.actionGet();
break;
case 1:
adapter.actionGet("30s");
break;
case 2:
adapter.actionGet(30000);
break;
case 3:
adapter.actionGet(TimeValue.timeValueSeconds(30));
break;
case 4:
adapter.actionGet(30, TimeUnit.SECONDS);
break;
default:
throw new AssertionError(method);
}
};
final CyclicBarrier barrier = new CyclicBarrier(2);
final Thread main = Thread.currentThread();
final Thread thread = new Thread(() -> {
try {
barrier.await();
} catch (final BrokenBarrierException | InterruptedException e) {
throw new RuntimeException(e);
}
main.interrupt();
});
thread.start();
final AtomicBoolean interrupted = new AtomicBoolean();
barrier.await();
try {
runnable.run();
} catch (final IllegalStateException e) {
interrupted.set(Thread.interrupted());
}
// we check this here instead of in the catch block to ensure that the catch block executed
assertTrue(interrupted.get());
thread.join();
}
use of java.util.concurrent.CyclicBarrier in project elasticsearch by elastic.
the class BulkWithUpdatesIT method testFailingVersionedUpdatedOnBulk.
public void testFailingVersionedUpdatedOnBulk() throws Exception {
createIndex("test");
index("test", "type", "1", "field", "1");
final BulkResponse[] responses = new BulkResponse[30];
final CyclicBarrier cyclicBarrier = new CyclicBarrier(responses.length);
Thread[] threads = new Thread[responses.length];
for (int i = 0; i < responses.length; i++) {
final int threadID = i;
threads[threadID] = new Thread(() -> {
try {
cyclicBarrier.await();
} catch (Exception e) {
return;
}
BulkRequestBuilder requestBuilder = client().prepareBulk();
requestBuilder.add(client().prepareUpdate("test", "type", "1").setVersion(1).setDoc(Requests.INDEX_CONTENT_TYPE, "field", threadID));
responses[threadID] = requestBuilder.get();
});
threads[threadID].start();
}
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
int successes = 0;
for (BulkResponse response : responses) {
if (!response.hasFailures()) {
successes++;
}
}
assertThat(successes, equalTo(1));
}
use of java.util.concurrent.CyclicBarrier in project jetty.project by eclipse.
the class SameNodeLoadTest method testLoad.
@Test
@Slow
public void testLoad() throws Exception {
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
String contextPath = "";
String servletMapping = "/server";
TestServer server1 = new TestServer(0, -1, 4, cacheFactory, storeFactory);
server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
try {
server1.start();
int port1 = server1.getPort();
HttpClient client = new HttpClient();
client.start();
try {
String url = "http://localhost:" + port1 + contextPath + servletMapping;
//create session via first server
ContentResponse response1 = client.GET(url + "?action=init");
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
//simulate 10 clients making 100 requests each
ExecutorService executor = Executors.newCachedThreadPool();
int clientsCount = 10;
CyclicBarrier barrier = new CyclicBarrier(clientsCount + 1);
int requestsCount = 100;
Worker[] workers = new Worker[clientsCount];
for (int i = 0; i < clientsCount; ++i) {
workers[i] = new Worker(barrier, client, requestsCount, sessionCookie, url);
executor.execute(workers[i]);
}
// Wait for all workers to be ready
barrier.await();
long start = System.nanoTime();
// Wait for all workers to be done
barrier.await();
long end = System.nanoTime();
long elapsed = TimeUnit.NANOSECONDS.toMillis(end - start);
System.err.println("Elapsed ms:" + elapsed);
executor.shutdownNow();
// Perform one request to get the result
Request request = client.newRequest(url + "?action=result");
request.header("Cookie", sessionCookie);
ContentResponse response2 = request.send();
assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
String response = response2.getContentAsString();
assertEquals(response.trim(), String.valueOf(clientsCount * requestsCount));
} finally {
client.stop();
}
} finally {
server1.stop();
}
}
use of java.util.concurrent.CyclicBarrier in project elastic-job by dangdangdotcom.
the class ExecutorServiceHandlerRegistryTest method assertGetExecutorServiceHandlerForConcurrent.
@Test
public void assertGetExecutorServiceHandlerForConcurrent() throws InterruptedException {
int threadCount = 100;
CyclicBarrier barrier = new CyclicBarrier(threadCount);
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
CountDownLatch latch = new CountDownLatch(threadCount);
Set<ExecutorService> set = new CopyOnWriteArraySet<>();
for (int i = 0; i < threadCount; i++) {
executorService.submit(new GetExecutorServiceHandlerTask(barrier, latch, set));
}
latch.await();
assertThat(set.size(), is(1));
assertThat(ExecutorServiceHandlerRegistry.getExecutorServiceHandler("test_job", new DefaultExecutorServiceHandler()), is(set.iterator().next()));
}
Aggregations