From 989a3d0807b42aab527041fd2af867618e998414 Mon Sep 17 00:00:00 2001 From: hexiaofeng Date: Wed, 8 May 2024 16:54:01 +0800 Subject: [PATCH] review retry --- .../bytekit/context/ExecutableContext.java | 4 +- ...orSupport.java => AbstractAttributes.java} | 50 ++++++------ .../bootstrap/util/AttributeAccessor.java | 47 ----------- .../live/agent/bootstrap/util/Attributes.java | 80 +++++++++++++++++++ .../governance/context/RequestContext.java | 8 ++ .../agent/governance/context/bag/Carrier.java | 6 +- .../agent/governance/context/bag/Courier.java | 32 ++++---- .../interceptor/AbstractInterceptor.java | 13 ++- .../governance/invoke/retry/Retrier.java | 2 - .../request/AbstractServiceRequest.java | 4 +- .../agent/governance/request/Request.java | 4 +- .../response/AbstractServiceResponse.java | 4 +- .../agent/governance/response/Response.java | 4 +- .../retry/Resilience4jRetrier.java | 16 +--- .../spring/retry/SpringRetrier.java | 3 - .../spring/retry/SpringRetryPolicy.java | 24 +++--- .../mariadb/v2/request/MariadbRequest.java | 4 +- .../mariadb/v3/request/MariadbRequest.java | 4 +- .../mongodb/v4/request/MongodbRequest.java | 4 +- .../v3_0/request/PostgresqlRequest.java | 4 +- .../v3_1/request/PostgresqlRequest.java | 4 +- .../v42/request/PostgresqlRequest.java | 4 +- .../v9_4/request/PostgresqlRequest.java | 4 +- .../redis/request/JedisRequest.java | 4 +- pom.xml | 5 ++ 25 files changed, 184 insertions(+), 154 deletions(-) rename joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/{AttributeAccessorSupport.java => AbstractAttributes.java} (54%) delete mode 100644 joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/AttributeAccessor.java create mode 100644 joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/Attributes.java diff --git a/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/bytekit/context/ExecutableContext.java b/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/bytekit/context/ExecutableContext.java index 926c5d90..08dc9c1a 100644 --- a/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/bytekit/context/ExecutableContext.java +++ b/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/bytekit/context/ExecutableContext.java @@ -15,7 +15,7 @@ */ package com.jd.live.agent.bootstrap.bytekit.context; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; import lombok.Getter; import lombok.Setter; @@ -23,7 +23,7 @@ * An abstract class representing an executable context. * This class provides a structure to hold information related to an executable task or operation. */ -public abstract class ExecutableContext extends AttributeAccessorSupport { +public abstract class ExecutableContext extends AbstractAttributes { /** * The type of the executable. diff --git a/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/AttributeAccessorSupport.java b/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/AbstractAttributes.java similarity index 54% rename from joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/AttributeAccessorSupport.java rename to joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/AbstractAttributes.java index 80ebbe7f..7607b0aa 100644 --- a/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/AttributeAccessorSupport.java +++ b/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/AbstractAttributes.java @@ -15,17 +15,27 @@ */ package com.jd.live.agent.bootstrap.util; -import java.io.Serializable; import java.util.LinkedHashMap; import java.util.Map; +import java.util.function.BiConsumer; -public abstract class AttributeAccessorSupport implements AttributeAccessor, Serializable { +/** + * Provides a skeletal implementation of the {@link Attributes} interface to minimize the effort required to implement this interface. + *

+ * This abstract class implements the {@code copy} and {@code forEach} methods of the {@code Attributes} interface, relying on the concrete + * class to implement the methods for accessing and modifying the attributes. The {@code copy} method uses the {@code forEach} method + * from the provided {@code Attributes} instance to copy all attributes to the current instance. Concrete implementations of this class + * must implement the {@code getAttribute}, {@code setAttribute}, {@code removeAttribute}, and {@code hasAttribute} methods. + *

+ */ +public abstract class AbstractAttributes implements Attributes { private Map attributes; - public AttributeAccessorSupport() { + public AbstractAttributes() { } + @Override public void setAttribute(String key, Object value) { if (key != null && value != null) { if (attributes == null) { @@ -35,10 +45,14 @@ public void setAttribute(String key, Object value) { } } + @Override + @SuppressWarnings("unchecked") public T getAttribute(String key) { return key == null || attributes == null ? null : (T) attributes.get(key); } + @Override + @SuppressWarnings("unchecked") public T removeAttribute(String key) { if (key != null && attributes != null) { return (T) attributes.remove(key); @@ -46,33 +60,15 @@ public T removeAttribute(String key) { return null; } + @Override public boolean hasAttribute(String key) { - return this.attributes.containsKey(key); - } - - public String[] attributeNames() { - return this.attributes.keySet().toArray(new String[0]); - } - - public void copyAttributesFrom(AttributeAccessor source) { - String[] attributeNames = source.attributeNames(); - for (String name : attributeNames) { - this.setAttribute(name, source.getAttribute(name)); - } + return key != null && attributes.containsKey(key); } - public boolean equals(Object other) { - if (this == other) { - return true; - } else if (!(other instanceof AttributeAccessorSupport)) { - return false; - } else { - AttributeAccessorSupport that = (AttributeAccessorSupport) other; - return this.attributes.equals(that.attributes); + @Override + public void attributes(BiConsumer consumer) { + if (attributes != null && consumer != null) { + attributes.forEach(consumer); } } - - public int hashCode() { - return this.attributes.hashCode(); - } } \ No newline at end of file diff --git a/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/AttributeAccessor.java b/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/AttributeAccessor.java deleted file mode 100644 index 4423ce3a..00000000 --- a/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/AttributeAccessor.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright © ${year} ${owner} (${email}) - * - * 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. - */ -package com.jd.live.agent.bootstrap.util; - -public interface AttributeAccessor { - - /** - * Retrieves an attribute by key. - * - * @param key The key of the attribute to retrieve. - * @return The value of the attribute, or null if not found. - */ - T getAttribute(String key); - - /** - * Sets or replaces an attribute with the specified key and value. - * - * @param key The key of the attribute. - * @param value The value of the attribute. - */ - void setAttribute(String key, Object value); - - /** - * Removes an attribute by its key. - * - * @param key The key of the attribute to remove. - * @return The removed attribute, or null if the attribute was not found. - */ - T removeAttribute(String key); - - boolean hasAttribute(String key); - - String[] attributeNames(); -} \ No newline at end of file diff --git a/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/Attributes.java b/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/Attributes.java new file mode 100644 index 00000000..aa8a1474 --- /dev/null +++ b/joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/util/Attributes.java @@ -0,0 +1,80 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * 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. + */ +package com.jd.live.agent.bootstrap.util; + +import java.util.function.BiConsumer; + +/** + * Provides a contract for setting and getting attributes generically. + * This interface allows for the storage and retrieval of attributes. + */ +public interface Attributes { + + /** + * Retrieves an attribute by key. + * + * @param key The key of the attribute to retrieve. + * @return The value of the attribute, or null if not found. + */ + T getAttribute(String key); + + /** + * Sets or replaces an attribute with the specified key and value. + * + * @param key The key of the attribute. + * @param value The value of the attribute. + */ + void setAttribute(String key, Object value); + + /** + * Removes an attribute by its key. + * + * @param key The key of the attribute to remove. + * @return The removed attribute, or null if the attribute was not found. + */ + T removeAttribute(String key); + + /** + * Checks if an attribute with the specified key exists. + *

This method is used to determine whether an attribute is associated with the given key.

+ * + * @param key The key of the attribute to check. Cannot be {@code null}. + * @return {@code true} if an attribute with the specified key exists; {@code false} otherwise. + */ + boolean hasAttribute(String key); + + /** + * Performs the given action for each attribute in this instance until all attributes + * have been processed or the action throws an exception. Actions are performed in + * the order of attribute insertion when possible. + * + * @param consumer The action to be performed for each attribute + */ + void attributes(BiConsumer consumer); + + /** + * Copies all source from the provided Attributes instance into this one. + * If an attribute with the same key already exists in this instance, its value + * is replaced with the value from the provided Attributes instance. + * + * @param source the Attributes instance from which to copy source + */ + default void copyAttribute(Attributes source) { + if (source != null) { + source.attributes(this::setAttribute); + } + } +} \ No newline at end of file diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/context/RequestContext.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/context/RequestContext.java index ef0e8388..9f790adc 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/context/RequestContext.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/context/RequestContext.java @@ -170,5 +170,13 @@ public static boolean hasCargo() { Carrier carrier = CARRIER.get(); return carrier != null && carrier.getCargos() != null && !carrier.getCargos().isEmpty(); } + + /** + * Determines if the current time has exceeded a specified deadline. + */ + public static boolean isTimeout() { + Long deadline = getAttribute(Carrier.ATTRIBUTE_DEADLINE); + return deadline != null && System.currentTimeMillis() > deadline; + } } diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/context/bag/Carrier.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/context/bag/Carrier.java index 586ee7bf..658d2be3 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/context/bag/Carrier.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/context/bag/Carrier.java @@ -15,7 +15,7 @@ */ package com.jd.live.agent.governance.context.bag; -import com.jd.live.agent.bootstrap.util.AttributeAccessor; +import com.jd.live.agent.bootstrap.util.Attributes; import java.util.*; import java.util.function.BiConsumer; @@ -30,7 +30,7 @@ * iterating over cargos and attributes in various ways to suit different use cases. *

*/ -public interface Carrier extends AttributeAccessor { +public interface Carrier extends Attributes { String ATTRIBUTE_FAILOVER_UNIT = "x-live-failover-unit"; @@ -40,6 +40,8 @@ public interface Carrier extends AttributeAccessor { String ATTRIBUTE_GATEWAY = "x-live-gateway"; + String ATTRIBUTE_DEADLINE = "deadline"; + /** * Retrieves all cargos carried by this carrier. * diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/context/bag/Courier.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/context/bag/Courier.java index 8a07a675..71eefc09 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/context/bag/Courier.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/context/bag/Courier.java @@ -15,24 +15,24 @@ */ package com.jd.live.agent.governance.context.bag; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; import java.util.Collection; import java.util.HashMap; import java.util.Map; -public class Courier extends AttributeAccessorSupport implements Carrier { +public class Courier extends AbstractAttributes implements Carrier { - protected Map tags; + protected Map cargos; @Override public Collection getCargos() { - return tags == null ? null : tags.values(); + return cargos == null ? null : cargos.values(); } @Override public Cargo getCargo(String key) { - return tags == null ? null : tags.get(key); + return cargos == null ? null : cargos.get(key); } @Override @@ -40,10 +40,10 @@ public void addCargo(Cargo cargo) { if (cargo != null) { String name = cargo.getKey(); if (name != null && !name.isEmpty()) { - if (tags == null) { - tags = new HashMap<>(); + if (cargos == null) { + cargos = new HashMap<>(); } - Cargo old = tags.putIfAbsent(cargo.getKey(), cargo); + Cargo old = cargos.putIfAbsent(cargo.getKey(), cargo); if (old != null && old != cargo) { cargo.add(cargo.getValues()); } @@ -54,27 +54,27 @@ public void addCargo(Cargo cargo) { @Override public void addCargo(String key, String value) { if (key != null && !key.isEmpty()) { - if (tags == null) { - tags = new HashMap<>(); + if (cargos == null) { + cargos = new HashMap<>(); } - tags.computeIfAbsent(key, Cargo::new).add(value); + cargos.computeIfAbsent(key, Cargo::new).add(value); } } @Override public void setCargo(String key, String value) { if (key != null && !key.isEmpty()) { - if (tags == null) { - tags = new HashMap<>(); + if (cargos == null) { + cargos = new HashMap<>(); } - tags.put(key, new Cargo(key, value)); + cargos.put(key, new Cargo(key, value)); } } @Override public void removeCargo(String key) { - if (key != null && !key.isEmpty() && tags != null) { - tags.remove(key); + if (key != null && !key.isEmpty() && cargos != null) { + cargos.remove(key); } } diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/interceptor/AbstractInterceptor.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/interceptor/AbstractInterceptor.java index f92c1ea1..d0cf3a13 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/interceptor/AbstractInterceptor.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/interceptor/AbstractInterceptor.java @@ -16,8 +16,8 @@ package com.jd.live.agent.governance.interceptor; import com.jd.live.agent.bootstrap.bytekit.context.MethodContext; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor; +import com.jd.live.agent.governance.context.RequestContext; import com.jd.live.agent.governance.instance.Endpoint; import com.jd.live.agent.governance.invoke.InboundInvocation; import com.jd.live.agent.governance.invoke.InboundInvocation.GatewayInboundInvocation; @@ -39,6 +39,8 @@ import java.util.List; import java.util.function.Supplier; +import static com.jd.live.agent.governance.invoke.retry.Retrier.DEADLINE_KEY; + /** * AbstractInterceptor is the base class for all interceptors within the framework. * It provides a common context for the interception process and defines the structure @@ -184,8 +186,8 @@ protected Supplier createRetrySupplier(MethodContext ctx) { } catch (Throwable throwable) { response = createResponse(ctx.getResult(), throwable); } finally { - if (response instanceof AttributeAccessorSupport) { - ((AttributeAccessorSupport) response).copyAttributesFrom(ctx); + if (response != null) { + response.copyAttribute(ctx); } } return response; @@ -214,7 +216,10 @@ protected Object invokeWithRetry(O invocation, MethodContext ctx) { RetrierFactory retrierFactory = context.getOrDefaultRetrierFactory(retryPolicy.getType()); Retrier retrier = retrierFactory == null ? null : retrierFactory.get(retryPolicy); if (retrier != null) { - ctx.setAttribute(Retrier.DEADLINE_KEY, System.currentTimeMillis() + retryPolicy.getTimeout()); + Long timeout = retryPolicy.getTimeout(); + if (timeout != null && timeout > 0) { + RequestContext.getOrCreate().setAttribute(DEADLINE_KEY, System.currentTimeMillis() + timeout); + } return retrier.execute(retrySupplier); } } diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/retry/Retrier.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/retry/Retrier.java index 064e1b2f..0945f9fc 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/retry/Retrier.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/retry/Retrier.java @@ -27,8 +27,6 @@ */ public interface Retrier { - String DEADLINE_KEY = "deadline"; - /** * Execute retry logic * diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/AbstractServiceRequest.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/AbstractServiceRequest.java index 07207f77..1c4ff0eb 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/AbstractServiceRequest.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/AbstractServiceRequest.java @@ -15,7 +15,7 @@ */ package com.jd.live.agent.governance.request; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; import lombok.Getter; import java.util.HashSet; @@ -32,7 +32,7 @@ * @param The type of the original request object this class wraps. */ @Getter -public abstract class AbstractServiceRequest extends AttributeAccessorSupport implements ServiceRequest { +public abstract class AbstractServiceRequest extends AbstractAttributes implements ServiceRequest { /** * The original request object associated with this service request. diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/Request.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/Request.java index ed7bdd63..960ff669 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/Request.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/Request.java @@ -15,7 +15,7 @@ */ package com.jd.live.agent.governance.request; -import com.jd.live.agent.bootstrap.util.AttributeAccessor; +import com.jd.live.agent.bootstrap.util.Attributes; /** * Represents a general request interface. @@ -26,7 +26,7 @@ * * @since 1.0.0 */ -public interface Request extends AttributeAccessor { +public interface Request extends Attributes { /** * A constant key used for identifying sticky sessions in live services. diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/response/AbstractServiceResponse.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/response/AbstractServiceResponse.java index 3e303b34..72c2a6ff 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/response/AbstractServiceResponse.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/response/AbstractServiceResponse.java @@ -15,7 +15,7 @@ */ package com.jd.live.agent.governance.response; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; import lombok.Getter; /** @@ -24,7 +24,7 @@ * @since 1.0.0 */ @Getter -public abstract class AbstractServiceResponse extends AttributeAccessorSupport implements ServiceResponse { +public abstract class AbstractServiceResponse extends AbstractAttributes implements ServiceResponse { protected final T response; diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/response/Response.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/response/Response.java index a8c88dba..f15fc6d7 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/response/Response.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/response/Response.java @@ -15,14 +15,14 @@ */ package com.jd.live.agent.governance.response; -import com.jd.live.agent.bootstrap.util.AttributeAccessor; +import com.jd.live.agent.bootstrap.util.Attributes; /** * Response * * @since 1.0.0 */ -public interface Response extends AttributeAccessor { +public interface Response extends Attributes { /** * Response Code diff --git a/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-resilience4j/src/main/java/com/jd/live/agent/implement/flowcontrol/resilience4j/retry/Resilience4jRetrier.java b/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-resilience4j/src/main/java/com/jd/live/agent/implement/flowcontrol/resilience4j/retry/Resilience4jRetrier.java index db34858f..60dcdde3 100644 --- a/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-resilience4j/src/main/java/com/jd/live/agent/implement/flowcontrol/resilience4j/retry/Resilience4jRetrier.java +++ b/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-resilience4j/src/main/java/com/jd/live/agent/implement/flowcontrol/resilience4j/retry/Resilience4jRetrier.java @@ -15,6 +15,7 @@ */ package com.jd.live.agent.implement.flowcontrol.resilience4j.retry; +import com.jd.live.agent.governance.context.RequestContext; import com.jd.live.agent.governance.invoke.retry.Retrier; import com.jd.live.agent.governance.policy.service.retry.RetryPolicy; import com.jd.live.agent.governance.response.Response; @@ -41,17 +42,8 @@ public Resilience4jRetrier(RetryPolicy policy) { RetryConfig config = RetryConfig.custom() .maxAttempts(policy.getRetry() + 1) .waitDuration(Duration.ofMillis(policy.getRetryInterval())) - .retryOnResult(response -> { - Response rsp = (Response) response; - if (!rsp.hasAttribute(DEADLINE_KEY) && policy.getTimeout() > 0) { - rsp.setAttribute(DEADLINE_KEY, System.currentTimeMillis() + policy.getTimeout()); - } - if (rsp.hasAttribute(DEADLINE_KEY) && System.currentTimeMillis() > (Long) rsp.getAttribute(DEADLINE_KEY)) { - return false; - } - return policy.isRetry(((Response) response).getCode()); - }) - .retryOnException(policy::isRetry) + .retryOnResult(response -> !RequestContext.isTimeout() && policy.isRetry(((Response) response).getCode())) + .retryOnException(throwable -> !RequestContext.isTimeout() && policy.isRetry(throwable)) .failAfterMaxAttempts(true) .build(); RetryRegistry registry = RetryRegistry.of(config); @@ -63,9 +55,7 @@ public Resilience4jRetrier(RetryPolicy policy) { */ @Override public T execute(Supplier supplier) { - // TODO retry timeout return retry.executeSupplier(supplier); - } /** diff --git a/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-spring/src/main/java/com/jd/live/agent/implement/flowcontrol/spring/retry/SpringRetrier.java b/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-spring/src/main/java/com/jd/live/agent/implement/flowcontrol/spring/retry/SpringRetrier.java index 65ebbf67..00cac35e 100644 --- a/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-spring/src/main/java/com/jd/live/agent/implement/flowcontrol/spring/retry/SpringRetrier.java +++ b/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-spring/src/main/java/com/jd/live/agent/implement/flowcontrol/spring/retry/SpringRetrier.java @@ -51,9 +51,6 @@ public SpringRetrier(RetryPolicy policy) { @Override public T execute(Supplier supplier) { return retryTemplate.execute(context -> { - if (!context.hasAttribute(Retrier.DEADLINE_KEY) && policy.getTimeout() > 0) { - context.setAttribute(Retrier.DEADLINE_KEY, System.currentTimeMillis() + policy.getTimeout()); - } T response = supplier.get(); context.setAttribute(SpringRetryPolicy.RESPONSE_KEY, response); return response; diff --git a/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-spring/src/main/java/com/jd/live/agent/implement/flowcontrol/spring/retry/SpringRetryPolicy.java b/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-spring/src/main/java/com/jd/live/agent/implement/flowcontrol/spring/retry/SpringRetryPolicy.java index a2c9ccb1..036f2423 100644 --- a/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-spring/src/main/java/com/jd/live/agent/implement/flowcontrol/spring/retry/SpringRetryPolicy.java +++ b/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-spring/src/main/java/com/jd/live/agent/implement/flowcontrol/spring/retry/SpringRetryPolicy.java @@ -15,7 +15,7 @@ */ package com.jd.live.agent.implement.flowcontrol.spring.retry; -import com.jd.live.agent.governance.invoke.retry.Retrier; +import com.jd.live.agent.governance.context.RequestContext; import com.jd.live.agent.governance.policy.service.retry.RetryPolicy; import com.jd.live.agent.governance.response.Response; import org.springframework.retry.RetryContext; @@ -38,21 +38,17 @@ public SpringRetryPolicy(RetryPolicy retryPolicy) { @Override public boolean canRetry(RetryContext context) { - Throwable t = context.getLastThrowable(); - if (context.hasAttribute(Retrier.DEADLINE_KEY)) { - Long deadline = (Long) context.getAttribute(Retrier.DEADLINE_KEY); - if (System.currentTimeMillis() > deadline) { - return false; - } + if (RequestContext.isTimeout()) { + return false; } - boolean can = (t == null || retryPolicy.isRetry(t)) && context.getRetryCount() < this.getMaxAttempts(); - if (!can && context.hasAttribute(RESPONSE_KEY)) { + Throwable t = context.getLastThrowable(); + boolean result = (t == null || retryPolicy.isRetry(t)) && context.getRetryCount() < this.getMaxAttempts(); + if (result) { Response response = (Response) context.getAttribute(RESPONSE_KEY); - can = retryPolicy.isRetry(response.getCode()) && context.getRetryCount() < this.getMaxAttempts(); - } - if (!can) { - can = super.canRetry(context); + if (response != null) { + result = retryPolicy.isRetry(response.getCode()); + } } - return can; + return result; } } diff --git a/joylive-plugin/joylive-protection/joylive-protection-mariadb2/src/main/java/com/jd/live/agent/plugin/protection/mariadb/v2/request/MariadbRequest.java b/joylive-plugin/joylive-protection/joylive-protection-mariadb2/src/main/java/com/jd/live/agent/plugin/protection/mariadb/v2/request/MariadbRequest.java index 29d5c9d8..0d76a0d3 100644 --- a/joylive-plugin/joylive-protection/joylive-protection-mariadb2/src/main/java/com/jd/live/agent/plugin/protection/mariadb/v2/request/MariadbRequest.java +++ b/joylive-plugin/joylive-protection/joylive-protection-mariadb2/src/main/java/com/jd/live/agent/plugin/protection/mariadb/v2/request/MariadbRequest.java @@ -15,12 +15,12 @@ */ package com.jd.live.agent.plugin.protection.mariadb.v2.request; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; import com.jd.live.agent.governance.request.DbRequest.SQLRequest; import org.mariadb.jdbc.internal.protocol.Protocol; import org.mariadb.jdbc.util.Options; -public class MariadbRequest extends AttributeAccessorSupport implements SQLRequest { +public class MariadbRequest extends AbstractAttributes implements SQLRequest { private final Protocol protocol; private final String sql; diff --git a/joylive-plugin/joylive-protection/joylive-protection-mariadb3/src/main/java/com/jd/live/agent/plugin/protection/mariadb/v3/request/MariadbRequest.java b/joylive-plugin/joylive-protection/joylive-protection-mariadb3/src/main/java/com/jd/live/agent/plugin/protection/mariadb/v3/request/MariadbRequest.java index 63d61dae..e71771a7 100644 --- a/joylive-plugin/joylive-protection/joylive-protection-mariadb3/src/main/java/com/jd/live/agent/plugin/protection/mariadb/v3/request/MariadbRequest.java +++ b/joylive-plugin/joylive-protection/joylive-protection-mariadb3/src/main/java/com/jd/live/agent/plugin/protection/mariadb/v3/request/MariadbRequest.java @@ -15,13 +15,13 @@ */ package com.jd.live.agent.plugin.protection.mariadb.v3.request; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; import com.jd.live.agent.governance.request.DbRequest.SQLRequest; import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.impl.StandardClient; import org.mariadb.jdbc.message.ClientMessage; -public class MariadbRequest extends AttributeAccessorSupport implements SQLRequest { +public class MariadbRequest extends AbstractAttributes implements SQLRequest { private final StandardClient client; private final ClientMessage request; diff --git a/joylive-plugin/joylive-protection/joylive-protection-mongodb4/src/main/java/com/jd/live/agent/plugin/protection/mongodb/v4/request/MongodbRequest.java b/joylive-plugin/joylive-protection/joylive-protection-mongodb4/src/main/java/com/jd/live/agent/plugin/protection/mongodb/v4/request/MongodbRequest.java index 38bd498a..52356d63 100644 --- a/joylive-plugin/joylive-protection/joylive-protection-mongodb4/src/main/java/com/jd/live/agent/plugin/protection/mongodb/v4/request/MongodbRequest.java +++ b/joylive-plugin/joylive-protection/joylive-protection-mongodb4/src/main/java/com/jd/live/agent/plugin/protection/mongodb/v4/request/MongodbRequest.java @@ -15,11 +15,11 @@ */ package com.jd.live.agent.plugin.protection.mongodb.v4.request; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; import com.jd.live.agent.governance.request.DbRequest.SQLRequest; import com.mongodb.ServerAddress; -public class MongodbRequest extends AttributeAccessorSupport implements SQLRequest { +public class MongodbRequest extends AbstractAttributes implements SQLRequest { private final ServerAddress serverAddress; private final String database; diff --git a/joylive-plugin/joylive-protection/joylive-protection-opengauss3.0/src/main/java/com/jd/live/agent/plugin/protection/opengauss/v3_0/request/PostgresqlRequest.java b/joylive-plugin/joylive-protection/joylive-protection-opengauss3.0/src/main/java/com/jd/live/agent/plugin/protection/opengauss/v3_0/request/PostgresqlRequest.java index ee9366a5..8e6f7b4f 100644 --- a/joylive-plugin/joylive-protection/joylive-protection-opengauss3.0/src/main/java/com/jd/live/agent/plugin/protection/opengauss/v3_0/request/PostgresqlRequest.java +++ b/joylive-plugin/joylive-protection/joylive-protection-opengauss3.0/src/main/java/com/jd/live/agent/plugin/protection/opengauss/v3_0/request/PostgresqlRequest.java @@ -15,12 +15,12 @@ */ package com.jd.live.agent.plugin.protection.opengauss.v3_0.request; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; import com.jd.live.agent.governance.request.DbRequest.SQLRequest; import org.opengauss.core.Query; import org.opengauss.core.QueryExecutor; -public class PostgresqlRequest extends AttributeAccessorSupport implements SQLRequest { +public class PostgresqlRequest extends AbstractAttributes implements SQLRequest { private final QueryExecutor executor; private final Query query; diff --git a/joylive-plugin/joylive-protection/joylive-protection-opengauss3.1/src/main/java/com/jd/live/agent/plugin/protection/opengauss/v3_1/request/PostgresqlRequest.java b/joylive-plugin/joylive-protection/joylive-protection-opengauss3.1/src/main/java/com/jd/live/agent/plugin/protection/opengauss/v3_1/request/PostgresqlRequest.java index d4a473f3..557e9e35 100644 --- a/joylive-plugin/joylive-protection/joylive-protection-opengauss3.1/src/main/java/com/jd/live/agent/plugin/protection/opengauss/v3_1/request/PostgresqlRequest.java +++ b/joylive-plugin/joylive-protection/joylive-protection-opengauss3.1/src/main/java/com/jd/live/agent/plugin/protection/opengauss/v3_1/request/PostgresqlRequest.java @@ -15,12 +15,12 @@ */ package com.jd.live.agent.plugin.protection.opengauss.v3_1.request; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; import com.jd.live.agent.governance.request.DbRequest.SQLRequest; import org.postgresql.core.Query; import org.postgresql.core.QueryExecutor; -public class PostgresqlRequest extends AttributeAccessorSupport implements SQLRequest { +public class PostgresqlRequest extends AbstractAttributes implements SQLRequest { private final QueryExecutor executor; private final Query query; diff --git a/joylive-plugin/joylive-protection/joylive-protection-postgresql42/src/main/java/com/jd/live/agent/plugin/protection/postgresql/v42/request/PostgresqlRequest.java b/joylive-plugin/joylive-protection/joylive-protection-postgresql42/src/main/java/com/jd/live/agent/plugin/protection/postgresql/v42/request/PostgresqlRequest.java index 5f7ad03b..1082a547 100644 --- a/joylive-plugin/joylive-protection/joylive-protection-postgresql42/src/main/java/com/jd/live/agent/plugin/protection/postgresql/v42/request/PostgresqlRequest.java +++ b/joylive-plugin/joylive-protection/joylive-protection-postgresql42/src/main/java/com/jd/live/agent/plugin/protection/postgresql/v42/request/PostgresqlRequest.java @@ -15,12 +15,12 @@ */ package com.jd.live.agent.plugin.protection.postgresql.v42.request; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; import com.jd.live.agent.governance.request.DbRequest.SQLRequest; import org.postgresql.core.Query; import org.postgresql.core.QueryExecutor; -public class PostgresqlRequest extends AttributeAccessorSupport implements SQLRequest { +public class PostgresqlRequest extends AbstractAttributes implements SQLRequest { private final QueryExecutor executor; private final Query query; diff --git a/joylive-plugin/joylive-protection/joylive-protection-postgresql9.4/src/main/java/com/jd/live/agent/plugin/protection/postgresql/v9_4/request/PostgresqlRequest.java b/joylive-plugin/joylive-protection/joylive-protection-postgresql9.4/src/main/java/com/jd/live/agent/plugin/protection/postgresql/v9_4/request/PostgresqlRequest.java index 51531f68..f8e43785 100644 --- a/joylive-plugin/joylive-protection/joylive-protection-postgresql9.4/src/main/java/com/jd/live/agent/plugin/protection/postgresql/v9_4/request/PostgresqlRequest.java +++ b/joylive-plugin/joylive-protection/joylive-protection-postgresql9.4/src/main/java/com/jd/live/agent/plugin/protection/postgresql/v9_4/request/PostgresqlRequest.java @@ -15,12 +15,12 @@ */ package com.jd.live.agent.plugin.protection.postgresql.v9_4.request; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; import com.jd.live.agent.governance.request.DbRequest.SQLRequest; import org.postgresql.core.ProtocolConnection; import org.postgresql.core.Query; -public class PostgresqlRequest extends AttributeAccessorSupport implements SQLRequest { +public class PostgresqlRequest extends AbstractAttributes implements SQLRequest { private final ProtocolConnection connection; private final Query query; diff --git a/joylive-plugin/joylive-protection/joylive-protection-redis/src/main/java/com/jd/live/agent/plugin/protection/redis/request/JedisRequest.java b/joylive-plugin/joylive-protection/joylive-protection-redis/src/main/java/com/jd/live/agent/plugin/protection/redis/request/JedisRequest.java index 3292aac6..4525c6d1 100644 --- a/joylive-plugin/joylive-protection/joylive-protection-redis/src/main/java/com/jd/live/agent/plugin/protection/redis/request/JedisRequest.java +++ b/joylive-plugin/joylive-protection/joylive-protection-redis/src/main/java/com/jd/live/agent/plugin/protection/redis/request/JedisRequest.java @@ -15,12 +15,12 @@ */ package com.jd.live.agent.plugin.protection.redis.request; -import com.jd.live.agent.bootstrap.util.AttributeAccessorSupport; +import com.jd.live.agent.bootstrap.util.AbstractAttributes; import com.jd.live.agent.governance.request.DbRequest; import redis.clients.jedis.CommandArguments; import redis.clients.jedis.Connection; -public class JedisRequest extends AttributeAccessorSupport implements DbRequest.CacheRequest { +public class JedisRequest extends AbstractAttributes implements DbRequest.CacheRequest { private final Connection connection; private final CommandArguments args; diff --git a/pom.xml b/pom.xml index 6af98119..e83fa5d7 100644 --- a/pom.xml +++ b/pom.xml @@ -122,6 +122,11 @@ maven-deploy-plugin ${maven-deploy-plugin.version} + + org.apache.maven.plugins + maven-install-plugin + ${maven-install-plugin.version} + org.apache.maven.plugins maven-source-plugin