Search in sources :

Example 76 with HelixManager

use of org.apache.helix.HelixManager in project helix by apache.

the class TestSwapInstance method TestSwap.

@Test
public void TestSwap() throws Exception {
    HelixManager manager = _controller;
    HelixDataAccessor helixAccessor = manager.getHelixDataAccessor();
    _setupTool.addResourceToCluster(CLUSTER_NAME, "MyDB", 64, STATE_MODEL);
    _setupTool.rebalanceStorageCluster(CLUSTER_NAME, "MyDB", _replica);
    ZNRecord idealStateOld1 = new ZNRecord("TestDB");
    ZNRecord idealStateOld2 = new ZNRecord("MyDB");
    IdealState is1 = helixAccessor.getProperty(helixAccessor.keyBuilder().idealStates("TestDB"));
    idealStateOld1.merge(is1.getRecord());
    IdealState is2 = helixAccessor.getProperty(helixAccessor.keyBuilder().idealStates("MyDB"));
    idealStateOld2.merge(is2.getRecord());
    Assert.assertTrue(ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME)));
    String instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + 0);
    ZKHelixAdmin tool = new ZKHelixAdmin(_gZkClient);
    _setupTool.getClusterManagementTool().enableInstance(CLUSTER_NAME, instanceName, false);
    boolean result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
    Assert.assertTrue(result);
    String instanceName2 = PARTICIPANT_PREFIX + "_" + (START_PORT + 444);
    _setupTool.addInstanceToCluster(CLUSTER_NAME, instanceName2);
    boolean exception = false;
    try {
        _setupTool.swapInstance(CLUSTER_NAME, instanceName, instanceName2);
    } catch (Exception e) {
        exception = true;
    }
    Assert.assertTrue(exception);
    _participants[0].syncStop();
    Thread.sleep(1000);
    exception = false;
    try {
        _setupTool.swapInstance(CLUSTER_NAME, instanceName, instanceName2);
    } catch (Exception e) {
        e.printStackTrace();
        exception = true;
    }
    Assert.assertFalse(exception);
    MockParticipantManager newParticipant = new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, instanceName2);
    newParticipant.syncStart();
    result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
    Assert.assertTrue(result);
    is1 = helixAccessor.getProperty(helixAccessor.keyBuilder().idealStates("TestDB"));
    is2 = helixAccessor.getProperty(helixAccessor.keyBuilder().idealStates("MyDB"));
    for (String key : idealStateOld1.getMapFields().keySet()) {
        for (String host : idealStateOld1.getMapField(key).keySet()) {
            if (host.equals(instanceName)) {
                Assert.assertTrue(idealStateOld1.getMapField(key).get(instanceName).equals(is1.getRecord().getMapField(key).get(instanceName2)));
            } else {
                Assert.assertTrue(idealStateOld1.getMapField(key).get(host).equals(is1.getRecord().getMapField(key).get(host)));
            }
        }
    }
    for (String key : idealStateOld1.getListFields().keySet()) {
        Assert.assertEquals(idealStateOld1.getListField(key).size(), is1.getRecord().getListField(key).size());
        for (int i = 0; i < idealStateOld1.getListField(key).size(); i++) {
            String host = idealStateOld1.getListField(key).get(i);
            String newHost = is1.getRecord().getListField(key).get(i);
            if (host.equals(instanceName)) {
                Assert.assertTrue(newHost.equals(instanceName2));
            } else {
                // System.out.println(key + " " + i+ " " + host + " "+newHost);
                // System.out.println(idealStateOld1.getListField(key));
                // System.out.println(is1.getRecord().getListField(key));
                Assert.assertTrue(host.equals(newHost));
            }
        }
    }
}
Also used : HelixManager(org.apache.helix.HelixManager) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) IdealState(org.apache.helix.model.IdealState) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 77 with HelixManager

use of org.apache.helix.HelixManager in project helix by apache.

the class TestControllerHistory method testControllerLeaderHistory.

@Test()
public void testControllerLeaderHistory() throws Exception {
    HelixManager manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, "admin", InstanceType.ADMINISTRATOR, ZK_ADDR);
    manager.connect();
    PropertyKey.Builder keyBuilder = new PropertyKey.Builder(CLUSTER_NAME);
    PropertyKey propertyKey = keyBuilder.controllerLeaderHistory();
    LeaderHistory leaderHistory = manager.getHelixDataAccessor().getProperty(propertyKey);
    Assert.assertNotNull(leaderHistory);
    List<String> list = leaderHistory.getRecord().getListField("HISTORY");
    Assert.assertEquals(list.size(), 1);
    for (int i = 0; i <= 12; i++) {
        _controller.syncStop();
        _controller = new ClusterControllerManager(ZK_ADDR, CLUSTER_NAME, "Controller-" + i);
        _controller.syncStart();
    }
    leaderHistory = manager.getHelixDataAccessor().getProperty(propertyKey);
    Assert.assertNotNull(leaderHistory);
    list = leaderHistory.getRecord().getListField("HISTORY");
    Assert.assertEquals(list.size(), 10);
    manager.disconnect();
}
Also used : ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) HelixManager(org.apache.helix.HelixManager) LeaderHistory(org.apache.helix.model.LeaderHistory) PropertyKey(org.apache.helix.PropertyKey) Test(org.testng.annotations.Test)

Example 78 with HelixManager

use of org.apache.helix.HelixManager in project helix by apache.

the class TestDistributedControllerManager method simpleIntegrationTest.

@Test
public void simpleIntegrationTest() 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);
    HelixManager[] distributedControllers = new HelixManager[n];
    for (int i = 0; i < n; i++) {
        int port = 12918 + i;
        distributedControllers[i] = new ZKHelixManager(clusterName, "localhost_" + port, InstanceType.CONTROLLER_PARTICIPANT, ZK_ADDR);
        distributedControllers[i].getStateMachineEngine().registerStateModelFactory("MasterSlave", new MockMSModelFactory());
        distributedControllers[i].connect();
    }
    boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // disconnect first distributed-controller, and verify second takes leadership
    distributedControllers[0].disconnect();
    // verify leader changes to localhost_12919
    Thread.sleep(100);
    result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance("localhost_12918")));
    LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
    Assert.assertNotNull(leader);
    Assert.assertEquals(leader.getId(), "localhost_12919");
    // clean up
    distributedControllers[1].disconnect();
    Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance("localhost_12919")));
    Assert.assertNull(accessor.getProperty(keyBuilder.controllerLeader()));
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : HelixManager(org.apache.helix.HelixManager) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) 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 79 with HelixManager

use of org.apache.helix.HelixManager in project helix by apache.

the class TestClusterStartsup method testParticipantStartUp.

@Test()
public void testParticipantStartUp() throws Exception {
    setupCluster();
    String controllerMsgPath = PropertyPathBuilder.controllerMessage(CLUSTER_NAME);
    _gZkClient.deleteRecursively(controllerMsgPath);
    HelixManager manager = null;
    try {
        manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, "localhost_" + (START_PORT + 1), InstanceType.PARTICIPANT, ZK_ADDR);
        manager.connect();
        Assert.fail("Should fail on connect() since cluster structure is not set up");
    } catch (HelixException e) {
    // OK
    }
    if (manager != null) {
        AssertJUnit.assertFalse(manager.isConnected());
    }
    try {
        manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, "localhost_" + (START_PORT + 3), InstanceType.PARTICIPANT, ZK_ADDR);
        manager.connect();
        Assert.fail("Should fail on connect() since cluster structure is not set up");
    } catch (HelixException e) {
    // OK
    }
    if (manager != null) {
        AssertJUnit.assertFalse(manager.isConnected());
    }
    setupCluster();
    String stateModelPath = PropertyPathBuilder.stateModelDef(CLUSTER_NAME);
    _gZkClient.deleteRecursively(stateModelPath);
    try {
        manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, "localhost_" + (START_PORT + 1), InstanceType.PARTICIPANT, ZK_ADDR);
        manager.connect();
        Assert.fail("Should fail on connect() since cluster structure is not set up");
    } catch (HelixException e) {
    // OK
    }
    if (manager != null) {
        AssertJUnit.assertFalse(manager.isConnected());
    }
    setupCluster();
    String instanceStatusUpdatePath = PropertyPathBuilder.instanceStatusUpdate(CLUSTER_NAME, "localhost_" + (START_PORT + 1));
    _gZkClient.deleteRecursively(instanceStatusUpdatePath);
    try {
        manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, "localhost_" + (START_PORT + 1), InstanceType.PARTICIPANT, ZK_ADDR);
        manager.connect();
        Assert.fail("Should fail on connect() since cluster structure is not set up");
    } catch (HelixException e) {
    // OK
    }
    if (manager != null) {
        AssertJUnit.assertFalse(manager.isConnected());
    }
}
Also used : HelixException(org.apache.helix.HelixException) HelixManager(org.apache.helix.HelixManager) Test(org.testng.annotations.Test)

Example 80 with HelixManager

use of org.apache.helix.HelixManager in project helix by apache.

the class GenericLeaderStandbyModel method onBecomeStandbyFromLeader.

@Transition(to = "STANDBY", from = "LEADER")
public void onBecomeStandbyFromLeader(Message message, NotificationContext context) {
    LOG.info("Become STANDBY from LEADER");
    HelixManager manager = context.getManager();
    if (manager == null) {
        throw new IllegalArgumentException("Require HelixManager in notification conext");
    }
    Builder keyBuilder = new Builder(manager.getClusterName());
    for (ChangeType notificationType : _notificationTypes) {
        if (notificationType == ChangeType.LIVE_INSTANCE) {
            manager.removeListener(keyBuilder.liveInstances(), _particHolder);
        } else if (notificationType == ChangeType.CONFIG) {
            manager.removeListener(keyBuilder.instanceConfigs(), _particHolder);
        } else if (notificationType == ChangeType.EXTERNAL_VIEW) {
            manager.removeListener(keyBuilder.externalViews(), _particHolder);
        } else {
            LOG.error("Unsupport notificationType:" + notificationType.toString());
        }
    }
}
Also used : HelixManager(org.apache.helix.HelixManager) ChangeType(org.apache.helix.HelixConstants.ChangeType) Builder(org.apache.helix.PropertyKey.Builder) Transition(org.apache.helix.participant.statemachine.Transition)

Aggregations

HelixManager (org.apache.helix.HelixManager)105 Test (org.testng.annotations.Test)44 HelixDataAccessor (org.apache.helix.HelixDataAccessor)35 ZNRecord (org.apache.helix.ZNRecord)27 Message (org.apache.helix.model.Message)23 PropertyKey (org.apache.helix.PropertyKey)20 Date (java.util.Date)18 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)17 Builder (org.apache.helix.PropertyKey.Builder)16 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)12 ExternalView (org.apache.helix.model.ExternalView)11 NotificationContext (org.apache.helix.NotificationContext)10 LiveInstance (org.apache.helix.model.LiveInstance)10 IdealState (org.apache.helix.model.IdealState)9 List (java.util.List)8 Criteria (org.apache.helix.Criteria)8 HelixAdmin (org.apache.helix.HelixAdmin)8 ZKHelixManager (org.apache.helix.manager.zk.ZKHelixManager)8 StringWriter (java.io.StringWriter)7