Search in sources :

Example 71 with TrackerClient

use of com.linkedin.d2.balancer.clients.TrackerClient in project rest.li by linkedin.

the class StateUpdaterTest method testClusterGenerationIdChange.

// Known to be flaky in CI
@Test(retryAnalyzer = ThreeRetries.class)
public void testClusterGenerationIdChange() throws InterruptedException {
    PartitionState state = new PartitionStateTestDataBuilder().setClusterGenerationId(DEFAULT_CLUSTER_GENERATION_ID).build();
    List<TrackerClient> trackerClients = TrackerClientMockHelper.mockTrackerClients(2, Arrays.asList(20, 20), Arrays.asList(10, 10), Arrays.asList(200L, 500L), Arrays.asList(100L, 200L), Arrays.asList(0, 0));
    ConcurrentMap<Integer, PartitionState> partitionLoadBalancerStateMap = new ConcurrentHashMap<>();
    partitionLoadBalancerStateMap.put(DEFAULT_PARTITION_ID, state);
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    D2RelativeStrategyProperties relativeStrategyProperties = RelativeLoadBalancerStrategyFactory.putDefaultValues(new D2RelativeStrategyProperties());
    _stateUpdater = new StateUpdater(relativeStrategyProperties, _quarantineManager, executorService, partitionLoadBalancerStateMap, Collections.emptyList(), SERVICE_NAME);
    // update will be scheduled twice, once from interval update, once from cluster change
    CountDownLatch countDownLatch = new CountDownLatch(2);
    Mockito.doAnswer(new ExecutionCountDown<>(countDownLatch)).when(_quarantineManager).updateQuarantineState(any(), any(), anyLong());
    // Cluster generation id changed from 0 to 1
    _stateUpdater.updateState(new HashSet<>(trackerClients), DEFAULT_PARTITION_ID, 1, false);
    if (!countDownLatch.await(5, TimeUnit.SECONDS)) {
        fail("cluster update failed to finish within 5 seconds");
    }
    assertEquals(_stateUpdater.getPointsMap(DEFAULT_PARTITION_ID).size(), 2);
    executorService.shutdown();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) Test(org.testng.annotations.Test)

Example 72 with TrackerClient

use of com.linkedin.d2.balancer.clients.TrackerClient in project rest.li by linkedin.

the class StateUpdaterTest method testForceUpdate.

// Known to be flaky in CI
@Test(retryAnalyzer = ThreeRetries.class)
public void testForceUpdate() throws InterruptedException {
    PartitionState state = new PartitionStateTestDataBuilder().setClusterGenerationId(DEFAULT_CLUSTER_GENERATION_ID).build();
    List<TrackerClient> trackerClients = TrackerClientMockHelper.mockTrackerClients(2, Arrays.asList(20, 20), Arrays.asList(10, 10), Arrays.asList(200L, 500L), Arrays.asList(100L, 200L), Arrays.asList(0, 0));
    ConcurrentMap<Integer, PartitionState> partitionLoadBalancerStateMap = new ConcurrentHashMap<>();
    partitionLoadBalancerStateMap.put(DEFAULT_PARTITION_ID, state);
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    D2RelativeStrategyProperties relativeStrategyProperties = RelativeLoadBalancerStrategyFactory.putDefaultValues(new D2RelativeStrategyProperties());
    _stateUpdater = new StateUpdater(relativeStrategyProperties, _quarantineManager, executorService, partitionLoadBalancerStateMap, Collections.emptyList(), SERVICE_NAME);
    // update will be scheduled three times, once from interval update, once from cluster change, once from force update
    CountDownLatch countDownLatch = new CountDownLatch(3);
    Mockito.doAnswer(new ExecutionCountDown<>(countDownLatch)).when(_quarantineManager).updateQuarantineState(any(), any(), anyLong());
    // Cluster generation id changed from 0 to 1
    _stateUpdater.updateState(new HashSet<>(trackerClients), DEFAULT_PARTITION_ID, 1, false);
    _stateUpdater.updateState(new HashSet<>(trackerClients), DEFAULT_PARTITION_ID, 1, true);
    if (!countDownLatch.await(5, TimeUnit.SECONDS)) {
        fail("cluster update failed to finish within 5 seconds");
    }
    assertEquals(_stateUpdater.getPointsMap(DEFAULT_PARTITION_ID).size(), 2);
    executorService.shutdown();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) D2RelativeStrategyProperties(com.linkedin.d2.D2RelativeStrategyProperties) Test(org.testng.annotations.Test)

Example 73 with TrackerClient

use of com.linkedin.d2.balancer.clients.TrackerClient in project rest.li by linkedin.

the class ClientSelectorTest method testGetTargetHostNotFound.

@Test
public void testGetTargetHostNotFound() {
    URI newUri = URI.create("new_uri");
    KeyMapper.TargetHostHints.setRequestContextTargetHost(_requestContext, newUri);
    TrackerClient trackerClient = _clientSelector.getTrackerClient(_request, _requestContext, DEFAULT_RING, DEFAULT_TRACKER_CLIENT_MAP);
    assertEquals(trackerClient, null);
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 74 with TrackerClient

use of com.linkedin.d2.balancer.clients.TrackerClient in project rest.li by linkedin.

the class ClientSelectorTest method testAllClientsExcluded.

@Test
public void testAllClientsExcluded() {
    LoadBalancerStrategy.ExcludedHostHints.addRequestContextExcludedHost(_requestContext, URI_1);
    LoadBalancerStrategy.ExcludedHostHints.addRequestContextExcludedHost(_requestContext, URI_2);
    LoadBalancerStrategy.ExcludedHostHints.addRequestContextExcludedHost(_requestContext, URI_3);
    TrackerClient trackerClient = _clientSelector.getTrackerClient(_request, _requestContext, DEFAULT_RING, DEFAULT_TRACKER_CLIENT_MAP);
    assertEquals(trackerClient, null);
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) Test(org.testng.annotations.Test)

Example 75 with TrackerClient

use of com.linkedin.d2.balancer.clients.TrackerClient in project rest.li by linkedin.

the class ClientSelectorTest method testRingAndHostInconsistency.

@Test
public void testRingAndHostInconsistency() {
    URI newUri = URI.create("new_uri");
    TrackerClient newTrackerClient = Mockito.mock(TrackerClient.class);
    Mockito.when(newTrackerClient.getUri()).thenReturn(newUri);
    Map<URI, TrackerClient> newTrackerClientMap = new HashMap<>();
    newTrackerClientMap.put(newUri, newTrackerClient);
    TrackerClient trackerClient = _clientSelector.getTrackerClient(_request, _requestContext, DEFAULT_RING, newTrackerClientMap);
    assertEquals(trackerClient, newTrackerClient, "The host should be picked from the tracker client list passed from the request because the ring is completely out of date");
}
Also used : TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) HashMap(java.util.HashMap) URI(java.net.URI) Test(org.testng.annotations.Test)

Aggregations

TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)117 URI (java.net.URI)83 Test (org.testng.annotations.Test)58 HashMap (java.util.HashMap)50 ArrayList (java.util.ArrayList)49 RequestContext (com.linkedin.r2.message.RequestContext)39 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)30 DegraderTrackerClient (com.linkedin.d2.balancer.clients.DegraderTrackerClient)26 Map (java.util.Map)25 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)22 DegraderTrackerClientImpl (com.linkedin.d2.balancer.clients.DegraderTrackerClientImpl)18 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)18 D2RelativeStrategyProperties (com.linkedin.d2.D2RelativeStrategyProperties)17 DegraderTrackerClientTest (com.linkedin.d2.balancer.clients.DegraderTrackerClientTest)17 NullStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback)15 DegraderLoadBalancerTest (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)14 URIRequest (com.linkedin.d2.balancer.util.URIRequest)14 DegraderControl (com.linkedin.util.degrader.DegraderControl)14 HashSet (java.util.HashSet)13 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)12