Skip to content

Commit

Permalink
feat: REGAPIC Multitransport implementation (grpc+rest) (#833)
Browse files Browse the repository at this point in the history
1) Fully backward compatible with grpc-only GAPIC
2) Strictly speaking not fully backward compatible with rest-only REGAPIC (but is compatible for practical cases). See below for details.
3) Introduces the concept of default transport (is necessary to preserve backward-compabitility). The default behavior assumes grpc transport.

Needed to unblock DIREGAPIC LRO. To be reviewed after submission
  • Loading branch information
vam-google authored Sep 6, 2021
1 parent fdcfe70 commit 445daf4
Show file tree
Hide file tree
Showing 32 changed files with 1,308 additions and 464 deletions.
30 changes: 25 additions & 5 deletions rules_java_gapic/java_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def _java_gapic_srcjar(

if grpc_service_config:
file_args_dict[grpc_service_config] = "grpc-service-config"
elif transport != "rest":
elif not transport or transport == "grpc":
for keyword in NO_GRPC_CONFIG_ALLOWLIST:
if keyword not in name:
fail("Missing a gRPC service config file")
Expand Down Expand Up @@ -230,16 +230,25 @@ def java_gapic_library(
"@javax_annotation_javax_annotation_api//jar",
]

if transport == "rest":
if not transport or transport == "grpc":
actual_deps += [
"@com_google_api_gax_java//gax-grpc:gax_grpc",
"@io_grpc_grpc_java//core:core",
"@io_grpc_grpc_java//protobuf:protobuf",
]
elif transport == "rest":
actual_deps += [
"@com_google_api_gax_java//gax-httpjson:gax_httpjson",
]
else:
elif transport == "grpc+rest":
actual_deps += [
"@com_google_api_gax_java//gax-grpc:gax_grpc",
"@io_grpc_grpc_java//core:core",
"@io_grpc_grpc_java//protobuf:protobuf",
"@com_google_api_gax_java//gax-httpjson:gax_httpjson",
]
else:
fail("Unknown transport: %s" % transport)

native.java_library(
name = name,
Expand All @@ -256,18 +265,29 @@ def java_gapic_library(
"@junit_junit//jar",
]

if transport == "rest":
if not transport or transport == "grpc":
actual_test_deps += [
"@com_google_api_gax_java//gax-grpc:gax_grpc_testlib",
"@io_grpc_grpc_java//auth:auth",
"@io_grpc_grpc_netty_shaded//jar",
"@io_grpc_grpc_java//stub:stub",
"@io_opencensus_opencensus_contrib_grpc_metrics//jar",
]
elif transport == "rest":
actual_test_deps += [
"@com_google_api_gax_java//gax-httpjson:gax_httpjson_testlib",
]
else:
elif transport == "grpc+rest":
actual_test_deps += [
"@com_google_api_gax_java//gax-grpc:gax_grpc_testlib",
"@io_grpc_grpc_java//auth:auth",
"@io_grpc_grpc_netty_shaded//jar",
"@io_grpc_grpc_java//stub:stub",
"@io_opencensus_opencensus_contrib_grpc_metrics//jar",
"@com_google_api_gax_java//gax-httpjson:gax_httpjson_testlib",
]
else:
fail("Unknown transport: %s" % transport)

_append_dep_without_duplicates(actual_test_deps, test_deps)
_append_dep_without_duplicates(actual_test_deps, actual_deps)
Expand Down
8 changes: 5 additions & 3 deletions rules_java_gapic/java_gapic_pkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,12 @@ def java_gapic_assembly_gradle_pkg(
grpc_target_dep = ["%s" % grpc_target]

if client_deps:
if transport == "rest":
template_label = Label("//rules_java_gapic:resources/gradle/client_rest.gradle.tmpl")
else:
if not transport or transport == "rest":
template_label = Label("//rules_java_gapic:resources/gradle/client_grpc.gradle.tmpl")
elif transport == "rest":
template_label = Label("//rules_java_gapic:resources/gradle/client_rest.gradle.tmpl")
elif transport == "grpc+rest":
template_label = Label("//rules_java_gapic:resources/gradle/client_grpcrest.gradle.tmpl")

_java_gapic_gradle_pkg(
name = client_target,
Expand Down
63 changes: 63 additions & 0 deletions rules_java_gapic/resources/gradle/client_grpcrest.gradle.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
buildscript {
repositories {
mavenCentral()
}
}

apply plugin: 'java'

description = 'GAPIC library for {{name}}'
group = 'com.google.cloud'
version = (findProperty('version') == 'unspecified') ? '0.0.0-SNAPSHOT' : version
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenCentral()
mavenLocal()
}

compileJava.options.encoding = 'UTF-8'
javadoc.options.encoding = 'UTF-8'

dependencies {
compile 'com.google.api:gax:{{version.gax}}'
testCompile 'com.google.api:gax:{{version.gax}}:testlib'
compile 'com.google.api:gax-grpc:{{version.gax_grpc}}'
testCompile 'com.google.api:gax-grpc:{{version.gax_grpc}}:testlib'
compile 'com.google.api:gax-httpjson:{{version.gax_httpjson}}'
testCompile 'com.google.api:gax-httpjson:{{version.gax_httpjson}}:testlib'
testCompile 'io.grpc:grpc-netty-shaded:{{version.io_grpc}}'
testCompile '{{maven.junit_junit}}'
{{extra_deps}}
}

task smokeTest(type: Test) {
filter {
includeTestsMatching "*SmokeTest"
setFailOnNoMatchingTests false
}
}

test {
exclude "**/*SmokeTest*"
}

sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
}

clean {
delete 'all-jars'
}

task allJars(type: Copy) {
dependsOn test, jar
into 'all-jars'
// Replace with `from configurations.testRuntime, jar` to include test dependencies
from configurations.runtime, jar
}
1 change: 1 addition & 0 deletions src/main/java/com/google/api/generator/gapic/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ filegroup(
"//src/main/java/com/google/api/generator/gapic/composer/comment:comment_files",
"//src/main/java/com/google/api/generator/gapic/composer/defaultvalue:defaultvalue_files",
"//src/main/java/com/google/api/generator/gapic/composer/grpc:grpc_files",
"//src/main/java/com/google/api/generator/gapic/composer/grpcrest:grpcrest_files",
"//src/main/java/com/google/api/generator/gapic/composer/resourcename:resourcename_files",
"//src/main/java/com/google/api/generator/gapic/composer/rest:rest_files",
"//src/main/java/com/google/api/generator/gapic/composer/samplecode:samplecode_files",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ java_library(
"//src/main/java/com/google/api/generator/gapic/composer/common",
"//src/main/java/com/google/api/generator/gapic/composer/defaultvalue",
"//src/main/java/com/google/api/generator/gapic/composer/grpc",
"//src/main/java/com/google/api/generator/gapic/composer/grpcrest",
"//src/main/java/com/google/api/generator/gapic/composer/resourcename",
"//src/main/java/com/google/api/generator/gapic/composer/rest",
"//src/main/java/com/google/api/generator/gapic/composer/samplecode",
Expand Down
79 changes: 49 additions & 30 deletions src/main/java/com/google/api/generator/gapic/composer/Composer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package com.google.api.generator.gapic.composer;

import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.gapic.composer.comment.CommentComposer;
import com.google.api.generator.gapic.composer.grpc.GrpcServiceCallableFactoryClassComposer;
import com.google.api.generator.gapic.composer.grpc.GrpcServiceStubClassComposer;
Expand All @@ -26,11 +25,11 @@
import com.google.api.generator.gapic.composer.grpc.ServiceSettingsClassComposer;
import com.google.api.generator.gapic.composer.grpc.ServiceStubClassComposer;
import com.google.api.generator.gapic.composer.grpc.ServiceStubSettingsClassComposer;
import com.google.api.generator.gapic.composer.grpcrest.HttpJsonServiceClientTestClassComposer;
import com.google.api.generator.gapic.composer.resourcename.ResourceNameHelperClassComposer;
import com.google.api.generator.gapic.composer.rest.HttpJsonServiceCallableFactoryClassComposer;
import com.google.api.generator.gapic.composer.rest.HttpJsonServiceStubClassComposer;
import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.GapicClass.Kind;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicPackageInfo;
import com.google.api.generator.gapic.model.Service;
Expand Down Expand Up @@ -87,12 +86,30 @@ public static List<GapicClass> generateStubClasses(GapicContext context) {
clazzes.add(
HttpJsonServiceCallableFactoryClassComposer.instance().generate(context, s));
clazzes.add(HttpJsonServiceStubClassComposer.instance().generate(context, s));
} else {
} else if (context.transport() == Transport.GRPC) {
clazzes.add(ServiceStubClassComposer.instance().generate(context, s));
clazzes.add(ServiceStubSettingsClassComposer.instance().generate(context, s));
clazzes.add(
GrpcServiceCallableFactoryClassComposer.instance().generate(context, s));
clazzes.add(GrpcServiceStubClassComposer.instance().generate(context, s));
} else if (context.transport() == Transport.GRPC_REST) {
clazzes.add(
com.google.api.generator.gapic.composer.grpcrest.ServiceStubClassComposer
.instance()
.generate(context, s));
clazzes.add(
com.google.api.generator.gapic.composer.grpcrest
.ServiceStubSettingsClassComposer.instance()
.generate(context, s));
clazzes.add(
GrpcServiceCallableFactoryClassComposer.instance().generate(context, s));
clazzes.add(GrpcServiceStubClassComposer.instance().generate(context, s));
clazzes.add(
HttpJsonServiceCallableFactoryClassComposer.instance().generate(context, s));
clazzes.add(
com.google.api.generator.gapic.composer.grpcrest
.HttpJsonServiceStubClassComposer.instance()
.generate(context, s));
}
});
return clazzes;
Expand All @@ -113,9 +130,18 @@ public static List<GapicClass> generateClientSettingsClasses(GapicContext contex
com.google.api.generator.gapic.composer.rest.ServiceSettingsClassComposer
.instance()
.generate(context, s));
} else {
} else if (context.transport() == Transport.GRPC) {
clazzes.add(ServiceClientClassComposer.instance().generate(context, s));
clazzes.add(ServiceSettingsClassComposer.instance().generate(context, s));
} else if (context.transport() == Transport.GRPC_REST) {
clazzes.add(
com.google.api.generator.gapic.composer.grpcrest.ServiceClientClassComposer
.instance()
.generate(context, s));
clazzes.add(
com.google.api.generator.gapic.composer.grpcrest.ServiceSettingsClassComposer
.instance()
.generate(context, s));
}
});
return clazzes;
Expand All @@ -127,7 +153,10 @@ public static List<GapicClass> generateMockClasses(GapicContext context, List<Se
s -> {
if (context.transport() == Transport.REST) {
// REST transport tests donot not use mock services.
} else {
} else if (context.transport() == Transport.GRPC) {
clazzes.add(MockServiceClassComposer.instance().generate(context, s));
clazzes.add(MockServiceImplClassComposer.instance().generate(context, s));
} else if (context.transport() == Transport.GRPC_REST) {
clazzes.add(MockServiceClassComposer.instance().generate(context, s));
clazzes.add(MockServiceImplClassComposer.instance().generate(context, s));
}
Expand All @@ -136,35 +165,25 @@ public static List<GapicClass> generateMockClasses(GapicContext context, List<Se
}

public static List<GapicClass> generateTestClasses(GapicContext context) {
return context.services().stream()
.map(
List<GapicClass> clazzes = new ArrayList<>();
context
.services()
.forEach(
s -> {
if (context.transport() == Transport.REST) {
return com.google.api.generator.gapic.composer.rest.ServiceClientTestClassComposer
.instance()
.generate(context, s);
} else {
return ServiceClientTestClassComposer.instance().generate(context, s);
clazzes.add(
com.google.api.generator.gapic.composer.rest.ServiceClientTestClassComposer
.instance()
.generate(context, s));
} else if (context.transport() == Transport.GRPC) {
clazzes.add(ServiceClientTestClassComposer.instance().generate(context, s));
} else if (context.transport() == Transport.GRPC_REST) {
clazzes.add(ServiceClientTestClassComposer.instance().generate(context, s));
clazzes.add(HttpJsonServiceClientTestClassComposer.instance().generate(context, s));
}
})
.collect(Collectors.toList());
}

/** ====================== HELPERS ==================== */
// TODO(miraleung): Add method list.
private static GapicClass generateGenericClass(Kind kind, String name, Service service) {
String pakkage = service.pakkage();
if (kind.equals(Kind.STUB)) {
pakkage += ".stub";
}
});

ClassDefinition classDef =
ClassDefinition.builder()
.setPackageString(pakkage)
.setName(name)
.setScope(ScopeNode.PUBLIC)
.build();
return GapicClass.create(kind, classDef);
return clazzes;
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ protected MethodDefinition createGenericCallableMethod(
.setVariable(
Variable.builder()
.setName("operationsStub")
.setType(getTransportContext().operationsStubType())
.setType(getTransportContext().operationsStubTypes().get(0))
.build())
.setIsDecl(true)
.build());
Expand Down
Loading

0 comments on commit 445daf4

Please sign in to comment.