Search in sources :

Example 6 with ExecutorService

use of java.util.concurrent.ExecutorService in project elasticsearch by elastic.

the class IndexAliasesIT method testWaitForAliasSimultaneousUpdate.

public void testWaitForAliasSimultaneousUpdate() throws Exception {
    final int aliasCount = 10;
    logger.info("--> creating index [test]");
    createIndex("test");
    ensureGreen();
    ExecutorService executor = Executors.newFixedThreadPool(aliasCount);
    for (int i = 0; i < aliasCount; i++) {
        final String aliasName = "alias" + i;
        executor.submit(new Runnable() {

            @Override
            public void run() {
                assertAcked(admin().indices().prepareAliases().addAlias("test", aliasName));
                client().index(indexRequest(aliasName).type("type1").id("1").source(source("1", "test"), XContentType.JSON)).actionGet();
            }
        });
    }
    executor.shutdown();
    boolean done = executor.awaitTermination(20, TimeUnit.SECONDS);
    assertThat(done, equalTo(true));
    if (!done) {
        executor.shutdownNow();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 7 with ExecutorService

use of java.util.concurrent.ExecutorService in project jetty.project by eclipse.

the class QoSFilterTest method testQosFilter.

@Test
public void testQosFilter() throws Exception {
    FilterHolder holder = new FilterHolder(QoSFilter2.class);
    holder.setAsyncSupported(true);
    holder.setInitParameter(QoSFilter.MAX_REQUESTS_INIT_PARAM, String.valueOf(MAX_QOS));
    _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    List<Worker2> workers = new ArrayList<>();
    for (int i = 0; i < NUM_CONNECTIONS; ++i) {
        workers.add(new Worker2(i));
    }
    ExecutorService executor = Executors.newFixedThreadPool(NUM_CONNECTIONS);
    List<Future<Void>> futures = executor.invokeAll(workers, 20, TimeUnit.SECONDS);
    rethrowExceptions(futures);
    if (TestServlet.__maxSleepers < MAX_QOS)
        LOG.warn("TEST WAS NOT PARALLEL ENOUGH!");
    else
        Assert.assertEquals(TestServlet.__maxSleepers, MAX_QOS);
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 8 with ExecutorService

use of java.util.concurrent.ExecutorService in project che by eclipse.

the class WorkspaceRuntimes method shutdown.

/**
     * Terminates workspace runtimes service, so no more workspaces are allowed to start
     * or to be stopped directly, all the running workspaces are going to be stopped,
     * all the starting tasks will be eventually interrupted.
     *
     * @throws IllegalStateException
     *         if component shutdown is already called
     */
public void shutdown() throws InterruptedException {
    if (!isShutdown.compareAndSet(false, true)) {
        throw new IllegalStateException("Workspace runtimes service shutdown has been already called");
    }
    List<String> idsToStop;
    try (@SuppressWarnings("unused") Unlocker u = locks.writeAllLock()) {
        idsToStop = states.entrySet().stream().filter(e -> e.getValue().status != WorkspaceStatus.STOPPING).map(Map.Entry::getKey).collect(Collectors.toList());
        states.clear();
    }
    if (!idsToStop.isEmpty()) {
        LOG.info("Shutdown running environments, environments to stop: '{}'", idsToStop.size());
        ExecutorService executor = Executors.newFixedThreadPool(2 * Runtime.getRuntime().availableProcessors(), new ThreadFactoryBuilder().setNameFormat("StopEnvironmentsPool-%d").setDaemon(false).build());
        for (String id : idsToStop) {
            executor.execute(() -> {
                try {
                    envEngine.stop(id);
                } catch (EnvironmentNotRunningException ignored) {
                // might be already stopped
                } catch (Exception x) {
                    LOG.error(x.getMessage(), x);
                }
            });
        }
        executor.shutdown();
        try {
            if (!executor.awaitTermination(30, TimeUnit.SECONDS)) {
                executor.shutdownNow();
                if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
                    LOG.error("Unable to stop runtimes termination pool");
                }
            }
        } catch (InterruptedException e) {
            executor.shutdownNow();
            Thread.currentThread().interrupt();
        }
    }
}
Also used : ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE(org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE) AgentLauncher(org.eclipse.che.api.agent.server.launcher.AgentLauncher) Agent(org.eclipse.che.api.agent.shared.model.Agent) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) StripedLocks(org.eclipse.che.commons.lang.concurrent.StripedLocks) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) RUNNING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) Future(java.util.concurrent.Future) WorkspaceStatusEvent(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) Map(java.util.Map) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) EventService(org.eclipse.che.api.core.notification.EventService) CancellationException(java.util.concurrent.CancellationException) MachineConfig(org.eclipse.che.api.core.model.machine.MachineConfig) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Collectors(java.util.stream.Collectors) Nullable(org.eclipse.che.commons.annotation.Nullable) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) CountDownLatch(java.util.concurrent.CountDownLatch) STARTING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STARTING) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) List(java.util.List) Environment(org.eclipse.che.api.core.model.workspace.Environment) CheEnvironmentEngine(org.eclipse.che.api.environment.server.CheEnvironmentEngine) AgentKey(org.eclipse.che.api.agent.shared.model.AgentKey) EventType(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent.EventType) AgentLauncherFactory(org.eclipse.che.api.agent.server.launcher.AgentLauncherFactory) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) AgentRegistry(org.eclipse.che.api.agent.server.AgentRegistry) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Workspace(org.eclipse.che.api.core.model.workspace.Workspace) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Singleton(javax.inject.Singleton) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(javax.inject.Inject) HashSet(java.util.HashSet) WebsocketMessageConsumer(org.eclipse.che.api.core.util.WebsocketMessageConsumer) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) Objects.requireNonNull(java.util.Objects.requireNonNull) ConflictException(org.eclipse.che.api.core.ConflictException) Comparator.comparing(java.util.Comparator.comparing) Instance(org.eclipse.che.api.machine.server.spi.Instance) SnapshotDao(org.eclipse.che.api.machine.server.spi.SnapshotDao) DtoFactory(org.eclipse.che.dto.server.DtoFactory) ExecutorService(java.util.concurrent.ExecutorService) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) Logger(org.slf4j.Logger) ExtendedMachine(org.eclipse.che.api.core.model.workspace.ExtendedMachine) SNAPSHOTTING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.SNAPSHOTTING) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) NotFoundException(org.eclipse.che.api.core.NotFoundException) TimeUnit(java.util.concurrent.TimeUnit) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) AgentSorter(org.eclipse.che.api.agent.server.impl.AgentSorter) ServerException(org.eclipse.che.api.core.ServerException) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MachineStartedHandler(org.eclipse.che.api.environment.server.MachineStartedHandler) Collections(java.util.Collections) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) ExecutorService(java.util.concurrent.ExecutorService) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) CancellationException(java.util.concurrent.CancellationException) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) ConflictException(org.eclipse.che.api.core.ConflictException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) NotFoundException(org.eclipse.che.api.core.NotFoundException) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) ServerException(org.eclipse.che.api.core.ServerException)

Example 9 with ExecutorService

use of java.util.concurrent.ExecutorService 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();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CyclicBarrier(java.util.concurrent.CyclicBarrier) HttpClient(org.eclipse.jetty.client.HttpClient) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 10 with ExecutorService

use of java.util.concurrent.ExecutorService in project che by eclipse.

the class SystemManager method stopServices.

/**
     * Stops some of the system services preparing system to lighter shutdown.
     * System status is changed from {@link SystemStatus#RUNNING} to
     * {@link SystemStatus#PREPARING_TO_SHUTDOWN}.
     *
     * @throws ConflictException
     *         when system status is different from running
     */
public void stopServices() throws ConflictException {
    if (!statusRef.compareAndSet(RUNNING, PREPARING_TO_SHUTDOWN)) {
        throw new ConflictException("System shutdown has been already called, system status: " + statusRef.get());
    }
    ExecutorService exec = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setDaemon(false).setNameFormat("ShutdownSystemServicesPool").setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance()).build());
    exec.execute(ThreadLocalPropagateContext.wrap(this::doStopServices));
    exec.shutdown();
}
Also used : ConflictException(org.eclipse.che.api.core.ConflictException) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

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