grpc-java icon indicating copy to clipboard operation
grpc-java copied to clipboard

ProtoReflectionService class should accept Protobuf schema files with the same name

Open gfigueroa opened this issue 4 years ago • 4 comments

What version of gRPC-Java are you using?

1.47.0

What is your environment?

macOS Big Sur 11.4, OpenJDK 17.0.3

What did you expect to see?

gRPC services added to a server should be exposed by the ProtoReflectionService class, even if the two services are defined in two different .proto files with the same name.

What did you see instead?

The ProtoReflectionService class only exposes the first service added to a gRPC server (with .addService() method). The issue comes from (ProtoReflectionService.java):

if (!seenFiles.contains(fileDescriptor.getName())) {
  seenFiles.add(fileDescriptor.getName());
  fileDescriptorsToProcess.add(fileDescriptor);
}

The implementation requires .proto file names to be different, even if they are in completely different source code repositories, directories, or have different namespaces.

Steps to reproduce the bug

  1. Define a first gRPC service called InternalService in a Protobuf schema file src/proto/internal/services.proto.
  2. Define a second gRPC service called ExternalService in a Protobuf schema file named src/proto/external/services.proto.
  3. Create Server and add the two services, and an instance of the ProtoReflectionService:
Server server = ServerBuilder
        .forPort(8980)
        .addService(new InternalService())
        .addService(new ExternalService())
        .addService(ProtoReflectionService.newInstance())
        .build();
  1. Call one of the rpcs in ExternalService (i.e. second service added to server) with grpcurl.
  2. Get back this error:
Error invoking method "specs.external.ExternalService/someRpc": target server does not expose service "specs.external.ExternalService"

gfigueroa avatar Nov 21 '22 14:11 gfigueroa

Thanks for reporting this. Could you please post both services.proto here? You could simplify them if you wish. We'd like to take a look at the package statements and how services are defined.

sergiitk avatar Dec 13 '22 22:12 sergiitk

@sergiitk thanks for your reply. I think I have found the source of the problem in the grpc code, where (leaf) file names are checked regardless of their path or proto definitions (ProtoReflectionService.java).

if (!seenFiles.contains(fileDescriptor.getName())) {
  seenFiles.add(fileDescriptor.getName());
  fileDescriptorsToProcess.add(fileDescriptor);
}

However, for completeness, below are the two proto files (simplified).

src/proto/internal/services.proto (used in InternalService):

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.service.internal_query_api";
option java_outer_classname = "InternalQueryGrpcProto";

package specs.grpc_service1;

service InternalService {
...

src/proto/external/services.proto (used in ExternalService):

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.service.matform.api";
option java_outer_classname = "MatformGrpcProto";

package specs.grpc_service2;

service ExternalService {
...

gfigueroa avatar Dec 14 '22 08:12 gfigueroa

I suspect the issue may be how the folders are handled by your build system.

I think I have found the source of the problem in the grpc code, where (leaf) file names are checked regardless of their path or proto definitions

That means getName() returns the bare name? getFullName() is an alias of getName() and I don't see any "get folder," so that doesn't seem right.

Testing shows I get the folder in the name. The value of io.grpc.reflection.v1alpha.ServerReflectionProto.getDescriptor().getName():

grpc/reflection/v1alpha/reflection.proto

So the problem is more likely that your two protos are both in the "root" of the proto path. There are three (!) namespaces:

  1. The proto file path
  2. The proto package
  3. The java package

The convention is to have (1) and (2) be mirrors of each other, so at the very least I'd expect the path of the internal service proto to end with specs/grpc_service1/services.proto. C++ uses (1) for imports but (2) for C++ namespace IIRC, so in general having them be the same (with just / replaced with .) makes life easier. To know more precisely what the path should be would depend on your build system configuration (Maven, Gradle, Bazel, etc).

ejona86 avatar Jan 03 '23 19:01 ejona86

Seems like this is resolved. If not, comment, and it can be reopened.

ejona86 avatar Jan 12 '23 19:01 ejona86