Search in sources :

Example 51 with AsyncResult

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

the class ConfigMapWatcher method eventReceived.

public void eventReceived(Action action, ConfigMap configMap) {
    ObjectMeta metadata = configMap.getMetadata();
    Map<String, String> labels = metadata.getLabels();
    if (cmPredicate.test(configMap)) {
        String name = metadata.getName();
        LOGGER.info("ConfigMap watch received event {} on map {} with labels {}", action, name, labels);
        Handler<AsyncResult<Void>> resultHandler = ar -> {
            if (ar.succeeded()) {
                LOGGER.info("Success processing ConfigMap watch event {} on map {} with labels {}", action, name, labels);
            } else {
                String message;
                if (ar.cause() instanceof InvalidConfigMapException) {
                    message = "ConfigMap " + name + " has an invalid 'data' section: " + ar.cause().getMessage();
                    LOGGER.error("{}", message);
                } else {
                    message = "Failure processing ConfigMap watch event " + action + " on map " + name + " with labels " + labels + ": " + ar.cause().getMessage();
                    LOGGER.error("{}", message, ar.cause());
                }
                controller.enqueue(controller.new Event(configMap, message, Controller.EventType.WARNING, errorResult -> {
                }));
            }
        };
        switch(action) {
            case ADDED:
                controller.onConfigMapAdded(configMap, resultHandler);
                break;
            case MODIFIED:
                controller.onConfigMapModified(configMap, resultHandler);
                break;
            case DELETED:
                controller.onConfigMapDeleted(configMap, resultHandler);
                break;
            case ERROR:
                LOGGER.error("Watch received action=ERROR for ConfigMap " + name);
        }
    }
}
Also used : KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Logger(org.slf4j.Logger) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Map(java.util.Map) LoggerFactory(org.slf4j.LoggerFactory) Watcher(io.fabric8.kubernetes.client.Watcher) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) AsyncResult(io.vertx.core.AsyncResult)

Example 52 with AsyncResult

use of io.vertx.core.AsyncResult 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 53 with AsyncResult

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

the class DistributedPublish method setup.

void setup(Handler<AsyncResult<Void>> handler) {
    EventBus eventBus = vertx.eventBus();
    MessageConsumer<String> consumer = createMessageConsumer(eventBus, address);
    consumer.handler(msg -> {
        eventBus.send(msg.body(), clusterManager.getNodeID());
    }).completionHandler(ar -> {
        if (ar.succeeded()) {
            handler.handle(Future.succeededFuture());
        } else {
            handler.handle(Future.failedFuture(ar.cause()));
        }
    });
}
Also used : EventBus(io.vertx.core.eventbus.EventBus) VertxInternal(io.vertx.core.impl.VertxInternal) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) AsyncResult(io.vertx.core.AsyncResult) RoutingContext(io.vertx.ext.web.RoutingContext) UUID(java.util.UUID) Handler(io.vertx.core.Handler) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) Future(io.vertx.core.Future) EventBus(io.vertx.core.eventbus.EventBus)

Example 54 with AsyncResult

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

the class DistributedTimeout method setup.

void setup(Handler<AsyncResult<Void>> handler) {
    EventBus eventBus = vertx.eventBus();
    MessageConsumer<Boolean> consumer = createMessageConsumer(eventBus, address);
    consumer.handler(msg -> {
        Boolean reply = msg.body();
        if (reply) {
            msg.reply(clusterManager.getNodeID());
        }
    }).completionHandler(ar -> {
        if (ar.succeeded()) {
            handler.handle(Future.succeededFuture());
        } else {
            handler.handle(Future.failedFuture(ar.cause()));
        }
    });
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) VertxInternal(io.vertx.core.impl.VertxInternal) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) RoutingContext(io.vertx.ext.web.RoutingContext) Future(io.vertx.core.Future) EventBus(io.vertx.core.eventbus.EventBus) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) ReplyException(io.vertx.core.eventbus.ReplyException) EventBus(io.vertx.core.eventbus.EventBus)

Example 55 with AsyncResult

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

the class StoreEndpoint method detectContentType.

/**
 * Try to detect the content type of a file
 * @param filepath the absolute path to the file to analyse
 * @return an observable emitting either the detected content type or an error
 * if the content type could not be detected or the file could not be read
 */
private Observable<String> detectContentType(String filepath) {
    ObservableFuture<String> result = RxHelper.observableFuture();
    Handler<AsyncResult<String>> resultHandler = result.toHandler();
    vertx.<String>executeBlocking(f -> {
        try {
            String mimeType = MimeTypeUtils.detect(new File(filepath));
            if (mimeType == null) {
                log.warn("Could not detect file type for " + filepath + ". Using " + "application/octet-stream.");
                mimeType = "application/octet-stream";
            }
            f.complete(mimeType);
        } catch (IOException e) {
            f.fail(e);
        }
    }, ar -> {
        if (ar.failed()) {
            resultHandler.handle(Future.failedFuture(ar.cause()));
        } else {
            String ct = ar.result();
            if (ct != null) {
                resultHandler.handle(Future.succeededFuture(ar.result()));
            } else {
                resultHandler.handle(Future.failedFuture(new HttpException(215)));
            }
        }
    });
    return result;
}
Also used : HttpException(io.georocket.util.HttpException) IOException(java.io.IOException) AsyncResult(io.vertx.core.AsyncResult) AsyncFile(io.vertx.core.file.AsyncFile) File(java.io.File)

Aggregations

AsyncResult (io.vertx.core.AsyncResult)162 Handler (io.vertx.core.Handler)106 Test (org.junit.Test)72 JsonObject (io.vertx.core.json.JsonObject)68 CountDownLatch (java.util.concurrent.CountDownLatch)62 Future (io.vertx.core.Future)59 List (java.util.List)49 RequestParameter (io.vertx.ext.web.api.RequestParameter)48 RequestParameters (io.vertx.ext.web.api.RequestParameters)48 HashMap (java.util.HashMap)45 Map (java.util.Map)42 IOException (java.io.IOException)41 ArrayList (java.util.ArrayList)40 Vertx (io.vertx.core.Vertx)35 Collectors (java.util.stream.Collectors)35 RoutingContext (io.vertx.ext.web.RoutingContext)28 Buffer (io.vertx.core.buffer.Buffer)24 StandardCharsets (java.nio.charset.StandardCharsets)23 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 Consumer (java.util.function.Consumer)23