Search in sources :

Example 21 with Message

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

the class TestHelixTaskExecutor method testCreateHandlerException.

@Test()
public void testCreateHandlerException() throws InterruptedException {
    System.out.println("START TestCMTaskExecutor.testCreateHandlerException()");
    HelixTaskExecutor executor = new HelixTaskExecutor();
    HelixManager manager = new MockClusterManager();
    TestMessageHandlerFactory factory = new TestMessageHandlerFactory();
    for (String type : factory.getMessageTypes()) {
        executor.registerMessageHandlerFactory(type, factory);
    }
    NotificationContext changeContext = new NotificationContext(manager);
    List<Message> msgList = new ArrayList<Message>();
    int nMsgs1 = 5;
    for (int i = 0; i < nMsgs1; i++) {
        Message msg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString());
        msg.setTgtSessionId(manager.getSessionId());
        msg.setTgtName("Localhost_1123");
        msg.setSrcName("127.101.1.23_2234");
        msg.setCorrelationId(UUID.randomUUID().toString());
        msgList.add(msg);
    }
    Message exceptionMsg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString());
    exceptionMsg.setTgtSessionId(manager.getSessionId());
    exceptionMsg.setMsgSubType("EXCEPTION");
    exceptionMsg.setTgtName("Localhost_1123");
    exceptionMsg.setSrcName("127.101.1.23_2234");
    exceptionMsg.setCorrelationId(UUID.randomUUID().toString());
    msgList.add(exceptionMsg);
    changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE);
    executor.onMessage("someInstance", msgList, changeContext);
    Thread.sleep(1000);
    AssertJUnit.assertTrue(factory._processedMsgIds.size() == nMsgs1);
    AssertJUnit.assertTrue(factory._handlersCreated == nMsgs1);
    AssertJUnit.assertTrue(exceptionMsg.getMsgState() == MessageState.UNPROCESSABLE);
    System.out.println("END TestCMTaskExecutor.testCreateHandlerException()");
}
Also used : Message(org.apache.helix.model.Message) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 22 with Message

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

the class TestDefaultControllerMsgHandlerFactory method testDefaultControllerMsgHandlerFactory.

@Test()
public void testDefaultControllerMsgHandlerFactory() {
    System.out.println("START TestDefaultControllerMsgHandlerFactory at " + new Date(System.currentTimeMillis()));
    DefaultControllerMessageHandlerFactory facotry = new DefaultControllerMessageHandlerFactory();
    Message message = new Message(MessageType.NO_OP, "0");
    NotificationContext context = new NotificationContext(null);
    boolean exceptionCaught = false;
    try {
        MessageHandler handler = facotry.createHandler(message, context);
    } catch (HelixException e) {
        exceptionCaught = true;
    }
    AssertJUnit.assertTrue(exceptionCaught);
    message = new Message(MessageType.CONTROLLER_MSG, "1");
    exceptionCaught = false;
    try {
        MessageHandler handler = facotry.createHandler(message, context);
    } catch (HelixException e) {
        exceptionCaught = true;
    }
    AssertJUnit.assertFalse(exceptionCaught);
    Map<String, String> resultMap = new HashMap<String, String>();
    message = new Message(MessageType.NO_OP, "3");
    DefaultControllerMessageHandler defaultHandler = new DefaultControllerMessageHandler(message, context);
    try {
        defaultHandler.handleMessage();
    } catch (HelixException e) {
        exceptionCaught = true;
    } catch (InterruptedException e) {
        LOG.error("Interrupted handling message", e);
    }
    AssertJUnit.assertTrue(exceptionCaught);
    message = new Message(MessageType.CONTROLLER_MSG, "4");
    defaultHandler = new DefaultControllerMessageHandler(message, context);
    exceptionCaught = false;
    try {
        defaultHandler.handleMessage();
    } catch (HelixException e) {
        exceptionCaught = true;
    } catch (InterruptedException e) {
        LOG.error("Interrupted handling message", e);
    }
    AssertJUnit.assertFalse(exceptionCaught);
    System.out.println("END TestDefaultControllerMsgHandlerFactory at " + new Date(System.currentTimeMillis()));
}
Also used : NotificationContext(org.apache.helix.NotificationContext) HelixException(org.apache.helix.HelixException) Message(org.apache.helix.model.Message) DefaultControllerMessageHandler(org.apache.helix.manager.zk.DefaultControllerMessageHandlerFactory.DefaultControllerMessageHandler) MessageHandler(org.apache.helix.messaging.handling.MessageHandler) HashMap(java.util.HashMap) DefaultControllerMessageHandlerFactory(org.apache.helix.manager.zk.DefaultControllerMessageHandlerFactory) DefaultControllerMessageHandler(org.apache.helix.manager.zk.DefaultControllerMessageHandlerFactory.DefaultControllerMessageHandler) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 23 with Message

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

the class MessagePoster method postTestMessage.

public void postTestMessage(String zkServer, String clusterName, String instanceName) {
    String msgSrc = "cm-instance-0";
    String msgId = "TestMessageId-2";
    Message message = new Message(MessageType.STATE_TRANSITION, msgId);
    message.setMsgId(msgId);
    message.setSrcName(msgSrc);
    message.setTgtName(instanceName);
    message.setMsgState(MessageState.NEW);
    message.setFromState("Slave");
    message.setToState("Master");
    message.setPartitionName("EspressoDB.partition-0." + instanceName);
    post(zkServer, message, clusterName, instanceName);
}
Also used : Message(org.apache.helix.model.Message)

Example 24 with Message

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

the class MessagePoster method postFaultInjectionMessage.

public void postFaultInjectionMessage(String zkServer, String clusterName, String instanceName, String payloadString, String partition) {
    Message message = new Message("FaultInjection", UUID.randomUUID().toString());
    if (payloadString != null) {
        message.getRecord().setSimpleField("faultType", payloadString);
    }
    if (partition != null) {
        message.setPartitionName(partition);
    }
    post(zkServer, message, clusterName, instanceName);
}
Also used : Message(org.apache.helix.model.Message)

Example 25 with Message

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

the class TestHelixTaskExecutor method testCMTaskExecutor.

@Test()
public void testCMTaskExecutor() throws Exception {
    System.out.println("START TestCMTaskExecutor");
    String msgId = "TestMessageId";
    Message message = new Message(MessageType.TASK_REPLY, msgId);
    message.setMsgId(msgId);
    message.setSrcName("cm-instance-0");
    message.setTgtName("cm-instance-1");
    message.setTgtSessionId("1234");
    message.setFromState("Offline");
    message.setToState("Slave");
    message.setPartitionName("TestDB_0");
    message.setResourceName("TestDB");
    message.setStateModelDef("MasterSlave");
    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);
    MockHelixTaskExecutor executor = new MockHelixTaskExecutor();
    MockMasterSlaveStateModel stateModel = new MockMasterSlaveStateModel();
    executor.registerMessageHandlerFactory(MessageType.TASK_REPLY.name(), new AsyncCallbackService());
    NotificationContext context = new NotificationContext(manager);
    CurrentState currentStateDelta = new CurrentState("TestDB");
    currentStateDelta.setState("TestDB_0", "OFFLINE");
    StateModelFactory<MockMasterSlaveStateModel> stateModelFactory = new StateModelFactory<MockMasterSlaveStateModel>() {

        @Override
        public MockMasterSlaveStateModel createNewStateModel(String resource, String partitionName) {
            // TODO Auto-generated method stub
            return new MockMasterSlaveStateModel();
        }
    };
    HelixStateTransitionHandler handler = new HelixStateTransitionHandler(stateModelFactory, stateModel, message, context, currentStateDelta);
    HelixTask task = new HelixTask(message, context, handler, executor);
    executor.scheduleTask(task);
    for (int i = 0; i < 10; i++) {
        if (!executor.isDone(task.getTaskId())) {
            Thread.sleep(500);
        }
    }
    AssertJUnit.assertTrue(stateModel.stateModelInvoked);
    System.out.println("END TestCMTaskExecutor");
}
Also used : Message(org.apache.helix.model.Message) HelixTask(org.apache.helix.messaging.handling.HelixTask) Builder(org.apache.helix.PropertyKey.Builder) AsyncCallbackService(org.apache.helix.messaging.handling.AsyncCallbackService) MockHelixTaskExecutor(org.apache.helix.mock.participant.MockHelixTaskExecutor) MockMasterSlaveStateModel(org.apache.helix.mock.statemodel.MockMasterSlaveStateModel) 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)

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