diff --git a/src/main/java/com/google/api/generator/gapic/composer/RetrySettingsComposer.java b/src/main/java/com/google/api/generator/gapic/composer/RetrySettingsComposer.java index 018b2288c7..35cffa36ba 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/RetrySettingsComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/RetrySettingsComposer.java @@ -34,7 +34,6 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import org.threeten.bp.Duration; public class RetrySettingsComposer { private static final Map STATIC_TYPES = createStaticTypes(); @@ -103,7 +102,7 @@ public static BlockStatement createRetryParamDefinitionsBlock( private static Map createStaticTypes() { List concreteClazzes = - Arrays.asList(Duration.class, ImmutableMap.class, RetrySettings.class); + Arrays.asList(org.threeten.bp.Duration.class, ImmutableMap.class, RetrySettings.class); return concreteClazzes.stream() .collect( Collectors.toMap( diff --git a/src/main/java/com/google/api/generator/gapic/model/GapicRetrySettings.java b/src/main/java/com/google/api/generator/gapic/model/GapicRetrySettings.java new file mode 100644 index 0000000000..c6539b2088 --- /dev/null +++ b/src/main/java/com/google/api/generator/gapic/model/GapicRetrySettings.java @@ -0,0 +1,49 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.api.generator.gapic.model; + +import com.google.auto.value.AutoValue; +import com.google.protobuf.Duration; +import io.grpc.serviceconfig.MethodConfig.RetryPolicy; + +@AutoValue +public abstract class GapicRetrySettings { + public enum Kind { + NONE, // No retry policy and no timeout. + NO_RETRY, // No retry policy, timeout only. + FULL // Retry policy and timeout. + }; + + public abstract Duration timeout(); + + public abstract RetryPolicy retryPolicy(); + + public abstract Kind kind(); + + public static Builder builder() { + return new AutoValue_GapicRetrySettings.Builder(); + } + + @AutoValue.Builder + public abstract static class Builder { + public abstract Builder setTimeout(Duration timeout); + + public abstract Builder setRetryPolicy(RetryPolicy retryPolicy); + + public abstract Builder setKind(Kind kind); + + public abstract GapicRetrySettings build(); + } +} diff --git a/src/main/java/com/google/api/generator/gapic/model/GapicServiceConfig.java b/src/main/java/com/google/api/generator/gapic/model/GapicServiceConfig.java index f8303deaee..c19e23bfdc 100644 --- a/src/main/java/com/google/api/generator/gapic/model/GapicServiceConfig.java +++ b/src/main/java/com/google/api/generator/gapic/model/GapicServiceConfig.java @@ -63,12 +63,33 @@ public static GapicServiceConfig create(ServiceConfig serviceConfig) { return new GapicServiceConfig(methodConfigs, methodConfigTable); } - public Map getAllRetrySettings(Service service) { + public Map getAllGapicRetrySettings(Service service) { return service.methods().stream() .collect( Collectors.toMap( m -> getRetryParamsName(service, m), - m -> RetrySettings.with(timeoutLookup(service, m), retryPolicyLookup(service, m)), + m -> { + GapicRetrySettings.Kind kind = GapicRetrySettings.Kind.FULL; + Optional retryPolicyIndexOpt = retryPolicyIndexLookup(service, m); + if (!retryPolicyIndexOpt.isPresent()) { + kind = GapicRetrySettings.Kind.NONE; + } else { + MethodConfig methodConfig = methodConfigs.get(retryPolicyIndexOpt.get()); + if (!methodConfig.hasTimeout() && !methodConfig.hasRetryPolicy()) { + kind = GapicRetrySettings.Kind.NONE; + } else { + kind = + methodConfig.hasRetryPolicy() + ? GapicRetrySettings.Kind.FULL + : GapicRetrySettings.Kind.NO_RETRY; + } + } + return GapicRetrySettings.builder() + .setTimeout(timeoutLookup(service, m)) + .setRetryPolicy(retryPolicyLookup(service, m)) + .setKind(kind) + .build(); + }, (r1, r2) -> r2, LinkedHashMap::new)); } diff --git a/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java b/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java index a59f1968f4..dbeb6c8e92 100644 --- a/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java +++ b/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java @@ -52,7 +52,7 @@ public void serviceConfig_noConfigsFound() { assertTrue(serviceConfigOpt.isPresent()); GapicServiceConfig serviceConfig = serviceConfigOpt.get(); - Map retrySettings = serviceConfig.getAllRetrySettings(service); + Map retrySettings = serviceConfig.getAllGapicRetrySettings(service); assertEquals(1, retrySettings.size()); String retryParamsName = serviceConfig.getRetryParamsName(service, service.methods().get(0)); assertEquals("no_retry_params", retryParamsName); @@ -60,6 +60,7 @@ public void serviceConfig_noConfigsFound() { assertEquals(GapicServiceConfig.EMPTY_TIMEOUT, retrySettings.get(retryParamsName).timeout()); assertEquals( GapicServiceConfig.EMPTY_RETRY_POLICY, retrySettings.get(retryParamsName).retryPolicy()); + assertEquals(GapicRetrySettings.Kind.NONE, retrySettings.get(retryParamsName).kind()); Map> retryCodes = serviceConfig.getAllRetryCodes(service); assertEquals(1, retryCodes.size()); @@ -80,7 +81,7 @@ public void serviceConfig_basic() { assertTrue(serviceConfigOpt.isPresent()); GapicServiceConfig serviceConfig = serviceConfigOpt.get(); - Map retrySettings = serviceConfig.getAllRetrySettings(service); + Map retrySettings = serviceConfig.getAllGapicRetrySettings(service); assertEquals(2, retrySettings.size()); Map> retryCodes = serviceConfig.getAllRetryCodes(service); assertEquals(2, retryCodes.size()); @@ -90,9 +91,10 @@ public void serviceConfig_basic() { assertThat(method).isNotNull(); String retryParamsName = serviceConfig.getRetryParamsName(service, method); assertEquals("retry_policy_1_params", retryParamsName); - RetrySettings settings = retrySettings.get(retryParamsName); + GapicRetrySettings settings = retrySettings.get(retryParamsName); assertThat(settings).isNotNull(); assertEquals(10, settings.timeout().getSeconds()); + assertEquals(GapicRetrySettings.Kind.FULL, settings.kind()); MethodConfig.RetryPolicy retryPolicy = settings.retryPolicy(); assertEquals(3, retryPolicy.getMaxAttempts()); @@ -115,6 +117,7 @@ public void serviceConfig_basic() { assertThat(settings).isNotNull(); assertEquals(5, settings.timeout().getSeconds()); assertEquals(GapicServiceConfig.EMPTY_RETRY_POLICY, settings.retryPolicy()); + assertEquals(GapicRetrySettings.Kind.NO_RETRY, settings.kind()); retryCodeName = serviceConfig.getRetryCodeName(service, method); assertEquals("no_retry_0_codes", retryCodeName);