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);
}
}
}
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 :-(
}
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()));
}
});
}
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()));
}
});
}
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;
}
Aggregations