Skip to content

Commit ca5f3a0

Browse files
committed
core, netty: server builders extend a public API class
1 parent de83f81 commit ca5f3a0

File tree

12 files changed

+196
-77
lines changed

12 files changed

+196
-77
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

+16-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,20 @@ protected final ClientInterceptor createCensusStatsClientInterceptor() {
373357
true, true, true, false /* real-time metrics */);
374358
}
375359

360+
protected final ServerStreamTracer.Factory createCustomCensusTracerFactory() {
361+
return InternalCensusStatsAccessor.getServerStreamTracerFactory(
362+
tagger, tagContextBinarySerializer, serverStatsRecorder,
363+
GrpcUtil.STOPWATCH_SUPPLIER,
364+
true, true, true, false /* real-time metrics */);
365+
}
366+
367+
/**
368+
* Override this when custom census module presence is different from {@link #metricsExpected()}.
369+
*/
370+
protected boolean customCensusModulePresent() {
371+
return metricsExpected();
372+
}
373+
376374
/**
377375
* Return true if exact metric values should be checked.
378376
*/
@@ -1510,7 +1508,7 @@ public void customMetadata() throws Exception {
15101508
@Test(timeout = 10000)
15111509
public void censusContextsPropagated() {
15121510
Assume.assumeTrue("Skip the test because server is not in the same process.", server != null);
1513-
Assume.assumeTrue(customCensusModulePresent);
1511+
Assume.assumeTrue(customCensusModulePresent());
15141512
Span clientParentSpan = Tracing.getTracer().spanBuilder("Test.interopTest").startSpan();
15151513
// A valid ID is guaranteed to be unique, so we can verify it is actually propagated.
15161514
assertTrue(clientParentSpan.getContext().getTraceId().isValid());

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
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;
21+
import io.grpc.netty.InternalNettyServerBuilder;
2122
import io.grpc.netty.NegotiationType;
2223
import io.grpc.netty.NettyChannelBuilder;
2324
import io.grpc.netty.NettyServerBuilder;
@@ -28,9 +29,12 @@
2829
public class AutoWindowSizingOnTest extends AbstractInteropTest {
2930

3031
@Override
31-
protected AbstractServerImplBuilder<?> getServerBuilder() {
32-
return NettyServerBuilder.forPort(0)
32+
protected ServerBuilder<?> getServerBuilder() {
33+
NettyServerBuilder builder = NettyServerBuilder.forPort(0)
3334
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
35+
// Disable the default census stats tracer, use testing tracer instead.
36+
InternalNettyServerBuilder.setStatsEnabled(builder, false);
37+
return builder.addStreamTracerFactory(createCustomCensusTracerFactory());
3438
}
3539

3640
@Override

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
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;
21+
import io.grpc.netty.InternalNettyServerBuilder;
2122
import io.grpc.netty.NegotiationType;
2223
import io.grpc.netty.NettyChannelBuilder;
2324
import io.grpc.netty.NettyServerBuilder;
@@ -38,14 +39,17 @@ public class Http2NettyLocalChannelTest extends AbstractInteropTest {
3839
private DefaultEventLoopGroup eventLoopGroup = new DefaultEventLoopGroup();
3940

4041
@Override
41-
protected AbstractServerImplBuilder<?> getServerBuilder() {
42-
return NettyServerBuilder
42+
protected ServerBuilder<?> getServerBuilder() {
43+
NettyServerBuilder builder = NettyServerBuilder
4344
.forAddress(new LocalAddress("in-process-1"))
4445
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW)
4546
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
4647
.channelType(LocalServerChannel.class)
4748
.workerEventLoopGroup(eventLoopGroup)
4849
.bossEventLoopGroup(eventLoopGroup);
50+
// Disable the default census stats tracer, use testing tracer instead.
51+
InternalNettyServerBuilder.setStatsEnabled(builder, false);
52+
return builder.addStreamTracerFactory(createCustomCensusTracerFactory());
4953
}
5054

5155
@Override

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
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;
26+
import io.grpc.netty.InternalNettyServerBuilder;
2627
import io.grpc.netty.NettyChannelBuilder;
2728
import io.grpc.netty.NettyServerBuilder;
2829
import io.netty.handler.ssl.ClientAuth;
@@ -41,10 +42,10 @@
4142
public class Http2NettyTest extends AbstractInteropTest {
4243

4344
@Override
44-
protected AbstractServerImplBuilder<?> getServerBuilder() {
45+
protected ServerBuilder<?> getServerBuilder() {
4546
// Starts the server with HTTPS.
4647
try {
47-
return NettyServerBuilder.forPort(0)
48+
NettyServerBuilder builder = NettyServerBuilder.forPort(0)
4849
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW)
4950
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
5051
.sslContext(GrpcSslContexts
@@ -53,6 +54,9 @@ protected AbstractServerImplBuilder<?> getServerBuilder() {
5354
.trustManager(TestUtils.loadCert("ca.pem"))
5455
.ciphers(TestUtils.preferredTestCiphers(), SupportedCipherSuiteFilter.INSTANCE)
5556
.build());
57+
// Disable the default census stats tracer, use testing tracer instead.
58+
InternalNettyServerBuilder.setStatsEnabled(builder, false);
59+
return builder.addStreamTracerFactory(createCustomCensusTracerFactory());
5660
} catch (IOException ex) {
5761
throw new RuntimeException(ex);
5862
}

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
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;
3030
import io.grpc.netty.GrpcSslContexts;
31+
import io.grpc.netty.InternalNettyServerBuilder;
3132
import io.grpc.netty.NettyServerBuilder;
3233
import io.grpc.okhttp.InternalOkHttpChannelBuilder;
3334
import io.grpc.okhttp.OkHttpChannelBuilder;
@@ -64,7 +65,7 @@ public static void loadConscrypt() throws Exception {
6465
}
6566

6667
@Override
67-
protected AbstractServerImplBuilder<?> getServerBuilder() {
68+
protected ServerBuilder<?> getServerBuilder() {
6869
// Starts the server with HTTPS.
6970
try {
7071
SslProvider sslProvider = SslContext.defaultServerProvider();
@@ -77,10 +78,13 @@ protected AbstractServerImplBuilder<?> getServerBuilder() {
7778
.forServer(TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key"));
7879
GrpcSslContexts.configure(contextBuilder, sslProvider);
7980
contextBuilder.ciphers(TestUtils.preferredTestCiphers(), SupportedCipherSuiteFilter.INSTANCE);
80-
return NettyServerBuilder.forPort(0)
81+
NettyServerBuilder builder = NettyServerBuilder.forPort(0)
8182
.flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW)
8283
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
8384
.sslContext(contextBuilder.build());
85+
// Disable the default census stats tracer, use testing tracer instead.
86+
InternalNettyServerBuilder.setStatsEnabled(builder, false);
87+
return builder.addStreamTracerFactory(createCustomCensusTracerFactory());
8488
} catch (IOException ex) {
8589
throw new RuntimeException(ex);
8690
}

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

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

1717
package io.grpc.testing.integration;
1818

19+
import io.grpc.ServerBuilder;
1920
import io.grpc.inprocess.InProcessChannelBuilder;
2021
import io.grpc.inprocess.InProcessServerBuilder;
2122
import io.grpc.inprocess.InternalInProcessChannelBuilder;
22-
import io.grpc.internal.AbstractServerImplBuilder;
23+
import io.grpc.inprocess.InternalInProcessServerBuilder;
2324
import org.junit.runner.RunWith;
2425
import org.junit.runners.JUnit4;
2526

@@ -30,9 +31,12 @@ public class InProcessTest extends AbstractInteropTest {
3031
private static final String SERVER_NAME = "test";
3132

3233
@Override
33-
protected AbstractServerImplBuilder<?> getServerBuilder() {
34+
protected ServerBuilder<?> getServerBuilder() {
3435
// Starts the in-process server.
35-
return InProcessServerBuilder.forName(SERVER_NAME);
36+
InProcessServerBuilder builder = InProcessServerBuilder.forName(SERVER_NAME);
37+
// Disable the default census stats tracer, use testing tracer instead.
38+
InternalInProcessServerBuilder.setStatsEnabled(builder, false);
39+
return builder.addStreamTracerFactory(createCustomCensusTracerFactory());
3640
}
3741

3842
@Override
@@ -43,6 +47,12 @@ protected InProcessChannelBuilder createChannelBuilder() {
4347
return builder.intercept(createCensusStatsClientInterceptor());
4448
}
4549

50+
@Override
51+
protected boolean customCensusModulePresent() {
52+
// Metrics values are not expected, but custom census module is still used.
53+
return true;
54+
}
55+
4656
@Override
4757
protected boolean metricsExpected() {
4858
// TODO(zhangkun83): InProcessTransport by-passes framer and deframer, thus message sizes are

0 commit comments

Comments
 (0)