Skip to content

Commit

Permalink
Fixed SonarCloud findings (ReactiveX#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobWin authored Apr 12, 2019
1 parent 0b563c3 commit 033f5c9
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,22 @@
package io.github.resilience4j.circuitbreaker.internal;


import static io.github.resilience4j.circuitbreaker.CircuitBreaker.State.CLOSED;
import static io.github.resilience4j.circuitbreaker.CircuitBreaker.State.DISABLED;
import static io.github.resilience4j.circuitbreaker.CircuitBreaker.State.FORCED_OPEN;
import static io.github.resilience4j.circuitbreaker.CircuitBreaker.State.HALF_OPEN;
import static io.github.resilience4j.circuitbreaker.CircuitBreaker.State.OPEN;
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.circuitbreaker.event.*;
import io.github.resilience4j.core.EventConsumer;
import io.github.resilience4j.core.EventProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent;
import io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnCallNotPermittedEvent;
import io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnErrorEvent;
import io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnIgnoredErrorEvent;
import io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnResetEvent;
import io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnStateTransitionEvent;
import io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnSuccessEvent;
import io.github.resilience4j.core.EventConsumer;
import io.github.resilience4j.core.EventProcessor;
import static io.github.resilience4j.circuitbreaker.CircuitBreaker.State.*;

/**
* A CircuitBreaker finite state machine.
Expand Down Expand Up @@ -189,7 +178,7 @@ public void reset() {
publishResetEvent();
}

private void stateTransition(State newState, Function<CircuitBreakerState, CircuitBreakerState> newStateGenerator) {
private void stateTransition(State newState, UnaryOperator<CircuitBreakerState> newStateGenerator) {
CircuitBreakerState previousState = stateReference.getAndUpdate(currentState -> {
if (currentState.getState() == newState) {
return currentState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
*/
package io.github.resilience4j.bulkhead.configure;

import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;

import io.github.resilience4j.bulkhead.BulkheadRegistry;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.core.lang.Nullable;
import io.github.resilience4j.utils.AnnotationExtractor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
Expand All @@ -30,9 +29,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;

import io.github.resilience4j.bulkhead.BulkheadRegistry;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.utils.AnnotationExtractor;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;

/**
* This Spring AOP aspect intercepts all methods which are annotated with a {@link Bulkhead} annotation.
Expand All @@ -46,7 +46,7 @@ public class BulkheadAspect implements Ordered {

private final BulkheadConfigurationProperties bulkheadConfigurationProperties;
private final BulkheadRegistry bulkheadRegistry;
private final List<BulkheadAspectExt> bulkheadAspectExts;
private final @Nullable List<BulkheadAspectExt> bulkheadAspectExts;

public BulkheadAspect(BulkheadConfigurationProperties backendMonitorPropertiesRegistry, BulkheadRegistry bulkheadRegistry, @Autowired(required = false) List<BulkheadAspectExt> bulkheadAspectExts) {
this.bulkheadConfigurationProperties = backendMonitorPropertiesRegistry;
Expand All @@ -59,12 +59,15 @@ public void matchAnnotatedClassOrMethod(Bulkhead Bulkhead) {
}

@Around(value = "matchAnnotatedClassOrMethod(backendMonitored)", argNames = "proceedingJoinPoint, backendMonitored")
public Object bulkheadAroundAdvice(ProceedingJoinPoint proceedingJoinPoint, Bulkhead backendMonitored) throws Throwable {
public Object bulkheadAroundAdvice(ProceedingJoinPoint proceedingJoinPoint, @Nullable Bulkhead backendMonitored) throws Throwable {
Method method = ((MethodSignature) proceedingJoinPoint.getSignature()).getMethod();
String methodName = method.getDeclaringClass().getName() + "#" + method.getName();
if (backendMonitored == null) {
backendMonitored = getBackendMonitoredAnnotation(proceedingJoinPoint);
}
if(backendMonitored == null) { //because annotations wasn't found
return proceedingJoinPoint.proceed();
}
String backend = backendMonitored.name();
io.github.resilience4j.bulkhead.Bulkhead bulkhead = getOrCreateBulkhead(methodName, backend);
Class<?> returnType = method.getReturnType();
Expand Down Expand Up @@ -94,6 +97,7 @@ private io.github.resilience4j.bulkhead.Bulkhead getOrCreateBulkhead(String meth
return bulkhead;
}

@Nullable
private Bulkhead getBackendMonitoredAnnotation(ProceedingJoinPoint proceedingJoinPoint) {
if (logger.isDebugEnabled()) {
logger.debug("bulkhead parameter is null");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
*
* Copyright 2018: Clint Checketts
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
@NonNullApi
@NonNullFields
package io.github.resilience4j.bulkhead.configure;

import io.github.resilience4j.core.lang.NonNullApi;
import io.github.resilience4j.core.lang.NonNullFields;
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
*/
package io.github.resilience4j.circuitbreaker.configure;

import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.core.lang.Nullable;
import io.github.resilience4j.utils.AnnotationExtractor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
Expand All @@ -31,10 +30,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;

import io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.utils.AnnotationExtractor;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

/**
* This Spring AOP aspect intercepts all methods which are annotated with a {@link CircuitBreaker} annotation.
Expand All @@ -48,7 +47,7 @@ public class CircuitBreakerAspect implements Ordered {

private final CircuitBreakerConfigurationProperties circuitBreakerProperties;
private final CircuitBreakerRegistry circuitBreakerRegistry;
private final List<CircuitBreakerAspectExt> circuitBreakerAspectExtList;
private final @Nullable List<CircuitBreakerAspectExt> circuitBreakerAspectExtList;

public CircuitBreakerAspect(CircuitBreakerConfigurationProperties backendMonitorPropertiesRegistry, CircuitBreakerRegistry circuitBreakerRegistry, @Autowired(required = false) List<CircuitBreakerAspectExt> circuitBreakerAspectExtList) {
this.circuitBreakerProperties = backendMonitorPropertiesRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package io.github.resilience4j.ratelimiter.configure;

import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;

import io.github.resilience4j.core.lang.Nullable;
import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import io.github.resilience4j.utils.AnnotationExtractor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
Expand All @@ -30,10 +30,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;

import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import io.github.resilience4j.utils.AnnotationExtractor;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;

/**
* This Spring AOP aspect intercepts all methods which are annotated with a {@link RateLimiter} annotation.
Expand All @@ -47,7 +47,7 @@ public class RateLimiterAspect implements Ordered {
private static final Logger logger = LoggerFactory.getLogger(RateLimiterAspect.class);
private final RateLimiterRegistry rateLimiterRegistry;
private final RateLimiterConfigurationProperties properties;
private final List<RateLimiterAspectExt> rateLimiterAspectExtList;
private final @Nullable List<RateLimiterAspectExt> rateLimiterAspectExtList;

public RateLimiterAspect(RateLimiterRegistry rateLimiterRegistry, RateLimiterConfigurationProperties properties, @Autowired(required = false) List<RateLimiterAspectExt> rateLimiterAspectExtList) {
this.rateLimiterRegistry = rateLimiterRegistry;
Expand All @@ -66,7 +66,7 @@ public void matchAnnotatedClassOrMethod(RateLimiter rateLimiter) {
}

@Around(value = "matchAnnotatedClassOrMethod(limitedService)", argNames = "proceedingJoinPoint, limitedService")
public Object rateLimiterAroundAdvice(ProceedingJoinPoint proceedingJoinPoint, RateLimiter limitedService) throws Throwable {
public Object rateLimiterAroundAdvice(ProceedingJoinPoint proceedingJoinPoint, @Nullable RateLimiter limitedService) throws Throwable {
RateLimiter targetService = limitedService;
Method method = ((MethodSignature) proceedingJoinPoint.getSignature()).getMethod();
String methodName = method.getDeclaringClass().getName() + "#" + method.getName();
Expand Down Expand Up @@ -104,6 +104,7 @@ private io.github.resilience4j.ratelimiter.RateLimiter getOrCreateRateLimiter(St
return rateLimiter;
}

@Nullable
private RateLimiter getRateLimiterAnnotation(ProceedingJoinPoint proceedingJoinPoint) {
return AnnotationExtractor.extract(proceedingJoinPoint.getTarget().getClass(), RateLimiter.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package io.github.resilience4j.retry.configure;

import io.github.resilience4j.core.lang.Nullable;
import io.github.resilience4j.retry.RetryRegistry;
import io.github.resilience4j.retry.annotation.Retry;
import io.github.resilience4j.utils.AnnotationExtractor;
import io.vavr.CheckedFunction0;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
Expand Down Expand Up @@ -44,7 +46,7 @@ public class RetryAspect implements Ordered {
private final static ScheduledExecutorService retryExecutorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
private final RetryConfigurationProperties retryConfigurationProperties;
private final RetryRegistry retryRegistry;
private final List<RetryAspectExt> retryAspectExtList;
private final @Nullable List<RetryAspectExt> retryAspectExtList;

/**
* @param retryConfigurationProperties spring retry config properties
Expand All @@ -64,7 +66,7 @@ public void matchAnnotatedClassOrMethod(Retry retry) {
}

@Around(value = "matchAnnotatedClassOrMethod(backendMonitored)", argNames = "proceedingJoinPoint, backendMonitored")
public Object retryAroundAdvice(ProceedingJoinPoint proceedingJoinPoint, Retry backendMonitored) throws Throwable {
public Object retryAroundAdvice(ProceedingJoinPoint proceedingJoinPoint, @Nullable Retry backendMonitored) throws Throwable {
Method method = ((MethodSignature) proceedingJoinPoint.getSignature()).getMethod();
String methodName = method.getDeclaringClass().getName() + "#" + method.getName();
if (backendMonitored == null) {
Expand Down Expand Up @@ -109,23 +111,9 @@ private io.github.resilience4j.retry.Retry getOrCreateRetry(String methodName, S
* @param proceedingJoinPoint the aspect joint point
* @return the retry annotation
*/
@Nullable
private Retry getBackendMonitoredAnnotation(ProceedingJoinPoint proceedingJoinPoint) {
if (logger.isDebugEnabled()) {
logger.debug("circuitBreaker parameter is null");
}
Retry retry = null;
Class<?> targetClass = proceedingJoinPoint.getTarget().getClass();
if (targetClass.isAnnotationPresent(Retry.class)) {
retry = targetClass.getAnnotation(Retry.class);
if (retry == null && logger.isDebugEnabled()) {
logger.debug("TargetClass has no annotation 'Retry'");
retry = targetClass.getDeclaredAnnotation(Retry.class);
if (retry == null && logger.isDebugEnabled()) {
logger.debug("TargetClass has no declared annotation 'Retry'");
}
}
}
return retry;
return AnnotationExtractor.extract(proceedingJoinPoint.getTarget().getClass(), Retry.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
*
* Copyright 2018: Clint Checketts
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
@NonNullApi
@NonNullFields
package io.github.resilience4j.retry.configure;

import io.github.resilience4j.core.lang.NonNullApi;
import io.github.resilience4j.core.lang.NonNullFields;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.resilience4j.utils;

import io.github.resilience4j.core.lang.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -18,6 +19,7 @@ private AnnotationExtractor() {
* @param annotationClass annotation class
* @return annotation
*/
@Nullable
public static <T extends Annotation> T extract(Class<?> targetClass, Class<T> annotationClass) {
T annotation = null;
if (targetClass.isAnnotationPresent(annotationClass)) {
Expand All @@ -30,7 +32,6 @@ public static <T extends Annotation> T extract(Class<?> targetClass, Class<T> an
}
}
}

return annotation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
*/
package io.github.resilience4j.circuitbreaker;

import java.time.Duration;
import java.util.function.Supplier;

import io.github.resilience4j.core.StopWatch;
import io.vertx.core.Future;

import java.util.function.Supplier;

/**
* CircuitBreaker decorators for Vert.x
*/
Expand Down Expand Up @@ -71,10 +69,10 @@ static <T> Supplier<Future<T>> decorateFuture(CircuitBreaker circuitBreaker, Sup
future.complete(result.result());
}
});
} catch (Throwable throwable) {
} catch (Exception exception) {
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onError(durationInNanos, throwable);
future.fail(throwable);
circuitBreaker.onError(durationInNanos, exception);
future.fail(exception);
}
}
return future;
Expand Down

0 comments on commit 033f5c9

Please sign in to comment.