Skip to content

Commit

Permalink
Improve the adaptation of retry policy
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhiguo committed May 7, 2024
1 parent 39b94ab commit b783d8a
Show file tree
Hide file tree
Showing 34 changed files with 1,152 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@
import com.jd.live.agent.governance.invoke.OutboundInvocation;
import com.jd.live.agent.governance.invoke.OutboundInvocation.HttpOutboundInvocation;
import com.jd.live.agent.governance.invoke.filter.*;
import com.jd.live.agent.governance.invoke.retry.Retrier;
import com.jd.live.agent.governance.invoke.retry.RetrierFactory;
import com.jd.live.agent.governance.policy.service.ServicePolicy;
import com.jd.live.agent.governance.policy.service.retry.RetryPolicy;
import com.jd.live.agent.governance.request.HttpRequest.HttpInboundRequest;
import com.jd.live.agent.governance.request.HttpRequest.HttpOutboundRequest;
import com.jd.live.agent.governance.request.ServiceRequest.InboundRequest;
import com.jd.live.agent.governance.request.ServiceRequest.OutboundRequest;
import com.jd.live.agent.governance.response.Response;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

/**
* AbstractInterceptor is the base class for all interceptors within the framework.
Expand Down Expand Up @@ -117,15 +125,18 @@ public static abstract class AbstractOutboundInterceptor<R extends OutboundReque
*/
protected final OutboundFilter[] outboundFilters;

protected final Map<String, RetrierFactory> retrierFactories;

/**
* Constructs a new AbstractOutboundInterceptor with the given InvocationContext and outbound filters.
*
* @param context the InvocationContext for the current invocation
* @param filters a list of OutboundFilter instances to be applied to the outbound request
* @param context the InvocationContext for the current invocation
* @param filters a list of OutboundFilter instances to be applied to the outbound request
*/
public AbstractOutboundInterceptor(InvocationContext context, List<OutboundFilter> filters) {
public AbstractOutboundInterceptor(InvocationContext context, List<OutboundFilter> filters, Map<String, RetrierFactory> retrierFactories) {
super(context);
this.outboundFilters = filters == null ? new OutboundFilter[0] : filters.toArray(new OutboundFilter[0]);
this.retrierFactories = retrierFactories;
}

/**
Expand Down Expand Up @@ -157,6 +168,30 @@ protected O process(R request) {
*/
protected abstract void process(O invocation);

/**
* Create a retry supplier
*
* @param target The object on which the method is called.
* @param method The method being called.
* @param allArguments All parameters of the method.
* @param result The result of the method call.
* @return Returns a supplier for retry logic.
*/
protected abstract Supplier<Response> createRetrySupplier(Object target, Method method, Object[] allArguments, Object result);

protected Response tryRetry(O invocation, Response response, Supplier<Response> retrySupplier) {
ServicePolicy servicePolicy = invocation == null ? null : invocation.getServiceMetadata().getServicePolicy();
RetryPolicy retryPolicy = servicePolicy == null ? null : servicePolicy.getRetryPolicy();
if (retryPolicy != null && retrierFactories != null) {
RetrierFactory retrierFactory = retrierFactories.get(retryPolicy.getType());
Retrier retrier = retrierFactory == null ? null : retrierFactory.get(retryPolicy);
if (retrier != null && retrier.isRetryable(response)) {
return retrier.execute(retrySupplier);
}
}
return null;
}

}

/**
Expand All @@ -176,8 +211,8 @@ public static abstract class AbstractRouteInterceptor<R extends OutboundRequest,
/**
* Constructs a new AbstractRouteInterceptor with the given InvocationContext and route filters.
*
* @param context the InvocationContext for the current invocation
* @param filters a list of RouteFilter instances to be applied during routing
* @param context the InvocationContext for the current invocation
* @param filters a list of RouteFilter instances to be applied during routing
*/
public AbstractRouteInterceptor(InvocationContext context, List<RouteFilter> filters) {
super(context);
Expand All @@ -199,8 +234,8 @@ protected O routing(R request) {
/**
* Initiates the routing process for the outbound request by creating an OutboundInvocation and providing a list of candidate endpoints.
*
* @param request the outbound request to be routed
* @param instances a list of Endpoint instances to be considered during routing
* @param request the outbound request to be routed
* @param instances a list of Endpoint instances to be considered during routing
* @return the resulting OutboundInvocation after initiating the routing process
*/
protected O routing(R request, List<? extends Endpoint> instances) {
Expand Down Expand Up @@ -230,8 +265,8 @@ protected O routing(R request, List<? extends Endpoint> instances) {
* Creates a new OutboundInvocation for the given outbound request and initializes it with a list of Endpoint instances.
* This method can be overridden by concrete subclasses if additional initialization is required.
*
* @param request the outbound request for which to create an OutboundInvocation
* @param instances a list of Endpoint instances to be associated with the OutboundInvocation
* @param request the outbound request for which to create an OutboundInvocation
* @param instances a list of Endpoint instances to be associated with the OutboundInvocation
* @return a new OutboundInvocation instance with the specified Endpoint instances
*/
protected O createOutlet(R request, List<? extends Endpoint> instances) {
Expand All @@ -252,8 +287,8 @@ public static abstract class AbstractHttpInboundInterceptor<T extends HttpInboun
/**
* Constructs a new AbstractHttpInboundInterceptor with the given InvocationContext and a list of inbound filters.
*
* @param context the InvocationContext for the current invocation
* @param filters a list of InboundFilter instances to be applied to the inbound request
* @param context the InvocationContext for the current invocation
* @param filters a list of InboundFilter instances to be applied to the inbound request
*/
public AbstractHttpInboundInterceptor(InvocationContext context, List<InboundFilter> filters) {
super(context, filters);
Expand Down Expand Up @@ -283,11 +318,11 @@ public static abstract class AbstractHttpOutboundInterceptor<T extends HttpOutbo
* Constructs a new instance of AbstractHttpOutboundInterceptor with the specified InvocationContext and a list
* of outbound filters.
*
* @param context The InvocationContext associated with the current invocation.
* @param filters The list of OutboundFilter instances that will be applied to the outbound request.
* @param context The InvocationContext associated with the current invocation.
* @param filters The list of OutboundFilter instances that will be applied to the outbound request.
*/
public AbstractHttpOutboundInterceptor(InvocationContext context, List<OutboundFilter> filters) {
super(context, filters);
public AbstractHttpOutboundInterceptor(InvocationContext context, List<OutboundFilter> filters, Map<String, RetrierFactory> retrierFactories) {
super(context, filters, retrierFactories);
}

@Override
Expand Down Expand Up @@ -368,9 +403,9 @@ public static abstract class AbstractGatewayInterceptor<I extends HttpInboundReq
/**
* Constructs a new AbstractGatewayInterceptor with the given InvocationContext and lists of inbound and route filters.
*
* @param context The InvocationContext for the current invocation.
* @param inboundFilters The list of InboundFilter instances to be applied to inbound requests.
* @param routeFilters The list of RouteFilter instances to be applied to the routing process.
* @param context The InvocationContext for the current invocation.
* @param inboundFilters The list of InboundFilter instances to be applied to inbound requests.
* @param routeFilters The list of RouteFilter instances to be applied to the routing process.
*/
public AbstractGatewayInterceptor(InvocationContext context, List<InboundFilter> inboundFilters, List<RouteFilter> routeFilters) {
super(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.jd.live.agent.governance.invoke.retry;

import com.jd.live.agent.governance.policy.service.retry.RetryPolicy;
import com.jd.live.agent.governance.response.Response;

import java.util.function.Supplier;

Expand All @@ -26,14 +27,22 @@
*/
public interface Retrier {

/**
* Determine whether to retry
*
* @param response Response
* @return true: retry, false: no need to retry
*/
boolean isRetryable(Response response);

/**
* Execute retry logic
*
* @param supplier Retry logic
* @param <T> Response type
* @return Response
*/
<T> T execute(Supplier<T> supplier);
<T extends Response> T execute(Supplier<T> supplier);

/**
* Get failover policy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ protected Map<String, String> supplementTag() {
/**
* Supplements the existing tag map with additional key-value pairs.
*
* <p>This method takes a variable number of string arguments representing key-value pairs of tags to be added to the current tag map. It creates a new map containing all the entries of the original tag map, if it exists, and then adds the new key-value pairs to it.</p>
* <p>This method takes a variable number of string arguments representing key-value pairs of tags to be added to the current tag map.
* It creates a new map containing all the entries of the original tag map, if it exists,
* and then adds the new key-value pairs to it.</p>
*
* <p>If the number of key-value pairs provided is not an even number, the last provided value will be ignored.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public class RetryPolicy extends PolicyId implements PolicyInheritWithId<RetryPo
*/
private Long id;

/**
* Retryer implementation type, default is Resilience4j.
*/
private String type = "Resilience4j";

/**
* The number of retry attempts that should be made in case of a failure. This parameter allows the system
* to attempt to recover from transient failures by retrying the failed operation.
Expand All @@ -65,7 +70,7 @@ public class RetryPolicy extends PolicyId implements PolicyInheritWithId<RetryPo
/**
* Collection of retry status codes. This parameter specifies which status codes should be considered retryable.
*/
private Set<Integer> retryableStatusCodes = new HashSet<>(Arrays.asList(500, 502, 503));
private Set<String> retryableStatusCodes = new HashSet<>(Arrays.asList("500", "502", "503"));

/**
* A collection of retryable exception class names.
Expand All @@ -82,6 +87,9 @@ public void supplement(RetryPolicy source) {
if (source == null) {
return;
}
if (type == null) {
type = source.type;
}
if (retry == null) {
retry = source.retry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ interface HttpInboundRequest extends HttpRequest, InboundRequest {
* This interface represents HTTP requests that are sent by a service.
* </p>
*
* @author Zhiguo.Chen
* @since 1.0.0
*/
interface HttpOutboundRequest extends HttpRequest, OutboundRequest {
Expand Down
Loading

0 comments on commit b783d8a

Please sign in to comment.