Search in sources :

Example 11 with IZkDataListener

use of org.I0Itec.zkclient.IZkDataListener in project helix by apache.

the class TestHelper method printZkListeners.

public static void printZkListeners(ZkClient client) throws Exception {
    Map<String, Set<IZkDataListener>> datalisteners = ZkTestHelper.getZkDataListener(client);
    Map<String, Set<IZkChildListener>> childListeners = ZkTestHelper.getZkChildListener(client);
    System.out.println("dataListeners {");
    for (String path : datalisteners.keySet()) {
        System.out.println("\t" + path + ": ");
        Set<IZkDataListener> set = datalisteners.get(path);
        for (IZkDataListener listener : set) {
            CallbackHandler handler = (CallbackHandler) listener;
            System.out.println("\t\t" + handler.getListener());
        }
    }
    System.out.println("}");
    System.out.println("childListeners {");
    for (String path : childListeners.keySet()) {
        System.out.println("\t" + path + ": ");
        Set<IZkChildListener> set = childListeners.get(path);
        for (IZkChildListener listener : set) {
            CallbackHandler handler = (CallbackHandler) listener;
            System.out.println("\t\t" + handler.getListener());
        }
    }
    System.out.println("}");
}
Also used : CallbackHandler(org.apache.helix.manager.zk.CallbackHandler) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) IZkChildListener(org.I0Itec.zkclient.IZkChildListener) IZkDataListener(org.I0Itec.zkclient.IZkDataListener)

Example 12 with IZkDataListener

use of org.I0Itec.zkclient.IZkDataListener in project helix by apache.

the class TestZkBasis method testWatchRemove.

/**
 * after calling zkclient#unsubscribeXXXListener()
 * an already registered watch will not be removed from ZooKeeper#watchManager#XXXWatches
 * immediately.
 * the watch will get removed on the following conditions:
 * 1) there is a set/delete on the listening path via the zkclient
 * 2) session expiry on the zkclient (i.e. the watch will not be renewed after session expiry)
 * @throws Exception
 */
@Test
public void testWatchRemove() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String testName = className + "_" + methodName;
    final ZkClient client = new ZkClient(ZK_ADDR, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
    // make sure "/testName/test" doesn't exist
    final String path = "/" + testName + "/test";
    client.createPersistent(path, true);
    ZkListener listener = new ZkListener();
    client.subscribeDataChanges(path, listener);
    client.subscribeChildChanges(path, listener);
    // listener should be in both ZkClient#_dataListener and ZkClient#_childListener set
    Map<String, Set<IZkDataListener>> dataListenerMap = ZkTestHelper.getZkDataListener(client);
    Assert.assertEquals(dataListenerMap.size(), 1, "ZkClient#_dataListener should have 1 listener");
    Set<IZkDataListener> dataListenerSet = dataListenerMap.get(path);
    Assert.assertNotNull(dataListenerSet, "ZkClient#_dataListener should have 1 listener on path: " + path);
    Assert.assertEquals(dataListenerSet.size(), 1, "ZkClient#_dataListener should have 1 listener on path: " + path);
    Map<String, Set<IZkChildListener>> childListenerMap = ZkTestHelper.getZkChildListener(client);
    Assert.assertEquals(childListenerMap.size(), 1, "ZkClient#_childListener should have 1 listener");
    Set<IZkChildListener> childListenerSet = childListenerMap.get(path);
    Assert.assertNotNull(childListenerSet, "ZkClient#_childListener should have 1 listener on path: " + path);
    Assert.assertEquals(childListenerSet.size(), 1, "ZkClient#_childListener should have 1 listener on path: " + path);
    // watch should be in ZooKeeper#watchManager#XXXWatches
    Map<String, List<String>> watchMap = ZkTestHelper.getZkWatch(client);
    // System.out.println("watchMap1: " + watchMap);
    List<String> dataWatch = watchMap.get("dataWatches");
    Assert.assertNotNull(dataWatch, "ZooKeeper#watchManager#dataWatches should have 1 data watch on path: " + path);
    Assert.assertEquals(dataWatch.size(), 1, "ZooKeeper#watchManager#dataWatches should have 1 data watch on path: " + path);
    Assert.assertEquals(dataWatch.get(0), path, "ZooKeeper#watchManager#dataWatches should have 1 data watch on path: " + path);
    List<String> childWatch = watchMap.get("childWatches");
    Assert.assertNotNull(childWatch, "ZooKeeper#watchManager#childWatches should have 1 child watch on path: " + path);
    Assert.assertEquals(childWatch.size(), 1, "ZooKeeper#watchManager#childWatches should have 1 child watch on path: " + path);
    Assert.assertEquals(childWatch.get(0), path, "ZooKeeper#watchManager#childWatches should have 1 child watch on path: " + path);
    client.unsubscribeDataChanges(path, listener);
    client.unsubscribeChildChanges(path, listener);
    // System.out.println("watchMap2: " + watchMap);
    ZkTestHelper.expireSession(client);
    // after session expiry, those watches should be removed
    watchMap = ZkTestHelper.getZkWatch(client);
    // System.out.println("watchMap3: " + watchMap);
    dataWatch = watchMap.get("dataWatches");
    Assert.assertTrue(dataWatch.isEmpty(), "ZooKeeper#watchManager#dataWatches should be empty");
    childWatch = watchMap.get("childWatches");
    Assert.assertTrue(childWatch.isEmpty(), "ZooKeeper#watchManager#childWatches should be empty");
    client.close();
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) Set(java.util.Set) IZkDataListener(org.I0Itec.zkclient.IZkDataListener) IZkChildListener(org.I0Itec.zkclient.IZkChildListener) List(java.util.List) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) Test(org.testng.annotations.Test)

Example 13 with IZkDataListener

use of org.I0Itec.zkclient.IZkDataListener in project helix by apache.

the class TestZkCallbackHandlerLeak method printZkListeners.

void printZkListeners(ZkClient client) throws Exception {
    Map<String, Set<IZkDataListener>> datalisteners = ZkTestHelper.getZkDataListener(client);
    Map<String, Set<IZkChildListener>> childListeners = ZkTestHelper.getZkChildListener(client);
    System.out.println("dataListeners {");
    for (String path : datalisteners.keySet()) {
        System.out.println("\t" + path + ": ");
        Set<IZkDataListener> set = datalisteners.get(path);
        for (IZkDataListener listener : set) {
            CallbackHandler handler = (CallbackHandler) listener;
            System.out.println("\t\t" + handler.getListener());
        }
    }
    System.out.println("}");
    System.out.println("childListeners {");
    for (String path : childListeners.keySet()) {
        System.out.println("\t" + path + ": ");
        Set<IZkChildListener> set = childListeners.get(path);
        for (IZkChildListener listener : set) {
            CallbackHandler handler = (CallbackHandler) listener;
            System.out.println("\t\t" + handler.getListener());
        }
    }
    System.out.println("}");
}
Also used : CallbackHandler(org.apache.helix.manager.zk.CallbackHandler) Set(java.util.Set) IZkChildListener(org.I0Itec.zkclient.IZkChildListener) IZkDataListener(org.I0Itec.zkclient.IZkDataListener)

Example 14 with IZkDataListener

use of org.I0Itec.zkclient.IZkDataListener in project helix by apache.

the class ZkClient method isPrefetchEnabled.

private boolean isPrefetchEnabled(IZkDataListener dataListener) {
    PreFetch preFetch = dataListener.getClass().getAnnotation(PreFetch.class);
    if (preFetch != null) {
        return preFetch.enabled();
    }
    Method callbackMethod = IZkDataListener.class.getMethods()[0];
    try {
        Method method = dataListener.getClass().getMethod(callbackMethod.getName(), callbackMethod.getParameterTypes());
        PreFetch preFetchInMethod = method.getAnnotation(PreFetch.class);
        if (preFetchInMethod != null) {
            return preFetchInMethod.enabled();
        }
    } catch (NoSuchMethodException e) {
        LOG.warn("No method " + callbackMethod.getName() + " defined in listener " + dataListener.getClass().getCanonicalName());
    }
    return true;
}
Also used : PreFetch(org.apache.helix.api.listeners.PreFetch) IZkDataListener(org.I0Itec.zkclient.IZkDataListener) Method(java.lang.reflect.Method)

Example 15 with IZkDataListener

use of org.I0Itec.zkclient.IZkDataListener in project samza by apache.

the class TestZkLeaderElector method testLeaderFailure.

/**
 * Tests that Leader Failure automatically promotes the next successor to become the leader
 */
@Test
public void testLeaderFailure() {
    /**
     * electionLatch and count together verify that:
     * 1. the registered listeners are actually invoked by the ZkClient on the correct path
     * 2. for a single participant failure, at-most 1 other participant is notified
     */
    final CountDownLatch electionLatch = new CountDownLatch(1);
    final AtomicInteger count = new AtomicInteger(0);
    BooleanResult isLeader1 = new BooleanResult();
    BooleanResult isLeader2 = new BooleanResult();
    BooleanResult isLeader3 = new BooleanResult();
    // Processor-1
    ZkUtils zkUtils1 = getZkUtilsWithNewClient();
    zkUtils1.registerProcessorAndGetId(new ProcessorData("processor1", "1"));
    ZkLeaderElector leaderElector1 = new ZkLeaderElector("processor1", zkUtils1, new IZkDataListener() {

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
        }

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
            count.incrementAndGet();
        }
    });
    leaderElector1.setLeaderElectorListener(() -> isLeader1.res = true);
    // Processor-2
    ZkUtils zkUtils2 = getZkUtilsWithNewClient();
    final String path2 = zkUtils2.registerProcessorAndGetId(new ProcessorData("processor2", "2"));
    ZkLeaderElector leaderElector2 = new ZkLeaderElector("processor2", zkUtils2, new IZkDataListener() {

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
        }

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
            String registeredIdStr = ZkKeyBuilder.parseIdFromPath(path2);
            Assert.assertNotNull(registeredIdStr);
            String predecessorIdStr = ZkKeyBuilder.parseIdFromPath(dataPath);
            Assert.assertNotNull(predecessorIdStr);
            try {
                int selfId = Integer.parseInt(registeredIdStr);
                int predecessorId = Integer.parseInt(predecessorIdStr);
                Assert.assertEquals(1, selfId - predecessorId);
            } catch (Exception e) {
                LOG.error(e.getLocalizedMessage());
            }
            count.incrementAndGet();
            electionLatch.countDown();
        }
    });
    leaderElector2.setLeaderElectorListener(() -> isLeader2.res = true);
    // Processor-3
    ZkUtils zkUtils3 = getZkUtilsWithNewClient();
    zkUtils3.registerProcessorAndGetId(new ProcessorData("processor3", "3"));
    ZkLeaderElector leaderElector3 = new ZkLeaderElector("processor3", zkUtils3, new IZkDataListener() {

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
        }

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
            count.incrementAndGet();
        }
    });
    leaderElector3.setLeaderElectorListener(() -> isLeader3.res = true);
    // Join Leader Election
    leaderElector1.tryBecomeLeader();
    leaderElector2.tryBecomeLeader();
    leaderElector3.tryBecomeLeader();
    Assert.assertTrue(TestZkUtils.testWithDelayBackOff(() -> isLeader1.res, 2, 100));
    Assert.assertFalse(TestZkUtils.testWithDelayBackOff(() -> isLeader2.res, 2, 100));
    Assert.assertFalse(TestZkUtils.testWithDelayBackOff(() -> isLeader3.res, 2, 100));
    Assert.assertTrue(leaderElector1.amILeader());
    Assert.assertFalse(leaderElector2.amILeader());
    Assert.assertFalse(leaderElector3.amILeader());
    List<String> currentActiveProcessors = zkUtils1.getSortedActiveProcessorsZnodes();
    Assert.assertEquals(3, currentActiveProcessors.size());
    // Leader Failure
    zkUtils1.close();
    currentActiveProcessors.remove(0);
    try {
        Assert.assertTrue(electionLatch.await(5, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        Assert.fail("Interrupted while waiting for leaderElection listener callback to complete!");
    }
    Assert.assertEquals(1, count.get());
    Assert.assertEquals(currentActiveProcessors, zkUtils2.getSortedActiveProcessorsZnodes());
    // Clean up
    zkUtils2.close();
    zkUtils3.close();
}
Also used : IZkDataListener(org.I0Itec.zkclient.IZkDataListener) CountDownLatch(java.util.concurrent.CountDownLatch) SamzaException(org.apache.samza.SamzaException) ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

IZkDataListener (org.I0Itec.zkclient.IZkDataListener)17 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)7 CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)3 Set (java.util.Set)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 Test (org.testng.annotations.Test)3 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)2 CallbackHandler (org.apache.helix.manager.zk.CallbackHandler)2 SamzaException (org.apache.samza.SamzaException)2 JobConfig (com.jeesuite.scheduler.model.JobConfig)1 MonitorCommond (com.jeesuite.scheduler.monitor.MonitorCommond)1 ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)1 URL (com.weibo.api.motan.rpc.URL)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1