use of java.util.concurrent.ExecutorService in project elasticsearch by elastic.
the class PrioritizedExecutorsTests method testExecutePrioritizedExecutorWithRunnables.
public void testExecutePrioritizedExecutorWithRunnables() throws Exception {
ExecutorService executor = EsExecutors.newSinglePrioritizing(getTestName(), EsExecutors.daemonThreadFactory(getTestName()), holder);
List<Integer> results = new ArrayList<>(8);
CountDownLatch awaitingLatch = new CountDownLatch(1);
CountDownLatch finishedLatch = new CountDownLatch(8);
executor.execute(new AwaitingJob(awaitingLatch));
executor.execute(new Job(7, Priority.LANGUID, results, finishedLatch));
executor.execute(new Job(5, Priority.LOW, results, finishedLatch));
executor.execute(new Job(2, Priority.HIGH, results, finishedLatch));
// will execute after the first LOW (fifo)
executor.execute(new Job(6, Priority.LOW, results, finishedLatch));
executor.execute(new Job(1, Priority.URGENT, results, finishedLatch));
executor.execute(new Job(4, Priority.NORMAL, results, finishedLatch));
// will execute after the first HIGH (fifo)
executor.execute(new Job(3, Priority.HIGH, results, finishedLatch));
executor.execute(new Job(0, Priority.IMMEDIATE, results, finishedLatch));
awaitingLatch.countDown();
finishedLatch.await();
assertThat(results.size(), equalTo(8));
assertThat(results.get(0), equalTo(0));
assertThat(results.get(1), equalTo(1));
assertThat(results.get(2), equalTo(2));
assertThat(results.get(3), equalTo(3));
assertThat(results.get(4), equalTo(4));
assertThat(results.get(5), equalTo(5));
assertThat(results.get(6), equalTo(6));
assertThat(results.get(7), equalTo(7));
terminate(executor);
}
use of java.util.concurrent.ExecutorService in project elasticsearch by elastic.
the class PrioritizedExecutorsTests method testSubmitPrioritizedExecutorWithCallables.
public void testSubmitPrioritizedExecutorWithCallables() throws Exception {
ExecutorService executor = EsExecutors.newSinglePrioritizing(getTestName(), EsExecutors.daemonThreadFactory(getTestName()), holder);
List<Integer> results = new ArrayList<>(8);
CountDownLatch awaitingLatch = new CountDownLatch(1);
CountDownLatch finishedLatch = new CountDownLatch(8);
executor.submit(new AwaitingJob(awaitingLatch));
executor.submit(new CallableJob(7, Priority.LANGUID, results, finishedLatch));
executor.submit(new CallableJob(5, Priority.LOW, results, finishedLatch));
executor.submit(new CallableJob(2, Priority.HIGH, results, finishedLatch));
// will execute after the first LOW (fifo)
executor.submit(new CallableJob(6, Priority.LOW, results, finishedLatch));
executor.submit(new CallableJob(1, Priority.URGENT, results, finishedLatch));
executor.submit(new CallableJob(4, Priority.NORMAL, results, finishedLatch));
// will execute after the first HIGH (fifo)
executor.submit(new CallableJob(3, Priority.HIGH, results, finishedLatch));
executor.submit(new CallableJob(0, Priority.IMMEDIATE, results, finishedLatch));
awaitingLatch.countDown();
finishedLatch.await();
assertThat(results.size(), equalTo(8));
assertThat(results.get(0), equalTo(0));
assertThat(results.get(1), equalTo(1));
assertThat(results.get(2), equalTo(2));
assertThat(results.get(3), equalTo(3));
assertThat(results.get(4), equalTo(4));
assertThat(results.get(5), equalTo(5));
assertThat(results.get(6), equalTo(6));
assertThat(results.get(7), equalTo(7));
terminate(executor);
}
use of java.util.concurrent.ExecutorService in project elasticsearch by elastic.
the class PrioritizedExecutorsTests method testSubmitPrioritizedExecutorWithMixed.
public void testSubmitPrioritizedExecutorWithMixed() throws Exception {
ExecutorService executor = EsExecutors.newSinglePrioritizing(getTestName(), EsExecutors.daemonThreadFactory(getTestName()), holder);
List<Integer> results = new ArrayList<>(8);
CountDownLatch awaitingLatch = new CountDownLatch(1);
CountDownLatch finishedLatch = new CountDownLatch(8);
executor.submit(new AwaitingJob(awaitingLatch));
executor.submit(new CallableJob(7, Priority.LANGUID, results, finishedLatch));
executor.submit(new Job(5, Priority.LOW, results, finishedLatch));
executor.submit(new CallableJob(2, Priority.HIGH, results, finishedLatch));
// will execute after the first LOW (fifo)
executor.submit(new Job(6, Priority.LOW, results, finishedLatch));
executor.submit(new CallableJob(1, Priority.URGENT, results, finishedLatch));
executor.submit(new Job(4, Priority.NORMAL, results, finishedLatch));
// will execute after the first HIGH (fifo)
executor.submit(new CallableJob(3, Priority.HIGH, results, finishedLatch));
executor.submit(new Job(0, Priority.IMMEDIATE, results, finishedLatch));
awaitingLatch.countDown();
finishedLatch.await();
assertThat(results.size(), equalTo(8));
assertThat(results.get(0), equalTo(0));
assertThat(results.get(1), equalTo(1));
assertThat(results.get(2), equalTo(2));
assertThat(results.get(3), equalTo(3));
assertThat(results.get(4), equalTo(4));
assertThat(results.get(5), equalTo(5));
assertThat(results.get(6), equalTo(6));
assertThat(results.get(7), equalTo(7));
terminate(executor);
}
use of java.util.concurrent.ExecutorService in project hibernate-orm by hibernate.
the class CorrectnessTestCase method test.
@Test
public void test() throws Exception {
ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS);
Map<Integer, List<Log<String>>> allFamilyNames = new HashMap<>();
Map<Integer, List<Log<Set<String>>>> allFamilyMembers = new HashMap<>();
running = true;
List<Future<Void>> futures = new ArrayList<>();
for (int node = 0; node < NUM_NODES; ++node) {
final int NODE = node;
for (int i = 0; i < NUM_THREADS_PER_NODE; ++i) {
final int I = i;
futures.add(exec.submit(() -> {
Thread.currentThread().setName("Node" + (char) ('A' + NODE) + "-thread-" + I);
threadNode.set(NODE);
while (running) {
Operation operation;
if (familyIds.size() < NUM_FAMILIES) {
operation = new InsertFamily(ThreadLocalRandom.current().nextInt(5) == 0);
} else {
operation = getOperation();
}
try {
operation.run();
} catch (Exception e) {
// ignore exceptions from optimistic failures and induced exceptions
if (hasCause(e, InducedException.class)) {
continue;
} else if (Stream.of(EXPECTED).anyMatch(exceptions -> matches(e, exceptions))) {
continue;
}
exceptions.add(e);
log.error("Failed " + operation.getClass().getName(), e);
}
}
synchronized (allFamilyNames) {
for (Map.Entry<Integer, List<Log<String>>> entry : familyNames.get().entrySet()) {
List<Log<String>> list = allFamilyNames.get(entry.getKey());
if (list == null)
allFamilyNames.put(entry.getKey(), list = new ArrayList<>());
list.addAll(entry.getValue());
}
for (Map.Entry<Integer, List<Log<Set<String>>>> entry : familyMembers.get().entrySet()) {
List<Log<Set<String>>> list = allFamilyMembers.get(entry.getKey());
if (list == null)
allFamilyMembers.put(entry.getKey(), list = new ArrayList<>());
list.addAll(entry.getValue());
}
}
return null;
}));
}
}
Exception failure = exceptions.poll(EXECUTION_TIME, TimeUnit.MILLISECONDS);
if (failure != null)
exceptions.addFirst(failure);
running = false;
exec.shutdown();
if (!exec.awaitTermination(1000, TimeUnit.SECONDS))
throw new IllegalStateException();
for (Future<Void> f : futures) {
// check for exceptions
f.get();
}
checkForEmptyPendingPuts();
log.infof("Generated %d timestamps%n", timestampGenerator.get());
AtomicInteger created = new AtomicInteger();
AtomicInteger removed = new AtomicInteger();
ForkJoinPool threadPool = ForkJoinPool.commonPool();
ArrayList<ForkJoinTask<?>> tasks = new ArrayList<>();
for (Map.Entry<Integer, List<Log<String>>> entry : allFamilyNames.entrySet()) {
tasks.add(threadPool.submit(() -> {
int familyId = entry.getKey();
List<Log<String>> list = entry.getValue();
created.incrementAndGet();
NavigableMap<Integer, List<Log<String>>> logByTime = getWritesAtTime(list);
checkCorrectness("family_name-" + familyId + "-", list, logByTime);
if (list.stream().anyMatch(l -> l.type == LogType.WRITE && l.getValue() == null)) {
removed.incrementAndGet();
}
}));
}
for (Map.Entry<Integer, List<Log<Set<String>>>> entry : allFamilyMembers.entrySet()) {
tasks.add(threadPool.submit(() -> {
int familyId = entry.getKey();
List<Log<Set<String>>> list = entry.getValue();
NavigableMap<Integer, List<Log<Set<String>>>> logByTime = getWritesAtTime(list);
checkCorrectness("family_members-" + familyId + "-", list, logByTime);
}));
}
for (ForkJoinTask<?> task : tasks) {
// with heavy logging this may have trouble to complete
task.get(30, TimeUnit.SECONDS);
}
if (!exceptions.isEmpty()) {
for (Exception e : exceptions) {
log.error("Test failure", e);
}
throw new IllegalStateException("There were " + exceptions.size() + " exceptions");
}
log.infof("Created %d families, removed %d%n", created.get(), removed.get());
}
use of java.util.concurrent.ExecutorService in project hibernate-orm by hibernate.
the class PutFromLoadStressTestCase method doTest.
private void doTest(boolean warmup) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
try {
CyclicBarrier barrier = new CyclicBarrier(NUM_THREADS + 1);
List<Future<String>> futures = new ArrayList<Future<String>>(NUM_THREADS);
for (int i = 0; i < NUM_THREADS; i++) {
Future<String> future = executor.submit(new SelectQueryRunner(barrier, warmup, i + 1));
futures.add(future);
Thread.sleep(LAUNCH_INTERVAL_MILLIS);
}
// wait for all threads to be ready
barrier.await();
long timeout = warmup ? WARMUP_TIME_SECS : RUNNING_TIME_SECS;
TimeUnit unit = TimeUnit.SECONDS;
// Wait for the duration of the test
Thread.sleep(unit.toMillis(timeout));
// Instruct tests to stop doing work
run.set(false);
// wait for all threads to finish
barrier.await(2, TimeUnit.MINUTES);
log.infof("[%s] All threads finished, check for exceptions", title(warmup));
for (Future<String> future : futures) {
String opsPerMS = future.get();
if (!warmup)
log.infof("[%s] Operations/ms: %s", title(warmup), opsPerMS);
}
log.infof("[%s] All future gets checked", title(warmup));
} catch (Exception e) {
log.errorf(e, "Error in one of the execution threads during %s", title(warmup));
throw e;
} finally {
executor.shutdownNow();
}
}
Aggregations