Search in sources :

Example 21 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestZK method testLeaderFailover.

@Test
public void testLeaderFailover() throws Exception {
    ZooKeeper zk = getClient(0);
    ZooKeeper zk2 = getClient(1);
    ZooKeeper zk3 = getClient(2);
    zk.create("/election", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    final Semaphore sem2 = new Semaphore(0);
    LeaderNoticeHandler r2 = new LeaderNoticeHandler() {

        @Override
        public void becomeLeader() {
            sem2.release();
        }

        @Override
        public void noticedTopologyChange(boolean added, boolean removed) {
        }
    };
    final Semaphore sem3 = new Semaphore(0);
    LeaderNoticeHandler r3 = new LeaderNoticeHandler() {

        @Override
        public void becomeLeader() {
            sem3.release();
        }

        @Override
        public void noticedTopologyChange(boolean added, boolean removed) {
        }
    };
    LeaderElector elector1 = new LeaderElector(zk, "/election", "node", new byte[0], null);
    LeaderElector elector2 = new LeaderElector(zk2, "/election", "node", new byte[0], r2);
    LeaderElector elector3 = new LeaderElector(zk3, "/election", "node", new byte[0], r3);
    elector1.start(true);
    elector2.start(true);
    elector3.start(true);
    assertTrue(elector1.isLeader());
    assertFalse(elector2.isLeader());
    assertFalse(elector3.isLeader());
    // 2 should become the leader
    elector1.shutdown();
    zk.close();
    assertTrue(sem2.tryAcquire(5, TimeUnit.SECONDS));
    assertTrue(elector2.isLeader());
    assertEquals(0, sem3.availablePermits());
    // 3 should become the leader now
    elector2.shutdown();
    zk2.close();
    assertTrue(sem3.tryAcquire(5, TimeUnit.SECONDS));
    assertTrue(elector3.isLeader());
    elector3.shutdown();
    zk3.close();
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 22 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestZK method testSessionExpireAndRecovery.

@Test
public void testSessionExpireAndRecovery() throws Exception {
    ZooKeeper zk = getClient(0);
    zk.create("/foo", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    failSite(1);
    recoverSite(1);
    for (int ii = 2; ii < 8; ii++) {
        failSite(ii);
    }
    failSite(0);
    zk = getClient(1);
    assertNull(zk.exists("/foo", false));
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Test(org.junit.Test)

Example 23 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestZK method testFailure.

@Test
public void testFailure() throws Exception {
    ZooKeeper zk = getClient(1);
    zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    assertEquals(zk.getData("/foo", false, null).length, 0);
    System.out.println("Created node");
    failSite(0);
    Thread.sleep(1000);
    assertEquals(zk.getData("/foo", false, null).length, 0);
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Test(org.junit.Test)

Example 24 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestZK method testRecovery.

@Test
public void testRecovery() throws Exception {
    ZooKeeper zk = getClient(0);
    zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.close();
    failSite(0);
    recoverSite(0);
    for (int ii = 1; ii < NUM_AGREEMENT_SITES; ii++) {
        zk = getClient(ii);
        assertEquals(zk.getData("/foo", false, null).length, 0);
    }
    zk = getClient(0);
    assertEquals(zk.getData("/foo", false, null).length, 0);
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Test(org.junit.Test)

Example 25 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestZK method testCAS.

@Test
public void testCAS() throws Exception {
    ZooKeeper zk = getClient(0);
    ZooKeeper zk2 = getClient(1);
    ZooKeeper zk3 = getClient(2);
    ZooKeeper zk4 = getClient(3);
    zk2.create("/foo", new byte[4], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    final int count = 100;
    Thread t1 = getThread(zk, count);
    Thread t2 = getThread(zk2, count);
    Thread t3 = getThread(zk3, count);
    Thread t4 = getThread(zk4, count);
    t1.start();
    t2.start();
    t3.start();
    t4.start();
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    ByteBuffer buf = ByteBuffer.wrap(zk.getData("/foo", false, null));
    assertEquals(count * 4, buf.getInt());
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

ZooKeeper (org.apache.zookeeper_voltpatches.ZooKeeper)62 Test (org.junit.Test)38 KeeperException (org.apache.zookeeper_voltpatches.KeeperException)14 JSONObject (org.json_voltpatches.JSONObject)14 Semaphore (java.util.concurrent.Semaphore)11 Stat (org.apache.zookeeper_voltpatches.data.Stat)10 ExecutionException (java.util.concurrent.ExecutionException)8 HostMessenger (org.voltcore.messaging.HostMessenger)8 IOException (java.io.IOException)7 JSONException (org.json_voltpatches.JSONException)7 WatchedEvent (org.apache.zookeeper_voltpatches.WatchedEvent)5 Watcher (org.apache.zookeeper_voltpatches.Watcher)5 VoltTable (org.voltdb.VoltTable)5 SettingsException (org.voltdb.settings.SettingsException)5 List (java.util.List)4 Map (java.util.Map)4 HostAndPort (com.google_voltpatches.common.net.HostAndPort)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 SocketException (java.net.SocketException)3 ImmutableMap (com.google_voltpatches.common.collect.ImmutableMap)2