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 03705acd..c59413b3 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 @@ -1,6 +1,21 @@ +/* + * 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.governance.invoke.retry; -import com.jd.live.agent.governance.policy.service.failover.FailoverPolicy; +import com.jd.live.agent.governance.policy.service.retry.RetryPolicy; import java.util.function.Supplier; @@ -25,5 +40,5 @@ public interface Retrier { * * @return policy */ - FailoverPolicy getPolicy(); + RetryPolicy getPolicy(); } diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/retry/RetrierFactory.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/retry/RetrierFactory.java index be1760c8..aaafd73e 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/retry/RetrierFactory.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/retry/RetrierFactory.java @@ -1,7 +1,22 @@ +/* + * 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.governance.invoke.retry; import com.jd.live.agent.core.extension.annotation.Extensible; -import com.jd.live.agent.governance.policy.service.failover.FailoverPolicy; +import com.jd.live.agent.governance.policy.service.retry.RetryPolicy; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -17,7 +32,7 @@ public interface RetrierFactory { Map> RETRIERS = new ConcurrentHashMap<>(); - default Retrier get(FailoverPolicy policy) { + default Retrier get(RetryPolicy policy) { if (policy == null) { return null; } @@ -42,9 +57,9 @@ default Retrier get(FailoverPolicy policy) { /** * Create Retrier * - * @param failoverPolicy Failure retry policy + * @param retryPolicy Failure retry policy * @return Retrier */ - Retrier create(FailoverPolicy failoverPolicy); + Retrier create(RetryPolicy retryPolicy); } diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/service/ServicePolicy.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/service/ServicePolicy.java index 1a86935a..cc944f51 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/service/ServicePolicy.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/service/ServicePolicy.java @@ -21,7 +21,7 @@ import com.jd.live.agent.governance.policy.PolicyId; import com.jd.live.agent.governance.policy.PolicyInherit; import com.jd.live.agent.governance.policy.PolicyInherit.PolicyInheritWithIdGen; -import com.jd.live.agent.governance.policy.service.failover.FailoverPolicy; +import com.jd.live.agent.governance.policy.service.retry.RetryPolicy; import com.jd.live.agent.governance.policy.service.lane.LanePolicy; import com.jd.live.agent.governance.policy.service.limit.ConcurrencyLimitPolicy; import com.jd.live.agent.governance.policy.service.limit.RateLimitPolicy; @@ -51,7 +51,7 @@ public class ServicePolicy extends PolicyId implements Cloneable, PolicyInheritW @Setter @Getter - private FailoverPolicy failoverPolicy; + private RetryPolicy retryPolicy; @Setter @Getter @@ -83,8 +83,8 @@ public void supplement(ServicePolicy source) { if (loadBalancePolicy != null && loadBalancePolicy.getId() == null) { loadBalancePolicy.setId(id); } - if (failoverPolicy != null && failoverPolicy.getId() == null) { - failoverPolicy.setId(id); + if (retryPolicy != null && retryPolicy.getId() == null) { + retryPolicy.setId(id); } if (livePolicy != null && livePolicy.getId() == null) { livePolicy.setId(id); @@ -107,7 +107,7 @@ public void supplement(ServicePolicy source) { } if (source != null) { livePolicy = copy(source.livePolicy, livePolicy, s -> new ServiceLivePolicy()); - failoverPolicy = copy(source.failoverPolicy, failoverPolicy, s -> new FailoverPolicy()); + retryPolicy = copy(source.retryPolicy, retryPolicy, s -> new RetryPolicy()); loadBalancePolicy = copy(source.loadBalancePolicy, loadBalancePolicy, s -> new LoadBalancePolicy()); if ((rateLimitPolicies == null || rateLimitPolicies.isEmpty()) && diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/service/failover/FailoverPolicy.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/service/retry/RetryPolicy.java similarity index 86% rename from joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/service/failover/FailoverPolicy.java rename to joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/service/retry/RetryPolicy.java index 20818ac4..a246c1d6 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/service/failover/FailoverPolicy.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/policy/service/retry/RetryPolicy.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.jd.live.agent.governance.policy.service.failover; +package com.jd.live.agent.governance.policy.service.retry; import com.jd.live.agent.governance.policy.PolicyId; import com.jd.live.agent.governance.policy.PolicyInherit.PolicyInheritWithId; @@ -41,7 +41,7 @@ @Getter @Setter @Consumer -public class FailoverPolicy extends PolicyId implements PolicyInheritWithId { +public class RetryPolicy extends PolicyId implements PolicyInheritWithId { /** * The unique identifier of the failover policy. This ID can be used to reference and manage the policy @@ -67,13 +67,18 @@ public class FailoverPolicy extends PolicyId implements PolicyInheritWithId retryableStatusCodes = new HashSet<>(Arrays.asList(500, 502, 503)); + /** + * A collection of retryable exception class names. + */ + private Set exceptionClassNames; + /** * The version of the policy. */ private long version; @Override - public void supplement(FailoverPolicy source) { + public void supplement(RetryPolicy source) { if (source == null) { return; } @@ -86,6 +91,9 @@ public void supplement(FailoverPolicy source) { if ((retryableStatusCodes == null || retryableStatusCodes.isEmpty()) && source.retryableStatusCodes != null) { retryableStatusCodes = source.retryableStatusCodes; } + if ((exceptionClassNames == null || exceptionClassNames.isEmpty()) && source.exceptionClassNames != null) { + exceptionClassNames = source.exceptionClassNames; + } if (version <= 0) { version = source.version; } 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 4789b3f4..52cf1c35 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 @@ -1,7 +1,22 @@ +/* + * 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.implement.flowcontrol.resilience4j.retry; import com.jd.live.agent.governance.invoke.retry.Retrier; -import com.jd.live.agent.governance.policy.service.failover.FailoverPolicy; +import com.jd.live.agent.governance.policy.service.retry.RetryPolicy; import io.github.resilience4j.retry.Retry; import io.github.resilience4j.retry.RetryConfig; import io.github.resilience4j.retry.RetryRegistry; @@ -16,17 +31,19 @@ */ public class Resilience4jRetrier implements Retrier { - private final FailoverPolicy policy; + private final RetryPolicy policy; private final Retry retry; - public Resilience4jRetrier(FailoverPolicy policy) { + public Resilience4jRetrier(RetryPolicy policy) { this.policy = policy; RetryConfig config = RetryConfig.custom() .maxAttempts(policy.getRetry()) .waitDuration(Duration.ofMillis(policy.getTimeoutInMilliseconds())) // TODO // .retryOnResult(response -> response.getStatus() == 500) + .retryOnException(throwable -> policy.getExceptionClassNames() != null + && policy.getExceptionClassNames().contains(throwable.getClass().getCanonicalName())) .failAfterMaxAttempts(true) .build(); RetryRegistry registry = RetryRegistry.of(config); @@ -45,7 +62,7 @@ public T execute(Supplier supplier) { * {@inheritDoc} */ @Override - public FailoverPolicy getPolicy() { + public RetryPolicy getPolicy() { return policy; } } diff --git a/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-resilience4j/src/main/java/com/jd/live/agent/implement/flowcontrol/resilience4j/retry/Resilience4jRetrierFactory.java b/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-resilience4j/src/main/java/com/jd/live/agent/implement/flowcontrol/resilience4j/retry/Resilience4jRetrierFactory.java index 2fe6963a..9ae2b5e4 100644 --- a/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-resilience4j/src/main/java/com/jd/live/agent/implement/flowcontrol/resilience4j/retry/Resilience4jRetrierFactory.java +++ b/joylive-implement/joylive-flowcontrol/joylive-flowcontrol-resilience4j/src/main/java/com/jd/live/agent/implement/flowcontrol/resilience4j/retry/Resilience4jRetrierFactory.java @@ -1,8 +1,23 @@ +/* + * 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.implement.flowcontrol.resilience4j.retry; 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.failover.FailoverPolicy; +import com.jd.live.agent.governance.policy.service.retry.RetryPolicy; /** * Resilience4jRetrierFactory @@ -12,7 +27,7 @@ public class Resilience4jRetrierFactory implements RetrierFactory { @Override - public Retrier create(FailoverPolicy failoverPolicy) { - return new Resilience4jRetrier(failoverPolicy); + public Retrier create(RetryPolicy retryPolicy) { + return new Resilience4jRetrier(retryPolicy); } }