Search in sources :

Example 16 with Message

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

the class ScheduledTaskStateModel method onBecomeCompletedFromOffline.

@Transition(to = "COMPLETED", from = "OFFLINE")
public void onBecomeCompletedFromOffline(Message message, NotificationContext context) throws InterruptedException {
    logger.info(_partitionKey + " onBecomeCompletedFromOffline");
    // Construct the inner task message from the mapfields of scheduledTaskQueue resource group
    Map<String, String> messageInfo = message.getRecord().getMapField(Message.Attributes.INNER_MESSAGE.toString());
    ZNRecord record = new ZNRecord(_partitionKey);
    record.getSimpleFields().putAll(messageInfo);
    Message taskMessage = new Message(record);
    if (logger.isDebugEnabled()) {
        logger.debug(taskMessage.getRecord().getSimpleFields().toString());
    }
    MessageHandler handler = _executor.createMessageHandler(taskMessage, new NotificationContext(null));
    if (handler == null) {
        throw new HelixException("Task message " + taskMessage.getMsgType() + " handler not found, task id " + _partitionKey);
    }
    // Invoke the internal handler to complete the task
    handler.handleMessage();
    logger.info(_partitionKey + " onBecomeCompletedFromOffline completed");
}
Also used : NotificationContext(org.apache.helix.NotificationContext) HelixException(org.apache.helix.HelixException) Message(org.apache.helix.model.Message) MessageHandler(org.apache.helix.messaging.handling.MessageHandler) ZNRecord(org.apache.helix.ZNRecord)

Example 17 with Message

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

the class TestDefaultMessagingService method TestMessageSend.

@Test()
public void TestMessageSend() {
    HelixManager manager = new MockHelixManager();
    DefaultMessagingService svc = new DefaultMessagingService(manager);
    TestMessageHandlerFactory factory = new TestMessageHandlerFactory();
    svc.registerMessageHandlerFactory(factory.getMessageType(), factory);
    Criteria recipientCriteria = new Criteria();
    recipientCriteria.setInstanceName("localhost_12919");
    recipientCriteria.setRecipientInstanceType(InstanceType.PARTICIPANT);
    recipientCriteria.setSelfExcluded(true);
    Message template = new Message(factory.getMessageType(), UUID.randomUUID().toString());
    AssertJUnit.assertEquals(0, svc.send(recipientCriteria, template));
    recipientCriteria.setSelfExcluded(false);
    AssertJUnit.assertEquals(1, svc.send(recipientCriteria, template));
    // all instances, all partitions
    recipientCriteria.setSelfExcluded(false);
    recipientCriteria.setInstanceName("%");
    recipientCriteria.setResource("DB");
    recipientCriteria.setPartition("%");
    AssertJUnit.assertEquals(200, svc.send(recipientCriteria, template));
    // all instances, all partitions, use * instead of %
    recipientCriteria.setSelfExcluded(false);
    recipientCriteria.setInstanceName("*");
    recipientCriteria.setResource("DB");
    recipientCriteria.setPartition("*");
    AssertJUnit.assertEquals(200, svc.send(recipientCriteria, template));
    // tail pattern
    recipientCriteria.setSelfExcluded(false);
    recipientCriteria.setInstanceName("localhost%");
    recipientCriteria.setResource("DB");
    recipientCriteria.setPartition("%");
    AssertJUnit.assertEquals(200, svc.send(recipientCriteria, template));
    // exclude this instance, send to all others for all partitions
    recipientCriteria.setSelfExcluded(true);
    recipientCriteria.setInstanceName("%");
    recipientCriteria.setResource("DB");
    recipientCriteria.setPartition("%");
    AssertJUnit.assertEquals(159, svc.send(recipientCriteria, template));
    // single instance, all partitions
    recipientCriteria.setSelfExcluded(true);
    recipientCriteria.setInstanceName("localhost_12920");
    recipientCriteria.setResource("DB");
    recipientCriteria.setPartition("%");
    AssertJUnit.assertEquals(39, svc.send(recipientCriteria, template));
    // single character wildcards
    recipientCriteria.setSelfExcluded(true);
    recipientCriteria.setInstanceName("l_calhost_12_20");
    recipientCriteria.setResource("DB");
    recipientCriteria.setPartition("%");
    AssertJUnit.assertEquals(39, svc.send(recipientCriteria, template));
    // head pattern
    recipientCriteria.setSelfExcluded(true);
    recipientCriteria.setInstanceName("%12920");
    recipientCriteria.setResource("DB");
    recipientCriteria.setPartition("%");
    AssertJUnit.assertEquals(39, svc.send(recipientCriteria, template));
    // middle pattern
    recipientCriteria.setSelfExcluded(true);
    recipientCriteria.setInstanceName("l%_12920");
    recipientCriteria.setResource("DB");
    recipientCriteria.setPartition("%");
    AssertJUnit.assertEquals(39, svc.send(recipientCriteria, template));
    // send to a controller
    recipientCriteria.setSelfExcluded(true);
    recipientCriteria.setInstanceName("localhost_12920");
    recipientCriteria.setRecipientInstanceType(InstanceType.CONTROLLER);
    recipientCriteria.setResource("DB");
    recipientCriteria.setPartition("%");
    AssertJUnit.assertEquals(1, svc.send(recipientCriteria, template));
}
Also used : HelixManager(org.apache.helix.HelixManager) Message(org.apache.helix.model.Message) Criteria(org.apache.helix.Criteria) Test(org.testng.annotations.Test)

Example 18 with Message

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

the class TestHelixTaskExecutor method testMsgSessionId.

@Test()
public void testMsgSessionId() throws InterruptedException {
    HelixTaskExecutor executor = new HelixTaskExecutor();
    HelixManager manager = new MockClusterManager();
    TestMessageHandlerFactory factory = new TestMessageHandlerFactory();
    for (String type : factory.getMessageTypes()) {
        executor.registerMessageHandlerFactory(type, factory);
    }
    TestMessageHandlerFactory2 factory2 = new TestMessageHandlerFactory2();
    for (String type : factory2.getMessageTypes()) {
        executor.registerMessageHandlerFactory(type, factory2);
    }
    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("*");
        msg.setTgtName("");
        msgList.add(msg);
    }
    int nMsgs2 = 4;
    for (int i = 0; i < nMsgs2; i++) {
        Message msg = new Message(factory2.getMessageTypes().get(0), UUID.randomUUID().toString());
        msg.setTgtSessionId("some other session id");
        msg.setTgtName("");
        msgList.add(msg);
    }
    changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE);
    executor.onMessage("someInstance", msgList, changeContext);
    Thread.sleep(1000);
    AssertJUnit.assertTrue(factory._processedMsgIds.size() == nMsgs1);
    AssertJUnit.assertTrue(factory2._processedMsgIds.size() == 0);
    AssertJUnit.assertTrue(factory._handlersCreated == nMsgs1);
    AssertJUnit.assertTrue(factory2._handlersCreated == 0);
    for (Message message : msgList) {
        if (factory.getMessageTypes().contains(message.getMsgType())) {
            AssertJUnit.assertTrue(factory._processedMsgIds.containsKey(message.getId()));
        }
    }
}
Also used : Message(org.apache.helix.model.Message) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 19 with Message

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

the class TestHelixTaskExecutor method testShutdown.

@Test()
public void testShutdown() throws InterruptedException {
    System.out.println("START TestCMTaskExecutor.testShutdown()");
    HelixTaskExecutor executor = new HelixTaskExecutor();
    HelixManager manager = new MockClusterManager();
    TestMessageHandlerFactory factory = new TestMessageHandlerFactory();
    for (String type : factory.getMessageTypes()) {
        executor.registerMessageHandlerFactory(type, factory);
    }
    TestMessageHandlerFactory2 factory2 = new TestMessageHandlerFactory2();
    for (String type : factory2.getMessageTypes()) {
        executor.registerMessageHandlerFactory(type, factory2);
    }
    CancellableHandlerFactory factory3 = new CancellableHandlerFactory();
    for (String type : factory3.getMessageTypes()) {
        executor.registerMessageHandlerFactory(type, factory3);
    }
    int nMsg1 = 10, nMsg2 = 10, nMsg3 = 10;
    List<Message> msgList = new ArrayList<Message>();
    for (int i = 0; i < nMsg1; i++) {
        Message msg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString());
        msg.setTgtSessionId("*");
        msg.setTgtName("Localhost_1123");
        msg.setSrcName("127.101.1.23_2234");
        msgList.add(msg);
    }
    for (int i = 0; i < nMsg2; i++) {
        Message msg = new Message(factory2.getMessageTypes().get(0), UUID.randomUUID().toString());
        msg.setTgtSessionId("*");
        msgList.add(msg);
        msg.setTgtName("Localhost_1123");
        msg.setSrcName("127.101.1.23_2234");
        msgList.add(msg);
    }
    for (int i = 0; i < nMsg3; i++) {
        Message msg = new Message(factory3.getMessageTypes().get(0), UUID.randomUUID().toString());
        msg.setTgtSessionId("*");
        msgList.add(msg);
        msg.setTgtName("Localhost_1123");
        msg.setSrcName("127.101.1.23_2234");
        msgList.add(msg);
    }
    NotificationContext changeContext = new NotificationContext(manager);
    changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE);
    executor.onMessage("some", msgList, changeContext);
    Thread.sleep(500);
    for (ExecutorService svc : executor._executorMap.values()) {
        Assert.assertFalse(svc.isShutdown());
    }
    Assert.assertTrue(factory._processedMsgIds.size() > 0);
    executor.shutdown();
    for (ExecutorService svc : executor._executorMap.values()) {
        Assert.assertTrue(svc.isShutdown());
    }
    System.out.println("END TestCMTaskExecutor.testShutdown()");
}
Also used : Message(org.apache.helix.model.Message) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Test(org.testng.annotations.Test)

Example 20 with Message

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

the class TestHelixTaskExecutor method testUnknownTypeMsgExecution.

@Test()
public void testUnknownTypeMsgExecution() throws InterruptedException {
    HelixTaskExecutor executor = new HelixTaskExecutor();
    HelixManager manager = new MockClusterManager();
    TestMessageHandlerFactory factory = new TestMessageHandlerFactory();
    for (String type : factory.getMessageTypes()) {
        executor.registerMessageHandlerFactory(type, factory);
    }
    TestMessageHandlerFactory2 factory2 = new TestMessageHandlerFactory2();
    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");
        msgList.add(msg);
    }
    int nMsgs2 = 4;
    for (int i = 0; i < nMsgs2; i++) {
        Message msg = new Message(factory2.getMessageTypes().get(0), UUID.randomUUID().toString());
        msg.setTgtSessionId(manager.getSessionId());
        msg.setTgtName("Localhost_1123");
        msg.setSrcName("127.101.1.23_2234");
        msgList.add(msg);
    }
    changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE);
    executor.onMessage("someInstance", msgList, changeContext);
    Thread.sleep(1000);
    AssertJUnit.assertTrue(factory._processedMsgIds.size() == nMsgs1);
    AssertJUnit.assertTrue(factory2._processedMsgIds.size() == 0);
    AssertJUnit.assertTrue(factory._handlersCreated == nMsgs1);
    AssertJUnit.assertTrue(factory2._handlersCreated == 0);
    for (Message message : msgList) {
        if (factory.getMessageTypes().contains(message.getMsgType())) {
            AssertJUnit.assertTrue(factory._processedMsgIds.containsKey(message.getId()));
        }
    }
}
Also used : Message(org.apache.helix.model.Message) ArrayList(java.util.ArrayList) 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