Search in sources :

Example 1 with LoadBalancerClusterListener

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

the class SimpleLoadBalancerState method shutdown.

@Override
public void shutdown(final PropertyEventShutdownCallback shutdown) {
    trace(_log, "shutdown");
    // shutdown all three registries, all tracker clients, and the event thread
    _executor.execute(new PropertyEvent("shutdown load balancer state") {

        @Override
        public void innerRun() {
            // Need to shutdown loadBalancerStrategies before the transportClients are shutdown
            for (Map<String, LoadBalancerStrategy> strategyEntry : _serviceStrategies.values()) {
                strategyEntry.values().forEach(LoadBalancerStrategy::shutdown);
            }
            // put all tracker clients into a single set for convenience
            Set<TransportClient> transportClients = new HashSet<>();
            for (Map<String, TransportClient> clientsByScheme : _serviceClients.values()) {
                transportClients.addAll(clientsByScheme.values());
            }
            Callback<None> trackerCallback = Callbacks.countDown(Callbacks.<None>adaptSimple(new SimpleCallback() {

                @Override
                public void onDone() {
                    shutdown.done();
                }
            }), transportClients.size());
            info(_log, "shutting down cluster clients");
            for (TransportClient transportClient : transportClients) {
                transportClient.shutdown(trackerCallback);
            }
            // so it is needed to notify all the listeners
            for (SimpleLoadBalancerStateListener listener : _listeners) {
                // Notify the strategy removal
                for (Map.Entry<String, Map<String, LoadBalancerStrategy>> serviceStrategy : _serviceStrategies.entrySet()) {
                    for (Map.Entry<String, LoadBalancerStrategy> strategyEntry : serviceStrategy.getValue().entrySet()) {
                        listener.onStrategyRemoved(serviceStrategy.getKey(), strategyEntry.getKey(), strategyEntry.getValue());
                    }
                    // Also notify the client removal
                    Map<URI, TrackerClient> trackerClients = _trackerClients.get(serviceStrategy.getKey());
                    if (trackerClients != null) {
                        for (TrackerClient client : trackerClients.values()) {
                            listener.onClientRemoved(serviceStrategy.getKey(), client);
                        }
                    }
                }
            }
            // When SimpleLoadBalancerStateis shutdown, all the cluster listener also need to be notified.
            for (LoadBalancerClusterListener clusterListener : _clusterListeners) {
                for (String clusterName : _clusterInfo.keySet()) {
                    clusterListener.onClusterRemoved(clusterName);
                }
            }
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) LoadBalancerClusterListener(com.linkedin.d2.balancer.LoadBalancerClusterListener) PropertyEvent(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEvent) RelativeLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) Callback(com.linkedin.common.callback.Callback) SimpleCallback(com.linkedin.common.callback.SimpleCallback) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) None(com.linkedin.common.util.None) SimpleCallback(com.linkedin.common.callback.SimpleCallback)

Example 2 with LoadBalancerClusterListener

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

the class SimpleLoadBalancerState method unregisterClusterListener.

@Override
public void unregisterClusterListener(final LoadBalancerClusterListener listener) {
    trace(_log, "unregister listener: ", listener);
    _executor.execute(new PropertyEvent("remove cluster listener for state") {

        @Override
        public void innerRun() {
            _clusterListeners.remove(listener);
        }
    });
}
Also used : PropertyEvent(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEvent)

Example 3 with LoadBalancerClusterListener

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

the class SimpleLoadBalancerState method registerClusterListener.

@Override
public void registerClusterListener(final LoadBalancerClusterListener listener) {
    trace(_log, "register listener: ", listener);
    _executor.execute(new PropertyEvent("add cluster listener for state") {

        @Override
        public void innerRun() {
            if (!_clusterListeners.contains(listener)) {
                // don't allow duplicates, there's no need for a cluster listener to be registered twice.
                _clusterListeners.add(listener);
            }
        }
    });
}
Also used : PropertyEvent(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEvent)

Aggregations

PropertyEvent (com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEvent)3 Callback (com.linkedin.common.callback.Callback)1 SimpleCallback (com.linkedin.common.callback.SimpleCallback)1 None (com.linkedin.common.util.None)1 LoadBalancerClusterListener (com.linkedin.d2.balancer.LoadBalancerClusterListener)1 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)1 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)1 RelativeLoadBalancerStrategy (com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy)1 PropertyEventShutdownCallback (com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback)1 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1