use of org.apache.helix.model.Message in project helix by apache.
the class HelixStateMachineEngine method sendNopMessage.
// TODO: duplicated code in DefaultMessagingService
private void sendNopMessage() {
if (_manager.isConnected()) {
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);
}
logger.info("Send NO_OP message to " + nopMsg.getTgtName() + ", msgId: " + nopMsg.getId());
} catch (Exception e) {
logger.error(e.toString());
}
}
}
use of org.apache.helix.model.Message in project helix by apache.
the class InstanceAccessor method getMessageOnInstance.
@GET
@Path("{instanceName}/messages/{messageId}")
public Response getMessageOnInstance(@PathParam("clusterId") String clusterId, @PathParam("instanceName") String instanceName, @PathParam("messageId") String messageId) throws IOException {
HelixDataAccessor accessor = getDataAccssor(clusterId);
Message message = accessor.getProperty(accessor.keyBuilder().message(instanceName, messageId));
if (message != null) {
return JSONRepresentation(message.getRecord());
}
return notFound();
}
use of org.apache.helix.model.Message in project helix by apache.
the class InstanceAccessor method getMessagesOnInstance.
@GET
@Path("{instanceName}/messages")
public Response getMessagesOnInstance(@PathParam("clusterId") String clusterId, @PathParam("instanceName") String instanceName) throws IOException {
HelixDataAccessor accessor = getDataAccssor(clusterId);
ObjectNode root = JsonNodeFactory.instance.objectNode();
root.put(Properties.id.name(), instanceName);
ArrayNode newMessages = root.putArray(InstanceProperties.new_messages.name());
ArrayNode readMessages = root.putArray(InstanceProperties.read_messages.name());
List<String> messages = accessor.getChildNames(accessor.keyBuilder().messages(instanceName));
if (messages == null || messages.size() == 0) {
return notFound();
}
for (String messageName : messages) {
Message message = accessor.getProperty(accessor.keyBuilder().message(instanceName, messageName));
if (message.getMsgState() == Message.MessageState.NEW) {
newMessages.add(messageName);
}
if (message.getMsgState() == Message.MessageState.READ) {
readMessages.add(messageName);
}
}
root.put(InstanceProperties.total_message_count.name(), newMessages.size() + readMessages.size());
root.put(InstanceProperties.read_message_count.name(), readMessages.size());
return JSONRepresentation(root);
}
use of org.apache.helix.model.Message in project helix by apache.
the class SchedulerTasksResource method post.
@Override
public Representation post(Representation entity) {
try {
String clusterName = (String) getRequest().getAttributes().get("clusterName");
Form form = new Form(entity);
ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
String msgTemplateString = ClusterRepresentationUtil.getFormJsonParameterString(form, MESSAGETEMPLATE);
if (msgTemplateString == null) {
throw new HelixException("SchedulerTasksResource need to have MessageTemplate specified.");
}
Map<String, String> messageTemplate = ClusterRepresentationUtil.getFormJsonParameters(form, MESSAGETEMPLATE);
String criteriaString = ClusterRepresentationUtil.getFormJsonParameterString(form, CRITERIA);
if (criteriaString == null) {
throw new HelixException("SchedulerTasksResource need to have Criteria specified.");
}
HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
LiveInstance leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
if (leader == null) {
throw new HelixException("There is no leader for the cluster " + clusterName);
}
Message schedulerMessage = new Message(MessageType.SCHEDULER_MSG, UUID.randomUUID().toString());
schedulerMessage.getRecord().getSimpleFields().put(CRITERIA, criteriaString);
schedulerMessage.getRecord().getMapFields().put(MESSAGETEMPLATE, messageTemplate);
schedulerMessage.setTgtSessionId(leader.getSessionId());
schedulerMessage.setTgtName("CONTROLLER");
schedulerMessage.setSrcInstanceType(InstanceType.CONTROLLER);
String taskQueueName = ClusterRepresentationUtil.getFormJsonParameterString(form, TASKQUEUENAME);
if (taskQueueName != null && taskQueueName.length() > 0) {
schedulerMessage.getRecord().setSimpleField(DefaultSchedulerMessageHandlerFactory.SCHEDULER_TASK_QUEUE, taskQueueName);
}
accessor.setProperty(accessor.keyBuilder().controllerMessage(schedulerMessage.getMsgId()), schedulerMessage);
Map<String, String> resultMap = new HashMap<String, String>();
resultMap.put("StatusUpdatePath", PropertyPathBuilder.controllerStatusUpdate(clusterName, MessageType.SCHEDULER_MSG.name(), schedulerMessage.getMsgId()));
resultMap.put("MessageType", Message.MessageType.SCHEDULER_MSG.name());
resultMap.put("MsgId", schedulerMessage.getMsgId());
// Assemble the rest URL for task status update
String ipAddress = InetAddress.getLocalHost().getCanonicalHostName();
String url = "http://" + ipAddress + ":" + getContext().getAttributes().get(RestAdminApplication.PORT) + "/clusters/" + clusterName + "/Controller/statusUpdates/SCHEDULER_MSG/" + schedulerMessage.getMsgId();
resultMap.put("statusUpdateUrl", url);
getResponse().setEntity(ClusterRepresentationUtil.ObjectToJson(resultMap), MediaType.APPLICATION_JSON);
getResponse().setStatus(Status.SUCCESS_OK);
} catch (Exception e) {
getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
getResponse().setStatus(Status.SUCCESS_OK);
LOG.error("", e);
}
return null;
}
use of org.apache.helix.model.Message in project helix by apache.
the class CurrentStateComputationStage method process.
@Override
public void process(ClusterEvent event) throws Exception {
ClusterDataCache cache = event.getAttribute(AttributeName.ClusterDataCache.name());
Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());
if (cache == null || resourceMap == null) {
throw new StageException("Missing attributes in event:" + event + ". Requires DataCache|RESOURCE");
}
Map<String, LiveInstance> liveInstances = cache.getLiveInstances();
CurrentStateOutput currentStateOutput = new CurrentStateOutput();
for (LiveInstance instance : liveInstances.values()) {
String instanceName = instance.getInstanceName();
String instanceSessionId = instance.getSessionId();
// update pending messages
Map<String, Message> messages = cache.getMessages(instanceName);
updatePendingMessages(instance, messages.values(), currentStateOutput, resourceMap);
// update current states.
Map<String, CurrentState> currentStateMap = cache.getCurrentState(instanceName, instanceSessionId);
updateCurrentStates(instance, currentStateMap.values(), currentStateOutput, resourceMap);
}
if (!cache.isTaskCache()) {
ClusterStatusMonitor clusterStatusMonitor = event.getAttribute(AttributeName.clusterStatusMonitor.name());
updateMissingTopStateStatus(cache, clusterStatusMonitor, resourceMap, currentStateOutput);
}
event.addAttribute(AttributeName.CURRENT_STATE.name(), currentStateOutput);
}
Aggregations