Search in sources :

Example 51 with Future

use of io.vertx.core.Future in project gravitee-gateway by gravitee-io.

the class FailoverInvoker method invoke.

@Override
public Request invoke(ExecutionContext executionContext, Request serverRequest, ReadStream<Buffer> stream, Handler<ProxyConnection> connectionHandler) {
    final Request failoverServerRequest = new FailoverRequest(serverRequest);
    circuitBreaker.execute(new io.vertx.core.Handler<Future<ProxyConnection>>() {

        @Override
        public void handle(Future<ProxyConnection> event) {
            invoker.invoke(executionContext, failoverServerRequest, stream, proxyConnection -> {
                proxyConnection.exceptionHandler(event::fail);
                proxyConnection.responseHandler(response -> event.complete(new FailoverProxyConnection(proxyConnection, response)));
            });
        }
    }).setHandler(new io.vertx.core.Handler<AsyncResult<ProxyConnection>>() {

        @Override
        public void handle(AsyncResult<ProxyConnection> event) {
            if (event.failed()) {
                FailoverConnection connection = new FailoverConnection();
                connectionHandler.handle(connection);
                connection.sendBadGatewayResponse();
            } else {
                FailoverProxyConnection proxyConnection = (FailoverProxyConnection) event.result();
                connectionHandler.handle(proxyConnection);
                proxyConnection.sendResponse();
            }
        }
    });
    return failoverServerRequest;
}
Also used : Request(io.gravitee.gateway.api.Request) Handler(io.gravitee.gateway.api.handler.Handler) ProxyConnection(io.gravitee.gateway.api.proxy.ProxyConnection) Future(io.vertx.core.Future) AsyncResult(io.vertx.core.AsyncResult)

Example 52 with Future

use of io.vertx.core.Future in project raml-module-builder by folio-org.

the class PostgresClientIT method parallel.

@Test
public void parallel(TestContext context) {
    /**
     * number of parallel queries
     */
    int n = 20;
    /**
     * sleep time in milliseconds
     */
    double sleep = 150;
    String selectSleep = "select pg_sleep(" + sleep / 1000 + ")";
    /**
     * maximum duration in milliseconds for the completion of all parallel queries
     */
    long maxDuration = (long) (n * sleep / 2);
    /* create n queries in parallel, each sleeping for some time.
     * If vert.x properly processes them in parallel it finishes
     * in less than half of the time needed for sequential processing.
     */
    Async async = context.async();
    PostgresClient client = PostgresClient.getInstance(vertx);
    List<Future> futures = new ArrayList<>(n);
    for (int i = 0; i < n; i++) {
        Future<ResultSet> future = Future.future();
        client.select(selectSleep, future.completer());
        futures.add(future);
    }
    long start = System.currentTimeMillis();
    CompositeFuture.all(futures).setHandler(handler -> {
        long duration = System.currentTimeMillis() - start;
        client.closeClient(whenDone -> {
        });
        context.assertTrue(handler.succeeded());
        context.assertTrue(duration < maxDuration, "duration must be less than " + maxDuration + " ms, it is " + duration + " ms");
        async.complete();
    });
}
Also used : Async(io.vertx.ext.unit.Async) ArrayList(java.util.ArrayList) ResultSet(io.vertx.ext.sql.ResultSet) Future(io.vertx.core.Future) CompositeFuture(io.vertx.core.CompositeFuture) Test(org.junit.Test)

Example 53 with Future

use of io.vertx.core.Future in project strimzi by strimzi.

the class ControllerAssignedKafkaImpl method changeReplicationFactor.

@Override
public void changeReplicationFactor(Topic topic, Handler<AsyncResult<Void>> handler) {
    LOGGER.info("Changing replication factor of topic {} to {}", topic.getTopicName(), topic.getNumReplicas());
    final String zookeeper = config.get(Config.ZOOKEEPER_CONNECT);
    Future<File> generateFuture = Future.future();
    // generate a reassignment
    vertx.executeBlocking(fut -> {
        try {
            LOGGER.debug("Generating reassignment json for topic {}", topic.getTopicName());
            String reassignment = generateReassignment(topic, zookeeper);
            LOGGER.debug("Reassignment json for topic {}: {}", topic.getTopicName(), reassignment);
            File reassignmentJsonFile = createTmpFile("-reassignment.json");
            try (Writer w = new OutputStreamWriter(new FileOutputStream(reassignmentJsonFile), StandardCharsets.UTF_8)) {
                w.write(reassignment);
            }
            fut.complete(reassignmentJsonFile);
        } catch (Exception e) {
            fut.fail(e);
        }
    }, generateFuture.completer());
    Future<File> executeFuture = Future.future();
    generateFuture.compose(reassignmentJsonFile -> {
        // execute the reassignment
        vertx.executeBlocking(fut -> {
            final Long throttle = config.get(Config.REASSIGN_THROTTLE);
            try {
                LOGGER.debug("Starting reassignment for topic {} with throttle {}", topic.getTopicName(), throttle);
                executeReassignment(reassignmentJsonFile, zookeeper, throttle);
                fut.complete(reassignmentJsonFile);
            } catch (Exception e) {
                fut.fail(e);
            }
        }, executeFuture.completer());
    }, executeFuture);
    Future<Void> periodicFuture = Future.future();
    Future<Void> reassignmentFinishedFuture = Future.future();
    executeFuture.compose(reassignmentJsonFile -> {
        // Poll repeatedly, calling --verify to remove the throttle
        long timeout = 10_000;
        long first = System.currentTimeMillis();
        final Long periodMs = config.get(Config.REASSIGN_VERIFY_INTERVAL_MS);
        LOGGER.debug("Verifying reassignment every {} seconds", TimeUnit.SECONDS.convert(periodMs, TimeUnit.MILLISECONDS));
        vertx.setPeriodic(periodMs, timerId -> vertx.<Boolean>executeBlocking(fut -> {
            LOGGER.debug(String.format("Verifying reassignment for topic {} (timer id=%s)", topic.getTopicName(), timerId));
            final Long throttle = config.get(Config.REASSIGN_THROTTLE);
            final boolean reassignmentComplete;
            try {
                reassignmentComplete = verifyReassignment(reassignmentJsonFile, zookeeper, throttle);
            } catch (Exception e) {
                fut.fail(e);
                return;
            }
            fut.complete(reassignmentComplete);
        }, ar -> {
            if (ar.succeeded()) {
                if (ar.result()) {
                    LOGGER.info("Reassignment complete");
                    delete(reassignmentJsonFile);
                    LOGGER.debug("Cancelling timer " + timerId);
                    vertx.cancelTimer(timerId);
                    reassignmentFinishedFuture.complete();
                } else if (System.currentTimeMillis() - first > timeout) {
                    LOGGER.error("Reassignment timed out");
                    delete(reassignmentJsonFile);
                    LOGGER.debug("Cancelling timer " + timerId);
                    vertx.cancelTimer(timerId);
                    reassignmentFinishedFuture.fail("Timeout");
                }
            } else {
                // reassignmentFinishedFuture.fail(ar.cause());
                LOGGER.error("Error while verifying reassignment", ar.cause());
            }
        }));
        periodicFuture.complete();
    }, periodicFuture);
    CompositeFuture.all(periodicFuture, reassignmentFinishedFuture).map((Void) null).setHandler(handler);
// TODO The algorithm should really be more like this:
// 1. Use the cmdline tool to generate an assignment
// 2. Set the throttles
// 3. Update the reassign_partitions znode
// 4. Watch for changes or deletion of reassign_partitions
// a. Update the throttles
// b. complete the handler
// Doing this is much better because means we don't have to batch reassignments
// and also means we need less state for reassignment
// though we aren't relieved of the statefullness wrt removing throttles :-(
}
Also used : NewPartitions(org.apache.kafka.clients.admin.NewPartitions) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) LoggerFactory(org.slf4j.LoggerFactory) Function(java.util.function.Function) ArrayList(java.util.ArrayList) AdminClient(org.apache.kafka.clients.admin.AdminClient) CompositeFuture(io.vertx.core.CompositeFuture) Charset(java.nio.charset.Charset) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) AsyncResult(io.vertx.core.AsyncResult) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) NewTopic(org.apache.kafka.clients.admin.NewTopic) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) KafkaFuture(org.apache.kafka.common.KafkaFuture) FileInputStream(java.io.FileInputStream) Future(io.vertx.core.Future) InputStreamReader(java.io.InputStreamReader) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Writer(java.io.Writer) Node(org.apache.kafka.common.Node) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) Collections(java.util.Collections) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 54 with Future

use of io.vertx.core.Future in project vertx-openshift-it by cescoffier.

the class LocksVerticle method setupRouter.

private Router setupRouter() {
    Router router = Router.router(vertx);
    router.get("/locks/:name").handler(rc -> {
        String name = rc.pathParam("name");
        getLock(name).compose(l -> {
            Future<Void> future = Future.future();
            vertx.setTimer(2000, tid -> {
                l.release();
                future.complete();
            });
            return future;
        }).setHandler(ar -> sendResponse(rc, ar));
    });
    router.get("/health").handler(rc -> {
        rc.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/plain").end("OK");
    });
    return router;
}
Also used : Lock(io.vertx.core.shareddata.Lock) AbstractVerticle(io.vertx.core.AbstractVerticle) HttpServer(io.vertx.core.http.HttpServer) HttpHeaders(io.vertx.core.http.HttpHeaders) Router(io.vertx.ext.web.Router) AsyncResult(io.vertx.core.AsyncResult) RoutingContext(io.vertx.ext.web.RoutingContext) Future(io.vertx.core.Future) Router(io.vertx.ext.web.Router) Future(io.vertx.core.Future)

Example 55 with Future

use of io.vertx.core.Future in project georocket by georocket.

the class MetadataVerticle method start.

@Override
public void start(Future<Void> startFuture) {
    // load and copy all indexer factories now and not lazily to avoid
    // concurrent modifications to the service loader's internal cache
    indexerFactories = ImmutableList.copyOf(ServiceLoader.load(IndexerFactory.class));
    queryCompiler = createQueryCompiler();
    queryCompiler.setQueryCompilers(indexerFactories);
    new ElasticsearchClientFactory(vertx).createElasticsearchClient(INDEX_NAME).doOnNext(es -> {
        client = es;
    }).flatMap(v -> client.ensureIndex()).flatMap(v -> ensureMapping()).subscribe(es -> {
        registerMessageConsumers();
        startFuture.complete();
    }, startFuture::fail);
}
Also used : URL(java.net.URL) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) Function(java.util.function.Function) DefaultQueryCompiler(io.georocket.query.DefaultQueryCompiler) LoggerFactory(io.vertx.core.logging.LoggerFactory) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) Observable(rx.Observable) Single(rx.Single) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) Logger(io.vertx.core.logging.Logger) Resources(com.google.common.io.Resources) ThrowableHelper.throwableToMessage(io.georocket.util.ThrowableHelper.throwableToMessage) IOException(java.io.IOException) ServiceLoader(java.util.ServiceLoader) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) ElasticsearchClientFactory(io.georocket.index.elasticsearch.ElasticsearchClientFactory) List(java.util.List) ElasticsearchClient(io.georocket.index.elasticsearch.ElasticsearchClient) MapUtils(io.georocket.util.MapUtils) ThrowableHelper.throwableToCode(io.georocket.util.ThrowableHelper.throwableToCode) AddressConstants(io.georocket.constants.AddressConstants) DefaultMetaIndexerFactory(io.georocket.index.generic.DefaultMetaIndexerFactory) ConfigConstants(io.georocket.constants.ConfigConstants) ElasticsearchClientFactory(io.georocket.index.elasticsearch.ElasticsearchClientFactory)

Aggregations

Future (io.vertx.core.Future)370 HttpURLConnection (java.net.HttpURLConnection)195 Handler (io.vertx.core.Handler)174 List (java.util.List)166 Objects (java.util.Objects)164 JsonObject (io.vertx.core.json.JsonObject)163 Promise (io.vertx.core.Promise)160 Vertx (io.vertx.core.Vertx)157 Buffer (io.vertx.core.buffer.Buffer)149 Optional (java.util.Optional)147 Logger (org.slf4j.Logger)136 LoggerFactory (org.slf4j.LoggerFactory)136 CompositeFuture (io.vertx.core.CompositeFuture)127 ClientErrorException (org.eclipse.hono.client.ClientErrorException)127 Map (java.util.Map)122 Span (io.opentracing.Span)117 AsyncResult (io.vertx.core.AsyncResult)112 TracingHelper (org.eclipse.hono.tracing.TracingHelper)98 Constants (org.eclipse.hono.util.Constants)97 ArrayList (java.util.ArrayList)94