Search in sources :

Example 1 with HelixStateTransitionHandler

use of org.apache.helix.messaging.handling.HelixStateTransitionHandler 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)

Example 2 with HelixStateTransitionHandler

use of org.apache.helix.messaging.handling.HelixStateTransitionHandler 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 3 with HelixStateTransitionHandler

use of org.apache.helix.messaging.handling.HelixStateTransitionHandler 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 4 with HelixStateTransitionHandler

use of org.apache.helix.messaging.handling.HelixStateTransitionHandler in project helix by apache.

the class HelixStateMachineEngine method createHandler.

@Override
public MessageHandler createHandler(Message message, NotificationContext context) {
    String type = message.getMsgType();
    if (!type.equals(MessageType.STATE_TRANSITION.name()) && !type.equals(MessageType.STATE_TRANSITION_CANCELLATION.name())) {
        throw new HelixException("Expect state-transition message type, but was " + message.getMsgType() + ", msgId: " + message.getMsgId());
    }
    String partitionKey = message.getPartitionName();
    String stateModelName = message.getStateModelDef();
    String resourceName = message.getResourceName();
    String sessionId = message.getTgtSessionId();
    int bucketSize = message.getBucketSize();
    if (stateModelName == null) {
        logger.error("Fail to create msg-handler because message does not contain stateModelDef. msgId: " + message.getId());
        return null;
    }
    String factoryName = message.getStateModelFactoryName();
    if (factoryName == null) {
        factoryName = HelixConstants.DEFAULT_STATE_MODEL_FACTORY;
    }
    StateModelFactory<? extends StateModel> stateModelFactory = getStateModelFactory(stateModelName, factoryName);
    if (stateModelFactory == null) {
        logger.warn("Fail to create msg-handler because cannot find stateModelFactory for model: " + stateModelName + " using factoryName: " + factoryName + " for resource: " + resourceName);
        return null;
    }
    // check if the state model definition exists and cache it
    if (!_stateModelDefs.containsKey(stateModelName)) {
        HelixDataAccessor accessor = _manager.getHelixDataAccessor();
        Builder keyBuilder = accessor.keyBuilder();
        StateModelDefinition stateModelDef = accessor.getProperty(keyBuilder.stateModelDef(stateModelName));
        if (stateModelDef == null) {
            throw new HelixException("fail to create msg-handler because stateModelDef for " + stateModelName + " does NOT exist");
        }
        _stateModelDefs.put(stateModelName, stateModelDef);
    }
    if (message.getBatchMessageMode() == false) {
        String initState = _stateModelDefs.get(message.getStateModelDef()).getInitialState();
        StateModel stateModel = stateModelFactory.getStateModel(resourceName, partitionKey);
        if (stateModel == null) {
            stateModel = stateModelFactory.createAndAddStateModel(resourceName, partitionKey);
            stateModel.updateState(initState);
        }
        if (message.getMsgType().equals(MessageType.STATE_TRANSITION_CANCELLATION.name())) {
            return new HelixStateTransitionCancellationHandler(stateModel, message, context);
        } else {
            // create currentStateDelta for this partition
            // TODO: move currentStateDelta to StateTransitionMsgHandler
            CurrentState currentStateDelta = new CurrentState(resourceName);
            currentStateDelta.setSessionId(sessionId);
            currentStateDelta.setStateModelDefRef(stateModelName);
            currentStateDelta.setStateModelFactoryName(factoryName);
            currentStateDelta.setBucketSize(bucketSize);
            currentStateDelta.setState(partitionKey, (stateModel.getCurrentState() == null) ? initState : stateModel.getCurrentState());
            return new HelixStateTransitionHandler(stateModelFactory, stateModel, message, context, currentStateDelta);
        }
    } else {
        BatchMessageWrapper wrapper = stateModelFactory.getBatchMessageWrapper(resourceName);
        if (wrapper == null) {
            wrapper = stateModelFactory.createAndAddBatchMessageWrapper(resourceName);
        }
        // get executor-service for the message
        TaskExecutor executor = (TaskExecutor) context.get(MapKey.TASK_EXECUTOR.toString());
        if (executor == null) {
            logger.error("fail to get executor-service for batch message: " + message.getId() + ". msgType: " + message.getMsgType() + ", resource: " + message.getResourceName());
            return null;
        }
        return new BatchMessageHandler(message, context, this, wrapper, executor);
    }
}
Also used : HelixStateTransitionCancellationHandler(org.apache.helix.messaging.handling.HelixStateTransitionCancellationHandler) Builder(org.apache.helix.PropertyKey.Builder) BatchMessageWrapper(org.apache.helix.messaging.handling.BatchMessageWrapper) HelixException(org.apache.helix.HelixException) HelixDataAccessor(org.apache.helix.HelixDataAccessor) TaskExecutor(org.apache.helix.messaging.handling.TaskExecutor) StateModelDefinition(org.apache.helix.model.StateModelDefinition) CurrentState(org.apache.helix.model.CurrentState) StateModel(org.apache.helix.participant.statemachine.StateModel) HelixStateTransitionHandler(org.apache.helix.messaging.handling.HelixStateTransitionHandler) BatchMessageHandler(org.apache.helix.messaging.handling.BatchMessageHandler)

Aggregations

Builder (org.apache.helix.PropertyKey.Builder)4 HelixStateTransitionHandler (org.apache.helix.messaging.handling.HelixStateTransitionHandler)4 CurrentState (org.apache.helix.model.CurrentState)4 StateModelDefinition (org.apache.helix.model.StateModelDefinition)4 HelixTask (org.apache.helix.messaging.handling.HelixTask)3 MockManager (org.apache.helix.mock.MockManager)3 Message (org.apache.helix.model.Message)3 Test (org.testng.annotations.Test)3 Date (java.util.Date)2 HelixTaskExecutor (org.apache.helix.messaging.handling.HelixTaskExecutor)2 MockMasterSlaveStateModel (org.apache.helix.mock.statemodel.MockMasterSlaveStateModel)2 StateModelFactory (org.apache.helix.participant.statemachine.StateModelFactory)2 HelixDataAccessor (org.apache.helix.HelixDataAccessor)1 HelixException (org.apache.helix.HelixException)1 AsyncCallbackService (org.apache.helix.messaging.handling.AsyncCallbackService)1 BatchMessageHandler (org.apache.helix.messaging.handling.BatchMessageHandler)1 BatchMessageWrapper (org.apache.helix.messaging.handling.BatchMessageWrapper)1 HelixStateTransitionCancellationHandler (org.apache.helix.messaging.handling.HelixStateTransitionCancellationHandler)1 TaskExecutor (org.apache.helix.messaging.handling.TaskExecutor)1 MockHelixTaskExecutor (org.apache.helix.mock.participant.MockHelixTaskExecutor)1