Search in sources :

Example 6 with Resource

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

the class CurrentStateComputationStage method updateMissingTopStateStatus.

private void updateMissingTopStateStatus(ClusterDataCache cache, ClusterStatusMonitor clusterStatusMonitor, Map<String, Resource> resourceMap, CurrentStateOutput currentStateOutput) {
    Map<String, Map<String, Long>> missingTopStateMap = cache.getMissingTopStateMap();
    long durationThreshold = Long.MAX_VALUE;
    if (cache.getClusterConfig() != null) {
        durationThreshold = cache.getClusterConfig().getMissTopStateDurationThreshold();
    }
    for (Resource resource : resourceMap.values()) {
        StateModelDefinition stateModelDef = cache.getStateModelDef(resource.getStateModelDefRef());
        if (stateModelDef == null || resource.getStateModelDefRef().equalsIgnoreCase(TASK_STATE_MODEL_NAME)) {
            // Resource does not have valid statemodel or it is task state model
            continue;
        }
        for (Partition partition : resource.getPartitions()) {
            Map<String, String> stateMap = currentStateOutput.getCurrentStateMap(resource.getResourceName(), partition);
            // Missing top state need to record
            if (!stateMap.values().contains(stateModelDef.getTopState()) && (!missingTopStateMap.containsKey(resource.getResourceName()) || !missingTopStateMap.get(resource.getResourceName()).containsKey(partition.getPartitionName()))) {
                reportNewTopStateMissing(cache, stateMap, missingTopStateMap, resource, partition, stateModelDef.getTopState());
            }
            // The first time participant started or controller switched will be ignored
            if (missingTopStateMap.containsKey(resource.getResourceName()) && missingTopStateMap.get(resource.getResourceName()).containsKey(partition.getPartitionName()) && stateMap.values().contains(stateModelDef.getTopState())) {
                reportTopStateComesBack(cache, stateMap, missingTopStateMap, resource, partition, clusterStatusMonitor, durationThreshold, stateModelDef.getTopState());
            }
        }
    }
    // Check whether it is already passed threshold
    for (String resourceName : missingTopStateMap.keySet()) {
        for (String partitionName : missingTopStateMap.get(resourceName).keySet()) {
            long startTime = missingTopStateMap.get(resourceName).get(partitionName);
            if (startTime > 0 && System.currentTimeMillis() - startTime > durationThreshold) {
                missingTopStateMap.get(resourceName).put(partitionName, TRANSITION_FAILED);
                if (clusterStatusMonitor != null) {
                    clusterStatusMonitor.updateMissingTopStateDurationStats(resourceName, 0L, false);
                }
            }
        }
    }
    if (clusterStatusMonitor != null) {
        clusterStatusMonitor.resetMaxMissingTopStateGauge();
    }
}
Also used : Partition(org.apache.helix.model.Partition) StateModelDefinition(org.apache.helix.model.StateModelDefinition) Resource(org.apache.helix.model.Resource) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with Resource

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

the class ResourceValidationStage method process.

@Override
public void process(ClusterEvent event) throws Exception {
    ClusterDataCache cache = event.getAttribute(AttributeName.ClusterDataCache.name());
    if (cache == null) {
        throw new StageException("Missing attributes in event:" + event + ". Requires DataCache");
    }
    Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());
    if (resourceMap == null) {
        throw new StageException("Resources must be computed prior to validation!");
    }
    Map<String, IdealState> idealStateMap = cache.getIdealStates();
    Map<String, Map<String, String>> idealStateRuleMap = cache.getIdealStateRules();
    for (String resourceName : idealStateMap.keySet()) {
        // check every ideal state against the ideal state rules
        // the pipeline should not process any resources that have an unsupported ideal state
        IdealState idealState = idealStateMap.get(resourceName);
        if (!idealStateRuleMap.isEmpty()) {
            boolean hasMatchingRule = false;
            for (String ruleName : idealStateRuleMap.keySet()) {
                Map<String, String> rule = idealStateRuleMap.get(ruleName);
                boolean matches = idealStateMatchesRule(idealState, rule);
                hasMatchingRule = hasMatchingRule || matches;
                if (matches) {
                    break;
                }
            }
            if (!hasMatchingRule) {
                LOG.warn("Resource " + resourceName + " does not have a valid ideal state!");
                resourceMap.remove(resourceName);
            }
        }
        // check that every resource to process has a live state model definition
        String stateModelDefRef = idealState.getStateModelDefRef();
        StateModelDefinition stateModelDef = cache.getStateModelDef(stateModelDefRef);
        if (stateModelDef == null) {
            LOG.warn("Resource " + resourceName + " uses state model " + stateModelDefRef + ", but it is not on the cluster!");
            resourceMap.remove(resourceName);
        }
    }
}
Also used : StateModelDefinition(org.apache.helix.model.StateModelDefinition) StageException(org.apache.helix.controller.pipeline.StageException) Resource(org.apache.helix.model.Resource) Map(java.util.Map) IdealState(org.apache.helix.model.IdealState)

Example 8 with Resource

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

the class TaskAssignmentStage method batchMessage.

List<Message> batchMessage(Builder keyBuilder, List<Message> messages, Map<String, Resource> resourceMap, Map<String, LiveInstance> liveInstanceMap, HelixManagerProperties properties) {
    // group messages by its CurrentState path + "/" + fromState + "/" + toState
    Map<String, Message> batchMessages = new HashMap<String, Message>();
    List<Message> outputMessages = new ArrayList<Message>();
    Iterator<Message> iter = messages.iterator();
    while (iter.hasNext()) {
        Message message = iter.next();
        String resourceName = message.getResourceName();
        Resource resource = resourceMap.get(resourceName);
        String instanceName = message.getTgtName();
        LiveInstance liveInstance = liveInstanceMap.get(instanceName);
        String participantVersion = null;
        if (liveInstance != null) {
            participantVersion = liveInstance.getHelixVersion();
        }
        if (resource == null || !resource.getBatchMessageMode() || participantVersion == null || !properties.isFeatureSupported("batch_message", participantVersion)) {
            outputMessages.add(message);
            continue;
        }
        String key = keyBuilder.currentState(message.getTgtName(), message.getTgtSessionId(), message.getResourceName()).getPath() + "/" + message.getFromState() + "/" + message.getToState();
        if (!batchMessages.containsKey(key)) {
            Message batchMessage = new Message(message.getRecord());
            batchMessage.setBatchMessageMode(true);
            outputMessages.add(batchMessage);
            batchMessages.put(key, batchMessage);
        }
        batchMessages.get(key).addPartitionName(message.getPartitionName());
    }
    return outputMessages;
}
Also used : Message(org.apache.helix.model.Message) LiveInstance(org.apache.helix.model.LiveInstance) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Resource(org.apache.helix.model.Resource)

Example 9 with Resource

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

the class TestJobStateOnCreation method beforeClass.

@BeforeClass
public void beforeClass() throws Exception {
    _cache = new ClusterDataCache();
    _idealState = new IdealState(WORKFLOW_NAME);
    _resource = new Resource(WORKFLOW_NAME);
    _currStateOutput = new CurrentStateOutput();
    _participants = new MockParticipantManager[_numNodes];
    String namespace = "/" + CLUSTER_NAME;
    if (_gZkClient.exists(namespace)) {
        _gZkClient.deleteRecursively(namespace);
    }
    _setupTool = new ClusterSetup(ZK_ADDR);
    _setupTool.addCluster(CLUSTER_NAME, true);
    createManagers();
}
Also used : ClusterDataCache(org.apache.helix.controller.stages.ClusterDataCache) Resource(org.apache.helix.model.Resource) ClusterSetup(org.apache.helix.tools.ClusterSetup) IdealState(org.apache.helix.model.IdealState) CurrentStateOutput(org.apache.helix.controller.stages.CurrentStateOutput) BeforeClass(org.testng.annotations.BeforeClass)

Example 10 with Resource

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

the class TestRebalancerMetrics method testLoadBalanceMetrics.

@Test
public void testLoadBalanceMetrics() {
    System.out.println("START testLoadBalanceMetrics at " + new Date(System.currentTimeMillis()));
    String resource = "testResourceName";
    int numPartition = 100;
    int numReplica = 3;
    int maxPending = 3;
    setupIdealState(5, new String[] { resource }, numPartition, numReplica, IdealState.RebalanceMode.FULL_AUTO, BuiltInStateModelDefinitions.MasterSlave.name());
    setupInstances(5);
    setupLiveInstances(4);
    setupStateModel();
    Map<String, Resource> resourceMap = getResourceMap(new String[] { resource }, numPartition, BuiltInStateModelDefinitions.MasterSlave.name());
    CurrentStateOutput currentStateOutput = new CurrentStateOutput();
    event.addAttribute(AttributeName.RESOURCES.name(), resourceMap);
    event.addAttribute(AttributeName.RESOURCES_TO_REBALANCE.name(), resourceMap);
    event.addAttribute(AttributeName.CURRENT_STATE.name(), currentStateOutput);
    ClusterStatusMonitor monitor = new ClusterStatusMonitor(_clusterName);
    monitor.active();
    event.addAttribute(AttributeName.clusterStatusMonitor.name(), monitor);
    runStage(event, new ReadClusterDataStage());
    runStage(event, new BestPossibleStateCalcStage());
    BestPossibleStateOutput bestPossibleStateOutput = event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.name());
    currentStateOutput = copyCurrentStateFromBestPossible(bestPossibleStateOutput, resource);
    event.addAttribute(AttributeName.CURRENT_STATE.name(), currentStateOutput);
    setupLiveInstances(4);
    runStage(event, new ReadClusterDataStage());
    ClusterDataCache cache = event.getAttribute(AttributeName.ClusterDataCache.name());
    setupThrottleConfig(cache.getClusterConfig(), StateTransitionThrottleConfig.RebalanceType.LOAD_BALANCE, maxPending);
    runStage(event, new BestPossibleStateCalcStage());
    runStage(event, new IntermediateStateCalcStage());
    ClusterStatusMonitor clusterStatusMonitor = event.getAttribute(AttributeName.clusterStatusMonitor.name());
    ResourceMonitor resourceMonitor = clusterStatusMonitor.getResourceMonitor(resource);
    long numPendingLoadBalance = resourceMonitor.getPendingLoadRebalancePartitionGauge();
    Assert.assertTrue(numPendingLoadBalance > 0);
    Assert.assertEquals(resourceMonitor.getLoadRebalanceThrottledPartitionGauge(), numPendingLoadBalance - maxPending);
    System.out.println("END testLoadBalanceMetrics at " + new Date(System.currentTimeMillis()));
}
Also used : BestPossibleStateOutput(org.apache.helix.controller.stages.BestPossibleStateOutput) ClusterDataCache(org.apache.helix.controller.stages.ClusterDataCache) ReadClusterDataStage(org.apache.helix.controller.stages.ReadClusterDataStage) IntermediateStateCalcStage(org.apache.helix.controller.stages.IntermediateStateCalcStage) Resource(org.apache.helix.model.Resource) Date(java.util.Date) CurrentStateOutput(org.apache.helix.controller.stages.CurrentStateOutput) BestPossibleStateCalcStage(org.apache.helix.controller.stages.BestPossibleStateCalcStage) Test(org.testng.annotations.Test) BaseStageTest(org.apache.helix.controller.stages.BaseStageTest)

Aggregations

Resource (org.apache.helix.model.Resource)35 Partition (org.apache.helix.model.Partition)16 Test (org.testng.annotations.Test)15 HashMap (java.util.HashMap)9 IdealState (org.apache.helix.model.IdealState)9 Message (org.apache.helix.model.Message)9 ArrayList (java.util.ArrayList)7 StageException (org.apache.helix.controller.pipeline.StageException)7 ClusterConfig (org.apache.helix.model.ClusterConfig)7 Date (java.util.Date)6 LiveInstance (org.apache.helix.model.LiveInstance)6 HelixDataAccessor (org.apache.helix.HelixDataAccessor)5 CurrentState (org.apache.helix.model.CurrentState)5 StateModelDefinition (org.apache.helix.model.StateModelDefinition)5 Map (java.util.Map)4 HelixManager (org.apache.helix.HelixManager)4 ZNRecord (org.apache.helix.ZNRecord)4 ClusterDataCache (org.apache.helix.controller.stages.ClusterDataCache)4 CurrentStateOutput (org.apache.helix.controller.stages.CurrentStateOutput)4 List (java.util.List)3