Skip to content

Commit f193e52

Browse files
authored
Merge pull request #1010 from oracle/delete_retry_call
Delete superfluous ReplaceDomainWithConfictRetry
2 parents 7900159 + 64d89a8 commit f193e52

File tree

2 files changed

+4
-247
lines changed

2 files changed

+4
-247
lines changed

operator/src/main/java/oracle/kubernetes/operator/helpers/CallBuilder.java

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
package oracle.kubernetes.operator.helpers;
66

7-
import static java.net.HttpURLConnection.HTTP_CONFLICT;
8-
97
import com.squareup.okhttp.Call;
108
import io.kubernetes.client.ApiCallback;
119
import io.kubernetes.client.ApiClient;
@@ -49,9 +47,6 @@
4947
import oracle.kubernetes.operator.calls.RequestParams;
5048
import oracle.kubernetes.operator.calls.SynchronousCallDispatcher;
5149
import oracle.kubernetes.operator.calls.SynchronousCallFactory;
52-
import oracle.kubernetes.operator.logging.LoggingFacade;
53-
import oracle.kubernetes.operator.logging.LoggingFactory;
54-
import oracle.kubernetes.operator.logging.MessageKeys;
5550
import oracle.kubernetes.operator.utils.PatchUtils;
5651
import oracle.kubernetes.operator.work.Step;
5752
import oracle.kubernetes.weblogic.domain.api.WeblogicApi;
@@ -65,8 +60,6 @@ public class CallBuilder {
6560
/** HTTP status code for "Not Found". */
6661
public static final int NOT_FOUND = 404;
6762

68-
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
69-
7063
private static final SynchronousCallDispatcher DEFAULT_DISPATCHER =
7164
new SynchronousCallDispatcher() {
7265
@Override
@@ -170,65 +163,6 @@ public VersionInfo readVersionCode() throws ApiException {
170163
requestParams, ((client, params) -> new VersionApi(client).getCode()));
171164
}
172165

173-
/**
174-
* Class extended by callers to {@link
175-
* #executeSynchronousCallWithConflictRetry(RequestParamsBuilder, SynchronousCallFactory,
176-
* ConflictRetry)} for building the RequestParams to be passed to {@link
177-
* #executeSynchronousCall(RequestParams, SynchronousCallFactory)}.
178-
*
179-
* @param <T> Type of kubernetes object to be passed to the API
180-
*/
181-
abstract static class RequestParamsBuilder<T> {
182-
T body;
183-
184-
public RequestParamsBuilder(T body) {
185-
this.body = body;
186-
}
187-
188-
abstract RequestParams buildRequestParams();
189-
190-
void setBody(T body) {
191-
this.body = body;
192-
}
193-
}
194-
195-
private <T> T executeSynchronousCallWithConflictRetry(
196-
RequestParamsBuilder requestParamsBuilder,
197-
SynchronousCallFactory<T> factory,
198-
ConflictRetry<T> conflictRetry)
199-
throws ApiException {
200-
int retryCount = 0;
201-
while (retryCount == 0 || retryCount < maxRetryCount) {
202-
retryCount++;
203-
RequestParams requestParams = requestParamsBuilder.buildRequestParams();
204-
try {
205-
return executeSynchronousCall(requestParams, factory);
206-
} catch (ApiException apiException) {
207-
boolean retry = false;
208-
if (apiException.getCode() == HTTP_CONFLICT
209-
&& conflictRetry != null
210-
&& retryCount < maxRetryCount) {
211-
T body = conflictRetry.getUpdatedObject();
212-
if (body != null) {
213-
requestParamsBuilder.setBody(body);
214-
retry = true;
215-
LOGGER.fine(
216-
MessageKeys.SYNC_RETRY,
217-
requestParams.call,
218-
apiException.getCode(),
219-
apiException.getMessage(),
220-
retryCount,
221-
maxRetryCount);
222-
}
223-
}
224-
if (!retry) {
225-
throw apiException;
226-
}
227-
}
228-
}
229-
return null;
230-
}
231-
232166
private <T> T executeSynchronousCall(
233167
RequestParams requestParams, SynchronousCallFactory<T> factory) throws ApiException {
234168
return DISPATCHER.execute(factory, requestParams, helper);
@@ -365,33 +299,6 @@ public Step readDomainAsync(String name, String namespace, ResponseStep<Domain>
365299
pretty,
366300
null);
367301

368-
/**
369-
* Replace domain.
370-
*
371-
* @param uid the domain uid (unique within the k8s cluster)
372-
* @param namespace Namespace
373-
* @param body Body
374-
* @param conflictRetry ConflictRetry implementation to be called to obtain the latest version of
375-
* the Domain for retrying the replaceDomain synchronous call if previous call failed with
376-
* Conflict response code (409)
377-
* @return Replaced domain
378-
* @throws ApiException APIException
379-
*/
380-
public Domain replaceDomainWithConflictRetry(
381-
String uid, String namespace, Domain body, ConflictRetry<Domain> conflictRetry)
382-
throws ApiException {
383-
return executeSynchronousCallWithConflictRetry(
384-
new RequestParamsBuilder<Domain>(body) {
385-
386-
@Override
387-
RequestParams buildRequestParams() {
388-
return new RequestParams("replaceDomain", namespace, uid, body);
389-
}
390-
},
391-
REPLACE_DOMAIN_CALL,
392-
conflictRetry);
393-
}
394-
395302
/**
396303
* Replace domain.
397304
*

operator/src/test/java/oracle/kubernetes/operator/helpers/CallBuilderTest.java

Lines changed: 4 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
88
import static java.net.HttpURLConnection.HTTP_CONFLICT;
9-
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
109
import static org.hamcrest.Matchers.equalTo;
1110
import static org.hamcrest.Matchers.instanceOf;
1211
import static org.hamcrest.junit.MatcherAssert.assertThat;
13-
import static org.junit.Assert.fail;
1412

1513
import com.google.gson.GsonBuilder;
1614
import com.meterware.pseudoserver.HttpUserAgentTest;
@@ -29,7 +27,6 @@
2927
import io.kubernetes.client.models.V1Status;
3028
import io.kubernetes.client.models.VersionInfo;
3129
import java.io.IOException;
32-
import java.lang.reflect.Field;
3330
import java.util.ArrayList;
3431
import java.util.Arrays;
3532
import java.util.List;
@@ -70,7 +67,7 @@ public void setUp() throws NoSuchFieldException {
7067
}
7168

7269
@After
73-
public void tearDown() throws Exception {
70+
public void tearDown() {
7471
for (Memento memento : mementos) memento.revert();
7572
}
7673

@@ -117,133 +114,6 @@ public void replaceDomain_conflictResponseCode_throws() throws ApiException {
117114
callBuilder.replaceDomain(UID, NAMESPACE, domain);
118115
}
119116

120-
@Test
121-
public void replaceDomainWithRetry_sendsNewDomain() throws ApiException {
122-
Domain domain = new Domain().withMetadata(createMetadata());
123-
defineHttpPutResponse(
124-
DOMAIN_RESOURCE, UID, domain, (json) -> requestBody = fromJson(json, Domain.class));
125-
126-
callBuilder.replaceDomainWithConflictRetry(UID, NAMESPACE, domain, () -> domain);
127-
128-
assertThat(requestBody, equalTo(domain));
129-
}
130-
131-
@Test
132-
public void replaceDomainWithRetry_withMaxRetryCountOfZero_sendsNewDomain()
133-
throws ApiException, NoSuchFieldException, IllegalAccessException {
134-
Domain domain = new Domain().withMetadata(createMetadata());
135-
defineHttpPutResponse(
136-
DOMAIN_RESOURCE, UID, domain, (json) -> requestBody = fromJson(json, Domain.class));
137-
138-
setMaxRetryCount(callBuilder, 0);
139-
callBuilder.replaceDomainWithConflictRetry(UID, NAMESPACE, domain, () -> domain);
140-
141-
assertThat(requestBody, equalTo(domain));
142-
}
143-
144-
@Test
145-
public void replaceDomainWithRetry_sendsNewDomain_afterRetry() throws ApiException {
146-
Domain domain = new Domain().withMetadata(createMetadata());
147-
ConflictOncePutServlet conflictOncePutServlet =
148-
new ConflictOncePutServlet(domain, (json) -> requestBody = fromJson(json, Domain.class));
149-
defineResource(DOMAIN_RESOURCE + "/" + UID, conflictOncePutServlet);
150-
151-
callBuilder.replaceDomainWithConflictRetry(UID, NAMESPACE, domain, () -> domain);
152-
153-
assertThat(requestBody, equalTo(domain));
154-
assertThat(conflictOncePutServlet.conflictReturned, equalTo(true));
155-
}
156-
157-
@Test(expected = ApiException.class)
158-
public void replaceDomainWithConflictRetry_conflictResponseCode_throws() throws ApiException {
159-
Domain domain = new Domain().withMetadata(createMetadata());
160-
defineHttpPutResponse(DOMAIN_RESOURCE, UID, domain, new ErrorCodePutServlet(HTTP_CONFLICT));
161-
162-
callBuilder.replaceDomainWithConflictRetry(UID, NAMESPACE, domain, () -> domain);
163-
}
164-
165-
@Test(expected = ApiException.class)
166-
public void replaceDomainWithConflictRetry_errorResponseCode_throws() throws ApiException {
167-
Domain domain = new Domain().withMetadata(createMetadata());
168-
defineHttpPutResponse(DOMAIN_RESOURCE, UID, domain, new ErrorCodePutServlet(HTTP_BAD_REQUEST));
169-
170-
callBuilder.replaceDomainWithConflictRetry(UID, NAMESPACE, domain, () -> domain);
171-
}
172-
173-
@Test
174-
public void replaceDomainWithConflictRetry_conflictResponseCode_retriedMaxTimes()
175-
throws ApiException, NoSuchFieldException, IllegalAccessException {
176-
final int MAX_RETRY_COUNT = 5;
177-
setMaxRetryCount(callBuilder, MAX_RETRY_COUNT);
178-
Domain domain = new Domain().withMetadata(createMetadata());
179-
ErrorCodePutServlet conflictPutServlet = new ErrorCodePutServlet(HTTP_CONFLICT);
180-
defineHttpPutResponse(DOMAIN_RESOURCE, UID, domain, conflictPutServlet);
181-
try {
182-
callBuilder.replaceDomainWithConflictRetry(UID, NAMESPACE, domain, () -> domain);
183-
fail("Expected ApiException not thrown");
184-
} catch (ApiException apiException) {
185-
186-
}
187-
assertThat(conflictPutServlet.numGetPutResponseCalled, equalTo(MAX_RETRY_COUNT));
188-
}
189-
190-
@Test
191-
public void replaceDomainWithConflictRetry_otherResponseCode_noRetries()
192-
throws ApiException, NoSuchFieldException, IllegalAccessException {
193-
Domain domain = new Domain().withMetadata(createMetadata());
194-
ErrorCodePutServlet conflictPutServlet = new ErrorCodePutServlet(HTTP_INTERNAL_ERROR);
195-
defineHttpPutResponse(DOMAIN_RESOURCE, UID, domain, conflictPutServlet);
196-
try {
197-
callBuilder.replaceDomainWithConflictRetry(UID, NAMESPACE, domain, () -> domain);
198-
fail("Expected ApiException not thrown");
199-
} catch (ApiException apiException) {
200-
201-
}
202-
assertThat(conflictPutServlet.numGetPutResponseCalled, equalTo(1));
203-
}
204-
205-
@Test
206-
public void replaceDomainWithConflictRetry_withMaxRetryCountOfZero_noRetries()
207-
throws ApiException, NoSuchFieldException, IllegalAccessException {
208-
Domain domain = new Domain().withMetadata(createMetadata());
209-
ErrorCodePutServlet conflictPutServlet = new ErrorCodePutServlet(HTTP_CONFLICT);
210-
defineHttpPutResponse(DOMAIN_RESOURCE, UID, domain, conflictPutServlet);
211-
setMaxRetryCount(callBuilder, 0);
212-
try {
213-
callBuilder.replaceDomainWithConflictRetry(UID, NAMESPACE, domain, () -> domain);
214-
fail("Expected ApiException not thrown");
215-
} catch (ApiException apiException) {
216-
assertThat(apiException.getCode(), equalTo(HTTP_CONFLICT));
217-
}
218-
assertThat(conflictPutServlet.numGetPutResponseCalled, equalTo(1));
219-
}
220-
221-
@Test
222-
public void replaceDomainWithConflictRetry_nullUpdatedObject_noRetries()
223-
throws ApiException, NoSuchFieldException, IllegalAccessException {
224-
Domain domain = new Domain().withMetadata(createMetadata());
225-
ErrorCodePutServlet conflictPutServlet = new ErrorCodePutServlet(HTTP_CONFLICT);
226-
defineHttpPutResponse(DOMAIN_RESOURCE, UID, domain, conflictPutServlet);
227-
try {
228-
callBuilder.replaceDomainWithConflictRetry(UID, NAMESPACE, domain, () -> null);
229-
fail("Expected ApiException not thrown");
230-
} catch (ApiException apiException) {
231-
assertThat(apiException.getCode(), equalTo(HTTP_CONFLICT));
232-
}
233-
assertThat(conflictPutServlet.numGetPutResponseCalled, equalTo(1));
234-
}
235-
236-
Field callBuilderMaxRetryCount;
237-
238-
private void setMaxRetryCount(CallBuilder callBuilder, int maxRetryCount)
239-
throws IllegalAccessException, NoSuchFieldException {
240-
if (callBuilderMaxRetryCount == null) {
241-
callBuilderMaxRetryCount = CallBuilder.class.getDeclaredField("maxRetryCount");
242-
callBuilderMaxRetryCount.setAccessible(true);
243-
}
244-
callBuilderMaxRetryCount.set(callBuilder, maxRetryCount);
245-
}
246-
247117
@Test
248118
public void createPV_returnsVolumeAsJson() throws ApiException {
249119
V1PersistentVolume volume = createPersistentVolume();
@@ -341,6 +211,7 @@ private void defineHttpPutResponse(
341211
defineResource(resourceName + "/" + name, new JsonPutServlet(response, bodyValidation));
342212
}
343213

214+
@SuppressWarnings("unused")
344215
private void defineHttpPutResponse(
345216
String resourceName, String name, Object response, PseudoServlet pseudoServlet) {
346217
defineResource(resourceName + "/" + name, pseudoServlet);
@@ -395,7 +266,7 @@ static class ErrorCodePutServlet extends PseudoServlet {
395266
int numGetPutResponseCalled = 0;
396267
final int errorCode;
397268

398-
public ErrorCodePutServlet(int errorCode) {
269+
ErrorCodePutServlet(int errorCode) {
399270
this.errorCode = errorCode;
400271
}
401272

@@ -406,24 +277,6 @@ public WebResource getPutResponse() {
406277
}
407278
}
408279

409-
static class ConflictOncePutServlet extends JsonBodyServlet {
410-
411-
boolean conflictReturned;
412-
413-
private ConflictOncePutServlet(Object returnValue, Consumer<String> bodyValidation) {
414-
super(returnValue, bodyValidation);
415-
}
416-
417-
@Override
418-
public WebResource getPutResponse() throws IOException {
419-
if (!conflictReturned) {
420-
conflictReturned = true;
421-
return new WebResource("", HTTP_CONFLICT);
422-
}
423-
return getResponse();
424-
}
425-
}
426-
427280
abstract static class JsonServlet extends PseudoServlet {
428281

429282
private WebResource response;
@@ -448,6 +301,7 @@ private void validateParameters() throws IOException {
448301
if (!validationErrors.isEmpty()) throw new IOException(String.join("\n", validationErrors));
449302
}
450303

304+
@SuppressWarnings("UnusedReturnValue")
451305
JsonServlet expectingParameter(String name, String value) {
452306
parameterExpectations.add(new ParameterExpectation(name, value));
453307
return this;
@@ -516,10 +370,6 @@ public WebResource getPostResponse() throws IOException {
516370

517371
static class JsonPutServlet extends JsonBodyServlet {
518372

519-
private JsonPutServlet(Object returnValue) {
520-
this(returnValue, null);
521-
}
522-
523373
private JsonPutServlet(Object returnValue, Consumer<String> bodyValidation) {
524374
super(returnValue, bodyValidation);
525375
}

0 commit comments

Comments
 (0)