Search in sources :

Example 16 with ClusterProperties

use of com.linkedin.d2.balancer.properties.ClusterProperties in project rest.li by linkedin.

the class R2D2Server method run.

public void run() throws Exception {
    // start everything
    for (Map.Entry<String, List<LoadBalancerEchoServer>> servers : _clusters.entrySet()) {
        List<String> schemes = new ArrayList<String>();
        schemes.add("http");
        putCluster(new ClusterProperties(servers.getKey(), schemes));
        for (final LoadBalancerEchoServer server : servers.getValue()) {
            for (int i = 1; i <= 3; ++i) {
                putService(new ServiceProperties("service-" + i + "-" + servers.getKey(), servers.getKey(), File.separator + "service-" + i + "-" + servers.getKey(), Arrays.asList("degrader")));
            }
            server.startServer();
            server.markUp();
        }
    }
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ArrayList(java.util.ArrayList) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) LoadBalancerEchoServer(com.linkedin.d2.balancer.util.LoadBalancerEchoServer)

Example 17 with ClusterProperties

use of com.linkedin.d2.balancer.properties.ClusterProperties in project rest.li by linkedin.

the class R2D2Server method putCluster.

private void putCluster(ClusterProperties clusterProperties) throws Exception {
    System.err.println("put: " + clusterProperties);
    ZKConnection client = new ZKConnection(_zookeeperHost + ":" + _zookeeperPort, 30000);
    PropertyStore<ClusterProperties> store = new ZooKeeperPermanentStore<ClusterProperties>(client, new ClusterPropertiesJsonSerializer(), _basePath + "/clusters");
    store.put(clusterProperties.getClusterName(), clusterProperties);
    client.getZooKeeper().close();
}
Also used : ZKConnection(com.linkedin.d2.discovery.stores.zk.ZKConnection) ClusterPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer) ZooKeeperPermanentStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperPermanentStore) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties)

Example 18 with ClusterProperties

use of com.linkedin.d2.balancer.properties.ClusterProperties in project rest.li by linkedin.

the class SimpleLoadBalancerStateTest method testVersion.

@Test(groups = { "small", "back-end" })
public void testVersion() throws URISyntaxException {
    reset();
    int expectedVersion = 0;
    URI uri = URI.create("http://cluster-1/test");
    List<String> schemes = new ArrayList<String>();
    Map<Integer, PartitionData> partitionData = new HashMap<Integer, PartitionData>(1);
    partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
    Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<URI, Map<Integer, PartitionData>>();
    uriData.put(uri, partitionData);
    schemes.add("http");
    // set up state
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    // one for uri properties onInit, and one for cluster properties onInit
    expectedVersion += 2;
    assertEquals(_state.getVersion(), expectedVersion);
    _state.listenToService("service-1", new NullStateListenerCallback());
    // one for service onInit
    ++expectedVersion;
    assertEquals(_state.getVersion(), expectedVersion);
    _clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1", schemes));
    // one for cluster onAdd
    ++expectedVersion;
    assertEquals(_state.getVersion(), expectedVersion);
    _uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
    // one for uri onAdd
    ++expectedVersion;
    assertEquals(_state.getVersion(), expectedVersion);
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random")));
    // one for service onAdd
    ++expectedVersion;
    assertEquals(_state.getVersion(), expectedVersion);
    // this shouldn't change the version
    _state.getClient("cluster-1", uri);
    assertEquals(_state.getVersion(), expectedVersion);
    // this shouldn't change the version
    _state.getStrategy("service-1", "http");
    assertEquals(_state.getVersion(), expectedVersion);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(java.net.URI) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 19 with ClusterProperties

use of com.linkedin.d2.balancer.properties.ClusterProperties in project rest.li by linkedin.

the class TestD2Config method verifyPartitionProperties.

public static void verifyPartitionProperties(String cluster, Map<String, Object> propertiesMap) throws IOException, URISyntaxException, PropertyStoreException {
    final ClusterProperties clusterprops = getClusterProperties(_zkclient, cluster);
    if (propertiesMap.get("partitionProperties") != null) {
        @SuppressWarnings("unchecked") Map<String, Object> properties = (Map<String, Object>) propertiesMap.get("partitionProperties");
        PartitionProperties.PartitionType partitionType = PartitionProperties.PartitionType.valueOf((String) properties.get("partitionType"));
        switch(partitionType) {
            case RANGE:
                {
                    long keyRangeStart = ((Number) properties.get("keyRangeStart")).longValue();
                    long partitionSize = ((Number) properties.get("partitionSize")).longValue();
                    int partitionCount = ((Number) properties.get("partitionCount")).intValue();
                    String regex = (String) properties.get("partitionKeyRegex");
                    RangeBasedPartitionProperties rbp = (RangeBasedPartitionProperties) clusterprops.getPartitionProperties();
                    assertEquals(keyRangeStart, rbp.getKeyRangeStart());
                    assertEquals(partitionSize, rbp.getPartitionSize());
                    assertEquals(partitionCount, rbp.getPartitionCount());
                    assertEquals(regex, rbp.getPartitionKeyRegex());
                }
                break;
            case HASH:
                {
                    int partitionCount = ((Number) properties.get("partitionCount")).intValue();
                    String regex = (String) properties.get("partitionKeyRegex");
                    String algorithm = (String) properties.get("hashAlgorithm");
                    HashBasedPartitionProperties.HashAlgorithm hashAlgorithm = HashBasedPartitionProperties.HashAlgorithm.valueOf(algorithm.toUpperCase());
                    HashBasedPartitionProperties hbp = (HashBasedPartitionProperties) clusterprops.getPartitionProperties();
                    assertEquals(partitionCount, hbp.getPartitionCount());
                    assertEquals(regex, hbp.getPartitionKeyRegex());
                    assertEquals(hashAlgorithm, hbp.getHashAlgorithm());
                }
                break;
            default:
                break;
        }
    }
}
Also used : PartitionProperties(com.linkedin.d2.balancer.properties.PartitionProperties) HashBasedPartitionProperties(com.linkedin.d2.balancer.properties.HashBasedPartitionProperties) RangeBasedPartitionProperties(com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties) HashBasedPartitionProperties(com.linkedin.d2.balancer.properties.HashBasedPartitionProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) RangeBasedPartitionProperties(com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties) Map(java.util.Map) HashMap(java.util.HashMap)

Example 20 with ClusterProperties

use of com.linkedin.d2.balancer.properties.ClusterProperties in project rest.li by linkedin.

the class TestD2Config method verifyClusterProperties.

@SuppressWarnings("unchecked")
public static void verifyClusterProperties(String cluster, Map<String, Object> propertiesMap) throws PropertyStoreException, IOException, URISyntaxException {
    ClusterProperties clusterProperties = getClusterProperties(_zkclient, cluster);
    if (propertiesMap.get(PropertyKeys.COLO_VARIANTS) != null) {
        String coloVariantsString = clusterProperties.getProperties().get(PropertyKeys.COLO_VARIANTS);
        List<String> coloVariants = Arrays.asList(coloVariantsString.split(D2Config.LIST_SEPARATOR));
        List<String> expectedColoVariants = (List<String>) propertiesMap.get(PropertyKeys.COLO_VARIANTS);
        Assert.assertTrue(coloVariants.containsAll(expectedColoVariants));
        Assert.assertTrue(expectedColoVariants.containsAll(coloVariants));
    }
    if (propertiesMap.get(PropertyKeys.MASTER_COLO) != null) {
        String masterColo = clusterProperties.getProperties().get(PropertyKeys.MASTER_COLO);
        String expectedMasterColo = (String) propertiesMap.get(PropertyKeys.MASTER_COLO);
        Assert.assertEquals(masterColo, expectedMasterColo);
    }
    if (propertiesMap.get(PropertyKeys.CLUSTER_VARIANTS) != null) {
        String clusterVariantsString = clusterProperties.getProperties().get(PropertyKeys.CLUSTER_VARIANTS);
        List<String> clusterVariants = Arrays.asList(clusterVariantsString.split(D2Config.LIST_SEPARATOR));
        List<String> expectedClusterVariants = (List<String>) propertiesMap.get(PropertyKeys.CLUSTER_VARIANTS);
        Assert.assertTrue(clusterVariants.containsAll(expectedClusterVariants));
        Assert.assertTrue(expectedClusterVariants.containsAll(clusterVariants));
    }
}
Also used : ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)43 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)33 HashMap (java.util.HashMap)30 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)28 ArrayList (java.util.ArrayList)24 URI (java.net.URI)22 Test (org.testng.annotations.Test)22 Map (java.util.Map)17 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)14 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)13 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)13 ClusterPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer)12 HashSet (java.util.HashSet)11 None (com.linkedin.common.util.None)10 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)10 ServicePropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer)10 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)10 DegraderLoadBalancerTest (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)10 FutureCallback (com.linkedin.common.callback.FutureCallback)9 NullStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback)9