Skip to content

Commit b3f39e7

Browse files
refactor: use container-provided channel registry (#106)
* refactor: use container-provided channel registry * fix: update snyk ignore * ci: update test publish plugin
1 parent 25000d1 commit b3f39e7

File tree

10 files changed

+40
-51
lines changed

10 files changed

+40
-51
lines changed

.github/workflows/pr-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ jobs:
4040
flags: unit
4141

4242
- name: Publish Unit Test Results
43-
uses: docker://ghcr.io/enricomi/publish-unit-test-result-action:v1.6
43+
uses: docker://ghcr.io/enricomi/publish-unit-test-result-action:v2
4444
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository
4545
with:
4646
github_token: ${{ secrets.GITHUB_TOKEN }}
47-
files: ./**/build/test-results/**/*.xml
47+
junit_files: ./**/build/test-results/**/*.xml

.snyk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ ignore:
55
SNYK-JAVA-IONETTY-1042268:
66
- '*':
77
reason: No replacement available
8-
expires: 2022-07-31T00:00:00.000Z
8+
expires: 2022-10-31T00:00:00.000Z
99
patch: {}
1010

hypertrace-core-graphql-grpc-utils/src/main/java/org/hypertrace/core/graphql/utils/grpc/DefaultGrpcChannelRegistry.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
package org.hypertrace.core.graphql.utils.grpc;
22

33
import io.grpc.ManagedChannel;
4-
import javax.inject.Inject;
5-
import javax.inject.Singleton;
6-
import org.hypertrace.core.graphql.spi.lifecycle.GraphQlServiceLifecycle;
74
import org.hypertrace.core.grpcutils.client.GrpcChannelConfig;
85

9-
@Singleton
106
class DefaultGrpcChannelRegistry implements GrpcChannelRegistry {
117

12-
private final org.hypertrace.core.grpcutils.client.GrpcChannelRegistry delegate =
13-
new org.hypertrace.core.grpcutils.client.GrpcChannelRegistry();
8+
private final org.hypertrace.core.grpcutils.client.GrpcChannelRegistry delegate;
149

15-
@Inject
16-
DefaultGrpcChannelRegistry(GraphQlServiceLifecycle serviceLifecycle) {
17-
serviceLifecycle.shutdownCompletion().thenRun(this.delegate::shutdown);
10+
DefaultGrpcChannelRegistry(org.hypertrace.core.grpcutils.client.GrpcChannelRegistry delegate) {
11+
this.delegate = delegate;
1812
}
1913

2014
@Override

hypertrace-core-graphql-grpc-utils/src/main/java/org/hypertrace/core/graphql/utils/grpc/GraphQlGrpcModule.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@
77

88
public class GraphQlGrpcModule extends AbstractModule {
99

10+
private final org.hypertrace.core.grpcutils.client.GrpcChannelRegistry channelRegistry;
11+
12+
public GraphQlGrpcModule(
13+
org.hypertrace.core.grpcutils.client.GrpcChannelRegistry channelRegistry) {
14+
this.channelRegistry = channelRegistry;
15+
}
16+
1017
@Override
1118
protected void configure() {
1219
bind(CallCredentials.class).toInstance(getClientCallCredsProvider().get());
1320
bind(GraphQlGrpcContextBuilder.class).to(DefaultGraphQlGrpcContextBuilder.class);
14-
bind(GrpcChannelRegistry.class).to(DefaultGrpcChannelRegistry.class);
21+
bind(GrpcChannelRegistry.class)
22+
.toInstance(new DefaultGrpcChannelRegistry(this.channelRegistry));
1523
bind(GrpcContextBuilder.class).to(PlatformGrpcContextBuilder.class);
1624
}
1725
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
11
package org.hypertrace.core.graphql.utils.grpc;
22

3-
import static org.junit.jupiter.api.Assertions.assertFalse;
43
import static org.junit.jupiter.api.Assertions.assertNotNull;
54
import static org.junit.jupiter.api.Assertions.assertNotSame;
65
import static org.junit.jupiter.api.Assertions.assertSame;
7-
import static org.junit.jupiter.api.Assertions.assertThrows;
8-
import static org.junit.jupiter.api.Assertions.assertTrue;
9-
import static org.mockito.Mockito.when;
106

117
import io.grpc.Channel;
12-
import io.grpc.ManagedChannel;
13-
import java.util.concurrent.CompletableFuture;
14-
import org.hypertrace.core.graphql.spi.lifecycle.GraphQlServiceLifecycle;
8+
import org.hypertrace.core.grpcutils.client.GrpcChannelRegistry;
159
import org.junit.jupiter.api.BeforeEach;
1610
import org.junit.jupiter.api.Test;
1711
import org.junit.jupiter.api.extension.ExtendWith;
18-
import org.mockito.Mock;
1912
import org.mockito.junit.jupiter.MockitoExtension;
2013

2114
@ExtendWith(MockitoExtension.class)
2215
class DefaultGrpcChannelRegistryTest {
23-
24-
@Mock GraphQlServiceLifecycle mockLifecycle;
25-
CompletableFuture<Void> shutdown;
26-
2716
DefaultGrpcChannelRegistry channelRegistry;
2817

2918
@BeforeEach
3019
void beforeEach() {
31-
this.shutdown = new CompletableFuture<>();
32-
when(this.mockLifecycle.shutdownCompletion()).thenReturn(this.shutdown);
33-
this.channelRegistry = new DefaultGrpcChannelRegistry(this.mockLifecycle);
20+
this.channelRegistry = new DefaultGrpcChannelRegistry(new GrpcChannelRegistry());
3421
}
3522

3623
@Test
@@ -45,21 +32,4 @@ void reusesChannelsForDuplicateRequests() {
4532
assertNotSame(firstChannel, this.channelRegistry.forAddress("foo", 1001));
4633
assertNotSame(firstChannel, this.channelRegistry.forAddress("bar", 1000));
4734
}
48-
49-
@Test
50-
void shutdownAllChannelsOnLifecycleShutdown() {
51-
ManagedChannel firstChannel = this.channelRegistry.forAddress("foo", 1000);
52-
ManagedChannel secondChannel = this.channelRegistry.forAddress("foo", 1002);
53-
assertFalse(firstChannel.isShutdown());
54-
assertFalse(secondChannel.isShutdown());
55-
this.shutdown.complete(null);
56-
assertTrue(firstChannel.isShutdown());
57-
assertTrue(secondChannel.isShutdown());
58-
}
59-
60-
@Test
61-
void throwsIfNewChannelRequestedAfterLifecycleShutdown() {
62-
this.shutdown.complete(null);
63-
assertThrows(AssertionError.class, () -> this.channelRegistry.forAddress("foo", 1000));
64-
}
6535
}

hypertrace-core-graphql-impl/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77
dependencies {
88
api(project(":hypertrace-core-graphql-spi"))
99
api("com.graphql-java-kickstart:graphql-java-servlet")
10+
api("org.hypertrace.core.grpcutils:grpc-client-utils")
1011

1112
implementation(project(":hypertrace-core-graphql-schema-registry"))
1213
implementation(project(":hypertrace-core-graphql-context"))

hypertrace-core-graphql-impl/src/main/java/org/hypertrace/core/graphql/impl/GraphQlFactory.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
import org.hypertrace.core.graphql.context.GraphQlRequestContextBuilder;
88
import org.hypertrace.core.graphql.spi.config.GraphQlServiceConfig;
99
import org.hypertrace.core.graphql.spi.lifecycle.GraphQlServiceLifecycle;
10+
import org.hypertrace.core.grpcutils.client.GrpcChannelRegistry;
1011

1112
public class GraphQlFactory {
1213
public static GraphQLConfiguration build(
13-
GraphQlServiceConfig config, GraphQlServiceLifecycle lifecycle) {
14-
final Injector injector = Guice.createInjector(new GraphQlModule(config, lifecycle));
14+
GraphQlServiceConfig config,
15+
GraphQlServiceLifecycle lifecycle,
16+
GrpcChannelRegistry grpcChannelRegistry) {
17+
final Injector injector =
18+
Guice.createInjector(new GraphQlModule(config, lifecycle, grpcChannelRegistry));
1519

1620
return GraphQLConfiguration.with(injector.getInstance(GraphQLSchema.class))
1721
.with(injector.getInstance(GraphQlRequestContextBuilder.class))

hypertrace-core-graphql-impl/src/main/java/org/hypertrace/core/graphql/impl/GraphQlModule.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,29 @@
1919
import org.hypertrace.core.graphql.utils.gateway.GatewayUtilsModule;
2020
import org.hypertrace.core.graphql.utils.grpc.GraphQlGrpcModule;
2121
import org.hypertrace.core.graphql.utils.schema.SchemaUtilsModule;
22+
import org.hypertrace.core.grpcutils.client.GrpcChannelRegistry;
2223

2324
class GraphQlModule extends AbstractModule {
2425

2526
private final GraphQlServiceConfig config;
2627
private final GraphQlServiceLifecycle lifecycle;
28+
private final org.hypertrace.core.grpcutils.client.GrpcChannelRegistry grpcChannelRegistry;
2729

28-
public GraphQlModule(final GraphQlServiceConfig config, final GraphQlServiceLifecycle lifecycle) {
30+
public GraphQlModule(
31+
final GraphQlServiceConfig config,
32+
final GraphQlServiceLifecycle lifecycle,
33+
final GrpcChannelRegistry grpcChannelRegistry) {
2934
this.config = config;
3035
this.lifecycle = lifecycle;
36+
this.grpcChannelRegistry = grpcChannelRegistry;
3137
}
3238

3339
@Override
3440
protected void configure() {
3541
bind(GraphQlServiceConfig.class).toInstance(this.config);
3642
bind(GraphQlServiceLifecycle.class).toInstance(this.lifecycle);
3743
install(new GraphQlRequestContextModule());
38-
install(new GraphQlGrpcModule());
44+
install(new GraphQlGrpcModule(this.grpcChannelRegistry));
3945
install(new GraphQlSchemaRegistryModule());
4046
install(new GraphQlDeserializationRegistryModule());
4147
install(new CommonSchemaModule());

hypertrace-core-graphql-impl/src/test/java/org/hypertrace/core/graphql/impl/GraphQlModuleTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.google.inject.Guice;
77
import org.hypertrace.core.graphql.spi.config.GraphQlServiceConfig;
88
import org.hypertrace.core.graphql.spi.lifecycle.GraphQlServiceLifecycle;
9+
import org.hypertrace.core.grpcutils.client.GrpcChannelRegistry;
910
import org.junit.jupiter.api.Test;
1011

1112
public class GraphQlModuleTest {
@@ -16,7 +17,9 @@ public void testResolveBindings() {
1617
() ->
1718
Guice.createInjector(
1819
new GraphQlModule(
19-
mock(GraphQlServiceConfig.class), mock(GraphQlServiceLifecycle.class)))
20+
mock(GraphQlServiceConfig.class),
21+
mock(GraphQlServiceLifecycle.class),
22+
mock(GrpcChannelRegistry.class)))
2023
.getAllBindings());
2124
}
2225
}

hypertrace-core-graphql-service/src/main/java/org/hypertrace/core/graphql/service/GraphQlService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ List<HttpHandlerDefinition> buildHandlerDefinition(HttpContainerEnvironment envi
3434
.port(config.getServicePort())
3535
.contextPath(config.getGraphQlUrlPath())
3636
.corsConfig(buildCorsConfig(config))
37-
.servlet(new GraphQlServiceHttpServlet(GraphQlFactory.build(config, serviceLifecycle)))
37+
.servlet(
38+
new GraphQlServiceHttpServlet(
39+
GraphQlFactory.build(
40+
config, serviceLifecycle, environment.getChannelRegistry())))
3841
.build());
3942
}
4043

0 commit comments

Comments
 (0)