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);
}
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);
}
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)));
}
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();
}
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();
}
Aggregations