use of com.google.api.codegen.RetryParamsDefinitionProto in project toolkit by googleapis.
the class RetryDefinitionsTransformer method createRetrySettingsDefinition.
public static ImmutableMap<String, RetrySettings> createRetrySettingsDefinition(DiagCollector diagCollector, InterfaceConfigProto interfaceConfigProto) {
ImmutableMap.Builder<String, RetrySettings> builder = ImmutableMap.builder();
for (RetryParamsDefinitionProto retryDef : interfaceConfigProto.getRetryParamsDefList()) {
try {
RetrySettings settings = RetrySettings.newBuilder().setInitialRetryDelay(Duration.ofMillis(retryDef.getInitialRetryDelayMillis())).setRetryDelayMultiplier(retryDef.getRetryDelayMultiplier()).setMaxRetryDelay(Duration.ofMillis(retryDef.getMaxRetryDelayMillis())).setInitialRpcTimeout(Duration.ofMillis(retryDef.getInitialRpcTimeoutMillis())).setRpcTimeoutMultiplier(retryDef.getRpcTimeoutMultiplier()).setMaxRpcTimeout(Duration.ofMillis(retryDef.getMaxRpcTimeoutMillis())).setTotalTimeout(Duration.ofMillis(retryDef.getTotalTimeoutMillis())).build();
builder.put(retryDef.getName(), settings);
} catch (IllegalStateException | NullPointerException e) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "error while creating retry params: %s (in interface %s)", e, interfaceConfigProto.getName()));
}
}
if (diagCollector.getErrorCount() > 0) {
return null;
}
return builder.build();
}
use of com.google.api.codegen.RetryParamsDefinitionProto in project toolkit by googleapis.
the class RetryDefinitionsTransformer method createRetrySettingsDefinition.
public static ImmutableMap<String, RetryParamsDefinitionProto> createRetrySettingsDefinition(InterfaceConfigProto interfaceConfigProto) {
ImmutableMap.Builder<String, RetryParamsDefinitionProto> builder = ImmutableMap.builder();
if (interfaceConfigProto.getRetryParamsDefCount() > 0) {
for (RetryParamsDefinitionProto retryDef : interfaceConfigProto.getRetryParamsDefList()) {
builder.put(retryDef.getName(), retryDef);
}
} else {
// Use default values.
RetryParamsDefinitionProto defaultRetryParams = RetryParamsDefinitionProto.getDefaultInstance().toBuilder().setInitialRetryDelayMillis(DEFAULT_INITIAL_RETRY_DELAY).setRetryDelayMultiplier(DEFAULT_RETRY_DELAY_MULTIPLIER).setMaxRetryDelayMillis(DEFAULT_MAX_RETRY_DELAY).setInitialRpcTimeoutMillis(DEFAULT_MAX_RPC_TIMEOUT_MILLIS).setRpcTimeoutMultiplier(DEFAULT_RPC_TIMEOUT_MULTIPLIER).setMaxRpcTimeoutMillis(DEFAULT_MAX_RPC_TIMEOUT_MILLIS).setTotalTimeoutMillis(DEFAULT_TOTAL_TIMEOUT_MILLIS).build();
builder.put(RETRY_PARAMS_DEFAULT_NAME, defaultRetryParams);
}
return builder.build();
}
use of com.google.api.codegen.RetryParamsDefinitionProto in project toolkit by googleapis.
the class GapicConfigProducerTest method testCreateProductWithGRPCServiceConfig.
@Test
public void testCreateProductWithGRPCServiceConfig() {
TestDataLocator locator = MixedPathTestDataLocator.create(this.getClass());
locator.addTestDataSource(CodegenTestUtil.class, "testsrc/protoannotations");
Model model = CodegenTestUtil.readModel(locator, tempDir, new String[] { "library.proto", "common_resources.proto", "another_service.proto" }, new String[] { "library.yaml" });
ServiceConfig serviceConfig = CodegenTestUtil.readGRPCServiceConfig(model.getDiagReporter().getDiagCollector(), locator, "library_grpc_service_config.json");
ConfigProto configProto = CodegenTestUtil.readConfig(model.getDiagReporter().getDiagCollector(), locator, new String[] { "library_v2_gapic.yaml" });
GapicProductConfig product = GapicProductConfig.create(model, configProto, null, "google.example.library.v1", null, TargetLanguage.GO, serviceConfig, TransportProtocol.GRPC);
assertThat(model.getDiagReporter().getDiagCollector().hasErrors()).isFalse();
assertThat(product).isNotNull();
InterfaceConfig libraryInterface = product.getInterfaceConfig("google.example.library.v1.LibraryService");
Map<String, RetryParamsDefinitionProto> params = libraryInterface.getRetrySettingsDefinition();
assertThat(params.get("retry_policy_1_params")).isNotNull();
assertThat(params.get("no_retry_1_params")).isNotNull();
assertThat(params.get("no_retry_1_params").getTotalTimeoutMillis()).isEqualTo(60000);
assertThat(params.get("no_retry_params")).isNotNull();
assertThat(params.get("no_retry_params").getTotalTimeoutMillis()).isEqualTo(0);
Map<String, ImmutableList<String>> codes = libraryInterface.getRetryCodesConfig().getRetryCodesDefinition();
assertThat(codes.get("retry_policy_1_codes")).isNotNull();
assertThat(codes.get("no_retry_1_codes")).isNotNull();
assertThat(codes.get("no_retry_codes")).isNotNull();
}
use of com.google.api.codegen.RetryParamsDefinitionProto in project toolkit by googleapis.
the class GrpcGapicRetryMapping method create.
public static GrpcGapicRetryMapping create(ServiceConfig config, ImmutableMap<String, Interface> protoInterfaces) {
Map<String, String> methodCodesMap = new HashMap<>();
Map<String, String> methodParamsMap = new HashMap<>();
Map<String, RetryCodesDefinitionProto> codesDefMap = new HashMap<>();
Map<String, RetryParamsDefinitionProto> paramsDefMap = new HashMap<>();
// add no_retry configs for unknown/unspecified methods
RetryParamsDefinitionProto.Builder defaultParamsBuilder = retryPolicyToParamsBuilder(RetryPolicy.getDefaultInstance(), 0, "no_retry_params");
paramsDefMap.putIfAbsent(defaultParamsBuilder.getName(), defaultParamsBuilder.build());
RetryCodesDefinitionProto.Builder defaultCodesBuilder = retryPolicyToCodesBuilder(RetryPolicy.getDefaultInstance(), "no_retry_codes");
codesDefMap.putIfAbsent(defaultCodesBuilder.getName(), defaultCodesBuilder.build());
// build retry-to-interface mapping from gRPC ServiceConfig
int retryIndex = 1;
int noRetryIndex = 1;
for (com.google.api.codegen.grpc.MethodConfig methodConfig : config.getMethodConfigList()) {
RetryPolicy retryPolicy = methodConfig.getRetryPolicy();
String policyName;
if (methodConfig.getRetryOrHedgingPolicyCase() == RetryOrHedgingPolicyCase.RETRY_POLICY) {
policyName = "retry_policy_" + retryIndex++;
} else {
// make "unique" no_retry configs because the MethodConfig timeout may differ
policyName = "no_retry_" + noRetryIndex++;
}
long timeout = Durations.toMillis(methodConfig.getTimeout());
// construct retry params from RetryPolicy
RetryParamsDefinitionProto.Builder paramsBuilder = retryPolicyToParamsBuilder(retryPolicy, timeout, policyName + "_params");
paramsDefMap.putIfAbsent(paramsBuilder.getName(), paramsBuilder.build());
// construct retry codes from RetryPolicy
RetryCodesDefinitionProto.Builder codesBuilder = retryPolicyToCodesBuilder(retryPolicy, policyName + "_codes");
codesDefMap.putIfAbsent(codesBuilder.getName(), codesBuilder.build());
// apply the MethodConfig.RetryPolicy to names
for (Name name : methodConfig.getNameList()) {
applyRetryPolicyToName(name, codesBuilder.getName(), paramsBuilder.getName(), methodCodesMap, methodParamsMap, protoInterfaces);
}
}
// make immutable
ImmutableMap<String, String> codes = ImmutableMap.copyOf(methodCodesMap);
ImmutableMap<String, String> params = ImmutableMap.copyOf(methodParamsMap);
ImmutableMap<String, RetryCodesDefinitionProto> codesProto = ImmutableMap.copyOf(codesDefMap);
ImmutableMap<String, RetryParamsDefinitionProto> paramsProto = ImmutableMap.copyOf(paramsDefMap);
return new AutoValue_GrpcGapicRetryMapping.Builder().setMethodCodesMap(codes).setMethodParamsMap(params).setCodesDefMap(codesProto).setParamsDefMap(paramsProto).build();
}
use of com.google.api.codegen.RetryParamsDefinitionProto in project toolkit by googleapis.
the class GoGapicSurfaceTransformer method generateRetryConfigDefinitions.
@VisibleForTesting
List<RetryConfigDefinitionView> generateRetryConfigDefinitions(InterfaceContext context, List<MethodModel> methods) {
Set<RetryConfigDefinitionView.Name> retryNames = new HashSet<>();
for (MethodModel method : methods) {
MethodConfig conf = context.getMethodConfig(method);
retryNames.add(RetryConfigDefinitionView.Name.create(conf.getRetrySettingsConfigName(), conf.getRetryCodesConfigName()));
}
TreeMap<RetryConfigDefinitionView.Name, RetryConfigDefinitionView> retryDef = new TreeMap<>();
Map<String, ImmutableList<String>> retryCodesDef = context.getInterfaceConfig().getRetryCodesConfig().getRetryCodesDefinition();
ImmutableMap<String, RetryParamsDefinitionProto> retryParamsDef = context.getInterfaceConfig().getRetrySettingsDefinition();
for (RetryConfigDefinitionView.Name name : retryNames) {
ImmutableList<String> codes = retryCodesDef.get(name.retryCodesConfigName());
if (codes.isEmpty()) {
continue;
}
List<String> retryCodeNames = new ArrayList<>();
for (String code : codes) {
retryCodeNames.add(context.getNamer().getStatusCodeName(code));
}
retryDef.put(name, RetryConfigDefinitionView.newBuilder().name(name).retryCodes(retryCodeNames).params(retryParamsDef.get(name.retrySettingsConfigName())).build());
}
if (!retryDef.isEmpty()) {
context.getImportTypeTable().saveNicknameFor("time;;;");
context.getImportTypeTable().saveNicknameFor("google.golang.org/grpc/codes;;;");
}
return new ArrayList<>(retryDef.values());
}
Aggregations