Skip to content

Commit

Permalink
Initial commit Vert.x Netty PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce committed May 12, 2017
1 parent ae8eeeb commit 2c16ff2
Show file tree
Hide file tree
Showing 24 changed files with 999 additions and 1,730 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ install:
# unshallow is needed by license-maven-plugin
- git fetch origin --unshallow
script:
- ./mvnw -s .travis.maven.settings.xml verify -Dtest-env=perf -Dsrcdeps.quiet=false
# - ./mvnw -s .travis.maven.settings.xml verify -Dtest-env=perf -Dsrcdeps.quiet=false
# - ./mvnw -s .travis.maven.settings.xml verify -Pitest
env:
global:
Expand Down
2 changes: 1 addition & 1 deletion hawkular-alerts-actions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<parent>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-reactor-netty-poc</artifactId>
<artifactId>hawkular-alerts-vertx-netty-poc</artifactId>
<version>1.7.0.Final-SNAPSHOT</version>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion hawkular-alerts-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<parent>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-reactor-netty-poc</artifactId>
<artifactId>hawkular-alerts-vertx-netty-poc</artifactId>
<version>1.7.0.Final-SNAPSHOT</version>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion hawkular-alerts-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>hawkular-alerts-reactor-netty-poc</artifactId>
<artifactId>hawkular-alerts-vertx-netty-poc</artifactId>
<groupId>org.hawkular.alerts</groupId>
<version>1.7.0.Final-SNAPSHOT</version>
</parent>
Expand Down
2 changes: 1 addition & 1 deletion hawkular-alerts-engine-extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<parent>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-reactor-netty-poc</artifactId>
<artifactId>hawkular-alerts-vertx-netty-poc</artifactId>
<version>1.7.0.Final-SNAPSHOT</version>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion hawkular-alerts-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<parent>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-reactor-netty-poc</artifactId>
<artifactId>hawkular-alerts-vertx-netty-poc</artifactId>
<version>1.7.0.Final-SNAPSHOT</version>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion hawkular-alerts-filter-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<parent>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-reactor-netty-poc</artifactId>
<artifactId>hawkular-alerts-vertx-netty-poc</artifactId>
<version>1.7.0.Final-SNAPSHOT</version>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion hawkular-alerts-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<parent>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-reactor-netty-poc</artifactId>
<artifactId>hawkular-alerts-vertx-netty-poc</artifactId>
<version>1.7.0.Final-SNAPSHOT</version>
</parent>

Expand Down
22 changes: 7 additions & 15 deletions hawkular-alerts-netty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-reactor-netty-poc</artifactId>
<artifactId>hawkular-alerts-vertx-netty-poc</artifactId>
<version>1.7.0.Final-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down Expand Up @@ -52,23 +52,15 @@
</dependency>

<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>${version.io.projectreactor}</version>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${version.io.vertx}</version>
</dependency>

<dependency>
<groupId>io.projectreactor.ipc</groupId>
<artifactId>reactor-netty</artifactId>
<version>${version.io.projectreactor.ipc}</version>
</dependency>

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<version>${version.io.netty}</version>
<classifier>linux-x86_64</classifier>
<scope>runtime</scope>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${version.io.vertx}</version>
</dependency>

<dependency>
Expand Down
1 change: 1 addition & 0 deletions hawkular-alerts-netty/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fi

JGROUPS_BIND_ADDR="127.0.0.1"
JAVA_OPTS="$JAVA_OPTS -Xmx64m -Xms64m -Djava.net.preferIPv4Stack=true -Djgroups.bind_addr=${JGROUPS_BIND_ADDR}"
JAVA_OPTS="$JAVA_OPTS -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory"
# JAVA_OPTS="$JAVA_OPTS -agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=y"

CLUSTER=$1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import org.hawkular.alerts.properties.AlertProperties;
import org.jboss.logging.Logger;

import reactor.ipc.netty.NettyContext;
import reactor.ipc.netty.http.server.HttpServer;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;

/**
* @author Jay Shaughnessy
Expand All @@ -28,7 +28,8 @@ public class AlertingServer implements AlertingServerMBean {
private static final String PORT_DEFAULT = "8080";
private static final String JMX_NAME = "org.hawkular.alerting:name=AlertingServer";

private NettyContext context;
private Vertx vertx;
private HttpServer server;
private HandlersManager handlers;
private AlertingServerThreadFactory threadFactory;
private ExecutorService executor;
Expand All @@ -44,15 +45,13 @@ public void start() {
StandaloneAlerts.start();
StandaloneActionPluginRegister.setExecutor(executor);
StandaloneActionPluginRegister.start();
handlers = new HandlersManager();
handlers.start();

vertx = Vertx.vertx();
handlers = new HandlersManager(vertx);
handlers.start();
server = vertx.createHttpServer();
log.infof("Starting Server at http://%s:%s", bindAdress, port);
context = HttpServer.create(bindAdress, port)
.newRouter(r -> r
.route(req -> true, (req, resp) -> handlers.process(req, resp)))
.block();
context.onClose().block();
server.requestHandler(handlers::handle).listen(port, bindAdress);
} catch (Exception e) {
log.fatal(e);
log.fatal("Forcing exit");
Expand All @@ -64,11 +63,12 @@ public void start() {
}

public String getStatus() {
return context != null ? "STARTED" : "STOPPED";
return server != null ? "STARTED" : "STOPPED";
}

public void stop() {
log.info("Stopping Server");
server.close();
StandaloneActionPluginRegister.stop();
StandaloneAlerts.stop();
log.info("Server stopped");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package org.hawkular.alerts.netty;

import static org.hawkular.alerts.netty.util.ResponseUtil.badRequest;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.hawkular.alerts.log.MsgLogger;
import org.hawkular.alerts.properties.AlertProperties;
import org.jboss.logging.Logger;
import org.reactivestreams.Publisher;

import io.netty.handler.codec.http.QueryStringDecoder;
import reactor.ipc.netty.http.server.HttpServerRequest;
import reactor.ipc.netty.http.server.HttpServerResponse;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.Router;

/**
* @author Jay Shaughnessy
Expand All @@ -28,54 +24,29 @@ public class HandlersManager {
private static final String BASE_URL = "hawkular-alerts.base-url";
private static final String BASE_URL_DEFAULT = "/hawkular/alerts";
public static final String TENANT_HEADER_NAME = "Hawkular-Tenant";
public static final String ROOT = "/";

private Router router;
private String baseUrl = AlertProperties.getProperty(BASE_URL, BASE_URL_DEFAULT);
private Map<String, RestHandler> endpoints = new HashMap<>();
private ClassLoader cl = Thread.currentThread().getContextClassLoader();

public HandlersManager(Vertx vertx) {
this.router = Router.router(vertx);
}

public void start() {
try {
scan();
log.info("Netty Handlers scan finished");
endpoints.entrySet().stream().forEach(endpoint -> endpoint.getValue().initRoutes(baseUrl, router));
} catch (IOException e) {
log.error(e.getMessage());
}
}

public Publisher<Void> process(HttpServerRequest req, HttpServerResponse resp) {
QueryStringDecoder query = new QueryStringDecoder(req.uri());
String path = query.path();
Map<String, List<String>> params = query.parameters();
log.debugf("%s %s %s", req.method().name(), path, params);
if (path.length() >= baseUrl.length()) {
String base = query.path().substring(0, baseUrl.length());
if (baseUrl.equals(base)) {
String endpoint = query.path().substring(baseUrl.length());
String subpath = endpoint;
if (endpoint.lastIndexOf('/') > 0) {
endpoint = endpoint.substring(0, endpoint.indexOf('/', 1));
}
subpath = subpath.substring(endpoint.length());
if (endpoint.isEmpty()) {
endpoint = ROOT;
}
if (subpath.isEmpty()) {
subpath = ROOT;
}
if (endpoints.get(endpoint) != null) {
return endpoints.get(endpoint).process(req, resp, tenantId(req), subpath, params);
} else {
subpath = subpath.equals(ROOT) ? endpoint : endpoint + subpath;
return endpoints.get(ROOT).process(req, resp, tenantId(req), subpath, params);
}
}
}
return badRequest(resp, "Endpoint [" + path + "] is not supported.");
}

public String tenantId(HttpServerRequest req) {
return req.requestHeaders().get(TENANT_HEADER_NAME);
public void handle(HttpServerRequest req) {
log.debugf("%s %s %s", req.method().name(), req.path(), req.params());
router.accept(req);
}

private void scan() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
package org.hawkular.alerts.netty;

import java.util.List;
import java.util.Map;

import org.reactivestreams.Publisher;

import reactor.ipc.netty.http.server.HttpServerRequest;
import reactor.ipc.netty.http.server.HttpServerResponse;
import io.vertx.ext.web.Router;

/**
* @author Jay Shaughnessy
* @author Lucas Ponce
*/
public interface RestHandler {

Publisher<Void> process(HttpServerRequest req,
HttpServerResponse resp,
String tenantId,
String subpath,
Map<String, List<String>> params);
void initRoutes(String baseUrl, Router router);
}

0 comments on commit 2c16ff2

Please sign in to comment.