Search in sources :

Example 1 with RetryRegistry

use of io.github.resilience4j.retry.RetryRegistry in project resilience4j by resilience4j.

the class RetryMetricsTest method shouldUseCustomPrefix.

@Test
public void shouldUseCustomPrefix() throws Throwable {
    // Given
    RetryRegistry retryRegistry = RetryRegistry.ofDefaults();
    Retry retry = retryRegistry.retry("testName");
    metricRegistry.registerAll(RetryMetrics.ofRetryRegistry("testPrefix", retryRegistry));
    // Given the HelloWorldService returns Hello world
    BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    String value = retry.executeSupplier(helloWorldService::returnHelloWorld);
    // Then
    assertThat(value).isEqualTo("Hello world");
    // Then the helloWorldService should be invoked 1 time
    BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(4);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue()).isEqualTo(0L);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(1L);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName." + FAILED_CALLS_WITH_RETRY).getValue()).isEqualTo(0L);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L);
}
Also used : RetryRegistry(io.github.resilience4j.retry.RetryRegistry) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 2 with RetryRegistry

use of io.github.resilience4j.retry.RetryRegistry in project resilience4j by resilience4j.

the class RetryMetricsTest method shouldRegisterMetricsWithoutRetry.

@Test
public void shouldRegisterMetricsWithoutRetry() throws Throwable {
    // Given
    RetryRegistry retryRegistry = RetryRegistry.ofDefaults();
    Retry retry = retryRegistry.retry("testName");
    metricRegistry.registerAll(RetryMetrics.ofRetryRegistry(retryRegistry));
    // Given the HelloWorldService returns Hello world
    BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    // Setup circuitbreaker with retry
    String value = retry.executeSupplier(helloWorldService::returnHelloWorld);
    // Then
    assertThat(value).isEqualTo("Hello world");
    // Then the helloWorldService should be invoked 1 time
    BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(4);
    assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue()).isEqualTo(0L);
    assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(1L);
    assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITH_RETRY).getValue()).isEqualTo(0L);
    assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L);
}
Also used : RetryRegistry(io.github.resilience4j.retry.RetryRegistry) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 3 with RetryRegistry

use of io.github.resilience4j.retry.RetryRegistry in project java-chassis by ServiceComb.

the class InvokerUtils method getOrCreateCompatibleRetry.

private static Retry getOrCreateCompatibleRetry(Invocation invocation) {
    RetryConfig retryConfig = RetryConfig.custom().maxAttempts(GovernanceConfiguration.getRetryNextServer(invocation.getMicroserviceName()) + GovernanceConfiguration.getRetrySameServer(invocation.getMicroserviceName()) + 1).retryOnResult(InvokerUtils::canRetryForStatusCode).retryOnException(InvokerUtils::canRetryForException).waitDuration(Duration.ofMillis(0)).build();
    RetryRegistry retryRegistry = RetryRegistry.of(retryConfig);
    return retryRegistry.retry(invocation.getMicroserviceName());
}
Also used : RetryConfig(io.github.resilience4j.retry.RetryConfig) RetryRegistry(io.github.resilience4j.retry.RetryRegistry)

Example 4 with RetryRegistry

use of io.github.resilience4j.retry.RetryRegistry in project java-chassis by ServiceComb.

the class RetryHandler method getRetry.

private Retry getRetry(RetryPolicy retryPolicy) {
    LOGGER.info("applying new policy: {}", retryPolicy.toString());
    RetryConfig config = RetryConfig.custom().maxAttempts(retryPolicy.getMaxAttempts() + 1).retryOnResult(response -> retryExtension.isRetry(retryPolicy.getRetryOnResponseStatus(), response)).retryOnException(e -> retryExtension.isRetry(e)).intervalFunction(getIntervalFunction(retryPolicy)).build();
    RetryRegistry registry = RetryRegistry.of(config);
    return registry.retry(retryPolicy.getName());
}
Also used : Logger(org.slf4j.Logger) Retry(io.github.resilience4j.retry.Retry) RetryConfig(io.github.resilience4j.retry.RetryConfig) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) IntervalFunction(io.github.resilience4j.core.IntervalFunction) RetryExtension(org.apache.servicecomb.governance.handler.ext.RetryExtension) GovernanceUtils(org.apache.servicecomb.governance.utils.GovernanceUtils) Component(org.springframework.stereotype.Component) GovernanceRequest(org.apache.servicecomb.governance.marker.GovernanceRequest) Duration(java.time.Duration) RetryProperties(org.apache.servicecomb.governance.properties.RetryProperties) RetryRegistry(io.github.resilience4j.retry.RetryRegistry) RetryPolicy(org.apache.servicecomb.governance.policy.RetryPolicy) RetryConfig(io.github.resilience4j.retry.RetryConfig) RetryRegistry(io.github.resilience4j.retry.RetryRegistry)

Example 5 with RetryRegistry

use of io.github.resilience4j.retry.RetryRegistry in project resilience4j by resilience4j.

the class RetryMetricsTest method shouldRegisterMetricsWithRetry.

@Test
public void shouldRegisterMetricsWithRetry() throws Throwable {
    // Given
    RetryRegistry retryRegistry = RetryRegistry.ofDefaults();
    Retry retry = retryRegistry.retry("testName");
    metricRegistry.registerAll(RetryMetrics.ofRetryRegistry(retryRegistry));
    // Given the HelloWorldService returns Hello world
    BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!")).willReturn("Hello world").willThrow(new WebServiceException("BAM!")).willThrow(new WebServiceException("BAM!")).willThrow(new WebServiceException("BAM!"));
    // Setup circuitbreaker with retry
    String value1 = retry.executeSupplier(helloWorldService::returnHelloWorld);
    Try.ofSupplier(Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld));
    // Then
    assertThat(value1).isEqualTo("Hello world");
    // Then the helloWorldService should be invoked 1 time
    BDDMockito.then(helloWorldService).should(times(5)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(4);
    assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue()).isEqualTo(1L);
    assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L);
    assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITH_RETRY).getValue()).isEqualTo(1L);
    assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L);
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) RetryRegistry(io.github.resilience4j.retry.RetryRegistry) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Aggregations

RetryRegistry (io.github.resilience4j.retry.RetryRegistry)6 Retry (io.github.resilience4j.retry.Retry)4 Test (org.junit.Test)4 RetryConfig (io.github.resilience4j.retry.RetryConfig)2 IntervalFunction (io.github.resilience4j.core.IntervalFunction)1 Meter (io.micrometer.core.instrument.Meter)1 Duration (java.time.Duration)1 WebServiceException (javax.xml.ws.WebServiceException)1 RetryExtension (org.apache.servicecomb.governance.handler.ext.RetryExtension)1 GovernanceRequest (org.apache.servicecomb.governance.marker.GovernanceRequest)1 RetryPolicy (org.apache.servicecomb.governance.policy.RetryPolicy)1 RetryProperties (org.apache.servicecomb.governance.properties.RetryProperties)1 GovernanceUtils (org.apache.servicecomb.governance.utils.GovernanceUtils)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Component (org.springframework.stereotype.Component)1