Search in sources :

Example 76 with Message

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

the class TestHelixTaskHandler method testInvocation.

@Test()
public void testInvocation() throws Exception {
    HelixTaskExecutor executor = new HelixTaskExecutor();
    System.out.println("START TestCMTaskHandler.testInvocation()");
    Message message = new Message(MessageType.STATE_TRANSITION, "Some unique id");
    message.setSrcName("cm-instance-0");
    message.setTgtSessionId("1234");
    message.setFromState("Offline");
    message.setToState("Slave");
    message.setPartitionName("TestDB_0");
    message.setMsgId("Some unique message id");
    message.setResourceName("TestDB");
    message.setTgtName("localhost");
    message.setStateModelDef("MasterSlave");
    message.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY);
    MockMasterSlaveStateModel stateModel = new MockMasterSlaveStateModel();
    NotificationContext context;
    MockManager manager = new MockManager("clusterName");
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    StateModelDefinition stateModelDef = new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave());
    Builder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.stateModelDef("MasterSlave"), stateModelDef);
    context = new NotificationContext(manager);
    CurrentState currentStateDelta = new CurrentState("TestDB");
    currentStateDelta.setState("TestDB_0", "OFFLINE");
    HelixStateTransitionHandler stHandler = new HelixStateTransitionHandler(null, stateModel, message, context, currentStateDelta);
    HelixTask handler;
    handler = new HelixTask(message, context, stHandler, executor);
    handler.call();
    AssertJUnit.assertTrue(stateModel.stateModelInvoked);
    System.out.println("END TestCMTaskHandler.testInvocation() at " + new Date(System.currentTimeMillis()));
}
Also used : MockMasterSlaveStateModel(org.apache.helix.mock.statemodel.MockMasterSlaveStateModel) Message(org.apache.helix.model.Message) HelixTask(org.apache.helix.messaging.handling.HelixTask) StateModelDefinition(org.apache.helix.model.StateModelDefinition) HelixTaskExecutor(org.apache.helix.messaging.handling.HelixTaskExecutor) Builder(org.apache.helix.PropertyKey.Builder) CurrentState(org.apache.helix.model.CurrentState) MockManager(org.apache.helix.mock.MockManager) HelixStateTransitionHandler(org.apache.helix.messaging.handling.HelixStateTransitionHandler) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 77 with Message

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

the class TestHelixTaskHandler method testInvocationAnnotated.

@Test()
public void testInvocationAnnotated() throws Exception {
    System.out.println("START TestCMTaskHandler.testInvocationAnnotated() at " + new Date(System.currentTimeMillis()));
    HelixTaskExecutor executor = new HelixTaskExecutor();
    Message message = new Message(MessageType.STATE_TRANSITION, "Some unique id");
    message.setSrcName("cm-instance-0");
    message.setTgtSessionId("1234");
    message.setFromState("Offline");
    message.setToState("Slave");
    message.setPartitionName("TestDB_0");
    message.setMsgId("Some unique message id");
    message.setResourceName("TestDB");
    message.setTgtName("localhost");
    message.setStateModelDef("MasterSlave");
    message.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY);
    MockStateModelAnnotated stateModel = new MockStateModelAnnotated();
    NotificationContext context;
    MockManager manager = new MockManager("clusterName");
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    StateModelDefinition stateModelDef = new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave());
    Builder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.stateModelDef("MasterSlave"), stateModelDef);
    context = new NotificationContext(manager);
    CurrentState currentStateDelta = new CurrentState("TestDB");
    currentStateDelta.setState("TestDB_0", "OFFLINE");
    StateModelFactory<MockStateModelAnnotated> stateModelFactory = new StateModelFactory<MockStateModelAnnotated>() {

        @Override
        public MockStateModelAnnotated createNewStateModel(String resource, String partitionName) {
            // TODO Auto-generated method stub
            return new MockStateModelAnnotated();
        }
    };
    HelixStateTransitionHandler stHandler = new HelixStateTransitionHandler(stateModelFactory, stateModel, message, context, currentStateDelta);
    HelixTask handler = new HelixTask(message, context, stHandler, executor);
    handler.call();
    AssertJUnit.assertTrue(stateModel.stateModelInvoked);
    System.out.println("END TestCMTaskHandler.testInvocationAnnotated() at " + new Date(System.currentTimeMillis()));
}
Also used : Message(org.apache.helix.model.Message) HelixTask(org.apache.helix.messaging.handling.HelixTask) HelixTaskExecutor(org.apache.helix.messaging.handling.HelixTaskExecutor) Builder(org.apache.helix.PropertyKey.Builder) MockStateModelAnnotated(org.apache.helix.mock.statemodel.MockStateModelAnnotated) Date(java.util.Date) StateModelDefinition(org.apache.helix.model.StateModelDefinition) CurrentState(org.apache.helix.model.CurrentState) StateModelFactory(org.apache.helix.participant.statemachine.StateModelFactory) MockManager(org.apache.helix.mock.MockManager) HelixStateTransitionHandler(org.apache.helix.messaging.handling.HelixStateTransitionHandler) Test(org.testng.annotations.Test)

Example 78 with Message

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

the class TestHelper method createMessage.

public static Message createMessage(String msgId, String fromState, String toState, String tgtName, String resourceName, String partitionName) {
    Message msg = new Message(MessageType.STATE_TRANSITION, msgId);
    msg.setFromState(fromState);
    msg.setToState(toState);
    msg.setTgtName(tgtName);
    msg.setResourceName(resourceName);
    msg.setPartitionName(partitionName);
    msg.setStateModelDef("MasterSlave");
    return msg;
}
Also used : Message(org.apache.helix.model.Message)

Example 79 with Message

use of org.apache.helix.model.Message 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 80 with Message

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

the class TestMessageThrottleStage method testMsgThrottleBasic.

@Test
public void testMsgThrottleBasic() throws Exception {
    String clusterName = "CLUSTER_" + _className + "_basic";
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    HelixManager manager = new DummyClusterManager(clusterName, accessor);
    // ideal state: node0 is MASTER, node1 is SLAVE
    // replica=2 means 1 master and 1 slave
    setupIdealState(clusterName, new int[] { 0, 1 }, new String[] { "TestDB" }, 1, 2);
    setupLiveInstances(clusterName, new int[] { 0, 1 });
    setupStateModel(clusterName);
    ClusterEvent event = new ClusterEvent(ClusterEventType.Unknown);
    ClusterDataCache cache = new ClusterDataCache(clusterName);
    event.addAttribute(AttributeName.helixmanager.name(), manager);
    event.addAttribute(AttributeName.ClusterDataCache.name(), cache);
    MessageThrottleStage throttleStage = new MessageThrottleStage();
    try {
        runStage(event, throttleStage);
        Assert.fail("Should throw exception since DATA_CACHE is null");
    } catch (Exception e) {
    // OK
    }
    Pipeline dataRefresh = new Pipeline();
    dataRefresh.addStage(new ReadClusterDataStage());
    runPipeline(event, dataRefresh);
    try {
        runStage(event, throttleStage);
        Assert.fail("Should throw exception since RESOURCE is null");
    } catch (Exception e) {
    // OK
    }
    runStage(event, new ResourceComputationStage());
    try {
        runStage(event, throttleStage);
        Assert.fail("Should throw exception since MESSAGE_SELECT is null");
    } catch (Exception e) {
    // OK
    }
    MessageSelectionStageOutput msgSelectOutput = new MessageSelectionStageOutput();
    List<Message> selectMessages = new ArrayList<Message>();
    Message msg = createMessage(MessageType.STATE_TRANSITION, "msgId-001", "OFFLINE", "SLAVE", "TestDB", "localhost_0");
    selectMessages.add(msg);
    msgSelectOutput.addMessages("TestDB", new Partition("TestDB_0"), selectMessages);
    event.addAttribute(AttributeName.MESSAGES_SELECTED.name(), msgSelectOutput);
    runStage(event, throttleStage);
    MessageThrottleStageOutput msgThrottleOutput = event.getAttribute(AttributeName.MESSAGES_THROTTLE.name());
    Assert.assertEquals(msgThrottleOutput.getMessages("TestDB", new Partition("TestDB_0")).size(), 1);
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : Partition(org.apache.helix.model.Partition) HelixManager(org.apache.helix.HelixManager) Message(org.apache.helix.model.Message) ArrayList(java.util.ArrayList) Date(java.util.Date) Pipeline(org.apache.helix.controller.pipeline.Pipeline) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Aggregations

Message (org.apache.helix.model.Message)116 Test (org.testng.annotations.Test)53 ArrayList (java.util.ArrayList)36 HelixDataAccessor (org.apache.helix.HelixDataAccessor)30 Builder (org.apache.helix.PropertyKey.Builder)28 HelixManager (org.apache.helix.HelixManager)22 ZNRecord (org.apache.helix.ZNRecord)22 Criteria (org.apache.helix.Criteria)21 Date (java.util.Date)19 HashMap (java.util.HashMap)18 Partition (org.apache.helix.model.Partition)18 PropertyKey (org.apache.helix.PropertyKey)17 LiveInstance (org.apache.helix.model.LiveInstance)13 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)12 NotificationContext (org.apache.helix.NotificationContext)11 CurrentState (org.apache.helix.model.CurrentState)10 HelixException (org.apache.helix.HelixException)9 Resource (org.apache.helix.model.Resource)9 StringWriter (java.io.StringWriter)8 List (java.util.List)8