Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vertx): deploy verticles with async decorated blocking handlers #930

Merged
merged 2 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/main/java/io/cryostat/Cryostat.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@

import com.nimbusds.jose.crypto.bc.BouncyCastleProviderSingleton;
import dagger.Component;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;

class Cryostat {

Expand All @@ -79,8 +81,16 @@ public static void main(String[] args) throws Exception {
client.credentialsManager().load();
client.ruleRegistry().loadRules();
client.ruleProcessor().enable();
client.httpServer().start();
client.webServer().start();
client.vertx()
.deployVerticle(
client.httpServer(),
new DeploymentOptions().setWorker(false),
res -> logger.info("HTTP Server Verticle Started"));
client.vertx()
.deployVerticle(
client.webServer(),
new DeploymentOptions().setWorker(true),
hareetd marked this conversation as resolved.
Show resolved Hide resolved
res -> logger.info("WebServer Verticle Started"));
client.messagingServer().start();
client.platformClient().start();
client.recordingMetadataManager().load();
Expand All @@ -97,6 +107,8 @@ interface Client {

RuleProcessor ruleProcessor();

Vertx vertx();

HttpServer httpServer();

WebServer webServer();
Expand Down
41 changes: 22 additions & 19 deletions src/main/java/io/cryostat/net/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

import io.cryostat.core.log.Logger;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.ServerWebSocket;

public class HttpServer {
public class HttpServer extends AbstractVerticle {

private final NetworkConfiguration netConf;
private final SslConfiguration sslConf;
Expand Down Expand Up @@ -97,37 +98,39 @@ public void removeShutdownListener(Runnable runnable) {
this.shutdownListeners.remove(runnable);
}

public Future<Void> start() throws SocketException, UnknownHostException {
@Override
public void start(Future<Void> future) {
if (isAlive()) {
return CompletableFuture.failedFuture(new IllegalStateException("Already started"));
future.fail(new IllegalStateException("Already started"));
return;
}

CompletableFuture<Void> future = new CompletableFuture<>();
this.server
.requestHandler(requestHandlerDelegate)
.webSocketHandler(websocketHandlerDelegate)
.listen(
res -> {
if (res.failed()) {
future.completeExceptionally(res.cause());
future.fail(res.cause());
return;
}
future.complete(null);
});

future.join(); // wait for async deployment to complete

logger.info(
"{} service running on {}://{}:{}",
isSsl() ? "HTTPS" : "HTTP",
isSsl() ? "https" : "http",
netConf.getWebServerHost(),
netConf.getExternalWebServerPort());
this.isAlive = true;

return CompletableFuture.completedFuture(null);
try {
logger.info(
"{} service running on {}://{}:{}",
isSsl() ? "HTTPS" : "HTTP",
isSsl() ? "https" : "http",
netConf.getWebServerHost(),
netConf.getExternalWebServerPort());
this.isAlive = true;
future.complete();
} catch (SocketException | UnknownHostException e) {
future.fail(e);
}
});
}

@Override
public void stop() {
if (!isAlive()) {
return;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/cryostat/net/web/WebModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public abstract class WebModule {
public static final String WEBSERVER_TEMP_DIR_PATH = "WEBSERVER_TEMP_DIR_PATH";

@Provides
@Singleton
hareetd marked this conversation as resolved.
Show resolved Hide resolved
static WebServer provideWebServer(
HttpServer httpServer,
NetworkConfiguration netConf,
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/io/cryostat/net/web/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@

import com.google.gson.Gson;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpHeaders;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.impl.HttpStatusException;
import io.vertx.ext.web.impl.BlockingHandlerDecorator;
import jdk.jfr.Category;
import jdk.jfr.Event;
import jdk.jfr.Label;
Expand All @@ -85,7 +87,7 @@
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.EnglishReasonPhraseCatalog;

public class WebServer {
public class WebServer extends AbstractVerticle {

// Use X- prefix so as to not trigger web-browser auth dialogs
public static final String AUTH_SCHEME_HEADER = "X-WWW-Authenticate";
Expand Down Expand Up @@ -113,6 +115,7 @@ public class WebServer {
this.logger = logger;
}

@Override
public void start() throws FlightRecorderException, SocketException, UnknownHostException {
Router router =
Router.router(server.getVertx()); // a vertx is only available after server started
Expand Down Expand Up @@ -206,7 +209,9 @@ public void start() throws FlightRecorderException, SocketException, UnknownHost
if (handler.isAsync()) {
route = route.handler(handler);
} else {
route = route.blockingHandler(handler, handler.isOrdered());
BlockingHandlerDecorator async =
new BlockingHandlerDecorator(handler, handler.isAsync());
route = route.handler(async);
}
route = route.failureHandler(failureHandler);
if (!handler.isAvailable()) {
Expand Down Expand Up @@ -272,6 +277,7 @@ public void setStatusCode(int code) {
}
}

@Override
public void stop() {
this.server.requestHandler(null);
}
Expand Down