Search in sources :

Example 1 with Verifier

use of org.apache.helix.TestHelper.Verifier in project helix by apache.

the class TestZkFlapping method testControllerFlapping.

@Test
public void testControllerFlapping() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    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
    32, // number of nodes
    1, // replicas
    1, "MasterSlave", false);
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
    controller.syncStart();
    final ZkClient client = controller.getZkClient();
    final ZkStateCountListener listener = new ZkStateCountListener();
    client.subscribeStateChanges(listener);
    final AtomicInteger expectDisconnectCnt = new AtomicInteger(0);
    final int n = ZKHelixManager.MAX_DISCONNECT_THRESHOLD;
    for (int i = 0; i < n; i++) {
        String oldSessionId = ZkTestHelper.getSessionId(client);
        ZkTestHelper.simulateZkStateDisconnected(client);
        expectDisconnectCnt.incrementAndGet();
        // wait until we get invoked by zk state change to disconnected
        TestHelper.verify(new Verifier() {

            @Override
            public boolean verify() throws Exception {
                return listener.count == expectDisconnectCnt.get();
            }
        }, 30 * 1000);
        String newSessionId = ZkTestHelper.getSessionId(client);
        Assert.assertEquals(newSessionId, oldSessionId);
    }
    // make sure controller is NOT disconnected
    LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
    Assert.assertNotNull(leader, "Leader should exist after " + n + " disconnects");
    // trigger flapping
    ZkTestHelper.simulateZkStateDisconnected(client);
    // wait until we get invoked by zk state change to disconnected
    boolean success = TestHelper.verify(new Verifier() {

        @Override
        public boolean verify() throws Exception {
            return client.getShutdownTrigger();
        }
    }, 30 * 1000);
    Assert.assertTrue(success, "The " + (n + 1) + "th disconnect event should trigger ZkHelixManager#disonnect");
    // make sure controller is disconnected
    success = TestHelper.verify(new TestHelper.Verifier() {

        @Override
        public boolean verify() throws Exception {
            LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
            return leader == null;
        }
    }, 5 * 1000);
    Assert.assertTrue(success, "Leader should be gone after " + (n + 1) + " disconnects");
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : Verifier(org.apache.helix.TestHelper.Verifier) Date(java.util.Date) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) Test(org.testng.annotations.Test)

Example 2 with Verifier

use of org.apache.helix.TestHelper.Verifier in project helix by apache.

the class TestZkFlapping method testParticipantFlapping.

@Test
public void testParticipantFlapping() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    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
    32, // number of nodes
    1, // replicas
    1, "MasterSlave", false);
    final String instanceName = "localhost_12918";
    MockParticipantManager participant = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
    participant.syncStart();
    final ZkClient client = participant.getZkClient();
    final ZkStateCountListener listener = new ZkStateCountListener();
    client.subscribeStateChanges(listener);
    final AtomicInteger expectDisconnectCnt = new AtomicInteger(0);
    final int n = ZKHelixManager.MAX_DISCONNECT_THRESHOLD;
    for (int i = 0; i < n; i++) {
        String oldSessionId = ZkTestHelper.getSessionId(client);
        ZkTestHelper.simulateZkStateDisconnected(client);
        expectDisconnectCnt.incrementAndGet();
        // wait until we get invoked by zk state change to disconnected
        TestHelper.verify(new Verifier() {

            @Override
            public boolean verify() throws Exception {
                return listener.count == expectDisconnectCnt.get();
            }
        }, 30 * 1000);
        String newSessionId = ZkTestHelper.getSessionId(client);
        Assert.assertEquals(newSessionId, oldSessionId);
    }
    client.unsubscribeStateChanges(listener);
    // make sure participant is NOT disconnected
    LiveInstance liveInstance = accessor.getProperty(keyBuilder.liveInstance(instanceName));
    Assert.assertNotNull(liveInstance, "Live-instance should exist after " + n + " disconnects");
    // trigger flapping
    ZkTestHelper.simulateZkStateDisconnected(client);
    // wait until we get invoked by zk state change to disconnected
    boolean success = TestHelper.verify(new Verifier() {

        @Override
        public boolean verify() throws Exception {
            return client.getShutdownTrigger();
        }
    }, 30 * 1000);
    Assert.assertTrue(success, "The " + (n + 1) + "th disconnect event should trigger ZkHelixManager#disonnect");
    // make sure participant is disconnected
    success = TestHelper.verify(new TestHelper.Verifier() {

        @Override
        public boolean verify() throws Exception {
            LiveInstance liveInstance = accessor.getProperty(keyBuilder.liveInstance(instanceName));
            return liveInstance == null;
        }
    }, 3 * 1000);
    Assert.assertTrue(success, "Live-instance should be gone after " + (n + 1) + " disconnects");
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) Verifier(org.apache.helix.TestHelper.Verifier) Date(java.util.Date) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) Test(org.testng.annotations.Test)

Example 3 with Verifier

use of org.apache.helix.TestHelper.Verifier in project helix by apache.

the class TestFullAutoNodeTagging method testUntag.

@Test
public void testUntag() throws Exception {
    final int NUM_PARTICIPANTS = 2;
    final int NUM_PARTITIONS = 4;
    final int NUM_REPLICAS = 1;
    final String RESOURCE_NAME = "TestResource0";
    final String TAG = "ASSIGNABLE";
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    final String clusterName = className + "_" + methodName;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    // Set up cluster
    // participant port
    TestHelper.setupCluster(// participant port
    clusterName, // participant port
    ZK_ADDR, // participant port
    12918, // participant name prefix
    "localhost", // resource name prefix
    "TestResource", // resources
    1, // partitions per resource
    NUM_PARTITIONS, // number of nodes
    NUM_PARTICIPANTS, // replicas
    NUM_REPLICAS, // use FULL_AUTO mode to test node tagging
    "OnlineOffline", // use FULL_AUTO mode to test node tagging
    RebalanceMode.FULL_AUTO, // do rebalance
    true);
    // Tag the resource
    final HelixAdmin helixAdmin = new ZKHelixAdmin(_gZkClient);
    IdealState idealState = helixAdmin.getResourceIdealState(clusterName, RESOURCE_NAME);
    idealState.setInstanceGroupTag(TAG);
    helixAdmin.setResourceIdealState(clusterName, RESOURCE_NAME, idealState);
    // Get a data accessor
    final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    // Tag the participants
    for (int i = 0; i < NUM_PARTICIPANTS; i++) {
        final String instanceName = "localhost_" + (12918 + i);
        helixAdmin.addInstanceTag(clusterName, instanceName, TAG);
    }
    // Start controller
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
    controller.syncStart();
    // Start participants
    MockParticipantManager[] participants = new MockParticipantManager[NUM_PARTICIPANTS];
    for (int i = 0; i < NUM_PARTICIPANTS; i++) {
        final String instanceName = "localhost_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
        participants[i].syncStart();
    }
    // Verify that there are NUM_PARTITIONS partitions in the external view, each having
    // NUM_REPLICAS replicas, where all assigned replicas are to tagged nodes, and they are all
    // ONLINE.
    Verifier v = new Verifier() {

        @Override
        public boolean verify() throws Exception {
            ExternalView externalView = pollForProperty(ExternalView.class, accessor, keyBuilder.externalView(RESOURCE_NAME), true);
            if (externalView == null) {
                return false;
            }
            Set<String> taggedInstances = Sets.newHashSet(helixAdmin.getInstancesInClusterWithTag(clusterName, TAG));
            Set<String> partitionSet = externalView.getPartitionSet();
            if (partitionSet.size() != NUM_PARTITIONS) {
                return false;
            }
            for (String partitionName : partitionSet) {
                Map<String, String> stateMap = externalView.getStateMap(partitionName);
                if (stateMap.size() != NUM_REPLICAS) {
                    return false;
                }
                for (String participantName : stateMap.keySet()) {
                    if (!taggedInstances.contains(participantName)) {
                        return false;
                    }
                    String state = stateMap.get(participantName);
                    if (!state.equalsIgnoreCase("ONLINE")) {
                        return false;
                    }
                }
            }
            return true;
        }
    };
    // Run the verifier for both nodes tagged
    boolean initialResult = TestHelper.verify(v, 10 * 1000);
    Assert.assertTrue(initialResult);
    // Untag a node
    helixAdmin.removeInstanceTag(clusterName, "localhost_12918", TAG);
    // Verify again
    boolean finalResult = TestHelper.verify(v, 10 * 1000);
    Assert.assertTrue(finalResult);
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ExternalView(org.apache.helix.model.ExternalView) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ZkVerifier(org.apache.helix.tools.ClusterStateVerifier.ZkVerifier) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) Verifier(org.apache.helix.TestHelper.Verifier) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) IdealState(org.apache.helix.model.IdealState) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 4 with Verifier

use of org.apache.helix.TestHelper.Verifier in project helix by apache.

the class TestStandAloneCMMain method testStandAloneCMMain.

@Test()
public void testStandAloneCMMain() throws Exception {
    logger.info("RUN testStandAloneCMMain() at " + new Date(System.currentTimeMillis()));
    ClusterControllerManager newController = null;
    for (int i = 1; i <= 2; i++) {
        String controllerName = "controller_" + i;
        newController = new ClusterControllerManager(ZK_ADDR, CLUSTER_NAME, controllerName);
        newController.syncStart();
    }
    // stopCurrentLeader(_zkClient, CLUSTER_NAME, _startCMResultMap);
    _controller.syncStop();
    final HelixDataAccessor accessor = new ZKHelixDataAccessor(CLUSTER_NAME, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    final String newControllerName = newController.getInstanceName();
    TestHelper.verify(new Verifier() {

        @Override
        public boolean verify() throws Exception {
            LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
            if (leader == null) {
                return false;
            }
            return leader.getInstanceName().equals(newControllerName);
        }
    }, 30 * 1000);
    boolean result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
    Assert.assertTrue(result);
    logger.info("STOP testStandAloneCMMain() at " + new Date(System.currentTimeMillis()));
}
Also used : ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) Verifier(org.apache.helix.TestHelper.Verifier) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) Date(java.util.Date) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) 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)

Aggregations

Date (java.util.Date)4 HelixDataAccessor (org.apache.helix.HelixDataAccessor)4 PropertyKey (org.apache.helix.PropertyKey)4 Verifier (org.apache.helix.TestHelper.Verifier)4 ZNRecord (org.apache.helix.ZNRecord)4 Test (org.testng.annotations.Test)4 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)3 LiveInstance (org.apache.helix.model.LiveInstance)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)2 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)2 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)2 HelixAdmin (org.apache.helix.HelixAdmin)1 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)1 ExternalView (org.apache.helix.model.ExternalView)1 IdealState (org.apache.helix.model.IdealState)1 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)1 ZkVerifier (org.apache.helix.tools.ClusterStateVerifier.ZkVerifier)1