diff --git a/platform-hybrid-service-framework/build.gradle.kts b/platform-hybrid-service-framework/build.gradle.kts new file mode 100644 index 0000000..2d3b041 --- /dev/null +++ b/platform-hybrid-service-framework/build.gradle.kts @@ -0,0 +1,15 @@ +plugins { + `java-library` + jacoco + id("org.hypertrace.publish-plugin") + id("org.hypertrace.jacoco-report-plugin") +} + +dependencies { + api(project(":platform-grpc-service-framework")) + api(project(":platform-http-service-framework")) + api(project(":platform-service-framework")) + + annotationProcessor("org.projectlombok:lombok:1.18.24") + compileOnly("org.projectlombok:lombok:1.18.24") +} diff --git a/platform-hybrid-service-framework/src/main/java/org/hypertrace/core/serviceframework/hybrid/HybridHttpHandlerFactory.java b/platform-hybrid-service-framework/src/main/java/org/hypertrace/core/serviceframework/hybrid/HybridHttpHandlerFactory.java new file mode 100644 index 0000000..08e7c42 --- /dev/null +++ b/platform-hybrid-service-framework/src/main/java/org/hypertrace/core/serviceframework/hybrid/HybridHttpHandlerFactory.java @@ -0,0 +1,10 @@ +package org.hypertrace.core.serviceframework.hybrid; + +import java.util.List; +import org.hypertrace.core.serviceframework.http.HttpHandlerDefinition; + +@FunctionalInterface +public interface HybridHttpHandlerFactory { + + List buildHandlers(HybridServiceContainerEnvironment containerEnvironment); +} diff --git a/platform-hybrid-service-framework/src/main/java/org/hypertrace/core/serviceframework/hybrid/HybridPlatformService.java b/platform-hybrid-service-framework/src/main/java/org/hypertrace/core/serviceframework/hybrid/HybridPlatformService.java new file mode 100644 index 0000000..8bca9a8 --- /dev/null +++ b/platform-hybrid-service-framework/src/main/java/org/hypertrace/core/serviceframework/hybrid/HybridPlatformService.java @@ -0,0 +1,79 @@ +package org.hypertrace.core.serviceframework.hybrid; + +import com.google.common.collect.Streams; +import io.grpc.protobuf.services.HealthStatusManager; +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; +import lombok.extern.slf4j.Slf4j; +import org.hypertrace.core.grpcutils.client.InProcessGrpcChannelRegistry; +import org.hypertrace.core.serviceframework.config.ConfigClient; +import org.hypertrace.core.serviceframework.grpc.GrpcPlatformServerDefinition; +import org.hypertrace.core.serviceframework.grpc.StandAloneGrpcPlatformServiceContainer; +import org.hypertrace.core.serviceframework.http.HttpContainer; +import org.hypertrace.core.serviceframework.http.HttpHandlerDefinition; +import org.hypertrace.core.serviceframework.http.HttpHandlerFactory; +import org.hypertrace.core.serviceframework.http.jetty.JettyHttpServerBuilder; + +@Slf4j +public abstract class HybridPlatformService extends StandAloneGrpcPlatformServiceContainer { + + private HttpContainer httpContainer; + + public HybridPlatformService(ConfigClient configClient) { + super(configClient); + } + + @Override + protected void doStart() { + this.httpContainer.start(); + super.doStart(); + } + + @Override + protected void doStop() { + this.httpContainer.stop(); + super.doStop(); + } + + protected abstract List getServerDefinitions(); + + protected List getHttpHandlerFactories() { + return List.of(); + } + + protected List getHybridHttpHandlerFactories() { + return List.of(); + } + + @Override + protected HybridServiceContainerEnvironment buildContainerEnvironment( + InProcessGrpcChannelRegistry channelRegistry, HealthStatusManager healthStatusManager) { + HybridServiceContainerEnvironment containerEnvironment = + new StandAloneHybridServiceContainerEnvironment( + channelRegistry, + healthStatusManager, + this.configClient, + this.getInProcessServerName(), + this.getLifecycle()); + this.httpContainer = this.buildHttpContainer(containerEnvironment); + return containerEnvironment; + } + + private HttpContainer buildHttpContainer(HybridServiceContainerEnvironment environment) { + return new JettyHttpServerBuilder() + .addHandlers(this.buildHandlerDefinitions(environment)) + .build(); + } + + private List buildHandlerDefinitions( + HybridServiceContainerEnvironment environment) { + return Streams.concat( + this.getHttpHandlerFactories().stream() + .map(handlerFactory -> handlerFactory.buildHandlers(environment)), + this.getHybridHttpHandlerFactories().stream() + .map(handlerFactory -> handlerFactory.buildHandlers(environment))) + .flatMap(Collection::stream) + .collect(Collectors.toUnmodifiableList()); + } +} diff --git a/platform-hybrid-service-framework/src/main/java/org/hypertrace/core/serviceframework/hybrid/HybridServiceContainerEnvironment.java b/platform-hybrid-service-framework/src/main/java/org/hypertrace/core/serviceframework/hybrid/HybridServiceContainerEnvironment.java new file mode 100644 index 0000000..640e086 --- /dev/null +++ b/platform-hybrid-service-framework/src/main/java/org/hypertrace/core/serviceframework/hybrid/HybridServiceContainerEnvironment.java @@ -0,0 +1,7 @@ +package org.hypertrace.core.serviceframework.hybrid; + +import org.hypertrace.core.serviceframework.grpc.GrpcServiceContainerEnvironment; +import org.hypertrace.core.serviceframework.http.HttpContainerEnvironment; + +public interface HybridServiceContainerEnvironment + extends GrpcServiceContainerEnvironment, HttpContainerEnvironment {} diff --git a/platform-hybrid-service-framework/src/main/java/org/hypertrace/core/serviceframework/hybrid/StandAloneHybridServiceContainerEnvironment.java b/platform-hybrid-service-framework/src/main/java/org/hypertrace/core/serviceframework/hybrid/StandAloneHybridServiceContainerEnvironment.java new file mode 100644 index 0000000..e923c13 --- /dev/null +++ b/platform-hybrid-service-framework/src/main/java/org/hypertrace/core/serviceframework/hybrid/StandAloneHybridServiceContainerEnvironment.java @@ -0,0 +1,33 @@ +package org.hypertrace.core.serviceframework.hybrid; + +import com.typesafe.config.Config; +import io.grpc.health.v1.HealthCheckResponse.ServingStatus; +import io.grpc.protobuf.services.HealthStatusManager; +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.hypertrace.core.grpcutils.client.InProcessGrpcChannelRegistry; +import org.hypertrace.core.serviceframework.config.ConfigClient; +import org.hypertrace.core.serviceframework.spi.PlatformServiceLifecycle; + +@AllArgsConstructor +class StandAloneHybridServiceContainerEnvironment implements HybridServiceContainerEnvironment { + + @Getter private final InProcessGrpcChannelRegistry channelRegistry; + private final HealthStatusManager healthStatusManager; + + private final ConfigClient configClient; + + @Getter private final String inProcessChannelName; + + @Getter private final PlatformServiceLifecycle lifecycle; + + @Override + public void reportServiceStatus(String serviceName, ServingStatus status) { + this.healthStatusManager.setStatus(serviceName, status); + } + + @Override + public Config getConfig(String serviceName) { + return this.configClient.getConfig(serviceName, null, null, null); + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index d54c8f8..651dfb6 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -14,6 +14,7 @@ plugins { include(":platform-grpc-service-framework") include(":platform-http-service-framework") +include(":platform-hybrid-service-framework") include(":platform-service-framework") include(":platform-metrics") include(":docstore-metrics")