Skip to content

Commit

Permalink
feat(transcoding): removed unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
zZHorizonZz committed Apr 21, 2024
1 parent 3e9ce41 commit 2af788d
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,6 @@ ServiceStartBuildItem initializeServer(GrpcServerRecorder recorder,
// Build the list of blocking methods per service implementation
Map<String, List<String>> blocking = new HashMap<>();
for (BindableServiceBuildItem bindable : bindables) {
log.infof("Bindable service: %s", bindable.serviceClass);
if (bindable.hasBlockingMethods()) {
blocking.put(bindable.serviceClass.toString(), bindable.blockingMethods);
}
Expand All @@ -701,10 +700,6 @@ ServiceStartBuildItem initializeServer(GrpcServerRecorder recorder,
}
}

log.infov("Blocking methods: {0}", blocking.size());
log.infov("Virtual methods: {0}", virtuals.size());
log.infov("Global interceptors: {0}", bindables.size());

if (!bindables.isEmpty()
|| (LaunchMode.current() == LaunchMode.DEVELOPMENT && buildTimeConfig.devMode.forceServerStart)) {
//Uses mainrouter when the 'quarkus.http.root-path' is not '/'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

import org.jboss.logging.Logger;

import com.google.protobuf.Message;

import io.grpc.BindableService;
import io.grpc.MethodDescriptor;
import io.grpc.ServerMethodDefinition;
import io.grpc.ServerServiceDefinition;
import io.quarkus.arc.Arc;
import io.quarkus.grpc.GrpcTranscodingMarshaller;
import io.quarkus.grpc.MutinyTranscodingService;
import io.quarkus.grpc.auth.GrpcSecurityInterceptor;
import io.quarkus.grpc.runtime.transcoding.*;
Expand Down Expand Up @@ -60,24 +63,18 @@ public RuntimeValue<GrpcTranscodingServer> initializeMarshallingServer(RuntimeVa
transcodingService.getGrpcServiceName());

for (ServerMethodDefinition<?, ?> serviceDefinition : grpcService.definition.getMethods()) {
MethodDescriptor<?, ?> methodDescriptor = serviceDefinition.getMethodDescriptor();
MethodDescriptor<Message, Message> methodDescriptor = (MethodDescriptor<Message, Message>) serviceDefinition
.getMethodDescriptor();
GrpcTranscodingMethod transcodingMethod = findTranscodingMethod(transcodingMethods, methodDescriptor);

String path = transcodingMethod.getUriTemplate();
HttpMethod httpMethod = HttpMethod.valueOf(transcodingMethod.getHttpMethodName());

LOGGER.info("Registering transcoding route for " + grpcService.getImplementationClassName() + " method "
+ methodDescriptor.getFullMethodName() + " => " + httpMethod + " " + path);

String fullMethodName = methodDescriptor.getFullMethodName()
.substring(methodDescriptor.getFullMethodName().lastIndexOf("/") + 1);
fullMethodName = Character.toLowerCase(fullMethodName.charAt(0)) + fullMethodName.substring(1);
GrpcTranscodingMetadata<?, ?> metadata = createMetadata(transcodingMethod, methodDescriptor,
transcodingService);

transcodingServer.addMethodMapping(path, methodDescriptor.getFullMethodName());
transcodingServer.addRequestMarshaller(methodDescriptor.getFullMethodName(),
transcodingService.findRequestMarshaller(fullMethodName));
transcodingServer.addResponseMarshaller(methodDescriptor.getFullMethodName(),
transcodingService.findResponseMarshaller(fullMethodName));
transcodingServer.addMetadataHandler(methodDescriptor.getFullMethodName(), metadata);

mappedMethods.add(serviceDefinition);

Expand Down Expand Up @@ -105,13 +102,26 @@ public void handle(Void unused) {
}
}

LOGGER.info("Binding transcoding server");
GrpcTranscodingBridge bridge = new GrpcTranscodingBridge(mappedMethods);
bridge.bind(transcodingServer);

return new RuntimeValue<>(transcodingServer);
}

private <Req extends Message, Resp extends Message> GrpcTranscodingMetadata<Req, Resp> createMetadata(
GrpcTranscodingMethod transcodingMethod, MethodDescriptor<Req, Resp> methodDescriptor,
MutinyTranscodingService transcodingService) {
String fullMethodName = methodDescriptor.getFullMethodName()
.substring(methodDescriptor.getFullMethodName().lastIndexOf("/") + 1);
fullMethodName = Character.toLowerCase(fullMethodName.charAt(0)) + fullMethodName.substring(1);

GrpcTranscodingMarshaller<Req> requestMarshaller = transcodingService.findRequestMarshaller(fullMethodName);
GrpcTranscodingMarshaller<Resp> responseMarshaller = transcodingService.findResponseMarshaller(fullMethodName);

return new GrpcTranscodingMetadata<>(transcodingMethod.getHttpMethodName(), fullMethodName, requestMarshaller,
responseMarshaller, methodDescriptor);
}

private List<GrpcTranscodingMethod> findTranscodingMethods(Map<String, List<GrpcTranscodingMethod>> transcodingMethods,
String grpcServiceName) {
List<GrpcTranscodingMethod> methods = new ArrayList<>();
Expand All @@ -131,7 +141,6 @@ private GrpcTranscodingMethod findTranscodingMethod(List<GrpcTranscodingMethod>
fullMethodName = Character.toLowerCase(fullMethodName.charAt(0)) + fullMethodName.substring(1);

for (GrpcTranscodingMethod transcodingMethod : transcodingMethods) {
LOGGER.info("Transcoding method: " + transcodingMethod.getGrpcMethodName());
if (transcodingMethod.getGrpcMethodName().startsWith(fullMethodName)) {
return transcodingMethod;
}
Expand Down Expand Up @@ -162,10 +171,7 @@ private static List<MutinyTranscodingService> collectTranscodingServices(Instanc

private static GrpcServerRecorder.GrpcServiceDefinition findGrpcService(
List<GrpcServerRecorder.GrpcServiceDefinition> grpcServices, MutinyTranscodingService transcodingService) {
LOGGER.info("Looking for gRPC service " + transcodingService.getGrpcServiceName() + " in " + grpcServices.size()
+ " services");
for (GrpcServerRecorder.GrpcServiceDefinition grpcService : grpcServices) {
LOGGER.info("gRPC service: " + grpcService.getImplementationClassName());
if (grpcService.getImplementationClassName().startsWith(transcodingService.getGrpcServiceName())) {
return grpcService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,13 @@ private static class ServerCallImpl<Req, Resp> extends ServerCall<Req, Resp> {
private final TranscodingReadStreamAdapter<Req> readAdapter;
private final TranscodingWriteStreamAdapter<Resp> writeAdapter;
private ServerCall.Listener<Req> listener;
private final Decompressor decompressor;
private Compressor compressor;
private boolean halfClosed;
private boolean closed;
private int messagesSent;
private final Attributes attributes;

public ServerCallImpl(GrpcServerRequest<Req, Resp> req, ServerMethodDefinition<Req, Resp> methodDef,
MethodDescriptor.Marshaller<Req> reqMarshaller, MethodDescriptor.Marshaller<Resp> respMarshaller) {
String encoding = "identity";

this.decompressor = DecompressorRegistry.getDefaultInstance().lookupDecompressor(encoding);
this.req = (GrpcTranscodingRequest<Req, Resp>) req;
this.methodDef = methodDef;
this.reqMarshaller = reqMarshaller;
Expand Down Expand Up @@ -186,8 +181,8 @@ public MethodDescriptor<Req, Resp> getMethodDescriptor() {

@Override
public void setCompression(String encoding) {
compressor = CompressorRegistry.getDefaultInstance().lookupCompressor(encoding);
req.response().encoding(encoding);
// ????
super.setCompression(encoding);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
package io.quarkus.grpc.runtime.transcoding;

import com.google.api.HttpRule;
import com.google.protobuf.Message;

import io.grpc.MethodDescriptor;
import io.quarkus.grpc.GrpcTranscodingMarshaller;

public class GrpcTranscodingMetadata<Req, Resp> {
public class GrpcTranscodingMetadata<Req extends Message, Resp extends Message> {

private final String httpMethodName;
private final String grpcMethodName;
private final GrpcTranscodingMarshaller<Req> requestMarshaller;
private final GrpcTranscodingMarshaller<Resp> responseMarshaller;
private final MethodDescriptor<Req, Resp> methodDescriptor;

public GrpcTranscodingMetadata(MethodDescriptor<Req, Resp> methodDescriptor, HttpRule httpRule) {
public GrpcTranscodingMetadata(String httpMethodName, String grpcMethodName,
GrpcTranscodingMarshaller<Req> requestMarshaller,
GrpcTranscodingMarshaller<Resp> responseMarshaller, MethodDescriptor<Req, Resp> methodDescriptor) {
this.httpMethodName = httpMethodName;
this.grpcMethodName = grpcMethodName;
this.requestMarshaller = requestMarshaller;
this.responseMarshaller = responseMarshaller;
this.methodDescriptor = methodDescriptor;
}

public String getName() {
return "grpc/" + methodDescriptor.getFullMethodName();
public String getHttpMethodName() {
return httpMethodName;
}

public String getGrpcMethodName() {
return grpcMethodName;
}

public GrpcTranscodingMarshaller<Req> getRequestMarshaller() {
return requestMarshaller;
}

public GrpcTranscodingMarshaller<Resp> getResponseMarshaller() {
return responseMarshaller;
}

public MethodDescriptor<Req, Resp> getMethodDescriptor() {
return methodDescriptor;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Map;

import io.grpc.MethodDescriptor;
import io.quarkus.grpc.GrpcTranscodingMarshaller;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
Expand All @@ -20,9 +19,8 @@ public class GrpcTranscodingServer implements GrpcServer {
private final Vertx vertx;
private Handler<GrpcServerRequest<Buffer, Buffer>> requestHandler;
private Map<String, String> methodMapping = new HashMap<>();
private Map<String, GrpcTranscodingMarshaller<?>> requestMarshallers = new HashMap<>();
private Map<String, GrpcTranscodingMarshaller<?>> responseMarshallers = new HashMap<>();
private Map<String, GrpcTranscodingServer.MethodCallHandler<?, ?>> methodCallHandlers = new HashMap<>();
private Map<String, GrpcTranscodingMetadata<?, ?>> metadataHandlers = new HashMap<>();

public GrpcTranscodingServer(Vertx vertx) {
this.vertx = vertx;
Expand All @@ -32,7 +30,6 @@ public GrpcTranscodingServer(Vertx vertx) {
public void handle(HttpServerRequest httpRequest) {
String mappedMethod = methodMapping.get(httpRequest.path());
if (mappedMethod == null) {
httpRequest.response().setStatusCode(404).end();
return;
}

Expand All @@ -42,17 +39,6 @@ public void handle(HttpServerRequest httpRequest) {
if (method != null) {
handle(method, httpRequest, methodCall);
} else {
/*
* Handler<GrpcServerRequest<Buffer, Buffer>> handler = requestHandler;
* if (handler != null) {
* GrpcServerRequestImpl<Buffer, Buffer> grpcRequest = new GrpcServerRequestImpl<>(httpRequest,
* GrpcMessageDecoder.IDENTITY, GrpcMessageEncoder.IDENTITY, methodCall);
* grpcRequest.init();
* handler.handle(grpcRequest);
* } else {
* httpRequest.response().setStatusCode(500).end();
* }
*/
httpRequest.response().setStatusCode(500).end();
}
}
Expand All @@ -75,11 +61,8 @@ public GrpcServer callHandler(Handler<GrpcServerRequest<Buffer, Buffer>> handler
public <Req, Resp> GrpcServer callHandler(MethodDescriptor<Req, Resp> methodDesc,
Handler<GrpcServerRequest<Req, Resp>> handler) {
if (handler != null) {
MethodDescriptor.Marshaller<Req> reqMarshaller = (MethodDescriptor.Marshaller<Req>) requestMarshallers
.get(methodDesc.getFullMethodName());

MethodDescriptor.Marshaller<Resp> respMarshaller = (MethodDescriptor.Marshaller<Resp>) responseMarshallers
.get(methodDesc.getFullMethodName());
MethodDescriptor.Marshaller<Req> reqMarshaller = findRequestMarshaller(methodDesc.getFullMethodName());
MethodDescriptor.Marshaller<Resp> respMarshaller = findResponseMarshaller(methodDesc.getFullMethodName());

methodCallHandlers.put(methodDesc.getFullMethodName(),
new GrpcTranscodingServer.MethodCallHandler<>(methodDesc,
Expand All @@ -95,22 +78,20 @@ public void addMethodMapping(String path, String fullMethodName) {
methodMapping.put(path, fullMethodName);
}

public void addRequestMarshaller(String fullMethodName, GrpcTranscodingMarshaller<?> marshaller) {
requestMarshallers.put(fullMethodName, marshaller);
}

public void addResponseMarshaller(String fullMethodName, GrpcTranscodingMarshaller<?> marshaller) {
responseMarshallers.put(fullMethodName, marshaller);
public void addMetadataHandler(String fullMethodName, GrpcTranscodingMetadata<?, ?> metadata) {
metadataHandlers.put(fullMethodName, metadata);
}

@SuppressWarnings("unchecked")
public <T> MethodDescriptor.Marshaller<T> findRequestMarshaller(String fullMethodName) {
return (MethodDescriptor.Marshaller<T>) requestMarshallers.get(fullMethodName);
GrpcTranscodingMetadata<?, ?> metadata = metadataHandlers.get(fullMethodName);
return (MethodDescriptor.Marshaller<T>) metadata.getRequestMarshaller();
}

@SuppressWarnings("unchecked")
public <T> MethodDescriptor.Marshaller<T> findResponseMarshaller(String fullMethodName) {
return (MethodDescriptor.Marshaller<T>) responseMarshallers.get(fullMethodName);
GrpcTranscodingMetadata<?, ?> metadata = metadataHandlers.get(fullMethodName);
return (MethodDescriptor.Marshaller<T>) metadata.getResponseMarshaller();
}

private static class MethodCallHandler<Req, Resp> implements Handler<GrpcServerRequest<Req, Resp>> {
Expand Down

This file was deleted.

0 comments on commit 2af788d

Please sign in to comment.