use of com.linkedin.d2.jmx.JmxManager in project rest.li by linkedin.
the class D2LoadBalancerJmxTest method testD2ClientJmxManagerRegisteringStrategies.
/**
* NOTE: when you find yourself modifying this test, make sure you are modifying it in a BACKWARD-COMPATIBLE way.
*/
@Test
private void testD2ClientJmxManagerRegisteringStrategies() {
JmxManager mockJmxManager = mock(JmxManager.class);
D2ClientJmxManager d2ClientJmxManager = new D2ClientJmxManager(DUMMY_STRING, mockJmxManager);
SimpleLoadBalancerState simpleLoadBalancerState = mock(SimpleLoadBalancerState.class);
d2ClientJmxManager.setSimpleLoadBalancerState(simpleLoadBalancerState);
ArgumentCaptor<SimpleLoadBalancerState.SimpleLoadBalancerStateListener> captor = ArgumentCaptor.forClass(SimpleLoadBalancerState.SimpleLoadBalancerStateListener.class);
// check it is registering the strategy correctly
Mockito.verify(simpleLoadBalancerState).register(captor.capture());
captor.getValue().onStrategyAdded(DUMMY_STRING, DUMMY_STRING, mock(LoadBalancerStrategy.class));
verify(mockJmxManager, times(1)).registerLoadBalancerStrategy(anyString(), any());
verify(mockJmxManager, times(0)).unregister(anyString());
// check it is unregistering correctly
captor.getValue().onStrategyRemoved(DUMMY_STRING, DUMMY_STRING, mock(LoadBalancerStrategy.class));
verify(mockJmxManager, times(1)).registerLoadBalancerStrategy(anyString(), any());
verify(mockJmxManager, times(1)).unregister(anyString());
// this should not trigger anything in the current version
captor.getValue().onClientAdded(DUMMY_STRING, mock(TrackerClient.class));
captor.getValue().onClientRemoved(DUMMY_STRING, mock(TrackerClient.class));
verify(mockJmxManager, times(1)).registerLoadBalancerStrategy(anyString(), any());
verify(mockJmxManager, times(1)).unregister(anyString());
}
use of com.linkedin.d2.jmx.JmxManager in project rest.li by linkedin.
the class D2LoadBalancerJmxTest method testRegisteringJmx.
/**
* Verify that all components are registered correctly at LB creation
*/
@Test(dataProvider = "loadBalancerFactories")
private void testRegisteringJmx(LoadBalancerWithFacilitiesFactory lbWithFacilitiesFactory) throws Exception {
setUpZK();
JmxManager jmxManager = mock(JmxManager.class);
FutureCallback<None> startCallback = new FutureCallback<>();
D2Client d2Client = getD2Client(lbWithFacilitiesFactory, jmxManager);
d2Client.start(startCallback);
startCallback.get();
verify(jmxManager, times(1)).registerLoadBalancer(any(), any());
verify(jmxManager, times(1)).registerLoadBalancerState(any(), any());
// uri, service and cluster stores
verify(jmxManager, times(3)).registerFileStore(any(), any());
// ZK might take a little before booting up and registering the stores
AssertionMethods.assertWithTimeout(10000, () -> {
// uri store
verify(jmxManager, times(1)).registerZooKeeperEphemeralStore(any(), any());
// service and cluster stores
verify(jmxManager, times(2)).registerZooKeeperPermanentStore(any(), any());
});
tearDownZK();
}
use of com.linkedin.d2.jmx.JmxManager 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<>(zkClient, new ClusterPropertiesJsonSerializer(), _basePath + "/clusters");
zkServiceRegistry = new ZooKeeperPermanentStore<>(zkClient, new ServicePropertiesJsonSerializer(), _basePath + "/services");
zkUriRegistry = new ZooKeeperEphemeralStore<>(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<>(testDirectory + File.separator + "cluster", FileSystemDirectory.FILE_STORE_EXTENSION, new ClusterPropertiesJsonSerializer());
FileStore<ServiceProperties> fsServiceStore = new FileStore<>(testDirectory + File.separator + "service", FileSystemDirectory.FILE_STORE_EXTENSION, new ServicePropertiesJsonSerializer());
FileStore<UriProperties> fsUriStore = new FileStore<>(testDirectory + File.separator + "uri", FileSystemDirectory.FILE_STORE_EXTENSION, 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<>(executorService, zkServiceRegistry);
serviceBus.register(fsServiceStore);
new ZooKeeperTogglingStore<>(zkServiceRegistry, fsServiceStore, serviceBus, true);
PropertyEventBus<UriProperties> uriBus = new PropertyEventBusImpl<>(executorService, zkUriRegistry);
uriBus.register(fsUriStore);
new ZooKeeperTogglingStore<>(zkUriRegistry, fsUriStore, uriBus, true);
PropertyEventBus<ClusterProperties> clusterBus = new PropertyEventBusImpl<>(executorService, zkClusterRegistry);
clusterBus.register(fsClusterStore);
new ZooKeeperTogglingStore<>(zkClusterRegistry, fsClusterStore, clusterBus, true);
Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<>();
// strategy and scheme factories
loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
Map<String, TransportClientFactory> clientFactories = new HashMap<>();
clientFactories.put("http", new HttpClientFactory.Builder().build());
// create the state
SimpleLoadBalancerState state = new SimpleLoadBalancerState(executorService, uriBus, clusterBus, serviceBus, clientFactories, loadBalancerStrategyFactories, null, null, false);
SimpleLoadBalancer balancer = new SimpleLoadBalancer(state, 5, TimeUnit.SECONDS, executorService);
new JmxManager().registerLoadBalancer("balancer", balancer).registerLoadBalancerState("state", state);
return balancer;
}
use of com.linkedin.d2.jmx.JmxManager in project rest.li by linkedin.
the class SimpleLoadBalancerSimulation method reset.
/**
* Reset the entire state of the simulation.
*/
public void reset() {
// simulation state
_random = new Random();
_possibleServices = Collections.synchronizedList(new ArrayList<>());
_possibleClusters = Collections.synchronizedList(new ArrayList<>());
_possiblePaths = Collections.synchronizedList(new ArrayList<>());
_possibleSchemes = Collections.synchronizedList(new ArrayList<>());
_possibleStrategies = Collections.synchronizedList(new ArrayList<>());
_possibleUris = Collections.synchronizedList(new ArrayList<>());
// load balancer state
_executorService = Executors.newSingleThreadScheduledExecutor();
;
// pretend that these are zk stores
_serviceRegistry = new MockStore<>();
_uriRegistry = new MockStore<>();
_clusterRegistry = new MockStore<>();
_loadBalancerStrategyFactories = new HashMap<>();
_clientFactories = new HashMap<>();
_state = new SimpleLoadBalancerState(_executorService, _uriRegistry, _clusterRegistry, _serviceRegistry, _clientFactories, _loadBalancerStrategyFactories);
_loadBalancer = new SimpleLoadBalancer(_state, 10, TimeUnit.SECONDS, _executorService);
FutureCallback<None> callback = new FutureCallback<>();
_loadBalancer.start(callback);
try {
callback.get();
} catch (Exception e) {
throw new RuntimeException("Balancer start failed", e);
}
// verification state
_expectedServiceProperties = new ConcurrentHashMap<>();
_expectedClusterProperties = new ConcurrentHashMap<>();
_expectedUriProperties = new ConcurrentHashMap<>();
_totalMessages = 0;
// TODO parameterize this
for (int i = 0; i < 10; ++i) {
_possibleServices.add("service-" + i);
_possibleClusters.add("cluster-" + i);
_possiblePaths.add("/some/path/" + i);
_possibleSchemes.add("scheme" + i % 3);
_possibleStrategies.add("strategy-" + i);
_clientFactories.put("scheme" + i % 2, new DoNothingClientFactory());
_loadBalancerStrategyFactories.put("strategy-" + i, _loadBalancerStrategyFactoryToTest);
}
for (int i = 0; i < 1000; ++i) {
_possibleUris.add(URI.create(random(_possibleSchemes) + "://host" + i % 100 + ":" + (1000 + _random.nextInt(1000)) + random(_possiblePaths)));
}
// add bad stuff
// add a bad scheme to prioritized schemes
_possibleSchemes.add("BAD_PRIORITIZED_SCHEME");
// add a bad scheme to possible uris
_possibleUris.add(URI.create("BADSCHEME://host1001:" + (1000 + _random.nextInt(1000)) + random(_possiblePaths)));
// register jmx goodies
new JmxManager().registerLoadBalancer("SimpleLoadBalancer", _loadBalancer).registerLoadBalancerState("SimpleLoadBalancerState", _state);
}
use of com.linkedin.d2.jmx.JmxManager in project rest.li by linkedin.
the class LoadBalancerClientCli method getLoadBalancer.
public static SimpleLoadBalancer getLoadBalancer(ZKConnection zkclient, String zkserver, String d2path, String service) throws IOException, IllegalStateException, URISyntaxException, PropertyStoreException, ExecutionException, TimeoutException, InterruptedException {
// zk stores
String clstoreString = zkserver + ZKFSUtil.clusterPath(d2path);
String scstoreString = zkserver + ZKFSUtil.servicePath(d2path);
String uristoreString = zkserver + ZKFSUtil.uriPath(d2path);
ZooKeeperPermanentStore<ClusterProperties> zkClusterRegistry = (ZooKeeperPermanentStore<ClusterProperties>) getStore(zkclient, clstoreString, new ClusterPropertiesJsonSerializer());
ZooKeeperPermanentStore<ServiceProperties> zkServiceRegistry = (ZooKeeperPermanentStore<ServiceProperties>) getStore(zkclient, scstoreString, new ServicePropertiesJsonSerializer());
ZooKeeperEphemeralStore<UriProperties> zkUriRegistry = (ZooKeeperEphemeralStore<UriProperties>) getEphemeralStore(zkclient, uristoreString, new UriPropertiesJsonSerializer(), new UriPropertiesMerger());
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("D2 PropertyEventExecutor"));
PropertyEventBus<ServiceProperties> serviceBus = new PropertyEventBusImpl<>(executor, zkServiceRegistry);
PropertyEventBus<UriProperties> uriBus = new PropertyEventBusImpl<>(executor, zkUriRegistry);
PropertyEventBus<ClusterProperties> clusterBus = new PropertyEventBusImpl<>(executor, zkClusterRegistry);
Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<>();
loadBalancerStrategyFactories.put("random", new RandomLoadBalancerStrategyFactory());
loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
loadBalancerStrategyFactories.put("degraderV2", new DegraderLoadBalancerStrategyFactoryV3());
loadBalancerStrategyFactories.put("degraderV3", new DegraderLoadBalancerStrategyFactoryV3());
loadBalancerStrategyFactories.put("degraderV2_1", new DegraderLoadBalancerStrategyFactoryV3());
Map<String, TransportClientFactory> clientFactories = new HashMap<>();
clientFactories.put("http", new HttpClientFactory.Builder().build());
// create the state
SimpleLoadBalancerState state = new SimpleLoadBalancerState(executor, uriBus, clusterBus, serviceBus, clientFactories, loadBalancerStrategyFactories, null, null, false);
SimpleLoadBalancer balancer = new SimpleLoadBalancer(state, 5, TimeUnit.SECONDS, executor);
FutureCallback<None> callback = new FutureCallback<>();
balancer.start(callback);
callback.get(5, TimeUnit.SECONDS);
new JmxManager().registerLoadBalancer("balancer", balancer).registerLoadBalancerState("state", state).registerScheduledThreadPoolExecutor("executorService", executor).registerZooKeeperPermanentStore("zkClusterRegistry", zkClusterRegistry).registerZooKeeperPermanentStore("zkServiceRegistry", zkServiceRegistry).registerZooKeeperEphemeralStore("zkUriRegistry", zkUriRegistry);
return balancer;
}
Aggregations