Search in sources :

Example 6 with NotificationContext

use of org.apache.helix.NotificationContext in project helix by apache.

the class TestDistControllerStateModel method testReset.

@Test()
public void testReset() {
    Message message = new Message(MessageType.STATE_TRANSITION, "0");
    message.setPartitionName(clusterName);
    message.setTgtName("controller_0");
    try {
        stateModel.onBecomeLeaderFromStandby(message, new NotificationContext(null));
    } catch (Exception e) {
        LOG.error("Exception becoming leader from standby", e);
    }
    stateModel.reset();
}
Also used : NotificationContext(org.apache.helix.NotificationContext) Message(org.apache.helix.model.Message) Test(org.testng.annotations.Test)

Example 7 with NotificationContext

use of org.apache.helix.NotificationContext 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 8 with NotificationContext

use of org.apache.helix.NotificationContext in project helix by apache.

the class RoutingTableProvider method shutdown.

/**
 * Shutdown current RoutingTableProvider. Once it is shutdown, it should never be reused.
 */
public void shutdown() {
    _routerUpdater.shutdown();
    if (_helixManager != null) {
        PropertyKey.Builder keyBuilder = _helixManager.getHelixDataAccessor().keyBuilder();
        switch(_sourceDataType) {
            case EXTERNALVIEW:
                _helixManager.removeListener(keyBuilder.externalViews(), this);
                break;
            case TARGETEXTERNALVIEW:
                _helixManager.removeListener(keyBuilder.targetExternalViews(), this);
                break;
            case CURRENTSTATES:
                NotificationContext context = new NotificationContext(_helixManager);
                context.setType(NotificationContext.Type.FINALIZE);
                updateCurrentStatesListeners(Collections.<LiveInstance>emptyList(), context);
                break;
            default:
                break;
        }
    }
}
Also used : NotificationContext(org.apache.helix.NotificationContext) PropertyKey(org.apache.helix.PropertyKey)

Example 9 with NotificationContext

use of org.apache.helix.NotificationContext in project helix by apache.

the class TestZKLiveInstanceData method testDataChange.

@Test
public void testDataChange() throws Exception {
    // Create an admin and add LiveInstanceChange listener to it
    HelixManager adminManager = HelixManagerFactory.getZKHelixManager(clusterName, null, InstanceType.ADMINISTRATOR, ZK_ADDR);
    adminManager.connect();
    final BlockingQueue<List<LiveInstance>> changeList = new LinkedBlockingQueue<List<LiveInstance>>();
    adminManager.addLiveInstanceChangeListener(new LiveInstanceChangeListener() {

        @Override
        public void onLiveInstanceChange(List<LiveInstance> liveInstances, NotificationContext changeContext) {
            // The queue is basically unbounded, so shouldn't throw exception when calling
            // "add".
            changeList.add(deepCopy(liveInstances));
        }
    });
    // Check the initial condition
    List<LiveInstance> instances = changeList.poll(1, TimeUnit.SECONDS);
    Assert.assertNotNull(instances, "Expecting a list of live instance");
    Assert.assertTrue(instances.isEmpty(), "Expecting an empty list of live instance");
    // Join as participant, should trigger a live instance change event
    HelixManager manager = HelixManagerFactory.getZKHelixManager(clusterName, "localhost_54321", InstanceType.PARTICIPANT, ZK_ADDR);
    manager.connect();
    instances = changeList.poll(1, TimeUnit.SECONDS);
    Assert.assertNotNull(instances, "Expecting a list of live instance");
    Assert.assertEquals(instances.size(), 1, "Expecting one live instance");
    Assert.assertEquals(instances.get(0).getInstanceName(), manager.getInstanceName());
    // Update data in the live instance node, should trigger another live instance change
    // event
    HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor();
    PropertyKey propertyKey = helixDataAccessor.keyBuilder().liveInstance(manager.getInstanceName());
    LiveInstance instance = helixDataAccessor.getProperty(propertyKey);
    Map<String, String> map = new TreeMap<String, String>();
    map.put("k1", "v1");
    instance.getRecord().setMapField("test", map);
    Assert.assertTrue(helixDataAccessor.updateProperty(propertyKey, instance), "Failed to update live instance node");
    instances = changeList.poll(1, TimeUnit.SECONDS);
    Assert.assertNotNull(instances, "Expecting a list of live instance");
    Assert.assertEquals(instances.get(0).getRecord().getMapField("test"), map, "Wrong map data.");
    manager.disconnect();
    // wait for callback finish
    Thread.sleep(1000);
    instances = changeList.poll(1, TimeUnit.SECONDS);
    Assert.assertNotNull(instances, "Expecting a list of live instance");
    Assert.assertTrue(instances.isEmpty(), "Expecting an empty list of live instance");
    adminManager.disconnect();
}
Also used : HelixManager(org.apache.helix.HelixManager) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TreeMap(java.util.TreeMap) NotificationContext(org.apache.helix.NotificationContext) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) LiveInstanceChangeListener(org.apache.helix.LiveInstanceChangeListener) ArrayList(java.util.ArrayList) List(java.util.List) PropertyKey(org.apache.helix.PropertyKey) Test(org.testng.annotations.Test)

Example 10 with NotificationContext

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

Aggregations

NotificationContext (org.apache.helix.NotificationContext)35 Message (org.apache.helix.model.Message)11 Test (org.testng.annotations.Test)11 HelixManager (org.apache.helix.HelixManager)10 HelixException (org.apache.helix.HelixException)9 Date (java.util.Date)6 ArrayList (java.util.ArrayList)4 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)4 ControllerChangeListener (org.apache.helix.ControllerChangeListener)4 LiveInstance (org.apache.helix.model.LiveInstance)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Set (java.util.Set)3 HelixTimerTask (org.apache.helix.HelixTimerTask)3 PropertyKey (org.apache.helix.PropertyKey)3 GenericHelixController (org.apache.helix.controller.GenericHelixController)3 DistributedLeaderElection (org.apache.helix.manager.zk.DistributedLeaderElection)3 MessageHandler (org.apache.helix.messaging.handling.MessageHandler)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ResultSet (java.sql.ResultSet)2