Skip to content

Commit 66ab956

Browse files
committed
Reapply "Eliminate MethodDescriptor from startCall and interceptCall for servers"
This reverts commit ef17830, which itself was a revert.
1 parent 2eaa540 commit 66ab956

File tree

47 files changed

+669
-428
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+669
-428
lines changed

benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/BenchmarkServiceGrpc.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,15 @@ public io.grpc.stub.StreamObserver<Req> invoke(
263263
}
264264
}
265265

266+
public static io.grpc.ServiceDescriptor getServiceDescriptor() {
267+
return new io.grpc.ServiceDescriptor(SERVICE_NAME,
268+
METHOD_UNARY_CALL,
269+
METHOD_STREAMING_CALL);
270+
}
271+
266272
public static io.grpc.ServerServiceDefinition bindService(
267273
final BenchmarkService serviceImpl) {
268-
return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME)
274+
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
269275
.addMethod(
270276
METHOD_UNARY_CALL,
271277
asyncUnaryCall(

benchmarks/src/generated/main/grpc/io/grpc/benchmarks/proto/WorkerServiceGrpc.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,17 @@ public io.grpc.stub.StreamObserver<Req> invoke(
366366
}
367367
}
368368

369+
public static io.grpc.ServiceDescriptor getServiceDescriptor() {
370+
return new io.grpc.ServiceDescriptor(SERVICE_NAME,
371+
METHOD_RUN_SERVER,
372+
METHOD_RUN_CLIENT,
373+
METHOD_CORE_COUNT,
374+
METHOD_QUIT_WORKER);
375+
}
376+
369377
public static io.grpc.ServerServiceDefinition bindService(
370378
final WorkerService serviceImpl) {
371-
return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME)
379+
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
372380
.addMethod(
373381
METHOD_RUN_SERVER,
374382
asyncBidiStreamingCall(

benchmarks/src/jmh/java/io/grpc/benchmarks/netty/AbstractBenchmark.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import io.grpc.ServerCall;
4242
import io.grpc.ServerCallHandler;
4343
import io.grpc.ServerServiceDefinition;
44+
import io.grpc.ServiceDescriptor;
4445
import io.grpc.Status;
4546
import io.grpc.benchmarks.ByteBufOutputMarshaller;
4647
import io.grpc.netty.NegotiationType;
@@ -258,13 +259,15 @@ public void setup(ExecutorType clientExecutor,
258259

259260
// Server implementation of unary & streaming methods
260261
serverBuilder.addService(
261-
ServerServiceDefinition.builder("benchmark")
262-
.addMethod(unaryMethod,
263-
new ServerCallHandler<ByteBuf, ByteBuf>() {
262+
ServerServiceDefinition.builder(
263+
new ServiceDescriptor("benchmark",
264+
unaryMethod,
265+
pingPongMethod,
266+
flowControlledStreaming))
267+
.addMethod(unaryMethod, new ServerCallHandler<ByteBuf, ByteBuf>() {
264268
@Override
265269
public ServerCall.Listener<ByteBuf> startCall(
266-
MethodDescriptor<ByteBuf, ByteBuf> method,
267-
final ServerCall<ByteBuf> call,
270+
final ServerCall<ByteBuf, ByteBuf> call,
268271
Metadata headers) {
269272
call.sendHeaders(new Metadata());
270273
call.request(1);
@@ -292,12 +295,10 @@ public void onComplete() {
292295
};
293296
}
294297
})
295-
.addMethod(pingPongMethod,
296-
new ServerCallHandler<ByteBuf, ByteBuf>() {
298+
.addMethod(pingPongMethod, new ServerCallHandler<ByteBuf, ByteBuf>() {
297299
@Override
298300
public ServerCall.Listener<ByteBuf> startCall(
299-
MethodDescriptor<ByteBuf, ByteBuf> method,
300-
final ServerCall<ByteBuf> call,
301+
final ServerCall<ByteBuf, ByteBuf> call,
301302
Metadata headers) {
302303
call.sendHeaders(new Metadata());
303304
call.request(1);
@@ -327,12 +328,10 @@ public void onComplete() {
327328
};
328329
}
329330
})
330-
.addMethod(flowControlledStreaming,
331-
new ServerCallHandler<ByteBuf, ByteBuf>() {
331+
.addMethod(flowControlledStreaming, new ServerCallHandler<ByteBuf, ByteBuf>() {
332332
@Override
333333
public ServerCall.Listener<ByteBuf> startCall(
334-
MethodDescriptor<ByteBuf, ByteBuf> method,
335-
final ServerCall<ByteBuf> call,
334+
final ServerCall<ByteBuf, ByteBuf> call,
336335
Metadata headers) {
337336
call.sendHeaders(new Metadata());
338337
call.request(1);

benchmarks/src/jmh/java/io/grpc/benchmarks/netty/HandlerRegistryBenchmark.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@
3131

3232
package io.grpc.benchmarks.netty;
3333

34+
import io.grpc.Metadata;
3435
import io.grpc.MethodDescriptor;
35-
import io.grpc.ServerMethodDefinition;
36+
import io.grpc.ServerCall;
37+
import io.grpc.ServerCall.Listener;
38+
import io.grpc.ServerCallHandler;
3639
import io.grpc.ServerServiceDefinition;
40+
import io.grpc.ServiceDescriptor;
3741
import io.grpc.util.MutableHandlerRegistry;
3842

3943
import org.openjdk.jmh.annotations.Benchmark;
@@ -80,13 +84,21 @@ public void setup() throws Exception {
8084
fullMethodNames = new ArrayList<String>(serviceCount * methodCountPerService);
8185
for (int serviceIndex = 0; serviceIndex < serviceCount; ++serviceIndex) {
8286
String serviceName = randomString();
83-
ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition.builder(serviceName);
87+
ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition.builder(
88+
new ServiceDescriptor(serviceName));
8489
for (int methodIndex = 0; methodIndex < methodCountPerService; ++methodIndex) {
8590
String methodName = randomString();
86-
MethodDescriptor<?, ?> methodDescriptor = MethodDescriptor.create(
91+
MethodDescriptor<Object, Object> methodDescriptor = MethodDescriptor.create(
8792
MethodDescriptor.MethodType.UNKNOWN,
8893
MethodDescriptor.generateFullMethodName(serviceName, methodName), null, null);
89-
serviceBuilder.addMethod(ServerMethodDefinition.create(methodDescriptor, null));
94+
serviceBuilder.addMethod(methodDescriptor,
95+
new ServerCallHandler<Object, Object>() {
96+
@Override
97+
public Listener<Object> startCall(ServerCall<Object, Object> call,
98+
Metadata headers) {
99+
return null;
100+
}
101+
});
90102
fullMethodNames.add(methodDescriptor.getFullMethodName());
91103
}
92104
registry.addService(serviceBuilder.build());

benchmarks/src/main/java/io/grpc/benchmarks/driver/LoadServer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import io.grpc.ServerCall;
4141
import io.grpc.ServerCallHandler;
4242
import io.grpc.ServerServiceDefinition;
43+
import io.grpc.ServiceDescriptor;
4344
import io.grpc.Status;
4445
import io.grpc.benchmarks.ByteBufOutputMarshaller;
4546
import io.grpc.benchmarks.Utils;
@@ -141,7 +142,8 @@ final class LoadServer {
141142
if (config.getServerType() == Control.ServerType.ASYNC_GENERIC_SERVER) {
142143
serverBuilder.addService(
143144
ServerServiceDefinition
144-
.builder(BenchmarkServiceGrpc.SERVICE_NAME)
145+
.builder(new ServiceDescriptor(BenchmarkServiceGrpc.SERVICE_NAME,
146+
GENERIC_STREAMING_PING_PONG_METHOD))
145147
.addMethod(GENERIC_STREAMING_PING_PONG_METHOD, new GenericServiceCallHandler())
146148
.build());
147149
} else {
@@ -230,9 +232,10 @@ public void onCompleted() {
230232
}
231233

232234
private class GenericServiceCallHandler implements ServerCallHandler<ByteBuf, ByteBuf> {
235+
233236
@Override
234-
public ServerCall.Listener<ByteBuf> startCall(MethodDescriptor<ByteBuf, ByteBuf> method,
235-
final ServerCall<ByteBuf> call, Metadata headers) {
237+
public ServerCall.Listener<ByteBuf> startCall(
238+
final ServerCall<ByteBuf, ByteBuf> call, Metadata headers) {
236239
call.sendHeaders(new Metadata());
237240
call.request(1);
238241
return new ServerCall.Listener<ByteBuf>() {

compiler/src/java_plugin/cpp/java_generator.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,31 @@ static void PrintMethodHandlerClass(const ServiceDescriptor* service,
851851
p->Print("}\n\n");
852852
}
853853

854+
static void PrintGetServiceDescriptorMethod(const ServiceDescriptor* service,
855+
map<string, string>* vars,
856+
Printer* p,
857+
bool generate_nano) {
858+
(*vars)["service_name"] = service->name();
859+
p->Print(
860+
*vars,
861+
"public static $ServiceDescriptor$ getServiceDescriptor() {\n");
862+
p->Indent();
863+
p->Print(*vars,
864+
"return new $ServiceDescriptor$(SERVICE_NAME");
865+
p->Indent();
866+
p->Indent();
867+
for (int i = 0; i < service->method_count(); ++i) {
868+
const MethodDescriptor* method = service->method(i);
869+
(*vars)["method_field_name"] = MethodPropertiesFieldName(method);
870+
p->Print(*vars, ",\n$method_field_name$");
871+
}
872+
p->Print(");\n");
873+
p->Outdent();
874+
p->Outdent();
875+
p->Outdent();
876+
p->Print("}\n\n");
877+
}
878+
854879
static void PrintBindServiceMethod(const ServiceDescriptor* service,
855880
map<string, string>* vars,
856881
Printer* p,
@@ -863,7 +888,7 @@ static void PrintBindServiceMethod(const ServiceDescriptor* service,
863888
p->Indent();
864889
p->Print(*vars,
865890
"return "
866-
"$ServerServiceDefinition$.builder(SERVICE_NAME)\n");
891+
"$ServerServiceDefinition$.builder(getServiceDescriptor())\n");
867892
p->Indent();
868893
p->Indent();
869894
for (int i = 0; i < service->method_count(); ++i) {
@@ -994,6 +1019,7 @@ static void PrintService(const ServiceDescriptor* service,
9941019
PrintStub(service, vars, p, BLOCKING_CLIENT_IMPL, generate_nano);
9951020
PrintStub(service, vars, p, FUTURE_CLIENT_IMPL, generate_nano);
9961021
PrintMethodHandlerClass(service, vars, p, generate_nano);
1022+
PrintGetServiceDescriptorMethod(service, vars, p, generate_nano);
9971023
PrintBindServiceMethod(service, vars, p, generate_nano);
9981024
p->Outdent();
9991025
p->Print("}\n");
@@ -1050,6 +1076,8 @@ void GenerateService(const ServiceDescriptor* service,
10501076
vars["BindableService"] = "io.grpc.BindableService";
10511077
vars["ServerServiceDefinition"] =
10521078
"io.grpc.ServerServiceDefinition";
1079+
vars["ServiceDescriptor"] =
1080+
"io.grpc.ServiceDescriptor";
10531081
vars["AbstractStub"] = "io.grpc.stub.AbstractStub";
10541082
vars["ImmutableList"] = "com.google.common.collect.ImmutableList";
10551083
vars["Collection"] = "java.util.Collection";

compiler/src/test/golden/TestService.java.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,18 @@ public class TestServiceGrpc {
400400
}
401401
}
402402

403+
public static io.grpc.ServiceDescriptor getServiceDescriptor() {
404+
return new io.grpc.ServiceDescriptor(SERVICE_NAME,
405+
METHOD_UNARY_CALL,
406+
METHOD_STREAMING_OUTPUT_CALL,
407+
METHOD_STREAMING_INPUT_CALL,
408+
METHOD_FULL_BIDI_CALL,
409+
METHOD_HALF_BIDI_CALL);
410+
}
411+
403412
public static io.grpc.ServerServiceDefinition bindService(
404413
final TestService serviceImpl) {
405-
return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME)
414+
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
406415
.addMethod(
407416
METHOD_UNARY_CALL,
408417
asyncUnaryCall(

compiler/src/testLite/golden/TestService.java.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,18 @@ public class TestServiceGrpc {
400400
}
401401
}
402402

403+
public static io.grpc.ServiceDescriptor getServiceDescriptor() {
404+
return new io.grpc.ServiceDescriptor(SERVICE_NAME,
405+
METHOD_UNARY_CALL,
406+
METHOD_STREAMING_OUTPUT_CALL,
407+
METHOD_STREAMING_INPUT_CALL,
408+
METHOD_FULL_BIDI_CALL,
409+
METHOD_HALF_BIDI_CALL);
410+
}
411+
403412
public static io.grpc.ServerServiceDefinition bindService(
404413
final TestService serviceImpl) {
405-
return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME)
414+
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
406415
.addMethod(
407416
METHOD_UNARY_CALL,
408417
asyncUnaryCall(

compiler/src/testNano/golden/TestService.java.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,18 @@ public class TestServiceGrpc {
478478
}
479479
}
480480

481+
public static io.grpc.ServiceDescriptor getServiceDescriptor() {
482+
return new io.grpc.ServiceDescriptor(SERVICE_NAME,
483+
METHOD_UNARY_CALL,
484+
METHOD_STREAMING_OUTPUT_CALL,
485+
METHOD_STREAMING_INPUT_CALL,
486+
METHOD_FULL_BIDI_CALL,
487+
METHOD_HALF_BIDI_CALL);
488+
}
489+
481490
public static io.grpc.ServerServiceDefinition bindService(
482491
final TestService serviceImpl) {
483-
return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME)
492+
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
484493
.addMethod(
485494
METHOD_UNARY_CALL,
486495
asyncUnaryCall(

core/src/main/java/io/grpc/Contexts.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,20 @@ private Contexts() {
5353
* the client.
5454
*
5555
* @param context to make {@link Context#current()}.
56-
* @param method being requested by the client.
5756
* @param call used to send responses to client.
5857
* @param headers received from client.
5958
* @param next handler used to create the listener to be wrapped.
6059
* @return listener that will receive events in the scope of the provided context.
6160
*/
6261
public static <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
6362
Context context,
64-
MethodDescriptor<ReqT, RespT> method,
65-
ServerCall<RespT> call,
63+
ServerCall<ReqT, RespT> call,
6664
Metadata headers,
6765
ServerCallHandler<ReqT, RespT> next) {
6866
Context previous = context.attach();
6967
try {
7068
return new ContextualizedServerCallListener<ReqT>(
71-
next.startCall(method, call, headers),
69+
next.startCall(call, headers),
7270
context);
7371
} finally {
7472
context.detach(previous);

0 commit comments

Comments
 (0)