Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, TypeNode> STATIC_TYPES = createStaticTypes();
Expand Down Expand Up @@ -103,7 +102,7 @@ public static BlockStatement createRetryParamDefinitionsBlock(

private static Map<String, TypeNode> createStaticTypes() {
List<Class> 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,33 @@ public static GapicServiceConfig create(ServiceConfig serviceConfig) {
return new GapicServiceConfig(methodConfigs, methodConfigTable);
}

public Map<String, RetrySettings> getAllRetrySettings(Service service) {
public Map<String, GapicRetrySettings> 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<Integer> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ public void serviceConfig_noConfigsFound() {
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();

Map<String, RetrySettings> retrySettings = serviceConfig.getAllRetrySettings(service);
Map<String, GapicRetrySettings> retrySettings = serviceConfig.getAllGapicRetrySettings(service);
assertEquals(1, retrySettings.size());
String retryParamsName = serviceConfig.getRetryParamsName(service, service.methods().get(0));
assertEquals("no_retry_params", retryParamsName);

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<String, List<Code>> retryCodes = serviceConfig.getAllRetryCodes(service);
assertEquals(1, retryCodes.size());
Expand All @@ -80,7 +81,7 @@ public void serviceConfig_basic() {
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();

Map<String, RetrySettings> retrySettings = serviceConfig.getAllRetrySettings(service);
Map<String, GapicRetrySettings> retrySettings = serviceConfig.getAllGapicRetrySettings(service);
assertEquals(2, retrySettings.size());
Map<String, List<Code>> retryCodes = serviceConfig.getAllRetryCodes(service);
assertEquals(2, retryCodes.size());
Expand All @@ -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());
Expand All @@ -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);
Expand Down