Search in sources :

Example 1 with MessageContext

use of org.apache.airavata.messaging.core.MessageContext in project airavata by apache.

the class AiravataServerHandler method createExperiment.

/**
 * Create an experiment for the specified user belonging to the gateway. The gateway identity is not explicitly passed
 * but inferred from the authentication header. This experiment is just a persistent place holder. The client
 * has to subsequently configure and launch the created experiment. No action is taken on Airavata Server except
 * registering the experiment in a persistent store.
 *
 * @param experiment@return The server-side generated.airavata.registry.core.experiment.globally unique identifier.
 * @throws org.apache.airavata.model.error.InvalidRequestException For any incorrect forming of the request itself.
 * @throws org.apache.airavata.model.error.AiravataClientException The following list of exceptions are thrown which Airavata Client can take corrective actions to resolve:
 *                                                               <p/>
 *                                                               UNKNOWN_GATEWAY_ID - If a Gateway is not registered with Airavata as a one time administrative
 *                                                               step, then Airavata Registry will not have a provenance area setup. The client has to follow
 *                                                               gateway registration steps and retry this request.
 *                                                               <p/>
 *                                                               AUTHENTICATION_FAILURE - How Authentication will be implemented is yet to be determined.
 *                                                               For now this is a place holder.
 *                                                               <p/>
 *                                                               INVALID_AUTHORIZATION - This will throw an authorization exception. When a more robust security hand-shake
 *                                                               is implemented, the authorization will be more substantial.
 * @throws org.apache.airavata.model.error.AiravataSystemException This exception will be thrown for any Airavata Server side issues and if the problem cannot be corrected by the client
 *                                                               rather an Airavata Administrator will be notified to take corrective action.
 */
@Override
@SecurityCheck
public String createExperiment(AuthzToken authzToken, String gatewayId, ExperimentModel experiment) throws InvalidRequestException, AiravataClientException, AiravataSystemException, AuthorizationException, TException {
    RegistryService.Client regClient = registryClientPool.getResource();
    SharingRegistryService.Client sharingClient = sharingClientPool.getResource();
    try {
        String experimentId = regClient.createExperiment(gatewayId, experiment);
        if (ServerSettings.isEnableSharing()) {
            try {
                Entity entity = new Entity();
                entity.setEntityId(experimentId);
                entity.setDomainId(experiment.getGatewayId());
                entity.setEntityTypeId(experiment.getGatewayId() + ":" + "EXPERIMENT");
                entity.setOwnerId(experiment.getUserName() + "@" + experiment.getGatewayId());
                entity.setName(experiment.getExperimentName());
                entity.setDescription(experiment.getDescription());
                entity.setParentEntityId(experiment.getProjectId());
                sharingClient.createEntity(entity);
            } catch (Exception ex) {
                logger.error(ex.getMessage(), ex);
                logger.error("Rolling back experiment creation Exp ID : " + experimentId);
                regClient.deleteExperiment(experimentId);
                AiravataSystemException ase = new AiravataSystemException();
                ase.setMessage("Failed to create sharing registry record");
                throw ase;
            }
        }
        ExperimentStatusChangeEvent event = new ExperimentStatusChangeEvent(ExperimentState.CREATED, experimentId, gatewayId);
        String messageId = AiravataUtils.getId("EXPERIMENT");
        MessageContext messageContext = new MessageContext(event, MessageType.EXPERIMENT, messageId, gatewayId);
        messageContext.setUpdatedTime(AiravataUtils.getCurrentTimestamp());
        if (statusPublisher != null) {
            statusPublisher.publish(messageContext);
        }
        logger.debug(experimentId, "Created new experiment with experiment name {}", experiment.getExperimentName());
        registryClientPool.returnResource(regClient);
        sharingClientPool.returnResource(sharingClient);
        return experimentId;
    } catch (Exception e) {
        logger.error("Error while creating the experiment with experiment name {}", experiment.getExperimentName());
        AiravataSystemException exception = new AiravataSystemException();
        exception.setAiravataErrorType(AiravataErrorType.INTERNAL_ERROR);
        exception.setMessage("Error while creating the experiment. More info : " + e.getMessage());
        registryClientPool.returnBrokenResource(regClient);
        sharingClientPool.returnBrokenResource(sharingClient);
        throw exception;
    }
}
Also used : SharingRegistryService(org.apache.airavata.sharing.registry.service.cpi.SharingRegistryService) ExperimentStatusChangeEvent(org.apache.airavata.model.messaging.event.ExperimentStatusChangeEvent) MessageContext(org.apache.airavata.messaging.core.MessageContext) RegistryService(org.apache.airavata.registry.api.RegistryService) SharingRegistryService(org.apache.airavata.sharing.registry.service.cpi.SharingRegistryService) RegistryServiceException(org.apache.airavata.registry.api.exception.RegistryServiceException) CredentialStoreException(org.apache.airavata.credential.store.exception.CredentialStoreException) AiravataException(org.apache.airavata.common.exception.AiravataException) TException(org.apache.thrift.TException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) SecurityCheck(org.apache.airavata.service.security.interceptor.SecurityCheck)

Example 2 with MessageContext

use of org.apache.airavata.messaging.core.MessageContext in project airavata by apache.

the class DBEventMessageHandler method onMessage.

@Override
public void onMessage(MessageContext messageContext) {
    log.info("Incoming DB event message. Message Id : " + messageContext.getMessageId());
    try {
        byte[] bytes = ThriftUtils.serializeThriftObject(messageContext.getEvent());
        DBEventMessage dbEventMessage = new DBEventMessage();
        ThriftUtils.createThriftFromBytes(bytes, dbEventMessage);
        DBEventMessageContext dBEventMessageContext = dbEventMessage.getMessageContext();
        switch(dbEventMessage.getDbEventType()) {
            case SUBSCRIBER:
                log.info("Registering " + dBEventMessageContext.getSubscriber().getSubscriberService() + " subscriber for " + dbEventMessage.getPublisherService());
                DbEventManagerZkUtils.createDBEventMgrZkNode(curatorClient, dbEventMessage.getPublisherService(), dBEventMessageContext.getSubscriber().getSubscriberService());
                break;
            case PUBLISHER:
                List<String> subscribers = DbEventManagerZkUtils.getSubscribersForPublisher(curatorClient, dbEventMessage.getPublisherService());
                if (subscribers.isEmpty()) {
                    log.error("No Subscribers registered for the service");
                    throw new DBEventManagerException("No Subscribers registered for the service");
                }
                String routingKey = getRoutingKeyFromList(subscribers);
                log.info("Publishing " + dbEventMessage.getPublisherService() + " db event to " + subscribers.toString());
                MessageContext messageCtx = new MessageContext(dbEventMessage, MessageType.DB_EVENT, "", "");
                messageCtx.setUpdatedTime(AiravataUtils.getCurrentTimestamp());
                DBEventManagerMessagingFactory.getDBEventPublisher().publish(messageCtx, routingKey);
                break;
        }
        log.info("Sending ack. Message Delivery Tag : " + messageContext.getDeliveryTag());
        DBEventManagerMessagingFactory.getDBEventSubscriber().sendAck(messageContext.getDeliveryTag());
    } catch (Exception e) {
        log.error("Error processing message.", e);
    }
}
Also used : DBEventMessage(org.apache.airavata.model.dbevent.DBEventMessage) DBEventMessageContext(org.apache.airavata.model.dbevent.DBEventMessageContext) DBEventManagerException(org.apache.airavata.db.event.manager.messaging.DBEventManagerException) MessageContext(org.apache.airavata.messaging.core.MessageContext) DBEventMessageContext(org.apache.airavata.model.dbevent.DBEventMessageContext) DBEventManagerException(org.apache.airavata.db.event.manager.messaging.DBEventManagerException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException)

Example 3 with MessageContext

use of org.apache.airavata.messaging.core.MessageContext in project airavata by apache.

the class RegistryServiceDBEventMessagingFactory method registerRegistryServiceWithPublishers.

public static boolean registerRegistryServiceWithPublishers(List<String> publisherList) throws AiravataException {
    for (String publisher : publisherList) {
        logger.info("Sending service discovery message. Publisher: " + publisher + ", Subscriber: " + DBEventService.REGISTRY.toString());
        DBEventSubscriber dbEventSubscriber = new DBEventSubscriber(DBEventService.REGISTRY.toString());
        DBEventMessageContext dbEventMessageContext = new DBEventMessageContext();
        dbEventMessageContext.setSubscriber(dbEventSubscriber);
        DBEventMessage dbEventMessage = new DBEventMessage(DBEventType.SUBSCRIBER, dbEventMessageContext, publisher);
        MessageContext messageContext = new MessageContext(dbEventMessage, MessageType.DB_EVENT, "", "");
        getDBEventPublisher().publish(messageContext, DBEventManagerConstants.getRoutingKey(DBEventService.DB_EVENT.toString()));
    }
    return true;
}
Also used : DBEventMessage(org.apache.airavata.model.dbevent.DBEventMessage) DBEventMessageContext(org.apache.airavata.model.dbevent.DBEventMessageContext) DBEventSubscriber(org.apache.airavata.model.dbevent.DBEventSubscriber) MessageContext(org.apache.airavata.messaging.core.MessageContext) DBEventMessageContext(org.apache.airavata.model.dbevent.DBEventMessageContext)

Example 4 with MessageContext

use of org.apache.airavata.messaging.core.MessageContext in project airavata by apache.

the class StatusConsumer method handleDelivery.

@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
    Message message = new Message();
    try {
        ThriftUtils.createThriftFromBytes(body, message);
        TBase event = null;
        String gatewayId = null;
        if (message.getMessageType().equals(MessageType.EXPERIMENT)) {
            ExperimentStatusChangeEvent experimentStatusChangeEvent = new ExperimentStatusChangeEvent();
            ThriftUtils.createThriftFromBytes(message.getEvent(), experimentStatusChangeEvent);
            log.debug(" Message Received with message id '" + message.getMessageId() + "' and with message type '" + message.getMessageType() + "'  with status " + experimentStatusChangeEvent.getState());
            event = experimentStatusChangeEvent;
            gatewayId = experimentStatusChangeEvent.getGatewayId();
        } else if (message.getMessageType().equals(MessageType.PROCESS)) {
            ProcessStatusChangeEvent processStatusChangeEvent = new ProcessStatusChangeEvent();
            ThriftUtils.createThriftFromBytes(message.getEvent(), processStatusChangeEvent);
            log.debug("Message Recieved with message id :" + message.getMessageId() + " and with " + "message type " + message.getMessageType() + " with status " + processStatusChangeEvent.getState());
            event = processStatusChangeEvent;
            gatewayId = processStatusChangeEvent.getProcessIdentity().getGatewayId();
        } else if (message.getMessageType().equals(MessageType.TASK)) {
            TaskStatusChangeEvent taskStatusChangeEvent = new TaskStatusChangeEvent();
            ThriftUtils.createThriftFromBytes(message.getEvent(), taskStatusChangeEvent);
            log.debug(" Message Received with message id '" + message.getMessageId() + "' and with message type '" + message.getMessageType() + "'  with status " + taskStatusChangeEvent.getState());
            event = taskStatusChangeEvent;
            gatewayId = taskStatusChangeEvent.getTaskIdentity().getGatewayId();
        } else if (message.getMessageType() == MessageType.PROCESSOUTPUT) {
            TaskOutputChangeEvent taskOutputChangeEvent = new TaskOutputChangeEvent();
            ThriftUtils.createThriftFromBytes(message.getEvent(), taskOutputChangeEvent);
            log.debug(" Message Received with message id '" + message.getMessageId() + "' and with message type '" + message.getMessageType());
            event = taskOutputChangeEvent;
            gatewayId = taskOutputChangeEvent.getTaskIdentity().getGatewayId();
        } else if (message.getMessageType().equals(MessageType.JOB)) {
            JobStatusChangeEvent jobStatusChangeEvent = new JobStatusChangeEvent();
            ThriftUtils.createThriftFromBytes(message.getEvent(), jobStatusChangeEvent);
            log.debug(" Message Received with message id '" + message.getMessageId() + "' and with message type '" + message.getMessageType() + "'  with status " + jobStatusChangeEvent.getState());
            event = jobStatusChangeEvent;
            gatewayId = jobStatusChangeEvent.getJobIdentity().getGatewayId();
        } else if (message.getMessageType().equals(MessageType.LAUNCHPROCESS)) {
            TaskSubmitEvent taskSubmitEvent = new TaskSubmitEvent();
            ThriftUtils.createThriftFromBytes(message.getEvent(), taskSubmitEvent);
            log.debug(" Message Received with message id '" + message.getMessageId() + "' and with message type '" + message.getMessageType() + "'  for experimentId: " + taskSubmitEvent.getExperimentId() + "and taskId: " + taskSubmitEvent.getTaskId());
            event = taskSubmitEvent;
            gatewayId = taskSubmitEvent.getGatewayId();
        } else if (message.getMessageType().equals(MessageType.TERMINATEPROCESS)) {
            TaskTerminateEvent taskTerminateEvent = new TaskTerminateEvent();
            ThriftUtils.createThriftFromBytes(message.getEvent(), taskTerminateEvent);
            log.debug(" Message Received with message id '" + message.getMessageId() + "' and with message type '" + message.getMessageType() + "'  for experimentId: " + taskTerminateEvent.getExperimentId() + "and taskId: " + taskTerminateEvent.getTaskId());
            event = taskTerminateEvent;
            gatewayId = null;
        }
        MessageContext messageContext = new MessageContext(event, message.getMessageType(), message.getMessageId(), gatewayId);
        messageContext.setUpdatedTime(AiravataUtils.getTime(message.getUpdatedTime()));
        messageContext.setIsRedeliver(envelope.isRedeliver());
        handler.onMessage(messageContext);
    } catch (TException e) {
        String msg = "Failed to de-serialize the thrift message, from routing keys: " + envelope.getRoutingKey();
        log.warn(msg, e);
    }
}
Also used : TException(org.apache.thrift.TException) TaskStatusChangeEvent(org.apache.airavata.model.messaging.event.TaskStatusChangeEvent) Message(org.apache.airavata.model.messaging.event.Message) TaskOutputChangeEvent(org.apache.airavata.model.messaging.event.TaskOutputChangeEvent) ExperimentStatusChangeEvent(org.apache.airavata.model.messaging.event.ExperimentStatusChangeEvent) JobStatusChangeEvent(org.apache.airavata.model.messaging.event.JobStatusChangeEvent) ProcessStatusChangeEvent(org.apache.airavata.model.messaging.event.ProcessStatusChangeEvent) TBase(org.apache.thrift.TBase) MessageContext(org.apache.airavata.messaging.core.MessageContext) TaskSubmitEvent(org.apache.airavata.model.messaging.event.TaskSubmitEvent) TaskTerminateEvent(org.apache.airavata.model.messaging.event.TaskTerminateEvent)

Example 5 with MessageContext

use of org.apache.airavata.messaging.core.MessageContext in project airavata by apache.

the class ProfileServiceUtils method getDBEventMessageContext.

/**
 * Constructs the dbEventMessageContext
 * @param entityType
 * @param crudType
 * @param entityModel
 * @return
 * @throws AiravataException
 */
public static MessageContext getDBEventMessageContext(EntityType entityType, CrudType crudType, TBase entityModel) throws AiravataException {
    try {
        // set the publisherContext
        DBEventMessage dbEventMessage = new DBEventMessage();
        DBEventPublisherContext publisherContext = new DBEventPublisherContext();
        publisherContext.setCrudType(crudType);
        publisherContext.setEntityDataModel(ThriftUtils.serializeThriftObject(entityModel));
        publisherContext.setEntityType(entityType);
        // create dbEventPublisher with publisherContext
        DBEventPublisher dbEventPublisher = new DBEventPublisher();
        dbEventPublisher.setPublisherContext(publisherContext);
        // set messageContext to dbEventPublisher
        DBEventMessageContext dbMessageContext = DBEventMessageContext.publisher(dbEventPublisher);
        // set dbEventMessage with messageContext
        dbEventMessage.setDbEventType(DBEventType.PUBLISHER);
        dbEventMessage.setPublisherService(DBEventManagerConstants.getDbEventServiceName(entityType));
        dbEventMessage.setMessageContext(dbMessageContext);
        // construct and return messageContext
        return new MessageContext(dbEventMessage, MessageType.DB_EVENT, "", "");
    } catch (Exception ex) {
        throw new AiravataException(ex.getMessage(), ex);
    }
}
Also used : MessageContext(org.apache.airavata.messaging.core.MessageContext) AiravataException(org.apache.airavata.common.exception.AiravataException) AiravataException(org.apache.airavata.common.exception.AiravataException)

Aggregations

MessageContext (org.apache.airavata.messaging.core.MessageContext)20 TException (org.apache.thrift.TException)9 AiravataException (org.apache.airavata.common.exception.AiravataException)6 ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)6 DBEventMessage (org.apache.airavata.model.dbevent.DBEventMessage)4 DBEventMessageContext (org.apache.airavata.model.dbevent.DBEventMessageContext)4 Message (org.apache.airavata.model.messaging.event.Message)4 URISyntaxException (java.net.URISyntaxException)3 UnknownHostException (java.net.UnknownHostException)3 ExperimentStatusChangeEvent (org.apache.airavata.model.messaging.event.ExperimentStatusChangeEvent)3 ExperimentSubmitEvent (org.apache.airavata.model.messaging.event.ExperimentSubmitEvent)3 TBase (org.apache.thrift.TBase)3 Timestamp (java.sql.Timestamp)2 CredentialReader (org.apache.airavata.credential.store.store.CredentialReader)2 DBEventSubscriber (org.apache.airavata.model.dbevent.DBEventSubscriber)2 ProcessStatusChangeEvent (org.apache.airavata.model.messaging.event.ProcessStatusChangeEvent)2 ProcessSubmitEvent (org.apache.airavata.model.messaging.event.ProcessSubmitEvent)2 OrchestratorException (org.apache.airavata.orchestrator.core.exception.OrchestratorException)2 RegistryException (org.apache.airavata.registry.cpi.RegistryException)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1