Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retry: add the ability to cap the maxRetryFactor. fix left-shifts so… #96

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Run AAA SDK CI

on: [push, pull_request, workflow_dispatch]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cache local Maven repository
uses: actions/cache@v2
with:
path: $HOME/.m2
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Setup Java
uses: actions/setup-java@v1
with:
java-version: '8'
- name: Verify Maven installation
run: mvn -v
- name: Run AAA SDK tests
run: mvn -Dhere.token.endpoint.url=https://stg.account.api.here.com/oauth2/token -Dhere.access.key.id=${{ secrets.ACCESS_KEY_ID }} -Dhere.access.key.secret=${{ secrets.ACCESS_KEY_SECRET }} clean install
15 changes: 0 additions & 15 deletions .travis.yml

This file was deleted.

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[![Build Status](https://travis-ci.com/heremaps/here-aaa-java-sdk.svg?branch=master)](https://travis-ci.com/heremaps/here-aaa-java-sdk)

HERE Authentication, Authorization, and Accounting

Introduction
Expand Down Expand Up @@ -30,6 +28,7 @@ Server, for use with HERE Services.
<version>0.4.23</version>
</dependency>
```

HERE OAuth Client Examples
------
Example usage of the HERE OAuth Client library; these are tutorials intended to be adapted into or
Expand Down
4 changes: 2 additions & 2 deletions examples/here-oauth-client-example/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>here-aaa-sdk</artifactId>
<groupId>com.here.account</groupId>
<version>0.4.22</version>
<version>0.4.25-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down Expand Up @@ -71,7 +71,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<version>4.13.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down
2 changes: 1 addition & 1 deletion examples/here-oauth-client-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>com.here.account</groupId>
<artifactId>here-aaa-sdk</artifactId>
<version>0.4.23-SNAPSHOT</version>
<version>0.4.25-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion here-oauth-client-dist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>com.here.account</groupId>
<artifactId>here-aaa-sdk</artifactId>
<version>0.4.23-SNAPSHOT</version>
<version>0.4.25-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion here-oauth-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>com.here.account</groupId>
<artifactId>here-aaa-sdk</artifactId>
<version>0.4.23-SNAPSHOT</version>
<version>0.4.25-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@ public class Socket5xxExponentialRandomBackoffPolicy implements RetryPolicy {

public static final int DEFAULT_MAX_NO_RETRIES = 3;
public static final int DEFAULT_RETRY_INTERVAL_MILLIS = 1000;
public static final int DEFAULT_MAX_RETRY_FACTOR = 1 << 30;

private final int maxNumberOfRetries;
private final int retryIntervalMillis;
private final int maxRetryFactor;

public Socket5xxExponentialRandomBackoffPolicy(){
this.maxNumberOfRetries = DEFAULT_MAX_NO_RETRIES;
this.retryIntervalMillis = DEFAULT_RETRY_INTERVAL_MILLIS;
this(DEFAULT_MAX_NO_RETRIES, DEFAULT_RETRY_INTERVAL_MILLIS);
}

public Socket5xxExponentialRandomBackoffPolicy(int maxNumberOfRetries, int retryIntervalMillis){
this(maxNumberOfRetries, retryIntervalMillis, DEFAULT_MAX_RETRY_FACTOR);
}

public Socket5xxExponentialRandomBackoffPolicy(int maxNumberOfRetries, int retryIntervalMillis,
int maxRetryFactor) {
this.maxNumberOfRetries = maxNumberOfRetries;
this.retryIntervalMillis = retryIntervalMillis;
this.maxRetryFactor = maxRetryFactor;
}

@Override
Expand All @@ -34,9 +41,18 @@ public boolean shouldRetry(RetryContext retryContext) {
|| (null != retryContext.getLastException() && retryContext.getLastException().getCause() instanceof SocketTimeoutException);
}

/**
* Employs the exponential random backoff policy using a base of 2 and exponent of number of retries,
* up to a maximum, subject to the configured maxRetryFactor, and multiplied by the retryIntervalMillis.
*
* @param retryContext An instance of {@link RetryContext}
* @return the next retry interval in milliseconds
*/
@Override
public int getNextRetryIntervalMillis(RetryContext retryContext){
int factor = 1 << (retryContext.getRetryCount());
return retryIntervalMillis * ThreadLocalRandom.current().nextInt(factor);
int retryCount = retryContext.getRetryCount();
int factor = Math.min(1 << (Math.min(retryCount, 30)), maxRetryFactor);
long value = ((long) retryIntervalMillis) * ThreadLocalRandom.current().nextInt(factor);
return (int) Math.min(Integer.MAX_VALUE, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.here.account.oauth2.retry;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import java.net.SocketTimeoutException;
import com.here.account.http.HttpProvider;

import static org.junit.Assert.assertTrue;

public class Socket5xxExponentialRandomBackoffPolicyTest {

private Socket5xxExponentialRandomBackoffPolicy socket5xxExponentialRandomBackoffPolicy;
int maxNumberOfRetries = Integer.MAX_VALUE;
int retryIntervalMillis = 500;
int maxRetryFactor = 5;

@Before
public void setUp() {
this.socket5xxExponentialRandomBackoffPolicy =
new Socket5xxExponentialRandomBackoffPolicy( maxNumberOfRetries, retryIntervalMillis,
maxRetryFactor);
}

@Test
public void test_largeRetryCount_notNegative() {
for (int j = 0; j < 10; j++) {
RetryContext retryContext = new RetryContext();
for (int i = 0; i < 40; i++) {
retryContext.incrementRetryCount();
int nextRetryIntervalMillis = socket5xxExponentialRandomBackoffPolicy.getNextRetryIntervalMillis(retryContext);
assertTrue("i=" + i + ", retryContext.getRetryCount()=" + retryContext.getRetryCount() + ", nextRetryIntervalMillis was negative " + nextRetryIntervalMillis, nextRetryIntervalMillis >= 0);
}
}
}

@Test
public void test_shouldRetry() {
HttpProvider.HttpResponse httpResponse = Mockito.mock(HttpProvider.HttpResponse.class);
Mockito.when(httpResponse.getStatusCode()).thenReturn(503);
Exception lastException = Mockito.mock(SocketTimeoutException.class);
RetryContext retryContext = new RetryContext();
retryContext.setLastRetryResponse(httpResponse);
retryContext.setLastException(lastException);
final int maxRetryIntervalMillis = maxRetryFactor * retryIntervalMillis;
for (int i = 0; i < 1000; i++) {
retryContext.incrementRetryCount();
assertTrue("shouldRetry should have been true",
socket5xxExponentialRandomBackoffPolicy.shouldRetry(retryContext));
int nextRetryIntervalMillis = socket5xxExponentialRandomBackoffPolicy.getNextRetryIntervalMillis(retryContext);
assertTrue("unexpected nextRetryIntervalMillis "
+ nextRetryIntervalMillis + ", should be >= 0 and <= " + maxRetryIntervalMillis,
nextRetryIntervalMillis >= 0 && nextRetryIntervalMillis <= maxRetryIntervalMillis);
}
}
}
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<name>HERE AAA Client SDK Parent POM</name>
<groupId>com.here.account</groupId>
<artifactId>here-aaa-sdk</artifactId>
<version>0.4.23-SNAPSHOT</version>
<version>0.4.25-SNAPSHOT</version>
<packaging>pom</packaging>
<description>here-aaa-sdk is for clients of the HERE AAA, and demos grant_type=client_credentials</description>
<url>http://github.com/heremaps/here-aaa-java-sdk</url>
Expand Down Expand Up @@ -55,18 +55,18 @@
<maven-failsafe-plugin.version>2.19.1</maven-failsafe-plugin.version>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<maven-javadoc-plugin.version>2.10.4</maven-javadoc-plugin.version>
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
<maven-source-plugin.version>2.3</maven-source-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<maven-gpg-plugin.version>3.0.1</maven-gpg-plugin.version>


<maven-resources-plugin.version>3.0.1</maven-resources-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>

<!-- Declare versions for dependencies -->
<apache.httpclient.version>4.5.2</apache.httpclient.version>
<ini4j.version>0.5.1</ini4j.version>
<jackson.version>2.10.0.pr1</jackson.version>
<apache.httpclient.version>4.5.13</apache.httpclient.version>
<ini4j.version>0.5.4</ini4j.version>
<jackson.version>2.13.3</jackson.version>
<junit.version>4.13.1</junit.version>
<mockito.version>1.10.19</mockito.version>
<ning.version>1.8.17</ning.version>
Expand Down