use of com.linkedin.d2.poolStrategyType in project rest.li by linkedin.
the class SimpleLoadBalancer method getPartitionInformation.
/**
* If given a collection of keys, the method will maps keys to partitions and
* return the servers that belongs to that partition up to limitHostPerPartition.
*
* If no keys are specified, the method will return hosts in all partitions
*
* @param serviceUri for example d2://articles
* @param keys all the keys we want to find the partition for
* @param limitHostPerPartition the number of hosts that we should return for this partition. Must be larger than 0.
* @param hash this will be used to create Iterator for the hosts in the hash ring
* @return Number of hosts in requested partitions. See {@link com.linkedin.d2.balancer.util.HostToKeyMapper} for more details.
* @throws ServiceUnavailableException
*/
@Override
public <K> HostToKeyMapper<K> getPartitionInformation(URI serviceUri, Collection<K> keys, int limitHostPerPartition, int hash) throws ServiceUnavailableException {
if (limitHostPerPartition <= 0) {
throw new IllegalArgumentException("limitHostPartition cannot be 0 or less");
}
ServiceProperties service = listenToServiceAndCluster(serviceUri);
String serviceName = service.getServiceName();
String clusterName = service.getClusterName();
ClusterProperties cluster = getClusterProperties(serviceName, clusterName);
LoadBalancerStateItem<UriProperties> uriItem = getUriItem(serviceName, clusterName, cluster);
UriProperties uris = uriItem.getProperty();
List<LoadBalancerState.SchemeStrategyPair> orderedStrategies = _state.getStrategiesForService(serviceName, service.getPrioritizedSchemes());
Map<Integer, Integer> partitionWithoutEnoughHost = new HashMap<Integer, Integer>();
if (!orderedStrategies.isEmpty()) {
// get the partitionId -> keys mapping
final PartitionAccessor accessor = getPartitionAccessor(serviceName, clusterName);
int maxPartitionId = accessor.getMaxPartitionId();
List<K> unmappedKeys = new ArrayList<K>();
Map<Integer, Set<K>> partitionSet = getPartitionSet(keys, accessor, unmappedKeys);
final LoadBalancerState.SchemeStrategyPair pair = orderedStrategies.get(0);
//get the partitionId -> host URIs list
Map<Integer, KeysAndHosts<K>> partitionDataMap = new HashMap<Integer, KeysAndHosts<K>>();
for (Integer partitionId : partitionSet.keySet()) {
Set<URI> possibleUris = uris.getUriBySchemeAndPartition(pair.getScheme(), partitionId);
List<TrackerClient> trackerClients = getPotentialClients(serviceName, service, possibleUris);
int size = trackerClients.size() <= limitHostPerPartition ? trackerClients.size() : limitHostPerPartition;
List<URI> rankedUri = new ArrayList<URI>(size);
Ring<URI> ring = pair.getStrategy().getRing(uriItem.getVersion(), partitionId, trackerClients);
Iterator<URI> iterator = ring.getIterator(hash);
while (iterator.hasNext() && rankedUri.size() < size) {
URI uri = iterator.next();
if (!rankedUri.contains(uri)) {
rankedUri.add(uri);
}
}
if (rankedUri.size() < limitHostPerPartition) {
partitionWithoutEnoughHost.put(partitionId, limitHostPerPartition - rankedUri.size());
}
KeysAndHosts<K> keysAndHosts = new KeysAndHosts<K>(partitionSet.get(partitionId), rankedUri);
partitionDataMap.put(partitionId, keysAndHosts);
}
return new HostToKeyMapper<K>(unmappedKeys, partitionDataMap, limitHostPerPartition, maxPartitionId + 1, partitionWithoutEnoughHost);
} else {
throw new ServiceUnavailableException(serviceName, "Unable to find a load balancer strategy");
}
}
use of com.linkedin.d2.poolStrategyType in project rest.li by linkedin.
the class PartitionPropertiesConverterTest method testHashModuloPartitionProperties.
@Test
public void testHashModuloPartitionProperties() {
final String partitionKeyRegex = "/foo/bar/(\\d+)";
final int partitionCount = 16;
final HashBasedPartitionProperties.HashAlgorithm hashAlgorithm = HashBasedPartitionProperties.HashAlgorithm.MODULO;
PartitionProperties partitionProperties = new HashBasedPartitionProperties(partitionKeyRegex, partitionCount, hashAlgorithm);
D2ClusterPartitionConfiguration.PartitionTypeSpecificData data = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
data.setHashAlgorithm(com.linkedin.d2.hashAlgorithm.MODULO);
D2ClusterPartitionConfiguration partitionConfig = new D2ClusterPartitionConfiguration().setType(PartitionTypeEnum.HASH).setPartitionKeyRegex(partitionKeyRegex).setPartitionCount(partitionCount).setPartitionTypeSpecificData(data);
Assert.assertEquals(PartitionPropertiesConverter.toConfig(partitionProperties), partitionConfig);
Assert.assertEquals(PartitionPropertiesConverter.toProperties(partitionConfig), partitionProperties);
}
use of com.linkedin.d2.poolStrategyType in project rest.li by linkedin.
the class TransportClientPropertiesConverterTest method testTransportClientPropertiesConverter.
@Test
public void testTransportClientPropertiesConverter() {
final Integer queryPostThreshold = 8192;
final Long requestTimeout = 10000l;
final Long maxResponseSize = 1003300l;
final Integer poolSize = 200;
final Integer poolWaiterSize = 32768;
final Long idleTimeout = 600000l;
final Long shutdownTimeout = 50000l;
final List<String> responseCompressionRaw = Arrays.asList("finder:*");
final List<String> responseContentEncoding = Arrays.asList("gzip", "snappy");
final List<String> requestContentEncoding = Arrays.asList("lz4", "identity");
final Boolean useResponseCompression = true;
final Integer maxHeaderSize = 8192;
final Integer maxChunkSize = 4096;
final poolStrategyType poolStrategy = poolStrategyType.LRU;
final Integer minPoolSize = 5;
final Integer maxConcurrentConnections = 1000;
final HttpProtocolVersionType protocolVersion = HttpProtocolVersionType.HTTP_1_1;
final List<String> allowedClientOverrideKeys = Arrays.asList(PropertyKeys.HTTP_REQUEST_TIMEOUT, PropertyKeys.HTTP_QUERY_POST_THRESHOLD);
Map<String, Object> transportClientProperties = new HashMap<>();
transportClientProperties.put(PropertyKeys.HTTP_QUERY_POST_THRESHOLD, queryPostThreshold.toString());
transportClientProperties.put(PropertyKeys.HTTP_REQUEST_TIMEOUT, requestTimeout.toString());
transportClientProperties.put(PropertyKeys.HTTP_MAX_RESPONSE_SIZE, maxResponseSize.toString());
transportClientProperties.put(PropertyKeys.HTTP_POOL_SIZE, poolSize.toString());
transportClientProperties.put(PropertyKeys.HTTP_POOL_WAITER_SIZE, poolWaiterSize.toString());
transportClientProperties.put(PropertyKeys.HTTP_IDLE_TIMEOUT, idleTimeout.toString());
transportClientProperties.put(PropertyKeys.HTTP_SHUTDOWN_TIMEOUT, shutdownTimeout.toString());
transportClientProperties.put(PropertyKeys.HTTP_RESPONSE_COMPRESSION_OPERATIONS, responseCompressionRaw.stream().collect(Collectors.joining(",")));
transportClientProperties.put(PropertyKeys.HTTP_RESPONSE_CONTENT_ENCODINGS, responseContentEncoding.stream().collect(Collectors.joining(",")));
transportClientProperties.put(PropertyKeys.HTTP_REQUEST_CONTENT_ENCODINGS, requestContentEncoding.stream().collect(Collectors.joining(",")));
transportClientProperties.put(PropertyKeys.HTTP_USE_RESPONSE_COMPRESSION, useResponseCompression.toString());
transportClientProperties.put(PropertyKeys.HTTP_MAX_HEADER_SIZE, maxHeaderSize.toString());
transportClientProperties.put(PropertyKeys.HTTP_MAX_CHUNK_SIZE, maxChunkSize.toString());
transportClientProperties.put(PropertyKeys.HTTP_POOL_STRATEGY, poolStrategy.name());
transportClientProperties.put(PropertyKeys.HTTP_POOL_MIN_SIZE, minPoolSize.toString());
transportClientProperties.put(PropertyKeys.HTTP_MAX_CONCURRENT_CONNECTIONS, maxConcurrentConnections.toString());
transportClientProperties.put(PropertyKeys.HTTP_PROTOCOL_VERSION, protocolVersion.name());
transportClientProperties.put(PropertyKeys.ALLOWED_CLIENT_OVERRIDE_KEYS, allowedClientOverrideKeys.stream().collect(Collectors.joining(",")));
D2TransportClientProperties d2TransportClientProperties = new D2TransportClientProperties().setQueryPostThreshold(queryPostThreshold).setRequestTimeout(requestTimeout).setMaxResponseSize(maxResponseSize).setPoolSize(poolSize).setPoolWaiterSize(poolWaiterSize).setIdleTimeout(idleTimeout).setShutdownTimeout(shutdownTimeout).setResponseCompressionOperations(new StringArray(responseCompressionRaw)).setResponseContentEncodings(new StringArray(responseContentEncoding)).setRequestContentEncodings(new StringArray(requestContentEncoding)).setUseResponseCompression(useResponseCompression).setMaxHeaderSize(maxHeaderSize).setMaxChunkSize(maxChunkSize).setPoolStrategy(poolStrategy).setMinPoolSize(minPoolSize).setMaxConcurrentConnections(maxConcurrentConnections).setProtocolVersion(protocolVersion).setAllowedClientOverrideKeys(new StringArray(allowedClientOverrideKeys));
Assert.assertEquals(TransportClientPropertiesConverter.toConfig(transportClientProperties), d2TransportClientProperties);
Assert.assertEquals(TransportClientPropertiesConverter.toProperties(d2TransportClientProperties), transportClientProperties);
}
use of com.linkedin.d2.poolStrategyType in project rest.li by linkedin.
the class PartitionPropertiesConverterTest method testHashMD5PartitionProperties.
@Test
public void testHashMD5PartitionProperties() {
final String partitionKeyRegex = "/foo/bar/(\\d+)";
final int partitionCount = 8;
final HashBasedPartitionProperties.HashAlgorithm hashAlgorithm = HashBasedPartitionProperties.HashAlgorithm.MD5;
PartitionProperties partitionProperties = new HashBasedPartitionProperties(partitionKeyRegex, partitionCount, hashAlgorithm);
D2ClusterPartitionConfiguration.PartitionTypeSpecificData data = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
data.setHashAlgorithm(com.linkedin.d2.hashAlgorithm.MD5);
D2ClusterPartitionConfiguration partitionConfig = new D2ClusterPartitionConfiguration().setType(PartitionTypeEnum.HASH).setPartitionKeyRegex(partitionKeyRegex).setPartitionCount(partitionCount).setPartitionTypeSpecificData(data);
Assert.assertEquals(PartitionPropertiesConverter.toProperties(partitionConfig), partitionProperties);
Assert.assertEquals(PartitionPropertiesConverter.toConfig(partitionProperties), partitionConfig);
}
Aggregations