Search in sources :

Example 1 with DefeatOnDisconnectLeader

use of com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader in project commons by twitter.

the class SingletonServiceTest method testLeaderDisconnect.

@Test
public void testLeaderDisconnect() throws Exception {
    Capture<LeaderControl> controlCapture = createCapture();
    CountDownLatch leading = new CountDownLatch(1);
    listener.onLeading(capture(controlCapture));
    expectLastCall().andAnswer(countDownAnswer(leading));
    CountDownLatch defeated = new CountDownLatch(1);
    listener.onDefeated(null);
    expectLastCall().andAnswer(countDownAnswer(defeated));
    control.replay();
    ZooKeeperClient zkClient = createZkClient();
    serverSet = new ServerSetImpl(zkClient, "/fake/path");
    candidate = new CandidateImpl(new Group(zkClient, ZooKeeperUtils.OPEN_ACL_UNSAFE, "/fake/path"));
    DefeatOnDisconnectLeader leader = new DefeatOnDisconnectLeader(zkClient, listener);
    service = new SingletonService(serverSet, candidate);
    service.lead(InetSocketAddress.createUnresolved("foo", PORT_A), ImmutableMap.of("http-admin", InetSocketAddress.createUnresolved("foo", PORT_B)), leader);
    leading.await();
    shutdownNetwork();
    defeated.await();
}
Also used : DefeatOnDisconnectLeader(com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader) CountDownLatch(java.util.concurrent.CountDownLatch) LeaderControl(com.twitter.common.zookeeper.SingletonService.LeaderControl) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 2 with DefeatOnDisconnectLeader

use of com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader in project commons by twitter.

the class SingletonServiceTest method testNonLeaderDisconnect.

@Test
public void testNonLeaderDisconnect() throws Exception {
    CountDownLatch elected = new CountDownLatch(1);
    listener.onLeading(EasyMock.<LeaderControl>anyObject());
    expectLastCall().andAnswer(countDownAnswer(elected));
    listener.onDefeated(null);
    expectLastCall().anyTimes();
    control.replay();
    ZooKeeperClient zkClient = createZkClient();
    String path = "/fake/path";
    // Create a fake leading candidate node to ensure that the leader in this test is never
    // elected.
    ZooKeeperUtils.ensurePath(zkClient, ZooKeeperUtils.OPEN_ACL_UNSAFE, path);
    String leaderNode = zkClient.get().create(path + "/" + SingletonService.LEADER_ELECT_NODE_PREFIX, "fake_leader".getBytes(), ZooKeeperUtils.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
    serverSet = new ServerSetImpl(zkClient, path);
    candidate = SingletonService.createSingletonCandidate(zkClient, path, ZooKeeperUtils.OPEN_ACL_UNSAFE);
    DefeatOnDisconnectLeader leader = new DefeatOnDisconnectLeader(zkClient, listener);
    service = new SingletonService(serverSet, candidate);
    service.lead(InetSocketAddress.createUnresolved("foo", PORT_A), ImmutableMap.of("http-admin", InetSocketAddress.createUnresolved("foo", PORT_B)), leader);
    final CountDownLatch disconnected = new CountDownLatch(1);
    zkClient.register(new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if ((event.getType() == EventType.None) && (event.getState() == KeeperState.Disconnected)) {
                disconnected.countDown();
            }
        }
    });
    shutdownNetwork();
    disconnected.await();
    restartNetwork();
    zkClient.get().delete(leaderNode, ZooKeeperUtils.ANY_VERSION);
    // Upon deletion of the fake leader node, the candidate should become leader.
    elected.await();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) DefeatOnDisconnectLeader(com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Aggregations

DefeatOnDisconnectLeader (com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader)2 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 LeaderControl (com.twitter.common.zookeeper.SingletonService.LeaderControl)1 WatchedEvent (org.apache.zookeeper.WatchedEvent)1 Watcher (org.apache.zookeeper.Watcher)1