Search in sources :

Example 1 with ZooKeeperConnectionManager

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

the class ZookeeperConnectionManagerTest method testMarkUpDuringSessionExpirationManyCallbacks.

@Test(invocationCount = 10, timeOut = 10000, retryAnalyzer = ThreeRetries.class)
public void testMarkUpDuringSessionExpirationManyCallbacks() throws Exception {
    ZooKeeperAnnouncer announcer = getZooKeeperAnnouncer(_cluster, _uri, WEIGHT);
    ZKPersistentConnection zkPersistentConnection = getZkPersistentConnection();
    ZooKeeperConnectionManager manager = createManager(true, zkPersistentConnection, announcer);
    // set up many concurrent callbacks
    FutureCallback<None> allMarkupsSucceed = new FutureCallback<>();
    int count = 1000;
    Callback<None> markUpAllServersCallback = new MultiCallback(allMarkupsSucceed, 2 * count);
    ExecutorService executorService = Executors.newScheduledThreadPool(100);
    for (int i = 0; i < count; i++) {
        executorService.execute(() -> {
            manager.markDownAllServers(new IgnoreCancelledCallback(markUpAllServersCallback));
            manager.markUpAllServers(new IgnoreCancelledCallback(markUpAllServersCallback));
        });
    }
    // expiring the connection
    long oldSessionId = zkPersistentConnection.getZooKeeper().getSessionId();
    ZKTestUtil.expireSession("localhost:" + PORT, zkPersistentConnection.getZooKeeper(), 10, TimeUnit.SECONDS);
    ZKTestUtil.waitForNewSessionEstablished(oldSessionId, zkPersistentConnection, 10, TimeUnit.SECONDS);
    try {
        allMarkupsSucceed.get(1, TimeUnit.MILLISECONDS);
        Assert.fail("All the callbacks were resolved before expiring the connection, which means it won't test that callbacks are invoked even after session expiration");
    } catch (Throwable e) {
    // expected
    }
    allMarkupsSucceed.get();
    // making sure that a new connection has been established. There should be no need to wait, because at least one markup should have been run on
    // the new connection, which means that by this part of code it should already have been established
    ZKTestUtil.waitForNewSessionEstablished(oldSessionId, zkPersistentConnection, 0, TimeUnit.SECONDS);
    // data validation
    dataValidation(_uri, _cluster, WEIGHT);
    shutdownManager(manager);
    executorService.shutdown();
}
Also used : ZKPersistentConnection(com.linkedin.d2.discovery.stores.zk.ZKPersistentConnection) ExecutorService(java.util.concurrent.ExecutorService) MultiCallback(com.linkedin.common.callback.MultiCallback) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 2 with ZooKeeperConnectionManager

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

the class ZookeeperConnectionManagerTest method testMarkDownDuringDisconnection.

@Test
public void testMarkDownDuringDisconnection() 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);
    // 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();
    markDownCallback.get();
    properties = store.get(_cluster);
    assertNotNull(properties);
    assertEquals(properties.Uris().size(), 0);
    shutdownManager(manager);
}
Also used : 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 3 with ZooKeeperConnectionManager

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

the class ZookeeperConnectionManagerTest method testMarkUpAndMarkDown.

@Test
public void testMarkUpAndMarkDown() 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);
    FutureCallback<None> markDownCallback = new FutureCallback<>();
    announcer.markDown(markDownCallback);
    markDownCallback.get();
    properties = store.get(_cluster);
    assertNotNull(properties);
    assertEquals(properties.Uris().size(), 0);
    shutdownManager(manager);
}
Also used : 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 4 with ZooKeeperConnectionManager

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

the class ZookeeperConnectionManagerTest method testMarkUpDuringDisconnection.

@Test
public void testMarkUpDuringDisconnection() throws Exception {
    ZooKeeperAnnouncer announcer = getZooKeeperAnnouncer(_cluster, _uri, WEIGHT);
    ZooKeeperConnectionManager manager = createManager(false, announcer);
    _zkServer.shutdown(false);
    FutureCallback<None> managerStartCallback = new FutureCallback<>();
    manager.start(managerStartCallback);
    _zkServer.restart();
    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);
    shutdownManager(manager);
}
Also used : 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 5 with ZooKeeperConnectionManager

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

the class SharedZkConnectionProviderTest method testManyHostsAnnouncementSharingConnections.

/**
 * Test announcing many hosts using one connection concurrently
 */
@Test(groups = "needZk")
public void testManyHostsAnnouncementSharingConnections() throws Exception {
    List<URI> hostNames = prepareHostNames(100, "testManyHostsAnnouncementSharingConnections");
    List<ZooKeeperConnectionManager> connectionManagers = prepareConnectionManagers(hostNames);
    startConnectionManagers(connectionManagers);
    UriProperties newProperties = _verificationStore.get(CLUSTER_NAME);
    assertNotNull(newProperties);
    assertEquals(newProperties.Uris().size(), 100);
    shutdownConnectionManagers(connectionManagers);
}
Also used : ZooKeeperConnectionManager(com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) URI(java.net.URI) 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