Search in sources :

Example 1 with GapicBatchingSettings

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

the class AbstractServiceStubSettingsClassComposer method createNestedClassInitDefaultsMethod.

private MethodDefinition createNestedClassInitDefaultsMethod(Service service, @Nullable GapicServiceConfig serviceConfig, TypeStore typeStore) {
    // TODO(miraleung): Robustify this against a null serviceConfig.
    TypeNode builderType = typeStore.get(NESTED_BUILDER_CLASS_NAME);
    VariableExpr builderVarExpr = VariableExpr.withVariable(Variable.builder().setType(builderType).setName("builder").build());
    List<Statement> bodyStatements = new ArrayList<>();
    // Iterate through methods twice to so we can have LRO expressions appear last.
    for (Method method : service.methods()) {
        Method.Stream streamKind = method.stream();
        if (streamKind.equals(Method.Stream.CLIENT) || streamKind.equals(Method.Stream.BIDI)) {
            continue;
        }
        if (!Objects.isNull(serviceConfig) && serviceConfig.hasBatchingSetting(service, method)) {
            Optional<GapicBatchingSettings> batchingSettingOpt = serviceConfig.getBatchingSetting(service, method);
            Preconditions.checkState(batchingSettingOpt.isPresent(), String.format("No batching setting found for service %s, method %s", service.name(), method.name()));
            String settingsGetterMethodName = String.format("%sSettings", JavaStyle.toLowerCamelCase(method.name()));
            bodyStatements.add(ExprStatement.withExpr(RetrySettingsComposer.createBatchingBuilderSettingsExpr(settingsGetterMethodName, batchingSettingOpt.get(), builderVarExpr)));
            bodyStatements.add(EMPTY_LINE_STATEMENT);
        }
        bodyStatements.add(ExprStatement.withExpr(RetrySettingsComposer.createSimpleBuilderSettingsExpr(service, serviceConfig, method, builderVarExpr, NESTED_RETRYABLE_CODE_DEFINITIONS_VAR_EXPR, NESTED_RETRY_PARAM_DEFINITIONS_VAR_EXPR)));
        bodyStatements.add(EMPTY_LINE_STATEMENT);
    }
    for (Method method : service.methods()) {
        if (!method.hasLro()) {
            continue;
        }
        bodyStatements.add(ExprStatement.withExpr(RetrySettingsComposer.createLroSettingsBuilderExpr(service, serviceConfig, method, builderVarExpr, NESTED_RETRYABLE_CODE_DEFINITIONS_VAR_EXPR, NESTED_RETRY_PARAM_DEFINITIONS_VAR_EXPR, getTransportContext().operationResponseTransformerType(), getTransportContext().operationMetadataTransformerType())));
        bodyStatements.add(EMPTY_LINE_STATEMENT);
    }
    return MethodDefinition.builder().setScope(ScopeNode.PRIVATE).setIsStatic(true).setReturnType(builderType).setName("initDefaults").setArguments(builderVarExpr.toBuilder().setIsDecl(true).build()).setBody(bodyStatements).setReturnExpr(builderVarExpr).build();
}
Also used : EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Method(com.google.api.generator.gapic.model.Method) Stream(com.google.api.generator.gapic.model.Method.Stream) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings)

Example 2 with GapicBatchingSettings

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

the class BatchingSettingsConfigParserTest method parseBatchingSettings_pubsub.

@Test
public void parseBatchingSettings_pubsub() {
    String filename = "pubsub_gapic.yaml";
    Path path = Paths.get(YAML_DIRECTORY, filename);
    Optional<List<GapicBatchingSettings>> settingsOpt = BatchingSettingsConfigParser.parse(path.toString());
    assertTrue(settingsOpt.isPresent());
    List<GapicBatchingSettings> batchingSettings = settingsOpt.get();
    assertEquals(1, batchingSettings.size());
    GapicBatchingSettings setting = batchingSettings.get(0);
    assertEquals("google.pubsub.v1", setting.protoPakkage());
    assertEquals("Publisher", setting.serviceName());
    assertEquals("Publish", setting.methodName());
    assertEquals(100, setting.elementCountThreshold());
    assertEquals(1048576, setting.requestByteThreshold());
    assertEquals(10, setting.delayThresholdMillis());
    assertThat(setting.flowControlElementLimit()).isNull();
    assertThat(setting.flowControlByteLimit()).isNull();
    assertEquals(GapicBatchingSettings.FlowControlLimitExceededBehavior.IGNORE, setting.flowControlLimitExceededBehavior());
    assertEquals("messages", setting.batchedFieldName());
    assertThat(setting.discriminatorFieldNames()).containsExactly("topic");
    assertEquals("message_ids", setting.subresponseFieldName());
}
Also used : Path(java.nio.file.Path) List(java.util.List) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings) Test(org.junit.Test)

Example 3 with GapicBatchingSettings

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

the class BatchingDescriptorComposerTest method batchingDescriptor_hasSubresponseField.

@Test
public void batchingDescriptor_hasSubresponseField() {
    FileDescriptor serviceFileDescriptor = PubsubProto.getDescriptor();
    FileDescriptor commonResourcesFileDescriptor = CommonResources.getDescriptor();
    ServiceDescriptor serviceDescriptor = serviceFileDescriptor.getServices().get(0);
    assertEquals("Publisher", serviceDescriptor.getName());
    Map<String, ResourceName> resourceNames = new HashMap<>();
    resourceNames.putAll(Parser.parseResourceNames(serviceFileDescriptor));
    resourceNames.putAll(Parser.parseResourceNames(commonResourcesFileDescriptor));
    Map<String, Message> messageTypes = Parser.parseMessages(serviceFileDescriptor);
    Set<ResourceName> outputResourceNames = new HashSet<>();
    List<Service> services = Parser.parseService(serviceFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
    String filename = "pubsub_gapic.yaml";
    Path path = Paths.get(TestProtoLoader.instance().getTestFilesDirectory(), filename);
    Optional<List<GapicBatchingSettings>> batchingSettingsOpt = BatchingSettingsConfigParser.parse(Optional.of(path.toString()));
    assertTrue(batchingSettingsOpt.isPresent());
    String jsonFilename = "pubsub_grpc_service_config.json";
    Path jsonPath = Paths.get(TestProtoLoader.instance().getTestFilesDirectory(), jsonFilename);
    Optional<GapicServiceConfig> configOpt = ServiceConfigParser.parse(jsonPath.toString());
    assertTrue(configOpt.isPresent());
    GapicServiceConfig config = configOpt.get();
    config.setBatchingSettings(batchingSettingsOpt);
    Service service = services.get(0);
    assertEquals("Publisher", service.name());
    Method method = findMethod(service, "Publish");
    GapicBatchingSettings batchingSetting = batchingSettingsOpt.get().get(0);
    assertEquals("Publish", batchingSetting.methodName());
    Expr batchingDescriptorExpr = BatchingDescriptorComposer.createBatchingDescriptorFieldDeclExpr(method, batchingSetting, messageTypes);
    batchingDescriptorExpr.accept(writerVisitor);
    Utils.saveCodegenToFile(this.getClass(), "BatchingDescriptorComposerTestSubresponse.golden", writerVisitor.write());
    Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "BatchingDescriptorComposerTestSubresponse.golden");
    Assert.assertCodeEquals(goldenFilePath, writerVisitor.write());
}
Also used : Path(java.nio.file.Path) Message(com.google.api.generator.gapic.model.Message) HashMap(java.util.HashMap) ResourceName(com.google.api.generator.gapic.model.ResourceName) Service(com.google.api.generator.gapic.model.Service) Method(com.google.api.generator.gapic.model.Method) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Expr(com.google.api.generator.engine.ast.Expr) ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) List(java.util.List) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with GapicBatchingSettings

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

the class BatchingDescriptorComposerTest method batchingDescriptor_noSubresponseField.

@Test
public void batchingDescriptor_noSubresponseField() {
    FileDescriptor serviceFileDescriptor = LoggingProto.getDescriptor();
    ServiceDescriptor serviceDescriptor = serviceFileDescriptor.getServices().get(0);
    assertEquals(serviceDescriptor.getName(), "LoggingServiceV2");
    List<FileDescriptor> protoFiles = Arrays.asList(serviceFileDescriptor, LogEntryProto.getDescriptor(), LoggingConfigProto.getDescriptor(), LoggingMetricsProto.getDescriptor());
    Map<String, ResourceName> resourceNames = new HashMap<>();
    Map<String, Message> messageTypes = new HashMap<>();
    for (FileDescriptor fileDescriptor : protoFiles) {
        resourceNames.putAll(Parser.parseResourceNames(fileDescriptor));
        messageTypes.putAll(Parser.parseMessages(fileDescriptor));
    }
    Set<ResourceName> outputResourceNames = new HashSet<>();
    List<Service> services = Parser.parseService(serviceFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
    String filename = "logging_gapic.yaml";
    Path path = Paths.get(TestProtoLoader.instance().getTestFilesDirectory(), filename);
    Optional<List<GapicBatchingSettings>> batchingSettingsOpt = BatchingSettingsConfigParser.parse(Optional.of(path.toString()));
    assertTrue(batchingSettingsOpt.isPresent());
    String jsonFilename = "logging_grpc_service_config.json";
    Path jsonPath = Paths.get(TestProtoLoader.instance().getTestFilesDirectory(), jsonFilename);
    Optional<GapicServiceConfig> configOpt = ServiceConfigParser.parse(jsonPath.toString());
    assertTrue(configOpt.isPresent());
    GapicServiceConfig config = configOpt.get();
    config.setBatchingSettings(batchingSettingsOpt);
    Service service = services.get(0);
    assertEquals("LoggingServiceV2", service.name());
    Method method = findMethod(service, "WriteLogEntries");
    GapicBatchingSettings batchingSetting = batchingSettingsOpt.get().get(0);
    assertEquals("WriteLogEntries", batchingSetting.methodName());
    Expr batchingDescriptorExpr = BatchingDescriptorComposer.createBatchingDescriptorFieldDeclExpr(method, batchingSetting, messageTypes);
    batchingDescriptorExpr.accept(writerVisitor);
    Utils.saveCodegenToFile(this.getClass(), "BatchingDescriptorComposerTestNoSubresponse.golden", writerVisitor.write());
    Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "BatchingDescriptorComposerTestNoSubresponse.golden");
    Assert.assertCodeEquals(goldenFilePath, writerVisitor.write());
}
Also used : Path(java.nio.file.Path) Message(com.google.api.generator.gapic.model.Message) HashMap(java.util.HashMap) ResourceName(com.google.api.generator.gapic.model.ResourceName) Service(com.google.api.generator.gapic.model.Service) Method(com.google.api.generator.gapic.model.Method) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Expr(com.google.api.generator.engine.ast.Expr) ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) List(java.util.List) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with GapicBatchingSettings

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

the class RetrySettingsComposerTest method batchingSettings_fullFlowControlSettings.

@Test
public void batchingSettings_fullFlowControlSettings() {
    String filename = "logging_gapic.yaml";
    Path path = Paths.get(TestProtoLoader.instance().getTestFilesDirectory(), filename);
    Optional<List<GapicBatchingSettings>> batchingSettingsOpt = BatchingSettingsConfigParser.parse(Optional.of(path.toString()));
    assertTrue(batchingSettingsOpt.isPresent());
    String jsonFilename = "logging_grpc_service_config.json";
    Path jsonPath = Paths.get(TestProtoLoader.instance().getTestFilesDirectory(), jsonFilename);
    Optional<GapicServiceConfig> configOpt = ServiceConfigParser.parse(jsonPath.toString());
    assertTrue(configOpt.isPresent());
    GapicServiceConfig config = configOpt.get();
    config.setBatchingSettings(batchingSettingsOpt);
    GapicContext context = TestProtoLoader.instance().parseLogging();
    Service service = context.services().get(0);
    assertEquals("LoggingServiceV2", service.name());
    VariableExpr builderVarExpr = createBuilderVarExpr(service);
    String methodSettingsName = "writeLogEntriesSettings";
    GapicBatchingSettings batchingSettings = GapicBatchingSettings.builder().setProtoPakkage("com.google.logging.v2").setServiceName("LoggingServiceV2").setMethodName("WriteLogEntries").setElementCountThreshold(1000).setRequestByteThreshold(1048576).setDelayThresholdMillis(50).setFlowControlElementLimit(100000).setFlowControlByteLimit(10485760).setFlowControlLimitExceededBehavior(GapicBatchingSettings.FlowControlLimitExceededBehavior.THROW_EXCEPTION).setBatchedFieldName("entries").setDiscriminatorFieldNames(Arrays.asList("log_name", "resource", "labels")).build();
    Expr builderExpr = RetrySettingsComposer.createBatchingBuilderSettingsExpr(methodSettingsName, batchingSettings, builderVarExpr);
    builderExpr.accept(writerVisitor);
    String expected = "builder" + ".writeLogEntriesSettings()" + ".setBatchingSettings(" + "BatchingSettings.newBuilder()" + ".setElementCountThreshold(1000L)" + ".setRequestByteThreshold(1048576L)" + ".setDelayThreshold(Duration.ofMillis(50L))" + ".setFlowControlSettings(" + "FlowControlSettings.newBuilder()" + ".setMaxOutstandingElementCount(100000L)" + ".setMaxOutstandingRequestBytes(10485760L)" + ".setLimitExceededBehavior(FlowController.LimitExceededBehavior.ThrowException)" + ".build())" + ".build())";
    assertEquals(expected, writerVisitor.write());
}
Also used : Path(java.nio.file.Path) Expr(com.google.api.generator.engine.ast.Expr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Service(com.google.api.generator.gapic.model.Service) List(java.util.List) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) GapicContext(com.google.api.generator.gapic.model.GapicContext) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings) Test(org.junit.Test)

Aggregations

GapicBatchingSettings (com.google.api.generator.gapic.model.GapicBatchingSettings)9 List (java.util.List)8 GapicServiceConfig (com.google.api.generator.gapic.model.GapicServiceConfig)6 Service (com.google.api.generator.gapic.model.Service)6 Path (java.nio.file.Path)6 Test (org.junit.Test)6 Expr (com.google.api.generator.engine.ast.Expr)5 Method (com.google.api.generator.gapic.model.Method)5 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)4 GapicContext (com.google.api.generator.gapic.model.GapicContext)4 Message (com.google.api.generator.gapic.model.Message)4 HashSet (java.util.HashSet)4 TypeNode (com.google.api.generator.engine.ast.TypeNode)3 ArrayList (java.util.ArrayList)3 VaporReference (com.google.api.generator.engine.ast.VaporReference)2 Field (com.google.api.generator.gapic.model.Field)2 ResourceName (com.google.api.generator.gapic.model.ResourceName)2 Preconditions (com.google.common.base.Preconditions)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)2