Skip to content

Commit 49786d0

Browse files
committed
core, netty: server builders extend a public API class
1 parent c76b2bc commit 49786d0

File tree

13 files changed

+253
-114
lines changed

13 files changed

+253
-114
lines changed

core/src/main/java/io/grpc/inprocess/InProcessServerBuilder.java

+33-8
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@
2121
import com.google.common.base.Preconditions;
2222
import io.grpc.Deadline;
2323
import io.grpc.ExperimentalApi;
24+
import io.grpc.ForwardingServerBuilder;
25+
import io.grpc.Internal;
26+
import io.grpc.ServerBuilder;
2427
import io.grpc.ServerStreamTracer;
25-
import io.grpc.internal.AbstractServerImplBuilder;
2628
import io.grpc.internal.FixedObjectPool;
2729
import io.grpc.internal.GrpcUtil;
30+
import io.grpc.internal.InternalServer;
2831
import io.grpc.internal.ObjectPool;
32+
import io.grpc.internal.ServerImplBuilder;
33+
import io.grpc.internal.ServerImplBuilder.ClientTransportServersBuilder;
2934
import io.grpc.internal.SharedResourcePool;
3035
import java.io.File;
3136
import java.util.Collections;
@@ -67,8 +72,7 @@
6772
* </pre>
6873
*/
6974
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1783")
70-
public final class InProcessServerBuilder
71-
extends AbstractServerImplBuilder<InProcessServerBuilder> {
75+
public final class InProcessServerBuilder extends ForwardingServerBuilder<InProcessServerBuilder> {
7276
/**
7377
* Create a server builder that will bind with the given name.
7478
*
@@ -93,22 +97,40 @@ public static String generateName() {
9397
return UUID.randomUUID().toString();
9498
}
9599

100+
private final ServerImplBuilder serverImplBuilder;
96101
final String name;
97102
int maxInboundMetadataSize = Integer.MAX_VALUE;
98103
ObjectPool<ScheduledExecutorService> schedulerPool =
99104
SharedResourcePool.forResource(GrpcUtil.TIMER_SERVICE);
100105

101106
private InProcessServerBuilder(String name) {
102107
this.name = Preconditions.checkNotNull(name, "name");
108+
109+
final class InProcessClientTransportServersBuilder implements ClientTransportServersBuilder {
110+
@Override
111+
public List<? extends InternalServer> buildClientTransportServers(
112+
List<? extends ServerStreamTracer.Factory> streamTracerFactories) {
113+
return buildTransportServers(streamTracerFactories);
114+
}
115+
}
116+
117+
serverImplBuilder = new ServerImplBuilder(new InProcessClientTransportServersBuilder());
118+
103119
// In-process transport should not record its traffic to the stats module.
104120
// https://github.com/grpc/grpc-java/issues/2284
105-
setStatsRecordStartedRpcs(false);
106-
setStatsRecordFinishedRpcs(false);
121+
serverImplBuilder.setStatsRecordStartedRpcs(false);
122+
serverImplBuilder.setStatsRecordFinishedRpcs(false);
107123
// Disable handshake timeout because it is unnecessary, and can trigger Thread creation that can
108124
// break some environments (like tests).
109125
handshakeTimeout(Long.MAX_VALUE, TimeUnit.SECONDS);
110126
}
111127

128+
@Internal
129+
@Override
130+
protected ServerBuilder<?> delegate() {
131+
return serverImplBuilder;
132+
}
133+
112134
/**
113135
* Provides a custom scheduled executor service.
114136
*
@@ -140,7 +162,7 @@ public InProcessServerBuilder scheduledExecutorService(
140162
* @since 1.24.0
141163
*/
142164
public InProcessServerBuilder deadlineTicker(Deadline.Ticker ticker) {
143-
setDeadlineTicker(ticker);
165+
serverImplBuilder.setDeadlineTicker(ticker);
144166
return this;
145167
}
146168

@@ -164,8 +186,7 @@ public InProcessServerBuilder maxInboundMetadataSize(int bytes) {
164186
return this;
165187
}
166188

167-
@Override
168-
protected List<InProcessServer> buildTransportServers(
189+
List<InProcessServer> buildTransportServers(
169190
List<? extends ServerStreamTracer.Factory> streamTracerFactories) {
170191
return Collections.singletonList(new InProcessServer(this, streamTracerFactories));
171192
}
@@ -174,4 +195,8 @@ protected List<InProcessServer> buildTransportServers(
174195
public InProcessServerBuilder useTransportSecurity(File certChain, File privateKey) {
175196
throw new UnsupportedOperationException("TLS not supported in InProcessServer");
176197
}
198+
199+
void setStatsEnabled(boolean value) {
200+
this.serverImplBuilder.setStatsEnabled(value);
201+
}
177202
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2020 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.inprocess;
18+
19+
import io.grpc.Internal;
20+
21+
/**
22+
* Internal {@link InProcessServerBuilder} accessor. This is intended for usage internal to
23+
* the gRPC team. If you *really* think you need to use this, contact the gRPC team first.
24+
*/
25+
@Internal
26+
public class InternalInProcessServerBuilder {
27+
public static void setStatsEnabled(InProcessServerBuilder builder, boolean value) {
28+
builder.setStatsEnabled(value);
29+
}
30+
31+
private InternalInProcessServerBuilder() {}
32+
}

interop-testing/src/main/java/io/grpc/testing/integration/AbstractInteropTest.java

+17-18
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import io.grpc.auth.MoreCallCredentials;
6565
import io.grpc.census.InternalCensusStatsAccessor;
6666
import io.grpc.census.internal.DeprecatedCensusConstants;
67-
import io.grpc.internal.AbstractServerImplBuilder;
6867
import io.grpc.internal.GrpcUtil;
6968
import io.grpc.internal.testing.StatsTestUtils;
7069
import io.grpc.internal.testing.StatsTestUtils.FakeStatsRecorder;
@@ -171,7 +170,6 @@ public abstract class AbstractInteropTest {
171170

172171
private ScheduledExecutorService testServiceExecutor;
173172
private Server server;
174-
private boolean customCensusModulePresent;
175173

176174
private final LinkedBlockingQueue<ServerStreamTracerInfo> serverStreamTracers =
177175
new LinkedBlockingQueue<>();
@@ -245,21 +243,7 @@ private void startServer() {
245243
new TestServiceImpl(testServiceExecutor),
246244
allInterceptors))
247245
.addStreamTracerFactory(serverStreamTracerFactory);
248-
if (builder instanceof AbstractServerImplBuilder) {
249-
customCensusModulePresent = true;
250-
ServerStreamTracer.Factory censusTracerFactory =
251-
InternalCensusStatsAccessor
252-
.getServerStreamTracerFactory(
253-
tagger, tagContextBinarySerializer, serverStatsRecorder,
254-
GrpcUtil.STOPWATCH_SUPPLIER,
255-
true, true, true, false /* real-time metrics */);
256-
AbstractServerImplBuilder<?> sb = (AbstractServerImplBuilder<?>) builder;
257-
io.grpc.internal.TestingAccessor.setStatsEnabled(sb, false);
258-
sb.addStreamTracerFactory(censusTracerFactory);
259-
}
260-
if (metricsExpected()) {
261-
assertThat(builder).isInstanceOf(AbstractServerImplBuilder.class);
262-
}
246+
263247
try {
264248
server = builder.build().start();
265249
} catch (IOException ex) {
@@ -373,6 +357,21 @@ protected final ClientInterceptor createCensusStatsClientInterceptor() {
373357
true, true, true, false /* real-time metrics */);
374358
}
375359

360+
protected final ServerStreamTracer.Factory createCustomCensusTracerFactory() {
361+
return InternalCensusStatsAccessor
362+
.getServerStreamTracerFactory(
363+
tagger, tagContextBinarySerializer, serverStatsRecorder,
364+
GrpcUtil.STOPWATCH_SUPPLIER,
365+
true, true, true, false /* real-time metrics */);
366+
}
367+
368+
/**
369+
* Return {@code true} when custom census module used.
370+
*/
371+
protected boolean customCensusModulePresent() {
372+
return false;
373+
}
374+
376375
/**
377376
* Return true if exact metric values should be checked.
378377
*/
@@ -1510,7 +1509,7 @@ public void customMetadata() throws Exception {
15101509
@Test(timeout = 10000)
15111510
public void censusContextsPropagated() {
15121511
Assume.assumeTrue("Skip the test because server is not in the same process.", server != null);
1513-
Assume.assumeTrue(customCensusModulePresent);
1512+
Assume.assumeTrue(customCensusModulePresent());
15141513
Span clientParentSpan = Tracing.getTracer().spanBuilder("Test.interopTest").startSpan();
15151514
// A valid ID is guaranteed to be unique, so we can verify it is actually propagated.
15161515
assertTrue(clientParentSpan.getContext().getTraceId().isValid());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2020 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.testing.integration;
18+
19+
import io.grpc.netty.InternalNettyServerBuilder;
20+
import io.grpc.netty.NettyServerBuilder;
21+
22+
public abstract class AbstractNettyInteropTest extends AbstractInteropTest {
23+
protected final NettyServerBuilder withCustomCensusModule(NettyServerBuilder builder) {
24+
InternalNettyServerBuilder.setStatsEnabled(builder, false);
25+
builder.addStreamTracerFactory(createCustomCensusTracerFactory());
26+
return builder;
27+
}
28+
29+
@Override
30+
protected boolean customCensusModulePresent() {
31+
return true;
32+
}
33+
}

interop-testing/src/test/java/io/grpc/testing/integration/AutoWindowSizingOnTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package io.grpc.testing.integration;
1818

19-
import io.grpc.internal.AbstractServerImplBuilder;
19+
import io.grpc.ServerBuilder;
2020
import io.grpc.netty.InternalNettyChannelBuilder;
2121
import io.grpc.netty.NegotiationType;
2222
import io.grpc.netty.NettyChannelBuilder;
@@ -25,12 +25,12 @@
2525
import org.junit.runners.JUnit4;
2626

2727
@RunWith(JUnit4.class)
28-
public class AutoWindowSizingOnTest extends AbstractInteropTest {
28+
public class AutoWindowSizingOnTest extends AbstractNettyInteropTest {
2929

3030
@Override
31-
protected AbstractServerImplBuilder<?> getServerBuilder() {
32-
return NettyServerBuilder.forPort(0)
33-
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
31+
protected ServerBuilder<?> getServerBuilder() {
32+
return withCustomCensusModule(
33+
NettyServerBuilder.forPort(0).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE));
3434
}
3535

3636
@Override

interop-testing/src/test/java/io/grpc/testing/integration/Http2NettyLocalChannelTest.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package io.grpc.testing.integration;
1818

19-
import io.grpc.internal.AbstractServerImplBuilder;
19+
import io.grpc.ServerBuilder;
2020
import io.grpc.netty.InternalNettyChannelBuilder;
2121
import io.grpc.netty.NegotiationType;
2222
import io.grpc.netty.NettyChannelBuilder;
@@ -33,19 +33,20 @@
3333
* Run transport tests over the Netty in-process channel.
3434
*/
3535
@RunWith(JUnit4.class)
36-
public class Http2NettyLocalChannelTest extends AbstractInteropTest {
36+
public class Http2NettyLocalChannelTest extends AbstractNettyInteropTest {
3737

3838
private DefaultEventLoopGroup eventLoopGroup = new DefaultEventLoopGroup();
3939

4040
@Override
41-
protected AbstractServerImplBuilder<?> getServerBuilder() {
42-
return NettyServerBuilder
43-
.forAddress(new LocalAddress("in-process-1"))
44-
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW)
45-
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
46-
.channelType(LocalServerChannel.class)
47-
.workerEventLoopGroup(eventLoopGroup)
48-
.bossEventLoopGroup(eventLoopGroup);
41+
protected ServerBuilder<?> getServerBuilder() {
42+
return withCustomCensusModule(
43+
NettyServerBuilder
44+
.forAddress(new LocalAddress("in-process-1"))
45+
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW)
46+
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
47+
.channelType(LocalServerChannel.class)
48+
.workerEventLoopGroup(eventLoopGroup)
49+
.bossEventLoopGroup(eventLoopGroup));
4950
}
5051

5152
@Override

interop-testing/src/test/java/io/grpc/testing/integration/Http2NettyTest.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNotEquals;
2121

22-
import io.grpc.internal.AbstractServerImplBuilder;
22+
import io.grpc.ServerBuilder;
2323
import io.grpc.internal.testing.TestUtils;
2424
import io.grpc.netty.GrpcSslContexts;
2525
import io.grpc.netty.InternalNettyChannelBuilder;
@@ -38,21 +38,22 @@
3838
* Integration tests for GRPC over HTTP2 using the Netty framework.
3939
*/
4040
@RunWith(JUnit4.class)
41-
public class Http2NettyTest extends AbstractInteropTest {
41+
public class Http2NettyTest extends AbstractNettyInteropTest {
4242

4343
@Override
44-
protected AbstractServerImplBuilder<?> getServerBuilder() {
44+
protected ServerBuilder<?> getServerBuilder() {
4545
// Starts the server with HTTPS.
4646
try {
47-
return NettyServerBuilder.forPort(0)
48-
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW)
49-
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
50-
.sslContext(GrpcSslContexts
51-
.forServer(TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key"))
52-
.clientAuth(ClientAuth.REQUIRE)
53-
.trustManager(TestUtils.loadCert("ca.pem"))
54-
.ciphers(TestUtils.preferredTestCiphers(), SupportedCipherSuiteFilter.INSTANCE)
55-
.build());
47+
return withCustomCensusModule(
48+
NettyServerBuilder.forPort(0)
49+
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW)
50+
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
51+
.sslContext(GrpcSslContexts
52+
.forServer(TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key"))
53+
.clientAuth(ClientAuth.REQUIRE)
54+
.trustManager(TestUtils.loadCert("ca.pem"))
55+
.ciphers(TestUtils.preferredTestCiphers(), SupportedCipherSuiteFilter.INSTANCE)
56+
.build()));
5657
} catch (IOException ex) {
5758
throw new RuntimeException(ex);
5859
}

interop-testing/src/test/java/io/grpc/testing/integration/Http2OkHttpTest.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.google.common.base.Throwables;
2424
import com.squareup.okhttp.ConnectionSpec;
2525
import io.grpc.ManagedChannel;
26-
import io.grpc.internal.AbstractServerImplBuilder;
26+
import io.grpc.ServerBuilder;
2727
import io.grpc.internal.GrpcUtil;
2828
import io.grpc.internal.testing.StreamRecorder;
2929
import io.grpc.internal.testing.TestUtils;
@@ -52,7 +52,7 @@
5252
* Integration tests for GRPC over Http2 using the OkHttp framework.
5353
*/
5454
@RunWith(JUnit4.class)
55-
public class Http2OkHttpTest extends AbstractInteropTest {
55+
public class Http2OkHttpTest extends AbstractNettyInteropTest {
5656

5757
private static final String BAD_HOSTNAME = "I.am.a.bad.hostname";
5858

@@ -64,7 +64,7 @@ public static void loadConscrypt() throws Exception {
6464
}
6565

6666
@Override
67-
protected AbstractServerImplBuilder<?> getServerBuilder() {
67+
protected ServerBuilder<?> getServerBuilder() {
6868
// Starts the server with HTTPS.
6969
try {
7070
SslProvider sslProvider = SslContext.defaultServerProvider();
@@ -77,10 +77,11 @@ protected AbstractServerImplBuilder<?> getServerBuilder() {
7777
.forServer(TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key"));
7878
GrpcSslContexts.configure(contextBuilder, sslProvider);
7979
contextBuilder.ciphers(TestUtils.preferredTestCiphers(), SupportedCipherSuiteFilter.INSTANCE);
80-
return NettyServerBuilder.forPort(0)
81-
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW)
82-
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
83-
.sslContext(contextBuilder.build());
80+
return withCustomCensusModule(
81+
NettyServerBuilder.forPort(0)
82+
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW)
83+
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
84+
.sslContext(contextBuilder.build()));
8485
} catch (IOException ex) {
8586
throw new RuntimeException(ex);
8687
}

0 commit comments

Comments
 (0)