Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rules_java_gapic/java_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ def java_gapic_library(
if gapic_yaml:
file_args_dict[gapic_yaml] = "gapic-config"

if gapic_yaml:
file_args_dict[gapic_yaml] = "gapic-config"

# Check the allow-list.
if service_yaml:
service_yaml_in_allowlist = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,24 @@ public class GapicServiceConfig {

private final List<MethodConfig> methodConfigs;
private final Map<MethodConfig.Name, Integer> methodConfigTable;
private final Map<MethodConfig.Name, GapicLroRetrySettings> lroRetrySettingsTable;
private final Map<MethodConfig.Name, GapicBatchingSettings> batchingSettingsTable;

private GapicServiceConfig(
List<MethodConfig> methodConfigs,
Map<MethodConfig.Name, Integer> methodConfigTable,
Map<MethodConfig.Name, GapicLroRetrySettings> lroRetrySettingsTable,
Map<MethodConfig.Name, GapicBatchingSettings> batchingSettingsTable) {
this.methodConfigs = methodConfigs;
this.methodConfigTable = methodConfigTable;
this.lroRetrySettingsTable = lroRetrySettingsTable;
this.batchingSettingsTable = batchingSettingsTable;
}

public static GapicServiceConfig create(
ServiceConfig serviceConfig, Optional<List<GapicBatchingSettings>> batchingSettingsOpt) {
ServiceConfig serviceConfig,
Optional<List<GapicLroRetrySettings>> lroRetrySettingsOpt,
Optional<List<GapicBatchingSettings>> batchingSettingsOpt) {
// Keep this processing logic out of the constructor.
Map<MethodConfig.Name, Integer> methodConfigTable = new HashMap<>();
List<MethodConfig> methodConfigs = serviceConfig.getMethodConfigList();
Expand All @@ -65,6 +70,20 @@ public static GapicServiceConfig create(
}
}

Map<MethodConfig.Name, GapicLroRetrySettings> lroRetrySettingsTable = new HashMap<>();
if (lroRetrySettingsOpt.isPresent()) {
for (GapicLroRetrySettings lroRetrySetting : lroRetrySettingsOpt.get()) {
lroRetrySettingsTable.put(
MethodConfig.Name.newBuilder()
.setService(
String.format(
"%s.%s", lroRetrySetting.protoPakkage(), lroRetrySetting.serviceName()))
.setMethod(lroRetrySetting.methodName())
.build(),
lroRetrySetting);
}
}

Map<MethodConfig.Name, GapicBatchingSettings> batchingSettingsTable = new HashMap<>();
if (batchingSettingsOpt.isPresent()) {
for (GapicBatchingSettings batchingSetting : batchingSettingsOpt.get()) {
Expand All @@ -79,7 +98,8 @@ public static GapicServiceConfig create(
}
}

return new GapicServiceConfig(methodConfigs, methodConfigTable, batchingSettingsTable);
return new GapicServiceConfig(
methodConfigs, methodConfigTable, lroRetrySettingsTable, batchingSettingsTable);
}

public Map<String, GapicRetrySettings> getAllGapicRetrySettings(Service service) {
Expand Down Expand Up @@ -118,10 +138,20 @@ public String getRetryParamsName(Service service, Method method) {
return NO_RETRY_PARAMS_NAME;
}

public boolean hasLroRetrySetting(Service service, Method method) {
return lroRetrySettingsTable.containsKey(toName(service, method));
}

public boolean hasBatchingSetting(Service service, Method method) {
return batchingSettingsTable.containsKey(toName(service, method));
}

public Optional<GapicLroRetrySettings> getLroRetrySetting(Service service, Method method) {
return hasLroRetrySetting(service, method)
? Optional.of(lroRetrySettingsTable.get(toName(service, method)))
: Optional.empty();
}

public Optional<GapicBatchingSettings> getBatchingSetting(Service service, Method method) {
return hasBatchingSetting(service, method)
? Optional.of(batchingSettingsTable.get(toName(service, method)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.api.generator.gapic.model.Field;
import com.google.api.generator.gapic.model.GapicBatchingSettings;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicLroRetrySettings;
import com.google.api.generator.gapic.model.GapicServiceConfig;
import com.google.api.generator.gapic.model.LongrunningOperation;
import com.google.api.generator.gapic.model.Message;
Expand Down Expand Up @@ -84,11 +85,13 @@ public static GapicContext parse(CodeGeneratorRequest request) {
PluginArgumentParser.parseGapicYamlConfigPath(request);
Optional<List<GapicBatchingSettings>> batchingSettingsOpt =
BatchingSettingsConfigParser.parse(gapicYamlConfigPathOpt);
Optional<List<GapicLroRetrySettings>> lroRetrySettingsOpt =
GapicLroRetrySettingsParser.parse(gapicYamlConfigPathOpt);

Optional<String> serviceConfigPathOpt = PluginArgumentParser.parseJsonConfigPath(request);
String serviceConfigPath = serviceConfigPathOpt.isPresent() ? serviceConfigPathOpt.get() : null;
Optional<GapicServiceConfig> serviceConfigOpt =
ServiceConfigParser.parse(serviceConfigPath, batchingSettingsOpt);
ServiceConfigParser.parse(serviceConfigPath, lroRetrySettingsOpt, batchingSettingsOpt);

Optional<String> serviceYamlConfigPathOpt =
PluginArgumentParser.parseServiceYamlConfigPath(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.api.generator.gapic.protoparser;

import com.google.api.generator.gapic.model.GapicBatchingSettings;
import com.google.api.generator.gapic.model.GapicLroRetrySettings;
import com.google.api.generator.gapic.model.GapicServiceConfig;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
Expand All @@ -28,12 +29,15 @@

public class ServiceConfigParser {
public static Optional<GapicServiceConfig> parse(
String serviceConfigFilePath, Optional<List<GapicBatchingSettings>> batchingSettingsOpt) {
String serviceConfigFilePath,
Optional<List<GapicLroRetrySettings>> lroRetrySettingsOpt,
Optional<List<GapicBatchingSettings>> batchingSettingsOpt) {
Optional<ServiceConfig> rawConfigOpt = parseFile(serviceConfigFilePath);
if (!rawConfigOpt.isPresent()) {
return Optional.empty();
}
return Optional.of(GapicServiceConfig.create(rawConfigOpt.get(), batchingSettingsOpt));
return Optional.of(
GapicServiceConfig.create(rawConfigOpt.get(), lroRetrySettingsOpt, batchingSettingsOpt));
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void batchingDescriptor_hasSubresponseField() {
String jsonFilename = "pubsub_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> configOpt =
ServiceConfigParser.parse(jsonPath.toString(), batchingSettingsOpt);
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), batchingSettingsOpt);
assertTrue(configOpt.isPresent());
GapicServiceConfig config = configOpt.get();

Expand Down Expand Up @@ -151,7 +151,7 @@ public void batchingDescriptor_noSubresponseField() {
String jsonFilename = "logging_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> configOpt =
ServiceConfigParser.parse(jsonPath.toString(), batchingSettingsOpt);
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), batchingSettingsOpt);
assertTrue(configOpt.isPresent());
GapicServiceConfig config = configOpt.get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void paramDefinitionsBlock_noConfigsFound() {
String jsonFilename = "retrying_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> serviceConfigOpt =
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty());
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), Optional.empty());
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();

Expand Down Expand Up @@ -129,7 +129,7 @@ public void paramDefinitionsBlock_basic() {
String jsonFilename = "showcase_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> serviceConfigOpt =
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty());
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), Optional.empty());
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();

Expand Down Expand Up @@ -181,7 +181,7 @@ public void codesDefinitionsBlock_noConfigsFound() {
String jsonFilename = "retrying_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> serviceConfigOpt =
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty());
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), Optional.empty());
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();

Expand Down Expand Up @@ -219,7 +219,7 @@ public void codesDefinitionsBlock_basic() {
String jsonFilename = "showcase_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> serviceConfigOpt =
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty());
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), Optional.empty());
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();

Expand Down Expand Up @@ -260,7 +260,7 @@ public void simpleBuilderExpr_basic() {
String jsonFilename = "showcase_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> serviceConfigOpt =
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty());
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), Optional.empty());
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();

Expand Down Expand Up @@ -343,7 +343,7 @@ public void lroBuilderExpr() {
String jsonFilename = "showcase_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> serviceConfigOpt =
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty());
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), Optional.empty());
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();

Expand Down Expand Up @@ -411,7 +411,7 @@ public void batchingSettings_minimalFlowControlSettings() {
String jsonFilename = "pubsub_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> configOpt =
ServiceConfigParser.parse(jsonPath.toString(), batchingSettingsOpt);
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), batchingSettingsOpt);
assertTrue(configOpt.isPresent());
GapicServiceConfig config = configOpt.get();

Expand Down Expand Up @@ -491,7 +491,7 @@ public void batchingSettings_fullFlowControlSettings() {
String jsonFilename = "logging_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> configOpt =
ServiceConfigParser.parse(jsonPath.toString(), batchingSettingsOpt);
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), batchingSettingsOpt);
assertTrue(configOpt.isPresent());
GapicServiceConfig config = configOpt.get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void generateServiceStubSettingsClasses_batchingWithEmptyResponses() thro
String jsonFilename = "logging_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> configOpt =
ServiceConfigParser.parse(jsonPath.toString(), batchingSettingsOpt);
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), batchingSettingsOpt);
assertTrue(configOpt.isPresent());
GapicServiceConfig config = configOpt.get();

Expand Down Expand Up @@ -127,7 +127,7 @@ public void generateServiceStubSettingsClasses_batchingWithNonemptyResponses()
String jsonFilename = "pubsub_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> configOpt =
ServiceConfigParser.parse(jsonPath.toString(), batchingSettingsOpt);
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), batchingSettingsOpt);
assertTrue(configOpt.isPresent());
GapicServiceConfig config = configOpt.get();

Expand Down Expand Up @@ -158,7 +158,7 @@ public void generateServiceStubSettingsClasses_basic() throws IOException {
String jsonFilename = "showcase_grpc_service_config.json";
Path jsonPath = Paths.get(ComposerConstants.TESTFILES_DIRECTORY, jsonFilename);
Optional<GapicServiceConfig> configOpt =
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty());
ServiceConfigParser.parse(jsonPath.toString(), Optional.empty(), Optional.empty());
assertTrue(configOpt.isPresent());
GapicServiceConfig config = configOpt.get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

public class GapicServiceConfigTest {
private static final double EPSILON = 1e-4;
private static final String JSON_DIRECTORY =
private static final String TESTDATA_DIRECTORY =
"src/test/java/com/google/api/generator/gapic/testdata/";

@Test
Expand All @@ -49,10 +49,11 @@ public void serviceConfig_noConfigsFound() {
Service service = parseService(echoFileDescriptor);

String jsonFilename = "retrying_grpc_service_config.json";
Path jsonPath = Paths.get(JSON_DIRECTORY, jsonFilename);
Path jsonPath = Paths.get(TESTDATA_DIRECTORY, jsonFilename);
Optional<List<GapicLroRetrySettings>> lroRetrySettingsOpt = Optional.empty();
Optional<List<GapicBatchingSettings>> batchingSettingsOpt = Optional.empty();
Optional<GapicServiceConfig> serviceConfigOpt =
ServiceConfigParser.parse(jsonPath.toString(), batchingSettingsOpt);
ServiceConfigParser.parse(jsonPath.toString(), lroRetrySettingsOpt, batchingSettingsOpt);
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();

Expand Down Expand Up @@ -80,10 +81,11 @@ public void serviceConfig_basic() {
Service service = parseService(echoFileDescriptor);

String jsonFilename = "showcase_grpc_service_config.json";
Path jsonPath = Paths.get(JSON_DIRECTORY, jsonFilename);
Path jsonPath = Paths.get(TESTDATA_DIRECTORY, jsonFilename);
Optional<List<GapicLroRetrySettings>> lroRetrySettingsOpt = Optional.empty();
Optional<List<GapicBatchingSettings>> batchingSettingsOpt = Optional.empty();
Optional<GapicServiceConfig> serviceConfigOpt =
ServiceConfigParser.parse(jsonPath.toString(), batchingSettingsOpt);
ServiceConfigParser.parse(jsonPath.toString(), lroRetrySettingsOpt, batchingSettingsOpt);
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();

Expand Down Expand Up @@ -137,7 +139,7 @@ public void serviceConfig_withBatchingSettings() {
Service service = parseService(echoFileDescriptor);

String jsonFilename = "showcase_grpc_service_config.json";
Path jsonPath = Paths.get(JSON_DIRECTORY, jsonFilename);
Path jsonPath = Paths.get(TESTDATA_DIRECTORY, jsonFilename);

GapicBatchingSettings origBatchingSetting =
GapicBatchingSettings.builder()
Expand All @@ -152,9 +154,10 @@ public void serviceConfig_withBatchingSettings() {
.build();
Optional<List<GapicBatchingSettings>> batchingSettingsOpt =
Optional.of(Arrays.asList(origBatchingSetting));
Optional<List<GapicLroRetrySettings>> lroRetrySettingsOpt = Optional.empty();

Optional<GapicServiceConfig> serviceConfigOpt =
ServiceConfigParser.parse(jsonPath.toString(), batchingSettingsOpt);
ServiceConfigParser.parse(jsonPath.toString(), lroRetrySettingsOpt, batchingSettingsOpt);
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();

Expand Down Expand Up @@ -204,6 +207,49 @@ public void serviceConfig_withBatchingSettings() {
assertFalse(serviceConfig.hasBatchingSetting(service, method));
}

@Test
public void serviceConfig_withLroRetrySettings() {
FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
ServiceDescriptor echoServiceDescriptor = echoFileDescriptor.getServices().get(0);
Service service = parseService(echoFileDescriptor);

String jsonFilename = "showcase_grpc_service_config.json";
Path jsonPath = Paths.get(TESTDATA_DIRECTORY, jsonFilename);
Optional<List<GapicBatchingSettings>> batchingSettingsOpt = Optional.empty();

// Construct LRO retry settings.
GapicLroRetrySettings origLroRetrySetting =
GapicLroRetrySettings.builder()
.setProtoPakkage("google.showcase.v1beta1")
.setServiceName("Echo")
.setMethodName("Echo")
.setInitialPollDelayMillis(100)
.setPollDelayMultiplier(1.5)
.setMaxPollDelayMillis(200)
.setTotalPollTimeoutMillis(300)
.build();
Optional<List<GapicLroRetrySettings>> lroRetrySettingsOpt =
Optional.of(Arrays.asList(origLroRetrySetting));

Optional<GapicServiceConfig> serviceConfigOpt =
ServiceConfigParser.parse(jsonPath.toString(), lroRetrySettingsOpt, batchingSettingsOpt);
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();

// Check LRO retry settings.
Method method = findMethod(service, "Echo");
assertTrue(serviceConfig.hasLroRetrySetting(service, method));
Optional<GapicLroRetrySettings> retrievedSettingsOpt =
serviceConfig.getLroRetrySetting(service, method);
assertTrue(retrievedSettingsOpt.isPresent());
GapicLroRetrySettings retrievedSettings = retrievedSettingsOpt.get();
assertEquals(
origLroRetrySetting.initialPollDelayMillis(), retrievedSettings.initialPollDelayMillis());
assertEquals(origLroRetrySetting.maxPollDelayMillis(), retrievedSettings.maxPollDelayMillis());
assertEquals(
origLroRetrySetting.totalPollTimeoutMillis(), retrievedSettings.totalPollTimeoutMillis());
}

private static Service parseService(FileDescriptor fileDescriptor) {
Map<String, Message> messageTypes = Parser.parseMessages(fileDescriptor);
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(fileDescriptor);
Expand Down
1 change: 1 addition & 0 deletions test/integration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ java_gapic_assembly_gradle_pkg(
java_gapic_library(
name = "redis_java_gapic",
srcs = ["@com_google_googleapis//google/cloud/redis/v1:redis_proto_with_info"],
gapic_yaml = "@com_google_googleapis//google/cloud/redis/v1:redis_gapic.yaml",
grpc_service_config = "@com_google_googleapis//google/cloud/redis/v1:redis_grpc_service_config.json",
package = "google.cloud.redis.v1",
test_deps = [
Expand Down