Search in sources :

Example 1 with RetryCodesDefinitionProto

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();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) TreeSet(java.util.TreeSet) RetryCodesDefinitionProto(com.google.api.codegen.RetryCodesDefinitionProto) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with RetryCodesDefinitionProto

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();
}
Also used : HashMap(java.util.HashMap) RetryParamsDefinitionProto(com.google.api.codegen.RetryParamsDefinitionProto) Name(com.google.api.codegen.grpc.MethodConfig.Name) RetryCodesDefinitionProto(com.google.api.codegen.RetryCodesDefinitionProto) RetryPolicy(com.google.api.codegen.grpc.MethodConfig.RetryPolicy)

Example 3 with RetryCodesDefinitionProto

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();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) TreeSet(java.util.TreeSet) RetryCodesDefinitionProto(com.google.api.codegen.RetryCodesDefinitionProto) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(javax.annotation.Nullable)

Aggregations

RetryCodesDefinitionProto (com.google.api.codegen.RetryCodesDefinitionProto)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 TreeSet (java.util.TreeSet)2 RetryParamsDefinitionProto (com.google.api.codegen.RetryParamsDefinitionProto)1 Name (com.google.api.codegen.grpc.MethodConfig.Name)1 RetryPolicy (com.google.api.codegen.grpc.MethodConfig.RetryPolicy)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 HashMap (java.util.HashMap)1 Nullable (javax.annotation.Nullable)1