23
23
import com .google .api .codegen .RetryCodesDefinitionProto ;
24
24
import com .google .api .codegen .util .ProtoParser ;
25
25
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 ;
28
26
import com .google .api .tools .framework .model .Method ;
29
- import com .google .api .tools .framework .model .SimpleLocation ;
30
27
import com .google .common .base .Strings ;
31
28
import com .google .common .collect .ImmutableList ;
32
29
import com .google .common .collect .ImmutableMap ;
33
30
import com .google .common .collect .ImmutableSet ;
31
+ import com .google .common .collect .Sets ;
34
32
import java .util .Collection ;
35
33
import java .util .HashMap ;
36
- import java .util .HashSet ;
37
34
import java .util .List ;
38
35
import java .util .Map ;
39
36
import java .util .Set ;
@@ -44,14 +41,15 @@ public class RetryCodesConfig {
44
41
45
42
public static ImmutableList <String > RETRY_CODES_FOR_HTTP_GET =
46
43
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 );
47
46
48
47
private Map <String , ImmutableList <String >> retryCodesDefinition = new HashMap <>();
49
48
private Map <String , String > methodRetryNames = new HashMap <>();
50
49
51
50
private ImmutableSet <String > retryCodeDefsFromGapicConfig ;
52
51
private ImmutableMap <String , ImmutableList <String >> finalRetryCodesDefinition ;
53
52
private ImmutableMap <String , String > finalMethodRetryNames ;
54
- private boolean error = false ;
55
53
56
54
/**
57
55
* 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() {
75
73
76
74
private RetryCodesConfig () {}
77
75
78
- public static RetryCodesConfig create (
79
- DiagCollector diagCollector , InterfaceConfigProto interfaceConfigProto ) {
76
+ public static RetryCodesConfig create (InterfaceConfigProto interfaceConfigProto ) {
80
77
RetryCodesConfig retryCodesConfig = new RetryCodesConfig ();
81
78
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 ();
90
81
return retryCodesConfig ;
91
82
}
92
83
93
84
public static RetryCodesConfig create (
94
- DiagCollector diagCollector ,
95
85
InterfaceConfigProto interfaceConfigProto ,
96
86
List <Method > methodsToGenerate ,
97
87
ProtoParser protoParser ) {
98
88
RetryCodesConfig retryCodesConfig = new RetryCodesConfig ();
99
89
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 ();
107
92
return retryCodesConfig ;
108
93
}
109
94
@@ -122,47 +107,32 @@ private static ImmutableMap<String, String> createMethodRetryNamesFromConfigProt
122
107
123
108
@ Nullable
124
109
private static ImmutableMap <String , ImmutableList <String >>
125
- createRetryCodesDefinitionFromConfigProto (
126
- DiagCollector diagCollector , InterfaceConfigProto interfaceConfigProto ) {
110
+ createRetryCodesDefinitionFromConfigProto (InterfaceConfigProto interfaceConfigProto ) {
127
111
ImmutableMap .Builder <String , ImmutableList <String >> builder = ImmutableMap .builder ();
128
112
for (RetryCodesDefinitionProto retryDef : interfaceConfigProto .getRetryCodesDefList ()) {
129
113
// 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 ());
143
115
builder .put (retryDef .getName (), ImmutableList .copyOf (codes ));
144
116
}
145
- if (diagCollector .getErrorCount () > 0 ) {
146
- return null ;
147
- }
148
117
return builder .build ();
149
118
}
150
119
120
+ private void setFinalRetryProperties () {
121
+ finalMethodRetryNames = ImmutableMap .copyOf (methodRetryNames );
122
+ finalRetryCodesDefinition = ImmutableMap .copyOf (retryCodesDefinition );
123
+ }
124
+
151
125
/**
152
126
* Returns a mapping of a retryCodeDef name to the list of retry codes it contains. Also populates
153
127
* the @param methodNameToRetryCodeNames with a mapping of a Method name to its retry code
154
128
* settings name.
155
129
*/
156
130
private void populateRetryCodesDefinition (
157
- DiagCollector diagCollector ,
158
131
InterfaceConfigProto interfaceConfigProto ,
159
132
Collection <Method > methodsToGenerate ,
160
133
ProtoParser protoParser ) {
161
134
// First create the retry codes definitions from the GAPIC config.
162
- populateRetryCodesDefinitionFromConfigProto (diagCollector , interfaceConfigProto );
163
- if (error ) {
164
- return ;
165
- }
135
+ populateRetryCodesDefinitionFromConfigProto (interfaceConfigProto );
166
136
167
137
// Then create the retry codes defs from the proto annotations, but don't overwrite
168
138
// existing retry codes defs from the GAPIC config.
@@ -175,10 +145,10 @@ private void populateRetryCodesDefinition(
175
145
* settings name.
176
146
*/
177
147
private void populateRetryCodesDefinitionFromConfigProto (
178
- DiagCollector diagCollector , InterfaceConfigProto interfaceConfigProto ) {
148
+ InterfaceConfigProto interfaceConfigProto ) {
179
149
180
150
ImmutableMap <String , ImmutableList <String >> retryCodesDefFromConfigProto =
181
- createRetryCodesDefinitionFromConfigProto (diagCollector , interfaceConfigProto );
151
+ createRetryCodesDefinitionFromConfigProto (interfaceConfigProto );
182
152
if (retryCodesDefFromConfigProto == null ) {
183
153
return ;
184
154
}
@@ -205,6 +175,11 @@ private void populateRetryCodesDefinitionWithProtoFile(
205
175
206
176
SymbolTable symbolTable = new SymbolTable ();
207
177
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
+
208
183
for (String retryCodesName : retryCodesDefinition .keySet ()) {
209
184
// Record all the preexisting retryCodeNames from configProto.
210
185
symbolTable .getNewSymbol (retryCodesName );
@@ -213,11 +188,8 @@ private void populateRetryCodesDefinitionWithProtoFile(
213
188
// Unite all HTTP GET methods that have no additional retry codes under one retry code name to
214
189
// reduce duplication.
215
190
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 ))) {
221
193
// The GAPIC config defined RETRY_CODES_IDEMPOTENT_NAME to have the same retry codes as
222
194
// this method would have,
223
195
// so we can just reuse RETRY_CODES_IDEMPOTENT_NAME.
@@ -228,9 +200,8 @@ private void populateRetryCodesDefinitionWithProtoFile(
228
200
229
201
// Unite all methods that have no retry codes under one retry code name to reduce duplication.
230
202
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 ))) {
234
205
noRetryName = RETRY_CODES_NON_IDEMPOTENT_NAME ;
235
206
} else {
236
207
noRetryName = symbolTable .getNewSymbol (RETRY_CODES_NON_IDEMPOTENT_NAME );
0 commit comments