Search in sources :

Example 66 with Future

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

the class RemoteScrollableHitSourceTests method testTooLargeResponse.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void testTooLargeResponse() throws Exception {
    ContentTooLongException tooLong = new ContentTooLongException("too long!");
    CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class);
    when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class), any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class))).then(new Answer<Future<HttpResponse>>() {

        @Override
        public Future<HttpResponse> answer(InvocationOnMock invocationOnMock) throws Throwable {
            HeapBufferedAsyncResponseConsumer consumer = (HeapBufferedAsyncResponseConsumer) invocationOnMock.getArguments()[1];
            FutureCallback callback = (FutureCallback) invocationOnMock.getArguments()[3];
            assertEquals(new ByteSizeValue(100, ByteSizeUnit.MB).bytesAsInt(), consumer.getBufferLimit());
            callback.failed(tooLong);
            return null;
        }
    });
    RemoteScrollableHitSource source = sourceWithMockedClient(true, httpClient);
    AtomicBoolean called = new AtomicBoolean();
    Consumer<Response> checkResponse = r -> called.set(true);
    Throwable e = expectThrows(RuntimeException.class, () -> source.doStartNextScroll(FAKE_SCROLL_ID, timeValueMillis(0), checkResponse));
    // Unwrap the some artifacts from the test
    while (e.getMessage().equals("failed")) {
        e = e.getCause();
    }
    // This next exception is what the user sees
    assertEquals("Remote responded with a chunk that was too large. Use a smaller batch size.", e.getMessage());
    // And that exception is reported as being caused by the underlying exception returned by the client
    assertSame(tooLong, e.getCause());
    assertFalse(called.get());
}
Also used : Response(org.elasticsearch.action.bulk.byscroll.ScrollableHitSource.Response) ByteSizeUnit(org.elasticsearch.common.unit.ByteSizeUnit) ScheduledFuture(java.util.concurrent.ScheduledFuture) URL(java.net.URL) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) StatusLine(org.apache.http.StatusLine) HeapBufferedAsyncResponseConsumer(org.elasticsearch.client.HeapBufferedAsyncResponseConsumer) Future(java.util.concurrent.Future) After(org.junit.After) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) Streams(org.elasticsearch.common.io.Streams) ThreadPool(org.elasticsearch.threadpool.ThreadPool) HttpAsyncRequestProducer(org.apache.http.nio.protocol.HttpAsyncRequestProducer) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) StandardCharsets(java.nio.charset.StandardCharsets) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Matchers.any(org.mockito.Matchers.any) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) Version(org.elasticsearch.Version) RestStatus(org.elasticsearch.rest.RestStatus) TimeValue.timeValueMinutes(org.elasticsearch.common.unit.TimeValue.timeValueMinutes) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) RestClient(org.elasticsearch.client.RestClient) ContentTooLongException(org.apache.http.ContentTooLongException) ParsingException(org.elasticsearch.common.ParsingException) BasicStatusLine(org.apache.http.message.BasicStatusLine) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SearchRequest(org.elasticsearch.action.search.SearchRequest) BackoffPolicy(org.elasticsearch.action.bulk.BackoffPolicy) BytesArray(org.elasticsearch.common.bytes.BytesArray) HttpAsyncResponseConsumer(org.apache.http.nio.protocol.HttpAsyncResponseConsumer) Answer(org.mockito.stubbing.Answer) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) TimeValue.timeValueMillis(org.elasticsearch.common.unit.TimeValue.timeValueMillis) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ESTestCase(org.elasticsearch.test.ESTestCase) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) FileSystemUtils(org.elasticsearch.common.io.FileSystemUtils) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Matchers.empty(org.hamcrest.Matchers.empty) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) FutureCallback(org.apache.http.concurrent.FutureCallback) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) InputStreamReader(java.io.InputStreamReader) Consumer(java.util.function.Consumer) ProtocolVersion(org.apache.http.ProtocolVersion) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) HttpResponse(org.apache.http.HttpResponse) InputStreamEntity(org.apache.http.entity.InputStreamEntity) HttpHost(org.apache.http.HttpHost) ContentTooLongException(org.apache.http.ContentTooLongException) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) HttpAsyncResponseConsumer(org.apache.http.nio.protocol.HttpAsyncResponseConsumer) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HeapBufferedAsyncResponseConsumer(org.elasticsearch.client.HeapBufferedAsyncResponseConsumer) Response(org.elasticsearch.action.bulk.byscroll.ScrollableHitSource.Response) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpAsyncRequestProducer(org.apache.http.nio.protocol.HttpAsyncRequestProducer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) ScheduledFuture(java.util.concurrent.ScheduledFuture) Future(java.util.concurrent.Future) FutureCallback(org.apache.http.concurrent.FutureCallback)

Example 67 with Future

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

the class ProjectServiceTest method testDeleteProjectsConcurrently.

@Test
public void testDeleteProjectsConcurrently() throws Exception {
    int threadNumber = 5 * (Runtime.getRuntime().availableProcessors() + 1);
    ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
    CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
    List<Future<ContainerResponse>> futures = new LinkedList<>();
    for (int i = 0; i < threadNumber; i++) {
        addMockedProjectConfigDto(ptRegistry.getProjectType("my_project_type"), "my_project_name" + i);
    }
    IntStream.range(0, threadNumber).forEach(i -> {
        futures.add(executor.submit(() -> {
            countDownLatch.countDown();
            countDownLatch.await();
            try {
                return launcher.service(DELETE, "http://localhost:8080/api/project/my_project_name" + i, "http://localhost:8080/api", null, null, null);
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        }));
    });
    boolean isNotDone;
    do {
        isNotDone = false;
        for (Future<ContainerResponse> future : futures) {
            if (!future.isDone()) {
                isNotDone = true;
            }
        }
    } while (isNotDone);
    for (Future<ContainerResponse> future : futures) {
        assertEquals(future.get().getStatus(), 204, "Error: " + future.get().getEntity());
    }
    executor.shutdown();
}
Also used : ContainerResponse(org.everrest.core.impl.ContainerResponse) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) IOException(java.io.IOException) ServerException(org.eclipse.che.api.core.ServerException) ValueStorageException(org.eclipse.che.api.project.server.type.ValueStorageException) ProjectTypeConstraintException(org.eclipse.che.api.project.server.type.ProjectTypeConstraintException) ConflictException(org.eclipse.che.api.core.ConflictException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) Test(org.testng.annotations.Test)

Example 68 with Future

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

the class RmiClient method stopAll.

public void stopAll(boolean wait) {
    List<ProcessInfo> processList;
    synchronized (infoMap) {
        processList = (infoMap.values().stream().filter(info -> info.processHandler != null).collect(Collectors.toList()));
    }
    if (processList.isEmpty()) {
        return;
    }
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future<?> future = executorService.submit(() -> {
        for (ProcessInfo processInfo : processList) {
            processInfo.processHandler.destroyProcess();
        }
        if (wait) {
            for (ProcessInfo processInfo : processList) {
                processInfo.processHandler.waitFor();
            }
        }
    });
    if (wait) {
        try {
            future.get();
        } catch (InterruptedException ignore) {
        } catch (java.util.concurrent.ExecutionException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    executorService.shutdown();
}
Also used : ProcessHandler(org.eclipse.che.plugin.maven.server.execution.ProcessHandler) Logger(org.slf4j.Logger) ProcessOutputType(org.eclipse.che.plugin.maven.server.execution.ProcessOutputType) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ExecutionException(org.eclipse.che.plugin.maven.server.execution.ExecutionException) ProcessListener(org.eclipse.che.plugin.maven.server.execution.ProcessListener) Registry(java.rmi.registry.Registry) Pair(org.eclipse.che.commons.lang.Pair) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) Future(java.util.concurrent.Future) ProcessExecutor(org.eclipse.che.plugin.maven.server.execution.ProcessExecutor) LocateRegistry(java.rmi.registry.LocateRegistry) ProcessEvent(org.eclipse.che.plugin.maven.server.execution.ProcessEvent) Map(java.util.Map) PortableRemoteObject(javax.rmi.PortableRemoteObject) ExecutorService(java.util.concurrent.ExecutorService) ExecutorService(java.util.concurrent.ExecutorService)

Example 69 with Future

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

the class Server method doStop.

/* ------------------------------------------------------------ */
@Override
protected void doStop() throws Exception {
    if (isDumpBeforeStop())
        dumpStdErr();
    if (LOG.isDebugEnabled())
        LOG.debug("doStop {}", this);
    MultiException mex = new MultiException();
    // list if graceful futures
    List<Future<Void>> futures = new ArrayList<>();
    // First close the network connectors to stop accepting new connections
    for (Connector connector : _connectors) futures.add(connector.shutdown());
    // Then tell the contexts that we are shutting down
    Handler[] gracefuls = getChildHandlersByClass(Graceful.class);
    for (Handler graceful : gracefuls) futures.add(((Graceful) graceful).shutdown());
    // Shall we gracefully wait for zero connections?
    long stopTimeout = getStopTimeout();
    if (stopTimeout > 0) {
        long stop_by = System.currentTimeMillis() + stopTimeout;
        if (LOG.isDebugEnabled())
            LOG.debug("Graceful shutdown {} by ", this, new Date(stop_by));
        // Wait for shutdowns
        for (Future<Void> future : futures) {
            try {
                if (!future.isDone())
                    future.get(Math.max(1L, stop_by - System.currentTimeMillis()), TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                mex.add(e);
            }
        }
    }
    // Cancel any shutdowns not done
    for (Future<Void> future : futures) if (!future.isDone())
        future.cancel(true);
    // Now stop the connectors (this will close existing connections)
    for (Connector connector : _connectors) {
        try {
            connector.stop();
        } catch (Throwable e) {
            mex.add(e);
        }
    }
    // And finally stop everything else
    try {
        super.doStop();
    } catch (Throwable e) {
        mex.add(e);
    }
    if (getStopAtShutdown())
        ShutdownThread.deregister(this);
    //Unregister the Server with the handler thread for receiving
    //remote stop commands as we are stopped already
    ShutdownMonitor.deregister(this);
    mex.ifExceptionThrow();
}
Also used : Graceful(org.eclipse.jetty.util.component.Graceful) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Date(java.util.Date) ServletException(javax.servlet.ServletException) MultiException(org.eclipse.jetty.util.MultiException) IOException(java.io.IOException) Future(java.util.concurrent.Future) MultiException(org.eclipse.jetty.util.MultiException)

Example 70 with Future

use of java.util.concurrent.Future 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)

Aggregations

Future (java.util.concurrent.Future)1138 ArrayList (java.util.ArrayList)479 ExecutorService (java.util.concurrent.ExecutorService)445 Test (org.junit.Test)413 ExecutionException (java.util.concurrent.ExecutionException)264 Callable (java.util.concurrent.Callable)206 IOException (java.io.IOException)177 ParallelTest (com.hazelcast.test.annotation.ParallelTest)148 QuickTest (com.hazelcast.test.annotation.QuickTest)148 HashMap (java.util.HashMap)92 List (java.util.List)84 CountDownLatch (java.util.concurrent.CountDownLatch)71 LinkedList (java.util.LinkedList)67 TimeoutException (java.util.concurrent.TimeoutException)63 HashSet (java.util.HashSet)62 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)59 Map (java.util.Map)58 ICompletableFuture (com.hazelcast.core.ICompletableFuture)57 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)53 File (java.io.File)46