Skip to content

Commit

Permalink
fix: bring back gapic.yaml to bazel and Java parsing (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
miraleung authored Nov 21, 2020
1 parent f286133 commit 01d30a2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
4 changes: 4 additions & 0 deletions rules_java_gapic/java_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def java_gapic_library(
package = None,
service_yaml = None,
grpc_service_config = None,
gapic_yaml = None,
deps = [],
test_deps = [],
**kwargs):
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"

Expand Down Expand Up @@ -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}"
1 change: 1 addition & 0 deletions test/integration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
97 changes: 92 additions & 5 deletions test/integration/goldens/logging/LoggingServiceV2StubSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -110,7 +119,7 @@ public class LoggingServiceV2StubSettings extends StubSettings<LoggingServiceV2S
.build();

private final UnaryCallSettings<DeleteLogRequest, Empty> deleteLogSettings;
private final UnaryCallSettings<WriteLogEntriesRequest, WriteLogEntriesResponse>
private final BatchingCallSettings<WriteLogEntriesRequest, WriteLogEntriesResponse>
writeLogEntriesSettings;
private final PagedCallSettings<
ListLogEntriesRequest, ListLogEntriesResponse, ListLogEntriesPagedResponse>
Expand Down Expand Up @@ -311,13 +320,73 @@ public ApiFuture<ListLogsPagedResponse> getFuturePagedResponse(
}
};

private static final BatchingDescriptor<WriteLogEntriesRequest, WriteLogEntriesResponse>
WRITE_LOG_ENTRIES_BATCHING_DESC =
new BatchingDescriptor<WriteLogEntriesRequest, WriteLogEntriesResponse>() {
@Override
public PartitionKey getBatchPartitionKey(WriteLogEntriesRequest request) {
return new PartitionKey(
request.getLogName(), request.getResource(), request.getLabels());
}

@Override
public RequestBuilder<WriteLogEntriesRequest> getRequestBuilder() {
return new RequestBuilder<WriteLogEntriesRequest>() {
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<? extends BatchedRequestIssuer<WriteLogEntriesResponse>> batch) {
for (BatchedRequestIssuer<WriteLogEntriesResponse> responder : batch) {
WriteLogEntriesResponse response = WriteLogEntriesResponse.newBuilder().build();
responder.setResponse(response);
}
}

@Override
public void splitException(
Throwable throwable,
Collection<? extends BatchedRequestIssuer<WriteLogEntriesResponse>> batch) {
for (BatchedRequestIssuer<WriteLogEntriesResponse> 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<DeleteLogRequest, Empty> deleteLogSettings() {
return deleteLogSettings;
}

/** Returns the object with the settings used for calls to writeLogEntries. */
public UnaryCallSettings<WriteLogEntriesRequest, WriteLogEntriesResponse>
public BatchingCallSettings<WriteLogEntriesRequest, WriteLogEntriesResponse>
writeLogEntriesSettings() {
return writeLogEntriesSettings;
}
Expand Down Expand Up @@ -425,7 +494,7 @@ protected LoggingServiceV2StubSettings(Builder settingsBuilder) throws IOExcepti
public static class Builder extends StubSettings.Builder<LoggingServiceV2StubSettings, Builder> {
private final ImmutableList<UnaryCallSettings.Builder<?, ?>> unaryMethodSettingsBuilders;
private final UnaryCallSettings.Builder<DeleteLogRequest, Empty> deleteLogSettings;
private final UnaryCallSettings.Builder<WriteLogEntriesRequest, WriteLogEntriesResponse>
private final BatchingCallSettings.Builder<WriteLogEntriesRequest, WriteLogEntriesResponse>
writeLogEntriesSettings;
private final PagedCallSettings.Builder<
ListLogEntriesRequest, ListLogEntriesResponse, ListLogEntriesPagedResponse>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -578,7 +665,7 @@ public UnaryCallSettings.Builder<DeleteLogRequest, Empty> deleteLogSettings() {
}

/** Returns the builder for the settings used for calls to writeLogEntries. */
public UnaryCallSettings.Builder<WriteLogEntriesRequest, WriteLogEntriesResponse>
public BatchingCallSettings.Builder<WriteLogEntriesRequest, WriteLogEntriesResponse>
writeLogEntriesSettings() {
return writeLogEntriesSettings;
}
Expand Down

0 comments on commit 01d30a2

Please sign in to comment.