Search in sources :

Example 1 with ZKFSLoadBalancer

use of com.linkedin.d2.balancer.zkfs.ZKFSLoadBalancer in project rest.li by linkedin.

the class LoadBalancerClientCli method getZKFSLoadBalancer.

public ZKFSLoadBalancer getZKFSLoadBalancer(String zkConnectString, String d2path, String d2ServicePath) throws Exception {
    _tmpDir = createTempDirectory(_tmpdirName);
    ZKFSComponentFactory componentFactory = new ZKFSComponentFactory();
    if (d2ServicePath == null || d2ServicePath.isEmpty()) {
        d2ServicePath = "services";
    }
    Map<String, TransportClientFactory> clientFactories = new HashMap<String, TransportClientFactory>();
    clientFactories.put("http", new HttpClientFactory());
    Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>>();
    loadBalancerStrategyFactories.put("random", new RandomLoadBalancerStrategyFactory());
    loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV2());
    loadBalancerStrategyFactories.put("degraderV2", new DegraderLoadBalancerStrategyFactoryV2());
    loadBalancerStrategyFactories.put("degraderV3", new DegraderLoadBalancerStrategyFactoryV3());
    loadBalancerStrategyFactories.put("degraderV2_1", new DegraderLoadBalancerStrategyFactoryV2_1());
    ZKFSTogglingLoadBalancerFactoryImpl factory = new ZKFSTogglingLoadBalancerFactoryImpl(componentFactory, TIMEOUT, TimeUnit.MILLISECONDS, d2path, _tmpDir.getAbsolutePath(), clientFactories, loadBalancerStrategyFactories, d2ServicePath, null, null, false);
    return new ZKFSLoadBalancer(zkConnectString, SESSION_TIMEOUT, (int) TIMEOUT, factory, null, d2path);
}
Also used : DegraderLoadBalancerStrategyFactoryV2(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV2) ZKFSTogglingLoadBalancerFactoryImpl(com.linkedin.d2.balancer.zkfs.ZKFSTogglingLoadBalancerFactoryImpl) HashMap(java.util.HashMap) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) DegraderLoadBalancerStrategyFactoryV2_1(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV2_1) ZKFSComponentFactory(com.linkedin.d2.balancer.zkfs.ZKFSComponentFactory) ZKFSLoadBalancer(com.linkedin.d2.balancer.zkfs.ZKFSLoadBalancer) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory)

Example 2 with ZKFSLoadBalancer

use of com.linkedin.d2.balancer.zkfs.ZKFSLoadBalancer in project rest.li by linkedin.

the class ZKFSLoadBalancer method start.

@Override
public void start(final Callback<None> callback) {
    LOG.info("Starting ZKFSLoadBalancer");
    LOG.info("ZK connect string: {}", _connectString);
    LOG.info("ZK session timeout: {}ms", _sessionTimeout);
    LOG.info("ZK initial connect timeout: {}ms", _initialZKTimeout);
    if (_connectString == null || _connectString.isEmpty()) {
        callback.onError(new IllegalArgumentException("ZooKeeper connection string is null or empty"));
        return;
    }
    if (_zkFlagFile == null) {
        LOG.info("ZK flag file not specified");
    } else {
        LOG.info("ZK flag file: {}", _zkFlagFile.getAbsolutePath());
        LOG.info("ZK currently suppressed by flag file: {}", suppressZK());
    }
    _zkConnection = new ZKConnection(_connectString, _sessionTimeout, _shutdownAsynchronously, _isSymlinkAware);
    final TogglingLoadBalancer balancer = _loadBalancerFactory.createLoadBalancer(_zkConnection, _executor);
    // all other cases, we service requests from the old LoadBalancer until the new one is started
    if (_currentLoadBalancer == null) {
        _currentLoadBalancer = balancer;
    }
    Callback<None> wrapped = new Callback<None>() {

        @Override
        public void onSuccess(None none) {
            _currentLoadBalancer = balancer;
            callback.onSuccess(none);
        }

        @Override
        public void onError(Throwable e) {
            callback.onError(e);
        }
    };
    if (!_startupCallback.compareAndSet(null, wrapped)) {
        throw new IllegalStateException("Startup already in progress");
    }
    _executor.execute(new PropertyEventThread.PropertyEvent("startup") {

        @Override
        public void innerRun() {
            _zkConnection.addStateListener(new ZKListener(balancer));
            try {
                _zkConnection.start();
            } catch (Exception e) {
                LOG.error("Failed to start ZooKeeper (bad configuration?), enabling backup stores", e);
                Callback<None> startupCallback = _startupCallback.getAndSet(null);
                // TODO this should never be null
                balancer.enableBackup(startupCallback);
                return;
            }
            LOG.info("Started ZooKeeper");
            _executor.schedule(new Runnable() {

                @Override
                public void run() {
                    Callback<None> startupCallback = _startupCallback.getAndSet(null);
                    if (startupCallback != null) {
                        // Noone has enabled the stores yet either way
                        LOG.error("No response from ZooKeeper within {}ms, enabling backup stores", _initialZKTimeout);
                        balancer.enableBackup(startupCallback);
                    }
                }
            }, _initialZKTimeout, TimeUnit.MILLISECONDS);
        }
    });
}
Also used : ZKConnection(com.linkedin.d2.discovery.stores.zk.ZKConnection) TogglingLoadBalancer(com.linkedin.d2.balancer.util.TogglingLoadBalancer) KeeperException(org.apache.zookeeper.KeeperException) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) Callback(com.linkedin.common.callback.Callback) None(com.linkedin.common.util.None) PropertyEventThread(com.linkedin.d2.discovery.event.PropertyEventThread)

Example 3 with ZKFSLoadBalancer

use of com.linkedin.d2.balancer.zkfs.ZKFSLoadBalancer in project rest.li by linkedin.

the class ZKFSTest method testKeyMapper.

@Test
public void testKeyMapper() throws Exception {
    final String TEST_SERVICE_NAME = "test-service";
    final String TEST_CLUSTER_NAME = "test-cluster";
    final URI TEST_SERVER_URI1 = URI.create("http://test-host-1/");
    final URI TEST_SERVER_URI2 = URI.create("http://test-host-2/");
    final int NUM_ITERATIONS = 5;
    startServer();
    try {
        ZKFSLoadBalancer balancer = getBalancer();
        FutureCallback<None> callback = new FutureCallback<None>();
        balancer.start(callback);
        callback.get(30, TimeUnit.SECONDS);
        ZKConnection conn = balancer.zkConnection();
        ZooKeeperPermanentStore<ServiceProperties> serviceStore = new ZooKeeperPermanentStore<ServiceProperties>(conn, new ServicePropertiesJsonSerializer(), ZKFSUtil.servicePath(BASE_PATH));
        ServiceProperties props = new ServiceProperties(TEST_SERVICE_NAME, TEST_CLUSTER_NAME, "/test", Arrays.asList("degrader"), Collections.<String, Object>emptyMap(), null, null, Arrays.asList("http"), null);
        serviceStore.put(TEST_SERVICE_NAME, props);
        ClusterProperties clusterProperties = new ClusterProperties(TEST_CLUSTER_NAME);
        ZooKeeperPermanentStore<ClusterProperties> clusterStore = new ZooKeeperPermanentStore<ClusterProperties>(conn, new ClusterPropertiesJsonSerializer(), ZKFSUtil.clusterPath(BASE_PATH));
        clusterStore.put(TEST_CLUSTER_NAME, clusterProperties);
        ZooKeeperEphemeralStore<UriProperties> uriStore = new ZooKeeperEphemeralStore<UriProperties>(conn, new UriPropertiesJsonSerializer(), new UriPropertiesMerger(), ZKFSUtil.uriPath(BASE_PATH), false, true);
        Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<URI, Map<Integer, PartitionData>>();
        Map<Integer, PartitionData> partitionData = new HashMap<Integer, PartitionData>(1);
        partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1.0d));
        uriData.put(TEST_SERVER_URI1, partitionData);
        uriData.put(TEST_SERVER_URI2, partitionData);
        UriProperties uriProps = new UriProperties(TEST_CLUSTER_NAME, uriData);
        callback = new FutureCallback<None>();
        uriStore.start(callback);
        callback.get(30, TimeUnit.SECONDS);
        uriStore.put(TEST_CLUSTER_NAME, uriProps);
        Set<Integer> keys = new HashSet<Integer>();
        for (int ii = 0; ii < 100; ++ii) {
            keys.add(ii);
        }
        for (int ii = 0; ii < NUM_ITERATIONS; ++ii) {
            KeyMapper mapper = balancer.getKeyMapper();
            MapKeyResult<URI, Integer> batches = mapper.mapKeysV2(URI.create("d2://" + TEST_SERVICE_NAME), keys);
            Assert.assertEquals(batches.getMapResult().size(), 2);
            for (Map.Entry<URI, Collection<Integer>> oneBatch : batches.getMapResult().entrySet()) {
                Assert.assertTrue(oneBatch.getKey().toString().startsWith("http://test-host-"));
                Assert.assertTrue(keys.containsAll(oneBatch.getValue()));
            }
        }
    } finally {
        stopServer();
    }
}
Also used : ServicePropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer) HashMap(java.util.HashMap) KeyMapper(com.linkedin.d2.balancer.KeyMapper) ZooKeeperPermanentStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperPermanentStore) UriPropertiesMerger(com.linkedin.d2.balancer.properties.UriPropertiesMerger) URI(java.net.URI) UriPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer) ClusterPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) FutureCallback(com.linkedin.common.callback.FutureCallback) HashSet(java.util.HashSet) ZKConnection(com.linkedin.d2.discovery.stores.zk.ZKConnection) ZooKeeperEphemeralStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Collection(java.util.Collection) None(com.linkedin.common.util.None) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 4 with ZKFSLoadBalancer

use of com.linkedin.d2.balancer.zkfs.ZKFSLoadBalancer in project rest.li by linkedin.

the class ZKFSTest method testClientFactoryProvider.

@Test
public void testClientFactoryProvider() throws Exception {
    startServer();
    try {
        ZKFSLoadBalancer balancer = getBalancer();
        FutureCallback<None> callback = new FutureCallback<None>();
        balancer.start(callback);
        callback.get(30, TimeUnit.SECONDS);
        Facilities facilities = balancer.getFacilities();
        TransportClientFactory factory = facilities.getClientFactory("http");
        Assert.assertNotNull(factory);
        Assert.assertTrue(factory instanceof HttpClientFactory);
    } finally {
        stopServer();
    }
}
Also used : None(com.linkedin.common.util.None) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) FutureCallback(com.linkedin.common.callback.FutureCallback) Facilities(com.linkedin.d2.balancer.Facilities) Test(org.testng.annotations.Test)

Example 5 with ZKFSLoadBalancer

use of com.linkedin.d2.balancer.zkfs.ZKFSLoadBalancer in project rest.li by linkedin.

the class ZKFSTest method testServiceDirectory.

@Test
public void testServiceDirectory() throws Exception {
    final String TEST_SERVICE_NAME = "testingService";
    startServer();
    try {
        ZKFSLoadBalancer balancer = getBalancer();
        FutureCallback<None> callback = new FutureCallback<None>();
        balancer.start(callback);
        callback.get(30, TimeUnit.SECONDS);
        Directory dir = balancer.getDirectory();
        ZKConnection conn = new ZKConnection("localhost:" + PORT, 30000);
        conn.start();
        ZooKeeperPermanentStore<ServiceProperties> store = new ZooKeeperPermanentStore<ServiceProperties>(conn, new ServicePropertiesJsonSerializer(), ZKFSUtil.servicePath(BASE_PATH));
        callback = new FutureCallback<None>();
        store.start(callback);
        callback.get(30, TimeUnit.SECONDS);
        ServiceProperties props = new ServiceProperties(TEST_SERVICE_NAME, "someCluster", "/somePath", Arrays.asList("someStrategy"));
        store.put(TEST_SERVICE_NAME, props);
        FutureCallback<List<String>> serviceCallback = new FutureCallback<List<String>>();
        dir.getServiceNames(serviceCallback);
        Assert.assertEquals(serviceCallback.get(30, TimeUnit.SECONDS), Collections.singletonList(TEST_SERVICE_NAME));
    } finally {
        stopServer();
    }
}
Also used : ZKConnection(com.linkedin.d2.discovery.stores.zk.ZKConnection) ServicePropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer) ZooKeeperPermanentStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperPermanentStore) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) List(java.util.List) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Directory(com.linkedin.d2.balancer.Directory) Test(org.testng.annotations.Test)

Aggregations

None (com.linkedin.common.util.None)6 FutureCallback (com.linkedin.common.callback.FutureCallback)5 ZKConnection (com.linkedin.d2.discovery.stores.zk.ZKConnection)5 Test (org.testng.annotations.Test)5 ZooKeeperPermanentStore (com.linkedin.d2.discovery.stores.zk.ZooKeeperPermanentStore)4 HashMap (java.util.HashMap)4 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)3 ClusterPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer)3 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)3 ServicePropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer)3 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)3 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)3 Directory (com.linkedin.d2.balancer.Directory)2 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)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 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)2 LoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory)2 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)2