From 19c7cf5b1e3e9acd3d18e64000d1de5c73d05632 Mon Sep 17 00:00:00 2001 From: Aaron Steinfeld Date: Wed, 27 Jul 2022 01:36:32 -0400 Subject: [PATCH 1/2] refactor: use http service framework --- hypertrace-core-graphql-impl/build.gradle.kts | 2 - .../build.gradle.kts | 8 +- .../build.gradle.kts | 12 +- .../core/graphql/service/GraphQlService.java | 104 +++++------------- 4 files changed, 35 insertions(+), 91 deletions(-) diff --git a/hypertrace-core-graphql-impl/build.gradle.kts b/hypertrace-core-graphql-impl/build.gradle.kts index e93911a2..0db51f7d 100644 --- a/hypertrace-core-graphql-impl/build.gradle.kts +++ b/hypertrace-core-graphql-impl/build.gradle.kts @@ -27,8 +27,6 @@ dependencies { implementation("org.slf4j:slf4j-api") implementation("com.google.inject:guice") - runtimeOnly("io.grpc:grpc-netty") - testImplementation("org.junit.jupiter:junit-jupiter") testImplementation("org.mockito:mockito-core") testImplementation("org.mockito:mockito-junit-jupiter") diff --git a/hypertrace-core-graphql-platform/build.gradle.kts b/hypertrace-core-graphql-platform/build.gradle.kts index a3430fe5..5204a0f5 100644 --- a/hypertrace-core-graphql-platform/build.gradle.kts +++ b/hypertrace-core-graphql-platform/build.gradle.kts @@ -10,10 +10,10 @@ dependencies { api(platform("io.grpc:grpc-bom:1.47.0")) constraints { - api("org.hypertrace.core.grpcutils:grpc-context-utils:0.7.4") - api("org.hypertrace.core.grpcutils:grpc-client-utils:0.7.4") - api("org.hypertrace.core.grpcutils:grpc-client-rx-utils:0.7.4") - api("org.hypertrace.core.grpcutils:grpc-client-rx-utils:0.7.4") + api("org.hypertrace.core.grpcutils:grpc-context-utils:0.7.6") + api("org.hypertrace.core.grpcutils:grpc-client-utils:0.7.6") + api("org.hypertrace.core.grpcutils:grpc-client-rx-utils:0.7.6") + api("org.hypertrace.core.grpcutils:grpc-client-rx-utils:0.7.6") api("org.hypertrace.gateway.service:gateway-service-api:0.2.10") api("org.hypertrace.core.attribute.service:caching-attribute-service-client:0.13.6") diff --git a/hypertrace-core-graphql-service/build.gradle.kts b/hypertrace-core-graphql-service/build.gradle.kts index cb223db8..0514ae77 100644 --- a/hypertrace-core-graphql-service/build.gradle.kts +++ b/hypertrace-core-graphql-service/build.gradle.kts @@ -8,24 +8,18 @@ plugins { dependencies { implementation(platform(project(":hypertrace-core-graphql-platform"))) - implementation("com.typesafe:config") - implementation("org.hypertrace.core.serviceframework:platform-service-framework:0.1.23") + implementation("org.hypertrace.core.serviceframework:platform-http-service-framework:0.2.0-SNAPSHOT") implementation("org.slf4j:slf4j-api") - implementation("org.eclipse.jetty:jetty-server:9.4.42.v20210604") - implementation("org.eclipse.jetty:jetty-servlet:9.4.42.v20210604") - implementation("org.eclipse.jetty:jetty-servlets:9.4.42.v20210604") - implementation("com.graphql-java-kickstart:graphql-java-servlet") implementation(project(":hypertrace-core-graphql-impl")) implementation(project(":hypertrace-core-graphql-spi")) runtimeOnly("org.apache.logging.log4j:log4j-slf4j-impl") + runtimeOnly("io.grpc:grpc-netty") } application { mainClass.set("org.hypertrace.core.serviceframework.PlatformServiceLauncher") } -tasks.run { - jvmArgs = listOf("-Dservice.name=${project.name}") -} + diff --git a/hypertrace-core-graphql-service/src/main/java/org/hypertrace/core/graphql/service/GraphQlService.java b/hypertrace-core-graphql-service/src/main/java/org/hypertrace/core/graphql/service/GraphQlService.java index 51c796c3..671989bf 100644 --- a/hypertrace-core-graphql-service/src/main/java/org/hypertrace/core/graphql/service/GraphQlService.java +++ b/hypertrace-core-graphql-service/src/main/java/org/hypertrace/core/graphql/service/GraphQlService.java @@ -1,95 +1,47 @@ package org.hypertrace.core.graphql.service; -import java.util.EnumSet; -import javax.servlet.DispatcherType; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.servlet.ServletHolder; -import org.eclipse.jetty.servlets.CrossOriginFilter; +import java.util.List; import org.hypertrace.core.graphql.impl.GraphQlFactory; import org.hypertrace.core.graphql.spi.config.GraphQlServiceConfig; -import org.hypertrace.core.serviceframework.PlatformService; import org.hypertrace.core.serviceframework.config.ConfigClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.hypertrace.core.serviceframework.http.HttpContainerEnvironment; +import org.hypertrace.core.serviceframework.http.HttpHandlerDefinition; +import org.hypertrace.core.serviceframework.http.HttpHandlerDefinition.CorsConfig; +import org.hypertrace.core.serviceframework.http.HttpHandlerFactory; +import org.hypertrace.core.serviceframework.http.StandAloneHttpPlatformServiceContainer; -public class GraphQlService extends PlatformService { - - private static final Logger LOG = LoggerFactory.getLogger(GraphQlService.class); - - private GraphQlServiceConfig graphQlServiceConfig; - private DefaultGraphQlServiceLifecycle serviceLifecycle; - private Server server; +public class GraphQlService extends StandAloneHttpPlatformServiceContainer { + private static final String SERVICE_NAME = "hypertrace-core-graphql"; public GraphQlService(ConfigClient configClient) { super(configClient); } @Override - protected void doInit() { - this.graphQlServiceConfig = new DefaultGraphQlServiceConfig(this.getAppConfig()); - this.serviceLifecycle = new DefaultGraphQlServiceLifecycle(); - this.server = new Server(this.graphQlServiceConfig.getServicePort()); - - ServletContextHandler context = new ServletContextHandler(); - if (this.graphQlServiceConfig.isCorsEnabled()) { - context.addFilter( - CrossOriginFilter.class, - this.graphQlServiceConfig.getGraphQlUrlPath(), - EnumSet.of(DispatcherType.REQUEST)); - } - - context.addServlet( - new ServletHolder( - new GraphQlServiceHttpServlet( - GraphQlFactory.build(this.graphQlServiceConfig, this.serviceLifecycle))), - this.graphQlServiceConfig.getGraphQlUrlPath()); - - this.server.setHandler(context); - this.server.setStopAtShutdown(true); + protected List getHandlerFactories() { + return List.of(this::buildHandlerDefinition); } - @Override - protected void doStart() { - LOG.info("Starting service: {}", this.getServiceName()); - - try { - server.start(); - } catch (Exception e) { - LOG.error("Failed to start service: {}", this.getServiceName()); - throw new RuntimeException(e); - } - - try { - server.join(); - } catch (InterruptedException ie) { - Thread.currentThread().interrupt(); - throw new RuntimeException(ie); - } + List buildHandlerDefinition(HttpContainerEnvironment environment) { + GraphQlServiceConfig config = + new DefaultGraphQlServiceConfig(environment.getConfig(SERVICE_NAME)); + DefaultGraphQlServiceLifecycle serviceLifecycle = new DefaultGraphQlServiceLifecycle(); + environment.getLifecycle().shutdownComplete().thenRun(serviceLifecycle::shutdown); + + return List.of( + HttpHandlerDefinition.builder() + .name("graphql") + .port(config.getServicePort()) + .contextPath(config.getGraphQlUrlPath()) + .corsConfig(buildCorsConfig(config)) + .servlet(new GraphQlServiceHttpServlet(GraphQlFactory.build(config, serviceLifecycle))) + .build()); } - @Override - protected void doStop() { - LOG.info("Shutting down service: {}", this.getServiceName()); - if (this.serviceLifecycle != null) { - this.serviceLifecycle.shutdown(); - } - while (!server.isStopped()) { - try { - server.stop(); - } catch (Exception e) { - LOG.error("Failed to shutdown service: {}", this.getServiceName()); - throw new RuntimeException(e); - } + private CorsConfig buildCorsConfig(GraphQlServiceConfig config) { + if (!config.isCorsEnabled()) { + return null; } - try { - Thread.sleep(100); - } catch (InterruptedException ignore) { - } - } - - @Override - public boolean healthCheck() { - return true; + return CorsConfig.builder().allowedHeaders(List.of("*")).build(); } } From 4ea44bb613e5972ecffb33938723024f075c6b89 Mon Sep 17 00:00:00 2001 From: Aaron Steinfeld Date: Wed, 27 Jul 2022 10:06:25 -0400 Subject: [PATCH 2/2] chore: update http framework to released version --- hypertrace-core-graphql-service/build.gradle.kts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hypertrace-core-graphql-service/build.gradle.kts b/hypertrace-core-graphql-service/build.gradle.kts index 0514ae77..1d8cd5df 100644 --- a/hypertrace-core-graphql-service/build.gradle.kts +++ b/hypertrace-core-graphql-service/build.gradle.kts @@ -8,7 +8,7 @@ plugins { dependencies { implementation(platform(project(":hypertrace-core-graphql-platform"))) - implementation("org.hypertrace.core.serviceframework:platform-http-service-framework:0.2.0-SNAPSHOT") + implementation("org.hypertrace.core.serviceframework:platform-http-service-framework:0.1.42") implementation("org.slf4j:slf4j-api") implementation("com.graphql-java-kickstart:graphql-java-servlet") @@ -22,4 +22,3 @@ dependencies { application { mainClass.set("org.hypertrace.core.serviceframework.PlatformServiceLauncher") } -