Search in sources :

Example 11 with DefaultLoadBalancerRetryHandler

use of com.netflix.client.DefaultLoadBalancerRetryHandler 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 12 with DefaultLoadBalancerRetryHandler

use of com.netflix.client.DefaultLoadBalancerRetryHandler 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 13 with DefaultLoadBalancerRetryHandler

use of com.netflix.client.DefaultLoadBalancerRetryHandler in project spring-cloud-netflix by spring-cloud.

the class SpringClientFactoryTests method testConfigureRetry.

@Test
public void testConfigureRetry() {
    SpringClientFactory factory = new SpringClientFactory();
    AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(RibbonAutoConfiguration.class, ArchaiusAutoConfiguration.class, HttpClientConfiguration.class);
    addEnvironment(parent, "foo.ribbon.MaxAutoRetries:2");
    factory.setApplicationContext(parent);
    DefaultLoadBalancerRetryHandler retryHandler = (DefaultLoadBalancerRetryHandler) factory.getLoadBalancerContext("foo").getRetryHandler();
    assertEquals(2, retryHandler.getMaxRetriesOnSameServer());
    parent.close();
    factory.destroy();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) Test(org.junit.Test)

Aggregations

DefaultLoadBalancerRetryHandler (com.netflix.client.DefaultLoadBalancerRetryHandler)13 Test (org.junit.Test)6 RetryHandler (com.netflix.client.RetryHandler)5 IClientConfig (com.netflix.client.config.IClientConfig)5 LoadBalancedRetryPolicy (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy)5 RibbonServer (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer)5 HttpRequest (org.springframework.http.HttpRequest)5 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)4 LoadBalancedRetryContext (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext)4 LoadBalancedRetryFactory (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory)4 RibbonLoadBalancedRetryFactory (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancedRetryFactory)4 RibbonLoadBalancerContext (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerContext)4 ServerIntrospector (org.springframework.cloud.netflix.ribbon.ServerIntrospector)4 SpringClientFactory (org.springframework.cloud.netflix.ribbon.SpringClientFactory)4 Server (com.netflix.loadbalancer.Server)3 IOException (java.io.IOException)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 RequestSpecificRetryHandler (com.netflix.client.RequestSpecificRetryHandler)1 AbstractLoadBalancer (com.netflix.loadbalancer.AbstractLoadBalancer)1 IRule (com.netflix.loadbalancer.IRule)1