Skip to content

Commit

Permalink
# modify GovernanceProperties type
Browse files Browse the repository at this point in the history
  • Loading branch information
develpoerX committed Jul 23, 2021
1 parent f43f52e commit 1816bf6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private CircuitBreaker getCircuitBreaker(CircuitBreakerPolicy policy) {
//可以选择基于时间的滑动窗口计数或者基于请求数量的滑动窗口计数
.slidingWindowType(policy.getSlidingWindowTypeEnum())
//滑动窗口,单位可能是请求数或者秒
.slidingWindowSize(policy.getSlidingWindowSize())
.slidingWindowSize(Integer.valueOf(policy.getSlidingWindowSize()))
.build();
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig);
return circuitBreakerRegistry.circuitBreaker(policy.getName(), circuitBreakerConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.function.Predicate;

import io.github.resilience4j.core.IntervalFunction;

import org.apache.servicecomb.governance.handler.ext.RetryExtension;
import org.apache.servicecomb.governance.marker.GovernanceRequest;
import org.apache.servicecomb.governance.policy.RetryPolicy;
Expand Down Expand Up @@ -65,7 +66,7 @@ private Retry getRetry(RetryPolicy retryPolicy) {
LOGGER.info("applying new policy: {}", retryPolicy.toString());

RetryConfig config = RetryConfig.custom()
.maxAttempts(retryPolicy.getMaxAttempts())
.maxAttempts(retryPolicy.getMaxAttempts() + 1)
.retryOnResult(getPredicate(retryPolicy.getRetryOnResponseStatus()))
.retryExceptions(retryExtension.retryExceptions())
.intervalFunction(getIntervalFunction(retryPolicy))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.servicecomb.governance.policy;

import org.apache.servicecomb.governance.utils.GovernanceUtils;
import org.springframework.util.StringUtils;

import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.SlidingWindowType;
Expand All @@ -38,7 +39,7 @@ public class CircuitBreakerPolicy extends AbstractPolicy {

public static final int DEFAULT_MINIMUM_NUMBER_CALLS = 100;

public static final int DEFAULT_SLIDING_WINDOW_SIZE = 100;
public static final String DEFAULT_SLIDING_WINDOW_SIZE = "100";

private int failureRateThreshold = DEFAULT_FAILURE_RATE_THRESHOLD;

Expand All @@ -54,7 +55,7 @@ public class CircuitBreakerPolicy extends AbstractPolicy {

private String slidingWindowType;

private int slidingWindowSize = DEFAULT_SLIDING_WINDOW_SIZE;
private String slidingWindowSize = DEFAULT_SLIDING_WINDOW_SIZE;

public CircuitBreakerPolicy() {
}
Expand Down Expand Up @@ -133,15 +134,12 @@ public void setMinimumNumberOfCalls(int minimumNumberOfCalls) {

public SlidingWindowType getSlidingWindowTypeEnum() {
if (StringUtils.isEmpty(slidingWindowType)) {
slidingWindowType = "count";
return SlidingWindowType.TIME_BASED;
}
switch (slidingWindowType) {
case "time":
return SlidingWindowType.TIME_BASED;
case "count":
default:
return SlidingWindowType.COUNT_BASED;
if (SlidingWindowType.COUNT_BASED.equals(slidingWindowType)) {
return SlidingWindowType.COUNT_BASED;
}
return SlidingWindowType.TIME_BASED;
}

public String getSlidingWindowType() {
Expand All @@ -153,12 +151,26 @@ public void setSlidingWindowType(String slidingWindowType) {
}

// time's unit is second
public int getSlidingWindowSize() {
public String getSlidingWindowSize() {
return slidingWindowSize;
}

public void setSlidingWindowSize(int slidingWindowSize) {
this.slidingWindowSize = slidingWindowSize;
public void setSlidingWindowSize(String slidingWindowSize) {
this.slidingWindowSize = getValue(slidingWindowSize);
}

private String getValue(String slidingWindowSize) {
if (StringUtils.isEmpty(slidingWindowSize)) {
return DEFAULT_SLIDING_WINDOW_SIZE;
}
if (slidingWindowSize.matches(GovernanceUtils.DIGIT_REGEX)) {
if (Long.valueOf(slidingWindowSize) < 0) {
throw new RuntimeException("The value should be more than 0.");
}
return slidingWindowSize;
}
Duration duration = Duration.parse(GovernanceUtils.DIGIT_PREFIX + slidingWindowSize);
return String.valueOf(duration.getSeconds());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void setRetryOnResponseStatus(List<String> retryOnResponseStatus) {
}

public int getMaxAttempts() {
return maxAttempts + 1;
return maxAttempts;
}

public void setMaxAttempts(int maxAttempts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,35 @@ public void test_circuit_breaker_properties_successfully_loaded() {
Assert.assertEquals(1, policies.size());
CircuitBreakerPolicy policy = policies.get("demo-circuitBreaker");
Assert.assertEquals(2, policy.getMinimumNumberOfCalls());
Assert.assertEquals(2, policy.getSlidingWindowSize());
Assert.assertEquals("2", policy.getSlidingWindowSize());
}

@Test
public void test_circuit_breaker_properties_Of_windows_size() {
dynamicValues.put("servicecomb.circuitBreaker.name1", "rules:\n"
+ "slidingWindowType: count\n"
+ "slidingWindowSize: 2");
dynamicValues.put("servicecomb.circuitBreaker.name2", "rules:\n"
+ "slidingWindowType: time\n"
+ "slidingWindowSize: 2");
dynamicValues.put("servicecomb.circuitBreaker.name3", "rules:\n"
+ "slidingWindowType: test\n"
+ "slidingWindowSize: 1M");

EventManager.post(new ConfigurationChangedEvent(new HashSet<>(dynamicValues.keySet())));

Map<String, CircuitBreakerPolicy> policies = circuitBreakerProperties.getParsedEntity();
Assert.assertEquals(4, policies.size());
CircuitBreakerPolicy policy = policies.get("name1");
Assert.assertEquals("count", policy.getSlidingWindowType());
Assert.assertEquals("2", policy.getSlidingWindowSize());

policy = policies.get("name2");
Assert.assertEquals("time", policy.getSlidingWindowType());
Assert.assertEquals("2", policy.getSlidingWindowSize());

policy = policies.get("name3");
Assert.assertEquals("60", policy.getSlidingWindowSize());
}

@Test
Expand Down Expand Up @@ -256,5 +284,4 @@ public void test_properties_changed_to_duration() {
policy = policies.get("test2");
Assert.assertEquals(60000, Duration.parse(policy.getMaxWaitDuration()).toMillis());
}

}

0 comments on commit 1816bf6

Please sign in to comment.