Search in sources :

Example 1 with RetryParamsDefinitionProto

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();
}
Also used : RetrySettings(com.google.api.gax.retrying.RetrySettings) RetryParamsDefinitionProto(com.google.api.codegen.RetryParamsDefinitionProto) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with RetryParamsDefinitionProto

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();
}
Also used : RetryParamsDefinitionProto(com.google.api.codegen.RetryParamsDefinitionProto) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with RetryParamsDefinitionProto

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();
}
Also used : ServiceConfig(com.google.api.codegen.grpc.ServiceConfig) ConfigProto(com.google.api.codegen.ConfigProto) ImmutableList(com.google.common.collect.ImmutableList) Model(com.google.api.tools.framework.model.Model) RetryParamsDefinitionProto(com.google.api.codegen.RetryParamsDefinitionProto) TestDataLocator(com.google.api.tools.framework.model.testing.TestDataLocator) MixedPathTestDataLocator(com.google.api.codegen.MixedPathTestDataLocator) Test(org.junit.Test)

Example 4 with RetryParamsDefinitionProto

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();
}
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 5 with RetryParamsDefinitionProto

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());
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) ImmutableList(com.google.common.collect.ImmutableList) RetryConfigDefinitionView(com.google.api.codegen.viewmodel.RetryConfigDefinitionView) ArrayList(java.util.ArrayList) RetryParamsDefinitionProto(com.google.api.codegen.RetryParamsDefinitionProto) TreeMap(java.util.TreeMap) MethodConfig(com.google.api.codegen.config.MethodConfig) HashSet(java.util.HashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

RetryParamsDefinitionProto (com.google.api.codegen.RetryParamsDefinitionProto)7 ImmutableMap (com.google.common.collect.ImmutableMap)3 CollectionConfigProto (com.google.api.codegen.CollectionConfigProto)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 ArrayList (java.util.ArrayList)2 ConfigProto (com.google.api.codegen.ConfigProto)1 DeprecatedCollectionConfigProto (com.google.api.codegen.DeprecatedCollectionConfigProto)1 InterfaceConfigProto (com.google.api.codegen.InterfaceConfigProto)1 MethodConfigProto (com.google.api.codegen.MethodConfigProto)1 MixedPathTestDataLocator (com.google.api.codegen.MixedPathTestDataLocator)1 RetryCodesDefinitionProto (com.google.api.codegen.RetryCodesDefinitionProto)1 MethodConfig (com.google.api.codegen.config.MethodConfig)1 MethodModel (com.google.api.codegen.config.MethodModel)1 Method (com.google.api.codegen.discovery.Method)1 Name (com.google.api.codegen.grpc.MethodConfig.Name)1 RetryPolicy (com.google.api.codegen.grpc.MethodConfig.RetryPolicy)1 ServiceConfig (com.google.api.codegen.grpc.ServiceConfig)1 RetryConfigDefinitionView (com.google.api.codegen.viewmodel.RetryConfigDefinitionView)1 RetrySettings (com.google.api.gax.retrying.RetrySettings)1