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