Search in sources :

Example 1 with GapicLroRetrySettings

use of com.google.api.generator.gapic.model.GapicLroRetrySettings in project gapic-generator-java by googleapis.

the class RetrySettingsComposer method createLroRetrySettingsExpr.

private static Expr createLroRetrySettingsExpr(Service service, Method method, GapicServiceConfig serviceConfig) {
    Expr lroRetrySettingsExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("RetrySettings")).setMethodName("newBuilder").build();
    long initialPollDelayMillis = LRO_DEFAULT_INITIAL_POLL_DELAY_MILLIS;
    double pollDelayMultiplier = LRO_DEFAULT_POLL_DELAY_MULTIPLIER;
    long maxPollDelayMillis = LRO_DEFAULT_MAX_POLL_DELAY_MILLIS;
    long totalPollTimeoutMillis = LRO_DEFAULT_TOTAL_POLL_TIMEOUT_MILLIS;
    if (serviceConfig.hasLroRetrySetting(service, method)) {
        Optional<GapicLroRetrySettings> lroRetrySettingsOpt = serviceConfig.getLroRetrySetting(service, method);
        if (lroRetrySettingsOpt.isPresent()) {
            GapicLroRetrySettings lroRetrySettings = lroRetrySettingsOpt.get();
            initialPollDelayMillis = lroRetrySettings.initialPollDelayMillis();
            pollDelayMultiplier = lroRetrySettings.pollDelayMultiplier();
            maxPollDelayMillis = lroRetrySettings.maxPollDelayMillis();
            totalPollTimeoutMillis = lroRetrySettings.totalPollTimeoutMillis();
        }
    }
    lroRetrySettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(lroRetrySettingsExpr).setMethodName("setInitialRetryDelay").setArguments(createDurationOfMillisExpr(toValExpr(initialPollDelayMillis))).build();
    lroRetrySettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(lroRetrySettingsExpr).setMethodName("setRetryDelayMultiplier").setArguments(toValExpr(pollDelayMultiplier)).build();
    lroRetrySettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(lroRetrySettingsExpr).setMethodName("setMaxRetryDelay").setArguments(createDurationOfMillisExpr(toValExpr(maxPollDelayMillis))).build();
    Expr zeroDurationExpr = EnumRefExpr.builder().setType(FIXED_TYPESTORE.get("Duration")).setName("ZERO").build();
    // TODO(miraleung): Add an "// ignored" comment here.
    lroRetrySettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(lroRetrySettingsExpr).setMethodName("setInitialRpcTimeout").setArguments(zeroDurationExpr).build();
    // TODO(miraleung): Add an "// ignored" comment here.
    lroRetrySettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(lroRetrySettingsExpr).setMethodName("setRpcTimeoutMultiplier").setArguments(toValExpr(LRO_DEFAULT_MAX_RPC_TIMEOUT)).build();
    // TODO(miraleung): Add an "// ignored" comment here.
    lroRetrySettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(lroRetrySettingsExpr).setMethodName("setMaxRpcTimeout").setArguments(zeroDurationExpr).build();
    lroRetrySettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(lroRetrySettingsExpr).setMethodName("setTotalTimeout").setArguments(createDurationOfMillisExpr(toValExpr(totalPollTimeoutMillis))).build();
    lroRetrySettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(lroRetrySettingsExpr).setMethodName("build").build();
    return lroRetrySettingsExpr;
}
Also used : EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) GapicLroRetrySettings(com.google.api.generator.gapic.model.GapicLroRetrySettings)

Example 2 with GapicLroRetrySettings

use of com.google.api.generator.gapic.model.GapicLroRetrySettings in project gapic-generator-java by googleapis.

the class GapicLroRetrySettingsParserTest method parseLroRetrySettings_lroRetrySettingsPresent.

@Test
public void parseLroRetrySettings_lroRetrySettingsPresent() {
    String filename = "dataproc_gapic.yaml";
    Path path = Paths.get(YAML_DIRECTORY, filename);
    Optional<List<GapicLroRetrySettings>> settingsOpt = GapicLroRetrySettingsParser.parse(path.toString());
    List<GapicLroRetrySettings> lroRetrySettings = settingsOpt.get();
    assertEquals(6, lroRetrySettings.size());
    // This LRO setting has a max_poll_delay_millis that will be parsed as an integer.
    GapicLroRetrySettings setting = lroRetrySettings.get(0);
    assertEquals("google.cloud.dataproc.v1", setting.protoPakkage());
    assertEquals("ClusterController", setting.serviceName());
    assertEquals("CreateCluster", setting.methodName());
    assertEquals(1000, setting.initialPollDelayMillis());
    assertEquals(2, setting.pollDelayMultiplier(), DELTA);
    assertEquals(10000, setting.maxPollDelayMillis());
    assertEquals(900000, setting.totalPollTimeoutMillis());
    // Sanity-check on a different service and method.
    // The list element order is coupled to the definition order in the gapic.yaml file.
    // This LRO setting has a max_poll_delay_millis that will be parsed as a double.
    setting = lroRetrySettings.get(4);
    assertEquals("google.cloud.dataproc.v1", setting.protoPakkage());
    assertEquals("WorkflowTemplateService", setting.serviceName());
    assertEquals("InstantiateWorkflowTemplate", setting.methodName());
    assertEquals(1000, setting.initialPollDelayMillis());
    assertEquals(2.5, setting.pollDelayMultiplier(), DELTA);
    assertEquals(10000, setting.maxPollDelayMillis());
    assertEquals(43200000, setting.totalPollTimeoutMillis());
}
Also used : Path(java.nio.file.Path) GapicLroRetrySettings(com.google.api.generator.gapic.model.GapicLroRetrySettings) List(java.util.List) Test(org.junit.Test)

Example 3 with GapicLroRetrySettings

use of com.google.api.generator.gapic.model.GapicLroRetrySettings in project gapic-generator-java by googleapis.

the class Parser method parse.

public static GapicContext parse(CodeGeneratorRequest request) {
    Optional<String> gapicYamlConfigPathOpt = PluginArgumentParser.parseGapicYamlConfigPath(request);
    Optional<List<GapicBatchingSettings>> batchingSettingsOpt = BatchingSettingsConfigParser.parse(gapicYamlConfigPathOpt);
    Optional<List<GapicLroRetrySettings>> lroRetrySettingsOpt = GapicLroRetrySettingsParser.parse(gapicYamlConfigPathOpt);
    Optional<GapicLanguageSettings> languageSettingsOpt = GapicLanguageSettingsParser.parse(gapicYamlConfigPathOpt);
    Optional<String> transportOpt = PluginArgumentParser.parseTransport(request);
    boolean willGenerateMetadata = PluginArgumentParser.hasMetadataFlag(request);
    Optional<String> serviceConfigPathOpt = PluginArgumentParser.parseJsonConfigPath(request);
    Optional<GapicServiceConfig> serviceConfigOpt = ServiceConfigParser.parse(serviceConfigPathOpt.orElse(null));
    if (serviceConfigOpt.isPresent()) {
        GapicServiceConfig serviceConfig = serviceConfigOpt.get();
        serviceConfig.setLroRetrySettings(lroRetrySettingsOpt);
        serviceConfig.setBatchingSettings(batchingSettingsOpt);
        serviceConfig.setLanguageSettings(languageSettingsOpt);
        serviceConfigOpt = Optional.of(serviceConfig);
    }
    Optional<String> serviceYamlConfigPathOpt = PluginArgumentParser.parseServiceYamlConfigPath(request);
    Optional<com.google.api.Service> serviceYamlProtoOpt = serviceYamlConfigPathOpt.flatMap(ServiceYamlParser::parse);
    // Collect the resource references seen in messages.
    Set<ResourceReference> outputResourceReferencesSeen = new HashSet<>();
    // Keep message and resource name parsing separate for cleaner logic.
    // While this takes an extra pass through the protobufs, the extra time is relatively trivial
    // and is worth the larger reduced maintenance cost.
    Map<String, Message> messages = parseMessages(request, outputResourceReferencesSeen);
    Map<String, ResourceName> resourceNames = parseResourceNames(request);
    messages = updateResourceNamesInMessages(messages, resourceNames.values());
    // Contains only resource names that are actually used. Usage refers to the presence of a
    // request message's field in an RPC's method_signature annotation. That is,  resource name
    // definitions
    // or references that are simply defined, but not used in such a manner, will not have
    // corresponding Java helper
    // classes generated.
    Set<ResourceName> outputArgResourceNames = new HashSet<>();
    List<Service> mixinServices = new ArrayList<>();
    Transport transport = Transport.parse(transportOpt.orElse(Transport.GRPC.toString()));
    List<Service> services = parseServices(request, messages, resourceNames, outputArgResourceNames, serviceYamlProtoOpt, serviceConfigOpt, mixinServices, transport);
    Preconditions.checkState(!services.isEmpty(), "No services found to generate");
    // Temporary workaround for Ads, who still need these resource names.
    if (services.get(0).protoPakkage().startsWith("google.ads.googleads.v")) {
        Function<ResourceName, String> typeNameFn = r -> r.resourceTypeString().substring(r.resourceTypeString().indexOf("/") + 1);
        Function<Set<ResourceName>, Set<String>> typeStringSetFn = sr -> sr.stream().map(typeNameFn).collect(Collectors.toSet());
        // Include all resource names present in message types for backwards-compatibility with the
        // monolith. In the future, this should be removed on a client library major semver update.
        // Resolve type name collisions with the ones present in the method arguments.
        final Set<String> typeStringSet = typeStringSetFn.apply(outputArgResourceNames);
        outputArgResourceNames.addAll(resourceNames.values().stream().filter(r -> r.hasParentMessageName() && !typeStringSet.contains(typeNameFn.apply(r))).collect(Collectors.toSet()));
        String servicePackage = services.get(0).pakkage();
        Map<String, ResourceName> patternsToResourceNames = ResourceParserHelpers.createPatternResourceNameMap(resourceNames);
        for (ResourceReference resourceReference : outputResourceReferencesSeen) {
            final Set<String> interimTypeStringSet = typeStringSetFn.apply(outputArgResourceNames);
            outputArgResourceNames.addAll(ResourceReferenceParser.parseResourceNames(resourceReference, servicePackage, null, resourceNames, patternsToResourceNames).stream().filter(r -> !interimTypeStringSet.contains(typeNameFn.apply(r))).collect(Collectors.toSet()));
        }
    }
    return GapicContext.builder().setServices(services).setMixinServices(mixinServices).setMessages(messages).setResourceNames(resourceNames).setHelperResourceNames(outputArgResourceNames).setServiceConfig(serviceConfigOpt.orElse(null)).setGapicMetadataEnabled(willGenerateMetadata).setServiceYamlProto(serviceYamlProtoOpt.orElse(null)).setTransport(transport).build();
}
Also used : HttpBindings(com.google.api.generator.gapic.model.HttpBindings) CodeGeneratorRequest(com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest) Arrays(java.util.Arrays) RoutingHeaderRule(com.google.api.generator.gapic.model.RoutingHeaderRule) OperationInfo(com.google.longrunning.OperationInfo) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) Field(com.google.api.generator.gapic.model.Field) Method(com.google.api.generator.gapic.model.Method) HttpRule(com.google.api.HttpRule) LongrunningOperation(com.google.api.generator.gapic.model.LongrunningOperation) Map(java.util.Map) ResourceProto(com.google.api.ResourceProto) FieldOptions(com.google.protobuf.DescriptorProtos.FieldOptions) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) BiMap(com.google.common.collect.BiMap) ResourceDescriptor(com.google.api.ResourceDescriptor) ImmutableSet(com.google.common.collect.ImmutableSet) ResourceReference(com.google.api.generator.gapic.model.ResourceReference) Collection(java.util.Collection) Set(java.util.Set) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) OperationsProto(com.google.longrunning.OperationsProto) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ResourceNameConstants(com.google.api.generator.gapic.utils.ResourceNameConstants) List(java.util.List) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) DocumentationRule(com.google.api.DocumentationRule) Optional(java.util.Optional) Transport(com.google.api.generator.gapic.model.Transport) GapicContext(com.google.api.generator.gapic.model.GapicContext) GapicLanguageSettings(com.google.api.generator.gapic.model.GapicLanguageSettings) IntStream(java.util.stream.IntStream) VaporReference(com.google.api.generator.engine.ast.VaporReference) TypeNode(com.google.api.generator.engine.ast.TypeNode) GapicLroRetrySettings(com.google.api.generator.gapic.model.GapicLroRetrySettings) Descriptor(com.google.protobuf.Descriptors.Descriptor) HashMap(java.util.HashMap) Function(java.util.function.Function) OperationResponseMapping(com.google.cloud.OperationResponseMapping) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) MethodDescriptor(com.google.protobuf.Descriptors.MethodDescriptor) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings) Maps(com.google.common.collect.Maps) ResourceName(com.google.api.generator.gapic.model.ResourceName) Service(com.google.api.generator.gapic.model.Service) HashBiMap(com.google.common.collect.HashBiMap) ServiceOptions(com.google.protobuf.DescriptorProtos.ServiceOptions) ExtendedOperationsProto(com.google.cloud.ExtendedOperationsProto) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) Preconditions(com.google.common.base.Preconditions) ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) OperationResponse(com.google.api.generator.gapic.model.OperationResponse) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Message(com.google.api.generator.gapic.model.Message) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) Collections(java.util.Collections) ClientProto(com.google.api.ClientProto) MessageOptions(com.google.protobuf.DescriptorProtos.MessageOptions) SourceCodeInfoLocation(com.google.api.generator.gapic.model.SourceCodeInfoLocation) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) Message(com.google.api.generator.gapic.model.Message) ResourceName(com.google.api.generator.gapic.model.ResourceName) ArrayList(java.util.ArrayList) GapicLanguageSettings(com.google.api.generator.gapic.model.GapicLanguageSettings) List(java.util.List) ArrayList(java.util.ArrayList) ResourceReference(com.google.api.generator.gapic.model.ResourceReference) HashSet(java.util.HashSet) Service(com.google.api.generator.gapic.model.Service) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) Transport(com.google.api.generator.gapic.model.Transport)

Aggregations

GapicLroRetrySettings (com.google.api.generator.gapic.model.GapicLroRetrySettings)3 ClientProto (com.google.api.ClientProto)1 DocumentationRule (com.google.api.DocumentationRule)1 HttpRule (com.google.api.HttpRule)1 ResourceDescriptor (com.google.api.ResourceDescriptor)1 ResourceProto (com.google.api.ResourceProto)1 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)1 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)1 Expr (com.google.api.generator.engine.ast.Expr)1 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)1 TypeNode (com.google.api.generator.engine.ast.TypeNode)1 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)1 VaporReference (com.google.api.generator.engine.ast.VaporReference)1 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)1 Field (com.google.api.generator.gapic.model.Field)1 GapicBatchingSettings (com.google.api.generator.gapic.model.GapicBatchingSettings)1 GapicContext (com.google.api.generator.gapic.model.GapicContext)1 GapicLanguageSettings (com.google.api.generator.gapic.model.GapicLanguageSettings)1 GapicServiceConfig (com.google.api.generator.gapic.model.GapicServiceConfig)1 HttpBindings (com.google.api.generator.gapic.model.HttpBindings)1