Search in sources :

Example 1 with JmxManager

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());
}
Also used : SimpleLoadBalancerState(com.linkedin.d2.balancer.simple.SimpleLoadBalancerState) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) Test(org.testng.annotations.Test) SharedZkConnectionProviderTest(com.linkedin.d2.discovery.stores.zk.SharedZkConnectionProviderTest)

Example 2 with JmxManager

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();
}
Also used : D2Client(com.linkedin.d2.balancer.D2Client) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test) SharedZkConnectionProviderTest(com.linkedin.d2.discovery.stores.zk.SharedZkConnectionProviderTest)

Example 3 with JmxManager

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;
}
Also used : ServicePropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer) HashMap(java.util.HashMap) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) 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) 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)

Example 4 with JmxManager

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);
}
Also used : SimpleLoadBalancerState(com.linkedin.d2.balancer.simple.SimpleLoadBalancerState) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) DoNothingClientFactory(com.linkedin.d2.balancer.simple.SimpleLoadBalancerTest.DoNothingClientFactory) Random(java.util.Random) JmxManager(com.linkedin.d2.jmx.JmxManager) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback)

Example 5 with JmxManager

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;
}
Also used : ServicePropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer) HashMap(java.util.HashMap) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ZooKeeperPermanentStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperPermanentStore) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) UriPropertiesMerger(com.linkedin.d2.balancer.properties.UriPropertiesMerger) 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) FutureCallback(com.linkedin.common.callback.FutureCallback) SimpleLoadBalancerState(com.linkedin.d2.balancer.simple.SimpleLoadBalancerState) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) ZooKeeperEphemeralStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) PropertyEventBusImpl(com.linkedin.d2.discovery.event.PropertyEventBusImpl) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) None(com.linkedin.common.util.None)

Aggregations

SimpleLoadBalancerState (com.linkedin.d2.balancer.simple.SimpleLoadBalancerState)4 FutureCallback (com.linkedin.common.callback.FutureCallback)3 None (com.linkedin.common.util.None)3 SimpleLoadBalancer (com.linkedin.d2.balancer.simple.SimpleLoadBalancer)3 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)3 JmxManager (com.linkedin.d2.jmx.JmxManager)3 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)2 ClusterPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer)2 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)2 ServicePropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer)2 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)2 UriPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer)2 UriPropertiesMerger (com.linkedin.d2.balancer.properties.UriPropertiesMerger)2 LoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory)2 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)2 PropertyEventBusImpl (com.linkedin.d2.discovery.event.PropertyEventBusImpl)2 SharedZkConnectionProviderTest (com.linkedin.d2.discovery.stores.zk.SharedZkConnectionProviderTest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)2 NamedThreadFactory (com.linkedin.r2.util.NamedThreadFactory)2