Skip to content

Commit

Permalink
Replace Component interface with Verticle. (#133)
Browse files Browse the repository at this point in the history
* replacing Component with  Verticle
* class name fix in server gradle file
  • Loading branch information
kmrdhruv authored Apr 18, 2024
1 parent 6ea6bb5 commit 429a3b5
Show file tree
Hide file tree
Showing 21 changed files with 537 additions and 918 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

public enum ComponentKind {
Server("Server"),
Controller("Controller"),
All("All");
Controller("Controller");

private final String name;
ComponentKind(String name) {
Expand Down
2 changes: 1 addition & 1 deletion server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ reporting {
}

application {
mainClass = "com.flipkart.varadhi.Server"
mainClass = "com.flipkart.varadhi.VaradhiApplication"
}

119 changes: 0 additions & 119 deletions server/src/main/java/com/flipkart/varadhi/RestVerticle.java

This file was deleted.

61 changes: 28 additions & 33 deletions server/src/main/java/com/flipkart/varadhi/VaradhiApplication.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.flipkart.varadhi;


import com.flipkart.varadhi.verticles.webserver.WebServerVerticle;
import com.flipkart.varadhi.core.cluster.MemberInfo;
import com.flipkart.varadhi.cluster.VaradhiClusterManager;
import com.flipkart.varadhi.cluster.custom.VaradhiZkClusterManager;
import com.flipkart.varadhi.components.Component;
import com.flipkart.varadhi.core.cluster.ComponentKind;
import com.flipkart.varadhi.components.controller.Controller;
import com.flipkart.varadhi.components.webserver.WebServer;
import com.flipkart.varadhi.verticles.controller.ControllerVerticle;
import com.flipkart.varadhi.config.AppConfiguration;
import com.flipkart.varadhi.config.MemberConfig;
import com.flipkart.varadhi.exceptions.InvalidConfigException;
Expand All @@ -17,6 +15,7 @@
import io.vertx.config.ConfigRetrieverOptions;
import io.vertx.config.ConfigStoreOptions;
import io.vertx.core.Future;
import io.vertx.core.Verticle;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.eventbus.DeliveryOptions;
Expand All @@ -31,8 +30,8 @@
import org.apache.curator.framework.CuratorFramework;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -48,31 +47,33 @@ public static void main(String[] args) {
String memberId = configuration.getMember().getMemberId();
CoreServices services = new CoreServices(configuration);
VaradhiZkClusterManager clusterManager = getClusterManager(configuration, memberId);
Map<ComponentKind, Component> components = getComponents(configuration, services, clusterManager);

createClusteredVertx(configuration, clusterManager, services, "127.0.0.1").compose(vertx ->
Future.all(components.entrySet().stream()
.map(es -> es.getValue().start(vertx).onComplete(ar -> {
if (ar.succeeded()) {
log.info("component: {} started.", es.getKey());
} else {
log.error("component: {} failed to start. {}", es.getKey(), ar.cause());
}
})).collect(Collectors.toList()))
).onComplete(ar -> {
if (ar.succeeded()) {
log.info("VaradhiApplication Started on {}.", host);
} else {
log.error("VaradhiApplication on host {} failed to start. {} ", host, ar.cause());
}
});
Map<ComponentKind, Verticle> verticles = getComponentVerticles(configuration, services, clusterManager);
createClusteredVertx(configuration, clusterManager, services, host).compose(vertx ->
Future.all(verticles.entrySet().stream()
.map(es -> vertx.deployVerticle(es.getValue()).onComplete(ar -> {
if (ar.succeeded()) {
log.info("component: {} started.", es.getKey());
} else {
log.error("component: {} failed to start. {}", es.getKey(), ar.cause());
}
})).collect(Collectors.toList()))
)
.onSuccess(ar -> log.info("VaradhiApplication Started on {}.", host))
.onFailure(t -> {
log.error("VaradhiApplication on host {} failed to start. {} ", host, t);
log.error("Closing the application.");
System.exit(-1);
});
} catch (Exception e) {
log.error("Failed to initialise the VaradhiApplication.", e);
log.error("Closing the application.");
System.exit(-1);
}
// TODO: check need for shutdown hook
}



private static VaradhiZkClusterManager getClusterManager(AppConfiguration config, String memberId) {
CuratorFramework curatorFramework = CuratorFrameworkCreator.create(config.getZookeeperOptions());
DeliveryOptions deliveryOptions = new DeliveryOptions();
Expand Down Expand Up @@ -143,19 +144,13 @@ public static AppConfiguration readConfigFromFile(String filePath) throws Invali
}
}

private static Map<ComponentKind, Component> getComponents(
private static Map<ComponentKind, Verticle> getComponentVerticles(
AppConfiguration config, CoreServices coreServices, VaradhiClusterManager clusterManager
) {
List<ComponentKind> configuredComponents = Arrays.stream(config.getMember().getRoles()).toList();
//TODO:: check if there is need for ordered sequence of component.
return Arrays.stream(ComponentKind.values())
.filter(kind -> !kind.equals(ComponentKind.All) && (
configuredComponents.contains(ComponentKind.All) || configuredComponents.contains(kind)
))
return Arrays.stream(config.getMember().getRoles()).distinct()
.collect(Collectors.toMap(Function.identity(), kind -> switch (kind) {
case Server -> new WebServer(config, coreServices, clusterManager);
case Controller -> new Controller(config, coreServices, clusterManager);
default -> throw new IllegalArgumentException("Unknown Component Kind: " + kind);
case Server -> new WebServerVerticle(config, coreServices, clusterManager);
case Controller -> new ControllerVerticle(config, coreServices, clusterManager);
}));
}
}
Loading

0 comments on commit 429a3b5

Please sign in to comment.