Search in sources :

Example 36 with Partition

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

the class TestStateTransitionPrirority method testResourceLevelPriorityForLoadBalance.

@Test(dataProvider = "ResourceLevelPriority")
public void testResourceLevelPriorityForLoadBalance(Map<String, String> resourceMap, String priorityField, List<String> expectedPriority) {
    preSetup(StateTransitionThrottleConfig.RebalanceType.LOAD_BALANCE, resourceMap.keySet(), priorityField, 10, 1);
    event.addAttribute(AttributeName.RESOURCES.name(), getResourceMap(resourceMap.keySet().toArray(new String[resourceMap.keySet().size()]), 1, "MasterSlave"));
    event.addAttribute(AttributeName.RESOURCES_TO_REBALANCE.name(), getResourceMap(resourceMap.keySet().toArray(new String[resourceMap.keySet().size()]), 1, "MasterSlave"));
    // Initialize bestpossible state and current state
    BestPossibleStateOutput bestPossibleStateOutput = new BestPossibleStateOutput();
    CurrentStateOutput currentStateOutput = new CurrentStateOutput();
    for (String resource : resourceMap.keySet()) {
        IdealState is = accessor.getProperty(accessor.keyBuilder().idealStates(resource));
        is.getRecord().setSimpleField(priorityField, resourceMap.get(resource));
        setSingleIdealState(is);
        Map<String, List<String>> partitionMap = new HashMap<String, List<String>>();
        Partition partition = new Partition(resource + "_0");
        String instanceName = HOSTNAME_PREFIX + resource.split("_")[1];
        String nextInstanceName = HOSTNAME_PREFIX + (Integer.parseInt(resource.split("_")[1]) + 1);
        partitionMap.put(partition.getPartitionName(), Collections.singletonList(nextInstanceName));
        bestPossibleStateOutput.setPreferenceLists(resource, partitionMap);
        bestPossibleStateOutput.setState(resource, partition, nextInstanceName, "MASTER");
        currentStateOutput.setCurrentState(resource, partition, instanceName, "MASTER");
    }
    event.addAttribute(AttributeName.BEST_POSSIBLE_STATE.name(), bestPossibleStateOutput);
    event.addAttribute(AttributeName.CURRENT_STATE.name(), currentStateOutput);
    runStage(event, new ReadClusterDataStage());
    // Keep update the current state.
    List<String> resourcePriority = new ArrayList<String>();
    for (int i = 0; i < resourceMap.size(); i++) {
        runStage(event, new IntermediateStateCalcStage());
        updateCurrentStatesForLoadBalance(resourcePriority, currentStateOutput);
    }
    Assert.assertEquals(resourcePriority, expectedPriority);
}
Also used : Partition(org.apache.helix.model.Partition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IdealState(org.apache.helix.model.IdealState) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.testng.annotations.Test)

Example 37 with Partition

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

the class TestPersistAssignmentStage method testSimple.

/**
 * Case where we have one resource in IdealState
 * @throws Exception
 */
@Test
public void testSimple() throws Exception {
    int nodes = 2;
    List<String> instances = new ArrayList<String>();
    for (int i = 0; i < nodes; i++) {
        instances.add("localhost_" + i);
    }
    int partitions = 10;
    int replicas = 1;
    String resourceName = "testResource";
    ZNRecord record = DefaultIdealStateCalculator.calculateIdealState(instances, partitions, replicas, resourceName, "ONLINE", "OFFLINE");
    IdealState idealState = new IdealState(record);
    idealState.setStateModelDefRef("OnlineOffline");
    // Read and load current state into event
    HelixDataAccessor accessor = _manager.getHelixDataAccessor();
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);
    runStage(_manager, event, new ReadClusterDataStage());
    runStage(_manager, event, new ResourceComputationStage());
    // Ensure persist best possible assignment is true
    ClusterConfig clusterConfig = new ClusterConfig(CLUSTER_NAME);
    clusterConfig.setPersistBestPossibleAssignment(true);
    ClusterDataCache cache = event.getAttribute(AttributeName.ClusterDataCache.name());
    cache.setClusterConfig(clusterConfig);
    // 1. Change best possible state (simulate a new rebalancer run)
    BestPossibleStateOutput bestPossibleStateOutput = new BestPossibleStateOutput();
    for (String partition : idealState.getPartitionSet()) {
        bestPossibleStateOutput.setState(resourceName, new Partition(partition), "localhost_3", "OFFLINE");
    }
    // 2. At the same time, set DelayRebalanceEnabled = true (simulate a Admin operation at the same time)
    idealState.setDelayRebalanceEnabled(true);
    accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);
    // Persist new assignment
    PersistAssignmentStage stage = new PersistAssignmentStage();
    event.addAttribute(AttributeName.BEST_POSSIBLE_STATE.name(), bestPossibleStateOutput);
    runStage(_manager, event, stage);
    IdealState newIdealState = accessor.getProperty(keyBuilder.idealStates(resourceName));
    // 1. New assignment should be set
    Assert.assertEquals(newIdealState.getPartitionSet().size(), idealState.getPartitionSet().size());
    for (String partition : idealState.getPartitionSet()) {
        Map<String, String> assignment = newIdealState.getInstanceStateMap(partition);
        Assert.assertNotNull(assignment);
        Assert.assertEquals(assignment.size(), 1);
        Assert.assertTrue(assignment.containsKey("localhost_3") && assignment.get("localhost_3").equals("OFFLINE"));
    }
    // 2. Admin config should be set
    Assert.assertTrue(newIdealState.isDelayRebalanceEnabled());
}
Also used : Partition(org.apache.helix.model.Partition) PersistAssignmentStage(org.apache.helix.controller.stages.PersistAssignmentStage) BestPossibleStateOutput(org.apache.helix.controller.stages.BestPossibleStateOutput) ResourceComputationStage(org.apache.helix.controller.stages.ResourceComputationStage) ClusterDataCache(org.apache.helix.controller.stages.ClusterDataCache) ReadClusterDataStage(org.apache.helix.controller.stages.ReadClusterDataStage) ArrayList(java.util.ArrayList) IdealState(org.apache.helix.model.IdealState) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) ClusterConfig(org.apache.helix.model.ClusterConfig) Test(org.testng.annotations.Test)

Example 38 with Partition

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

the class DeprecatedTaskRebalancer method emptyAssignment.

private static ResourceAssignment emptyAssignment(String name, CurrentStateOutput currStateOutput) {
    ResourceAssignment assignment = new ResourceAssignment(name);
    Set<Partition> partitions = currStateOutput.getCurrentStateMappedPartitions(name);
    for (Partition partition : partitions) {
        Map<String, String> currentStateMap = currStateOutput.getCurrentStateMap(name, partition);
        Map<String, String> replicaMap = Maps.newHashMap();
        for (String instanceName : currentStateMap.keySet()) {
            replicaMap.put(instanceName, HelixDefinedState.DROPPED.toString());
        }
        assignment.addReplicaMap(partition, replicaMap);
    }
    return assignment;
}
Also used : Partition(org.apache.helix.model.Partition) ResourceAssignment(org.apache.helix.model.ResourceAssignment)

Example 39 with Partition

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

the class JobRebalancer method processTaskWithPendingMessage.

private void processTaskWithPendingMessage(ResourceAssignment prevAssignment, Integer pId, String pName, String instance, Message pendingMessage, TaskState jobState, TaskPartitionState currState, Map<Integer, PartitionAssignment> paMap, Set<Integer> assignedPartitions) {
    Map<String, String> stateMap = prevAssignment.getReplicaMap(new Partition(pName));
    if (stateMap != null) {
        String prevState = stateMap.get(instance);
        if (!pendingMessage.getToState().equals(prevState)) {
            LOG.warn(String.format("Task pending to-state is %s while previous assigned state is %s. This should not" + "heppen.", pendingMessage.getToState(), prevState));
        }
        if (jobState == TaskState.TIMING_OUT && currState == TaskPartitionState.INIT && prevState.equals(TaskPartitionState.RUNNING.name())) {
            // While job is timing out, if the task is pending on INIT->RUNNING, set it back to INIT,
            // so that Helix will cancel the transition.
            paMap.put(pId, new PartitionAssignment(instance, TaskPartitionState.INIT.name()));
            assignedPartitions.add(pId);
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("Task partition %s has a pending state transition on instance %s INIT->RUNNING. " + "Setting it back to INIT so that Helix can cancel the transition(if enabled).", pName, instance, prevState));
            }
        } else {
            // Otherwise, Just copy forward
            // the state assignment from the previous ideal state.
            paMap.put(pId, new PartitionAssignment(instance, prevState));
            assignedPartitions.add(pId);
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("Task partition %s has a pending state transition on instance %s. Using the previous ideal state which was %s.", pName, instance, prevState));
            }
        }
    }
}
Also used : Partition(org.apache.helix.model.Partition)

Example 40 with Partition

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

the class JobRebalancer method isJobFinished.

private boolean isJobFinished(JobContext jobContext, String jobResource, CurrentStateOutput currentStateOutput) {
    for (int pId : jobContext.getPartitionSet()) {
        TaskPartitionState state = jobContext.getPartitionState(pId);
        Partition partition = new Partition(pName(jobResource, pId));
        String instance = jobContext.getAssignedParticipant(pId);
        Message pendingMessage = currentStateOutput.getPendingState(jobResource, partition, instance);
        // If state is INIT but is pending INIT->RUNNING, it's not yet safe to say the job finished
        if (state == TaskPartitionState.RUNNING || (state == TaskPartitionState.INIT && pendingMessage != null)) {
            return false;
        }
    }
    return true;
}
Also used : Partition(org.apache.helix.model.Partition) Message(org.apache.helix.model.Message)

Aggregations

Partition (org.apache.helix.model.Partition)50 Message (org.apache.helix.model.Message)18 Test (org.testng.annotations.Test)17 HashMap (java.util.HashMap)16 Resource (org.apache.helix.model.Resource)16 Map (java.util.Map)12 ArrayList (java.util.ArrayList)10 Date (java.util.Date)10 HelixDataAccessor (org.apache.helix.HelixDataAccessor)9 HelixManager (org.apache.helix.HelixManager)9 ZNRecord (org.apache.helix.ZNRecord)9 IdealState (org.apache.helix.model.IdealState)9 StateModelDefinition (org.apache.helix.model.StateModelDefinition)9 ResourceAssignment (org.apache.helix.model.ResourceAssignment)8 PartitionStateMap (org.apache.helix.controller.common.PartitionStateMap)7 Pipeline (org.apache.helix.controller.pipeline.Pipeline)7 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)6 HashSet (java.util.HashSet)5 List (java.util.List)5 BestPossibleStateOutput (org.apache.helix.controller.stages.BestPossibleStateOutput)5