Search in sources :

Example 1 with ResourceComputationStage

use of org.apache.helix.controller.stages.ResourceComputationStage in project helix by apache.

the class ClusterExternalViewVerifier method calculateBestPossibleState.

BestPossibleStateOutput calculateBestPossibleState(ClusterDataCache cache) throws Exception {
    ClusterEvent event = new ClusterEvent(ClusterEventType.StateVerifier);
    event.addAttribute(AttributeName.ClusterDataCache.name(), cache);
    List<Stage> stages = new ArrayList<Stage>();
    stages.add(new ResourceComputationStage());
    stages.add(new CurrentStateComputationStage());
    stages.add(new BestPossibleStateCalcStage());
    for (Stage stage : stages) {
        runStage(event, stage);
    }
    return event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.name());
}
Also used : ClusterEvent(org.apache.helix.controller.stages.ClusterEvent) ResourceComputationStage(org.apache.helix.controller.stages.ResourceComputationStage) BestPossibleStateCalcStage(org.apache.helix.controller.stages.BestPossibleStateCalcStage) ArrayList(java.util.ArrayList) BestPossibleStateCalcStage(org.apache.helix.controller.stages.BestPossibleStateCalcStage) Stage(org.apache.helix.controller.pipeline.Stage) ResourceComputationStage(org.apache.helix.controller.stages.ResourceComputationStage) CurrentStateComputationStage(org.apache.helix.controller.stages.CurrentStateComputationStage) CurrentStateComputationStage(org.apache.helix.controller.stages.CurrentStateComputationStage)

Example 2 with ResourceComputationStage

use of org.apache.helix.controller.stages.ResourceComputationStage in project helix by apache.

the class TestSkipBestPossibleCalculation method test.

@Test()
public void test() throws Exception {
    int numResource = 5;
    for (int i = 0; i < numResource; i++) {
        String dbName = "TestDB_" + i;
        _setupTool.addResourceToCluster(CLUSTER_NAME, dbName, _PARTITIONS, STATE_MODEL, IdealState.RebalanceMode.CUSTOMIZED.name());
        _setupTool.rebalanceResource(CLUSTER_NAME, dbName, 3);
    }
    ClusterDataCache cache = new ClusterDataCache("CLUSTER_" + TestHelper.getTestClassName());
    cache.setTaskCache(false);
    cache.refresh(_manager.getHelixDataAccessor());
    ClusterEvent event = new ClusterEvent(CLUSTER_NAME, ClusterEventType.IdealStateChange);
    event.addAttribute(AttributeName.ClusterDataCache.name(), cache);
    runStage(_manager, event, new ResourceComputationStage());
    runStage(_manager, event, new CurrentStateComputationStage());
    Assert.assertEquals(cache.getCachedResourceAssignments().size(), 0);
    runStage(_manager, event, new BestPossibleStateCalcStage());
    Assert.assertEquals(cache.getCachedResourceAssignments().size(), numResource);
    cache.notifyDataChange(HelixConstants.ChangeType.INSTANCE_CONFIG);
    cache.refresh(_manager.getHelixDataAccessor());
    Assert.assertEquals(cache.getCachedResourceAssignments().size(), 0);
    runStage(_manager, event, new BestPossibleStateCalcStage());
    Assert.assertEquals(cache.getCachedResourceAssignments().size(), numResource);
    cache.notifyDataChange(HelixConstants.ChangeType.IDEAL_STATE);
    cache.refresh(_manager.getHelixDataAccessor());
    Assert.assertEquals(cache.getCachedResourceAssignments().size(), 0);
    runStage(_manager, event, new BestPossibleStateCalcStage());
    Assert.assertEquals(cache.getCachedResourceAssignments().size(), numResource);
    cache.notifyDataChange(HelixConstants.ChangeType.LIVE_INSTANCE);
    cache.refresh(_manager.getHelixDataAccessor());
    Assert.assertEquals(cache.getCachedResourceAssignments().size(), 0);
    runStage(_manager, event, new BestPossibleStateCalcStage());
    Assert.assertEquals(cache.getCachedResourceAssignments().size(), numResource);
    cache.requireFullRefresh();
    cache.refresh(_manager.getHelixDataAccessor());
    Assert.assertEquals(cache.getCachedResourceAssignments().size(), 0);
    runStage(_manager, event, new BestPossibleStateCalcStage());
    Assert.assertEquals(cache.getCachedResourceAssignments().size(), numResource);
    cache.notifyDataChange(HelixConstants.ChangeType.CURRENT_STATE);
    cache.refresh(_manager.getHelixDataAccessor());
    Assert.assertEquals(cache.getCachedResourceAssignments().size(), numResource);
    cache.notifyDataChange(HelixConstants.ChangeType.RESOURCE_CONFIG);
    cache.refresh(_manager.getHelixDataAccessor());
    Assert.assertEquals(cache.getCachedResourceAssignments().size(), 0);
}
Also used : ClusterEvent(org.apache.helix.controller.stages.ClusterEvent) ClusterDataCache(org.apache.helix.controller.stages.ClusterDataCache) ResourceComputationStage(org.apache.helix.controller.stages.ResourceComputationStage) BestPossibleStateCalcStage(org.apache.helix.controller.stages.BestPossibleStateCalcStage) CurrentStateComputationStage(org.apache.helix.controller.stages.CurrentStateComputationStage) Test(org.testng.annotations.Test)

Example 3 with ResourceComputationStage

use of org.apache.helix.controller.stages.ResourceComputationStage in project helix by apache.

the class TaskTestUtil method calculateBestPossibleState.

public static BestPossibleStateOutput calculateBestPossibleState(ClusterDataCache cache, HelixManager manager) throws Exception {
    ClusterEvent event = new ClusterEvent(ClusterEventType.Unknown);
    event.addAttribute(AttributeName.ClusterDataCache.name(), cache);
    event.addAttribute(AttributeName.helixmanager.name(), manager);
    List<Stage> stages = new ArrayList<Stage>();
    stages.add(new ReadClusterDataStage());
    stages.add(new ResourceComputationStage());
    stages.add(new CurrentStateComputationStage());
    stages.add(new BestPossibleStateCalcStage());
    for (Stage stage : stages) {
        runStage(event, stage);
    }
    return event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.name());
}
Also used : ClusterEvent(org.apache.helix.controller.stages.ClusterEvent) ResourceComputationStage(org.apache.helix.controller.stages.ResourceComputationStage) ReadClusterDataStage(org.apache.helix.controller.stages.ReadClusterDataStage) BestPossibleStateCalcStage(org.apache.helix.controller.stages.BestPossibleStateCalcStage) ArrayList(java.util.ArrayList) BestPossibleStateCalcStage(org.apache.helix.controller.stages.BestPossibleStateCalcStage) Stage(org.apache.helix.controller.pipeline.Stage) ReadClusterDataStage(org.apache.helix.controller.stages.ReadClusterDataStage) ResourceComputationStage(org.apache.helix.controller.stages.ResourceComputationStage) CurrentStateComputationStage(org.apache.helix.controller.stages.CurrentStateComputationStage) CurrentStateComputationStage(org.apache.helix.controller.stages.CurrentStateComputationStage)

Example 4 with ResourceComputationStage

use of org.apache.helix.controller.stages.ResourceComputationStage in project helix by apache.

the class BestPossibleExternalViewVerifier method calcBestPossState.

/**
 * calculate the best possible state note that DROPPED states are not checked since when
 * kick off the BestPossibleStateCalcStage we are providing an empty current state map
 *
 * @param cache
 * @return
 * @throws Exception
 */
private BestPossibleStateOutput calcBestPossState(ClusterDataCache cache) throws Exception {
    ClusterEvent event = new ClusterEvent(ClusterEventType.StateVerifier);
    event.addAttribute(AttributeName.ClusterDataCache.name(), cache);
    runStage(event, new ResourceComputationStage());
    runStage(event, new CurrentStateComputationStage());
    // TODO: be caution here, should be handled statelessly.
    runStage(event, new BestPossibleStateCalcStage());
    BestPossibleStateOutput output = event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.name());
    return output;
}
Also used : ClusterEvent(org.apache.helix.controller.stages.ClusterEvent) BestPossibleStateOutput(org.apache.helix.controller.stages.BestPossibleStateOutput) ResourceComputationStage(org.apache.helix.controller.stages.ResourceComputationStage) BestPossibleStateCalcStage(org.apache.helix.controller.stages.BestPossibleStateCalcStage) CurrentStateComputationStage(org.apache.helix.controller.stages.CurrentStateComputationStage)

Example 5 with ResourceComputationStage

use of org.apache.helix.controller.stages.ResourceComputationStage 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)

Aggregations

ResourceComputationStage (org.apache.helix.controller.stages.ResourceComputationStage)5 BestPossibleStateCalcStage (org.apache.helix.controller.stages.BestPossibleStateCalcStage)4 ClusterEvent (org.apache.helix.controller.stages.ClusterEvent)4 CurrentStateComputationStage (org.apache.helix.controller.stages.CurrentStateComputationStage)4 ArrayList (java.util.ArrayList)3 Stage (org.apache.helix.controller.pipeline.Stage)2 BestPossibleStateOutput (org.apache.helix.controller.stages.BestPossibleStateOutput)2 ClusterDataCache (org.apache.helix.controller.stages.ClusterDataCache)2 ReadClusterDataStage (org.apache.helix.controller.stages.ReadClusterDataStage)2 Test (org.testng.annotations.Test)2 HelixDataAccessor (org.apache.helix.HelixDataAccessor)1 PropertyKey (org.apache.helix.PropertyKey)1 ZNRecord (org.apache.helix.ZNRecord)1 PersistAssignmentStage (org.apache.helix.controller.stages.PersistAssignmentStage)1 ClusterConfig (org.apache.helix.model.ClusterConfig)1 IdealState (org.apache.helix.model.IdealState)1 Partition (org.apache.helix.model.Partition)1