Search in sources :

Example 6 with LoadBalancedRetryPolicy

use of org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancedRetryFactoryTests method testGetRetryPolicyRetryCount.

@Test
public void testGetRetryPolicyRetryCount() throws Exception {
    int sameServer = 3;
    int nextServer = 3;
    RibbonServer server = getRibbonServer();
    IClientConfig config = mock(IClientConfig.class);
    doReturn(sameServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(nextServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(false).when(config).get(eq(CommonClientConfigKey.OkToRetryOnAllOperations), eq(false));
    doReturn(config).when(clientFactory).getClientConfig(eq(server.getServiceId()));
    doReturn("").when(config).getPropertyAsString(eq(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES), eq(""));
    clientFactory.getLoadBalancerContext(server.getServiceId()).setRetryHandler(new DefaultLoadBalancerRetryHandler(config));
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    RibbonLoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
    LoadBalancedRetryPolicy policy = factory.createRetryPolicy(server.getServiceId(), client);
    HttpRequest request = mock(HttpRequest.class);
    doReturn(HttpMethod.GET).when(request).getMethod();
    LoadBalancedRetryContext context = spy(new LoadBalancedRetryContext(null, request));
    // inner loop is for same server retries
    for (int i = 0; i < nextServer + 1; i++) {
        // the same sever counter and increment the next server counter
        for (int j = 0; j < sameServer + 1; j++) {
            if (j < 3) {
                assertThat(policy.canRetrySameServer(context), is(true));
            } else {
                assertThat(policy.canRetrySameServer(context), is(false));
            }
            policy.registerThrowable(context, new IOException());
        }
        if (i < 3) {
            assertThat(policy.canRetryNextServer(context), is(true));
        } else {
            assertThat(policy.canRetryNextServer(context), is(false));
        }
    }
    assertThat(context.isExhaustedOnly(), is(true));
    assertThat(policy.retryableStatusCode(400), is(false));
    verify(context, times(4)).setServiceInstance(any(ServiceInstance.class));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) IClientConfig(com.netflix.client.config.IClientConfig) ServiceInstance(org.springframework.cloud.client.ServiceInstance) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) IOException(java.io.IOException) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) Test(org.junit.Test)

Example 7 with LoadBalancedRetryPolicy

use of org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancedRetryFactoryTests method testGetRetryPolicyNoRetry.

@Test
public void testGetRetryPolicyNoRetry() throws Exception {
    int sameServer = 0;
    int nextServer = 0;
    boolean retryOnAllOps = false;
    RibbonServer server = getRibbonServer();
    IClientConfig config = mock(IClientConfig.class);
    doReturn(sameServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(sameServer).when(config).getPropertyAsInteger(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(nextServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(nextServer).when(config).getPropertyAsInteger(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(retryOnAllOps).when(config).get(eq(CommonClientConfigKey.OkToRetryOnAllOperations), anyBoolean());
    doReturn(retryOnAllOps).when(config).getPropertyAsBoolean(eq(CommonClientConfigKey.OkToRetryOnAllOperations), anyBoolean());
    doReturn("").when(config).getPropertyAsString(eq(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES), eq(""));
    doReturn(server.getServiceId()).when(config).getClientName();
    doReturn(config).when(clientFactory).getClientConfig(eq(server.getServiceId()));
    clientFactory.getLoadBalancerContext(server.getServiceId()).setRetryHandler(new DefaultLoadBalancerRetryHandler(config));
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    RibbonLoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
    LoadBalancedRetryPolicy policy = factory.createRetryPolicy(server.getServiceId(), client);
    HttpRequest request = mock(HttpRequest.class);
    doReturn(HttpMethod.GET).when(request).getMethod();
    LoadBalancedRetryContext context = new LoadBalancedRetryContext(null, request);
    assertThat(policy.canRetryNextServer(context), is(true));
    assertThat(policy.canRetrySameServer(context), is(false));
    assertThat(policy.retryableStatusCode(400), is(false));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) IClientConfig(com.netflix.client.config.IClientConfig) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) Test(org.junit.Test)

Example 8 with LoadBalancedRetryPolicy

use of org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancedRetryFactoryTests method testCiruitRelatedExceptionsUpdateServerStats.

@Test
public void testCiruitRelatedExceptionsUpdateServerStats() throws Exception {
    int sameServer = 3;
    int nextServer = 3;
    RibbonServer server = getRibbonServer();
    IClientConfig config = mock(IClientConfig.class);
    doReturn(sameServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(nextServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(false).when(config).get(eq(CommonClientConfigKey.OkToRetryOnAllOperations), eq(false));
    doReturn(config).when(clientFactory).getClientConfig(eq(server.getServiceId()));
    doReturn("").when(config).getPropertyAsString(eq(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES), eq(""));
    clientFactory.getLoadBalancerContext(server.getServiceId()).setRetryHandler(new DefaultLoadBalancerRetryHandler(config));
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    RibbonLoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
    LoadBalancedRetryPolicy policy = factory.createRetryPolicy(server.getServiceId(), client);
    HttpRequest request = mock(HttpRequest.class);
    LoadBalancedRetryContext context = spy(new LoadBalancedRetryContext(null, request));
    doReturn(server).when(context).getServiceInstance();
    policy.registerThrowable(context, new IOException());
    verify(serverStats, times(0)).incrementSuccessiveConnectionFailureCount();
    // Circuit Related should increment failure count
    policy.registerThrowable(context, new SocketException());
    verify(serverStats, times(1)).incrementSuccessiveConnectionFailureCount();
}
Also used : HttpRequest(org.springframework.http.HttpRequest) SocketException(java.net.SocketException) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) IClientConfig(com.netflix.client.config.IClientConfig) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) IOException(java.io.IOException) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) Test(org.junit.Test)

Example 9 with LoadBalancedRetryPolicy

use of org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy in project spring-cloud-netflix by spring-cloud.

the class RetryableRibbonLoadBalancingHttpClient method executeWithRetry.

private RibbonApacheHttpResponse executeWithRetry(RibbonApacheHttpRequest request, LoadBalancedRetryPolicy retryPolicy, RetryCallback<RibbonApacheHttpResponse, Exception> callback, RecoveryCallback<RibbonApacheHttpResponse> recoveryCallback) throws Exception {
    RetryTemplate retryTemplate = new RetryTemplate();
    boolean retryable = isRequestRetryable(request);
    retryTemplate.setRetryPolicy(retryPolicy == null || !retryable ? new NeverRetryPolicy() : new RetryPolicy(request, retryPolicy, this, this.getClientName()));
    BackOffPolicy backOffPolicy = loadBalancedRetryFactory.createBackOffPolicy(this.getClientName());
    retryTemplate.setBackOffPolicy(backOffPolicy == null ? new NoBackOffPolicy() : backOffPolicy);
    RetryListener[] retryListeners = this.loadBalancedRetryFactory.createRetryListeners(this.getClientName());
    if (retryListeners != null && retryListeners.length != 0) {
        retryTemplate.setListeners(retryListeners);
    }
    return retryTemplate.execute(callback, recoveryCallback);
}
Also used : RetryTemplate(org.springframework.retry.support.RetryTemplate) BackOffPolicy(org.springframework.retry.backoff.BackOffPolicy) NoBackOffPolicy(org.springframework.retry.backoff.NoBackOffPolicy) NoBackOffPolicy(org.springframework.retry.backoff.NoBackOffPolicy) NeverRetryPolicy(org.springframework.retry.policy.NeverRetryPolicy) InterceptorRetryPolicy(org.springframework.cloud.client.loadbalancer.InterceptorRetryPolicy) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) NeverRetryPolicy(org.springframework.retry.policy.NeverRetryPolicy) RetryListener(org.springframework.retry.RetryListener)

Example 10 with LoadBalancedRetryPolicy

use of org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy in project spring-cloud-netflix by spring-cloud.

the class RetryableOkHttpLoadBalancingClient method executeWithRetry.

private OkHttpRibbonResponse executeWithRetry(OkHttpRibbonRequest request, LoadBalancedRetryPolicy retryPolicy, RetryCallback<OkHttpRibbonResponse, Exception> callback, RecoveryCallback<OkHttpRibbonResponse> recoveryCallback) throws Exception {
    RetryTemplate retryTemplate = new RetryTemplate();
    BackOffPolicy backOffPolicy = loadBalancedRetryFactory.createBackOffPolicy(this.getClientName());
    retryTemplate.setBackOffPolicy(backOffPolicy == null ? new NoBackOffPolicy() : backOffPolicy);
    RetryListener[] retryListeners = this.loadBalancedRetryFactory.createRetryListeners(this.getClientName());
    if (retryListeners != null && retryListeners.length != 0) {
        retryTemplate.setListeners(retryListeners);
    }
    boolean retryable = isRequestRetryable(request);
    retryTemplate.setRetryPolicy(retryPolicy == null || !retryable ? new NeverRetryPolicy() : new RetryPolicy(request, retryPolicy, this, this.getClientName()));
    return retryTemplate.execute(callback, recoveryCallback);
}
Also used : RetryTemplate(org.springframework.retry.support.RetryTemplate) BackOffPolicy(org.springframework.retry.backoff.BackOffPolicy) NoBackOffPolicy(org.springframework.retry.backoff.NoBackOffPolicy) NoBackOffPolicy(org.springframework.retry.backoff.NoBackOffPolicy) NeverRetryPolicy(org.springframework.retry.policy.NeverRetryPolicy) InterceptorRetryPolicy(org.springframework.cloud.client.loadbalancer.InterceptorRetryPolicy) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) NeverRetryPolicy(org.springframework.retry.policy.NeverRetryPolicy) RetryListener(org.springframework.retry.RetryListener)

Aggregations

LoadBalancedRetryPolicy (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy)10 HttpRequest (org.springframework.http.HttpRequest)8 IClientConfig (com.netflix.client.config.IClientConfig)7 LoadBalancedRetryContext (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext)7 Test (org.junit.Test)6 RibbonServer (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer)6 DefaultLoadBalancerRetryHandler (com.netflix.client.DefaultLoadBalancerRetryHandler)5 ServiceInstance (org.springframework.cloud.client.ServiceInstance)3 InterceptorRetryPolicy (org.springframework.cloud.client.loadbalancer.InterceptorRetryPolicy)3 RetryListener (org.springframework.retry.RetryListener)3 BackOffPolicy (org.springframework.retry.backoff.BackOffPolicy)3 NoBackOffPolicy (org.springframework.retry.backoff.NoBackOffPolicy)3 NeverRetryPolicy (org.springframework.retry.policy.NeverRetryPolicy)3 RetryTemplate (org.springframework.retry.support.RetryTemplate)3 IOException (java.io.IOException)2 URI (java.net.URI)2 ContextAwareRequest (org.springframework.cloud.netflix.ribbon.support.ContextAwareRequest)2 RetryCallback (org.springframework.retry.RetryCallback)2 RequestSpecificRetryHandler (com.netflix.client.RequestSpecificRetryHandler)1 RetryHandler (com.netflix.client.RetryHandler)1