From a2752c6207a06b5aa7a1388508680902a45999df Mon Sep 17 00:00:00 2001 From: kishan Date: Fri, 14 Jan 2011 03:48:09 +0530 Subject: [PATCH] bug 7842: Move events to service layer from http api --- api/src/com/cloud/event/ActionEvent.java | 2 ++ api/src/com/cloud/user/UserContext.java | 14 ++++++-- .../xen/resource/CitrixResourceBase.java | 1 + server/src/com/cloud/api/ApiServer.java | 10 +++--- .../com/cloud/event/ActionEventCallback.java | 33 ++++++++----------- server/src/com/cloud/event/EventUtils.java | 3 +- .../cloud/server/ManagementServerImpl.java | 2 +- .../com/cloud/storage/StorageManagerImpl.java | 4 +-- 8 files changed, 39 insertions(+), 30 deletions(-) diff --git a/api/src/com/cloud/event/ActionEvent.java b/api/src/com/cloud/event/ActionEvent.java index 1a705babbf7..f489e380cb2 100644 --- a/api/src/com/cloud/event/ActionEvent.java +++ b/api/src/com/cloud/event/ActionEvent.java @@ -28,4 +28,6 @@ import java.lang.annotation.Target; @Retention(RUNTIME) public @interface ActionEvent { boolean create() default false; + String eventType(); + String eventDescription(); } diff --git a/api/src/com/cloud/user/UserContext.java b/api/src/com/cloud/user/UserContext.java index ae35844718c..1a48d159bd5 100644 --- a/api/src/com/cloud/user/UserContext.java +++ b/api/src/com/cloud/user/UserContext.java @@ -30,7 +30,8 @@ public class UserContext { private long userId; private String sessionId; - private Account account; + private Account account; + private long startEventId = 0; private boolean apiServer; @@ -101,5 +102,14 @@ public class UserContext { public static void unregisterContext() { s_currentContext.set(null); - } + } + + public void setStartEventId(long startEventId) { + this.startEventId = startEventId; + } + + public long getStartEventId() { + return startEventId; + } + } diff --git a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java index c385cea725b..621a78e3784 100644 --- a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java +++ b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java @@ -933,6 +933,7 @@ public abstract class CitrixResourceBase implements ServerResource { for (NicTO nic : nics) { if(nic.getType() == TrafficType.Control){ networkUsage(conn, nic.getIp(), "create", null); + break; } } } diff --git a/server/src/com/cloud/api/ApiServer.java b/server/src/com/cloud/api/ApiServer.java index 7c4f00831b9..36b18417ccd 100755 --- a/server/src/com/cloud/api/ApiServer.java +++ b/server/src/com/cloud/api/ApiServer.java @@ -386,12 +386,14 @@ public class ApiServer implements HttpRequestHandler { params.put("ctxAccountId", String.valueOf(account.getId())); } + long startEventId = ctx.getStartEventId(); + // save the scheduled event Long eventId = EventUtils.saveScheduledEvent((userId == null) ? User.UID_SYSTEM : userId, asyncCmd.getEntityOwnerId(), - asyncCmd.getEventType(), asyncCmd.getEventDescription()); - - if (eventId != null) { - params.put("starteventid", eventId.toString()); + asyncCmd.getEventType(), asyncCmd.getEventDescription(), startEventId); + + if(startEventId == 0){ + ctx.setStartEventId(eventId); } AsyncJobVO job = new AsyncJobVO(); diff --git a/server/src/com/cloud/event/ActionEventCallback.java b/server/src/com/cloud/event/ActionEventCallback.java index 1c8f8d93759..65da7e6a8b0 100644 --- a/server/src/com/cloud/event/ActionEventCallback.java +++ b/server/src/com/cloud/event/ActionEventCallback.java @@ -24,7 +24,6 @@ import net.sf.cglib.proxy.Callback; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; -import com.cloud.api.BaseAsyncCmd; import com.cloud.user.Account; import com.cloud.user.User; import com.cloud.user.UserContext; @@ -40,28 +39,20 @@ public class ActionEventCallback implements MethodInterceptor, AnnotationInterce @Override public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { - if(args.length > 0){ - if((args[0] != null) && (args[0] instanceof BaseAsyncCmd)){ - UserContext ctx = UserContext.current(); - Long userID = ctx.getCallerUserId(); - userId = (userID == null) ? User.UID_SYSTEM : userID; - BaseAsyncCmd cmd = (BaseAsyncCmd)args[0]; - eventType = cmd.getEventType(); - accountId = cmd.getEntityOwnerId(); - description = cmd.getEventDescription(); - Long startEventID = cmd.getStartEventId(); - if(startEventID == null){ - startEventId = 0; - } - } - } - EventVO event = null; ActionEvent actionEvent = method.getAnnotation(ActionEvent.class); + EventVO event = null; if (actionEvent != null) { create = actionEvent.create(); - } - if(!create){ - event = interceptStart(method); + UserContext ctx = UserContext.current(); + Long userID = ctx.getCallerUserId(); + userId = (userID == null) ? User.UID_SYSTEM : userID; + eventType = actionEvent.eventType(); + description = actionEvent.eventDescription(); + startEventId = ctx.getStartEventId(); + + if(!create){ + event = interceptStart(method); + } } try { return methodProxy.invokeSuper(object, args); @@ -114,6 +105,8 @@ public class ActionEventCallback implements MethodInterceptor, AnnotationInterce if(create){ //This start event has to be used for subsequent events of this action startEventId = EventUtils.saveCreatedEvent(userId, accountId, EventVO.LEVEL_INFO, eventType, "Successfully created entity for "+description); + UserContext ctx = UserContext.current(); + ctx.setStartEventId(startEventId); } else { EventUtils.saveEvent(userId, accountId, EventVO.LEVEL_INFO, eventType, "Successfully completed "+description, startEventId); } diff --git a/server/src/com/cloud/event/EventUtils.java b/server/src/com/cloud/event/EventUtils.java index 40fd1848843..02406407288 100755 --- a/server/src/com/cloud/event/EventUtils.java +++ b/server/src/com/cloud/event/EventUtils.java @@ -20,12 +20,13 @@ public class EventUtils { /* * Save event after scheduling an async job */ - public static Long saveScheduledEvent(Long userId, Long accountId, String type, String description) { + public static Long saveScheduledEvent(Long userId, Long accountId, String type, String description, long startEventId) { EventVO event = new EventVO(); event.setUserId(userId); event.setAccountId(accountId); event.setType(type); event.setState(Event.State.Scheduled); + event.setStartId(startEventId); event.setDescription("Scheduled async job for "+description); event = _eventDao.persist(event); return event.getId(); diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 536553b4211..12bcc34864d 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -2918,7 +2918,7 @@ public class ManagementServerImpl implements ManagementServer { } } - @Override @ActionEvent + @Override public boolean deleteDomain(DeleteDomainCmd cmd) throws InvalidParameterValueException, PermissionDeniedException { Account account = UserContext.current().getCaller(); Long domainId = cmd.getId(); diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index 0ed7b773c63..ea14bd96b6e 100755 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -1570,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 @ActionEvent (create=true) + @Override 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(); @@ -1728,7 +1728,7 @@ public class StorageManagerImpl implements StorageManager, StorageService, Manag return volume; } - @Override @DB @ActionEvent + @Override @DB public VolumeVO createVolume(CreateVolumeCmd cmd) { VolumeVO volume = _volsDao.findById(cmd.getEntityId()); // VolumeVO createdVolume = null;