use of java.util.concurrent.ForkJoinTask 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.ForkJoinTask in project buck by facebook.
the class TargetGraphHashing method hashTargetGraph.
/**
* Given a {@link TargetGraph} and any number of root nodes to traverse,
* returns a map of {@code (BuildTarget, HashCode)} pairs for all root
* build targets and their dependencies.
*/
public ImmutableMap<BuildTarget, HashCode> hashTargetGraph() throws CycleException {
try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus, PerfEventId.of("ShowTargetHashes"))) {
AcyclicDepthFirstPostOrderTraversal<TargetNode<?, ?>> traversal = new AcyclicDepthFirstPostOrderTraversal<>(node -> targetGraph.getAll(node.getDeps()).iterator());
final Map<BuildTarget, ForkJoinTask<HashCode>> buildTargetHashes = new HashMap<>();
Queue<ForkJoinTask<HashCode>> tasksToSchedule = new ArrayDeque<>();
// Start all the node tasks, bottom up
for (final TargetNode<?, ?> node : traversal.traverse(roots)) {
HashNodeTask task = new HashNodeTask(node, buildTargetHashes);
buildTargetHashes.put(node.getBuildTarget(), task);
tasksToSchedule.add(task);
}
// Execute tasks in parallel
ForkJoinPool pool = new ForkJoinPool(numThreads);
for (ForkJoinTask<HashCode> task : tasksToSchedule) {
pool.execute(task);
}
// Wait for all scheduled tasks to complete
return ImmutableMap.copyOf(Maps.transformEntries(buildTargetHashes, (key, value) -> value.join()));
}
}
Aggregations