From 596a6657dcb0d1e20044128ad991805f04586276 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Fri, 20 Nov 2020 16:58:26 -0800 Subject: [PATCH] fix: bring back gapic.yaml to bazel and Java parsing --- rules_java_gapic/java_gapic.bzl | 4 + run.sh | 9 +- test/integration/BUILD.bazel | 1 + .../logging/LoggingServiceV2StubSettings.java | 97 ++++++++++++++++++- 4 files changed, 105 insertions(+), 6 deletions(-) diff --git a/rules_java_gapic/java_gapic.bzl b/rules_java_gapic/java_gapic.bzl index 26d87cacaf..4a004b57fa 100644 --- a/rules_java_gapic/java_gapic.bzl +++ b/rules_java_gapic/java_gapic.bzl @@ -85,6 +85,7 @@ def java_gapic_library( package = None, service_yaml = None, grpc_service_config = None, + gapic_yaml = None, deps = [], test_deps = [], **kwargs): @@ -93,6 +94,9 @@ def java_gapic_library( if grpc_service_config: file_args_dict[grpc_service_config] = "grpc-service-config" + if gapic_yaml: + file_args_dict[gapic_yaml] = "gapic-config" + # Check the allow-list. if service_yaml: service_yaml_in_allowlist = False diff --git a/run.sh b/run.sh index efe66747a0..5581e0f378 100755 --- a/run.sh +++ b/run.sh @@ -18,6 +18,7 @@ DEFINE_string --alias=s service_config '' 'Path to the JSON service config' # Optional flags. DEFINE_bool --alias=c use_cached false 'If true, does not rebuild the plugin.' DEFINE_string --alias=o out '/tmp/test' 'Output directory' +DEFINE_string gapic_config '' 'Path to the config ending in gapic.yaml. Optional' gbash::init_google "$@" @@ -78,13 +79,19 @@ then SERVICE_CONFIG_OPT="grpc-service-config=$FLAGS_service_config" fi +GAPIC_CONFIG_OPT="" +if [ -n "$FLAGS_gapic_config" ] +then + GAPIC_CONFIG_OPT="gapic-config=$FLAGS_gapic_config" +fi + # Run protoc. protoc -I="${PROTOC_INCLUDE_DIR}" -I="${FLAGS_googleapis}" -I="${FLAGS_protos}" \ -I="${FLAGS_googleapis}/google/longrunning" \ --include_source_info \ --plugin=bazel-bin/protoc-gen-java_gapic ${FLAGS_protos}/*.proto \ --java_gapic_out="${FLAGS_out}" \ - --java_gapic_opt="${SERVICE_CONFIG_OPT}" \ + --java_gapic_opt="${SERVICE_CONFIG_OPT},${GAPIC_CONFIG_OPT}" \ --experimental_allow_proto3_optional echo_success "Output files written to ${FLAGS_out}" diff --git a/test/integration/BUILD.bazel b/test/integration/BUILD.bazel index d4760f7941..162b68f2aa 100644 --- a/test/integration/BUILD.bazel +++ b/test/integration/BUILD.bazel @@ -115,6 +115,7 @@ java_gapic_assembly_gradle_pkg( java_gapic_library( name = "logging_java_gapic", srcs = ["@com_google_googleapis//google/logging/v2:logging_proto_with_info"], + gapic_yaml = "@com_google_googleapis//google/logging/v2:logging_gapic.yaml", grpc_service_config = "@com_google_googleapis//google/logging/v2:logging_grpc_service_config.json", package = "google.logging.v2", test_deps = [ diff --git a/test/integration/goldens/logging/LoggingServiceV2StubSettings.java b/test/integration/goldens/logging/LoggingServiceV2StubSettings.java index ae0e186fd6..62289f4be5 100644 --- a/test/integration/goldens/logging/LoggingServiceV2StubSettings.java +++ b/test/integration/goldens/logging/LoggingServiceV2StubSettings.java @@ -24,6 +24,11 @@ import com.google.api.core.ApiFunction; import com.google.api.core.ApiFuture; import com.google.api.core.BetaApi; +import com.google.api.gax.batching.BatchingSettings; +import com.google.api.gax.batching.FlowControlSettings; +import com.google.api.gax.batching.FlowController; +import com.google.api.gax.batching.PartitionKey; +import com.google.api.gax.batching.RequestBuilder; import com.google.api.gax.core.GaxProperties; import com.google.api.gax.core.GoogleCredentialsProvider; import com.google.api.gax.core.InstantiatingExecutorProvider; @@ -33,6 +38,9 @@ import com.google.api.gax.retrying.RetrySettings; import com.google.api.gax.rpc.ApiCallContext; import com.google.api.gax.rpc.ApiClientHeaderProvider; +import com.google.api.gax.rpc.BatchedRequestIssuer; +import com.google.api.gax.rpc.BatchingCallSettings; +import com.google.api.gax.rpc.BatchingDescriptor; import com.google.api.gax.rpc.ClientContext; import com.google.api.gax.rpc.PageContext; import com.google.api.gax.rpc.PagedCallSettings; @@ -59,6 +67,7 @@ import com.google.logging.v2.WriteLogEntriesResponse; import com.google.protobuf.Empty; import java.io.IOException; +import java.util.Collection; import java.util.List; import java.util.Objects; import javax.annotation.Generated; @@ -110,7 +119,7 @@ public class LoggingServiceV2StubSettings extends StubSettings deleteLogSettings; - private final UnaryCallSettings + private final BatchingCallSettings writeLogEntriesSettings; private final PagedCallSettings< ListLogEntriesRequest, ListLogEntriesResponse, ListLogEntriesPagedResponse> @@ -311,13 +320,73 @@ public ApiFuture getFuturePagedResponse( } }; + private static final BatchingDescriptor + WRITE_LOG_ENTRIES_BATCHING_DESC = + new BatchingDescriptor() { + @Override + public PartitionKey getBatchPartitionKey(WriteLogEntriesRequest request) { + return new PartitionKey( + request.getLogName(), request.getResource(), request.getLabels()); + } + + @Override + public RequestBuilder getRequestBuilder() { + return new RequestBuilder() { + private WriteLogEntriesRequest.Builder builder; + + @Override + public void appendRequest(WriteLogEntriesRequest request) { + if (Objects.isNull(builder)) { + builder = request.toBuilder(); + } else { + builder.addAllEntries(request.getEntriesList()); + } + } + + @Override + public WriteLogEntriesRequest build() { + return builder.build(); + } + }; + } + + @Override + public void splitResponse( + WriteLogEntriesResponse batchResponse, + Collection> batch) { + for (BatchedRequestIssuer responder : batch) { + WriteLogEntriesResponse response = WriteLogEntriesResponse.newBuilder().build(); + responder.setResponse(response); + } + } + + @Override + public void splitException( + Throwable throwable, + Collection> batch) { + for (BatchedRequestIssuer responder : batch) { + responder.setException(throwable); + } + } + + @Override + public long countElements(WriteLogEntriesRequest request) { + return request.getEntriesCount(); + } + + @Override + public long countBytes(WriteLogEntriesRequest request) { + return request.getSerializedSize(); + } + }; + /** Returns the object with the settings used for calls to deleteLog. */ public UnaryCallSettings deleteLogSettings() { return deleteLogSettings; } /** Returns the object with the settings used for calls to writeLogEntries. */ - public UnaryCallSettings + public BatchingCallSettings writeLogEntriesSettings() { return writeLogEntriesSettings; } @@ -425,7 +494,7 @@ protected LoggingServiceV2StubSettings(Builder settingsBuilder) throws IOExcepti public static class Builder extends StubSettings.Builder { private final ImmutableList> unaryMethodSettingsBuilders; private final UnaryCallSettings.Builder deleteLogSettings; - private final UnaryCallSettings.Builder + private final BatchingCallSettings.Builder writeLogEntriesSettings; private final PagedCallSettings.Builder< ListLogEntriesRequest, ListLogEntriesResponse, ListLogEntriesPagedResponse> @@ -481,7 +550,9 @@ protected Builder(ClientContext clientContext) { super(clientContext); deleteLogSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); - writeLogEntriesSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + writeLogEntriesSettings = + BatchingCallSettings.newBuilder(WRITE_LOG_ENTRIES_BATCHING_DESC) + .setBatchingSettings(BatchingSettings.newBuilder().build()); listLogEntriesSettings = PagedCallSettings.newBuilder(LIST_LOG_ENTRIES_PAGE_STR_FACT); listMonitoredResourceDescriptorsSettings = PagedCallSettings.newBuilder(LIST_MONITORED_RESOURCE_DESCRIPTORS_PAGE_STR_FACT); @@ -533,6 +604,22 @@ private static Builder initDefaults(Builder builder) { .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("retry_policy_1_codes")) .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("retry_policy_1_params")); + 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()); + builder .writeLogEntriesSettings() .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("retry_policy_1_codes")) @@ -578,7 +665,7 @@ public UnaryCallSettings.Builder deleteLogSettings() { } /** Returns the builder for the settings used for calls to writeLogEntries. */ - public UnaryCallSettings.Builder + public BatchingCallSettings.Builder writeLogEntriesSettings() { return writeLogEntriesSettings; }