Search in sources :

Example 71 with IdealState

use of org.apache.helix.model.IdealState in project helix by apache.

the class TestIdealStateBuilder method testSemiAutoISBuilder.

@Test
public void testSemiAutoISBuilder() {
    SemiAutoModeISBuilder builder = new SemiAutoModeISBuilder("test-db");
    builder.setStateModel("MasterSlave").setNumPartitions(2).setNumReplica(2);
    builder.assignPreferenceList("test-db_0", "node_0", "node_1").assignPreferenceList("test-db_1", "node_1", "node_0");
    IdealState idealState = null;
    try {
        idealState = builder.build();
    } catch (Exception e) {
        Assert.fail("fail to build an auto mode ideal-state.", e);
    }
    // System.out.println("ideal-state: " + idealState);
    Assert.assertEquals(idealState.getRebalanceMode(), IdealState.RebalanceMode.SEMI_AUTO, "rebalancer mode should be semi-auto");
}
Also used : IdealState(org.apache.helix.model.IdealState) Test(org.testng.annotations.Test)

Example 72 with IdealState

use of org.apache.helix.model.IdealState in project helix by apache.

the class ClusterSetup method expandResource.

public void expandResource(String clusterName, String resourceName) {
    IdealState idealState = _admin.getResourceIdealState(clusterName, resourceName);
    if (idealState.getRebalanceMode() == RebalanceMode.FULL_AUTO || idealState.getRebalanceMode() == RebalanceMode.CUSTOMIZED) {
        _logger.info("Skipping idealState " + idealState.getResourceName() + " " + idealState.getRebalanceMode());
        return;
    }
    boolean anyLiveInstance = false;
    for (List<String> list : idealState.getRecord().getListFields().values()) {
        if (list.contains(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString())) {
            _logger.info("Skipping idealState " + idealState.getResourceName() + " with ANY_LIVEINSTANCE");
            anyLiveInstance = true;
            continue;
        }
    }
    if (anyLiveInstance) {
        return;
    }
    try {
        int replica = Integer.parseInt(idealState.getReplicas());
    } catch (Exception e) {
        _logger.error("", e);
        return;
    }
    if (idealState.getRecord().getListFields().size() == 0) {
        _logger.warn("Resource " + resourceName + " not balanced, skip");
        return;
    }
    balanceIdealState(clusterName, idealState);
}
Also used : IdealState(org.apache.helix.model.IdealState) HelixException(org.apache.helix.HelixException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException)

Example 73 with IdealState

use of org.apache.helix.model.IdealState in project helix by apache.

the class ClusterSetup method addResourceProperty.

public void addResourceProperty(String clusterName, String resourceName, String propertyKey, String propertyVal) {
    IdealState idealState = _admin.getResourceIdealState(clusterName, resourceName);
    if (idealState == null) {
        throw new HelixException("Resource: " + resourceName + " has NOT been added yet");
    }
    idealState.getRecord().setSimpleField(propertyKey, propertyVal);
    _admin.setResourceIdealState(clusterName, resourceName, idealState);
}
Also used : HelixException(org.apache.helix.HelixException) IdealState(org.apache.helix.model.IdealState)

Example 74 with IdealState

use of org.apache.helix.model.IdealState in project helix by apache.

the class ClusterSetup method createIdealStateForResourceGroup.

/**
 * Create an IdealState for a resource that belongs to a resource group We use
 * "resourceGroupName$resourceInstanceTag" as the IdealState znode name to differetiate different
 * resources from the same resourceGroup.
 */
public IdealState createIdealStateForResourceGroup(String resourceGroupName, String resourceTag, int numPartition, int replica, String rebalanceMode, String stateModelDefName) {
    String idealStateId = genIdealStateNameWithResourceTag(resourceGroupName, resourceTag);
    IdealState idealState = new IdealState(idealStateId);
    idealState.setNumPartitions(numPartition);
    idealState.setStateModelDefRef(stateModelDefName);
    IdealState.RebalanceMode mode = idealState.rebalanceModeFromString(rebalanceMode, IdealState.RebalanceMode.SEMI_AUTO);
    idealState.setRebalanceMode(mode);
    idealState.setReplicas("" + replica);
    idealState.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY);
    idealState.setResourceGroupName(resourceGroupName);
    idealState.setInstanceGroupTag(resourceTag);
    idealState.enableGroupRouting(true);
    return idealState;
}
Also used : RebalanceMode(org.apache.helix.model.IdealState.RebalanceMode) IdealState(org.apache.helix.model.IdealState)

Example 75 with IdealState

use of org.apache.helix.model.IdealState in project helix by apache.

the class StrictMatchExternalViewVerifier method verifyState.

@Override
protected boolean verifyState() {
    try {
        PropertyKey.Builder keyBuilder = _accessor.keyBuilder();
        // read cluster once and do verification
        ClusterDataCache cache = new ClusterDataCache();
        cache.refresh(_accessor);
        Map<String, IdealState> idealStates = cache.getIdealStates();
        if (idealStates == null) {
            // ideal state is null because ideal state is dropped
            idealStates = Collections.emptyMap();
        }
        // filter out all resources that use Task state model
        Iterator<Map.Entry<String, IdealState>> it = idealStates.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, IdealState> pair = it.next();
            if (pair.getValue().getStateModelDefRef().equals(TaskConstants.STATE_MODEL_NAME)) {
                it.remove();
            }
        }
        // verify live instances.
        if (_expectLiveInstances != null && !_expectLiveInstances.isEmpty()) {
            Set<String> actualLiveNodes = cache.getLiveInstances().keySet();
            if (!_expectLiveInstances.equals(actualLiveNodes)) {
                return false;
            }
        }
        Map<String, ExternalView> extViews = _accessor.getChildValuesMap(keyBuilder.externalViews());
        if (extViews == null) {
            extViews = Collections.emptyMap();
        }
        // Filter resources if requested
        if (_resources != null && !_resources.isEmpty()) {
            idealStates.keySet().retainAll(_resources);
            extViews.keySet().retainAll(_resources);
        }
        // add empty idealState for the resource
        for (String resource : extViews.keySet()) {
            if (!idealStates.containsKey(resource)) {
                idealStates.put(resource, new IdealState(resource));
            }
        }
        for (String resourceName : idealStates.keySet()) {
            ExternalView extView = extViews.get(resourceName);
            IdealState idealState = idealStates.get(resourceName);
            if (extView == null) {
                if (idealState.isExternalViewDisabled()) {
                    continue;
                } else {
                    LOG.debug("externalView for " + resourceName + " is not available");
                    return false;
                }
            }
            boolean result = verifyExternalView(cache, extView, idealState);
            if (!result) {
                return false;
            }
        }
        return true;
    } catch (Exception e) {
        LOG.error("exception in verification", e);
        return false;
    }
}
Also used : ExternalView(org.apache.helix.model.ExternalView) ClusterDataCache(org.apache.helix.controller.stages.ClusterDataCache) IdealState(org.apache.helix.model.IdealState) HelixException(org.apache.helix.HelixException) HashMap(java.util.HashMap) Map(java.util.Map) PropertyKey(org.apache.helix.PropertyKey)

Aggregations

IdealState (org.apache.helix.model.IdealState)250 Test (org.testng.annotations.Test)110 ZNRecord (org.apache.helix.ZNRecord)69 ExternalView (org.apache.helix.model.ExternalView)54 ArrayList (java.util.ArrayList)50 HelixAdmin (org.apache.helix.HelixAdmin)42 HashMap (java.util.HashMap)38 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)35 Date (java.util.Date)33 HelixDataAccessor (org.apache.helix.HelixDataAccessor)33 Builder (org.apache.helix.PropertyKey.Builder)33 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)33 HashSet (java.util.HashSet)31 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)28 Map (java.util.Map)26 PropertyKey (org.apache.helix.PropertyKey)26 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)24 StateModelDefinition (org.apache.helix.model.StateModelDefinition)23 List (java.util.List)21 HelixException (org.apache.helix.HelixException)21