Search in sources :

Example 21 with PartitionData

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

the class ZookeeperConnectionManagerTest method testMarkUpAndMarkDown.

@Test
public void testMarkUpAndMarkDown() throws IOException, ExecutionException, InterruptedException, PropertyStoreException {
    final String uri = "http://cluster-2/test";
    final String cluster = "cluster-2";
    final double weight = 0.5d;
    ZooKeeperAnnouncer announcer = new ZooKeeperAnnouncer(new ZooKeeperServer());
    announcer.setCluster(cluster);
    announcer.setUri(uri);
    Map<Integer, PartitionData> partitionWeight = new HashMap<Integer, PartitionData>();
    partitionWeight.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(weight));
    announcer.setPartitionData(partitionWeight);
    ZooKeeperConnectionManager manager = createManager(announcer);
    FutureCallback<None> managerStartCallback = new FutureCallback<None>();
    manager.start(managerStartCallback);
    managerStartCallback.get();
    ZooKeeperEphemeralStore<UriProperties> store = createAndStartUriStore();
    UriProperties properties = store.get(cluster);
    assertNotNull(properties);
    assertEquals(properties.getPartitionDataMap(URI.create(uri)).get(DefaultPartitionAccessor.DEFAULT_PARTITION_ID).getWeight(), weight);
    assertEquals(properties.Uris().size(), 1);
    FutureCallback<None> markDownCallback = new FutureCallback<None>();
    announcer.markDown(markDownCallback);
    markDownCallback.get();
    properties = store.get(cluster);
    assertNotNull(properties);
    assertEquals(properties.Uris().size(), 0);
}
Also used : HashMap(java.util.HashMap) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 22 with PartitionData

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

the class SimpleLoadBalancerStateTest method testUpdatePartitionDataMap.

@Test(groups = { "small", "back-end" })
public void testUpdatePartitionDataMap() {
    reset();
    URI uri = URI.create("http://cluster-1/test");
    List<String> schemes = new ArrayList<String>();
    Map<Integer, PartitionData> partitionDataMap = new HashMap<Integer, PartitionData>(1);
    Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<URI, Map<Integer, PartitionData>>();
    uriData.put(uri, partitionDataMap);
    schemes.add("http");
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    _state.listenToService("service-1", new NullStateListenerCallback());
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"), Collections.<String, Object>emptyMap(), null, null, schemes, null));
    // first we announce uri with empty partition data map
    _uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
    TrackerClient client = _state.getClient("service-1", uri);
    assertNotNull(client);
    assertEquals(client.getUri(), uri);
    // tracker client should see empty partition data map
    assertTrue(client.getParttitionDataMap().isEmpty());
    // then we update this uri to have a non-empty partition data map
    partitionDataMap.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
    _uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
    TrackerClient updatedClient = _state.getClient("service-1", uri);
    assertNotNull(updatedClient);
    // should have got a different tracker client
    assertNotSame(client, updatedClient);
    assertEquals(updatedClient.getUri(), uri);
    // this updated client should have updated partition data map
    assertFalse(updatedClient.getParttitionDataMap().isEmpty());
    assertEquals(updatedClient.getParttitionDataMap(), partitionDataMap);
}
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) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 23 with PartitionData

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

the class SimpleLoadBalancerStateTest method testGetClientAfterServiceMetadataChange.

@Test(groups = { "small", "back-end" })
public void testGetClientAfterServiceMetadataChange() {
    reset();
    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");
    assertNull(_state.getClient("service-1", uri));
    // set up state
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    assertNull(_state.getClient("service-1", uri));
    _state.listenToService("service-1", new NullStateListenerCallback());
    assertNull(_state.getClient("service-1", uri));
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"), Collections.<String, Object>emptyMap(), null, null, schemes, null));
    assertNull(_state.getClient("service-1", uri));
    _uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
    TrackerClient client = _state.getClient("service-1", uri);
    assertNotNull(client);
    assertEquals(client.getUri(), uri);
    //now we publish an event that tells service-1 changes cluster. Now it's hosted in cluster-2
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-2", "/test", Arrays.asList("random"), Collections.<String, Object>emptyMap(), null, null, schemes, null));
    //this time, since we haven't listened to cluster-2 and there's no uri in cluster-2, we get no client.
    assertNull(_state.getClient("service-1", uri));
    //we shouldn't be affected by any update to cluster-1 uris because now service-1 listen to cluster-2
    _uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
    assertNull(_state.getClient("service-1", uri));
    //now let's create URI for cluster 2 and make the state listen to cluster_2
    _state.listenToCluster("cluster-2", new NullStateListenerCallback());
    URI uri2 = URI.create("http://cluster-2/test");
    Map<Integer, PartitionData> partitionData2 = new HashMap<Integer, PartitionData>(1);
    partitionData2.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
    Map<URI, Map<Integer, PartitionData>> uriData2 = new HashMap<URI, Map<Integer, PartitionData>>();
    uriData2.put(uri2, partitionData2);
    //if we start publishing new event to cluster-2 then we should get trackerClient
    _uriRegistry.put("cluster-2", new UriProperties("cluster-2", uriData2));
    client = _state.getClient("service-1", uri2);
    assertNotNull(client);
    assertEquals(client.getUri(), uri2);
    //just to make sure that any event from cluster-1 doesn't affect us anymore
    _uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
    client = _state.getClient("service-1", uri2);
    assertNotNull(client);
    assertEquals(client.getUri(), uri2);
    //if we pass uri from cluster-1, we also get no tracker client
    client = _state.getClient("service-1", uri);
    assertNull(client);
}
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) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 24 with PartitionData

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

the class SimpleLoadBalancerStateTest method testGetSSLClient.

@Test(groups = { "small", "back-end" })
public void testGetSSLClient() throws URISyntaxException {
    reset(true);
    URI uri = URI.create("https://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("https");
    assertNull(_state.getClient("service-1", uri));
    // set up state
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    assertNull(_state.getClient("service-1", uri));
    _state.listenToService("service-1", new NullStateListenerCallback());
    assertNull(_state.getClient("service-1", uri));
    Map<String, Object> transportClientProperties = new HashMap<String, Object>();
    transportClientProperties.put(HttpClientFactory.HTTP_SSL_CONTEXT, _sslContext);
    transportClientProperties.put(HttpClientFactory.HTTP_SSL_PARAMS, _sslParameters);
    transportClientProperties = Collections.unmodifiableMap(transportClientProperties);
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"), Collections.<String, Object>emptyMap(), transportClientProperties, null, schemes, null));
    assertNull(_state.getClient("service-1", uri));
    _uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
    TrackerClient client = _state.getClient("service-1", uri);
    assertNotNull(client);
    assertEquals(client.getUri(), uri);
}
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) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 25 with PartitionData

use of com.linkedin.d2.balancer.properties.PartitionData 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)

Aggregations

PartitionData (com.linkedin.d2.balancer.properties.PartitionData)55 HashMap (java.util.HashMap)53 Test (org.testng.annotations.Test)36 URI (java.net.URI)35 Map (java.util.Map)31 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)28 ArrayList (java.util.ArrayList)25 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)21 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)20 None (com.linkedin.common.util.None)15 FutureCallback (com.linkedin.common.callback.FutureCallback)14 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)13 DegraderLoadBalancerTest (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)13 NullStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback)12 RequestContext (com.linkedin.r2.message.RequestContext)11 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)9 LoadBalancerState (com.linkedin.d2.balancer.LoadBalancerState)8 PartitionAccessor (com.linkedin.d2.balancer.util.partitions.PartitionAccessor)7 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)7 HashSet (java.util.HashSet)7