Search in sources :

Example 1 with CompletableFuture

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

the class SlowClientsTest method testSlowClientsWithSmallThreadPool.

@Test(timeout = 10000)
public void testSlowClientsWithSmallThreadPool() throws Exception {
    File keystore = MavenTestingUtils.getTestResourceFile("keystore");
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(keystore.getAbsolutePath());
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyManagerPassword("keypwd");
    int maxThreads = 6;
    int contentLength = 8 * 1024 * 1024;
    QueuedThreadPool serverThreads = new QueuedThreadPool(maxThreads);
    serverThreads.setDetailedDump(true);
    Server server = new Server(serverThreads);
    try {
        ServerConnector connector = new ServerConnector(server, 1, 1, sslContextFactory);
        connector.setPort(8888);
        server.addConnector(connector);
        server.setHandler(new AbstractHandler() {

            @Override
            public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                baseRequest.setHandled(true);
                logger.info("SERVING {}", target);
                // Write some big content.
                response.getOutputStream().write(new byte[contentLength]);
                logger.info("SERVED {}", target);
            }
        });
        server.start();
        SSLContext sslContext = sslContextFactory.getSslContext();
        CompletableFuture[] futures = new CompletableFuture[2 * maxThreads];
        ExecutorService executor = Executors.newFixedThreadPool(futures.length);
        for (int i = 0; i < futures.length; i++) {
            int k = i;
            futures[i] = CompletableFuture.runAsync(() -> {
                try (SSLSocket socket = (SSLSocket) sslContext.getSocketFactory().createSocket("localhost", connector.getLocalPort())) {
                    socket.setSoTimeout(contentLength / 1024);
                    OutputStream output = socket.getOutputStream();
                    String target = "/" + k;
                    String request = "GET " + target + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
                    output.write(request.getBytes(StandardCharsets.UTF_8));
                    output.flush();
                    while (serverThreads.getIdleThreads() > 0) Thread.sleep(50);
                    InputStream input = socket.getInputStream();
                    while (true) {
                        int read = input.read();
                        if (read < 0)
                            break;
                    }
                    logger.info("FINISHED {}", target);
                } catch (IOException x) {
                    throw new UncheckedIOException(x);
                } catch (InterruptedException x) {
                    throw new UncheckedIOException(new InterruptedIOException());
                }
            }, executor);
        }
        CompletableFuture.allOf(futures).join();
    } finally {
        server.stop();
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) Server(org.eclipse.jetty.server.Server) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) SSLContext(javax.net.ssl.SSLContext) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) CompletableFuture(java.util.concurrent.CompletableFuture) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File) Test(org.junit.Test)

Example 2 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project vert.x by eclipse.

the class Http2ClientTest method testClientRequestWriteability.

@Test
public void testClientRequestWriteability() throws Exception {
    Buffer content = Buffer.buffer();
    Buffer expected = Buffer.buffer();
    String chunk = TestUtils.randomAlphaString(100);
    CompletableFuture<Void> done = new CompletableFuture<>();
    AtomicBoolean paused = new AtomicBoolean();
    AtomicInteger numPause = new AtomicInteger();
    server.requestHandler(req -> {
        Context ctx = vertx.getOrCreateContext();
        done.thenAccept(v1 -> {
            paused.set(false);
            ctx.runOnContext(v2 -> {
                req.resume();
            });
        });
        numPause.incrementAndGet();
        req.pause();
        paused.set(true);
        req.handler(content::appendBuffer);
        req.endHandler(v -> {
            assertEquals(expected, content);
            req.response().end();
        });
    });
    startServer();
    HttpClientRequest req = client.post(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
        testComplete();
    }).setChunked(true).exceptionHandler(err -> {
        fail();
    });
    AtomicInteger sent = new AtomicInteger();
    AtomicInteger count = new AtomicInteger();
    AtomicInteger drained = new AtomicInteger();
    vertx.setPeriodic(1, timerID -> {
        Context ctx = vertx.getOrCreateContext();
        if (req.writeQueueFull()) {
            assertTrue(paused.get());
            assertEquals(1, numPause.get());
            req.drainHandler(v -> {
                assertOnIOContext(ctx);
                assertEquals(0, drained.getAndIncrement());
                assertEquals(1, numPause.get());
                assertFalse(paused.get());
                req.end();
            });
            vertx.cancelTimer(timerID);
            done.complete(null);
        } else {
            count.incrementAndGet();
            expected.appendString(chunk);
            req.write(chunk);
            sent.addAndGet(chunk.length());
        }
    });
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) HttpClientRequest(io.vertx.core.http.HttpClientRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 3 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project vert.x by eclipse.

the class Http1xTest method testInvalidHttpResponse.

@Test
public void testInvalidHttpResponse() {
    waitFor(2);
    AtomicInteger count = new AtomicInteger(0);
    CompletableFuture<Void> sendResp = new CompletableFuture<>();
    NetServer server = vertx.createNetServer();
    String match = "GET /somepath HTTP/1.1\r\nHost: localhost:8080\r\n\r\n";
    server.connectHandler(so -> {
        StringBuilder content = new StringBuilder();
        so.handler(buff -> {
            content.append(buff);
            while (content.toString().startsWith(match)) {
                content.delete(0, match.length());
                switch(count.getAndIncrement()) {
                    case 0:
                        sendResp.thenAccept(v -> {
                        });
                        break;
                    case 1:
                        Buffer resp1 = Buffer.buffer(TestUtils.randomAlphaString(40) + "\r\n");
                        Buffer resp2 = Buffer.buffer("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
                        so.write(Buffer.buffer().appendBuffer(resp1).appendBuffer(resp2));
                        break;
                    default:
                        fail();
                        break;
                }
            }
        });
    }).listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(s -> {
        client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setPipelining(true).setMaxPoolSize(1));
        AtomicBoolean fail1 = new AtomicBoolean();
        HttpClientRequest req1 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
            fail();
        }).exceptionHandler(err -> {
            if (fail1.compareAndSet(false, true)) {
                assertEquals(IllegalArgumentException.class, err.getClass());
                complete();
            }
        });
        AtomicBoolean fail2 = new AtomicBoolean();
        HttpClientRequest req2 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
            resp.bodyHandler(buff -> {
                assertEquals("okusa", buff.toString());
                testComplete();
            });
        }).exceptionHandler(err -> {
            if (fail2.compareAndSet(false, true)) {
                assertEquals(VertxException.class, err.getClass());
                complete();
            }
        });
        req1.end();
        req2.end();
    }));
    await();
}
Also used : IntStream(java.util.stream.IntStream) java.util(java.util) io.vertx.core(io.vertx.core) io.vertx.core.impl(io.vertx.core.impl) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) io.vertx.core.http(io.vertx.core.http) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientRequestImpl(io.vertx.core.http.impl.HttpClientRequestImpl) Buffer(io.vertx.core.buffer.Buffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestUtils(io.vertx.test.core.TestUtils) RecordParser(io.vertx.core.parsetools.RecordParser) Pump(io.vertx.core.streams.Pump) JsonObject(io.vertx.core.json.JsonObject) Buffer(io.vertx.core.buffer.Buffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 4 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project crate by crate.

the class AlterTableOperation method executeAlterTable.

public CompletableFuture<Long> executeAlterTable(AlterTableAnalyzedStatement analysis) {
    DocTableInfo table = analysis.table();
    if (table.isAlias() && !table.isPartitioned()) {
        return CompletableFutures.failedFuture(new AlterTableAliasException(table.ident().fqn()));
    }
    List<CompletableFuture<Long>> results = new ArrayList<>(3);
    if (table.isPartitioned()) {
        // create new filtered partition table settings
        PartitionedTableParameterInfo tableSettingsInfo = (PartitionedTableParameterInfo) table.tableParameterInfo();
        TableParameter parameterWithFilteredSettings = new TableParameter(analysis.tableParameter().settings(), tableSettingsInfo.partitionTableSettingsInfo().supportedInternalSettings());
        Optional<PartitionName> partitionName = analysis.partitionName();
        if (partitionName.isPresent()) {
            String index = partitionName.get().asIndexName();
            results.add(updateMapping(analysis.tableParameter().mappings(), index));
            results.add(updateSettings(parameterWithFilteredSettings, index));
        } else {
            // template gets all changes unfiltered
            results.add(updateTemplate(analysis.tableParameter(), table.ident()));
            if (!analysis.excludePartitions()) {
                String[] indices = table.concreteIndices();
                results.add(updateMapping(analysis.tableParameter().mappings(), indices));
                results.add(updateSettings(parameterWithFilteredSettings, indices));
            }
        }
    } else {
        results.add(updateMapping(analysis.tableParameter().mappings(), table.ident().indexName()));
        results.add(updateSettings(analysis.tableParameter(), table.ident().indexName()));
    }
    final CompletableFuture<Long> result = new CompletableFuture<>();
    applyMultiFutureCallback(result, results);
    return result;
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableParameter(io.crate.analyze.TableParameter) PartitionedTableParameterInfo(io.crate.analyze.PartitionedTableParameterInfo) PartitionName(io.crate.metadata.PartitionName) CompletableFuture(java.util.concurrent.CompletableFuture) AlterTableAliasException(io.crate.exceptions.AlterTableAliasException)

Example 5 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project crate by crate.

the class RepositoryService method execute.

public CompletableFuture<Long> execute(DropRepositoryAnalyzedStatement analyzedStatement) {
    final CompletableFuture<Long> future = new CompletableFuture<>();
    final String repoName = analyzedStatement.repositoryName();
    deleteRepositoryAction.execute(new DeleteRepositoryRequest(repoName), new ActionListener<DeleteRepositoryResponse>() {

        @Override
        public void onResponse(DeleteRepositoryResponse deleteRepositoryResponse) {
            if (!deleteRepositoryResponse.isAcknowledged()) {
                LOGGER.info("delete repository '{}' not acknowledged", repoName);
            }
            future.complete(1L);
        }

        @Override
        public void onFailure(Throwable e) {
            future.completeExceptionally(e);
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DeleteRepositoryResponse(org.elasticsearch.action.admin.cluster.repositories.delete.DeleteRepositoryResponse) DeleteRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.delete.DeleteRepositoryRequest)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)2896 Test (org.junit.Test)1078 List (java.util.List)742 ArrayList (java.util.ArrayList)736 Map (java.util.Map)522 IOException (java.io.IOException)453 Collectors (java.util.stream.Collectors)431 HashMap (java.util.HashMap)413 TimeUnit (java.util.concurrent.TimeUnit)410 Collections (java.util.Collections)391 UUID (java.util.UUID)366 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)365 CompletionException (java.util.concurrent.CompletionException)358 ExecutionException (java.util.concurrent.ExecutionException)338 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)332 Collection (java.util.Collection)326 Duration (java.time.Duration)295 Function (java.util.function.Function)290 Futures (io.pravega.common.concurrent.Futures)289 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)284