Search in sources :

Example 16 with ZooKeeperConnectionManager

use of com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager in project rest.li by linkedin.

the class ZookeeperConnectionManagerTest method testMarkUpWithSinglePartition.

@Test
public void testMarkUpWithSinglePartition() throws Exception {
    double newWeight = 10d;
    ZooKeeperAnnouncer announcer = getZooKeeperSinglePartitionAnnouncer(_cluster, _uri, PARTITION1_ID, PARTITION1_WEIGHT);
    announcer.setWeight(newWeight);
    ZooKeeperConnectionManager manager = createManager(true, announcer);
    ZooKeeperEphemeralStore<UriProperties> store = createAndStartUriStore();
    UriProperties properties = store.get(_cluster);
    assertNotNull(properties);
    assertNull(properties.getPartitionDataMap(URI.create(_uri)).get(DefaultPartitionAccessor.DEFAULT_PARTITION_ID));
    assertNotEquals(properties.getPartitionDataMap(URI.create(_uri)).get(PARTITION1_ID).getWeight(), PARTITION1_WEIGHT);
    assertEquals(properties.getPartitionDataMap(URI.create(_uri)).get(PARTITION1_ID).getWeight(), newWeight);
    assertEquals(properties.Uris().size(), 1);
    shutdownManager(manager);
}
Also used : UriProperties(com.linkedin.d2.balancer.properties.UriProperties) Test(org.testng.annotations.Test)

Example 17 with ZooKeeperConnectionManager

use of com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager in project rest.li by linkedin.

the class ZookeeperConnectionManagerTest method testMarkUp.

@Test
public void testMarkUp() throws Exception {
    ZooKeeperAnnouncer announcer = getZooKeeperAnnouncer(_cluster, _uri, WEIGHT);
    ZooKeeperConnectionManager manager = createManager(true, announcer);
    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);
    shutdownManager(manager);
}
Also used : UriProperties(com.linkedin.d2.balancer.properties.UriProperties) Test(org.testng.annotations.Test)

Example 18 with ZooKeeperConnectionManager

use of com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager in project rest.li by linkedin.

the class ZookeeperConnectionManagerTest method testMarkUpDuringSessionExpiration.

@Test(invocationCount = 10, timeOut = 10000)
public void testMarkUpDuringSessionExpiration() throws Exception {
    // set up
    final double newWeight = 1.5d;
    ZooKeeperAnnouncer announcer = getZooKeeperAnnouncer(_cluster, _uri, WEIGHT);
    ZKPersistentConnection zkPersistentConnection = getZkPersistentConnection();
    ZooKeeperConnectionManager manager = createManager(true, zkPersistentConnection, announcer);
    // the new WEIGHT will be picked up only if the connection is re-established
    announcer.setWeight(newWeight);
    // expiring the connection
    long oldSessionId = zkPersistentConnection.getZooKeeper().getSessionId();
    ZKTestUtil.expireSession("localhost:" + PORT, zkPersistentConnection.getZooKeeper(), 10, TimeUnit.SECONDS);
    // making sure that a new connection has been established.
    ZKTestUtil.waitForNewSessionEstablished(oldSessionId, zkPersistentConnection, 10, TimeUnit.SECONDS);
    // validation
    ZooKeeperEphemeralStore<UriProperties> store = createAndStartUriStore();
    AssertionMethods.assertWithTimeout(1000, () -> {
        UriProperties properties = store.get(_cluster);
        assertNotNull(properties);
        if (properties.getPartitionDataMap(URI.create(_uri)) == null) {
            Assert.fail("Supposed to have the uri present in ZK");
        }
        assertEquals(properties.getPartitionDataMap(URI.create(_uri)).get(DefaultPartitionAccessor.DEFAULT_PARTITION_ID).getWeight(), newWeight);
        assertEquals(properties.Uris().size(), 1);
    });
    shutdownManager(manager);
}
Also used : ZKPersistentConnection(com.linkedin.d2.discovery.stores.zk.ZKPersistentConnection) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) Test(org.testng.annotations.Test)

Example 19 with ZooKeeperConnectionManager

use of com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager in project rest.li by linkedin.

the class ZookeeperConnectionManagerTest method testMarkDownAndUpDuringDisconnection.

@Test
public void testMarkDownAndUpDuringDisconnection() throws Exception {
    ZooKeeperAnnouncer announcer = getZooKeeperAnnouncer(_cluster, _uri, WEIGHT);
    ZooKeeperConnectionManager manager = createManager(true, announcer);
    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);
    _zkServer.shutdown(false);
    FutureCallback<None> markDownCallback = new FutureCallback<>();
    announcer.markDown(markDownCallback);
    FutureCallback<None> markUpCallback = new FutureCallback<>();
    announcer.markUp(markUpCallback);
    // ugly, but we need to wait for a while just so that Disconnect event is propagated
    // to the caller before we restart zk sever.
    Thread.sleep(1000);
    _zkServer.restart();
    markUpCallback.get(10, TimeUnit.SECONDS);
    try {
        markDownCallback.get();
        Assert.fail("mark down should have thrown CancellationException.");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof CancellationException);
    }
    properties = store.get(_cluster);
    assertNotNull(properties);
    assertEquals(properties.getPartitionDataMap(URI.create(_uri)).get(DefaultPartitionAccessor.DEFAULT_PARTITION_ID).getWeight(), WEIGHT);
    assertEquals(properties.Uris().size(), 1);
    shutdownManager(manager);
}
Also used : CancellationException(java.util.concurrent.CancellationException) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) ExecutionException(java.util.concurrent.ExecutionException) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 20 with ZooKeeperConnectionManager

use of com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager in project rest.li by linkedin.

the class ZookeeperConnectionManagerTest method testDelayMarkUp.

@Test
public void testDelayMarkUp() throws Exception {
    ZooKeeperAnnouncer announcer = new ZooKeeperAnnouncer(new ZooKeeperServer(), false);
    announcer.setCluster(_cluster);
    announcer.setUri(_uri);
    Map<Integer, PartitionData> partitionWeight = new HashMap<>();
    partitionWeight.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(ZookeeperConnectionManagerTest.WEIGHT));
    announcer.setPartitionData(partitionWeight);
    ZooKeeperConnectionManager manager = createManager(true, announcer);
    ZooKeeperEphemeralStore<UriProperties> store = createAndStartUriStore();
    UriProperties properties = store.get(_cluster);
    assertNull(properties);
    FutureCallback<None> markUpCallback = new FutureCallback<>();
    announcer.markUp(markUpCallback);
    markUpCallback.get();
    UriProperties propertiesAfterMarkUp = store.get(_cluster);
    assertNotNull(propertiesAfterMarkUp);
    assertEquals(propertiesAfterMarkUp.getPartitionDataMap(URI.create(_uri)).get(DefaultPartitionAccessor.DEFAULT_PARTITION_ID).getWeight(), ZookeeperConnectionManagerTest.WEIGHT);
    assertEquals(propertiesAfterMarkUp.Uris().size(), 1);
    shutdownManager(manager);
}
Also used : PartitionData(com.linkedin.d2.balancer.properties.PartitionData) HashMap(java.util.HashMap) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)16 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)15 FutureCallback (com.linkedin.common.callback.FutureCallback)12 None (com.linkedin.common.util.None)12 ZooKeeperConnectionManager (com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager)11 URI (java.net.URI)6 HashMap (java.util.HashMap)4 ExecutionException (java.util.concurrent.ExecutionException)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 TimeoutException (java.util.concurrent.TimeoutException)3 MultiCallback (com.linkedin.common.callback.MultiCallback)2 D2Client (com.linkedin.d2.balancer.D2Client)2 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)2 ZooKeeperAnnouncer (com.linkedin.d2.balancer.servers.ZooKeeperAnnouncer)2 ZKPersistentConnection (com.linkedin.d2.discovery.stores.zk.ZKPersistentConnection)2 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)2 List (java.util.List)2 Random (java.util.Random)2