Search in sources :

Example 91 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project open-kilda by telstra.

the class ZkWatchDogTest method testProcessValidNodeBuildVersion.

@Test
public void testProcessValidNodeBuildVersion() throws KeeperException, InterruptedException {
    ZkWatchDog watchDog = Mockito.mock(ZkWatchDog.class);
    watchDog.signalPath = "/test/path";
    watchDog.buildVersionPath = "/test/bw_path";
    doCallRealMethod().when(watchDog).process(any());
    WatchedEvent event = new WatchedEvent(EventType.NodeDataChanged, KeeperState.SyncConnected, watchDog.buildVersionPath);
    watchDog.process(event);
    verify(watchDog, Mockito.times(1)).subscribeBuildVersion();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Test(org.junit.Test)

Example 92 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project open-kilda by telstra.

the class ZkWatchDogTest method testProcessValidNodeSignal.

@Test
public void testProcessValidNodeSignal() throws KeeperException, InterruptedException {
    ZkWatchDog watchDog = Mockito.mock(ZkWatchDog.class);
    watchDog.signalPath = "/test/path";
    watchDog.buildVersionPath = "/test/bw_path";
    doCallRealMethod().when(watchDog).process(any());
    WatchedEvent event = new WatchedEvent(EventType.NodeDataChanged, KeeperState.SyncConnected, watchDog.signalPath);
    watchDog.process(event);
    verify(watchDog, Mockito.times(1)).subscribeSignal();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Test(org.junit.Test)

Example 93 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project open-kilda by telstra.

the class ZkWatchDogTest method testProcessWrongNodeBuildVersion.

@Test
public void testProcessWrongNodeBuildVersion() throws KeeperException, InterruptedException {
    ZkWatchDog watchDog = Mockito.mock(ZkWatchDog.class);
    watchDog.signalPath = "/test/path";
    watchDog.buildVersionPath = "/test/bw_path";
    doCallRealMethod().when(watchDog).process(any());
    WatchedEvent event = new WatchedEvent(EventType.NodeDataChanged, KeeperState.SyncConnected, watchDog.buildVersionPath + "data");
    watchDog.process(event);
    verify(watchDog, Mockito.times(0)).subscribeBuildVersion();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Test(org.junit.Test)

Example 94 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.

the class QuorumTest method testNoLogBeforeLeaderEstablishment.

/**
     * Tests if closeSession can be logged before a leader gets established, which
     * could lead to a locked-out follower (see ZOOKEEPER-790). 
     * 
     * The test works as follows. It has a client connecting to a follower f and
     * sending batches of 1,000 updates. The goal is that f has a zxid higher than
     * all other servers in the initial leader election. This way we can crash and
     * recover the follower so that the follower believes it is the leader once it
     * recovers (LE optimization: once a server receives a message from all other 
     * servers, it picks a leader.
     * 
     * It also makes the session timeout very short so that we force the false 
     * leader to close the session and write it to the log in the buggy code (before 
     * ZOOKEEPER-790). Once f drops leadership and finds the current leader, its epoch
     * is higher, and it rejects the leader. Now, if we prevent the leader from closing
     * the session by only starting up (see Leader.lead()) once it obtains a quorum of 
     * supporters, then f will find the current leader and support it because it won't
     * have a highe epoch.
     * 
     */
@Test
public void testNoLogBeforeLeaderEstablishment() throws Exception {
    final Semaphore sem = new Semaphore(0);
    qu = new QuorumUtil(2, 10);
    qu.startQuorum();
    int index = 1;
    while (qu.getPeer(index).peer.leader == null) index++;
    Leader leader = qu.getPeer(index).peer.leader;
    Assert.assertNotNull(leader);
    /*
         * Reusing the index variable to select a follower to connect to
         */
    index = (index == 1) ? 2 : 1;
    ZooKeeper zk = new DisconnectableZooKeeper("127.0.0.1:" + qu.getPeer(index).peer.getClientPort(), ClientBase.CONNECTION_TIMEOUT, new Watcher() {

        public void process(WatchedEvent event) {
        }
    });
    zk.create("/blah", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    for (int i = 0; i < 50000; i++) {
        zk.setData("/blah", new byte[0], -1, new AsyncCallback.StatCallback() {

            public void processResult(int rc, String path, Object ctx, Stat stat) {
                counter++;
                if (rc != 0) {
                    errors++;
                }
                if (counter == 20000) {
                    sem.release();
                }
            }
        }, null);
        if (i == 5000) {
            qu.shutdown(index);
            LOG.info("Shutting down s1");
        }
        if (i == 12000) {
            qu.start(index);
            LOG.info("Setting up server: " + index);
        }
        if ((i % 1000) == 0) {
            Thread.sleep(500);
        }
    }
    // Wait until all updates return
    sem.tryAcquire(15, TimeUnit.SECONDS);
    // Verify that server is following and has the same epoch as the leader
    Assert.assertTrue("Not following", qu.getPeer(index).peer.follower != null);
    long epochF = (qu.getPeer(index).peer.getActiveServer().getZxid() >> 32L);
    long epochL = (leader.getEpoch() >> 32L);
    Assert.assertTrue("Zxid: " + qu.getPeer(index).peer.getActiveServer().getZxid() + "Current epoch: " + epochF, epochF == epochL);
    zk.close();
}
Also used : Leader(org.apache.zookeeper.server.quorum.Leader) AsyncCallback(org.apache.zookeeper.AsyncCallback) Watcher(org.apache.zookeeper.Watcher) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) Semaphore(java.util.concurrent.Semaphore) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) Test(org.junit.Test)

Example 95 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project weave by continuuity.

the class ZKClientTest method testRetry.

@Test
public void testRetry() throws ExecutionException, InterruptedException, TimeoutException {
    File dataDir = Files.createTempDir();
    InMemoryZKServer zkServer = InMemoryZKServer.builder().setDataDir(dataDir).setTickTime(1000).build();
    zkServer.startAndWait();
    int port = zkServer.getLocalAddress().getPort();
    final CountDownLatch disconnectLatch = new CountDownLatch(1);
    ZKClientService client = ZKClientServices.delegate(ZKClients.retryOnFailure(ZKClientService.Builder.of(zkServer.getConnectionStr()).setConnectionWatcher(new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == Event.KeeperState.Disconnected) {
                disconnectLatch.countDown();
            }
        }
    }).build(), RetryStrategies.fixDelay(0, TimeUnit.SECONDS)));
    client.startAndWait();
    zkServer.stopAndWait();
    Assert.assertTrue(disconnectLatch.await(1, TimeUnit.SECONDS));
    final CountDownLatch createLatch = new CountDownLatch(1);
    Futures.addCallback(client.create("/testretry/test", null, CreateMode.PERSISTENT), new FutureCallback<String>() {

        @Override
        public void onSuccess(String result) {
            createLatch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
            t.printStackTrace(System.out);
        }
    });
    TimeUnit.SECONDS.sleep(2);
    zkServer = InMemoryZKServer.builder().setDataDir(dataDir).setAutoCleanDataDir(true).setPort(port).setTickTime(1000).build();
    zkServer.startAndWait();
    try {
        Assert.assertTrue(createLatch.await(5, TimeUnit.SECONDS));
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Aggregations

WatchedEvent (org.apache.zookeeper.WatchedEvent)194 Watcher (org.apache.zookeeper.Watcher)123 ZooKeeper (org.apache.zookeeper.ZooKeeper)67 CountDownLatch (java.util.concurrent.CountDownLatch)64 KeeperException (org.apache.zookeeper.KeeperException)56 Test (org.junit.Test)38 Stat (org.apache.zookeeper.data.Stat)35 IOException (java.io.IOException)31 Test (org.testng.annotations.Test)27 Test (org.junit.jupiter.api.Test)18 CuratorFramework (org.apache.curator.framework.CuratorFramework)14 AsyncCallback (org.apache.zookeeper.AsyncCallback)14 List (java.util.List)10 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)10 Set (java.util.Set)7 TimeoutException (java.util.concurrent.TimeoutException)7 HashSet (java.util.HashSet)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RetryOneTime (org.apache.curator.retry.RetryOneTime)6