Search in sources :

Example 66 with TrackerClient

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

the class SimpleLoadBalancerStateTest method testListValueInTransportClientProperties.

@Test(groups = { "small", "back-end" })
public void testListValueInTransportClientProperties() throws URISyntaxException {
    reset();
    URI uri = URI.create("http://cluster-1/test");
    List<String> schemes = new ArrayList<>();
    Map<Integer, PartitionData> partitionData = new HashMap<>(1);
    partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
    Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<>();
    uriData.put(uri, partitionData);
    schemes.add("http");
    assertNull(_state.getClient("service-1", uri));
    // set up state
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    assertNull(_state.getClient("service-1", uri));
    _state.listenToService("service-1", new NullStateListenerCallback());
    assertNull(_state.getClient("service-1", uri));
    Map<String, Object> transportClientProperties = new HashMap<>();
    List<String> allowedClientOverrideKeys = new ArrayList<>();
    allowedClientOverrideKeys.add(PropertyKeys.HTTP_REQUEST_TIMEOUT);
    allowedClientOverrideKeys.add(PropertyKeys.HTTP_RESPONSE_COMPRESSION_OPERATIONS);
    transportClientProperties.put(PropertyKeys.ALLOWED_CLIENT_OVERRIDE_KEYS, allowedClientOverrideKeys);
    List<String> compressionOperations = new ArrayList<>();
    compressionOperations.add("get");
    compressionOperations.add("batch_get");
    compressionOperations.add("get_all");
    transportClientProperties.put(PropertyKeys.HTTP_RESPONSE_COMPRESSION_OPERATIONS, compressionOperations);
    transportClientProperties = Collections.unmodifiableMap(transportClientProperties);
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"), Collections.<String, Object>emptyMap(), transportClientProperties, null, schemes, null));
    assertNull(_state.getClient("service-1", uri));
    _uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
    TrackerClient client = _state.getClient("service-1", uri);
    assertNotNull(client);
    assertEquals(client.getUri(), uri);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(java.net.URI) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 67 with TrackerClient

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

the class SimpleLoadBalancerStateTest method testGetClientWithoutScheme.

@Test(groups = { "small", "back-end" })
public void testGetClientWithoutScheme() throws URISyntaxException {
    reset();
    URI uri = URI.create("cluster-1/test");
    List<String> schemes = new ArrayList<>();
    Map<Integer, PartitionData> partitionData = new HashMap<>(1);
    partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
    Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<>();
    uriData.put(uri, partitionData);
    schemes.add("http");
    // set up state
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    _state.listenToService("service-1", new NullStateListenerCallback());
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"), Collections.emptyMap(), null, null, schemes, null));
    assertNull(_state.getClient("service-1", uri));
    // the URI without Scheme will get us nothing
    _uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
    assertNull(_state.getClient("service-1", uri));
    // correct URI will return the right client
    uri = URI.create("http://cluster-1/test1");
    uriData.put(uri, partitionData);
    _uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
    TrackerClient client = _state.getClient("service-1", uri);
    assertNotNull(client);
    assertEquals(client.getUri(), uri);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(java.net.URI) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 68 with TrackerClient

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

the class QuarantineManagerTest method testEnrollOneQuarantineOneRecovery.

@Test
public void testEnrollOneQuarantineOneRecovery() {
    LoadBalancerQuarantine quarantine = Mockito.mock(LoadBalancerQuarantine.class);
    List<TrackerClient> trackerClients = TrackerClientMockHelper.mockTrackerClients(3);
    Map<TrackerClient, LoadBalancerQuarantine> existingQuarantineMap = new HashMap<>();
    existingQuarantineMap.put(trackerClients.get(1), quarantine);
    Mockito.when(quarantine.checkUpdateQuarantineState()).thenReturn(true);
    setup(0.5, true, true);
    _quarantineManager.tryEnableQuarantine();
    PartitionState state = new PartitionStateTestDataBuilder().setTrackerClientStateMap(trackerClients, Arrays.asList(StateUpdater.MIN_HEALTH_SCORE, StateUpdater.MIN_HEALTH_SCORE, QuarantineManager.INITIAL_RECOVERY_HEALTH_SCORE), Arrays.asList(TrackerClientState.HealthState.UNHEALTHY, TrackerClientState.HealthState.NEUTRAL, TrackerClientState.HealthState.UNHEALTHY), Arrays.asList(20, 20, 20)).build();
    _quarantineManager.updateQuarantineState(state, state, DEFAULT_AVG_CLUSTER_LATENCY);
    assertEquals(state.getRecoveryTrackerClients().size(), 1);
    assertTrue(state.getRecoveryTrackerClients().contains(trackerClients.get(1)));
    assertEquals(state.getQuarantineMap().size(), 1);
    assertTrue(state.getQuarantineMap().containsKey(trackerClients.get(0)));
}
Also used : LoadBalancerQuarantine(com.linkedin.d2.balancer.strategies.LoadBalancerQuarantine) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 69 with TrackerClient

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

the class LoadBalancerStrategyTestRunner method runInterval.

/**
 * Execute one interval with the given request count
 */
private void runInterval(List<Integer> partitionIds) {
    int currentIntervalIndex = (int) (_clockedExecutor.currentTimeMillis() / LoadBalancerStrategyTestRunnerBuilder.INTERVAL_IN_MILLIS);
    int requestCount = _requestsManager.getRequestCount(currentIntervalIndex);
    int partitionIndex = 0;
    for (int i = 0; i < requestCount; i++) {
        // construct the requests
        URIRequest uriRequest = new URIRequest("d2://" + _serviceName + "/" + i);
        RestRequest restRequest = new RestRequestBuilder(uriRequest.getURI()).build();
        RequestContext requestContext = new RequestContext();
        int partitionId = partitionIds.get(partitionIndex);
        Map<URI, TrackerClient> trackerClientMap = _partitionTrackerClientsMap.get(partitionId);
        // Get client with default generation id and cluster id
        TrackerClient trackerClient = null;
        try {
            trackerClient = _strategy.getTrackerClient(restRequest, requestContext, DEFAULT_GENERATION_ID, partitionId, trackerClientMap);
        } catch (NullPointerException ex) {
            System.out.println("Encountered error " + ex);
        }
        partitionIndex = partitionIndex >= partitionIds.size() - 1 ? 0 : partitionIndex + 1;
        TransportCallback<RestResponse> restCallback = (response) -> {
        };
        if (trackerClient != null) {
            // Send the request to the picked host if the decision is not DROP
            trackerClient.restRequest(restRequest, requestContext, Collections.emptyMap(), restCallback);
            // Increase the count in the current request count map
            URI uri = trackerClient.getUri();
            if (_currentRequestCountMap.containsKey(trackerClient.getUri())) {
                _currentRequestCountMap.put(uri, _currentRequestCountMap.get(uri) + 1);
            } else {
                _currentRequestCountMap.put(uri, 1);
            }
        }
    }
    updateState();
}
Also used : Arrays(java.util.Arrays) RelativeLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) LoggerFactory(org.slf4j.LoggerFactory) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) HashMap(java.util.HashMap) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestResponse(com.linkedin.r2.message.rest.RestResponse) ArrayList(java.util.ArrayList) URIRequest(com.linkedin.d2.balancer.util.URIRequest) Future(java.util.concurrent.Future) Request(com.linkedin.r2.message.Request) ClockedExecutor(com.linkedin.test.util.ClockedExecutor) Map(java.util.Map) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) Logger(org.slf4j.Logger) DegraderLoadBalancerStrategyV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyV3) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RequestContext(com.linkedin.r2.message.RequestContext) RelativeLoadBalancerTestHelper(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerTestHelper) Collections(java.util.Collections) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) RestResponse(com.linkedin.r2.message.rest.RestResponse) URIRequest(com.linkedin.d2.balancer.util.URIRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI)

Example 70 with TrackerClient

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

the class LoadBalancerStrategyTestRunner method runInconsistencyTrackerClients.

private void runInconsistencyTrackerClients(List<Integer> partitionIds, int numTrackerClients) {
    int currentIntervalIndex = (int) (_clockedExecutor.currentTimeMillis() / LoadBalancerStrategyTestRunnerBuilder.INTERVAL_IN_MILLIS);
    int requestCount = _requestsManager.getRequestCount(currentIntervalIndex);
    int partitionIndex = 0;
    for (int i = 0; i < requestCount; i++) {
        // construct the requests
        URIRequest uriRequest = new URIRequest("d2://" + _serviceName + "/" + i);
        RestRequest restRequest = new RestRequestBuilder(uriRequest.getURI()).build();
        RequestContext requestContext = new RequestContext();
        int partitionId = partitionIds.get(partitionIndex);
        Map<URI, TrackerClient> partialTrackerClientsMap = new HashMap<>();
        int index = 0;
        for (Map.Entry<URI, TrackerClient> entry : _partitionTrackerClientsMap.get(partitionId).entrySet()) {
            if (index < numTrackerClients) {
                partialTrackerClientsMap.put(entry.getKey(), entry.getValue());
            }
            index++;
            if (index >= numTrackerClients) {
                break;
            }
        }
        // Get client with default generation id and cluster id
        TrackerClient trackerClient = null;
        try {
            trackerClient = _strategy.getTrackerClient(restRequest, requestContext, DEFAULT_GENERATION_ID, partitionId, partialTrackerClientsMap);
        } catch (NullPointerException ex) {
            System.out.println("Encountered error " + ex);
        }
        partitionIndex = partitionIndex >= partitionIds.size() - 1 ? 0 : partitionIndex + 1;
        TransportCallback<RestResponse> restCallback = (response) -> {
        };
        if (trackerClient != null) {
            // Send the request to the picked host if the decision is not DROP
            trackerClient.restRequest(restRequest, requestContext, Collections.emptyMap(), restCallback);
            // Increase the count in the current request count map
            URI uri = trackerClient.getUri();
            if (_currentRequestCountMap.containsKey(trackerClient.getUri())) {
                _currentRequestCountMap.put(uri, _currentRequestCountMap.get(uri) + 1);
            } else {
                _currentRequestCountMap.put(uri, 1);
            }
        }
    }
    updateState();
}
Also used : Arrays(java.util.Arrays) RelativeLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) LoggerFactory(org.slf4j.LoggerFactory) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) HashMap(java.util.HashMap) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestResponse(com.linkedin.r2.message.rest.RestResponse) ArrayList(java.util.ArrayList) URIRequest(com.linkedin.d2.balancer.util.URIRequest) Future(java.util.concurrent.Future) Request(com.linkedin.r2.message.Request) ClockedExecutor(com.linkedin.test.util.ClockedExecutor) Map(java.util.Map) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) Logger(org.slf4j.Logger) DegraderLoadBalancerStrategyV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyV3) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RequestContext(com.linkedin.r2.message.RequestContext) RelativeLoadBalancerTestHelper(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerTestHelper) Collections(java.util.Collections) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) URIRequest(com.linkedin.d2.balancer.util.URIRequest) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map)

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