Search in sources :

Example 11 with PropertyEventShutdownCallback

use of com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback in project rest.li by linkedin.

the class SimpleLoadBalancerSimulation method verifyState.

/**
   * Compare the simulator's view of reality with the load balancer's. This method should
   * be called after every step is performed and all threads have finished.
   */
public void verifyState() {
    // verify that we consumed all messages before we do anything
    for (int i = 0; i < _queues.length; ++i) {
        if (_queues[i].size() > 0) {
            fail("there were messages left in the queue. all messages should have been consumed during this simulation step.");
        }
    }
    // verify that all clients have been shut down
    for (Map.Entry<String, TransportClientFactory> e : _clientFactories.entrySet()) {
        DoNothingClientFactory factory = (DoNothingClientFactory) e.getValue();
        if (factory.getRunningClientCount() != 0) {
            fail("Not all clients were shut down from factory " + e.getKey());
        }
    }
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        PropertyEventShutdownCallback callback = new PropertyEventShutdownCallback() {

            @Override
            public void done() {
                latch.countDown();
            }
        };
        _state.shutdown(callback);
        if (!latch.await(60, TimeUnit.SECONDS)) {
            fail("unable to shutdown state");
        }
    } catch (InterruptedException e) {
        fail("unable to shutdown state in verifyState.");
    }
    // New load balancer with no timeout; the code below checks for services that don't
    // exist,
    // and a load balancer with non-zero timeout will just timeout waiting for them to be
    // registered, which will never happen because the PropertyEventThread is shut down.
    _loadBalancer = new SimpleLoadBalancer(_state, 0, TimeUnit.SECONDS);
    // verify services are as we expect
    for (String possibleService : _possibleServices) {
        // about it
        if (!_expectedServiceProperties.containsKey(possibleService) || !_state.isListeningToService(possibleService)) {
            LoadBalancerStateItem<ServiceProperties> serviceItem = _state.getServiceProperties(possibleService);
            assertTrue(serviceItem == null || serviceItem.getProperty() == null);
        } else {
            ServiceProperties serviceProperties = _expectedServiceProperties.get(possibleService);
            ClusterProperties clusterProperties = _expectedClusterProperties.get(serviceProperties.getClusterName());
            UriProperties uriProperties = _expectedUriProperties.get(serviceProperties.getClusterName());
            assertEquals(_state.getServiceProperties(possibleService).getProperty(), serviceProperties);
            // verify round robin'ing of the hosts for this service
            for (int i = 0; i < 100; ++i) {
                try {
                    // this call will queue up messages if we're not listening to the service, but
                    // it's ok, because all of the messengers have been stopped.
                    final TransportClient client = _loadBalancer.getClient(new URIRequest("d2://" + possibleService + random(_possiblePaths)), new RequestContext());
                    // if we didn't receive service unavailable, we should
                    // get a client back
                    assertNotNull(client);
                } catch (ServiceUnavailableException e) {
                    if (uriProperties != null && clusterProperties != null) {
                        // only way to get here is if the prioritized
                        // schemes could find no available uris in the
                        // cluster. let's see if we can find a URI that
                        // matches a prioritized scheme in the cluster.
                        Set<String> schemes = new HashSet<String>();
                        for (URI uri : uriProperties.Uris()) {
                            schemes.add(uri.getScheme());
                        }
                        for (String scheme : clusterProperties.getPrioritizedSchemes()) {
                            // the code.
                            if (schemes.contains(scheme) && _clientFactories.containsKey(scheme)) {
                                break;
                            }
                            assertFalse(schemes.contains(scheme) && _clientFactories.containsKey(scheme), "why couldn't a client be found for schemes " + clusterProperties.getPrioritizedSchemes() + " with URIs: " + uriProperties.Uris());
                        }
                    }
                }
            }
        }
    }
    // verify clusters are as we expect
    for (String possibleCluster : _possibleClusters) {
        LoadBalancerStateItem<ClusterProperties> clusterItem = _state.getClusterProperties(possibleCluster);
        if (!_expectedClusterProperties.containsKey(possibleCluster) || !_state.isListeningToCluster(possibleCluster)) {
            assertTrue(clusterItem == null || clusterItem.getProperty() == null, "cluster item for " + possibleCluster + " is not null: " + clusterItem);
        } else {
            assertNotNull(clusterItem, "Item for cluster " + possibleCluster + " should not be null, listening: " + _state.isListeningToCluster(possibleCluster) + ", keys: " + _expectedClusterProperties.keySet());
            assertEquals(clusterItem.getProperty(), _expectedClusterProperties.get(possibleCluster));
        }
    }
    // verify uris are as we expect
    for (String possibleCluster : _possibleClusters) {
        LoadBalancerStateItem<UriProperties> uriItem = _state.getUriProperties(possibleCluster);
        if (!_expectedUriProperties.containsKey(possibleCluster) || !_state.isListeningToCluster(possibleCluster)) {
            assertTrue(uriItem == null || uriItem.getProperty() == null);
        } else {
            assertNotNull(uriItem);
            assertEquals(uriItem.getProperty(), _expectedUriProperties.get(possibleCluster));
        }
    }
}
Also used : PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) Set(java.util.Set) HashSet(java.util.HashSet) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) URIRequest(com.linkedin.d2.balancer.util.URIRequest) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) DoNothingClientFactory(com.linkedin.d2.balancer.simple.SimpleLoadBalancerTest.DoNothingClientFactory) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory)

Example 12 with PropertyEventShutdownCallback

use of com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback in project rest.li by linkedin.

the class LoadBalancerClientCli method shutdown.

public void shutdown() throws Exception {
    if (_zkClusterRegistry != null) {
        try {
            shutdownZKRegistry(_zkClusterRegistry);
        } catch (Exception e) {
            _log.error("Failed to shutdown ZooKeeperPermanentStore<ClusterProperties> zkClusterRegistry.");
        }
    }
    if (_zkServiceRegistry != null) {
        try {
            shutdownZKRegistry(_zkServiceRegistry);
        } catch (Exception e) {
            _log.error("Failed to shutdown ZooKeeperPermanentStore<ServiceProperties> zkServiceRegistry.");
        }
    }
    if (_zkUriRegistry != null) {
        try {
            shutdownZKRegistry(_zkUriRegistry);
        } catch (Exception e) {
            _log.error("Failed to shutdown ZooKeeperEphemeralStore<UriProperties> zkUriRegistry.");
        }
    }
    try {
        if (_client != null) {
            LoadBalancerUtil.syncShutdownClient(_client, _log);
        }
    } catch (Exception e) {
        _log.error("Failed to shutdown dynamic client.");
    }
    if (_zkfsLoadBalancer != null) {
        try {
            final CountDownLatch latch = new CountDownLatch(1);
            _zkfsLoadBalancer.shutdown(new PropertyEventShutdownCallback() {

                @Override
                public void done() {
                    latch.countDown();
                }
            });
            if (!latch.await(5, TimeUnit.SECONDS)) {
                _log.error("unable to shut down store");
            }
        } catch (Exception e) {
            _log.error("Failed to shutdown zkfsLoadBalancer.");
        }
    }
    try {
        deleteTempDir();
    } catch (Exception e) {
        _log.error("Failed to delete directory " + _tmpDir);
    }
    try {
        _zkclient.shutdown();
    } catch (Exception e) {
        _log.error("Failed to shutdown zk client.");
    }
}
Also used : PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) CountDownLatch(java.util.concurrent.CountDownLatch) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) ParseException(org.apache.commons.cli.ParseException) PropertyStoreException(com.linkedin.d2.discovery.stores.PropertyStoreException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with PropertyEventShutdownCallback

use of com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback in project rest.li by linkedin.

the class LoadBalancerClientCli method shutdownPropertyStore.

private void shutdownPropertyStore(PropertyStore<?> store, long timeout, TimeUnit unit) throws Exception {
    final CountDownLatch registryLatch = new CountDownLatch(1);
    store.shutdown(new PropertyEventShutdownCallback() {

        @Override
        public void done() {
            registryLatch.countDown();
        }
    });
    try {
        registryLatch.await(timeout, unit);
    } catch (InterruptedException e) {
        System.err.println("unable to shutdown store: " + store);
    }
}
Also used : PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 14 with PropertyEventShutdownCallback

use of com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback in project rest.li by linkedin.

the class ZooKeeperServer method shutdown.

public void shutdown() {
    info(_log, "shutting down zk server");
    final CountDownLatch latch = new CountDownLatch(1);
    _store.shutdown(new PropertyEventShutdownCallback() {

        @Override
        public void done() {
            latch.countDown();
        }
    });
    try {
        if (!latch.await(5, TimeUnit.SECONDS)) {
            warn(_log, "unable to shut down propertly");
        } else {
            info(_log, "shutting down complete");
        }
    } catch (InterruptedException e) {
        warn(_log, "unable to shut down propertly.. got interrupt exception while waiting");
    }
}
Also used : PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 15 with PropertyEventShutdownCallback

use of com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback in project rest.li by linkedin.

the class ZooKeeperEphemeralStoreTest method testShutdown.

@Test(groups = { "small", "back-end" })
public void testShutdown() throws InterruptedException, IOException, PropertyStoreException, ExecutionException {
    PropertyStore<String> store = getStore();
    final CountDownLatch latch = new CountDownLatch(1);
    store.shutdown(new PropertyEventShutdownCallback() {

        @Override
        public void done() {
            latch.countDown();
        }
    });
    if (!latch.await(5, TimeUnit.SECONDS)) {
        fail("unable to shut down store");
    }
}
Also used : PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Aggregations

PropertyEventShutdownCallback (com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback)16 CountDownLatch (java.util.concurrent.CountDownLatch)14 Test (org.testng.annotations.Test)7 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 None (com.linkedin.common.util.None)4 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)4 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)4 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)4 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)4 URIRequest (com.linkedin.d2.balancer.util.URIRequest)4 RequestContext (com.linkedin.r2.message.RequestContext)4 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)4 URI (java.net.URI)4 FutureCallback (com.linkedin.common.callback.FutureCallback)3 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)3 RewriteClient (com.linkedin.d2.balancer.clients.RewriteClient)3 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)3 LoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory)3