Search in sources :

Example 1 with InstanceType

use of org.apache.helix.InstanceType in project incubator-gobblin by apache.

the class GobblinHelixMessagingService method generateMessage.

@Override
public Map<InstanceType, List<Message>> generateMessage(final Criteria recipientCriteria, final Message message) {
    Map<InstanceType, List<Message>> messagesToSendMap = new HashMap<InstanceType, List<Message>>();
    InstanceType instanceType = recipientCriteria.getRecipientInstanceType();
    if (instanceType == InstanceType.CONTROLLER) {
        List<Message> messages = generateMessagesForController(message);
        messagesToSendMap.put(InstanceType.CONTROLLER, messages);
    // _dataAccessor.setControllerProperty(PropertyType.MESSAGES,
    // newMessage.getRecord(), CreateMode.PERSISTENT);
    } else if (instanceType == InstanceType.PARTICIPANT) {
        List<Message> messages = new ArrayList<Message>();
        List<Map<String, String>> matchedList = _gobblinHelixCriteriaEvaluator.evaluateCriteria(recipientCriteria, _manager);
        if (!matchedList.isEmpty()) {
            Map<String, String> sessionIdMap = new HashMap<String, String>();
            if (recipientCriteria.isSessionSpecific()) {
                HelixDataAccessor accessor = _manager.getHelixDataAccessor();
                PropertyKey.Builder keyBuilder = accessor.keyBuilder();
                List<LiveInstance> liveInstances = accessor.getChildValues(keyBuilder.liveInstances());
                for (LiveInstance liveInstance : liveInstances) {
                    sessionIdMap.put(liveInstance.getInstanceName(), liveInstance.getSessionId());
                }
            }
            for (Map<String, String> map : matchedList) {
                String id = UUID.randomUUID().toString();
                Message newMessage = new Message(message.getRecord(), id);
                String srcInstanceName = _manager.getInstanceName();
                String tgtInstanceName = map.get("instanceName");
                // Don't send message to self
                if (recipientCriteria.isSelfExcluded() && srcInstanceName.equalsIgnoreCase(tgtInstanceName)) {
                    continue;
                }
                newMessage.setSrcName(srcInstanceName);
                newMessage.setTgtName(tgtInstanceName);
                newMessage.setResourceName(map.get("resourceName"));
                newMessage.setPartitionName(map.get("partitionName"));
                if (recipientCriteria.isSessionSpecific()) {
                    newMessage.setTgtSessionId(sessionIdMap.get(tgtInstanceName));
                }
                messages.add(newMessage);
            }
            messagesToSendMap.put(InstanceType.PARTICIPANT, messages);
        }
    }
    return messagesToSendMap;
}
Also used : Message(org.apache.helix.model.Message) HashMap(java.util.HashMap) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) ArrayList(java.util.ArrayList) List(java.util.List) InstanceType(org.apache.helix.InstanceType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with InstanceType

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

the class DefaultMessagingService method generateMessage.

public Map<InstanceType, List<Message>> generateMessage(final Criteria recipientCriteria, final Message message) {
    Map<InstanceType, List<Message>> messagesToSendMap = new HashMap<InstanceType, List<Message>>();
    InstanceType instanceType = recipientCriteria.getRecipientInstanceType();
    if (instanceType == InstanceType.CONTROLLER) {
        List<Message> messages = generateMessagesForController(message);
        messagesToSendMap.put(InstanceType.CONTROLLER, messages);
    // _dataAccessor.setControllerProperty(PropertyType.MESSAGES,
    // newMessage.getRecord(), CreateMode.PERSISTENT);
    } else if (instanceType == InstanceType.PARTICIPANT) {
        List<Message> messages = new ArrayList<Message>();
        List<Map<String, String>> matchedList = _evaluator.evaluateCriteria(recipientCriteria, _manager);
        if (!matchedList.isEmpty()) {
            Map<String, String> sessionIdMap = new HashMap<String, String>();
            if (recipientCriteria.isSessionSpecific()) {
                HelixDataAccessor accessor = _manager.getHelixDataAccessor();
                Builder keyBuilder = accessor.keyBuilder();
                List<LiveInstance> liveInstances = accessor.getChildValues(keyBuilder.liveInstances());
                for (LiveInstance liveInstance : liveInstances) {
                    sessionIdMap.put(liveInstance.getInstanceName(), liveInstance.getSessionId());
                }
            }
            for (Map<String, String> map : matchedList) {
                String id = UUID.randomUUID().toString();
                Message newMessage = new Message(message.getRecord(), id);
                String srcInstanceName = _manager.getInstanceName();
                String tgtInstanceName = map.get("instanceName");
                // Don't send message to self
                if (recipientCriteria.isSelfExcluded() && srcInstanceName.equalsIgnoreCase(tgtInstanceName)) {
                    continue;
                }
                newMessage.setSrcName(srcInstanceName);
                newMessage.setTgtName(tgtInstanceName);
                newMessage.setResourceName(map.get("resourceName"));
                newMessage.setPartitionName(map.get("partitionName"));
                if (recipientCriteria.isSessionSpecific()) {
                    newMessage.setTgtSessionId(sessionIdMap.get(tgtInstanceName));
                }
                messages.add(newMessage);
            }
            messagesToSendMap.put(InstanceType.PARTICIPANT, messages);
        }
    }
    return messagesToSendMap;
}
Also used : Message(org.apache.helix.model.Message) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConfigScopeBuilder(org.apache.helix.model.builder.ConfigScopeBuilder) Builder(org.apache.helix.PropertyKey.Builder) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) ArrayList(java.util.ArrayList) List(java.util.List) InstanceType(org.apache.helix.InstanceType) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with InstanceType

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

the class DistributedLeaderElection method onControllerChange.

/**
 * may be accessed by multiple threads: zk-client thread and
 * ZkHelixManager.disconnect()->reset() TODO: Refactor accessing
 * HelixMangerMain class statically
 */
@Override
public synchronized void onControllerChange(NotificationContext changeContext) {
    HelixManager manager = changeContext.getManager();
    if (manager == null) {
        LOG.error("missing attributes in changeContext. requires HelixManager");
        return;
    }
    InstanceType type = manager.getInstanceType();
    if (type != InstanceType.CONTROLLER && type != InstanceType.CONTROLLER_PARTICIPANT) {
        LOG.error("fail to become controller because incorrect instanceType (was " + type.toString() + ", requires CONTROLLER | CONTROLLER_PARTICIPANT)");
        return;
    }
    ControllerManagerHelper controllerHelper = new ControllerManagerHelper(_manager, _controllerTimerTasks);
    try {
        if (changeContext.getType().equals(NotificationContext.Type.INIT) || changeContext.getType().equals(NotificationContext.Type.CALLBACK)) {
            LOG.info(_manager.getInstanceName() + " is trying to acquire leadership for cluster: " + _manager.getClusterName());
            HelixDataAccessor accessor = manager.getHelixDataAccessor();
            Builder keyBuilder = accessor.keyBuilder();
            while (accessor.getProperty(keyBuilder.controllerLeader()) == null) {
                boolean success = tryUpdateController(manager);
                if (success) {
                    LOG.info(_manager.getInstanceName() + " acquired leadership for cluster: " + _manager.getClusterName());
                    updateHistory(manager);
                    _manager.getHelixDataAccessor().getBaseDataAccessor().reset();
                    controllerHelper.addListenersToController(_controller);
                    controllerHelper.startControllerTimerTasks();
                }
            }
        } else if (changeContext.getType().equals(NotificationContext.Type.FINALIZE)) {
            LOG.info(_manager.getInstanceName() + " relinquish leadership for cluster: " + _manager.getClusterName());
            controllerHelper.stopControllerTimerTasks();
            controllerHelper.removeListenersFromController(_controller);
            /**
             * clear write-through cache
             */
            _manager.getHelixDataAccessor().getBaseDataAccessor().reset();
        }
    } catch (Exception e) {
        LOG.error("Exception when trying to become leader", e);
    }
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) HelixManager(org.apache.helix.HelixManager) Builder(org.apache.helix.PropertyKey.Builder) InstanceType(org.apache.helix.InstanceType)

Example 4 with InstanceType

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

the class DefaultMessagingService method send.

@Override
public int send(final Criteria recipientCriteria, final Message message, AsyncCallback callbackOnReply, int timeOut, int retryCount) {
    Map<InstanceType, List<Message>> generateMessage = generateMessage(recipientCriteria, message);
    int totalMessageCount = 0;
    for (List<Message> messages : generateMessage.values()) {
        totalMessageCount += messages.size();
    }
    _logger.info("Send " + totalMessageCount + " messages with criteria " + recipientCriteria);
    if (totalMessageCount == 0) {
        return 0;
    }
    String correlationId = null;
    if (callbackOnReply != null) {
        int totalTimeout = timeOut * (retryCount + 1);
        if (totalTimeout < 0) {
            totalTimeout = -1;
        }
        callbackOnReply.setTimeout(totalTimeout);
        correlationId = UUID.randomUUID().toString();
        for (List<Message> messages : generateMessage.values()) {
            callbackOnReply.setMessagesSent(messages);
        }
        _asyncCallbackService.registerAsyncCallback(correlationId, callbackOnReply);
    }
    for (InstanceType receiverType : generateMessage.keySet()) {
        List<Message> list = generateMessage.get(receiverType);
        for (Message tempMessage : list) {
            tempMessage.setRetryCount(retryCount);
            tempMessage.setExecutionTimeout(timeOut);
            tempMessage.setSrcInstanceType(_manager.getInstanceType());
            if (correlationId != null) {
                tempMessage.setCorrelationId(correlationId);
            }
            HelixDataAccessor accessor = _manager.getHelixDataAccessor();
            Builder keyBuilder = accessor.keyBuilder();
            if (receiverType == InstanceType.CONTROLLER) {
                // _manager.getDataAccessor().setProperty(PropertyType.MESSAGES_CONTROLLER,
                // tempMessage,
                // tempMessage.getId());
                accessor.setProperty(keyBuilder.controllerMessage(tempMessage.getId()), tempMessage);
            }
            if (receiverType == InstanceType.PARTICIPANT) {
                accessor.setProperty(keyBuilder.message(tempMessage.getTgtName(), tempMessage.getId()), tempMessage);
            }
        }
    }
    if (callbackOnReply != null) {
        // start timer if timeout is set
        callbackOnReply.startTimer();
    }
    return totalMessageCount;
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) Message(org.apache.helix.model.Message) ConfigScopeBuilder(org.apache.helix.model.builder.ConfigScopeBuilder) Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList) List(java.util.List) InstanceType(org.apache.helix.InstanceType)

Aggregations

HelixDataAccessor (org.apache.helix.HelixDataAccessor)4 InstanceType (org.apache.helix.InstanceType)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Builder (org.apache.helix.PropertyKey.Builder)3 Message (org.apache.helix.model.Message)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 LiveInstance (org.apache.helix.model.LiveInstance)2 ConfigScopeBuilder (org.apache.helix.model.builder.ConfigScopeBuilder)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 HelixManager (org.apache.helix.HelixManager)1