Search in sources :

Example 36 with ExecutorService

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);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 37 with ExecutorService

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);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 38 with ExecutorService

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);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 39 with ExecutorService

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());
}
Also used : Arrays(java.util.Arrays) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) Transaction(org.hibernate.Transaction) Future(java.util.concurrent.Future) PessimisticLockException(org.hibernate.PessimisticLockException) PersistentClass(org.hibernate.mapping.PersistentClass) Map(java.util.Map) StaleStateException(org.hibernate.StaleStateException) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SessionFactory(org.hibernate.SessionFactory) Set(java.util.Set) Category(org.junit.experimental.categories.Category) Executors(java.util.concurrent.Executors) InvocationTargetException(java.lang.reflect.InvocationTargetException) InterceptorConfiguration(org.infinispan.configuration.cache.InterceptorConfiguration) Stream(java.util.stream.Stream) AfterClassOnce(org.hibernate.testing.AfterClassOnce) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) TestingJtaPlatformImpl(org.hibernate.testing.jta.TestingJtaPlatformImpl) InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) H2Dialect(org.hibernate.dialect.H2Dialect) LockAcquisitionException(org.hibernate.exception.LockAcquisitionException) TestResourceTracker(org.infinispan.test.fwk.TestResourceTracker) RunWith(org.junit.runner.RunWith) SimpleDateFormat(java.text.SimpleDateFormat) Session(org.hibernate.Session) Metadata(org.hibernate.boot.Metadata) ArrayList(java.util.ArrayList) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BiConsumer(java.util.function.BiConsumer) JtaTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl) Environment(org.hibernate.cfg.Environment) Family(org.hibernate.test.cache.infinispan.stress.entities.Family) Properties(java.util.Properties) Files(java.nio.file.Files) BlockingDeque(java.util.concurrent.BlockingDeque) BufferedWriter(java.io.BufferedWriter) OptimisticLockException(javax.persistence.OptimisticLockException) RollbackCommand(org.infinispan.commands.tx.RollbackCommand) IOException(java.io.IOException) Test(org.junit.Test) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) Field(java.lang.reflect.Field) File(java.io.File) TreeMap(java.util.TreeMap) JdbcResourceLocalTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl) ForkJoinPool(java.util.concurrent.ForkJoinPool) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) InfinispanMessageLogger(org.hibernate.cache.infinispan.util.InfinispanMessageLogger) Date(java.util.Date) TransactionStatus(org.hibernate.resource.transaction.spi.TransactionStatus) Person(org.hibernate.test.cache.infinispan.stress.entities.Person) JtaAwareConnectionProviderImpl(org.hibernate.testing.jta.JtaAwareConnectionProviderImpl) InvocationContext(org.infinispan.context.InvocationContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AccessType(org.hibernate.cache.spi.access.AccessType) Method(java.lang.reflect.Method) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Parameterized(org.junit.runners.Parameterized) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) StaleObjectStateException(org.hibernate.StaleObjectStateException) Collection(org.hibernate.mapping.Collection) NavigableMap(java.util.NavigableMap) Collectors(java.util.stream.Collectors) TransactionException(org.hibernate.TransactionException) MetadataSources(org.hibernate.boot.MetadataSources) List(java.util.List) PersistenceException(javax.persistence.PersistenceException) Address(org.hibernate.test.cache.infinispan.stress.entities.Address) PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) TimeoutException(org.infinispan.util.concurrent.TimeoutException) Restrictions(org.hibernate.criterion.Restrictions) RootClass(org.hibernate.mapping.RootClass) HashMap(java.util.HashMap) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ConcurrentMap(java.util.concurrent.ConcurrentMap) RegionAccessStrategy(org.hibernate.cache.spi.access.RegionAccessStrategy) HashSet(java.util.HashSet) RollbackException(javax.transaction.RollbackException) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) ForkJoinTask(java.util.concurrent.ForkJoinTask) LockMode(org.hibernate.LockMode) Iterator(java.util.Iterator) CustomParameterized(org.hibernate.testing.junit4.CustomParameterized) NoJtaPlatform(org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform) CommitCommand(org.infinispan.commands.tx.CommitCommand) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) CacheMode(org.infinispan.configuration.cache.CacheMode) BaseCustomInterceptor(org.infinispan.interceptors.base.BaseCustomInterceptor) Ignore(org.junit.Ignore) BeforeClassOnce(org.hibernate.testing.BeforeClassOnce) VisitableCommand(org.infinispan.commands.VisitableCommand) Comparator(java.util.Comparator) TransactionManager(javax.transaction.TransactionManager) InvalidationCacheAccessDelegate(org.hibernate.cache.infinispan.access.InvalidationCacheAccessDelegate) RemoteException(org.infinispan.remoting.RemoteException) Collections(java.util.Collections) Set(java.util.Set) HashSet(java.util.HashSet) NavigableMap(java.util.NavigableMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ForkJoinTask(java.util.concurrent.ForkJoinTask) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) PessimisticLockException(org.hibernate.PessimisticLockException) StaleStateException(org.hibernate.StaleStateException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) LockAcquisitionException(org.hibernate.exception.LockAcquisitionException) OptimisticLockException(javax.persistence.OptimisticLockException) IOException(java.io.IOException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) StaleObjectStateException(org.hibernate.StaleObjectStateException) TransactionException(org.hibernate.TransactionException) PersistenceException(javax.persistence.PersistenceException) TimeoutException(org.infinispan.util.concurrent.TimeoutException) RollbackException(javax.transaction.RollbackException) RemoteException(org.infinispan.remoting.RemoteException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Map(java.util.Map) TreeMap(java.util.TreeMap) NavigableMap(java.util.NavigableMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test)

Example 40 with ExecutorService

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();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) TimeUnit(java.util.concurrent.TimeUnit) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)4721 Test (org.junit.Test)1645 ArrayList (java.util.ArrayList)1157 Future (java.util.concurrent.Future)1064 CountDownLatch (java.util.concurrent.CountDownLatch)722 IOException (java.io.IOException)699 Callable (java.util.concurrent.Callable)612 ExecutionException (java.util.concurrent.ExecutionException)532 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)343 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)296 HashMap (java.util.HashMap)257 List (java.util.List)257 Test (org.testng.annotations.Test)240 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)235 File (java.io.File)210 TimeoutException (java.util.concurrent.TimeoutException)207 Map (java.util.Map)201 HashSet (java.util.HashSet)171 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)169 Test (org.junit.jupiter.api.Test)163