Search in sources :

Example 91 with Message

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());
        }
    }
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) Message(org.apache.helix.model.Message) Builder(org.apache.helix.PropertyKey.Builder) HelixException(org.apache.helix.HelixException)

Example 92 with Message

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();
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) Message(org.apache.helix.model.Message) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 93 with Message

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);
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) ObjectNode(org.codehaus.jackson.node.ObjectNode) Message(org.apache.helix.model.Message) ArrayNode(org.codehaus.jackson.node.ArrayNode) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 94 with Message

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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixException(org.apache.helix.HelixException) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) Message(org.apache.helix.model.Message) Form(org.restlet.data.Form) HashMap(java.util.HashMap) HelixException(org.apache.helix.HelixException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException)

Example 95 with Message

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);
}
Also used : Message(org.apache.helix.model.Message) StageException(org.apache.helix.controller.pipeline.StageException) Resource(org.apache.helix.model.Resource) ClusterStatusMonitor(org.apache.helix.monitoring.mbeans.ClusterStatusMonitor) LiveInstance(org.apache.helix.model.LiveInstance) CurrentState(org.apache.helix.model.CurrentState)

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