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();
}
}
}
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);
}
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());
}
}
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;
}
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);
}
}
}
}
Aggregations