Search in sources :

Example 21 with Resource

use of org.apache.helix.model.Resource 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)

Example 22 with Resource

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

the class TestCurrentStateComputationStage method testEmptyCS.

@Test
public void testEmptyCS() {
    Map<String, Resource> resourceMap = getResourceMap();
    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 output = event.getAttribute(AttributeName.CURRENT_STATE.name());
    AssertJUnit.assertEquals(output.getCurrentStateMap("testResourceName", new Partition("testResourceName_0")).size(), 0);
}
Also used : Partition(org.apache.helix.model.Partition) Resource(org.apache.helix.model.Resource) Test(org.testng.annotations.Test)

Example 23 with Resource

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

the class TestResourceComputationStage method testMultipleResourcesWithSomeDropped.

@Test
public void testMultipleResourcesWithSomeDropped() throws Exception {
    int nodes = 5;
    List<String> instances = new ArrayList<String>();
    for (int i = 0; i < nodes; i++) {
        instances.add("localhost_" + i);
    }
    String[] resources = new String[] { "testResource1", "testResource2" };
    List<IdealState> idealStates = new ArrayList<IdealState>();
    for (int i = 0; i < resources.length; i++) {
        int partitions = 10;
        int replicas = 1;
        String resourceName = resources[i];
        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);
        idealStates.add(idealState);
    }
    // ADD A LIVE INSTANCE WITH A CURRENT STATE THAT CONTAINS RESOURCE WHICH NO
    // LONGER EXISTS IN IDEALSTATE
    String instanceName = "localhost_" + 3;
    LiveInstance liveInstance = new LiveInstance(instanceName);
    String sessionId = UUID.randomUUID().toString();
    liveInstance.setSessionId(sessionId);
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    Builder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.liveInstance(instanceName), liveInstance);
    String oldResource = "testResourceOld";
    CurrentState currentState = new CurrentState(oldResource);
    currentState.setState("testResourceOld_0", "OFFLINE");
    currentState.setState("testResourceOld_1", "SLAVE");
    currentState.setState("testResourceOld_2", "MASTER");
    currentState.setStateModelDefRef("MasterSlave");
    accessor.setProperty(keyBuilder.currentState(instanceName, sessionId, oldResource), currentState);
    ResourceComputationStage stage = new ResourceComputationStage();
    runStage(event, new ReadClusterDataStage());
    runStage(event, stage);
    Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());
    // +1 because it will have one for current state
    AssertJUnit.assertEquals(resources.length + 1, 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());
    }
    // Test the data derived from CurrentState
    AssertJUnit.assertTrue(resourceMap.containsKey(oldResource));
    AssertJUnit.assertEquals(resourceMap.get(oldResource).getResourceName(), oldResource);
    AssertJUnit.assertEquals(resourceMap.get(oldResource).getStateModelDefRef(), currentState.getStateModelDefRef());
    AssertJUnit.assertEquals(resourceMap.get(oldResource).getPartitions().size(), currentState.getPartitionStateMap().size());
    AssertJUnit.assertNotNull(resourceMap.get(oldResource).getPartition("testResourceOld_0"));
    AssertJUnit.assertNotNull(resourceMap.get(oldResource).getPartition("testResourceOld_1"));
    AssertJUnit.assertNotNull(resourceMap.get(oldResource).getPartition("testResourceOld_2"));
}
Also used : Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList) Resource(org.apache.helix.model.Resource) IdealState(org.apache.helix.model.IdealState) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) CurrentState(org.apache.helix.model.CurrentState) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 24 with Resource

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

the class TestResourceValidationStage method testIdealStateValidity.

@Test
public void testIdealStateValidity() throws Exception {
    MockAccessor accessor = new MockAccessor();
    accessor.setProperty(accessor.keyBuilder().clusterConfig(), new ClusterConfig("TestCluster"));
    // create some ideal states
    String masterSlaveCustomResource = "masterSlaveCustomResource";
    String onlineOfflineFullAutoResource = "onlineOfflineFullAutoResource";
    String masterSlaveSemiAutoInvalidResource = "masterSlaveSemiAutoInvalidResource";
    createIS(accessor, masterSlaveCustomResource, "MasterSlave", RebalanceMode.CUSTOMIZED);
    createIS(accessor, onlineOfflineFullAutoResource, "OnlineOffline", RebalanceMode.FULL_AUTO);
    createIS(accessor, masterSlaveSemiAutoInvalidResource, "MasterSlave", RebalanceMode.SEMI_AUTO);
    // create some ideal state specs
    createISSpec(accessor, masterSlaveCustomResource + "_spec", "MasterSlave", RebalanceMode.CUSTOMIZED);
    createISSpec(accessor, onlineOfflineFullAutoResource + "_spec", "OnlineOffline", RebalanceMode.FULL_AUTO);
    // add some state models
    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_TO_REBALANCE.name());
    Assert.assertTrue(resourceMap.containsKey(masterSlaveCustomResource));
    Assert.assertTrue(resourceMap.containsKey(onlineOfflineFullAutoResource));
    Assert.assertTrue(resourceMap.containsKey(masterSlaveSemiAutoInvalidResource));
    // run resource validation
    new ResourceValidationStage().process(event);
    Map<String, Resource> finalResourceMap = event.getAttribute(AttributeName.RESOURCES.name());
    Assert.assertTrue(finalResourceMap.containsKey(masterSlaveCustomResource));
    Assert.assertTrue(finalResourceMap.containsKey(onlineOfflineFullAutoResource));
    Assert.assertFalse(finalResourceMap.containsKey(masterSlaveSemiAutoInvalidResource));
}
Also used : MockAccessor(org.apache.helix.MockAccessor) Resource(org.apache.helix.model.Resource) ClusterConfig(org.apache.helix.model.ClusterConfig) Test(org.testng.annotations.Test)

Example 25 with Resource

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

the class TestStateTransitionPrirority method testPartitionLevelPriority.

@Test(dataProvider = "PartitionLevelPriority")
public void testPartitionLevelPriority(String resourceName, Map<String, Map<String, String>> bestPossibleMap, Map<String, Map<String, String>> currentStateMap, List<String> preferenceList, List<String> expectedPriority) {
    preSetup(StateTransitionThrottleConfig.RebalanceType.RECOVERY_BALANCE, new HashSet<String>(Arrays.asList(resourceName)), "no_field", 3, 3);
    // Add load rebalance throttle config
    ClusterConfig clusterConfig = accessor.getProperty(accessor.keyBuilder().clusterConfig());
    StateTransitionThrottleConfig throttleConfigForLoadRebalance = new StateTransitionThrottleConfig(StateTransitionThrottleConfig.RebalanceType.LOAD_BALANCE, StateTransitionThrottleConfig.ThrottleScope.CLUSTER, 1);
    List<StateTransitionThrottleConfig> currentThrottleConfig = clusterConfig.getStateTransitionThrottleConfigs();
    currentThrottleConfig.add(throttleConfigForLoadRebalance);
    clusterConfig.setStateTransitionThrottleConfigs(currentThrottleConfig);
    setClusterConfig(clusterConfig);
    // Initialize best possible state, current state and resource map.
    Resource resource = new Resource(resourceName);
    BestPossibleStateOutput bestPossibleStateOutput = new BestPossibleStateOutput();
    CurrentStateOutput currentStateOutput = new CurrentStateOutput();
    for (String partitionName : bestPossibleMap.keySet()) {
        Partition partition = new Partition(partitionName);
        bestPossibleStateOutput.setPreferenceList(resourceName, partitionName, preferenceList);
        for (String instanceName : bestPossibleMap.get(partitionName).keySet()) {
            bestPossibleStateOutput.setState(resourceName, partition, instanceName, bestPossibleMap.get(partitionName).get(instanceName));
            currentStateOutput.setCurrentState(resourceName, partition, instanceName, currentStateMap.get(partitionName).get(instanceName));
        }
        resource.addPartition(partitionName);
    }
    resource.setStateModelDefRef("MasterSlave");
    event.addAttribute(AttributeName.RESOURCES.name(), Collections.singletonMap(resourceName, resource));
    event.addAttribute(AttributeName.RESOURCES_TO_REBALANCE.name(), resource);
    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> partitionPriority = new ArrayList<String>();
    for (int i = 0; i < bestPossibleMap.size(); i++) {
        runStage(event, new IntermediateStateCalcStage());
        updateCurrentStateForPartitionLevelPriority(partitionPriority, currentStateOutput, resourceName, bestPossibleMap);
    }
    Assert.assertEquals(partitionPriority, expectedPriority);
}
Also used : Partition(org.apache.helix.model.Partition) Resource(org.apache.helix.model.Resource) ArrayList(java.util.ArrayList) StateTransitionThrottleConfig(org.apache.helix.api.config.StateTransitionThrottleConfig) 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