Skip to content

Commit

Permalink
Merge remote-tracking branch 'github/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed May 7, 2024
2 parents c364e2b + ffa0135 commit 39b94ab
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -25,5 +40,5 @@ public interface Retrier {
*
* @return policy
*/
FailoverPolicy getPolicy();
RetryPolicy getPolicy();
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +32,7 @@ public interface RetrierFactory {

Map<Long, AtomicReference<Retrier>> RETRIERS = new ConcurrentHashMap<>();

default Retrier get(FailoverPolicy policy) {
default Retrier get(RetryPolicy policy) {
if (policy == null) {
return null;
}
Expand All @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +51,7 @@ public class ServicePolicy extends PolicyId implements Cloneable, PolicyInheritW

@Setter
@Getter
private FailoverPolicy failoverPolicy;
private RetryPolicy retryPolicy;

@Setter
@Getter
Expand Down Expand Up @@ -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);
Expand All @@ -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()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,7 +41,7 @@
@Getter
@Setter
@Consumer
public class FailoverPolicy extends PolicyId implements PolicyInheritWithId<FailoverPolicy> {
public class RetryPolicy extends PolicyId implements PolicyInheritWithId<RetryPolicy> {

/**
* The unique identifier of the failover policy. This ID can be used to reference and manage the policy
Expand All @@ -67,13 +67,18 @@ public class FailoverPolicy extends PolicyId implements PolicyInheritWithId<Fail
*/
private Set<Integer> retryableStatusCodes = new HashSet<>(Arrays.asList(500, 502, 503));

/**
* A collection of retryable exception class names.
*/
private Set<String> exceptionClassNames;

/**
* The version of the policy.
*/
private long version;

@Override
public void supplement(FailoverPolicy source) {
public void supplement(RetryPolicy source) {
if (source == null) {
return;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -45,7 +62,7 @@ public <T> T execute(Supplier<T> supplier) {
* {@inheritDoc}
*/
@Override
public FailoverPolicy getPolicy() {
public RetryPolicy getPolicy() {
return policy;
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
}

0 comments on commit 39b94ab

Please sign in to comment.