Skip to content

Commit 52b3c45

Browse files
author
Chris Wiechmann
committed
Adding new parameter retryDelay
Fixes #213
1 parent 663dbf1 commit 52b3c45

7 files changed

Lines changed: 57 additions & 14 deletions

File tree

modules/apim-adapter/assembly/conf/env.properties.sample

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,14 @@ admin_password=changeme
9898
# Best is to configure it an environment file, but you can also use a command line argument:
9999
# (-apiBasepath /fr/apim/v13/portal)
100100
# Example:
101-
# apiBasepath=/fr/apim/v13/portal
101+
# apiBasepath=/fr/apim/v13/portal
102+
103+
# You may set the retryDelay parameter if you experience quite often unexpected responses from the
104+
# API-Manager REST-API. For instance, reporting an Unknown API for an API just created, while trying
105+
# to create access for it.
106+
# This may occur more often, when you have more APIs, Applications, etc. in your API-Manager registry.
107+
# The APIM-CLI will try again such requests if the response is unexpected. As all other parameters you
108+
# can also set it as a command line parameter: -retryDelay 3000. See issue: #213 for more details.
109+
# Defaults to 1000 (1 second)
110+
# Example delay of 2 seconds:
111+
# retryDelay=2000

modules/apim-adapter/src/main/java/com/axway/apim/adapter/APIManagerAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ public void loginToAPIManager(boolean useAdminClient) throws AppException {
216216
int statusCode = httpResponse.getStatusLine().getStatusCode();
217217
if(statusCode != 303 && (statusCode < 200 || statusCode > 299)) {
218218
String response = EntityUtils.toString(httpResponse.getEntity());
219-
LOG.warn("Login failed with statusCode: " +statusCode+ " ... Try again in 1 second.");
220-
Thread.sleep(1000);
219+
LOG.warn("Login failed with statusCode: " +statusCode+ " ... Try again in "+cmd.getRetryDelay()+" milliseconds. (you may set -retryDelay <milliseconds>)");
220+
Thread.sleep(cmd.getRetryDelay());
221221
httpResponse = loginRequest.execute();
222222
statusCode = httpResponse.getStatusLine().getStatusCode();
223223
if(statusCode != 303){

modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIManagerAPIAccessAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ public APIAccess createAPIAccess(APIAccess apiAccess, AbstractEntity parentEntit
194194
String response = EntityUtils.toString(httpResponse.getEntity());
195195
if(statusCode < 200 || statusCode > 299){
196196
if((statusCode==403 || statusCode==404) && response.contains("Unknown API")) {
197-
LOG.warn("Got unexpected error: 'Unknown API' while creating API-Access ... Try again in 1 second.");
198-
Thread.sleep(1000);
197+
LOG.warn("Got unexpected error: 'Unknown API' while creating API-Access ... Try again in "+cmd.getRetryDelay()+" milliseconds. (you may set -retryDelay <milliseconds>)");
198+
Thread.sleep(cmd.getRetryDelay());
199199
httpResponse = request.execute();
200200
response = EntityUtils.toString(httpResponse.getEntity());
201201
statusCode = httpResponse.getStatusLine().getStatusCode();

modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIManagerAPIAdapter.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,8 @@ private void addBackendResourcePath(API api, APISpecification apiDefinition, boo
527527
String response = EntityUtils.toString(httpResponse.getEntity());
528528
if(statusCode != 200){
529529
if((statusCode >= 400 && statusCode<=499) && response.contains("Unknown API")) {
530-
LOG.warn("Got unexpected error: 'Unknown API' while trying to read Backend-API ... Try again in 1 second.");
531-
Thread.sleep(1000);
530+
LOG.warn("Got unexpected error: 'Unknown API' while trying to read Backend-API ... Try again in "+cmd.getRetryDelay()+" milliseconds. (you may set -retryDelay <milliseconds>)");
531+
Thread.sleep(cmd.getRetryDelay());
532532
httpResponse = request.execute();
533533
statusCode = httpResponse.getStatusLine().getStatusCode();
534534
if(statusCode != 200) {
@@ -962,8 +962,8 @@ public void upgradeAccessToNewerAPI(API apiToUpgradeAccess, API referenceAPI) th
962962
if(statusCode < 200 || statusCode > 299){
963963
String response = EntityUtils.toString(httpResponse.getEntity());
964964
if((statusCode==404 || statusCode==400)) { // Status-Code 400 is returned by 7.7-20200331 ?!
965-
LOG.warn("Got unexpected error '" + response + " ("+statusCode+")' while taking over application quota to newer API ... Try again in 1 second.");
966-
Thread.sleep(1000);
965+
LOG.warn("Got unexpected error '" + response + " ("+statusCode+")' while taking over application quota to newer API ... Try again in "+cmd.getRetryDelay()+" milliseconds. (you may set -retryDelay <milliseconds>)");
966+
Thread.sleep(cmd.getRetryDelay());
967967
httpResponse = request.execute();
968968
statusCode = httpResponse.getStatusLine().getStatusCode();
969969
if(statusCode < 200 || statusCode > 299){
@@ -1024,8 +1024,8 @@ public boolean upgradeAccessToNewerAPI(API apiToUpgradeAccess, API referenceAPI,
10241024
if(statusCode != 204){
10251025
String response = EntityUtils.toString(httpResponse.getEntity());
10261026
if((statusCode==403 || statusCode==404) && response.contains("Unknown API")) {
1027-
LOG.warn("Got unexpected error: 'Unknown API' while granting access to newer API ... Try again in 1 second.");
1028-
Thread.sleep(1000);
1027+
LOG.warn("Got unexpected error: 'Unknown API' while granting access to newer API ... Try again in "+cmd.getRetryDelay()+" milliseconds. (you may set -retryDelay <milliseconds>)");
1028+
Thread.sleep(cmd.getRetryDelay());
10291029
httpResponse = request.execute();
10301030
statusCode = httpResponse.getStatusLine().getStatusCode();
10311031
if(statusCode != 204) {
@@ -1073,8 +1073,8 @@ public void grantClientOrganization(List<Organization> grantAccessToOrgs, API ap
10731073
if(statusCode != 204){
10741074
String response = EntityUtils.toString(httpResponse.getEntity());
10751075
if((statusCode==403 || statusCode==404) && response.contains("Unknown API")) {
1076-
LOG.warn("Got unexpected error: 'Unknown API' while creating API-Access ... Try again in 1 second.");
1077-
Thread.sleep(1000);
1076+
LOG.warn("Got unexpected error: 'Unknown API' while creating API-Access ... Try again in "+cmd.getRetryDelay()+" milliseconds. (you may set -retryDelay <milliseconds>)");
1077+
Thread.sleep(cmd.getRetryDelay());
10781078
httpResponse = apiCall.execute();
10791079
statusCode = httpResponse.getStatusLine().getStatusCode();
10801080
if(statusCode != 204) {

modules/apim-adapter/src/main/java/com/axway/apim/lib/CoreCLIOptions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public Parameters getParams() throws AppException {
4343
params.setProxyPort((getValue("httpProxyPort")!=null) ? Integer.valueOf(getValue("httpProxyPort")) : null);
4444
params.setProxyUsername(getValue("httpProxyUsername"));
4545
params.setProxyPassword(getValue("httpProxyPassword"));
46+
params.setRetryDelay(getValue("retryDelay"));
47+
4648

4749
return (Parameters) params;
4850
}
@@ -147,6 +149,11 @@ public void addOptions() {
147149
option.setRequired(false);
148150
option.setArgName("true");
149151
cliOptions.addInternalOption(option);
152+
153+
option = new Option("retryDelay", true, "Retry delay in milliseconds for the some of the flaky REST-API-Manager API-Calls. See issue #213");
154+
option.setRequired(false);
155+
option.setArgName("true");
156+
cliOptions.addInternalOption(option);
150157
}
151158

152159
@Override

modules/apim-adapter/src/main/java/com/axway/apim/lib/CoreParameters.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public static Mode valueOfDefault(String key) {
103103

104104
private String proxyPassword;
105105

106+
private int retryDelay;
107+
106108
public CoreParameters() {
107109
super();
108110
CoreParameters.instance = this;
@@ -413,6 +415,28 @@ public void setProxyPassword(String proxyPassword) {
413415
this.proxyPassword = proxyPassword;
414416
}
415417

418+
public int getRetryDelay() {
419+
if(retryDelay==0) return 1000;
420+
return retryDelay;
421+
}
422+
423+
public void setRetryDelay(String retryDelay) {
424+
this.retryDelay = 1000;
425+
if(retryDelay==null || retryDelay.equals("null")) {
426+
return;
427+
}
428+
try {
429+
this.retryDelay = Integer.parseInt(retryDelay);
430+
LOG.info("Retrying unexpected API-Manager REST-API responses with a delay of " + this.retryDelay + " milliseconds.");
431+
} catch(Exception e) {
432+
LOG.error("Error while parsing given retryDelay: '"+retryDelay+"' as a milliseconds. Using default of 1000 milliseconds.");
433+
}
434+
}
435+
436+
public void setRetryDelay(int retryDelay) {
437+
this.retryDelay = retryDelay;
438+
}
439+
416440
public Boolean isZeroDowntimeUpdate() {
417441
if(zeroDowntimeUpdate==null) return false;
418442
return zeroDowntimeUpdate;

modules/apim-adapter/src/test/java/com/axway/lib/CoreCLIOptionsTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public class CoreCLIOptionsTest {
1313
@Test
1414
public void testCoreParameters() throws AppException {
15-
String[] args = {"-h", "api-env", "-u", "apiadmin", "-p", "changeme", "-port", "8888", "-apimCLIHome", "My-home-is-my-castle", "-clearCache", "ALL", "-returnCodeMapping", "10:0", "-rollback", "false", "-force", "-ignoreCache", "-ignoreAdminAccount"};
15+
String[] args = {"-h", "api-env", "-u", "apiadmin", "-p", "changeme", "-port", "8888", "-apimCLIHome", "My-home-is-my-castle", "-clearCache", "ALL", "-returnCodeMapping", "10:0", "-rollback", "false", "-force", "-ignoreCache", "-ignoreAdminAccount", "-retryDelay", "10000"};
1616
CLIOptions options = SampleCLIOptions.create(args);
1717
CoreParameters params = (CoreParameters) options.getParams();
1818

@@ -27,13 +27,15 @@ public void testCoreParameters() throws AppException {
2727
Assert.assertFalse(params.isRollback());
2828
Assert.assertTrue(params.isIgnoreCache());
2929
Assert.assertTrue(params.isIgnoreAdminAccount());
30+
Assert.assertEquals(params.getRetryDelay(), 10000);
3031
}
3132

3233
@Test
3334
public void testOldForceParameter() throws AppException {
3435
String[] args = {"-s", "api-env", "-f", "true"};
3536
CLIOptions options = SampleCLIOptions.create(args);
3637
CoreParameters params = (CoreParameters) options.getParams();
38+
Assert.assertEquals(params.getRetryDelay(), 1000, "Should default be 1 second");
3739
Assert.assertTrue(params.isForce());
3840
}
3941

0 commit comments

Comments
 (0)