Skip to content

Commit

Permalink
Fix SpotBug issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cizhang-amzn committed Jul 24, 2024
1 parent 52d44dc commit 8f5f3aa
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 15 deletions.
3 changes: 3 additions & 0 deletions aws-organizations-account/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>--add-opens java.base/java.util=ALL-UNNAMED</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public abstract class BaseHandlerStd extends BaseHandler<CallbackContext> {
protected static final String ACCOUNT_CREATION_STATUS_SUCCEEDED = "SUCCEEDED";
protected static final String ACCOUNT_CREATION_STATUS_FAILED = "FAILED";
// ExponentialBackoffJitter Constants
private static final Random RANDOM = new Random();
protected static final double RANDOMIZATION_FACTOR = 0.5;
protected static final double RANDOMIZATION_FACTOR_FOR_DESCRIBE_CREATE_ACCOUNT_STATUS = 0.2;
protected static final int BASE_DELAY = 15; // in second
Expand Down Expand Up @@ -162,9 +163,8 @@ public ProgressEvent<ResourceModel, CallbackContext> handleErrorTranslation(
}

public final int computeDelayBeforeNextRetry(int retryAttempt, int baseDelay, double randomizationFactor) {
Random random = new Random();
int exponentialBackoff = (int) Math.pow(2, retryAttempt) * baseDelay;
int jitter = random.nextInt((int) Math.ceil(exponentialBackoff * randomizationFactor));
int jitter = RANDOM.nextInt((int) Math.ceil(exponentialBackoff * randomizationFactor));
return exponentialBackoff + jitter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,30 @@
@lombok.Getter
@lombok.Setter
@lombok.ToString
@lombok.EqualsAndHashCode(callSuper = true)
@lombok.EqualsAndHashCode(callSuper = true, exclude = "actionToRetryAttemptMap")
public class CallbackContext extends StdCallbackContext {
private Map<String, Integer> actionToRetryAttemptMap = new HashMap<>();

// Manually implement the setter with a defensive copy
public void setActionToRetryAttemptMap(Map<String, Integer> actionToRetryAttemptMap) {
this.actionToRetryAttemptMap = new HashMap<>(actionToRetryAttemptMap);
}

// Manually implement the getter with a defensive copy
public Map<String, Integer> getActionToRetryAttemptMap() {
return new HashMap<>(actionToRetryAttemptMap);
}

public int getCurrentRetryAttempt(final AccountConstants.Action actionName, final AccountConstants.Handler handlerName) {
String key = actionName.toString() + handlerName.toString();
return this.actionToRetryAttemptMap.getOrDefault(key, 0);
}

public void setCurrentRetryAttempt(final AccountConstants.Action actionName, final AccountConstants.Handler handlerName) {
String key = actionName.toString() + handlerName.toString();
this.actionToRetryAttemptMap.put(key, getCurrentRetryAttempt(actionName, handlerName)+1);
}

// used in CREATE handler
private boolean isAccountCreated = false;
private String createAccountRequestId;
Expand Down
3 changes: 3 additions & 0 deletions aws-organizations-organization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>--add-opens java.base/java.util=ALL-UNNAMED</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Random;

public abstract class BaseHandlerStd extends BaseHandler<CallbackContext> {
private static final Random RANDOM = new Random();
private static final double RANDOMIZATION_FACTOR = 0.5;
private static final int BASE_DELAY = 15; //in seconds
private static final int MAX_RETRY_ATTEMPT_FOR_RETRIABLE_EXCEPTION = 2;
Expand Down Expand Up @@ -125,9 +126,8 @@ && isRetriableException(e)) {
}

public final int computeDelayBeforeNextRetry(int retryAttempt) {
Random random = new Random();
int exponentialBackoff = (int) Math.pow(2, retryAttempt) * BASE_DELAY;
int jitter = random.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR));
int jitter = RANDOM.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR));
return exponentialBackoff + jitter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@
@lombok.Getter
@lombok.Setter
@lombok.ToString
@lombok.EqualsAndHashCode(callSuper = true)
@lombok.EqualsAndHashCode(callSuper = true, exclude = "actionToRetryAttemptMap")
public class CallbackContext extends StdCallbackContext {
private Map<String, Integer> actionToRetryAttemptMap = new HashMap<>();

// Manually implement the setter with a defensive copy
public void setActionToRetryAttemptMap(Map<String, Integer> actionToRetryAttemptMap) {
this.actionToRetryAttemptMap = new HashMap<>(actionToRetryAttemptMap);
}

// Manually implement the getter with a defensive copy
public Map<String, Integer> getActionToRetryAttemptMap() {
return new HashMap<>(actionToRetryAttemptMap);
}

// Used to set Propagation Delay in the CreateHandler call chain.
public boolean propagationDelay = false;
// used in CREATE handler re-invoking
Expand All @@ -21,6 +31,7 @@ public int getCurrentRetryAttempt(final OrganizationConstants.Action actionName,
String key = actionName.toString() + handlerName.toString();
return this.actionToRetryAttemptMap.getOrDefault(key, 0);
}

public void setCurrentRetryAttempt(final OrganizationConstants.Action actionName, final OrganizationConstants.Handler handlerName) {
String key = actionName.toString() + handlerName.toString();
this.actionToRetryAttemptMap.put(key, getCurrentRetryAttempt(actionName, handlerName) + 1);
Expand Down
3 changes: 3 additions & 0 deletions aws-organizations-organizationalunit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>--add-opens java.base/java.util=ALL-UNNAMED</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

public abstract class BaseHandlerStd extends BaseHandler<CallbackContext> {
// ExponentialBackoffJitter Constants
private static final Random RANDOM = new Random();
private static final double RANDOMIZATION_FACTOR = 0.5;
private static final int BASE_DELAY = 15; // in seconds
private static final int MAX_RETRY_ATTEMPT_FOR_RETRIABLE_EXCEPTION = 2;
Expand Down Expand Up @@ -122,9 +123,8 @@ && isRetriableException(e)) {
}

public final int computeDelayBeforeNextRetry(int retryAttempt) {
Random random = new Random();
int exponentialBackoff = (int) Math.pow(2, retryAttempt) * BASE_DELAY;
int jitter = random.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR));
int jitter = RANDOM.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR));
return exponentialBackoff + jitter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@
@lombok.Getter
@lombok.Setter
@lombok.ToString
@lombok.EqualsAndHashCode(callSuper = true)
@lombok.EqualsAndHashCode(callSuper = true, exclude = "actionToRetryAttemptMap")
public class CallbackContext extends StdCallbackContext {
private Map<String, Integer> actionToRetryAttemptMap = new HashMap<>();

// Manually implement the setter with a defensive copy
public void setActionToRetryAttemptMap(Map<String, Integer> actionToRetryAttemptMap) {
this.actionToRetryAttemptMap = new HashMap<>(actionToRetryAttemptMap);
}

// Manually implement the getter with a defensive copy
public Map<String, Integer> getActionToRetryAttemptMap() {
return new HashMap<>(actionToRetryAttemptMap);
}

public int getCurrentRetryAttempt(final Constants.Action actionName, final Constants.Handler handlerName) {
String key = actionName.toString() + handlerName.toString();
return this.actionToRetryAttemptMap.getOrDefault(key, 0);
}

public void setCurrentRetryAttempt(final Constants.Action actionName, final Constants.Handler handlerName) {
String key = actionName.toString() + handlerName.toString();
this.actionToRetryAttemptMap.put(key, getCurrentRetryAttempt(actionName, handlerName)+1);
Expand Down
3 changes: 3 additions & 0 deletions aws-organizations-policy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>--add-opens java.base/java.util=ALL-UNNAMED</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

public abstract class BaseHandlerStd extends BaseHandler<CallbackContext> {
// ExponentialBackoffJitter Constants
private static final Random RANDOM = new Random();
private static final double RANDOMIZATION_FACTOR = 0.5;
private static final int BASE_DELAY = 15; // in seconds
private static final int MAX_RETRY_ATTEMPT_FOR_RETRIABLE_EXCEPTION = 2;
Expand Down Expand Up @@ -119,9 +120,8 @@ && isRetriableException(e)) {
}

public final int computeDelayBeforeNextRetry(int retryAttempt) {
Random random = new Random();
int exponentialBackoff = (int) Math.pow(2, retryAttempt) * BASE_DELAY;
int jitter = random.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR));
int jitter = RANDOM.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR));
return exponentialBackoff + jitter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,30 @@
@lombok.Getter
@lombok.Setter
@lombok.ToString
@lombok.EqualsAndHashCode(callSuper = true)
@lombok.EqualsAndHashCode(callSuper = true, exclude = "actionToRetryAttemptMap")
public class CallbackContext extends StdCallbackContext {
private Map<String, Integer> actionToRetryAttemptMap = new HashMap<>();

// Manually implement the setter with a defensive copy
public void setActionToRetryAttemptMap(Map<String, Integer> actionToRetryAttemptMap) {
this.actionToRetryAttemptMap = new HashMap<>(actionToRetryAttemptMap);
}

// Manually implement the getter with a defensive copy
public Map<String, Integer> getActionToRetryAttemptMap() {
return new HashMap<>(actionToRetryAttemptMap);
}

public int getCurrentRetryAttempt(final PolicyConstants.Action actionName, final PolicyConstants.Handler handlerName) {
String key = actionName.toString() + handlerName.toString();
return this.actionToRetryAttemptMap.getOrDefault(key, 0);
}

public void setCurrentRetryAttempt(final PolicyConstants.Action actionName, final PolicyConstants.Handler handlerName) {
String key = actionName.toString() + handlerName.toString();
this.actionToRetryAttemptMap.put(key, getCurrentRetryAttempt(actionName, handlerName)+1);
}

// used in CREATE handler re-invoking
private boolean isPolicyCreated = false;
// used in DELETE handler re-invoking
Expand Down
3 changes: 3 additions & 0 deletions aws-organizations-resourcepolicy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>--add-opens java.base/java.util=ALL-UNNAMED</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

public abstract class BaseHandlerStd extends BaseHandler<CallbackContext> {
// ExponentialBackoffJitter Constants
private static final Random RANDOM = new Random();
private static final double RANDOMIZATION_FACTOR = 0.5;
private static final int BASE_DELAY = 15; // in seconds
private static final int MAX_RETRY_ATTEMPT_FOR_RETRIABLE_EXCEPTION = 2;
Expand Down Expand Up @@ -114,9 +115,8 @@ && isRetriableException(e)) {
}

public final int computeDelayBeforeNextRetry(int retryAttempt) {
Random random = new Random();
int exponentialBackoff = (int) Math.pow(2, retryAttempt) * BASE_DELAY;
int jitter = random.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR));
int jitter = RANDOM.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR));
return exponentialBackoff + jitter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@
@lombok.Getter
@lombok.Setter
@lombok.ToString
@lombok.EqualsAndHashCode(callSuper = true)
@lombok.EqualsAndHashCode(callSuper = true, exclude = "actionToRetryAttemptMap")
public class CallbackContext extends StdCallbackContext {
private Map<String, Integer> actionToRetryAttemptMap = new HashMap<>();

// Manually implement the setter with a defensive copy
public void setActionToRetryAttemptMap(Map<String, Integer> actionToRetryAttemptMap) {
this.actionToRetryAttemptMap = new HashMap<>(actionToRetryAttemptMap);
}

// Manually implement the getter with a defensive copy
public Map<String, Integer> getActionToRetryAttemptMap() {
return new HashMap<>(actionToRetryAttemptMap);
}

public int getCurrentRetryAttempt(final ResourcePolicyConstants.Action actionName, final ResourcePolicyConstants.Handler handlerName) {
String key = actionName.toString() + handlerName.toString();
return this.actionToRetryAttemptMap.getOrDefault(key, 0);
}

public void setCurrentRetryAttempt(final ResourcePolicyConstants.Action actionName, final ResourcePolicyConstants.Handler handlerName) {
String key = actionName.toString() + handlerName.toString();
this.actionToRetryAttemptMap.put(key, getCurrentRetryAttempt(actionName, handlerName)+1);
Expand Down

0 comments on commit 8f5f3aa

Please sign in to comment.