Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Commit 70f22db

Browse files
Fix missing default retries (#2718)
* Fix missing default retries * Revert python and ruby baselines * Manual update of py and ruby baselines * Remove unneeded diag tracking
1 parent dffe03b commit 70f22db

14 files changed

+82
-65
lines changed

src/main/java/com/google/api/codegen/config/DiscoGapicInterfaceConfig.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ static DiscoGapicInterfaceConfig createInterfaceConfig(
7171
ResourceNameMessageConfigs messageConfigs,
7272
ImmutableMap<String, ResourceNameConfig> resourceNameConfigs) {
7373

74-
RetryCodesConfig retryCodesConfig =
75-
RetryCodesConfig.create(model.getDiagCollector(), interfaceConfigProto);
74+
RetryCodesConfig retryCodesConfig = RetryCodesConfig.create(interfaceConfigProto);
7675
ImmutableMap<String, RetryParamsDefinitionProto> retrySettingsDefinition =
7776
RetryDefinitionsTransformer.createRetrySettingsDefinition(interfaceConfigProto);
7877

src/main/java/com/google/api/codegen/config/GapicInterfaceConfig.java

-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ static GapicInterfaceConfig createInterfaceConfig(
119119

120120
RetryCodesConfig retryCodesConfig =
121121
RetryCodesConfig.create(
122-
diagCollector,
123122
interfaceConfigProto,
124123
new ArrayList<>(interfaceInput.getMethodsToGenerate().keySet()),
125124
protoParser);

src/main/java/com/google/api/codegen/config/RetryCodesConfig.java

+27-56
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@
2323
import com.google.api.codegen.RetryCodesDefinitionProto;
2424
import com.google.api.codegen.util.ProtoParser;
2525
import com.google.api.codegen.util.SymbolTable;
26-
import com.google.api.tools.framework.model.Diag;
27-
import com.google.api.tools.framework.model.DiagCollector;
2826
import com.google.api.tools.framework.model.Method;
29-
import com.google.api.tools.framework.model.SimpleLocation;
3027
import com.google.common.base.Strings;
3128
import com.google.common.collect.ImmutableList;
3229
import com.google.common.collect.ImmutableMap;
3330
import com.google.common.collect.ImmutableSet;
31+
import com.google.common.collect.Sets;
3432
import java.util.Collection;
3533
import java.util.HashMap;
36-
import java.util.HashSet;
3734
import java.util.List;
3835
import java.util.Map;
3936
import java.util.Set;
@@ -44,14 +41,15 @@ public class RetryCodesConfig {
4441

4542
public static ImmutableList<String> RETRY_CODES_FOR_HTTP_GET =
4643
DEFAULT_RETRY_CODES.get(RETRY_CODES_IDEMPOTENT_NAME);
44+
public static ImmutableList<String> RETRY_CODES_FOR_HTTP_NON_GET =
45+
DEFAULT_RETRY_CODES.get(RETRY_CODES_NON_IDEMPOTENT_NAME);
4746

4847
private Map<String, ImmutableList<String>> retryCodesDefinition = new HashMap<>();
4948
private Map<String, String> methodRetryNames = new HashMap<>();
5049

5150
private ImmutableSet<String> retryCodeDefsFromGapicConfig;
5251
private ImmutableMap<String, ImmutableList<String>> finalRetryCodesDefinition;
5352
private ImmutableMap<String, String> finalMethodRetryNames;
54-
private boolean error = false;
5553

5654
/**
5755
* A map of retry config names to the list of codes to retry on, e.g. { "idempotent" :
@@ -75,35 +73,22 @@ public Set<String> getRetryCodeDefsFromGapicConfig() {
7573

7674
private RetryCodesConfig() {}
7775

78-
public static RetryCodesConfig create(
79-
DiagCollector diagCollector, InterfaceConfigProto interfaceConfigProto) {
76+
public static RetryCodesConfig create(InterfaceConfigProto interfaceConfigProto) {
8077
RetryCodesConfig retryCodesConfig = new RetryCodesConfig();
8178

82-
retryCodesConfig.populateRetryCodesDefinitionFromConfigProto(
83-
diagCollector, interfaceConfigProto);
84-
if (retryCodesConfig.error) {
85-
return null;
86-
}
87-
retryCodesConfig.finalMethodRetryNames = ImmutableMap.copyOf(retryCodesConfig.methodRetryNames);
88-
retryCodesConfig.finalRetryCodesDefinition =
89-
ImmutableMap.copyOf(retryCodesConfig.retryCodesDefinition);
79+
retryCodesConfig.populateRetryCodesDefinitionFromConfigProto(interfaceConfigProto);
80+
retryCodesConfig.setFinalRetryProperties();
9081
return retryCodesConfig;
9182
}
9283

9384
public static RetryCodesConfig create(
94-
DiagCollector diagCollector,
9585
InterfaceConfigProto interfaceConfigProto,
9686
List<Method> methodsToGenerate,
9787
ProtoParser protoParser) {
9888
RetryCodesConfig retryCodesConfig = new RetryCodesConfig();
9989
retryCodesConfig.populateRetryCodesDefinition(
100-
diagCollector, interfaceConfigProto, methodsToGenerate, protoParser);
101-
if (retryCodesConfig.error) {
102-
return null;
103-
}
104-
retryCodesConfig.finalMethodRetryNames = ImmutableMap.copyOf(retryCodesConfig.methodRetryNames);
105-
retryCodesConfig.finalRetryCodesDefinition =
106-
ImmutableMap.copyOf(retryCodesConfig.retryCodesDefinition);
90+
interfaceConfigProto, methodsToGenerate, protoParser);
91+
retryCodesConfig.setFinalRetryProperties();
10792
return retryCodesConfig;
10893
}
10994

@@ -122,47 +107,32 @@ private static ImmutableMap<String, String> createMethodRetryNamesFromConfigProt
122107

123108
@Nullable
124109
private static ImmutableMap<String, ImmutableList<String>>
125-
createRetryCodesDefinitionFromConfigProto(
126-
DiagCollector diagCollector, InterfaceConfigProto interfaceConfigProto) {
110+
createRetryCodesDefinitionFromConfigProto(InterfaceConfigProto interfaceConfigProto) {
127111
ImmutableMap.Builder<String, ImmutableList<String>> builder = ImmutableMap.builder();
128112
for (RetryCodesDefinitionProto retryDef : interfaceConfigProto.getRetryCodesDefList()) {
129113
// Enforce ordering on set for baseline test consistency.
130-
Set<String> codes = new TreeSet<>();
131-
for (String codeText : retryDef.getRetryCodesList()) {
132-
try {
133-
codes.add(String.valueOf(codeText));
134-
} catch (IllegalArgumentException e) {
135-
diagCollector.addDiag(
136-
Diag.error(
137-
SimpleLocation.TOPLEVEL,
138-
"status code not found: '%s' (in interface %s)",
139-
codeText,
140-
interfaceConfigProto.getName()));
141-
}
142-
}
114+
Set<String> codes = new TreeSet<>(retryDef.getRetryCodesList());
143115
builder.put(retryDef.getName(), ImmutableList.copyOf(codes));
144116
}
145-
if (diagCollector.getErrorCount() > 0) {
146-
return null;
147-
}
148117
return builder.build();
149118
}
150119

120+
private void setFinalRetryProperties() {
121+
finalMethodRetryNames = ImmutableMap.copyOf(methodRetryNames);
122+
finalRetryCodesDefinition = ImmutableMap.copyOf(retryCodesDefinition);
123+
}
124+
151125
/**
152126
* Returns a mapping of a retryCodeDef name to the list of retry codes it contains. Also populates
153127
* the @param methodNameToRetryCodeNames with a mapping of a Method name to its retry code
154128
* settings name.
155129
*/
156130
private void populateRetryCodesDefinition(
157-
DiagCollector diagCollector,
158131
InterfaceConfigProto interfaceConfigProto,
159132
Collection<Method> methodsToGenerate,
160133
ProtoParser protoParser) {
161134
// First create the retry codes definitions from the GAPIC config.
162-
populateRetryCodesDefinitionFromConfigProto(diagCollector, interfaceConfigProto);
163-
if (error) {
164-
return;
165-
}
135+
populateRetryCodesDefinitionFromConfigProto(interfaceConfigProto);
166136

167137
// Then create the retry codes defs from the proto annotations, but don't overwrite
168138
// existing retry codes defs from the GAPIC config.
@@ -175,10 +145,10 @@ private void populateRetryCodesDefinition(
175145
* settings name.
176146
*/
177147
private void populateRetryCodesDefinitionFromConfigProto(
178-
DiagCollector diagCollector, InterfaceConfigProto interfaceConfigProto) {
148+
InterfaceConfigProto interfaceConfigProto) {
179149

180150
ImmutableMap<String, ImmutableList<String>> retryCodesDefFromConfigProto =
181-
createRetryCodesDefinitionFromConfigProto(diagCollector, interfaceConfigProto);
151+
createRetryCodesDefinitionFromConfigProto(interfaceConfigProto);
182152
if (retryCodesDefFromConfigProto == null) {
183153
return;
184154
}
@@ -205,6 +175,11 @@ private void populateRetryCodesDefinitionWithProtoFile(
205175

206176
SymbolTable symbolTable = new SymbolTable();
207177

178+
// Add retry codes for default cases (idempotent and non_idempotent) if they have not
179+
// already be added previously (in the GAPIC config).
180+
retryCodesDefinition.putIfAbsent(RETRY_CODES_IDEMPOTENT_NAME, RETRY_CODES_FOR_HTTP_GET);
181+
retryCodesDefinition.putIfAbsent(RETRY_CODES_NON_IDEMPOTENT_NAME, RETRY_CODES_FOR_HTTP_NON_GET);
182+
208183
for (String retryCodesName : retryCodesDefinition.keySet()) {
209184
// Record all the preexisting retryCodeNames from configProto.
210185
symbolTable.getNewSymbol(retryCodesName);
@@ -213,11 +188,8 @@ private void populateRetryCodesDefinitionWithProtoFile(
213188
// Unite all HTTP GET methods that have no additional retry codes under one retry code name to
214189
// reduce duplication.
215190
String httpGetRetryName;
216-
Set<String> defaultRetryCodesForHttpGet = new HashSet<>(RETRY_CODES_FOR_HTTP_GET);
217-
Set<String> existingIdempotentRetryCodes =
218-
new HashSet<>(
219-
retryCodesDefinition.getOrDefault(RETRY_CODES_IDEMPOTENT_NAME, ImmutableList.of()));
220-
if (defaultRetryCodesForHttpGet.equals(existingIdempotentRetryCodes)) {
191+
if (Sets.newHashSet(RETRY_CODES_FOR_HTTP_GET)
192+
.containsAll(retryCodesDefinition.get(RETRY_CODES_IDEMPOTENT_NAME))) {
221193
// The GAPIC config defined RETRY_CODES_IDEMPOTENT_NAME to have the same retry codes as
222194
// this method would have,
223195
// so we can just reuse RETRY_CODES_IDEMPOTENT_NAME.
@@ -228,9 +200,8 @@ private void populateRetryCodesDefinitionWithProtoFile(
228200

229201
// Unite all methods that have no retry codes under one retry code name to reduce duplication.
230202
String noRetryName;
231-
if (retryCodesDefinition
232-
.getOrDefault(RETRY_CODES_NON_IDEMPOTENT_NAME, ImmutableList.of())
233-
.isEmpty()) {
203+
if (Sets.newHashSet(RETRY_CODES_FOR_HTTP_NON_GET)
204+
.containsAll(retryCodesDefinition.get(RETRY_CODES_NON_IDEMPOTENT_NAME))) {
234205
noRetryName = RETRY_CODES_NON_IDEMPOTENT_NAME;
235206
} else {
236207
noRetryName = symbolTable.getNewSymbol(RETRY_CODES_NON_IDEMPOTENT_NAME);

src/test/java/com/google/api/codegen/gapic/testdata/java/java_multiple_services.baseline

+6
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,9 @@ public class DecrementerServiceStubSettings extends StubSettings<DecrementerServ
10861086

10871087
static {
10881088
ImmutableMap.Builder<String, ImmutableSet<StatusCode.Code>> definitions = ImmutableMap.builder();
1089+
definitions.put(
1090+
"idempotent",
1091+
ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList(StatusCode.Code.DEADLINE_EXCEEDED, StatusCode.Code.UNAVAILABLE)));
10891092
definitions.put(
10901093
"non_idempotent",
10911094
ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList()));
@@ -1877,6 +1880,9 @@ public class IncrementerServiceStubSettings extends StubSettings<IncrementerServ
18771880

18781881
static {
18791882
ImmutableMap.Builder<String, ImmutableSet<StatusCode.Code>> definitions = ImmutableMap.builder();
1883+
definitions.put(
1884+
"idempotent",
1885+
ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList(StatusCode.Code.DEADLINE_EXCEEDED, StatusCode.Code.UNAVAILABLE)));
18801886
definitions.put(
18811887
"non_idempotent",
18821888
ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList()));

src/test/java/com/google/api/codegen/gapic/testdata/java/java_no_path_templates.baseline

+3
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,9 @@ public class NoTemplatesApiServiceStubSettings extends StubSettings<NoTemplatesA
959959

960960
static {
961961
ImmutableMap.Builder<String, ImmutableSet<StatusCode.Code>> definitions = ImmutableMap.builder();
962+
definitions.put(
963+
"idempotent",
964+
ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList(StatusCode.Code.DEADLINE_EXCEEDED, StatusCode.Code.UNAVAILABLE)));
962965
definitions.put(
963966
"non_idempotent",
964967
ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList()));

src/test/java/com/google/api/codegen/gapic/testdata/nodejs/nodejs_multiple_services.baseline

+8
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ module.exports = DecrementerServiceClient;
392392
"interfaces": {
393393
"google.cloud.example.v1.foo.DecrementerService": {
394394
"retry_codes": {
395+
"idempotent": [
396+
"DEADLINE_EXCEEDED",
397+
"UNAVAILABLE"
398+
],
395399
"non_idempotent": []
396400
},
397401
"retry_params": {
@@ -710,6 +714,10 @@ module.exports = IncrementerServiceClient;
710714
"interfaces": {
711715
"google.cloud.example.v1.foo.IncrementerService": {
712716
"retry_codes": {
717+
"idempotent": [
718+
"DEADLINE_EXCEEDED",
719+
"UNAVAILABLE"
720+
],
713721
"non_idempotent": []
714722
},
715723
"retry_params": {

src/test/java/com/google/api/codegen/gapic/testdata/nodejs/nodejs_no_path_templates.baseline

+4
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ module.exports = NoTemplatesApiServiceClient;
387387
"interfaces": {
388388
"google.cloud.example.v1.NoTemplatesAPIService": {
389389
"retry_codes": {
390+
"idempotent": [
391+
"DEADLINE_EXCEEDED",
392+
"UNAVAILABLE"
393+
],
390394
"non_idempotent": []
391395
},
392396
"retry_params": {

src/test/java/com/google/api/codegen/gapic/testdata/php/php_no_path_templates.baseline

+4
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ class NoTemplatesApiServiceClient extends NoTemplatesApiServiceGapicClient
282282
"interfaces": {
283283
"google.cloud.example.v1.NoTemplatesAPIService": {
284284
"retry_codes": {
285+
"idempotent": [
286+
"DEADLINE_EXCEEDED",
287+
"UNAVAILABLE"
288+
],
285289
"non_idempotent": []
286290
},
287291
"retry_params": {

src/test/java/com/google/api/codegen/gapic/testdata/py/python_multiple_services.baseline

+8
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,10 @@ config = {
10731073
"interfaces": {
10741074
"google.cloud.example.v1.foo.DecrementerService": {
10751075
"retry_codes": {
1076+
"idempotent": [
1077+
"DEADLINE_EXCEEDED",
1078+
"UNAVAILABLE"
1079+
],
10761080
"non_idempotent": []
10771081
},
10781082
"retry_params": {
@@ -1307,6 +1311,10 @@ config = {
13071311
"interfaces": {
13081312
"google.cloud.example.v1.foo.IncrementerService": {
13091313
"retry_codes": {
1314+
"idempotent": [
1315+
"DEADLINE_EXCEEDED",
1316+
"UNAVAILABLE"
1317+
],
13101318
"non_idempotent": []
13111319
},
13121320
"retry_params": {

src/test/java/com/google/api/codegen/gapic/testdata/py/python_no_path_templates.baseline

+4
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,10 @@ config = {
10121012
"interfaces": {
10131013
"google.cloud.example.v1.NoTemplatesAPIService": {
10141014
"retry_codes": {
1015+
"idempotent": [
1016+
"DEADLINE_EXCEEDED",
1017+
"UNAVAILABLE"
1018+
],
10151019
"non_idempotent": []
10161020
},
10171021
"retry_params": {

src/test/java/com/google/api/codegen/gapic/testdata/ruby/ruby_multiple_services.baseline

+8
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,10 @@ end
11461146
"interfaces": {
11471147
"google.cloud.example.v1.foo.DecrementerService": {
11481148
"retry_codes": {
1149+
"idempotent": [
1150+
"DEADLINE_EXCEEDED",
1151+
"UNAVAILABLE"
1152+
],
11491153
"non_idempotent": []
11501154
},
11511155
"retry_params": {
@@ -1425,6 +1429,10 @@ end
14251429
"interfaces": {
14261430
"google.cloud.example.v1.foo.IncrementerService": {
14271431
"retry_codes": {
1432+
"idempotent": [
1433+
"DEADLINE_EXCEEDED",
1434+
"UNAVAILABLE"
1435+
],
14281436
"non_idempotent": []
14291437
},
14301438
"retry_params": {

src/test/java/com/google/api/codegen/protoannotations/testdata/java_library_no_gapic_config.baseline

+3
Original file line numberDiff line numberDiff line change
@@ -8278,6 +8278,9 @@ public class MyProtoStubSettings extends StubSettings<MyProtoStubSettings> {
82788278

82798279
static {
82808280
ImmutableMap.Builder<String, ImmutableSet<StatusCode.Code>> definitions = ImmutableMap.builder();
8281+
definitions.put(
8282+
"idempotent",
8283+
ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList(StatusCode.Code.DEADLINE_EXCEEDED, StatusCode.Code.UNAVAILABLE)));
82818284
definitions.put(
82828285
"non_idempotent",
82838286
ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList()));

src/test/java/com/google/api/codegen/protoannotations/testdata/ruby_library_no_gapic_config.baseline

+4
Original file line numberDiff line numberDiff line change
@@ -5143,6 +5143,10 @@ end
51435143
"interfaces": {
51445144
"google.example.library.v1.MyProto": {
51455145
"retry_codes": {
5146+
"idempotent": [
5147+
"DEADLINE_EXCEEDED",
5148+
"UNAVAILABLE"
5149+
],
51465150
"non_idempotent": []
51475151
},
51485152
"retry_params": {

src/test/java/com/google/api/codegen/transformer/RetryDefinitionsTransformerTest.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ public void testWithConfigAndInterface() {
9999
DiagCollector diagCollector = new BoundedDiagCollector();
100100

101101
RetryCodesConfig retryCodesConfig =
102-
RetryCodesConfig.create(
103-
diagCollector, interfaceConfigProto, apiInterface.getMethods(), protoParser);
102+
RetryCodesConfig.create(interfaceConfigProto, apiInterface.getMethods(), protoParser);
104103

105104
Map<String, ImmutableList<String>> retryCodesDef = retryCodesConfig.getRetryCodesDefinition();
106105
Map<String, String> retryCodesMap = retryCodesConfig.getMethodRetryNames();
@@ -154,11 +153,8 @@ public void testWithInterfaceOnly() {
154153
.addMethods(MethodConfigProto.newBuilder().setName(PERMISSION_DENIED_METHOD_NAME))
155154
.build();
156155

157-
DiagCollector diagCollector = new BoundedDiagCollector();
158-
159156
RetryCodesConfig retryCodesConfig =
160-
RetryCodesConfig.create(
161-
diagCollector, bareBonesConfigProto, apiInterface.getMethods(), protoParser);
157+
RetryCodesConfig.create(bareBonesConfigProto, apiInterface.getMethods(), protoParser);
162158

163159
Map<String, ImmutableList<String>> retryCodesDef = retryCodesConfig.getRetryCodesDefinition();
164160
Map<String, String> retryCodesMap = retryCodesConfig.getMethodRetryNames();

0 commit comments

Comments
 (0)