Search in sources :

Example 1 with ClusterInfoItem

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

the class ClusterInfoJmxTest method testGetCanaryDistributionPolicy.

@Test(dataProvider = "getCanaryDistributionPoliciesTestData")
public void testGetCanaryDistributionPolicy(CanaryDistributionProvider.Distribution distribution, int expectedValue) {
    ClusterInfoJmx clusterInfoJmx = new ClusterInfoJmx(new ClusterInfoItem(_mockedSimpleBalancerState, new ClusterProperties("Foo"), new PartitionAccessor() {

        @Override
        public int getMaxPartitionId() {
            return 0;
        }

        @Override
        public int getPartitionId(URI uri) {
            return 0;
        }
    }, distribution));
    Assert.assertEquals(clusterInfoJmx.getCanaryDistributionPolicy(), expectedValue);
}
Also used : PartitionAccessor(com.linkedin.d2.balancer.util.partitions.PartitionAccessor) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) ClusterInfoItem(com.linkedin.d2.balancer.simple.ClusterInfoItem) URI(java.net.URI) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 2 with ClusterInfoItem

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

the class JmxManagerTest method testRegisterJmxBeansSourceObjects.

@Test(dataProvider = "getJmxBeansSourceObjects", invocationCount = 2)
public void testRegisterJmxBeansSourceObjects(Object jmxBeanSourceObject) {
    String name = "Bar";
    ObjectName jmxObjName = null;
    try {
        jmxObjName = _jmxManager.getName(name);
    } catch (MalformedObjectNameException e) {
        Assert.fail("Unexpected bad JMX object name: " + e.getMessage());
    }
    if (jmxBeanSourceObject instanceof ServiceProperties) {
        LoadBalancerStateItem<ServiceProperties> servicePropertiesLoadBalancerStateItem = new LoadBalancerStateItem<>((ServiceProperties) jmxBeanSourceObject, 0, 0, CanaryDistributionProvider.Distribution.CANARY);
        _jmxManager.registerServiceProperties(name, servicePropertiesLoadBalancerStateItem);
        try {
            Assert.assertEquals(_jmxManager.getMBeanServer().getAttribute(jmxObjName, "ServicePropertiesLBStateItem"), servicePropertiesLoadBalancerStateItem);
        } catch (Exception e) {
            Assert.fail("Failed to check MBean attribute: " + e.getMessage());
        }
    } else if (jmxBeanSourceObject instanceof ClusterInfoItem) {
        _jmxManager.registerClusterInfo(name, (ClusterInfoItem) jmxBeanSourceObject);
        try {
            Assert.assertEquals(_jmxManager.getMBeanServer().getAttribute(jmxObjName, "ClusterInfoItem"), jmxBeanSourceObject);
        } catch (Exception e) {
            Assert.fail("Failed to check MBean attribute: " + e.getMessage());
        }
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) LoadBalancerStateItem(com.linkedin.d2.balancer.LoadBalancerStateItem) ClusterInfoItem(com.linkedin.d2.balancer.simple.ClusterInfoItem) MalformedObjectNameException(javax.management.MalformedObjectNameException) ObjectName(javax.management.ObjectName) Test(org.testng.annotations.Test)

Example 3 with ClusterInfoItem

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

the class SimpleLoadBalancerStateTest method testNotifyListenersOnPropertiesChanges.

@Test
public void testNotifyListenersOnPropertiesChanges() {
    reset();
    ClusterProperties clusterProperties = new ClusterProperties("cluster-1", Collections.singletonList("Random"));
    ClusterInfoItem clusterInfoItem = new ClusterInfoItem(_state, clusterProperties, new PartitionAccessor() {

        @Override
        public int getMaxPartitionId() {
            return 0;
        }

        @Override
        public int getPartitionId(URI uri) throws PartitionAccessException {
            return 0;
        }
    });
    ServiceProperties serviceProperties = new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"));
    LoadBalancerStateItem<ServiceProperties> servicePropertiesLBItem = new LoadBalancerStateItem<ServiceProperties>(serviceProperties, 0, 0);
    TestListener[] listeners = new TestListener[] { new TestListener(), new TestListener() };
    Arrays.stream(listeners).forEach(listener -> _state.register(listener));
    _state.notifyListenersOnServicePropertiesUpdates(servicePropertiesLBItem);
    _state.notifyListenersOnClusterInfoUpdates(clusterInfoItem);
    for (TestListener listener : listeners) {
        Assert.assertEquals(listener.clusterInfoUpdated.size(), 1);
        Assert.assertEquals(listener.clusterInfoUpdated.get(0).getClusterPropertiesItem().getProperty(), clusterProperties);
        Assert.assertEquals(listener.servicePropertiesUpdated.size(), 1);
        Assert.assertEquals(listener.servicePropertiesUpdated.get(0).getProperty(), serviceProperties);
    }
    _state.notifyListenersOnServicePropertiesRemovals(servicePropertiesLBItem);
    _state.notifyListenersOnClusterInfoRemovals(clusterInfoItem);
    for (TestListener listener : listeners) {
        Assert.assertEquals(listener.clusterInfoRemoved.size(), 1);
        Assert.assertEquals(listener.clusterInfoRemoved.get(0).getClusterPropertiesItem().getProperty(), clusterProperties);
        Assert.assertEquals(listener.servicePropertiesRemoved.size(), 1);
        Assert.assertEquals(listener.servicePropertiesRemoved.get(0).getProperty(), serviceProperties);
    }
}
Also used : DefaultPartitionAccessor(com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor) PartitionAccessor(com.linkedin.d2.balancer.util.partitions.PartitionAccessor) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) LoadBalancerStateItem(com.linkedin.d2.balancer.LoadBalancerStateItem) URI(java.net.URI) PartitionAccessException(com.linkedin.d2.balancer.util.partitions.PartitionAccessException) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 4 with ClusterInfoItem

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

the class ClusterLoadBalancerSubscriberTest method testHandleRemove.

@Test
public void testHandleRemove() {
    String clusterName = "mock-cluster-foo";
    ClusterLoadBalancerSubscriberFixture fixture = new ClusterLoadBalancerSubscriberFixture();
    ClusterInfoItem clusterInfoItemToRemove = new ClusterInfoItem(fixture._simpleLoadBalancerState, new ClusterProperties(clusterName), new PartitionAccessor() {

        @Override
        public int getMaxPartitionId() {
            return 0;
        }

        @Override
        public int getPartitionId(URI uri) {
            return 0;
        }
    }, CanaryDistributionProvider.Distribution.CANARY);
    fixture._simpleLoadBalancerState.getClusterInfo().put(clusterName, clusterInfoItemToRemove);
    fixture.getMockSubscriber(false).handleRemove(clusterName);
    Assert.assertFalse(fixture._simpleLoadBalancerState.getClusterInfo().containsKey(clusterName));
    verify(fixture._simpleLoadBalancerState, times(1)).notifyListenersOnClusterInfoRemovals(clusterInfoItemToRemove);
    verify(fixture._simpleLoadBalancerState, times(1)).notifyClusterListenersOnRemove(clusterName);
}
Also used : PartitionAccessor(com.linkedin.d2.balancer.util.partitions.PartitionAccessor) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 5 with ClusterInfoItem

use of com.linkedin.d2.balancer.simple.ClusterInfoItem 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) {
                // Send removal notifications for service properties.
                for (LoadBalancerStateItem<ServiceProperties> serviceProperties : _serviceProperties.values()) {
                    listener.onServicePropertiesRemoval(serviceProperties);
                }
                // Send removal notification for cluster properties.
                for (ClusterInfoItem clusterInfoItem : _clusterInfo.values()) {
                    listener.onClusterInfoRemoval(clusterInfoItem);
                }
                // 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 SimpleLoadBalancerState is 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) LoadBalancerStateItem(com.linkedin.d2.balancer.LoadBalancerStateItem) 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)

Aggregations

ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)6 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)5 ClusterInfoItem (com.linkedin.d2.balancer.simple.ClusterInfoItem)5 PartitionAccessor (com.linkedin.d2.balancer.util.partitions.PartitionAccessor)5 URI (java.net.URI)5 Test (org.testng.annotations.Test)4 LoadBalancerStateItem (com.linkedin.d2.balancer.LoadBalancerStateItem)3 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)2 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)2 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 SimpleLoadBalancerStateListener (com.linkedin.d2.balancer.simple.SimpleLoadBalancerState.SimpleLoadBalancerStateListener)1 DegraderLoadBalancerTest (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)1 RelativeLoadBalancerStrategy (com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy)1 DefaultPartitionAccessor (com.linkedin.d2.balancer.util.partitions.DefaultPartitionAccessor)1 PartitionAccessException (com.linkedin.d2.balancer.util.partitions.PartitionAccessException)1 PropertyEvent (com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEvent)1 PropertyEventShutdownCallback (com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback)1