Search in sources :

Example 11 with MockParticipantManager

use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.

the class TestHelixAdminCli method testStartCluster.

@Test
public void testStartCluster() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    String grandClusterName = clusterName + "_grand";
    final int n = 6;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    MockParticipantManager[] participants = new MockParticipantManager[n];
    ClusterDistributedController[] controllers = new ClusterDistributedController[2];
    setupCluster(clusterName, grandClusterName, n, participants, controllers);
    // activate clusters
    // wrong grand clusterName
    String command = "-zkSvr localhost:2183 -activateCluster " + clusterName + " nonExistGrandCluster true";
    try {
        ClusterSetup.processCommandLineArgs(command.split("\\s+"));
        Assert.fail("add " + clusterName + " to grandCluster should fail since grandCluster doesn't exists");
    } catch (Exception e) {
    // OK
    }
    // wrong cluster name
    command = "-zkSvr localhost:2183 -activateCluster nonExistCluster " + grandClusterName + " true";
    try {
        ClusterSetup.processCommandLineArgs(command.split("\\s+"));
        Assert.fail("add nonExistCluster to " + grandClusterName + " should fail since nonExistCluster doesn't exists");
    } catch (Exception e) {
    // OK
    }
    command = "-zkSvr localhost:2183 -activateCluster " + clusterName + " " + grandClusterName + " true";
    ClusterSetup.processCommandLineArgs(command.split("\\s+"));
    Thread.sleep(500);
    // drop a running cluster
    command = "-zkSvr localhost:2183 -dropCluster " + clusterName;
    try {
        ClusterSetup.processCommandLineArgs(command.split("\\s+"));
        Assert.fail("drop " + clusterName + " should fail since it's still running");
    } catch (Exception e) {
    // OK
    }
    // verify leader node
    BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    HelixDataAccessor accessor = new ZKHelixDataAccessor(grandClusterName, baseAccessor);
    LiveInstance controllerLeader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
    Assert.assertNotNull(controllerLeader, "controllerLeader should be either controller_9000 or controller_9001");
    Assert.assertTrue(controllerLeader.getInstanceName().startsWith("controller_900"));
    accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
    LiveInstance leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
    for (int i = 0; i < 20; i++) {
        if (leader != null) {
            break;
        }
        Thread.sleep(200);
        leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
    }
    Assert.assertTrue(leader.getInstanceName().startsWith("controller_900"));
    boolean verifyResult = ClusterStateVerifier.verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(verifyResult);
    verifyResult = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(verifyResult);
    // clean up
    for (ClusterDistributedController controller : controllers) {
        controller.syncStop();
    }
    for (int i = 0; i < participants.length; i++) {
        participants[i].syncStop();
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) MasterNbInExtViewVerifier(org.apache.helix.tools.ClusterStateVerifier.MasterNbInExtViewVerifier) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) ClusterDistributedController(org.apache.helix.integration.manager.ClusterDistributedController) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 12 with MockParticipantManager

use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.

the class TestZkManagerWithAutoFallbackStore method testBasic.

@Test
public void testBasic() 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
    32, // number of nodes
    n, // replicas
    2, "MasterSlave", // do rebalance
    false);
    // start participants
    MockParticipantManager[] participants = new MockParticipantManager[n];
    for (int i = 0; i < 1; i++) {
        String instanceName = "localhost_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
        participants[i].syncStart();
    }
    // add some data to fallback path: HELIX_PROPERTYSTORE
    BaseDataAccessor<ZNRecord> accessor = participants[0].getHelixDataAccessor().getBaseDataAccessor();
    for (int i = 0; i < 10; i++) {
        String path = String.format("/%s/HELIX_PROPERTYSTORE/%d", clusterName, i);
        ZNRecord record = new ZNRecord("" + i);
        record.setSimpleField("key1", "value1");
        accessor.set(path, record, AccessOption.PERSISTENT);
    }
    ZkHelixPropertyStore<ZNRecord> store = participants[0].getHelixPropertyStore();
    // read shall use fallback paths
    for (int i = 0; i < 10; i++) {
        String path = String.format("/%d", i);
        ZNRecord record = store.get(path, null, 0);
        Assert.assertNotNull(record);
        Assert.assertEquals(record.getId(), "" + i);
        Assert.assertNotNull(record.getSimpleField("key1"));
        Assert.assertEquals(record.getSimpleField("key1"), "value1");
    }
    // update shall update new paths
    for (int i = 0; i < 10; i++) {
        String path = String.format("/%d", i);
        store.update(path, new DataUpdater<ZNRecord>() {

            @Override
            public ZNRecord update(ZNRecord currentData) {
                if (currentData != null) {
                    currentData.setSimpleField("key2", "value2");
                }
                return currentData;
            }
        }, AccessOption.PERSISTENT);
    }
    for (int i = 0; i < 10; i++) {
        String path = String.format("/%d", i);
        ZNRecord record = store.get(path, null, 0);
        Assert.assertNotNull(record);
        Assert.assertEquals(record.getId(), "" + i);
        Assert.assertNotNull(record.getSimpleField("key1"));
        Assert.assertEquals(record.getSimpleField("key1"), "value1");
        Assert.assertNotNull(record.getSimpleField("key2"));
        Assert.assertEquals(record.getSimpleField("key2"), "value2");
    }
    // set shall use new path
    for (int i = 10; i < 20; i++) {
        String path = String.format("/%d", i);
        ZNRecord record = new ZNRecord("" + i);
        record.setSimpleField("key3", "value3");
        store.set(path, record, AccessOption.PERSISTENT);
    }
    for (int i = 10; i < 20; i++) {
        String path = String.format("/%d", i);
        ZNRecord record = store.get(path, null, 0);
        Assert.assertNotNull(record);
        Assert.assertEquals(record.getId(), "" + i);
        Assert.assertNotNull(record.getSimpleField("key3"));
        Assert.assertEquals(record.getSimpleField("key3"), "value3");
    }
    participants[0].syncStop();
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) Date(java.util.Date) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 13 with MockParticipantManager

use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.

the class TestSemiAutoStateTransition method testOfflineToSecondTopState.

@Test
public void testOfflineToSecondTopState() throws Exception {
    _participants[0].syncStop();
    Thread.sleep(2000L);
    ExternalView externalView = _accessor.getProperty(_keyBuilder.externalView(WorkflowGenerator.DEFAULT_TGT_DB + "0"));
    Map<String, String> stateMap = externalView.getStateMap(WorkflowGenerator.DEFAULT_TGT_DB + "0_0");
    Assert.assertEquals("MASTER", stateMap.get(PARTICIPANT_PREFIX + "_" + (_startPort + 1)));
    Assert.assertEquals("SLAVE", stateMap.get(PARTICIPANT_PREFIX + "_" + (_startPort + 2)));
    String instanceName = PARTICIPANT_PREFIX + "_" + _startPort;
    _participants[0] = new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, instanceName);
    // add a state model with non-OFFLINE initial state
    StateMachineEngine stateMach = _participants[0].getStateMachineEngine();
    MockDelayMSStateModelFactory delayFactory = new MockDelayMSStateModelFactory().setDelay(300000L);
    stateMach.registerStateModelFactory(MASTER_SLAVE_STATE_MODEL, delayFactory);
    _participants[0].syncStart();
    Thread.sleep(2000L);
    externalView = _accessor.getProperty(_keyBuilder.externalView(WorkflowGenerator.DEFAULT_TGT_DB + "0"));
    stateMap = externalView.getStateMap(WorkflowGenerator.DEFAULT_TGT_DB + "0_0");
    Assert.assertEquals("OFFLINE", stateMap.get(PARTICIPANT_PREFIX + "_" + _startPort));
    Assert.assertEquals("MASTER", stateMap.get(PARTICIPANT_PREFIX + "_" + (_startPort + 1)));
    Assert.assertEquals("SLAVE", stateMap.get(PARTICIPANT_PREFIX + "_" + (_startPort + 2)));
}
Also used : ExternalView(org.apache.helix.model.ExternalView) StateMachineEngine(org.apache.helix.participant.StateMachineEngine) MockDelayMSStateModelFactory(org.apache.helix.mock.participant.MockDelayMSStateModelFactory) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) Test(org.testng.annotations.Test)

Example 14 with MockParticipantManager

use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.

the class TestZkClusterManager method testLiveInstanceInfoProvider.

@Test
public void testLiveInstanceInfoProvider() throws Exception {
    System.out.println("START " + className + ".testLiveInstanceInfoProvider() at " + new Date(System.currentTimeMillis()));
    final String clusterName = CLUSTER_PREFIX + "_" + className + "_liveInstanceInfoProvider";
    class provider implements LiveInstanceInfoProvider {

        boolean _flag = false;

        public provider(boolean genSessionId) {
            _flag = genSessionId;
        }

        @Override
        public ZNRecord getAdditionalLiveInstanceInfo() {
            ZNRecord record = new ZNRecord("info");
            record.setSimpleField("simple", "value");
            List<String> listFieldVal = new ArrayList<String>();
            listFieldVal.add("val1");
            listFieldVal.add("val2");
            listFieldVal.add("val3");
            record.setListField("list", listFieldVal);
            Map<String, String> mapFieldVal = new HashMap<String, String>();
            mapFieldVal.put("k1", "val1");
            mapFieldVal.put("k2", "val2");
            mapFieldVal.put("k3", "val3");
            record.setMapField("map", mapFieldVal);
            if (_flag) {
                record.setSimpleField("SESSION_ID", "value");
                record.setSimpleField("LIVE_INSTANCE", "value");
                record.setSimpleField("Others", "value");
            }
            return record;
        }
    }
    TestHelper.setupEmptyCluster(_gZkClient, clusterName);
    int[] ids = { 0, 1, 2, 3, 4, 5 };
    setupInstances(clusterName, ids);
    // ///////////////////
    ZKHelixManager manager = new ZKHelixManager(clusterName, "localhost_0", InstanceType.PARTICIPANT, ZK_ADDR);
    manager.connect();
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    LiveInstance liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_0"));
    Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 0);
    Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 0);
    Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 3);
    manager = new ZKHelixManager(clusterName, "localhost_1", InstanceType.PARTICIPANT, ZK_ADDR);
    manager.setLiveInstanceInfoProvider(new provider(false));
    manager.connect();
    accessor = manager.getHelixDataAccessor();
    liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_1"));
    Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 4);
    manager = new ZKHelixManager(clusterName, "localhost_2", InstanceType.PARTICIPANT, ZK_ADDR);
    manager.setLiveInstanceInfoProvider(new provider(true));
    manager.connect();
    accessor = manager.getHelixDataAccessor();
    liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_2"));
    Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 5);
    Assert.assertFalse(liveInstance.getSessionId().equals("value"));
    Assert.assertFalse(liveInstance.getLiveInstance().equals("value"));
    // //////////////////////////////////
    MockParticipantManager manager2 = new MockParticipantManager(ZK_ADDR, clusterName, "localhost_3");
    manager2.setLiveInstanceInfoProvider(new provider(true));
    manager2.connect();
    accessor = manager2.getHelixDataAccessor();
    liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_3"));
    Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 5);
    Assert.assertFalse(liveInstance.getSessionId().equals("value"));
    Assert.assertFalse(liveInstance.getLiveInstance().equals("value"));
    String sessionId = liveInstance.getSessionId();
    ZkTestHelper.expireSession(manager2.getZkClient());
    Thread.sleep(1000);
    liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_3"));
    Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1);
    Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 5);
    Assert.assertFalse(liveInstance.getSessionId().equals("value"));
    Assert.assertFalse(liveInstance.getLiveInstance().equals("value"));
    Assert.assertFalse(sessionId.equals(liveInstance.getSessionId()));
    System.out.println("END " + className + ".testLiveInstanceInfoProvider() at " + new Date(System.currentTimeMillis()));
}
Also used : MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) HashMap(java.util.HashMap) LiveInstanceInfoProvider(org.apache.helix.LiveInstanceInfoProvider) ArrayList(java.util.ArrayList) Date(java.util.Date) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 15 with MockParticipantManager

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

Aggregations

MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)167 Test (org.testng.annotations.Test)113 Date (java.util.Date)101 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)101 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)43 ZNRecord (org.apache.helix.ZNRecord)40 IdealState (org.apache.helix.model.IdealState)35 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)35 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)34 HashMap (java.util.HashMap)33 HelixDataAccessor (org.apache.helix.HelixDataAccessor)30 ClusterSetup (org.apache.helix.tools.ClusterSetup)28 BeforeClass (org.testng.annotations.BeforeClass)27 ExternalView (org.apache.helix.model.ExternalView)24 BestPossibleExternalViewVerifier (org.apache.helix.tools.ClusterVerifiers.BestPossibleExternalViewVerifier)20 HelixClusterVerifier (org.apache.helix.tools.ClusterVerifiers.HelixClusterVerifier)19 Map (java.util.Map)18 PropertyKey (org.apache.helix.PropertyKey)16 Builder (org.apache.helix.PropertyKey.Builder)15 ClusterDistributedController (org.apache.helix.integration.manager.ClusterDistributedController)15