Search in sources :

Example 21 with Partition

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

the class StrictMatchExternalViewVerifier method computeIdealPartitionState.

private Map<String, Map<String, String>> computeIdealPartitionState(ClusterDataCache cache, IdealState idealState) {
    String stateModelDefName = idealState.getStateModelDefRef();
    StateModelDefinition stateModelDef = cache.getStateModelDef(stateModelDefName);
    Map<String, Map<String, String>> idealPartitionState = new HashMap<String, Map<String, String>>();
    Set<String> liveEnabledInstances = new HashSet<String>(cache.getLiveInstances().keySet());
    liveEnabledInstances.removeAll(cache.getDisabledInstances());
    for (String partition : idealState.getPartitionSet()) {
        List<String> preferenceList = AbstractRebalancer.getPreferenceList(new Partition(partition), idealState, liveEnabledInstances);
        Map<String, String> idealMapping = HelixUtil.computeIdealMapping(preferenceList, stateModelDef, liveEnabledInstances);
        idealPartitionState.put(partition, idealMapping);
    }
    return idealPartitionState;
}
Also used : Partition(org.apache.helix.model.Partition) HashMap(java.util.HashMap) StateModelDefinition(org.apache.helix.model.StateModelDefinition) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 22 with Partition

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

the class TestBestPossibleCalcStageCompatibility method testSemiAutoModeCompatibility.

@Test
public void testSemiAutoModeCompatibility() {
    System.out.println("START TestBestPossibleStateCalcStage at " + new Date(System.currentTimeMillis()));
    String[] resources = new String[] { "testResourceName" };
    setupIdealStateDeprecated(5, resources, 10, 1, IdealStateModeProperty.AUTO);
    setupLiveInstances(5);
    setupStateModel();
    Map<String, Resource> resourceMap = getResourceMap();
    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);
    ReadClusterDataStage stage1 = new ReadClusterDataStage();
    runStage(event, stage1);
    BestPossibleStateCalcStage stage2 = new BestPossibleStateCalcStage();
    runStage(event, stage2);
    BestPossibleStateOutput output = event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.name());
    for (int p = 0; p < 5; p++) {
        Partition resource = new Partition("testResourceName_" + p);
        AssertJUnit.assertEquals("MASTER", output.getInstanceStateMap("testResourceName", resource).get("localhost_" + (p + 1) % 5));
    }
    System.out.println("END TestBestPossibleStateCalcStage at " + new Date(System.currentTimeMillis()));
}
Also used : Partition(org.apache.helix.model.Partition) Resource(org.apache.helix.model.Resource) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 23 with Partition

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

the class TestBestPossibleCalcStageCompatibility method testCustomModeCompatibility.

@Test
public void testCustomModeCompatibility() {
    System.out.println("START TestBestPossibleStateCalcStage at " + new Date(System.currentTimeMillis()));
    String[] resources = new String[] { "testResourceName" };
    setupIdealStateDeprecated(5, resources, 10, 1, IdealStateModeProperty.CUSTOMIZED);
    setupLiveInstances(5);
    setupStateModel();
    Map<String, Resource> resourceMap = getResourceMap();
    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);
    ReadClusterDataStage stage1 = new ReadClusterDataStage();
    runStage(event, stage1);
    BestPossibleStateCalcStage stage2 = new BestPossibleStateCalcStage();
    runStage(event, stage2);
    BestPossibleStateOutput output = event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.name());
    for (int p = 0; p < 5; p++) {
        Partition resource = new Partition("testResourceName_" + p);
        AssertJUnit.assertNull(output.getInstanceStateMap("testResourceName", resource).get("localhost_" + (p + 1) % 5));
    }
    System.out.println("END TestBestPossibleStateCalcStage at " + new Date(System.currentTimeMillis()));
}
Also used : Partition(org.apache.helix.model.Partition) Resource(org.apache.helix.model.Resource) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 24 with Partition

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

the class TestBestPossibleStateCalcStage method testSimple.

@Test
public void testSimple() {
    System.out.println("START TestBestPossibleStateCalcStage at " + new Date(System.currentTimeMillis()));
    // List<IdealState> idealStates = new ArrayList<IdealState>();
    String[] resources = new String[] { "testResourceName" };
    int numPartition = 5;
    int numReplica = 1;
    setupIdealState(5, resources, numPartition, numReplica, RebalanceMode.SEMI_AUTO, BuiltInStateModelDefinitions.MasterSlave.name());
    setupLiveInstances(5);
    setupStateModel();
    Map<String, Resource> resourceMap = getResourceMap(resources, 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);
    ReadClusterDataStage stage1 = new ReadClusterDataStage();
    runStage(event, stage1);
    BestPossibleStateCalcStage stage2 = new BestPossibleStateCalcStage();
    runStage(event, stage2);
    BestPossibleStateOutput output = event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.name());
    for (int p = 0; p < 5; p++) {
        Partition resource = new Partition("testResourceName_" + p);
        AssertJUnit.assertEquals("MASTER", output.getInstanceStateMap("testResourceName", resource).get("localhost_" + (p + 1) % 5));
    }
    System.out.println("END TestBestPossibleStateCalcStage at " + new Date(System.currentTimeMillis()));
}
Also used : Partition(org.apache.helix.model.Partition) Resource(org.apache.helix.model.Resource) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 25 with Partition

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

the class TestCurrentStateComputationStage method testSimpleCS.

@Test
public void testSimpleCS() {
    // setup resource
    Map<String, Resource> resourceMap = getResourceMap();
    setupLiveInstances(5);
    event.addAttribute(AttributeName.RESOURCES.name(), resourceMap);
    event.addAttribute(AttributeName.RESOURCES_TO_REBALANCE.name(), resourceMap);
    CurrentStateComputationStage stage = new CurrentStateComputationStage();
    runStage(event, new ReadClusterDataStage());
    runStage(event, stage);
    CurrentStateOutput output1 = event.getAttribute(AttributeName.CURRENT_STATE.name());
    AssertJUnit.assertEquals(output1.getCurrentStateMap("testResourceName", new Partition("testResourceName_0")).size(), 0);
    // Add a state transition messages
    Message message = new Message(Message.MessageType.STATE_TRANSITION, "msg1");
    message.setFromState("OFFLINE");
    message.setToState("SLAVE");
    message.setResourceName("testResourceName");
    message.setPartitionName("testResourceName_1");
    message.setTgtName("localhost_3");
    message.setTgtSessionId("session_3");
    Builder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.message("localhost_" + 3, message.getId()), message);
    runStage(event, new ReadClusterDataStage());
    runStage(event, stage);
    CurrentStateOutput output2 = event.getAttribute(AttributeName.CURRENT_STATE.name());
    String pendingState = output2.getPendingState("testResourceName", new Partition("testResourceName_1"), "localhost_3").getToState();
    AssertJUnit.assertEquals(pendingState, "SLAVE");
    ZNRecord record1 = new ZNRecord("testResourceName");
    // Add a current state that matches sessionId and one that does not match
    CurrentState stateWithLiveSession = new CurrentState(record1);
    stateWithLiveSession.setSessionId("session_3");
    stateWithLiveSession.setStateModelDefRef("MasterSlave");
    stateWithLiveSession.setState("testResourceName_1", "OFFLINE");
    ZNRecord record2 = new ZNRecord("testResourceName");
    CurrentState stateWithDeadSession = new CurrentState(record2);
    stateWithDeadSession.setSessionId("session_dead");
    stateWithDeadSession.setStateModelDefRef("MasterSlave");
    stateWithDeadSession.setState("testResourceName_1", "MASTER");
    accessor.setProperty(keyBuilder.currentState("localhost_3", "session_3", "testResourceName"), stateWithLiveSession);
    accessor.setProperty(keyBuilder.currentState("localhost_3", "session_dead", "testResourceName"), stateWithDeadSession);
    runStage(event, new ReadClusterDataStage());
    runStage(event, stage);
    CurrentStateOutput output3 = event.getAttribute(AttributeName.CURRENT_STATE.name());
    String currentState = output3.getCurrentState("testResourceName", new Partition("testResourceName_1"), "localhost_3");
    AssertJUnit.assertEquals(currentState, "OFFLINE");
}
Also used : Partition(org.apache.helix.model.Partition) Message(org.apache.helix.model.Message) Builder(org.apache.helix.PropertyKey.Builder) CurrentState(org.apache.helix.model.CurrentState) Resource(org.apache.helix.model.Resource) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

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