use of java.util.concurrent.BrokenBarrierException in project elasticsearch by elastic.
the class InternalEngineTests method testSequenceNumberAdvancesToMaxSeqOnEngineOpenOnPrimary.
public void testSequenceNumberAdvancesToMaxSeqOnEngineOpenOnPrimary() throws BrokenBarrierException, InterruptedException, IOException {
engine.close();
final int docs = randomIntBetween(1, 32);
InternalEngine initialEngine = null;
try {
final CountDownLatch latch = new CountDownLatch(1);
final CyclicBarrier barrier = new CyclicBarrier(2);
final AtomicBoolean skip = new AtomicBoolean();
final AtomicLong expectedLocalCheckpoint = new AtomicLong(SequenceNumbersService.NO_OPS_PERFORMED);
final List<Thread> threads = new ArrayList<>();
final SequenceNumbersService seqNoService = new SequenceNumbersService(shardId, defaultSettings, SequenceNumbersService.NO_OPS_PERFORMED, SequenceNumbersService.NO_OPS_PERFORMED, SequenceNumbersService.UNASSIGNED_SEQ_NO) {
@Override
public long generateSeqNo() {
final long seqNo = super.generateSeqNo();
if (skip.get()) {
try {
barrier.await();
latch.await();
} catch (BrokenBarrierException | InterruptedException e) {
throw new RuntimeException(e);
}
} else {
if (expectedLocalCheckpoint.get() + 1 == seqNo) {
expectedLocalCheckpoint.set(seqNo);
}
}
return seqNo;
}
};
initialEngine = createEngine(defaultSettings, store, primaryTranslogDir, newMergePolicy(), null, () -> seqNoService);
final InternalEngine finalInitialEngine = initialEngine;
for (int i = 0; i < docs; i++) {
final String id = Integer.toString(i);
final ParsedDocument doc = testParsedDocument(id, "test", null, testDocumentWithTextField(), SOURCE, null);
skip.set(randomBoolean());
final Thread thread = new Thread(() -> {
try {
finalInitialEngine.index(indexForDoc(doc));
} catch (IOException e) {
throw new AssertionError(e);
}
});
thread.start();
if (skip.get()) {
threads.add(thread);
barrier.await();
} else {
thread.join();
}
}
assertThat(initialEngine.seqNoService().getLocalCheckpoint(), equalTo(expectedLocalCheckpoint.get()));
assertThat(initialEngine.seqNoService().getMaxSeqNo(), equalTo((long) (docs - 1)));
initialEngine.flush(true, true);
latch.countDown();
for (final Thread thread : threads) {
thread.join();
}
} finally {
IOUtils.close(initialEngine);
}
try (Engine recoveringEngine = new InternalEngine(copy(initialEngine.config(), EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG))) {
assertThat(recoveringEngine.seqNoService().getLocalCheckpoint(), greaterThanOrEqualTo((long) (docs - 1)));
}
}
use of java.util.concurrent.BrokenBarrierException in project elasticsearch by elastic.
the class TasksIT method testNodeNotFoundButTaskFound.
public void testNodeNotFoundButTaskFound() throws Exception {
// Save a fake task that looks like it is from a node that isn't part of the cluster
CyclicBarrier b = new CyclicBarrier(2);
TaskResultsService resultsService = internalCluster().getInstance(TaskResultsService.class);
resultsService.storeResult(new TaskResult(new TaskInfo(new TaskId("fake", 1), "test", "test", "", null, 0, 0, false, TaskId.EMPTY_TASK_ID), new RuntimeException("test")), new ActionListener<Void>() {
@Override
public void onResponse(Void response) {
try {
b.await();
} catch (InterruptedException | BrokenBarrierException e) {
onFailure(e);
}
}
@Override
public void onFailure(Exception e) {
throw new RuntimeException(e);
}
});
b.await();
// Now we can find it!
GetTaskResponse response = expectFinishedTask(new TaskId("fake:1"));
assertEquals("test", response.getTask().getTask().getAction());
assertNotNull(response.getTask().getError());
assertNull(response.getTask().getResponse());
}
use of java.util.concurrent.BrokenBarrierException in project elasticsearch by elastic.
the class RemoteClusterConnectionTests method testCloseWhileConcurrentlyConnecting.
public void testCloseWhileConcurrentlyConnecting() throws IOException, InterruptedException, BrokenBarrierException {
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 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 + 1);
for (int i = 0; i < threads.length; i++) {
final int numConnectionAttempts = randomIntBetween(10, 100);
threads[i] = new Thread() {
@Override
public void run() {
try {
barrier.await();
CountDownLatch latch = new CountDownLatch(numConnectionAttempts);
for (int i = 0; i < numConnectionAttempts; i++) {
AtomicReference<RuntimeException> executed = new AtomicReference<>();
ActionListener<Void> listener = ActionListener.wrap(x -> {
if (executed.compareAndSet(null, new RuntimeException())) {
latch.countDown();
} else {
throw new AssertionError("shit's been called twice", executed.get());
}
}, x -> {
if (executed.compareAndSet(null, new RuntimeException())) {
latch.countDown();
} else {
throw new AssertionError("shit's been called twice", executed.get());
}
if (x instanceof RejectedExecutionException || x instanceof AlreadyClosedException || x instanceof CancellableThreads.ExecutionCancelledException) {
} else {
throw new AssertionError(x);
}
});
connection.updateSeedNodes(seedNodes, listener);
}
latch.await();
} catch (Exception ex) {
throw new AssertionError(ex);
}
}
};
threads[i].start();
}
barrier.await();
connection.close();
}
}
}
}
use of java.util.concurrent.BrokenBarrierException 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.BrokenBarrierException in project jersey by jersey.
the class ParallelTest method testParallel.
@Test
public void testParallel() throws BrokenBarrierException, InterruptedException, TimeoutException {
final ScheduledExecutorService executor = Executors.newScheduledThreadPool(PARALLEL_CLIENTS);
try {
final WebTarget target = target();
for (int i = 1; i <= PARALLEL_CLIENTS; i++) {
final int id = i;
executor.submit(new Runnable() {
@Override
public void run() {
try {
startBarrier.await();
Response response;
response = target.path(PATH).request().get();
assertEquals("GET", response.readEntity(String.class));
receivedCounter.incrementAndGet();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
LOGGER.log(Level.WARNING, "Client thread " + id + " interrupted.", ex);
} catch (BrokenBarrierException ex) {
LOGGER.log(Level.INFO, "Client thread " + id + " failed on broken barrier.", ex);
} catch (Throwable t) {
t.printStackTrace();
LOGGER.log(Level.WARNING, "Client thread " + id + " failed on unexpected exception.", t);
} finally {
doneLatch.countDown();
}
}
});
}
startBarrier.await(1, TimeUnit.SECONDS);
assertTrue("Waiting for clients to finish has timed out.", doneLatch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
assertEquals("Resource counter", PARALLEL_CLIENTS, resourceCounter.get());
assertEquals("Received counter", PARALLEL_CLIENTS, receivedCounter.get());
} finally {
executor.shutdownNow();
Assert.assertTrue("Executor termination", executor.awaitTermination(5, TimeUnit.SECONDS));
}
}
Aggregations