From 5174463979aadc78460391b9c97b0ec50a205956 Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Tue, 6 Feb 2024 12:07:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=203.1.0=201.=20=20=E9=87=8D=E8=AF=95?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=B5=8C=E5=A5=97=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/core/annotation/Propagation.java | 14 ++++ .../client/core/annotation/Retryable.java | 7 +- .../intercepter/EasyRetryInterceptor.java | 13 +++- .../core/intercepter/RetrySiteSnapshot.java | 64 +++++++++++++++---- 4 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/annotation/Propagation.java diff --git a/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/annotation/Propagation.java b/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/annotation/Propagation.java new file mode 100644 index 000000000..e4cc0ebaa --- /dev/null +++ b/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/annotation/Propagation.java @@ -0,0 +1,14 @@ +package com.aizuda.easy.retry.client.core.annotation; + +/** + * @author: xiaowoniu + * @date : 2024-02-05 + * @since : 3.1.0 + */ +public enum Propagation { + + REQUIRED, + REQUIRES_NEW + ; + +} diff --git a/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/annotation/Retryable.java b/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/annotation/Retryable.java index 996d80392..9be247ddf 100644 --- a/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/annotation/Retryable.java +++ b/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/annotation/Retryable.java @@ -121,6 +121,11 @@ public @interface Retryable { */ TimeUnit unit() default TimeUnit.MILLISECONDS; - + /** + * 重试传播机制 + * + * @return Propagation + */ + Propagation propagation() default Propagation.REQUIRED; } diff --git a/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/intercepter/EasyRetryInterceptor.java b/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/intercepter/EasyRetryInterceptor.java index 0cfcc425f..d137b4472 100644 --- a/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/intercepter/EasyRetryInterceptor.java +++ b/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/intercepter/EasyRetryInterceptor.java @@ -3,6 +3,7 @@ package com.aizuda.easy.retry.client.core.intercepter; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import com.aizuda.easy.retry.client.common.config.EasyRetryProperties; +import com.aizuda.easy.retry.client.core.annotation.Propagation; import com.aizuda.easy.retry.client.core.annotation.Retryable; import com.aizuda.easy.retry.client.common.cache.GroupVersionCache; import com.aizuda.easy.retry.client.core.cache.RetryerInfoCache; @@ -73,8 +74,13 @@ public class EasyRetryInterceptor implements MethodInterceptor, AfterAdvice, Ser Retryable retryable = getAnnotationParameter(invocation.getMethod()); String executorClassName = invocation.getThis().getClass().getName(); String methodEntrance = getMethodEntrance(retryable, executorClassName); - if (StrUtil.isBlank(RetrySiteSnapshot.getMethodEntrance())) { + + if (Propagation.REQUIRES_NEW.equals(retryable.propagation())) { RetrySiteSnapshot.setMethodEntrance(methodEntrance); + } else if (!RetrySiteSnapshot.existedMethodEntrance()) { + RetrySiteSnapshot.setMethodEntrance(methodEntrance); + } else { + EasyRetryLog.LOCAL.info("无需设置入口标志:[{}]", traceId); } Throwable throwable = null; @@ -112,6 +118,11 @@ public class EasyRetryInterceptor implements MethodInterceptor, AfterAdvice, Ser } } + // 无需开启重试的场景,需要清除缓存信息 + if ((RetrySiteSnapshot.isMethodEntrance(methodEntrance) && !RetrySiteSnapshot.isRunning())) { + RetrySiteSnapshot.removeAll(); + } + if (throwable != null) { throw throwable; } else { diff --git a/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/intercepter/RetrySiteSnapshot.java b/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/intercepter/RetrySiteSnapshot.java index 40408d097..1bd795b9f 100644 --- a/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/intercepter/RetrySiteSnapshot.java +++ b/easy-retry-client/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/intercepter/RetrySiteSnapshot.java @@ -1,18 +1,18 @@ package com.aizuda.easy.retry.client.core.intercepter; -import cn.hutool.core.util.StrUtil; import com.aizuda.easy.retry.client.core.RetrySiteSnapshotContext; import com.aizuda.easy.retry.client.core.exception.EasyRetryClientException; import com.aizuda.easy.retry.client.core.loader.EasyRetrySpiLoader; import com.aizuda.easy.retry.common.core.constant.SystemConstants; import com.aizuda.easy.retry.common.core.model.EasyRetryHeaders; +import lombok.AllArgsConstructor; import lombok.Getter; import java.util.Deque; import java.util.Objects; import java.util.Optional; -import java.util.Stack; import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.atomic.AtomicInteger; /** * 重试现场记录器 @@ -30,7 +30,7 @@ public class RetrySiteSnapshot { /** * 标记重试方法入口 */ - private static final RetrySiteSnapshotContext> RETRY_CLASS_METHOD_ENTRANCE = EasyRetrySpiLoader.loadRetrySiteSnapshotContext(); + private static final RetrySiteSnapshotContext> RETRY_CLASS_METHOD_ENTRANCE = EasyRetrySpiLoader.loadRetrySiteSnapshotContext(); /** * 重试状态 @@ -74,22 +74,44 @@ public class RetrySiteSnapshot { } public static String getMethodEntrance() { - Deque stack = RETRY_CLASS_METHOD_ENTRANCE.get(); - return stack.peek(); + Deque stack = RETRY_CLASS_METHOD_ENTRANCE.get(); + if (Objects.isNull(stack) || Objects.isNull(stack.peek())) { + return null; + } + + return stack.peek().methodEntrance; + } + + public static boolean existedMethodEntrance() { + Deque stack = RETRY_CLASS_METHOD_ENTRANCE.get(); + if (Objects.isNull(stack)) { + return Boolean.FALSE; + } + + MethodEntranceMeta meta = stack.peek(); + if(Objects.isNull(meta)) { + return Boolean.FALSE; + } + + return Boolean.TRUE; } public static void setMethodEntrance(String methodEntrance) { - Deque stack = RETRY_CLASS_METHOD_ENTRANCE.get(); + Deque stack = RETRY_CLASS_METHOD_ENTRANCE.get(); if (Objects.isNull(RETRY_CLASS_METHOD_ENTRANCE.get())) { stack = new LinkedBlockingDeque<>(); + RETRY_CLASS_METHOD_ENTRANCE.set(stack); } - stack.push(methodEntrance); - RETRY_CLASS_METHOD_ENTRANCE.set(stack); + MethodEntranceMeta meta; + if (!isRunning() && !isRetryFlow()) { + meta = new MethodEntranceMeta(methodEntrance); + stack.push(meta); + } } public static void removeMethodEntrance() { - Deque stack = RETRY_CLASS_METHOD_ENTRANCE.get(); + Deque stack = RETRY_CLASS_METHOD_ENTRANCE.get(); if (Objects.isNull(stack)) { return; } @@ -99,15 +121,20 @@ public class RetrySiteSnapshot { return; } - stack.pop(); + if (!isRunning() && !isRetryFlow()) { + stack.pop(); + } + } public static boolean isMethodEntrance(String methodEntrance) { - if (StrUtil.isBlank(getMethodEntrance())) { - return false; + Deque stack = RETRY_CLASS_METHOD_ENTRANCE.get(); + if (Objects.isNull(stack) || Objects.isNull(stack.peek())) { + return Boolean.FALSE; } - return getMethodEntrance().equals(methodEntrance); + MethodEntranceMeta peek = stack.peek(); + return methodEntrance.equals(peek.methodEntrance); } public static Integer getStatus() { @@ -187,12 +214,12 @@ public class RetrySiteSnapshot { public static void removeAll() { removeStatus(); - removeMethodEntrance(); removeStage(); removeAttemptNumber(); removeEntryMethodTime(); removeRetryHeader(); removeRetryStatusCode(); + removeMethodEntrance(); } /** @@ -260,4 +287,13 @@ public class RetrySiteSnapshot { } + @Getter + @AllArgsConstructor + public static class MethodEntranceMeta { + +// private AtomicInteger depth; + + private String methodEntrance; + } + }