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