Search in sources :

Example 51 with ZKHelixDataAccessor

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

the class TestSemiAutoRebalance method beforeClass.

@BeforeClass
public void beforeClass() throws InterruptedException {
    System.out.println("START " + getShortClassName() + " at " + new Date(System.currentTimeMillis()));
    String namespace = "/" + CLUSTER_NAME;
    if (_gZkClient.exists(namespace)) {
        _gZkClient.deleteRecursively(namespace);
    }
    // setup storage cluster
    _gSetupTool.addCluster(CLUSTER_NAME, true);
    _gSetupTool.addResourceToCluster(CLUSTER_NAME, DB_NAME, PARTITION_NUMBER, STATE_MODEL, IdealState.RebalanceMode.SEMI_AUTO.toString());
    _accessor = new ZKHelixDataAccessor(CLUSTER_NAME, _baseAccessor);
    _keyBuilder = _accessor.keyBuilder();
    List<String> instances = new ArrayList<String>();
    for (int i = 0; i < PARTICIPANT_NUMBER; i++) {
        String instance = PARTICIPANT_PREFIX + "_" + (PARTICIPANT_START_PORT + i);
        _gSetupTool.addInstanceToCluster(CLUSTER_NAME, instance);
        instances.add(instance);
    }
    _gSetupTool.rebalanceStorageCluster(CLUSTER_NAME, DB_NAME, REPLICA_NUMBER);
    // start dummy participants
    for (int i = 0; i < PARTICIPANT_NUMBER; i++) {
        MockParticipantManager participant = new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, instances.get(i));
        participant.syncStart();
        _participants.add(participant);
    }
    // start controller
    String controllerName = CONTROLLER_PREFIX + "_0";
    _controller = new ClusterControllerManager(ZK_ADDR, CLUSTER_NAME, controllerName);
    _controller.syncStart();
    Thread.sleep(1000);
    // verify ideal state and external view
    IdealState idealState = _accessor.getProperty(_keyBuilder.idealStates(DB_NAME));
    Assert.assertNotNull(idealState);
    Assert.assertEquals(idealState.getNumPartitions(), PARTITION_NUMBER);
    for (String partition : idealState.getPartitionSet()) {
        List<String> preferenceList = idealState.getPreferenceList(partition);
        Assert.assertNotNull(preferenceList);
        Assert.assertEquals(preferenceList.size(), REPLICA_NUMBER);
    }
    ExternalView externalView = _accessor.getProperty(_keyBuilder.externalView(DB_NAME));
    Assert.assertNotNull(externalView);
    Assert.assertEquals(externalView.getPartitionSet().size(), PARTITION_NUMBER);
    for (String partition : externalView.getPartitionSet()) {
        Map<String, String> stateMap = externalView.getStateMap(partition);
        Assert.assertEquals(stateMap.size(), REPLICA_NUMBER);
        int masters = 0;
        for (String state : stateMap.values()) {
            if (state.equals(MasterSlaveSMD.States.MASTER.name())) {
                ++masters;
            }
        }
        Assert.assertEquals(masters, 1);
    }
}
Also used : ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) ExternalView(org.apache.helix.model.ExternalView) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) ArrayList(java.util.ArrayList) Date(java.util.Date) IdealState(org.apache.helix.model.IdealState) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) BeforeClass(org.testng.annotations.BeforeClass)

Example 52 with ZKHelixDataAccessor

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

the class TestP2PMessageSemiAuto method beforeClass.

@BeforeClass
public void beforeClass() throws InterruptedException {
    System.out.println("START " + getShortClassName() + " at " + new Date(System.currentTimeMillis()));
    // setup storage cluster
    _gSetupTool.addCluster(CLUSTER_NAME, true);
    for (int i = 0; i < PARTICIPANT_NUMBER; i++) {
        String instance = PARTICIPANT_PREFIX + "_" + (PARTICIPANT_START_PORT + i);
        _gSetupTool.addInstanceToCluster(CLUSTER_NAME, instance);
        _instances.add(instance);
    }
    // start dummy participants
    for (int i = 0; i < PARTICIPANT_NUMBER; i++) {
        MockParticipantManager participant = new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, _instances.get(i));
        participant.setTransition(new DelayedTransitionBase(50));
        participant.syncStart();
        _participants.add(participant);
    }
    createDBInSemiAuto(_gSetupTool, CLUSTER_NAME, DB_NAME_1, _instances, BuiltInStateModelDefinitions.MasterSlave.name(), PARTITION_NUMBER, REPLICA_NUMBER);
    createDBInSemiAuto(_gSetupTool, CLUSTER_NAME, DB_NAME_2, _instances, BuiltInStateModelDefinitions.MasterSlave.name(), PARTITION_NUMBER, REPLICA_NUMBER);
    // start controller
    String controllerName = CONTROLLER_PREFIX + "_0";
    _controller = new ClusterControllerManager(ZK_ADDR, CLUSTER_NAME, controllerName);
    _controller.syncStart();
    _clusterVerifier = new BestPossibleExternalViewVerifier.Builder(CLUSTER_NAME).setZkClient(_gZkClient).build();
    Assert.assertTrue(_clusterVerifier.verify());
    _configAccessor = new ConfigAccessor(_gZkClient);
    _accessor = new ZKHelixDataAccessor(CLUSTER_NAME, _baseAccessor);
}
Also used : ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) DelayedTransitionBase(org.apache.helix.integration.DelayedTransitionBase) BestPossibleExternalViewVerifier(org.apache.helix.tools.ClusterVerifiers.BestPossibleExternalViewVerifier) ConfigAccessor(org.apache.helix.ConfigAccessor) Date(java.util.Date) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) BeforeClass(org.testng.annotations.BeforeClass)

Example 53 with ZKHelixDataAccessor

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

the class TestParticipantManager 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 = 1;
    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
    1, "MasterSlave", // do rebalance
    true);
    HelixManager participant = new ZKHelixManager(clusterName, "localhost_12918", InstanceType.PARTICIPANT, ZK_ADDR);
    participant.getStateMachineEngine().registerStateModelFactory("MasterSlave", new MockMSModelFactory());
    participant.connect();
    HelixManager controller = new ZKHelixManager(clusterName, "controller_0", InstanceType.CONTROLLER, ZK_ADDR);
    controller.connect();
    boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // cleanup
    controller.disconnect();
    participant.disconnect();
    // verify all live-instances and leader nodes are gone
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance("localhost_12918")));
    Assert.assertNull(accessor.getProperty(keyBuilder.controllerLeader()));
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) HelixManager(org.apache.helix.HelixManager) 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) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Example 54 with ZKHelixDataAccessor

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

the class ClusterSetup method swapInstance.

public void swapInstance(String clusterName, String oldInstanceName, String newInstanceName) {
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
    Builder keyBuilder = accessor.keyBuilder();
    InstanceConfig oldConfig = accessor.getProperty(keyBuilder.instanceConfig(oldInstanceName));
    if (oldConfig == null) {
        String error = "Old instance " + oldInstanceName + " does not exist, cannot swap";
        _logger.warn(error);
        throw new HelixException(error);
    }
    InstanceConfig newConfig = accessor.getProperty(keyBuilder.instanceConfig(newInstanceName));
    if (newConfig == null) {
        String error = "New instance " + newInstanceName + " does not exist, cannot swap";
        _logger.warn(error);
        throw new HelixException(error);
    }
    ClusterConfig clusterConfig = accessor.getProperty(keyBuilder.clusterConfig());
    // ensure old instance is disabled, otherwise fail
    if (oldConfig.getInstanceEnabled() && (clusterConfig.getDisabledInstances() == null || !clusterConfig.getDisabledInstances().containsKey(oldInstanceName))) {
        String error = "Old instance " + oldInstanceName + " is enabled, it need to be disabled and turned off";
        _logger.warn(error);
        throw new HelixException(error);
    }
    // ensure old instance is down, otherwise fail
    List<String> liveInstanceNames = accessor.getChildNames(accessor.keyBuilder().liveInstances());
    if (liveInstanceNames.contains(oldInstanceName)) {
        String error = "Old instance " + oldInstanceName + " is still on, it need to be disabled and turned off";
        _logger.warn(error);
        throw new HelixException(error);
    }
    dropInstanceFromCluster(clusterName, oldInstanceName);
    List<IdealState> existingIdealStates = accessor.getChildValues(accessor.keyBuilder().idealStates());
    for (IdealState idealState : existingIdealStates) {
        swapInstanceInIdealState(idealState, oldInstanceName, newInstanceName);
        accessor.setProperty(accessor.keyBuilder().idealStates(idealState.getResourceName()), idealState);
    }
}
Also used : HelixException(org.apache.helix.HelixException) InstanceConfig(org.apache.helix.model.InstanceConfig) ConstraintItemBuilder(org.apache.helix.model.builder.ConstraintItemBuilder) OptionBuilder(org.apache.commons.cli.OptionBuilder) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) Builder(org.apache.helix.PropertyKey.Builder) ZNRecord(org.apache.helix.ZNRecord) IdealState(org.apache.helix.model.IdealState) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) ClusterConfig(org.apache.helix.model.ClusterConfig)

Example 55 with ZKHelixDataAccessor

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

the class ClusterSetup method dropInstanceFromCluster.

public void dropInstanceFromCluster(String clusterName, String instanceId) {
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
    Builder keyBuilder = accessor.keyBuilder();
    InstanceConfig instanceConfig = InstanceConfig.toInstanceConfig(instanceId);
    instanceId = instanceConfig.getInstanceName();
    // ensure node is stopped
    LiveInstance liveInstance = accessor.getProperty(keyBuilder.liveInstance(instanceId));
    if (liveInstance != null) {
        throw new HelixException("Can't drop " + instanceId + ", please stop " + instanceId + " before drop it");
    }
    InstanceConfig config = accessor.getProperty(keyBuilder.instanceConfig(instanceId));
    if (config == null) {
        String error = "Node " + instanceId + " does not exist, cannot drop";
        _logger.warn(error);
        throw new HelixException(error);
    }
    ClusterConfig clusterConfig = accessor.getProperty(keyBuilder.clusterConfig());
    // ensure node is disabled, otherwise fail
    if (config.getInstanceEnabled() && (clusterConfig.getDisabledInstances() == null || !clusterConfig.getDisabledInstances().containsKey(instanceId))) {
        String error = "Node " + instanceId + " is enabled, cannot drop";
        _logger.warn(error);
        throw new HelixException(error);
    }
    _admin.dropInstance(clusterName, config);
}
Also used : HelixException(org.apache.helix.HelixException) InstanceConfig(org.apache.helix.model.InstanceConfig) LiveInstance(org.apache.helix.model.LiveInstance) ConstraintItemBuilder(org.apache.helix.model.builder.ConstraintItemBuilder) OptionBuilder(org.apache.commons.cli.OptionBuilder) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) Builder(org.apache.helix.PropertyKey.Builder) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) ClusterConfig(org.apache.helix.model.ClusterConfig)

Aggregations

ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)104 ZNRecord (org.apache.helix.ZNRecord)78 Date (java.util.Date)66 Test (org.testng.annotations.Test)66 Builder (org.apache.helix.PropertyKey.Builder)47 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)41 HelixDataAccessor (org.apache.helix.HelixDataAccessor)38 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)37 IdealState (org.apache.helix.model.IdealState)33 ZkBaseDataAccessor (org.apache.helix.manager.zk.ZkBaseDataAccessor)30 PropertyKey (org.apache.helix.PropertyKey)29 ExternalView (org.apache.helix.model.ExternalView)21 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)21 LiveInstance (org.apache.helix.model.LiveInstance)20 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)16 HelixManager (org.apache.helix.HelixManager)15 Message (org.apache.helix.model.Message)12 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)11 HashMap (java.util.HashMap)10 ZkClient (org.apache.helix.manager.zk.ZkClient)10