-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
ServerImplBuilder.java
349 lines (313 loc) · 12.8 KB
/
ServerImplBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright 2020 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc.internal;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.errorprone.annotations.DoNotCall;
import io.grpc.BinaryLog;
import io.grpc.BindableService;
import io.grpc.CompressorRegistry;
import io.grpc.Context;
import io.grpc.Deadline;
import io.grpc.DecompressorRegistry;
import io.grpc.HandlerRegistry;
import io.grpc.InternalChannelz;
import io.grpc.InternalGlobalInterceptors;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerCallExecutorSupplier;
import io.grpc.ServerInterceptor;
import io.grpc.ServerMethodDefinition;
import io.grpc.ServerServiceDefinition;
import io.grpc.ServerStreamTracer;
import io.grpc.ServerTransportFilter;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
/**
* Default builder for {@link io.grpc.Server} instances, for usage in Transport implementations.
*/
public final class ServerImplBuilder extends ServerBuilder<ServerImplBuilder> {
private static final Logger log = Logger.getLogger(ServerImplBuilder.class.getName());
@DoNotCall("ClientTransportServersBuilder is required, use a constructor")
public static ServerBuilder<?> forPort(int port) {
throw new UnsupportedOperationException(
"ClientTransportServersBuilder is required, use a constructor");
}
// defaults
private static final ObjectPool<? extends Executor> DEFAULT_EXECUTOR_POOL =
SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR);
private static final HandlerRegistry DEFAULT_FALLBACK_REGISTRY = new DefaultFallbackRegistry();
private static final DecompressorRegistry DEFAULT_DECOMPRESSOR_REGISTRY =
DecompressorRegistry.getDefaultInstance();
private static final CompressorRegistry DEFAULT_COMPRESSOR_REGISTRY =
CompressorRegistry.getDefaultInstance();
private static final long DEFAULT_HANDSHAKE_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(120);
// mutable state
final InternalHandlerRegistry.Builder registryBuilder =
new InternalHandlerRegistry.Builder();
final List<ServerTransportFilter> transportFilters = new ArrayList<>();
final List<ServerInterceptor> interceptors = new ArrayList<>();
private final List<ServerStreamTracer.Factory> streamTracerFactories = new ArrayList<>();
private final ClientTransportServersBuilder clientTransportServersBuilder;
HandlerRegistry fallbackRegistry = DEFAULT_FALLBACK_REGISTRY;
ObjectPool<? extends Executor> executorPool = DEFAULT_EXECUTOR_POOL;
DecompressorRegistry decompressorRegistry = DEFAULT_DECOMPRESSOR_REGISTRY;
CompressorRegistry compressorRegistry = DEFAULT_COMPRESSOR_REGISTRY;
long handshakeTimeoutMillis = DEFAULT_HANDSHAKE_TIMEOUT_MILLIS;
Deadline.Ticker ticker = Deadline.getSystemTicker();
private boolean statsEnabled = true;
private boolean recordStartedRpcs = true;
private boolean recordFinishedRpcs = true;
private boolean recordRealTimeMetrics = false;
private boolean tracingEnabled = true;
@Nullable BinaryLog binlog;
InternalChannelz channelz = InternalChannelz.instance();
CallTracer.Factory callTracerFactory = CallTracer.getDefaultFactory();
@Nullable
ServerCallExecutorSupplier executorSupplier;
/**
* An interface to provide to provide transport specific information for the server. This method
* is meant for Transport implementors and should not be used by normal users.
*/
public interface ClientTransportServersBuilder {
InternalServer buildClientTransportServers(
List<? extends ServerStreamTracer.Factory> streamTracerFactories);
}
/**
* Creates a new server builder with given transport servers provider.
*/
public ServerImplBuilder(ClientTransportServersBuilder clientTransportServersBuilder) {
this.clientTransportServersBuilder = checkNotNull(clientTransportServersBuilder,
"clientTransportServersBuilder");
}
@Override
public ServerImplBuilder directExecutor() {
return executor(MoreExecutors.directExecutor());
}
@Override
public ServerImplBuilder executor(@Nullable Executor executor) {
this.executorPool = executor != null ? new FixedObjectPool<>(executor) : DEFAULT_EXECUTOR_POOL;
return this;
}
@Override
public ServerImplBuilder callExecutor(ServerCallExecutorSupplier executorSupplier) {
this.executorSupplier = checkNotNull(executorSupplier);
return this;
}
@Override
public ServerImplBuilder addService(ServerServiceDefinition service) {
registryBuilder.addService(checkNotNull(service, "service"));
return this;
}
@Override
public ServerImplBuilder addService(BindableService bindableService) {
return addService(checkNotNull(bindableService, "bindableService").bindService());
}
@Override
public ServerImplBuilder addTransportFilter(ServerTransportFilter filter) {
transportFilters.add(checkNotNull(filter, "filter"));
return this;
}
@Override
public ServerImplBuilder intercept(ServerInterceptor interceptor) {
interceptors.add(checkNotNull(interceptor, "interceptor"));
return this;
}
@Override
public ServerImplBuilder addStreamTracerFactory(ServerStreamTracer.Factory factory) {
streamTracerFactories.add(checkNotNull(factory, "factory"));
return this;
}
@Override
public ServerImplBuilder fallbackHandlerRegistry(@Nullable HandlerRegistry registry) {
this.fallbackRegistry = registry != null ? registry : DEFAULT_FALLBACK_REGISTRY;
return this;
}
@Override
public ServerImplBuilder decompressorRegistry(@Nullable DecompressorRegistry registry) {
this.decompressorRegistry = registry != null ? registry : DEFAULT_DECOMPRESSOR_REGISTRY;
return this;
}
@Override
public ServerImplBuilder compressorRegistry(@Nullable CompressorRegistry registry) {
this.compressorRegistry = registry != null ? registry : DEFAULT_COMPRESSOR_REGISTRY;
return this;
}
@Override
public ServerImplBuilder handshakeTimeout(long timeout, TimeUnit unit) {
checkArgument(timeout > 0, "handshake timeout is %s, but must be positive", timeout);
this.handshakeTimeoutMillis = checkNotNull(unit, "unit").toMillis(timeout);
return this;
}
@Override
public ServerImplBuilder setBinaryLog(@Nullable BinaryLog binaryLog) {
this.binlog = binaryLog;
return this;
}
/**
* Disable or enable stats features. Enabled by default.
*/
public void setStatsEnabled(boolean value) {
this.statsEnabled = value;
}
/**
* Disable or enable stats recording for RPC upstarts. Effective only if {@link
* #setStatsEnabled} is set to true. Enabled by default.
*/
public void setStatsRecordStartedRpcs(boolean value) {
recordStartedRpcs = value;
}
/**
* Disable or enable stats recording for RPC completions. Effective only if {@link
* #setStatsEnabled} is set to true. Enabled by default.
*/
public void setStatsRecordFinishedRpcs(boolean value) {
recordFinishedRpcs = value;
}
/**
* Disable or enable real-time metrics recording. Effective only if {@link #setStatsEnabled} is
* set to true. Disabled by default.
*/
public void setStatsRecordRealTimeMetrics(boolean value) {
recordRealTimeMetrics = value;
}
/**
* Disable or enable tracing features. Enabled by default.
*/
public void setTracingEnabled(boolean value) {
tracingEnabled = value;
}
/**
* Sets a custom deadline ticker. This should only be called from InProcessServerBuilder.
*/
public void setDeadlineTicker(Deadline.Ticker ticker) {
this.ticker = checkNotNull(ticker, "ticker");
}
@Override
public Server build() {
return new ServerImpl(this,
clientTransportServersBuilder.buildClientTransportServers(getTracerFactories()),
Context.ROOT);
}
@VisibleForTesting
List<? extends ServerStreamTracer.Factory> getTracerFactories() {
ArrayList<ServerStreamTracer.Factory> tracerFactories = new ArrayList<>();
boolean isGlobalInterceptorsTracersSet = false;
List<ServerInterceptor> globalServerInterceptors
= InternalGlobalInterceptors.getServerInterceptors();
List<ServerStreamTracer.Factory> globalServerStreamTracerFactories
= InternalGlobalInterceptors.getServerStreamTracerFactories();
if (globalServerInterceptors != null) {
tracerFactories.addAll(globalServerStreamTracerFactories);
interceptors.addAll(globalServerInterceptors);
isGlobalInterceptorsTracersSet = true;
}
if (!isGlobalInterceptorsTracersSet && statsEnabled) {
ServerStreamTracer.Factory censusStatsTracerFactory = null;
try {
Class<?> censusStatsAccessor =
Class.forName("io.grpc.census.InternalCensusStatsAccessor");
Method getServerStreamTracerFactoryMethod =
censusStatsAccessor.getDeclaredMethod(
"getServerStreamTracerFactory",
boolean.class,
boolean.class,
boolean.class);
censusStatsTracerFactory =
(ServerStreamTracer.Factory) getServerStreamTracerFactoryMethod
.invoke(
null,
recordStartedRpcs,
recordFinishedRpcs,
recordRealTimeMetrics);
} catch (ClassNotFoundException e) {
// Replace these separate catch statements with multicatch when Android min-API >= 19
log.log(Level.FINE, "Unable to apply census stats", e);
} catch (NoSuchMethodException e) {
log.log(Level.FINE, "Unable to apply census stats", e);
} catch (IllegalAccessException e) {
log.log(Level.FINE, "Unable to apply census stats", e);
} catch (InvocationTargetException e) {
log.log(Level.FINE, "Unable to apply census stats", e);
}
if (censusStatsTracerFactory != null) {
tracerFactories.add(censusStatsTracerFactory);
}
}
if (!isGlobalInterceptorsTracersSet && tracingEnabled) {
ServerStreamTracer.Factory tracingStreamTracerFactory = null;
try {
Class<?> censusTracingAccessor =
Class.forName("io.grpc.census.InternalCensusTracingAccessor");
Method getServerStreamTracerFactoryMethod =
censusTracingAccessor.getDeclaredMethod("getServerStreamTracerFactory");
tracingStreamTracerFactory =
(ServerStreamTracer.Factory) getServerStreamTracerFactoryMethod.invoke(null);
} catch (ClassNotFoundException e) {
// Replace these separate catch statements with multicatch when Android min-API >= 19
log.log(Level.FINE, "Unable to apply census stats", e);
} catch (NoSuchMethodException e) {
log.log(Level.FINE, "Unable to apply census stats", e);
} catch (IllegalAccessException e) {
log.log(Level.FINE, "Unable to apply census stats", e);
} catch (InvocationTargetException e) {
log.log(Level.FINE, "Unable to apply census stats", e);
}
if (tracingStreamTracerFactory != null) {
tracerFactories.add(tracingStreamTracerFactory);
}
}
tracerFactories.addAll(streamTracerFactories);
tracerFactories.trimToSize();
return Collections.unmodifiableList(tracerFactories);
}
public InternalChannelz getChannelz() {
return channelz;
}
private static final class DefaultFallbackRegistry extends HandlerRegistry {
@Override
public List<ServerServiceDefinition> getServices() {
return Collections.emptyList();
}
@Nullable
@Override
public ServerMethodDefinition<?, ?> lookupMethod(
String methodName, @Nullable String authority) {
return null;
}
}
/**
* Returns the internal ExecutorPool for offloading tasks.
*/
public ObjectPool<? extends Executor> getExecutorPool() {
return this.executorPool;
}
@Override
public ServerImplBuilder useTransportSecurity(File certChain, File privateKey) {
throw new UnsupportedOperationException("TLS not supported in ServerImplBuilder");
}
}