Search in sources :

Example 11 with Resource

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

the class BaseStageTest method getResourceMap.

protected Map<String, Resource> getResourceMap(String[] resources, int partitions, String stateModel, ClusterConfig clusterConfig, ResourceConfig resourceConfig) {
    Map<String, Resource> resourceMap = new HashMap<String, Resource>();
    for (String r : resources) {
        Resource testResource = new Resource(r, clusterConfig, resourceConfig);
        testResource.setStateModelDefRef(stateModel);
        for (int i = 0; i < partitions; i++) {
            testResource.addPartition(r + "_" + i);
        }
        resourceMap.put(r, testResource);
    }
    return resourceMap;
}
Also used : HashMap(java.util.HashMap) Resource(org.apache.helix.model.Resource)

Example 12 with Resource

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

the class BaseStageTest method getResourceMap.

protected Map<String, Resource> getResourceMap() {
    Map<String, Resource> resourceMap = new HashMap<String, Resource>();
    Resource testResource = new Resource("testResourceName");
    testResource.setStateModelDefRef("MasterSlave");
    testResource.addPartition("testResourceName_0");
    testResource.addPartition("testResourceName_1");
    testResource.addPartition("testResourceName_2");
    testResource.addPartition("testResourceName_3");
    testResource.addPartition("testResourceName_4");
    resourceMap.put("testResourceName", testResource);
    return resourceMap;
}
Also used : HashMap(java.util.HashMap) Resource(org.apache.helix.model.Resource)

Example 13 with Resource

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

the class TestResourceComputationStage method testSimple.

/**
 * Case where we have one resource in IdealState
 * @throws Exception
 */
@Test
public void testSimple() throws Exception {
    int nodes = 5;
    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, "MASTER", "SLAVE");
    IdealState idealState = new IdealState(record);
    idealState.setStateModelDefRef("MasterSlave");
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    Builder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);
    ResourceComputationStage stage = new ResourceComputationStage();
    runStage(event, new ReadClusterDataStage());
    runStage(event, stage);
    Map<String, Resource> resource = event.getAttribute(AttributeName.RESOURCES_TO_REBALANCE.name());
    AssertJUnit.assertEquals(1, resource.size());
    AssertJUnit.assertEquals(resource.keySet().iterator().next(), resourceName);
    AssertJUnit.assertEquals(resource.values().iterator().next().getResourceName(), resourceName);
    AssertJUnit.assertEquals(resource.values().iterator().next().getStateModelDefRef(), idealState.getStateModelDefRef());
    AssertJUnit.assertEquals(resource.values().iterator().next().getPartitions().size(), partitions);
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList) Resource(org.apache.helix.model.Resource) ZNRecord(org.apache.helix.ZNRecord) IdealState(org.apache.helix.model.IdealState) Test(org.testng.annotations.Test)

Example 14 with Resource

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

the class TestResourceComputationStage method testMultipleResources.

@Test
public void testMultipleResources() throws Exception {
    // List<IdealState> idealStates = new ArrayList<IdealState>();
    String[] resources = new String[] { "testResource1", "testResource2" };
    List<IdealState> idealStates = setupIdealState(5, resources, 10, 1, RebalanceMode.SEMI_AUTO);
    ResourceComputationStage stage = new ResourceComputationStage();
    runStage(event, new ReadClusterDataStage());
    runStage(event, stage);
    Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES_TO_REBALANCE.name());
    AssertJUnit.assertEquals(resources.length, resourceMap.size());
    for (int i = 0; i < resources.length; i++) {
        String resourceName = resources[i];
        IdealState idealState = idealStates.get(i);
        AssertJUnit.assertTrue(resourceMap.containsKey(resourceName));
        AssertJUnit.assertEquals(resourceMap.get(resourceName).getResourceName(), resourceName);
        AssertJUnit.assertEquals(resourceMap.get(resourceName).getStateModelDefRef(), idealState.getStateModelDefRef());
        AssertJUnit.assertEquals(resourceMap.get(resourceName).getPartitions().size(), idealState.getNumPartitions());
    }
}
Also used : Resource(org.apache.helix.model.Resource) IdealState(org.apache.helix.model.IdealState) Test(org.testng.annotations.Test)

Example 15 with Resource

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

the class TestResourceValidationStage method testMissingStateModel.

@Test
public void testMissingStateModel() throws Exception {
    MockAccessor accessor = new MockAccessor();
    accessor.setProperty(accessor.keyBuilder().clusterConfig(), new ClusterConfig("TestCluster"));
    // create an ideal state and no spec
    String masterSlaveCustomResource = "masterSlaveCustomResource";
    String leaderStandbyCustomResource = "leaderStandbyCustomResource";
    createIS(accessor, masterSlaveCustomResource, "MasterSlave", RebalanceMode.CUSTOMIZED);
    createIS(accessor, leaderStandbyCustomResource, "LeaderStandby", RebalanceMode.CUSTOMIZED);
    // add some state models (but not leader standby)
    addStateModels(accessor);
    // refresh the cache
    ClusterEvent event = new ClusterEvent(ClusterEventType.Unknown);
    ClusterDataCache cache = new ClusterDataCache();
    cache.refresh(accessor);
    event.addAttribute(AttributeName.ClusterDataCache.name(), cache);
    // run resource computation
    new ResourceComputationStage().process(event);
    Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());
    Assert.assertTrue(resourceMap.containsKey(masterSlaveCustomResource));
    Assert.assertTrue(resourceMap.containsKey(leaderStandbyCustomResource));
    // run resource validation
    new ResourceValidationStage().process(event);
    Map<String, Resource> finalResourceMap = event.getAttribute(AttributeName.RESOURCES.name());
    Assert.assertTrue(finalResourceMap.containsKey(masterSlaveCustomResource));
    Assert.assertFalse(finalResourceMap.containsKey(leaderStandbyCustomResource));
}
Also used : MockAccessor(org.apache.helix.MockAccessor) Resource(org.apache.helix.model.Resource) ClusterConfig(org.apache.helix.model.ClusterConfig) Test(org.testng.annotations.Test)

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