Search in sources :

Example 1 with CircuitBreakerRetryPolicy

use of cn.taketoday.retry.policy.CircuitBreakerRetryPolicy in project today-infrastructure by TAKETODAY.

the class AnnotationAwareRetryOperationsInterceptor method getStatefulInterceptor.

private MethodInterceptor getStatefulInterceptor(Object target, Method method, Retryable retryable) {
    RetryTemplate template = createTemplate(retryable.listeners());
    template.setRetryContextCache(this.retryContextCache);
    CircuitBreaker circuit = AnnotatedElementUtils.findMergedAnnotation(method, CircuitBreaker.class);
    if (circuit == null) {
        circuit = findAnnotationOnTarget(target, method, CircuitBreaker.class);
    }
    if (circuit != null) {
        RetryPolicy policy = getRetryPolicy(circuit);
        CircuitBreakerRetryPolicy breaker = new CircuitBreakerRetryPolicy(policy);
        breaker.setOpenTimeout(getOpenTimeout(circuit));
        breaker.setResetTimeout(getResetTimeout(circuit));
        template.setRetryPolicy(breaker);
        template.setBackOffPolicy(new NoBackOffPolicy());
        String label = circuit.label();
        if (!StringUtils.hasText(label)) {
            label = method.toGenericString();
        }
        return RetryInterceptorBuilder.circuitBreaker().keyGenerator(new FixedKeyGenerator("circuit")).retryOperations(template).recoverer(getRecoverer(target, method)).label(label).build();
    }
    RetryPolicy policy = getRetryPolicy(retryable);
    template.setRetryPolicy(policy);
    template.setBackOffPolicy(getBackoffPolicy(retryable.backoff()));
    String label = retryable.label();
    return RetryInterceptorBuilder.stateful().keyGenerator(this.methodArgumentsKeyGenerator).newMethodArgumentsIdentifier(this.newMethodArgumentsIdentifier).retryOperations(template).label(label).recoverer(getRecoverer(target, method)).build();
}
Also used : FixedKeyGenerator(cn.taketoday.retry.interceptor.FixedKeyGenerator) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) NoBackOffPolicy(cn.taketoday.retry.backoff.NoBackOffPolicy) CircuitBreakerRetryPolicy(cn.taketoday.retry.policy.CircuitBreakerRetryPolicy) RetryPolicy(cn.taketoday.retry.RetryPolicy) ExpressionRetryPolicy(cn.taketoday.retry.policy.ExpressionRetryPolicy) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) CircuitBreakerRetryPolicy(cn.taketoday.retry.policy.CircuitBreakerRetryPolicy)

Example 2 with CircuitBreakerRetryPolicy

use of cn.taketoday.retry.policy.CircuitBreakerRetryPolicy in project today-framework by TAKETODAY.

the class CircuitBreakerStatisticsTests method testFailedRecoveryCountsAsAbort.

@Test
public void testFailedRecoveryCountsAsAbort() throws Throwable {
    this.retryTemplate.setRetryPolicy(new CircuitBreakerRetryPolicy(new NeverRetryPolicy()));
    this.recovery = new RecoveryCallback<Object>() {

        @Override
        public Object recover(RetryContext context) throws Exception {
            throw new ExhaustedRetryException("Planned exhausted");
        }
    };
    try {
        this.retryTemplate.execute(this.callback, this.recovery, this.state);
        fail("Expected ExhaustedRetryException");
    } catch (ExhaustedRetryException e) {
    // Fine
    }
    MutableRetryStatistics stats = (MutableRetryStatistics) repository.findOne("test");
    assertEquals(1, stats.getStartedCount());
    assertEquals(1, stats.getAbortCount());
    assertEquals(0, stats.getRecoveryCount());
}
Also used : ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryContext(cn.taketoday.retry.RetryContext) CircuitBreakerRetryPolicy(cn.taketoday.retry.policy.CircuitBreakerRetryPolicy) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.Test)

Example 3 with CircuitBreakerRetryPolicy

use of cn.taketoday.retry.policy.CircuitBreakerRetryPolicy in project today-framework by TAKETODAY.

the class CircuitBreakerStatisticsTests method testCircuitOpenWhenNotRetryable.

@Test
public void testCircuitOpenWhenNotRetryable() throws Throwable {
    this.retryTemplate.setRetryPolicy(new CircuitBreakerRetryPolicy(new NeverRetryPolicy()));
    Object result = this.retryTemplate.execute(this.callback, this.recovery, this.state);
    MutableRetryStatistics stats = (MutableRetryStatistics) repository.findOne("test");
    assertEquals(1, stats.getStartedCount());
    assertEquals(RECOVERED, result);
    result = this.retryTemplate.execute(this.callback, this.recovery, this.state);
    assertEquals(RECOVERED, result);
    assertEquals("There should be two recoveries", 2, stats.getRecoveryCount());
    assertEquals("There should only be one error because the circuit is now open", 1, stats.getErrorCount());
    assertEquals(true, stats.getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN));
    // Both recoveries are through a short circuit because we used NeverRetryPolicy
    assertEquals(2, stats.getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_SHORT_COUNT));
    resetAndAssert(this.cache, stats);
}
Also used : CircuitBreakerRetryPolicy(cn.taketoday.retry.policy.CircuitBreakerRetryPolicy) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) Test(org.junit.Test)

Example 4 with CircuitBreakerRetryPolicy

use of cn.taketoday.retry.policy.CircuitBreakerRetryPolicy in project today-framework by TAKETODAY.

the class CircuitBreakerStatisticsTests method testCircuitOpenWithNoRecovery.

@Test
public void testCircuitOpenWithNoRecovery() throws Throwable {
    this.retryTemplate.setRetryPolicy(new CircuitBreakerRetryPolicy(new NeverRetryPolicy()));
    this.retryTemplate.setThrowLastExceptionOnExhausted(true);
    try {
        this.retryTemplate.execute(this.callback, this.state);
    } catch (Exception e) {
    }
    try {
        this.retryTemplate.execute(this.callback, this.state);
    } catch (Exception e) {
    }
    MutableRetryStatistics stats = (MutableRetryStatistics) repository.findOne("test");
    assertEquals("There should be two aborts", 2, stats.getAbortCount());
    assertEquals("There should only be one error because the circuit is now open", 1, stats.getErrorCount());
    assertEquals(true, stats.getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN));
    resetAndAssert(this.cache, stats);
}
Also used : CircuitBreakerRetryPolicy(cn.taketoday.retry.policy.CircuitBreakerRetryPolicy) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.Test)

Example 5 with CircuitBreakerRetryPolicy

use of cn.taketoday.retry.policy.CircuitBreakerRetryPolicy in project today-framework by TAKETODAY.

the class AnnotationAwareRetryOperationsInterceptor method getStatefulInterceptor.

private MethodInterceptor getStatefulInterceptor(Object target, Method method, Retryable retryable) {
    RetryTemplate template = createTemplate(retryable.listeners());
    template.setRetryContextCache(this.retryContextCache);
    CircuitBreaker circuit = AnnotatedElementUtils.findMergedAnnotation(method, CircuitBreaker.class);
    if (circuit == null) {
        circuit = findAnnotationOnTarget(target, method, CircuitBreaker.class);
    }
    if (circuit != null) {
        RetryPolicy policy = getRetryPolicy(circuit);
        CircuitBreakerRetryPolicy breaker = new CircuitBreakerRetryPolicy(policy);
        breaker.setOpenTimeout(getOpenTimeout(circuit));
        breaker.setResetTimeout(getResetTimeout(circuit));
        template.setRetryPolicy(breaker);
        template.setBackOffPolicy(new NoBackOffPolicy());
        String label = circuit.label();
        if (!StringUtils.hasText(label)) {
            label = method.toGenericString();
        }
        return RetryInterceptorBuilder.circuitBreaker().keyGenerator(new FixedKeyGenerator("circuit")).retryOperations(template).recoverer(getRecoverer(target, method)).label(label).build();
    }
    RetryPolicy policy = getRetryPolicy(retryable);
    template.setRetryPolicy(policy);
    template.setBackOffPolicy(getBackoffPolicy(retryable.backoff()));
    String label = retryable.label();
    return RetryInterceptorBuilder.stateful().keyGenerator(this.methodArgumentsKeyGenerator).newMethodArgumentsIdentifier(this.newMethodArgumentsIdentifier).retryOperations(template).label(label).recoverer(getRecoverer(target, method)).build();
}
Also used : FixedKeyGenerator(cn.taketoday.retry.interceptor.FixedKeyGenerator) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) NoBackOffPolicy(cn.taketoday.retry.backoff.NoBackOffPolicy) CircuitBreakerRetryPolicy(cn.taketoday.retry.policy.CircuitBreakerRetryPolicy) RetryPolicy(cn.taketoday.retry.RetryPolicy) ExpressionRetryPolicy(cn.taketoday.retry.policy.ExpressionRetryPolicy) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) CircuitBreakerRetryPolicy(cn.taketoday.retry.policy.CircuitBreakerRetryPolicy)

Aggregations

CircuitBreakerRetryPolicy (cn.taketoday.retry.policy.CircuitBreakerRetryPolicy)8 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)6 Test (org.junit.Test)6 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)4 RetryContext (cn.taketoday.retry.RetryContext)2 RetryPolicy (cn.taketoday.retry.RetryPolicy)2 NoBackOffPolicy (cn.taketoday.retry.backoff.NoBackOffPolicy)2 FixedKeyGenerator (cn.taketoday.retry.interceptor.FixedKeyGenerator)2 ExpressionRetryPolicy (cn.taketoday.retry.policy.ExpressionRetryPolicy)2 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)2 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)2