Search in sources :

Example 56 with HelixDataAccessor

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

the class HelixTaskExecutor method syncSessionToController.

private void syncSessionToController(HelixManager manager) {
    if (_lastSessionSyncTime == null || System.currentTimeMillis() - _lastSessionSyncTime > SESSION_SYNC_INTERVAL) {
        // > delay since last sync
        HelixDataAccessor accessor = manager.getHelixDataAccessor();
        PropertyKey key = new Builder(manager.getClusterName()).controllerMessage(SESSION_SYNC);
        if (accessor.getProperty(key) == null) {
            LOG.info(String.format("Participant %s syncs session with controller", manager.getInstanceName()));
            Message msg = new Message(MessageType.PARTICIPANT_SESSION_CHANGE, SESSION_SYNC);
            msg.setSrcName(manager.getInstanceName());
            msg.setTgtSessionId("*");
            msg.setMsgState(MessageState.NEW);
            msg.setMsgId(SESSION_SYNC);
            Criteria cr = new Criteria();
            cr.setRecipientInstanceType(InstanceType.CONTROLLER);
            cr.setSessionSpecific(false);
            manager.getMessagingService().send(cr, msg);
            _lastSessionSyncTime = System.currentTimeMillis();
        }
    }
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) Message(org.apache.helix.model.Message) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) Builder(org.apache.helix.PropertyKey.Builder) Criteria(org.apache.helix.Criteria) PropertyKey(org.apache.helix.PropertyKey)

Example 57 with HelixDataAccessor

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

the class CriteriaEvaluator method evaluateCriteria.

/**
 * Examine persisted data to match wildcards in {@link Criteria}
 * @param recipientCriteria Criteria specifying the message destinations
 * @param manager connection to the persisted data
 * @return map of evaluated criteria
 */
public List<Map<String, String>> evaluateCriteria(Criteria recipientCriteria, HelixManager manager) {
    // get the data
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    List<HelixProperty> properties;
    DataSource dataSource = recipientCriteria.getDataSource();
    String resourceName = recipientCriteria.getResource();
    String instanceName = recipientCriteria.getInstanceName();
    switch(dataSource) {
        case EXTERNALVIEW:
            properties = getProperty(accessor, resourceName, keyBuilder.externalViews(), keyBuilder.externalView(resourceName), DataSource.EXTERNALVIEW.name());
            break;
        case IDEALSTATES:
            properties = getProperty(accessor, resourceName, keyBuilder.idealStates(), keyBuilder.idealStates(resourceName), DataSource.IDEALSTATES.name());
            break;
        case LIVEINSTANCES:
            properties = getProperty(accessor, instanceName, keyBuilder.liveInstances(), keyBuilder.liveInstance(instanceName), DataSource.LIVEINSTANCES.name());
            break;
        case INSTANCES:
            properties = getProperty(accessor, instanceName, keyBuilder.instances(), keyBuilder.instance(instanceName), DataSource.INSTANCES.name());
            break;
        default:
            return Lists.newArrayList();
    }
    // flatten the data
    List<ZNRecordRow> allRows = ZNRecordRow.flatten(HelixProperty.convertToList(properties));
    // save the matches
    Set<String> liveParticipants = accessor.getChildValuesMap(keyBuilder.liveInstances()).keySet();
    List<ZNRecordRow> result = Lists.newArrayList();
    for (ZNRecordRow row : allRows) {
        // The participant instance name is stored in the return value of either getRecordId() or getMapSubKey()
        if (rowMatches(recipientCriteria, row) && (liveParticipants.contains(row.getRecordId()) || liveParticipants.contains(row.getMapSubKey()))) {
            result.add(row);
        }
    }
    Set<Map<String, String>> selected = Sets.newHashSet();
    // deduplicate and convert the matches into the required format
    for (ZNRecordRow row : result) {
        Map<String, String> resultRow = new HashMap<String, String>();
        resultRow.put("instanceName", !recipientCriteria.getInstanceName().equals("") ? (!Strings.isNullOrEmpty(row.getMapSubKey()) ? row.getMapSubKey() : row.getRecordId()) : "");
        resultRow.put("resourceName", !recipientCriteria.getResource().equals("") ? row.getRecordId() : "");
        resultRow.put("partitionName", !recipientCriteria.getPartition().equals("") ? row.getMapKey() : "");
        resultRow.put("partitionState", !recipientCriteria.getPartitionState().equals("") ? row.getMapValue() : "");
        selected.add(resultRow);
    }
    logger.info("Query returned " + selected.size() + " rows");
    return Lists.newArrayList(selected);
}
Also used : HashMap(java.util.HashMap) DataSource(org.apache.helix.Criteria.DataSource) HelixDataAccessor(org.apache.helix.HelixDataAccessor) HelixProperty(org.apache.helix.HelixProperty) HashMap(java.util.HashMap) Map(java.util.Map) PropertyKey(org.apache.helix.PropertyKey)

Example 58 with HelixDataAccessor

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

the class DefaultMessagingService method sendNopMessageInternal.

private void sendNopMessageInternal() {
    try {
        Message nopMsg = new Message(MessageType.NO_OP, UUID.randomUUID().toString());
        nopMsg.setSrcName(_manager.getInstanceName());
        HelixDataAccessor accessor = _manager.getHelixDataAccessor();
        Builder keyBuilder = accessor.keyBuilder();
        if (_manager.getInstanceType() == InstanceType.CONTROLLER || _manager.getInstanceType() == InstanceType.CONTROLLER_PARTICIPANT) {
            nopMsg.setTgtName(InstanceType.CONTROLLER.name());
            accessor.setProperty(keyBuilder.controllerMessage(nopMsg.getId()), nopMsg);
        }
        if (_manager.getInstanceType() == InstanceType.PARTICIPANT || _manager.getInstanceType() == InstanceType.CONTROLLER_PARTICIPANT) {
            nopMsg.setTgtName(_manager.getInstanceName());
            accessor.setProperty(keyBuilder.message(nopMsg.getTgtName(), nopMsg.getId()), nopMsg);
        }
    } catch (Exception e) {
        _logger.error(e.toString());
    }
}
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)

Example 59 with HelixDataAccessor

use of org.apache.helix.HelixDataAccessor 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 60 with HelixDataAccessor

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

the class BatchMessageHandler method postHandleMessage.

public void postHandleMessage() {
    if (_message.getBatchMessageMode() == true && _batchMsgWrapper != null) {
        _batchMsgWrapper.end(_message, _notificationContext);
    }
    // update currentState
    HelixManager manager = _notificationContext.getManager();
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    ConcurrentHashMap<String, CurrentStateUpdate> csUpdateMap = (ConcurrentHashMap<String, CurrentStateUpdate>) _notificationContext.get(MapKey.CURRENT_STATE_UPDATE.toString());
    if (csUpdateMap != null) {
        Map<PropertyKey, CurrentState> csUpdate = mergeCurStateUpdate(csUpdateMap);
        // TODO: change to use asyncSet
        for (PropertyKey key : csUpdate.keySet()) {
            // curStateMap.get(key));
            if (!accessor.updateProperty(key, csUpdate.get(key))) {
                LOG.error("Fails to persist current state to ZK for key " + key);
            }
        }
    }
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) HelixManager(org.apache.helix.HelixManager) CurrentState(org.apache.helix.model.CurrentState) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PropertyKey(org.apache.helix.PropertyKey)

Aggregations

HelixDataAccessor (org.apache.helix.HelixDataAccessor)173 ZNRecord (org.apache.helix.ZNRecord)91 PropertyKey (org.apache.helix.PropertyKey)69 Test (org.testng.annotations.Test)67 Builder (org.apache.helix.PropertyKey.Builder)59 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)40 Date (java.util.Date)39 HelixManager (org.apache.helix.HelixManager)35 IdealState (org.apache.helix.model.IdealState)33 LiveInstance (org.apache.helix.model.LiveInstance)31 HashMap (java.util.HashMap)30 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)30 Message (org.apache.helix.model.Message)30 ArrayList (java.util.ArrayList)28 ExternalView (org.apache.helix.model.ExternalView)26 PropertyPathBuilder (org.apache.helix.PropertyPathBuilder)25 Map (java.util.Map)19 HelixException (org.apache.helix.HelixException)19 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)19 InstanceConfig (org.apache.helix.model.InstanceConfig)17