Search in sources :

Example 51 with ServiceProperties

use of com.linkedin.d2.balancer.properties.ServiceProperties in project rest.li by linkedin.

the class SimpleLoadBalancer method getRings.

public Map<Integer, Ring<URI>> getRings(URI serviceUri) throws ServiceUnavailableException {
    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());
    if (!orderedStrategies.isEmpty()) {
        final LoadBalancerState.SchemeStrategyPair pair = orderedStrategies.get(0);
        final PartitionAccessor accessor = getPartitionAccessor(serviceName, clusterName);
        int maxPartitionId = accessor.getMaxPartitionId();
        Map<Integer, Ring<URI>> ringMap = new HashMap<Integer, Ring<URI>>((maxPartitionId + 1) * 2);
        for (int partitionId = 0; partitionId <= maxPartitionId; partitionId++) {
            Set<URI> possibleUris = uris.getUriBySchemeAndPartition(pair.getScheme(), partitionId);
            List<TrackerClient> trackerClients = getPotentialClients(serviceName, service, possibleUris);
            Ring<URI> ring = pair.getStrategy().getRing(uriItem.getVersion(), partitionId, trackerClients);
            // ring will never be null; it can be empty
            ringMap.put(partitionId, ring);
        }
        return ringMap;
    } else {
        throw new ServiceUnavailableException(serviceName, "Unable to find a load balancer strategy");
    }
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) URI(java.net.URI) LoadBalancerState(com.linkedin.d2.balancer.LoadBalancerState) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) PartitionAccessor(com.linkedin.d2.balancer.util.partitions.PartitionAccessor) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) Ring(com.linkedin.d2.balancer.util.hashing.Ring) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties)

Example 52 with ServiceProperties

use of com.linkedin.d2.balancer.properties.ServiceProperties in project rest.li by linkedin.

the class SimpleLoadBalancerState method refreshServiceStrategies.

void refreshServiceStrategies(ServiceProperties serviceProperties) {
    info(_log, "refreshing service strategies for service: ", serviceProperties);
    List<String> strategyList = serviceProperties.getLoadBalancerStrategyList();
    LoadBalancerStrategyFactory<? extends LoadBalancerStrategy> factory = null;
    if (strategyList != null && !strategyList.isEmpty()) {
        // waiting for all clients to update their code level before any clients can use it.
        for (String strategy : strategyList) {
            factory = _loadBalancerStrategyFactories.get(strategy);
            if (factory != null) {
                break;
            }
        }
    }
    // to refreshServiceStrategies without the strategy existing yet.
    if (factory == null) {
        warn(_log, "No valid strategy found. ", serviceProperties);
    }
    Map<String, LoadBalancerStrategy> strategyMap = new ConcurrentHashMap<String, LoadBalancerStrategy>();
    if (factory != null && serviceProperties.getPrioritizedSchemes() != null && !serviceProperties.getPrioritizedSchemes().isEmpty()) {
        List<String> schemes = serviceProperties.getPrioritizedSchemes();
        for (String scheme : schemes) {
            Map<String, Object> loadBalancerStrategyProperties = new HashMap<String, Object>(serviceProperties.getLoadBalancerStrategyProperties());
            // Save the service path as a property -- Quarantine may need this info to construct correct
            // health checking path
            loadBalancerStrategyProperties.put(PropertyKeys.PATH, serviceProperties.getPath());
            LoadBalancerStrategy strategy = factory.newLoadBalancer(serviceProperties.getServiceName(), loadBalancerStrategyProperties, serviceProperties.getDegraderProperties());
            strategyMap.put(scheme, strategy);
        }
    } else {
        warn(_log, "unable to find cluster or factory for ", serviceProperties, ": ", factory);
    }
    Map<String, LoadBalancerStrategy> oldStrategies = _serviceStrategies.put(serviceProperties.getServiceName(), strategyMap);
    _serviceStrategiesCache.remove(serviceProperties.getServiceName());
    info(_log, "removing strategies ", serviceProperties.getServiceName(), ": ", oldStrategies);
    info(_log, "putting strategies ", serviceProperties.getServiceName(), ": ", strategyMap);
    // notify listeners of the removed strategy
    if (oldStrategies != null) {
        // shutdown strategies before notification
        oldStrategies.values().forEach(LoadBalancerStrategy::shutdown);
        for (SimpleLoadBalancerStateListener listener : _listeners) {
            for (Map.Entry<String, LoadBalancerStrategy> oldStrategy : oldStrategies.entrySet()) {
                listener.onStrategyRemoved(serviceProperties.getServiceName(), oldStrategy.getKey(), oldStrategy.getValue());
            }
        }
    }
    // they will get confused and remove what was just added.
    if (!strategyMap.isEmpty()) {
        for (SimpleLoadBalancerStateListener listener : _listeners) {
            // notify listeners of the added strategy
            for (Map.Entry<String, LoadBalancerStrategy> newStrategy : strategyMap.entrySet()) {
                listener.onStrategyAdded(serviceProperties.getServiceName(), newStrategy.getKey(), newStrategy.getValue());
            }
        }
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 53 with ServiceProperties

use of com.linkedin.d2.balancer.properties.ServiceProperties in project rest.li by linkedin.

the class SimpleLoadBalancer method listenToServiceAndCluster.

private ServiceProperties listenToServiceAndCluster(URI uri) throws ServiceUnavailableException {
    if (!D2_SCHEME_NAME.equalsIgnoreCase(uri.getScheme())) {
        throw new IllegalArgumentException("Unsupported scheme in URI " + uri);
    }
    // get the service for this uri
    String serviceName = LoadBalancerUtil.getServiceNameFromUri(uri);
    ServiceProperties service = getLoadBalancedServiceProperties(serviceName);
    String clusterName = service.getClusterName();
    listenToCluster(serviceName, clusterName);
    return service;
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties)

Example 54 with ServiceProperties

use of com.linkedin.d2.balancer.properties.ServiceProperties in project rest.li by linkedin.

the class SimpleLoadBalancer method getPartitionAccessor.

@Override
public PartitionAccessor getPartitionAccessor(URI serviceUri) throws ServiceUnavailableException {
    ServiceProperties service = listenToServiceAndCluster(serviceUri);
    String serviceName = service.getServiceName();
    String clusterName = service.getClusterName();
    return getPartitionAccessor(serviceName, clusterName);
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties)

Example 55 with ServiceProperties

use of com.linkedin.d2.balancer.properties.ServiceProperties in project rest.li by linkedin.

the class LoadBalancerEchoClient method getLoadBalancer.

public static SimpleLoadBalancer getLoadBalancer(String hostPort) throws IOException, PropertyStoreException {
    // zk stores
    ZooKeeperPermanentStore<ClusterProperties> zkClusterRegistry = null;
    ZooKeeperPermanentStore<ServiceProperties> zkServiceRegistry = null;
    ZooKeeperEphemeralStore<UriProperties> zkUriRegistry = null;
    ZKConnection zkClient = new ZKConnection(hostPort, 10000);
    zkClusterRegistry = new ZooKeeperPermanentStore<ClusterProperties>(zkClient, new ClusterPropertiesJsonSerializer(), _basePath + "/clusters");
    zkServiceRegistry = new ZooKeeperPermanentStore<ServiceProperties>(zkClient, new ServicePropertiesJsonSerializer(), _basePath + "/services");
    zkUriRegistry = new ZooKeeperEphemeralStore<UriProperties>(zkClient, new UriPropertiesJsonSerializer(), new UriPropertiesMerger(), _basePath + "/uris", false, true);
    // fs stores
    File testDirectory = LoadBalancerUtil.createTempDirectory("lb-degrader-witih-file-store-large");
    testDirectory.deleteOnExit();
    new File(testDirectory + File.separator + "cluster").mkdir();
    new File(testDirectory + File.separator + "service").mkdir();
    new File(testDirectory + File.separator + "uri").mkdir();
    FileStore<ClusterProperties> fsClusterStore = new FileStore<ClusterProperties>(testDirectory + File.separator + "cluster", ".ini", new ClusterPropertiesJsonSerializer());
    FileStore<ServiceProperties> fsServiceStore = new FileStore<ServiceProperties>(testDirectory + File.separator + "service", ".ini", new ServicePropertiesJsonSerializer());
    FileStore<UriProperties> fsUriStore = new FileStore<UriProperties>(testDirectory + File.separator + "uri", ".ini", new UriPropertiesJsonSerializer());
    // chains
    PropertyEventThread thread = new PropertyEventThread("echo client event thread");
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("echo client event thread"));
    // start up the world
    thread.start();
    PropertyEventBus<ServiceProperties> serviceBus = new PropertyEventBusImpl<ServiceProperties>(executorService, zkServiceRegistry);
    serviceBus.register(fsServiceStore);
    new ZooKeeperTogglingStore<ServiceProperties>(zkServiceRegistry, fsServiceStore, serviceBus, true);
    PropertyEventBus<UriProperties> uriBus = new PropertyEventBusImpl<UriProperties>(executorService, zkUriRegistry);
    uriBus.register(fsUriStore);
    new ZooKeeperTogglingStore<UriProperties>(zkUriRegistry, fsUriStore, uriBus, true);
    PropertyEventBus<ClusterProperties> clusterBus = new PropertyEventBusImpl<ClusterProperties>(executorService, zkClusterRegistry);
    clusterBus.register(fsClusterStore);
    new ZooKeeperTogglingStore<ClusterProperties>(zkClusterRegistry, fsClusterStore, clusterBus, true);
    Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>>();
    // strategy and scheme factories
    loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
    Map<String, TransportClientFactory> clientFactories = new HashMap<String, TransportClientFactory>();
    clientFactories.put("http", new HttpClientFactory());
    // create the state
    SimpleLoadBalancerState state = new SimpleLoadBalancerState(executorService, uriBus, clusterBus, serviceBus, clientFactories, loadBalancerStrategyFactories, null, null, false);
    SimpleLoadBalancer balancer = new SimpleLoadBalancer(state, 5, TimeUnit.SECONDS);
    new JmxManager().registerLoadBalancer("balancer", balancer).registerLoadBalancerState("state", state);
    return balancer;
}
Also used : ServicePropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer) HashMap(java.util.HashMap) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) UriPropertiesMerger(com.linkedin.d2.balancer.properties.UriPropertiesMerger) ZooKeeperTogglingStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperTogglingStore) UriPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer) JmxManager(com.linkedin.d2.jmx.JmxManager) ClusterPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SimpleLoadBalancerState(com.linkedin.d2.balancer.simple.SimpleLoadBalancerState) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) ZKConnection(com.linkedin.d2.discovery.stores.zk.ZKConnection) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) FileStore(com.linkedin.d2.discovery.stores.file.FileStore) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) PropertyEventBusImpl(com.linkedin.d2.discovery.event.PropertyEventBusImpl) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) File(java.io.File) PropertyEventThread(com.linkedin.d2.discovery.event.PropertyEventThread)

Aggregations

ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)53 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)37 HashMap (java.util.HashMap)36 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)33 URI (java.net.URI)30 Test (org.testng.annotations.Test)29 ArrayList (java.util.ArrayList)28 Map (java.util.Map)22 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)21 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)19 DegraderLoadBalancerTest (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)19 NullStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback)17 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)15 ServicePropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer)14 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)14 HashSet (java.util.HashSet)12 UriPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer)11 None (com.linkedin.common.util.None)10 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)10 FutureCallback (com.linkedin.common.callback.FutureCallback)9