Search in sources :

Example 1 with CallbackHandler

use of org.apache.helix.manager.zk.CallbackHandler in project helix by apache.

the class TestConsecutiveZkSessionExpiry method testDistributedController.

@Test
public void testDistributedController() throws Exception {
    // Logger.getRootLogger().setLevel(Level.INFO);
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    int n = 2;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    // participant port
    TestHelper.setupCluster(// participant port
    clusterName, // participant port
    ZK_ADDR, // participant port
    12918, // participant name prefix
    "localhost", // resource name prefix
    "TestDB", // resources
    1, // partitions per resource
    4, // number of nodes
    n, // replicas
    2, "MasterSlave", // do rebalance
    true);
    ClusterDistributedController[] distributedControllers = new ClusterDistributedController[n];
    CountDownLatch startCountdown = new CountDownLatch(1);
    CountDownLatch endCountdown = new CountDownLatch(1);
    for (int i = 0; i < n; i++) {
        String contrllerName = "localhost_" + (12918 + i);
        distributedControllers[i] = new ClusterDistributedController(ZK_ADDR, clusterName, contrllerName);
        distributedControllers[i].getStateMachineEngine().registerStateModelFactory("MasterSlave", new MockMSModelFactory());
        if (i == 0) {
            distributedControllers[i].addPreConnectCallback(new PreConnectTestCallback(contrllerName, startCountdown, endCountdown));
        }
        distributedControllers[i].connect();
    }
    boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // expire the session of distributedController
    LOG.info("1st Expiring distributedController session...");
    String oldSessionId = distributedControllers[0].getSessionId();
    ZkTestHelper.asyncExpireSession(distributedControllers[0].getZkClient());
    String newSessionId = distributedControllers[0].getSessionId();
    LOG.info("Expried distributedController session. oldSessionId: " + oldSessionId + ", newSessionId: " + newSessionId);
    // expire zk session again during HelixManager#handleNewSession()
    startCountdown.await();
    LOG.info("2nd Expiring distributedController session...");
    oldSessionId = distributedControllers[0].getSessionId();
    ZkTestHelper.asyncExpireSession(distributedControllers[0].getZkClient());
    newSessionId = distributedControllers[0].getSessionId();
    LOG.info("Expried distributedController session. oldSessionId: " + oldSessionId + ", newSessionId: " + newSessionId);
    endCountdown.countDown();
    result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // verify leader changes to localhost_12919
    HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    Assert.assertNotNull(pollForProperty(LiveInstance.class, accessor, keyBuilder.liveInstance("localhost_12918"), true));
    LiveInstance leader = pollForProperty(LiveInstance.class, accessor, keyBuilder.controllerLeader(), true);
    Assert.assertNotNull(leader);
    Assert.assertEquals(leader.getId(), "localhost_12919");
    // check localhost_12918 has 2 handlers: message and data-accessor
    LOG.debug("handlers: " + TestHelper.printHandlers(distributedControllers[0]));
    List<CallbackHandler> handlers = distributedControllers[0].getHandlers();
    Assert.assertEquals(handlers.size(), 2, "Distributed controller should have 2 handler (message) after lose leadership, but was " + handlers.size());
    // clean up
    distributedControllers[0].disconnect();
    distributedControllers[1].disconnect();
    Assert.assertNull(pollForProperty(LiveInstance.class, accessor, keyBuilder.liveInstance("localhost_12918"), false));
    Assert.assertNull(pollForProperty(LiveInstance.class, accessor, keyBuilder.liveInstance("localhost_12919"), false));
    Assert.assertNull(pollForProperty(LiveInstance.class, accessor, keyBuilder.controllerLeader(), false));
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : CallbackHandler(org.apache.helix.manager.zk.CallbackHandler) CountDownLatch(java.util.concurrent.CountDownLatch) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) MockMSModelFactory(org.apache.helix.mock.participant.MockMSModelFactory) LiveInstance(org.apache.helix.model.LiveInstance) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 2 with CallbackHandler

use of org.apache.helix.manager.zk.CallbackHandler in project helix by apache.

the class TestHelper method printHandlers.

// debug code
public static String printHandlers(ZkTestManager manager) {
    StringBuilder sb = new StringBuilder();
    List<CallbackHandler> handlers = manager.getHandlers();
    sb.append(manager.getInstanceName() + " has " + handlers.size() + " cb-handlers. [");
    for (int i = 0; i < handlers.size(); i++) {
        CallbackHandler handler = handlers.get(i);
        String path = handler.getPath();
        sb.append(path.substring(manager.getClusterName().length() + 1) + ": " + handler.getListener());
        if (i < (handlers.size() - 1)) {
            sb.append(", ");
        }
    }
    sb.append("]");
    return sb.toString();
}
Also used : CallbackHandler(org.apache.helix.manager.zk.CallbackHandler)

Example 3 with CallbackHandler

use of org.apache.helix.manager.zk.CallbackHandler 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 4 with CallbackHandler

use of org.apache.helix.manager.zk.CallbackHandler in project helix by apache.

the class TestZkCallbackHandlerLeak method printHandlers.

// debug code
static String printHandlers(ZkTestManager manager) {
    StringBuilder sb = new StringBuilder();
    List<CallbackHandler> handlers = manager.getHandlers();
    sb.append(manager.getInstanceName() + " has " + handlers.size() + " cb-handlers. [");
    for (int i = 0; i < handlers.size(); i++) {
        CallbackHandler handler = handlers.get(i);
        String path = handler.getPath();
        sb.append(path.substring(manager.getClusterName().length() + 1) + ": " + handler.getListener());
        if (i < (handlers.size() - 1)) {
            sb.append(", ");
        }
    }
    sb.append("]");
    return sb.toString();
}
Also used : CallbackHandler(org.apache.helix.manager.zk.CallbackHandler)

Example 5 with CallbackHandler

use of org.apache.helix.manager.zk.CallbackHandler 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)

Aggregations

CallbackHandler (org.apache.helix.manager.zk.CallbackHandler)6 Set (java.util.Set)2 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)2 IZkDataListener (org.I0Itec.zkclient.IZkDataListener)2 PropertyKey (org.apache.helix.PropertyKey)2 ZNRecord (org.apache.helix.ZNRecord)2 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)2 LiveInstance (org.apache.helix.model.LiveInstance)2 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 HelixDataAccessor (org.apache.helix.HelixDataAccessor)1 MockMSModelFactory (org.apache.helix.mock.participant.MockMSModelFactory)1 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)1 Test (org.testng.annotations.Test)1