bug 7842: Move events to service layer from http api

This commit is contained in:
kishan 2011-01-13 19:22:22 +05:30
parent 6fe42d144f
commit 388e689755
9 changed files with 34 additions and 53 deletions

View File

@ -17,5 +17,15 @@
*/
package com.cloud.event;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Target({TYPE, METHOD})
@Retention(RUNTIME)
public @interface ActionEvent {
boolean create() default false;
}

View File

@ -21,6 +21,7 @@ import java.util.Date;
public interface Event {
public enum State {
Created,
Scheduled,
Started,
Completed;

View File

@ -23,7 +23,7 @@
documented, please contact the author.
-->
<components.xml>
<interceptor library="com.cloud.configuration.DefaultIntercetorLibrary"/>
<interceptor library="com.cloud.configuration.DefaultInterceptorLibrary"/>
<management-server class="com.cloud.server.ManagementServerImpl" library="com.cloud.configuration.DefaultComponentLibrary">
<adapters key="com.cloud.agent.manager.allocator.HostAllocator">
<adapter name="FirstFitRouting" class="com.cloud.agent.manager.allocator.impl.RecreateHostAllocator"/>

View File

@ -64,56 +64,41 @@ public class ApiDispatcher {
public void dispatchCreateCmd(BaseAsyncCreateCmd cmd, Map<String, String> params) {
boolean created = false;
String errorMsg = "";
long startId = 0;
if(cmd.getCreateEventType() != null){
startId = cmd.saveStartedEvent(cmd.getCreateEventType(), cmd.getCreateEventDescription(), 0L);
}
setupParameters(cmd, params);
try {
cmd.create();
created = true;
} catch (Throwable t) {
if (t instanceof InvalidParameterValueException || t instanceof IllegalArgumentException) {
s_logger.info(t.getMessage());
errorMsg = "Parameter error";
throw new ServerApiException(BaseCmd.PARAM_ERROR, t.getMessage());
} else if (t instanceof PermissionDeniedException) {
s_logger.info("PermissionDenied: " + t.getMessage());
errorMsg = "Permission denied";
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, t.getMessage());
} else if (t instanceof AccountLimitException) {
s_logger.info(t.getMessage());
errorMsg = "Account resource limit error";
throw new ServerApiException(BaseCmd.ACCOUNT_RESOURCE_LIMIT_ERROR, t.getMessage());
}else if (t instanceof InsufficientCapacityException) {
s_logger.info(t.getMessage());
errorMsg = "Insufficient capacity";
throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, t.getMessage());
} else if (t instanceof ResourceAllocationException) {
s_logger.info(t.getMessage());
errorMsg = "Resource allocation error";
throw new ServerApiException(BaseCmd.RESOURCE_ALLOCATION_ERROR, t.getMessage());
} else if (t instanceof ResourceUnavailableException) {
s_logger.warn("Exception: ", t);
errorMsg = "Resource unavailable error";
throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, t.getMessage());
} else if (t instanceof AsyncCommandQueued) {
throw (AsyncCommandQueued)t;
} else if (t instanceof ServerApiException) {
s_logger.warn(t.getClass() + " : " + ((ServerApiException) t).getDescription());
errorMsg = ((ServerApiException) t).getDescription();
if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, errorMsg);
} else {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, BaseCmd.USER_ERROR_MESSAGE);
}
} else {
errorMsg = "Internal error";
s_logger.error("Exception while executing " + cmd.getClass().getSimpleName() + ":", t);
if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, t.getMessage());
@ -121,64 +106,42 @@ public class ApiDispatcher {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, BaseCmd.USER_ERROR_MESSAGE);
}
}
} finally {
if(cmd.getCreateEventType() != null){
if (created){
cmd.saveCompletedEvent(EventVO.LEVEL_INFO, cmd.getCreateEventType(), cmd.getCreateEventDescription()+" successfull. Id: "+cmd.getEntityId(), startId);
} else {
cmd.saveCompletedEvent(EventVO.LEVEL_ERROR, cmd.getCreateEventType(), cmd.getCreateEventDescription()+" failed. "+errorMsg, startId);
}
}
}
}
public void dispatch(BaseCmd cmd, Map<String, String> params) {
boolean success = false;
String errorMsg = "";
setupParameters(cmd, params);
try {
if(cmd instanceof BaseAsyncCmd){
((BaseAsyncCmd)cmd).saveStartedEvent();
}
cmd.execute();
success = true;
} catch (Throwable t) {
if (t instanceof InvalidParameterValueException || t instanceof IllegalArgumentException) {
s_logger.info(t.getMessage());
errorMsg = "Parameter error";
throw new ServerApiException(BaseCmd.PARAM_ERROR, t.getMessage());
} else if (t instanceof PermissionDeniedException) {
s_logger.info("PermissionDenied: " + t.getMessage());
errorMsg = "Permission denied";
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, t.getMessage());
} else if (t instanceof AccountLimitException) {
s_logger.info(t.getMessage());
errorMsg = "Account resource limit error";
throw new ServerApiException(BaseCmd.ACCOUNT_RESOURCE_LIMIT_ERROR, t.getMessage());
} else if (t instanceof InsufficientCapacityException) {
s_logger.info(t.getMessage());
errorMsg = "Insufficient capacity";
throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, t.getMessage());
} else if (t instanceof ResourceAllocationException) {
s_logger.warn("Exception: ", t);
errorMsg = "Resource allocation error";
throw new ServerApiException(BaseCmd.RESOURCE_ALLOCATION_ERROR, t.getMessage());
} else if (t instanceof ResourceUnavailableException) {
s_logger.warn("Exception: ", t);
errorMsg = "Resource unavailable error";
throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, t.getMessage());
} else if (t instanceof AsyncCommandQueued) {
throw (AsyncCommandQueued)t;
} else if (t instanceof ServerApiException) {
errorMsg = ((ServerApiException) t).getDescription();
s_logger.warn(t.getClass() + " : " + ((ServerApiException) t).getDescription());
if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, errorMsg);
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ((ServerApiException) t).getDescription());
} else {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, BaseCmd.USER_ERROR_MESSAGE);
}
} else {
errorMsg = "Internal error";
s_logger.error("Exception while executing " + cmd.getClass().getSimpleName() + ":", t);
if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, t.getMessage());
@ -186,15 +149,6 @@ public class ApiDispatcher {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, BaseCmd.USER_ERROR_MESSAGE);
}
}
} finally {
if(cmd instanceof BaseAsyncCmd){
BaseAsyncCmd asyncCmd = (BaseAsyncCmd)cmd;
if(success){
asyncCmd.saveCompletedEvent(EventVO.LEVEL_INFO, asyncCmd.getEventDescription()+" completed successfully");
} else {
asyncCmd.saveCompletedEvent(EventVO.LEVEL_ERROR, asyncCmd.getEventDescription()+" failed. "+errorMsg);
}
}
}
}

View File

@ -19,6 +19,7 @@ package com.cloud.configuration;
import java.util.List;
import com.cloud.event.ActionEventCallback;
import com.cloud.utils.component.AnnotationInterceptor;
import com.cloud.utils.component.InterceptorLibrary;
import com.cloud.utils.db.DatabaseCallback;
@ -28,5 +29,6 @@ public class DefaultInterceptorLibrary implements InterceptorLibrary {
@Override
public void addInterceptors(List<AnnotationInterceptor<?>> interceptors) {
interceptors.add(new DatabaseCallback());
interceptors.add(new ActionEventCallback());
}
}

View File

@ -40,7 +40,7 @@ public class EventUtils {
event.setAccountId(accountId);
event.setType(type);
event.setState(Event.State.Started);
event.setDescription(description);
event.setDescription("Starting job for "+description);
event.setStartId(startEventId);
event = _eventDao.persist(event);
return event.getId();
@ -104,4 +104,16 @@ public class EventUtils {
event = _eventDao.persist(event);
return event.getId();
}
public static Long saveCreatedEvent(Long userId, Long accountId, String level, String type, String description) {
EventVO event = new EventVO();
event.setUserId(userId);
event.setAccountId(accountId);
event.setType(type);
event.setLevel(level);
event.setState(Event.State.Created);
event.setDescription(description);
event = _eventDao.persist(event);
return event.getId();
}
}

View File

@ -163,6 +163,7 @@ import com.cloud.dc.dao.PodVlanMapDao;
import com.cloud.dc.dao.VlanDao;
import com.cloud.domain.DomainVO;
import com.cloud.domain.dao.DomainDao;
import com.cloud.event.ActionEvent;
import com.cloud.event.Event;
import com.cloud.event.EventTypes;
import com.cloud.event.EventUtils;
@ -2917,7 +2918,7 @@ public class ManagementServerImpl implements ManagementServer {
}
}
@Override
@Override @ActionEvent
public boolean deleteDomain(DeleteDomainCmd cmd) throws InvalidParameterValueException, PermissionDeniedException {
Account account = UserContext.current().getCaller();
Long domainId = cmd.getId();

View File

@ -89,6 +89,7 @@ import com.cloud.dc.dao.HostPodDao;
import com.cloud.deploy.DeployDestination;
import com.cloud.domain.Domain;
import com.cloud.domain.dao.DomainDao;
import com.cloud.event.ActionEvent;
import com.cloud.event.Event;
import com.cloud.event.EventTypes;
import com.cloud.event.EventUtils;
@ -1569,7 +1570,7 @@ public class StorageManagerImpl implements StorageManager, StorageService, Manag
/*Just allocate a volume in the database, don't send the createvolume cmd to hypervisor. The volume will be finally created only when it's attached to a VM.*/
@Override
@Override @ActionEvent (create=true)
public VolumeVO allocVolume(CreateVolumeCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, ResourceAllocationException {
// FIXME: some of the scheduled event stuff might be missing here...
Account account = UserContext.current().getCaller();
@ -1727,7 +1728,7 @@ public class StorageManagerImpl implements StorageManager, StorageService, Manag
return volume;
}
@Override @DB
@Override @DB @ActionEvent
public VolumeVO createVolume(CreateVolumeCmd cmd) {
VolumeVO volume = _volsDao.findById(cmd.getEntityId());
// VolumeVO createdVolume = null;

View File

@ -853,7 +853,7 @@ public class ComponentLocator implements ComponentLocatorMBean {
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
throws SAXException {
if (qName.equals("interceptors") && s_interceptors.size() == 0) {
if (qName.equals("interceptor") && s_interceptors.size() == 0) {
synchronized(s_interceptors){
if (s_interceptors.size() == 0) {
String libraryName = getAttribute(atts, "library");