Search in sources :

Example 41 with Message

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

the class HelixBatchMessageTask method getTaskId.

@Override
public String getTaskId() {
    StringBuilder sb = new StringBuilder();
    sb.append(_batchMsg.getId());
    sb.append("/");
    List<String> msgIdList = new ArrayList<String>();
    if (_subMsgs != null) {
        for (Message msg : _subMsgs) {
            msgIdList.add(msg.getId());
        }
    }
    sb.append(msgIdList);
    return sb.toString();
}
Also used : Message(org.apache.helix.model.Message) ArrayList(java.util.ArrayList)

Example 42 with Message

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

the class CallbackHandler method invoke.

public void invoke(NotificationContext changeContext) throws Exception {
    Type type = changeContext.getType();
    long start = System.currentTimeMillis();
    // This allows the listener to work with one change at a time
    synchronized (_manager) {
        if (logger.isInfoEnabled()) {
            logger.info(Thread.currentThread().getId() + " START:INVOKE " + _path + " listener:" + _listener + " type: " + type);
        }
        if (!_expectTypes.contains(type)) {
            logger.warn("Callback handler received event in wrong order. Listener: " + _listener + ", path: " + _path + ", expected types: " + _expectTypes + " but was " + type);
            return;
        }
        _expectTypes = nextNotificationType.get(type);
        if (type == Type.INIT || type == Type.FINALIZE) {
            subscribeForChanges(changeContext.getType(), _path, _watchChild);
        } else {
            // put SubscribeForChange run in async thread to reduce the latency of zk callback handling.
            subscribeForChangesAsyn(changeContext.getType(), _path, _watchChild);
        }
        _expectTypes = nextNotificationType.get(type);
        if (_changeType == IDEAL_STATE) {
            IdealStateChangeListener idealStateChangeListener = (IdealStateChangeListener) _listener;
            List<IdealState> idealStates = preFetch(_propertyKey);
            idealStateChangeListener.onIdealStateChange(idealStates, changeContext);
        } else if (_changeType == INSTANCE_CONFIG) {
            if (_listener instanceof ConfigChangeListener) {
                ConfigChangeListener configChangeListener = (ConfigChangeListener) _listener;
                List<InstanceConfig> configs = preFetch(_propertyKey);
                configChangeListener.onConfigChange(configs, changeContext);
            } else if (_listener instanceof InstanceConfigChangeListener) {
                InstanceConfigChangeListener listener = (InstanceConfigChangeListener) _listener;
                List<InstanceConfig> configs = preFetch(_propertyKey);
                listener.onInstanceConfigChange(configs, changeContext);
            }
        } else if (_changeType == RESOURCE_CONFIG) {
            ResourceConfigChangeListener listener = (ResourceConfigChangeListener) _listener;
            List<ResourceConfig> configs = preFetch(_propertyKey);
            listener.onResourceConfigChange(configs, changeContext);
        } else if (_changeType == CLUSTER_CONFIG) {
            ClusterConfigChangeListener listener = (ClusterConfigChangeListener) _listener;
            ClusterConfig config = null;
            if (_preFetchEnabled) {
                config = _accessor.getProperty(_propertyKey);
            }
            listener.onClusterConfigChange(config, changeContext);
        } else if (_changeType == CONFIG) {
            ScopedConfigChangeListener listener = (ScopedConfigChangeListener) _listener;
            List<HelixProperty> configs = preFetch(_propertyKey);
            listener.onConfigChange(configs, changeContext);
        } else if (_changeType == LIVE_INSTANCE) {
            LiveInstanceChangeListener liveInstanceChangeListener = (LiveInstanceChangeListener) _listener;
            List<LiveInstance> liveInstances = preFetch(_propertyKey);
            liveInstanceChangeListener.onLiveInstanceChange(liveInstances, changeContext);
        } else if (_changeType == CURRENT_STATE) {
            CurrentStateChangeListener currentStateChangeListener = (CurrentStateChangeListener) _listener;
            String instanceName = PropertyPathConfig.getInstanceNameFromPath(_path);
            List<CurrentState> currentStates = preFetch(_propertyKey);
            currentStateChangeListener.onStateChange(instanceName, currentStates, changeContext);
        } else if (_changeType == MESSAGE) {
            MessageListener messageListener = (MessageListener) _listener;
            String instanceName = PropertyPathConfig.getInstanceNameFromPath(_path);
            List<Message> messages = preFetch(_propertyKey);
            messageListener.onMessage(instanceName, messages, changeContext);
        } else if (_changeType == MESSAGES_CONTROLLER) {
            MessageListener messageListener = (MessageListener) _listener;
            List<Message> messages = preFetch(_propertyKey);
            messageListener.onMessage(_manager.getInstanceName(), messages, changeContext);
        } else if (_changeType == EXTERNAL_VIEW || _changeType == TARGET_EXTERNAL_VIEW) {
            ExternalViewChangeListener externalViewListener = (ExternalViewChangeListener) _listener;
            List<ExternalView> externalViewList = preFetch(_propertyKey);
            externalViewListener.onExternalViewChange(externalViewList, changeContext);
        } else if (_changeType == CONTROLLER) {
            ControllerChangeListener controllerChangelistener = (ControllerChangeListener) _listener;
            controllerChangelistener.onControllerChange(changeContext);
        } else {
            logger.warn("Unknown change type: " + _changeType);
        }
        long end = System.currentTimeMillis();
        if (logger.isInfoEnabled()) {
            logger.info(Thread.currentThread().getId() + " END:INVOKE " + _path + " listener:" + _listener + " type: " + type + " Took: " + (end - start) + "ms");
        }
        if (_monitor != null) {
            _monitor.increaseCallbackCounters(end - start);
        }
    }
}
Also used : Message(org.apache.helix.model.Message) MessageListener(org.apache.helix.api.listeners.MessageListener) IdealState(org.apache.helix.model.IdealState) ScopedConfigChangeListener(org.apache.helix.api.listeners.ScopedConfigChangeListener) InstanceConfig(org.apache.helix.model.InstanceConfig) HelixProperty(org.apache.helix.HelixProperty) CurrentState(org.apache.helix.model.CurrentState) ClusterConfigChangeListener(org.apache.helix.api.listeners.ClusterConfigChangeListener) List(java.util.List) ResourceConfig(org.apache.helix.model.ResourceConfig) ResourceConfigChangeListener(org.apache.helix.api.listeners.ResourceConfigChangeListener) InstanceConfigChangeListener(org.apache.helix.api.listeners.InstanceConfigChangeListener) IdealStateChangeListener(org.apache.helix.api.listeners.IdealStateChangeListener) Type(org.apache.helix.NotificationContext.Type) ChangeType(org.apache.helix.HelixConstants.ChangeType) EventType(org.apache.zookeeper.Watcher.Event.EventType) ClusterConfigChangeListener(org.apache.helix.api.listeners.ClusterConfigChangeListener) InstanceConfigChangeListener(org.apache.helix.api.listeners.InstanceConfigChangeListener) ScopedConfigChangeListener(org.apache.helix.api.listeners.ScopedConfigChangeListener) ResourceConfigChangeListener(org.apache.helix.api.listeners.ResourceConfigChangeListener) ConfigChangeListener(org.apache.helix.api.listeners.ConfigChangeListener) CurrentStateChangeListener(org.apache.helix.api.listeners.CurrentStateChangeListener) ControllerChangeListener(org.apache.helix.api.listeners.ControllerChangeListener) LiveInstanceChangeListener(org.apache.helix.api.listeners.LiveInstanceChangeListener) ExternalViewChangeListener(org.apache.helix.api.listeners.ExternalViewChangeListener) ClusterConfig(org.apache.helix.model.ClusterConfig)

Example 43 with Message

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

the class ZkUnitTestBase method createMessage.

protected Message createMessage(MessageType type, String msgId, String fromState, String toState, String resourceName, String tgtName) {
    Message msg = new Message(type.toString(), msgId);
    msg.setFromState(fromState);
    msg.setToState(toState);
    msg.getRecord().setSimpleField(Attributes.RESOURCE_NAME.toString(), resourceName);
    msg.setTgtName(tgtName);
    return msg;
}
Also used : Message(org.apache.helix.model.Message)

Example 44 with Message

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

the class TestMessagePartitionStateMismatch method testStateMismatch.

@Test
public void testStateMismatch() throws InterruptedException {
    // String controllerName = CONTROLLER_PREFIX + "_0";
    // _startCMResultMap.get(controllerName)._manager;
    HelixManager manager = _controller;
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    Builder kb = accessor.keyBuilder();
    ExternalView ev = accessor.getProperty(kb.externalView(TEST_DB));
    Map<String, LiveInstance> liveinstanceMap = accessor.getChildValuesMap(accessor.keyBuilder().liveInstances());
    for (String instanceName : liveinstanceMap.keySet()) {
        String sessionid = liveinstanceMap.get(instanceName).getSessionId();
        for (String partition : ev.getPartitionSet()) {
            if (ev.getStateMap(partition).containsKey(instanceName)) {
                String uuid = UUID.randomUUID().toString();
                Message message = new Message(MessageType.STATE_TRANSITION, uuid);
                boolean rand = new Random().nextInt(10) > 5;
                if (ev.getStateMap(partition).get(instanceName).equals("MASTER")) {
                    message.setSrcName(manager.getInstanceName());
                    message.setTgtName(instanceName);
                    message.setMsgState(MessageState.NEW);
                    message.setPartitionName(partition);
                    message.setResourceName(TEST_DB);
                    message.setFromState(rand ? "SLAVE" : "OFFLINE");
                    message.setToState(rand ? "MASTER" : "SLAVE");
                    message.setTgtSessionId(sessionid);
                    message.setSrcSessionId(manager.getSessionId());
                    message.setStateModelDef("MasterSlave");
                    message.setStateModelFactoryName("DEFAULT");
                } else if (ev.getStateMap(partition).get(instanceName).equals("SLAVE")) {
                    message.setSrcName(manager.getInstanceName());
                    message.setTgtName(instanceName);
                    message.setMsgState(MessageState.NEW);
                    message.setPartitionName(partition);
                    message.setResourceName(TEST_DB);
                    message.setFromState(rand ? "MASTER" : "OFFLINE");
                    message.setToState(rand ? "SLAVE" : "SLAVE");
                    message.setTgtSessionId(sessionid);
                    message.setSrcSessionId(manager.getSessionId());
                    message.setStateModelDef("MasterSlave");
                    message.setStateModelFactoryName("DEFAULT");
                }
                accessor.setProperty(accessor.keyBuilder().message(instanceName, message.getMsgId()), message);
            }
        }
    }
    Thread.sleep(3000);
    ExternalView ev2 = accessor.getProperty(kb.externalView(TEST_DB));
    Assert.assertTrue(ev.equals(ev2));
}
Also used : ExternalView(org.apache.helix.model.ExternalView) HelixDataAccessor(org.apache.helix.HelixDataAccessor) HelixManager(org.apache.helix.HelixManager) LiveInstance(org.apache.helix.model.LiveInstance) Message(org.apache.helix.model.Message) Random(java.util.Random) Builder(org.apache.helix.PropertyKey.Builder) Test(org.testng.annotations.Test)

Example 45 with Message

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

the class TestMsgSelectionStage method testMasterXfer.

@Test
public void testMasterXfer() {
    System.out.println("START testMasterXfer at " + new Date(System.currentTimeMillis()));
    Map<String, LiveInstance> liveInstances = new HashMap<String, LiveInstance>();
    liveInstances.put("localhost_0", new LiveInstance("localhost_0"));
    liveInstances.put("localhost_1", new LiveInstance("localhost_1"));
    Map<String, String> currentStates = new HashMap<String, String>();
    currentStates.put("localhost_0", "SLAVE");
    currentStates.put("localhost_1", "MASTER");
    Map<String, Message> pendingMessages = new HashMap<String, Message>();
    List<Message> messages = new ArrayList<Message>();
    messages.add(TestHelper.createMessage("msgId_0", "SLAVE", "MASTER", "localhost_0", "TestDB", "TestDB_0"));
    messages.add(TestHelper.createMessage("msgId_1", "MASTER", "SLAVE", "localhost_1", "TestDB", "TestDB_0"));
    Map<String, Bounds> stateConstraints = new HashMap<String, Bounds>();
    stateConstraints.put("MASTER", new Bounds(0, 1));
    stateConstraints.put("SLAVE", new Bounds(0, 2));
    Map<String, Integer> stateTransitionPriorities = new HashMap<String, Integer>();
    stateTransitionPriorities.put("MASTER-SLAVE", 0);
    stateTransitionPriorities.put("SLAVE-MASTER", 1);
    List<Message> selectedMsg = new MessageSelectionStage().selectMessages(liveInstances, currentStates, pendingMessages, messages, stateConstraints, stateTransitionPriorities, BuiltInStateModelDefinitions.MasterSlave.getStateModelDefinition(), false);
    Assert.assertEquals(selectedMsg.size(), 1);
    Assert.assertEquals(selectedMsg.get(0).getMsgId(), "msgId_1");
    System.out.println("END testMasterXfer at " + new Date(System.currentTimeMillis()));
}
Also used : Message(org.apache.helix.model.Message) HashMap(java.util.HashMap) Bounds(org.apache.helix.controller.stages.MessageSelectionStage.Bounds) ArrayList(java.util.ArrayList) Date(java.util.Date) LiveInstance(org.apache.helix.model.LiveInstance) 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