use of com.google.api.codegen.RetryCodesDefinitionProto in project toolkit by googleapis.
the class RetryDefinitionsTransformer method createRetryCodesDefinition.
public static ImmutableMap<String, ImmutableSet<String>> createRetryCodesDefinition(DiagCollector diagCollector, InterfaceConfigProto interfaceConfigProto) {
ImmutableMap.Builder<String, ImmutableSet<String>> builder = ImmutableMap.builder();
for (RetryCodesDefinitionProto retryDef : interfaceConfigProto.getRetryCodesDefList()) {
// Enforce ordering on set for baseline test consistency.
Set<String> codes = new TreeSet<>();
for (String codeText : retryDef.getRetryCodesList()) {
try {
codes.add(String.valueOf(codeText));
} catch (IllegalArgumentException e) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "status code not found: '%s' (in interface %s)", codeText, interfaceConfigProto.getName()));
}
}
builder.put(retryDef.getName(), (new ImmutableSet.Builder<String>()).addAll(codes).build());
}
if (diagCollector.getErrorCount() > 0) {
return null;
}
return builder.build();
}
use of com.google.api.codegen.RetryCodesDefinitionProto 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.RetryCodesDefinitionProto in project toolkit by googleapis.
the class RetryCodesConfig method createRetryCodesDefinitionFromConfigProto.
@Nullable
private static ImmutableMap<String, ImmutableList<String>> createRetryCodesDefinitionFromConfigProto(InterfaceConfigProto interfaceConfigProto) {
ImmutableMap.Builder<String, ImmutableList<String>> builder = ImmutableMap.builder();
for (RetryCodesDefinitionProto retryDef : interfaceConfigProto.getRetryCodesDefList()) {
// Enforce ordering on set for baseline test consistency.
Set<String> codes = new TreeSet<>(retryDef.getRetryCodesList());
builder.put(retryDef.getName(), ImmutableList.copyOf(codes));
}
return builder.build();
}
Aggregations