cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kelv...@apache.org
Subject [2/2] git commit: updated refs/heads/vmsync to a8a09ba
Date Mon, 06 May 2013 17:02:18 GMT
hook-up new sync with VirtualMachineGuru(s)


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/a8a09ba2
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/a8a09ba2
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/a8a09ba2

Branch: refs/heads/vmsync
Commit: a8a09ba2879b82b51b3d96eb84288d2380cf6c06
Parents: ba9a948
Author: Kelven Yang <kelveny@gmail.com>
Authored: Mon May 6 10:01:46 2013 -0700
Committer: Kelven Yang <kelveny@gmail.com>
Committed: Mon May 6 10:01:46 2013 -0700

----------------------------------------------------------------------
 api/src/com/cloud/async/AsyncJob.java              |    7 +-
 core/src/com/cloud/async/AsyncJobJournalVO.java    |  108 +++++
 core/src/com/cloud/async/AsyncJobVO.java           |   12 -
 .../src/com/cloud/serializer/SerializerHelper.java |   47 ++-
 .../network/lb/ElasticLoadBalancerManagerImpl.java |    9 +
 .../com/cloud/async/AsyncJobExecutionContext.java  |   25 ++
 server/src/com/cloud/async/AsyncJobManager.java    |    2 +
 .../src/com/cloud/async/AsyncJobManagerImpl.java   |   38 ++-
 server/src/com/cloud/async/AsyncJobMonitor.java    |    6 +-
 .../src/com/cloud/async/dao/AsyncJobDaoImpl.java   |    1 -
 .../com/cloud/async/dao/AsyncJobJournalDao.java    |   26 ++
 .../cloud/async/dao/AsyncJobJournalDaoImpl.java    |   44 ++
 .../com/cloud/cluster/dao/StackMaidDaoImpl.java    |    2 +-
 .../AgentBasedConsoleProxyManager.java             |    9 +
 .../consoleproxy/ConsoleProxyManagerImpl.java      |   34 ++
 .../router/VirtualNetworkApplianceManagerImpl.java |    9 +
 .../secondary/SecondaryStorageManagerImpl.java     |   11 +-
 server/src/com/cloud/vm/UserVmManagerImpl.java     |    8 +
 server/src/com/cloud/vm/VirtualMachineGuru.java    |    7 +-
 server/src/com/cloud/vm/VirtualMachineManager.java |    7 +
 .../com/cloud/vm/VirtualMachineManagerImpl.java    |  323 ++++++++++++---
 .../com/cloud/vm/VirtualMachinePowerStateSync.java |    6 +
 .../cloud/vm/VirtualMachinePowerStateSyncImpl.java |   96 ++++--
 server/src/com/cloud/vm/VmWorkConstants.java       |    4 +-
 server/src/com/cloud/vm/VmWorkJobDispatcher.java   |   27 +-
 .../cloudstack/messagebus/SubjectConstants.java    |   26 --
 .../cloudstack/messagebus/TopicConstants.java      |   26 ++
 .../com/cloud/async/AsyncJobTestConfiguration.java |    7 +
 .../cloud/async/MockVirtualMachineManagerImpl.java |   14 +-
 .../test/com/cloud/async/TestAsyncJobManager.java  |   59 +++-
 .../test/com/cloud/vm/MockUserVmManagerImpl.java   |    8 +
 .../cloud/vm/VirtualMachineManagerImplTest.java    |    2 +
 .../vm/VmWorkMockVirtualMachineManagerImpl.java    |   17 +-
 server/test/com/cloud/vm/VmWorkTest.java           |   15 +
 .../test/com/cloud/vm/VmWorkTestConfiguration.java |    7 +
 setup/db/db/schema-410to420.sql                    |   11 +-
 36 files changed, 896 insertions(+), 164 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/api/src/com/cloud/async/AsyncJob.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/async/AsyncJob.java b/api/src/com/cloud/async/AsyncJob.java
index c8dc642..0d61f92 100644
--- a/api/src/com/cloud/async/AsyncJob.java
+++ b/api/src/com/cloud/async/AsyncJob.java
@@ -22,8 +22,11 @@ import org.apache.cloudstack.api.InternalIdentity;
 import java.util.Date;
 
 public interface AsyncJob extends Identity, InternalIdentity {
-    Long getParentId();
-    
+	
+	public enum JournalType { 
+		SUCCESS, FAILURE 
+	};
+	
     String getType();
     
     String getDispatcher();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/core/src/com/cloud/async/AsyncJobJournalVO.java
----------------------------------------------------------------------
diff --git a/core/src/com/cloud/async/AsyncJobJournalVO.java b/core/src/com/cloud/async/AsyncJobJournalVO.java
new file mode 100644
index 0000000..1cf57e9
--- /dev/null
+++ b/core/src/com/cloud/async/AsyncJobJournalVO.java
@@ -0,0 +1,108 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.async;
+
+import java.util.Date;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import com.cloud.utils.DateUtil;
+import com.cloud.utils.db.GenericDao;
+
+@Entity
+@Table(name="async_job_journal")
+public class AsyncJobJournalVO {
+	@Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name="id")
+    private Long id = null;
+
+    @Column(name="job_id")
+	private long jobId;
+    
+    @Column(name="journal_type", updatable=false, nullable=false, length=32)
+    @Enumerated(value=EnumType.STRING)
+    private AsyncJob.JournalType journalType;
+    
+    @Column(name="journal_text", length=1024)
+    private String journalText;
+
+    @Column(name="journal_obj", length=1024)
+    private String journalObjJsonString;
+    
+    @Column(name=GenericDao.CREATED_COLUMN)
+    protected Date created;
+
+    public AsyncJobJournalVO() {
+    	created = DateUtil.currentGMTTime();
+    }
+    
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public long getJobId() {
+		return jobId;
+	}
+
+	public void setJobId(long jobId) {
+		this.jobId = jobId;
+	}
+
+	public AsyncJob.JournalType getJournalType() {
+		return journalType;
+	}
+
+	public void setJournalType(AsyncJob.JournalType journalType) {
+		this.journalType = journalType;
+	}
+
+	public String getJournalText() {
+		return journalText;
+	}
+
+	public void setJournalText(String journalText) {
+		this.journalText = journalText;
+	}
+
+	public String getJournalObjJsonString() {
+		return journalObjJsonString;
+	}
+
+	public void setJournalObjJsonString(String journalObjJsonString) {
+		this.journalObjJsonString = journalObjJsonString;
+	}
+
+	public Date getCreated() {
+		return created;
+	}
+
+	public void setCreated(Date created) {
+		this.created = created;
+	}
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/core/src/com/cloud/async/AsyncJobVO.java
----------------------------------------------------------------------
diff --git a/core/src/com/cloud/async/AsyncJobVO.java b/core/src/com/cloud/async/AsyncJobVO.java
index 53d2e1e..bfb2416 100644
--- a/core/src/com/cloud/async/AsyncJobVO.java
+++ b/core/src/com/cloud/async/AsyncJobVO.java
@@ -46,9 +46,6 @@ public class AsyncJobVO implements AsyncJob {
     @Column(name="id")
     private Long id = null;
 	
-    @Column(name="parent_id")
-    private Long parentId;
-
     @Column(name="job_type", length=32)
     protected String type;
     
@@ -143,15 +140,6 @@ public class AsyncJobVO implements AsyncJob {
 	}
 	
 	@Override
-	public Long getParentId() {
-		return this.parentId;
-	}
-	
-	public void setParentId(Long parentId) {
-		this.parentId = parentId;
-	}
-	
-	@Override
 	public String getType() {
 		return this.type;
 	}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/core/src/com/cloud/serializer/SerializerHelper.java
----------------------------------------------------------------------
diff --git a/core/src/com/cloud/serializer/SerializerHelper.java b/core/src/com/cloud/serializer/SerializerHelper.java
index dd858b7..e3449f6 100644
--- a/core/src/com/cloud/serializer/SerializerHelper.java
+++ b/core/src/com/cloud/serializer/SerializerHelper.java
@@ -16,6 +16,12 @@
 // under the License.
 package com.cloud.serializer;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -24,6 +30,7 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
+import org.apache.commons.codec.binary.Base64;
 import org.apache.log4j.Logger;
 
 import com.cloud.utils.DateUtil;
@@ -37,7 +44,7 @@ public class SerializerHelper {
     public static final Logger s_logger = Logger.getLogger(SerializerHelper.class.getName());
     public static String token = "/";
 
-    public static String toSerializedStringOld(Object result) {
+    public static String toSerializedString(Object result) {
         if(result != null) {
             Class<?> clz = result.getClass();
             Gson gson = GsonHelper.getGson();
@@ -83,6 +90,44 @@ public class SerializerHelper {
             throw e;
         }
     }
+    
+    public static String toObjectSerializedString(Serializable object) {
+    	assert(object != null);
+    	
+    	ByteArrayOutputStream bs = new ByteArrayOutputStream();
+    	try {
+    		ObjectOutputStream os = new ObjectOutputStream(bs);
+    		os.writeObject(object);
+    		os.close();
+    		bs.close();
+    		
+    		return Base64.encodeBase64URLSafeString(bs.toByteArray());
+    	} catch(IOException e) {
+    		s_logger.error("Unexpected exception", e);
+    	}
+    	return null;
+    }
+    
+    public static Object fromObjectSerializedString(String base64EncodedString) {
+    	if(base64EncodedString == null)
+    		return null;
+    	
+    	byte[] content = Base64.decodeBase64(base64EncodedString);
+    	ByteArrayInputStream bs = new ByteArrayInputStream(content);
+    	try {
+    		ObjectInputStream is = new ObjectInputStream(bs);
+    		Object obj = is.readObject();
+    		is.close();
+    		bs.close();
+    		return obj;
+    	} catch(IOException e) {
+    		s_logger.error("Unexpected exception", e);
+    	} catch(ClassNotFoundException e) {
+    		s_logger.error("Unexpected exception", e);
+    	}
+    	
+    	return null;
+    }
 
     public static List<Pair<String, Object>> toPairList(Object o, String name) {
         List<Pair<String, Object>> l = new ArrayList<Pair<String, Object>>();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java b/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java
index 283b517..0c5f972 100644
--- a/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java
+++ b/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java
@@ -131,6 +131,7 @@ import com.cloud.vm.NicProfile;
 import com.cloud.vm.ReservationContext;
 import com.cloud.vm.VMInstanceVO;
 import com.cloud.vm.VirtualMachine;
+import com.cloud.vm.VmWork;
 import com.cloud.vm.VirtualMachine.State;
 import com.cloud.vm.VirtualMachineGuru;
 import com.cloud.vm.VirtualMachineManager;
@@ -1001,5 +1002,13 @@ ElasticLoadBalancerManager, VirtualMachineGuru<DomainRouterVO> {
     @Override
     public void prepareStop(VirtualMachineProfile<DomainRouterVO> profile) {
     } 
+    
+    @Override
+    public void vmWorkStart(VmWork work) {
+    }
+    
+    @Override
+    public void vmWorkStop(VmWork work) {
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/async/AsyncJobExecutionContext.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/async/AsyncJobExecutionContext.java b/server/src/com/cloud/async/AsyncJobExecutionContext.java
index f71a391..c027975 100644
--- a/server/src/com/cloud/async/AsyncJobExecutionContext.java
+++ b/server/src/com/cloud/async/AsyncJobExecutionContext.java
@@ -16,9 +16,13 @@
 // under the License.
 package com.cloud.async;
 
+import javax.inject.Inject;
+
 public class AsyncJobExecutionContext  {
 	private AsyncJob _job;
 	
+	@Inject private  AsyncJobManager _jobMgr; 
+	
 	private static ThreadLocal<AsyncJobExecutionContext> s_currentExectionContext = new ThreadLocal<AsyncJobExecutionContext>();
 
 	public AsyncJobExecutionContext() {
@@ -40,6 +44,27 @@ public class AsyncJobExecutionContext  {
 		_job = job;
 	}
 	
+    public void completeAsyncJob(int jobStatus, int resultCode, Object resultObject) {
+    	assert(_job != null);
+    	_jobMgr.completeAsyncJob(_job.getId(), jobStatus, resultCode, resultObject);
+    }
+    
+    public void updateAsyncJobStatus(int processStatus, Object resultObject) {
+    	assert(_job != null);
+    	_jobMgr.updateAsyncJobStatus(_job.getId(), processStatus, resultObject);
+    }
+    
+    public void updateAsyncJobAttachment(String instanceType, Long instanceId) {
+    	assert(_job != null);
+    	_jobMgr.updateAsyncJobAttachment(_job.getId(), instanceType, instanceId);
+    }
+	
+	public void logJobJournal(AsyncJob.JournalType journalType, String 
+	    journalText, String journalObjJson) {
+		assert(_job != null);
+		_jobMgr.logJobJournal(_job.getId(), journalType, journalText, journalObjJson);
+	}
+	
 	public static AsyncJobExecutionContext getCurrentExecutionContext() {
 		return s_currentExectionContext.get();
 	}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/async/AsyncJobManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/async/AsyncJobManager.java b/server/src/com/cloud/async/AsyncJobManager.java
index fc8de18..8cafa5f 100644
--- a/server/src/com/cloud/async/AsyncJobManager.java
+++ b/server/src/com/cloud/async/AsyncJobManager.java
@@ -37,6 +37,8 @@ public interface AsyncJobManager extends Manager {
     public void completeAsyncJob(long jobId, int jobStatus, int resultCode, Object resultObject);
     public void updateAsyncJobStatus(long jobId, int processStatus, Object resultObject);
     public void updateAsyncJobAttachment(long jobId, String instanceType, Long instanceId);
+    public void logJobJournal(long jobId, AsyncJob.JournalType journalType, String 
+    	journalText, String journalObjJson);
    
     public void releaseSyncSource();
     public void syncAsyncJobExecution(AsyncJob job, String syncObjType, long syncObjId, long queueSizeLimit);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/async/AsyncJobManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/async/AsyncJobManagerImpl.java b/server/src/com/cloud/async/AsyncJobManagerImpl.java
index f091d5d..468782e 100644
--- a/server/src/com/cloud/async/AsyncJobManagerImpl.java
+++ b/server/src/com/cloud/async/AsyncJobManagerImpl.java
@@ -43,6 +43,7 @@ import org.apache.log4j.NDC;
 
 import com.cloud.api.ApiSerializerHelper;
 import com.cloud.async.dao.AsyncJobDao;
+import com.cloud.async.dao.AsyncJobJournalDao;
 import com.cloud.cluster.ClusterManager;
 import com.cloud.cluster.ClusterManagerListener;
 import com.cloud.cluster.ManagementServerHostVO;
@@ -58,6 +59,7 @@ import com.cloud.utils.DateUtil;
 import com.cloud.utils.NumbersUtil;
 import com.cloud.utils.Predicate;
 import com.cloud.utils.PropertiesUtil;
+import com.cloud.utils.component.ComponentContext;
 import com.cloud.utils.component.ManagerBase;
 import com.cloud.utils.concurrency.NamedThreadFactory;
 import com.cloud.utils.db.DB;
@@ -82,6 +84,7 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
     @Inject private ClusterManager _clusterMgr;
     @Inject private AccountManager _accountMgr;
     @Inject private AsyncJobDao _jobDao;
+    @Inject private AsyncJobJournalDao _journalDao;
     @Inject private ConfigurationDao _configDao;
     @Inject private List<AsyncJobDispatcher> _jobDispatchers;
     @Inject private MessageBus _messageBus;
@@ -175,9 +178,9 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
                     ", resultCode: " + resultCode + ", result: " + resultObject);
         }
 
-        Transaction txt = Transaction.currentTxn();
+        Transaction txn = Transaction.currentTxn();
         try {
-            txt.start();
+            txn.start();
             AsyncJobVO job = _jobDao.findById(jobId);
             if(job == null) {
                 if(s_logger.isDebugEnabled()) {
@@ -185,9 +188,18 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
                             ", resultCode: " + resultCode + ", result: " + resultObject);
                 }
 
-                txt.rollback();
+                txn.rollback();
                 return;
             }
+            
+            if(job.getStatus() != AsyncJobResult.STATUS_IN_PROGRESS) {
+                if(s_logger.isDebugEnabled()) {
+                    s_logger.debug("job-" + jobId + " is already completed.");
+                }
+            	
+            	txn.rollback();
+            	return;
+            }
 
             job.setCompleteMsid(getMsid());
             job.setStatus(jobStatus);
@@ -203,10 +215,10 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
 
             job.setLastUpdated(DateUtil.currentGMTTime());
             _jobDao.update(jobId, job);
-            txt.commit();
+            txn.commit();
         } catch(Exception e) {
             s_logger.error("Unexpected exception while completing async job-" + jobId, e);
-            txt.rollback();
+            txn.rollback();
         }
     }
 
@@ -267,6 +279,18 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
         }
     }
     
+    @Override @DB
+    public void logJobJournal(long jobId, AsyncJob.JournalType journalType, String 
+        journalText, String journalObjJson) {
+    	AsyncJobJournalVO journal = new AsyncJobJournalVO();
+    	journal.setJobId(jobId);
+    	journal.setJournalType(journalType);
+    	journal.setJournalText(journalText);
+    	journal.setJournalObjJsonString(journalObjJson);
+    	
+    	_journalDao.persist(journal);
+    }
+    
     @Override
     public void syncAsyncJobExecution(AsyncJob job, String syncObjType, long syncObjId, long queueSizeLimit) {
         if(s_logger.isDebugEnabled()) {
@@ -420,7 +444,9 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
                         s_logger.warn("Unable to register active job " + job.getId() + " to JMX monitoring due to exception " + ExceptionUtil.toString(e));
                     }
                     
-                    AsyncJobExecutionContext.setCurrentExecutionContext(new AsyncJobExecutionContext(job));
+                    AsyncJobExecutionContext.setCurrentExecutionContext(
+                    	(AsyncJobExecutionContext)ComponentContext.inject(new AsyncJobExecutionContext(job))
+                    );
                     
                     // execute the job
                     if(s_logger.isDebugEnabled()) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/async/AsyncJobMonitor.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/async/AsyncJobMonitor.java b/server/src/com/cloud/async/AsyncJobMonitor.java
index 4208444..bd5b2cd 100644
--- a/server/src/com/cloud/async/AsyncJobMonitor.java
+++ b/server/src/com/cloud/async/AsyncJobMonitor.java
@@ -27,7 +27,7 @@ import javax.naming.ConfigurationException;
 import org.apache.cloudstack.framework.messagebus.MessageBus;
 import org.apache.cloudstack.framework.messagebus.MessageDispatcher;
 import org.apache.cloudstack.framework.messagebus.MessageHandler;
-import org.apache.cloudstack.messagebus.SubjectConstants;
+import org.apache.cloudstack.messagebus.TopicConstants;
 import org.apache.log4j.Logger;
 
 import com.cloud.utils.component.ManagerBase;
@@ -66,7 +66,7 @@ public class AsyncJobMonitor extends ManagerBase {
 		_inactivityWarningThresholdMs = thresholdMs;
 	}
 	
-	@MessageHandler(topic=SubjectConstants.JOB_HEARTBEAT)
+	@MessageHandler(topic=TopicConstants.JOB_HEARTBEAT)
 	public void onJobHeartbeatNotify(String subject, String senderAddress, Object args) {
 		if(args != null && args instanceof Long) {
 			synchronized(this) {
@@ -93,7 +93,7 @@ public class AsyncJobMonitor extends ManagerBase {
 	public boolean configure(String name, Map<String, Object> params)
 			throws ConfigurationException {
 		
-		_messageBus.subscribe(SubjectConstants.JOB_HEARTBEAT, MessageDispatcher.getDispatcher(this));
+		_messageBus.subscribe(TopicConstants.JOB_HEARTBEAT, MessageDispatcher.getDispatcher(this));
 		_timer.scheduleAtFixedRate(new TimerTask() {
 
 			@Override

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/async/dao/AsyncJobDaoImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/async/dao/AsyncJobDaoImpl.java b/server/src/com/cloud/async/dao/AsyncJobDaoImpl.java
index 0e51b3a..992b942 100644
--- a/server/src/com/cloud/async/dao/AsyncJobDaoImpl.java
+++ b/server/src/com/cloud/async/dao/AsyncJobDaoImpl.java
@@ -23,7 +23,6 @@ import java.util.List;
 
 import org.apache.log4j.Logger;
 
-import com.cloud.async.AsyncJob;
 import com.cloud.async.AsyncJobResult;
 import com.cloud.async.AsyncJobVO;
 import com.cloud.utils.db.DB;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/async/dao/AsyncJobJournalDao.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/async/dao/AsyncJobJournalDao.java b/server/src/com/cloud/async/dao/AsyncJobJournalDao.java
new file mode 100644
index 0000000..6cfbe93
--- /dev/null
+++ b/server/src/com/cloud/async/dao/AsyncJobJournalDao.java
@@ -0,0 +1,26 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.async.dao;
+
+import java.util.List;
+
+import com.cloud.async.AsyncJobJournalVO;
+import com.cloud.utils.db.GenericDao;
+
+public interface AsyncJobJournalDao extends GenericDao<AsyncJobJournalVO, Long> {
+	List<AsyncJobJournalVO> getJobJournal(long jobId);
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/async/dao/AsyncJobJournalDaoImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/async/dao/AsyncJobJournalDaoImpl.java b/server/src/com/cloud/async/dao/AsyncJobJournalDaoImpl.java
new file mode 100644
index 0000000..5dd0141
--- /dev/null
+++ b/server/src/com/cloud/async/dao/AsyncJobJournalDaoImpl.java
@@ -0,0 +1,44 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.async.dao;
+
+import java.util.List;
+
+import com.cloud.async.AsyncJobJournalVO;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria.Op;
+
+public class AsyncJobJournalDaoImpl extends GenericDaoBase<AsyncJobJournalVO, Long> implements AsyncJobJournalDao {
+
+	private final SearchBuilder<AsyncJobJournalVO> JobJournalSearch;	
+
+	public AsyncJobJournalDaoImpl() {
+		JobJournalSearch = createSearchBuilder();
+		JobJournalSearch.and("jobId", JobJournalSearch.entity().getJobId(), Op.EQ);
+		JobJournalSearch.done();
+	}
+	
+	@Override
+	public List<AsyncJobJournalVO> getJobJournal(long jobId) {
+		SearchCriteria<AsyncJobJournalVO> sc = JobJournalSearch.create();
+		sc.setParameters("jobId", jobId);
+		
+		return this.listBy(sc);
+	}
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/cluster/dao/StackMaidDaoImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/cluster/dao/StackMaidDaoImpl.java b/server/src/com/cloud/cluster/dao/StackMaidDaoImpl.java
index 243b00f..59d1a33 100644
--- a/server/src/com/cloud/cluster/dao/StackMaidDaoImpl.java
+++ b/server/src/com/cloud/cluster/dao/StackMaidDaoImpl.java
@@ -91,7 +91,7 @@ public class StackMaidDaoImpl extends GenericDaoBase<CheckPointVO, Long> impleme
 		delegateItem.setThreadId(Thread.currentThread().getId());
 		delegateItem.setSeq(seq);
 		delegateItem.setDelegate(delegateClzName);
-		delegateItem.setContext(SerializerHelper.toSerializedStringOld(context));
+		delegateItem.setContext(SerializerHelper.toSerializedString(context));
 		delegateItem.setCreated(DateUtil.currentGMTTime());
 		
 		super.persist(delegateItem);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/consoleproxy/AgentBasedConsoleProxyManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/consoleproxy/AgentBasedConsoleProxyManager.java b/server/src/com/cloud/consoleproxy/AgentBasedConsoleProxyManager.java
index 6f8575d..58cd660 100755
--- a/server/src/com/cloud/consoleproxy/AgentBasedConsoleProxyManager.java
+++ b/server/src/com/cloud/consoleproxy/AgentBasedConsoleProxyManager.java
@@ -59,6 +59,7 @@ import com.cloud.vm.VirtualMachineGuru;
 import com.cloud.vm.VirtualMachineManager;
 import com.cloud.vm.VirtualMachineName;
 import com.cloud.vm.VirtualMachineProfile;
+import com.cloud.vm.VmWork;
 import com.cloud.vm.dao.ConsoleProxyDao;
 import com.cloud.vm.dao.UserVmDao;
 import com.cloud.vm.dao.VMInstanceDao;
@@ -356,4 +357,12 @@ public class AgentBasedConsoleProxyManager extends ManagerBase implements Consol
     @Override
     public void prepareStop(VirtualMachineProfile<ConsoleProxyVO> profile) {
     }
+    
+    @Override
+    public void vmWorkStart(VmWork work) {
+    }
+    
+    @Override
+    public void vmWorkStop(VmWork work) {
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
index 1edd869..7ee889b 100755
--- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
+++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
@@ -55,11 +55,14 @@ import com.cloud.agent.api.to.NicTO;
 import com.cloud.agent.api.to.VirtualMachineTO;
 import com.cloud.agent.manager.Commands;
 import com.cloud.api.commands.DestroyConsoleProxyCmd;
+import com.cloud.async.AsyncJobExecutionContext;
+import com.cloud.async.AsyncJobResult;
 import com.cloud.certificate.dao.CertificateDao;
 import com.cloud.cluster.ClusterManager;
 import com.cloud.configuration.Config;
 import com.cloud.configuration.ZoneConfig;
 import com.cloud.configuration.dao.ConfigurationDao;
+import com.cloud.dao.EntityManager;
 import com.cloud.dc.DataCenter;
 import com.cloud.dc.DataCenter.NetworkType;
 import com.cloud.dc.DataCenterVO;
@@ -107,6 +110,7 @@ import com.cloud.resource.ResourceManager;
 import com.cloud.resource.ResourceStateAdapter;
 import com.cloud.resource.ServerResource;
 import com.cloud.resource.UnableDeleteHostException;
+import com.cloud.serializer.SerializerHelper;
 import com.cloud.service.ServiceOfferingVO;
 import com.cloud.service.dao.ServiceOfferingDao;
 import com.cloud.servlet.ConsoleProxyServlet;
@@ -121,8 +125,10 @@ import com.cloud.storage.dao.VMTemplateHostDao;
 import com.cloud.template.TemplateManager;
 import com.cloud.user.Account;
 import com.cloud.user.AccountManager;
+import com.cloud.user.AccountVO;
 import com.cloud.user.User;
 import com.cloud.user.UserContext;
+import com.cloud.user.UserVO;
 import com.cloud.utils.DateUtil;
 import com.cloud.utils.NumbersUtil;
 import com.cloud.utils.Pair;
@@ -143,6 +149,7 @@ import com.cloud.vm.NicProfile;
 import com.cloud.vm.ReservationContext;
 import com.cloud.vm.SystemVmLoadScanHandler;
 import com.cloud.vm.SystemVmLoadScanner;
+import com.cloud.vm.VmWork;
 import com.cloud.vm.SystemVmLoadScanner.AfterScanAction;
 import com.cloud.vm.VMInstanceVO;
 import com.cloud.vm.VirtualMachine;
@@ -151,6 +158,7 @@ import com.cloud.vm.VirtualMachineGuru;
 import com.cloud.vm.VirtualMachineManager;
 import com.cloud.vm.VirtualMachineName;
 import com.cloud.vm.VirtualMachineProfile;
+import com.cloud.vm.VmWorkStart;
 import com.cloud.vm.dao.ConsoleProxyDao;
 import com.cloud.vm.dao.UserVmDetailsDao;
 import com.cloud.vm.dao.VMInstanceDao;
@@ -234,6 +242,9 @@ public class ConsoleProxyManagerImpl extends ManagerBase implements ConsoleProxy
     TemplateManager templateMgr;
     @Inject
     IPAddressDao _ipAddressDao;
+    
+    @Inject
+    EntityManager _entityMgr;
 
     private ConsoleProxyListener _listener;
 
@@ -2028,4 +2039,27 @@ public class ConsoleProxyManagerImpl extends ManagerBase implements ConsoleProxy
     @Override
     public void prepareStop(VirtualMachineProfile<ConsoleProxyVO> profile) {
     }
+
+    @Override
+    public void vmWorkStart(VmWork work) {
+    	assert(work instanceof VmWorkStart);
+    	
+    	ConsoleProxyVO vm = findById(work.getVmId());
+    	
+    	UserVO user = _entityMgr.findById(UserVO.class, work.getUserId());
+    	AccountVO account = _entityMgr.findById(AccountVO.class, work.getAccountId());
+    	
+    	try {
+	    	_itMgr.processVmStartWork(vm, ((VmWorkStart)work).getParams(), 
+	    		user, account,  ((VmWorkStart)work).getPlan());
+    	} catch(Exception e) {
+    		String result = SerializerHelper.toObjectSerializedString(e);
+    		AsyncJobExecutionContext.getCurrentExecutionContext().completeAsyncJob(AsyncJobResult.STATUS_FAILED, 0, result);
+    	}
+    }
+    
+    @Override
+    public void vmWorkStop(VmWork work) {
+    	// TODO
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
index ab91059..7eff938 100755
--- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
+++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
@@ -228,6 +228,7 @@ import com.cloud.vm.ReservationContextImpl;
 import com.cloud.vm.UserVmVO;
 import com.cloud.vm.VMInstanceVO;
 import com.cloud.vm.VirtualMachine;
+import com.cloud.vm.VmWork;
 import com.cloud.vm.VirtualMachine.State;
 import com.cloud.vm.VirtualMachineGuru;
 import com.cloud.vm.VirtualMachineManager;
@@ -3712,4 +3713,12 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V
             }
         }
     }
+    
+    @Override
+    public void vmWorkStart(VmWork work) {
+    }
+    
+    @Override
+    public void vmWorkStop(VmWork work) {
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
index c94224b..170e4bf 100755
--- a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
+++ b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
@@ -129,6 +129,7 @@ import com.cloud.vm.SecondaryStorageVm;
 import com.cloud.vm.SecondaryStorageVmVO;
 import com.cloud.vm.SystemVmLoadScanHandler;
 import com.cloud.vm.SystemVmLoadScanner;
+import com.cloud.vm.VmWork;
 import com.cloud.vm.SystemVmLoadScanner.AfterScanAction;
 import com.cloud.vm.VirtualMachine;
 import com.cloud.vm.VirtualMachine.State;
@@ -1471,6 +1472,14 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
 
 	@Override
 	public void prepareStop(VirtualMachineProfile<SecondaryStorageVmVO> profile) {
-		
 	}
+	
+    @Override
+    public void vmWorkStart(VmWork work) {
+    }
+    
+    @Override
+    public void vmWorkStop(VmWork work) {
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/vm/UserVmManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java
index 646cf92..1b00997 100755
--- a/server/src/com/cloud/vm/UserVmManagerImpl.java
+++ b/server/src/com/cloud/vm/UserVmManagerImpl.java
@@ -4056,5 +4056,13 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Use
     @Override
     public void prepareStop(VirtualMachineProfile<UserVmVO> profile) {
     }
+    
+    @Override
+    public void vmWorkStart(VmWork work) {
+    }
+    
+    @Override
+    public void vmWorkStop(VmWork work) {
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/vm/VirtualMachineGuru.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/VirtualMachineGuru.java b/server/src/com/cloud/vm/VirtualMachineGuru.java
index a4f0f90..b3fafe3 100644
--- a/server/src/com/cloud/vm/VirtualMachineGuru.java
+++ b/server/src/com/cloud/vm/VirtualMachineGuru.java
@@ -116,5 +116,10 @@ public interface VirtualMachineGuru<T extends VirtualMachine> {
      */
     void prepareStop(VirtualMachineProfile<T> profile);
     
-    void doVmStart(VmWork work);
+    /**
+     * VM work handlers
+     * @param work
+     */
+    void vmWorkStart(VmWork work);
+    void vmWorkStop(VmWork work);
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/vm/VirtualMachineManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/VirtualMachineManager.java b/server/src/com/cloud/vm/VirtualMachineManager.java
index c98cc91..1032abc 100644
--- a/server/src/com/cloud/vm/VirtualMachineManager.java
+++ b/server/src/com/cloud/vm/VirtualMachineManager.java
@@ -94,6 +94,8 @@ public interface VirtualMachineManager extends Manager {
     
     Collection<VirtualMachineGuru<? extends VMInstanceVO>> getRegisteredGurus();
 
+    <T extends VMInstanceVO> VirtualMachineGuru<T> getVmGuru(T vm);
+    
     boolean stateTransitTo(VMInstanceVO vm, VirtualMachine.Event e, Long hostId) throws NoTransitionException;
 
     <T extends VMInstanceVO> T advanceStart(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException, OperationTimedoutException;
@@ -201,4 +203,9 @@ public interface VirtualMachineManager extends Manager {
             throws ResourceUnavailableException, ConcurrentOperationException,
             ManagementServerException, VirtualMachineMigrationException;
 
+    //
+    // VM work handlers
+    //
+    <T extends VMInstanceVO> T processVmStartWork(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account, DeploymentPlan planToDeploy)
+           throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException;
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/vm/VirtualMachineManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java
index 773bd26..555fc90 100755
--- a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java
+++ b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java
@@ -39,7 +39,7 @@ import javax.naming.ConfigurationException;
 import com.cloud.capacity.CapacityManager;
 import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
 import org.apache.cloudstack.framework.messagebus.MessageBus;
-import org.apache.cloudstack.messagebus.SubjectConstants;
+import org.apache.cloudstack.messagebus.TopicConstants;
 import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
 
 import com.cloud.dc.*;
@@ -55,6 +55,8 @@ import com.cloud.agent.manager.Commands;
 import com.cloud.agent.manager.allocator.HostAllocator;
 import com.cloud.alert.AlertManager;
 import com.cloud.api.ApiSerializerHelper;
+import com.cloud.async.AsyncJob;
+import com.cloud.async.AsyncJobExecutionContext;
 import com.cloud.async.AsyncJobManager;
 import com.cloud.async.AsyncJobResult;
 import com.cloud.cluster.ClusterManager;
@@ -62,7 +64,6 @@ import com.cloud.configuration.Config;
 import com.cloud.configuration.ConfigurationManager;
 import com.cloud.configuration.dao.ConfigurationDao;
 import com.cloud.dc.DataCenter;
-import com.cloud.dc.DataCenterVO;
 import com.cloud.dc.HostPodVO;
 import com.cloud.dc.dao.DataCenterDao;
 import com.cloud.dc.dao.HostPodDao;
@@ -118,7 +119,6 @@ import com.cloud.storage.dao.VMTemplateDao;
 import com.cloud.storage.dao.VolumeDao;
 import com.cloud.user.Account;
 import com.cloud.user.AccountManager;
-import com.cloud.user.AccountVO;
 import com.cloud.user.User;
 import com.cloud.user.dao.AccountDao;
 import com.cloud.user.dao.UserDao;
@@ -143,9 +143,7 @@ import com.cloud.vm.VirtualMachine.State;
 import com.cloud.vm.dao.NicDao;
 import com.cloud.vm.dao.UserVmDao;
 import com.cloud.vm.dao.VMInstanceDao;
-import com.cloud.vm.snapshot.VMSnapshot;
 import com.cloud.vm.snapshot.VMSnapshotManager;
-import com.cloud.vm.snapshot.VMSnapshotVO;
 import com.cloud.vm.snapshot.dao.VMSnapshotDao;
 import com.cloud.vm.dao.UserVmDetailsDao;
 
@@ -177,8 +175,12 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
     protected DomainDao _domainDao;
     @Inject
     protected ClusterManager _clusterMgr;
+   
+/*    
     @Inject
     protected ItWorkDao _workDao;
+*/
+    
     @Inject
     protected UserVmDao _userVmDao;
     @Inject
@@ -278,6 +280,12 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
     		return _vmGurus.values();
     	}
     }
+   
+    @Override
+    @SuppressWarnings("unchecked")
+    public <T extends VMInstanceVO> VirtualMachineGuru<T> getVmGuru(T vm) {
+        return (VirtualMachineGuru<T>) _vmGurus.get(vm.getType());
+    }
 
     @Override
     @DB
@@ -357,11 +365,6 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
         return allocate(vm, template, serviceOffering, new Pair<DiskOfferingVO, Long>(serviceOffering, null), null, networks, null, plan, hyperType, owner);
     }
 
-    @SuppressWarnings("unchecked")
-    private <T extends VMInstanceVO> VirtualMachineGuru<T> getVmGuru(T vm) {
-        return (VirtualMachineGuru<T>) _vmGurus.get(vm.getType());
-    }
-
     @Override
     public <T extends VMInstanceVO> boolean expunge(T vm, User caller, Account account) throws ResourceUnavailableException {
         try {
@@ -483,6 +486,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
         }
     }
 
+/*    
     protected boolean checkWorkItems(VMInstanceVO vm, State state) throws ConcurrentOperationException {
         while (true) {
             ItWorkVO vo = _workDao.findByOutstandingWork(vm.getId(), state);
@@ -513,9 +517,10 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
             }
             s_logger.debug("Waiting some more to make sure there's no activity on " + vm);
         }
-
     }
-
+*/
+    
+/*    
     @DB
     protected <T extends VMInstanceVO> Ternary<T, ReservationContext, ItWorkVO> changeToStartState(VirtualMachineGuru<T> vmGuru, T vm, User caller, Account account)
             throws ConcurrentOperationException {
@@ -583,18 +588,52 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
 
         throw new ConcurrentOperationException("Unable to change the state of " + vm);
     }
+*/
 
-    protected <T extends VMInstanceVO> boolean changeState(T vm, Event event, Long hostId, ItWorkVO work, Step step) throws NoTransitionException {
+    @DB
+    protected <T extends VMInstanceVO> Ternary<T, ReservationContext, VmWorkJobVO> changeToStartState(VirtualMachineGuru<T> vmGuru, T vm, User caller, Account account)
+        throws ConcurrentOperationException {
+    	
+        long vmId = vm.getId();
+        
+        Ternary<T, ReservationContext, VmWorkJobVO> result = null;
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        try {
+	        VmWorkJobVO work = this._workJobDao.findById(AsyncJobExecutionContext.getCurrentExecutionContext().getJob().getId());
+	        
+	        Journal journal = new Journal.LogJournal("Creating " + vm, s_logger);
+	        ReservationContextImpl context = new ReservationContextImpl(work.getUuid(), journal, caller, account);
+	        if (stateTransitTo(vm, Event.StartRequested, null, work.getUuid())) {
+	            if (s_logger.isDebugEnabled()) {
+	                s_logger.debug("Successfully transitioned to start state for " + vm + " reservation id = " + work.getId());
+	            }
+	            result = new Ternary<T, ReservationContext, VmWorkJobVO>(vmGuru.findById(vmId), context, work);
+	            txn.commit();
+	            return result;
+	        }
+        } catch (NoTransitionException e) {
+            s_logger.warn("Unable to transition into Starting state due to " + e.getMessage());
+        } finally {
+        	if(result == null)
+        		txn.rollback();
+        }
+        
+        throw new ConcurrentOperationException("Unable to change the state of " + vm);
+    }
+    
+    protected <T extends VMInstanceVO> boolean changeState(T vm, Event event, Long hostId, VmWorkJobVO work, Step step) throws NoTransitionException {
         // FIXME: We should do this better.
-        Step previousStep = work.getStep();
-        _workDao.updateStep(work, step);
+        VmWorkJobVO.Step previousStep = work.getStep();
+        
         boolean result = false;
         try {
             result = stateTransitTo(vm, event, hostId);
             return result;
         } finally {
             if (!result) {
-                _workDao.updateStep(work, previousStep);
+            	work.setStep(previousStep);
+            	this._workJobDao.update(work.getId(), work);
             }
         }
     }
@@ -606,7 +645,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
     }
     
     @DB
-    public <T extends VMInstanceVO> T advanceStartNew(final T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account, DeploymentPlan planToDeploy)
+    public <T extends VMInstanceVO> T advanceStart(final T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account, DeploymentPlan planToDeploy)
         throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
     	
     	VmWorkJobVO workJob = null;
@@ -624,7 +663,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
         		workJob = pendingWorkJobs.get(0);
         	} else {
         		workJob = new VmWorkJobVO();
-        		
+        	
         		workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
         		workJob.setCmd(VmWorkConstants.VM_WORK_START);
         		
@@ -657,7 +696,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
     	
     	// TODO : will refactor to fully-asynchronized way in the future
     	_jobMgr.waitAndCheck(
-    		new String[] { SubjectConstants.VM_POWER_STATE, SubjectConstants.JOB_STATE }, 
+    		new String[] { TopicConstants.VM_POWER_STATE, TopicConstants.JOB_STATE }, 
     		3000L, 600000L, new Predicate() {
 
 				@Override
@@ -677,20 +716,19 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
     	return vm;
     }
 
-    public <T extends VMInstanceVO> T doVmStart(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account, DeploymentPlan planToDeploy)
+    @Override
+    public <T extends VMInstanceVO> T processVmStartWork(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account, DeploymentPlan planToDeploy)
             throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
         long vmId = vm.getId();
         VirtualMachineGuru<T> vmGuru = getVmGuru(vm);
 
         vm = vmGuru.findById(vm.getId());
-        Ternary<T, ReservationContext, ItWorkVO> start = changeToStartState(vmGuru, vm, caller, account);
-        if (start == null) {
-            return vmGuru.findById(vmId);
-        }
+  
+        Ternary<T, ReservationContext, VmWorkJobVO> start = changeToStartState(vmGuru, vm, caller, account);
+        assert(start != null);
 
-        vm = start.first();
         ReservationContext ctx = start.second();
-        ItWorkVO work = start.third();
+        VmWorkJobVO work = start.third();
 
         T startedVm = null;
         ServiceOfferingVO offering = _offeringDao.findById(vm.getServiceOfferingId());
@@ -700,7 +738,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
             s_logger.debug("Trying to deploy VM, vm has dcId: " + vm.getDataCenterId() + " and podId: " + vm.getPodIdToDeployIn());
         }
         DataCenterDeployment plan = new DataCenterDeployment(vm.getDataCenterId(), vm.getPodIdToDeployIn(), null, null, null, null, ctx);
-        if(planToDeploy != null && planToDeploy.getDataCenterId() != 0){
+        if(planToDeploy != null && planToDeploy.getDataCenterId() != 0) {
             if (s_logger.isDebugEnabled()) {
                 s_logger.debug("advanceStart: DeploymentPlan is provided, using dcId:" + planToDeploy.getDataCenterId() + ", podId: " + planToDeploy.getPodId() + ", clusterId: "
                         + planToDeploy.getClusterId() + ", hostId: " + planToDeploy.getHostId() + ", poolId: " + planToDeploy.getPoolId());
@@ -712,8 +750,6 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
 
         boolean canRetry = true;
         try {
-            Journal journal = start.second().getJournal();
-
             ExcludeList avoids = null;
             if (planToDeploy != null) {
                 avoids = planToDeploy.getAvoids();
@@ -791,7 +827,8 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
                     }
                     if (dest != null) {
                         avoids.addHost(dest.getHost().getId());
-                        journal.record("Deployment found ", vmProfile, dest);
+                        AsyncJobExecutionContext.getCurrentExecutionContext().logJobJournal(
+                        	AsyncJob.JournalType.SUCCESS, "Deployment found, dest host: " + dest.getHost().getId(), null);
                         break;
                     }
                 }
@@ -846,17 +883,13 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
 
                     vmGuru.finalizeDeployment(cmds, vmProfile, dest, ctx);
 
-
-                    work = _workDao.findById(work.getId());
-                    if (work == null || work.getStep() != Step.Prepare) {
-                        throw new ConcurrentOperationException("Work steps have been changed: " + work);
-                    }
-                    _workDao.updateStep(work, Step.Starting);
+                    work.setStep(VmWorkJobVO.Step.Starting);
+                    _workJobDao.update(work.getId(), work);
 
                     _agentMgr.send(destHostId, cmds);
 
-                    _workDao.updateStep(work, Step.Started);
-
+                    work.setStep(VmWorkJobVO.Step.Started);
+                    _workJobDao.update(work.getId(), work);
 
                     StartAnswer startAnswer = cmds.getAnswer(StartAnswer.class);
                     if (startAnswer != null && startAnswer.getResult()) {
@@ -925,9 +958,9 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
                     throw new AgentUnavailableException("Unable to start instance due to " + e.getMessage(), destHostId, e);
                 } finally {
                     if (startedVm == null && canRetry) {
-                        Step prevStep = work.getStep();
-                        _workDao.updateStep(work, Step.Release);
-                        if (prevStep == Step.Started || prevStep == Step.Starting) {
+                        VmWorkJobVO.Step prevStep = work.getStep();
+                        _workJobDao.updateStep(work.getId(), VmWorkJobVO.Step.Release);
+                        if (prevStep == VmWorkJobVO.Step.Started || prevStep == VmWorkJobVO.Step.Starting) {
                             cleanup(vmGuru, vmProfile, work, Event.OperationFailed, false, caller, account);
                         } else {
                             //if step is not starting/started, send cleanup command with force=true
@@ -951,7 +984,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
         return startedVm;
     }
     
-    
+ /*   
     @Override
     public <T extends VMInstanceVO> T advanceStart(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account, DeploymentPlan planToDeploy)
             throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
@@ -1227,7 +1260,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
 
         return startedVm;
     }
-
+*/
     @Override
     public <T extends VMInstanceVO> boolean stop(T vm, User user, Account account) throws ResourceUnavailableException {
         try {
@@ -1263,18 +1296,18 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
         return true;
     }
 
-    protected <T extends VMInstanceVO> boolean cleanup(VirtualMachineGuru<T> guru, VirtualMachineProfile<T> profile, ItWorkVO work, Event event, boolean force, User user, Account account) {
+    protected <T extends VMInstanceVO> boolean cleanup(VirtualMachineGuru<T> guru, VirtualMachineProfile<T> profile, VmWorkJobVO work, Event event, boolean force, User user, Account account) {
         T vm = profile.getVirtualMachine();
         State state = vm.getState();
         s_logger.debug("Cleaning up resources for the vm " + vm + " in " + state + " state");
         if (state == State.Starting) {
-            Step step = work.getStep();
-            if (step == Step.Starting && !force) {
+            VmWorkJobVO.Step step = work.getStep();
+            if (step == VmWorkJobVO.Step.Starting && !force) {
                 s_logger.warn("Unable to cleanup vm " + vm + "; work state is incorrect: " + step);
                 return false;
             }
 
-            if (step == Step.Started || step == Step.Starting || step == Step.Release) {
+            if (step == VmWorkJobVO.Step.Started || step == VmWorkJobVO.Step.Starting || step == VmWorkJobVO.Step.Release) {
                 if (vm.getHostId() != null) {
                     if (!sendStop(guru, profile, force)) {
                         s_logger.warn("Failed to stop vm " + vm + " in " + State.Starting + " state as a part of cleanup process");
@@ -1283,7 +1316,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
                 }
             }
 
-            if (step != Step.Release && step != Step.Prepare && step != Step.Started && step != Step.Starting) {
+            if (step != VmWorkJobVO.Step.Release && step != VmWorkJobVO.Step.Prepare && step != VmWorkJobVO.Step.Started && step != VmWorkJobVO.Step.Starting) {
                 s_logger.debug("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step);
                 return true;
             }
@@ -1325,7 +1358,146 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
         s_logger.debug("Successfully cleanued up resources for the vm " + vm + " in " + state + " state");
         return true;
     }
+    
+    @Override
+    public <T extends VMInstanceVO> boolean advanceStop(T vm, boolean forced, User user, Account account) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException {
+    	// ???
+    	return true;
+    }
+    
+    public <T extends VMInstanceVO> boolean processVmStopWork(T vm, boolean forced, User user, Account account) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException {
+        VmWorkJobVO work = _workJobDao.findById(AsyncJobExecutionContext.getCurrentExecutionContext().getJob().getId());
+
+        Long hostId = vm.getHostId();
+        if (hostId == null) {
+            if (!forced) {
+                if (s_logger.isDebugEnabled()) {
+                    s_logger.debug("HostId is null but this is not a forced stop, cannot stop vm " + vm + " with state:" + vm.getState());
+                }
+                return false;
+            }
+            
+            try {
+                stateTransitTo(vm, Event.AgentReportStopped, null, null);
+            } catch (NoTransitionException e) {
+                s_logger.warn(e.getMessage());
+            }
+  
+            _workJobDao.updateStep(work.getId(), VmWorkJobVO.Step.Done);
+            return true;
+        }
+
+        VirtualMachineGuru<T> vmGuru = getVmGuru(vm);
+        VirtualMachineProfile<T> profile = new VirtualMachineProfileImpl<T>(vm);
+
+        try {
+            if (!stateTransitTo(vm, Event.StopRequested, vm.getHostId())) {
+                throw new ConcurrentOperationException("VM is being operated on.");
+            }
+        } catch (NoTransitionException e1) {
+            if (!forced) {
+                throw new CloudRuntimeException("We cannot stop " + vm + " when it is in state " + vm.getState());
+            }
+            
+            State state = vm.getState();
+            boolean doCleanup = false;
+            if (s_logger.isDebugEnabled()) {
+                s_logger.debug("Unable to transition the state but we're moving on because it's forced stop");
+            }
+            if (state == State.Starting || state == State.Migrating) {
+                doCleanup = true;
+            } else if (state == State.Stopping) {
+                doCleanup = true;
+            }
+
+            if (doCleanup) {
+                if (cleanup(vmGuru, new VirtualMachineProfileImpl<T>(vm), work, Event.StopRequested, forced, user, account)) {
+                    try {
+                        if (s_logger.isDebugEnabled()) {
+                            s_logger.debug("Updating work item to Done, id:" + work.getId());
+                        }
+                        return changeState(vm, Event.AgentReportStopped, null, work, Step.Done);
+                    } catch (NoTransitionException e) {
+                        s_logger.warn("Unable to cleanup " + vm);
+                        return false;
+                    }
+                } else {
+                    if (s_logger.isDebugEnabled()) {
+                        s_logger.debug("Failed to cleanup VM: " + vm);
+                    }
+                    throw new CloudRuntimeException("Failed to cleanup " + vm + " , current state " + vm.getState());
+                }
+            }
+        }
+
+        vmGuru.prepareStop(profile);
+        
+        StopCommand stop = new StopCommand(vm);
+        boolean stopped = false;
+        StopAnswer answer = null;
+        try {
+            answer = (StopAnswer) _agentMgr.send(vm.getHostId(), stop);
+            stopped = answer.getResult();
+            if (!stopped) {
+                throw new CloudRuntimeException("Unable to stop the virtual machine due to " + answer.getDetails());
+            }
+            vmGuru.finalizeStop(profile, answer);
+
+        } catch (AgentUnavailableException e) {
+        } catch (OperationTimedoutException e) {
+        } finally {
+            if (!stopped) {
+                if (!forced) {
+                    s_logger.warn("Unable to stop vm " + vm);
+                    try {
+                        stateTransitTo(vm, Event.OperationFailed, vm.getHostId());
+                    } catch (NoTransitionException e) {
+                        s_logger.warn("Unable to transition the state " + vm);
+                    }
+                    return false;
+                } else {
+                    s_logger.warn("Unable to actually stop " + vm + " but continue with release because it's a force stop");
+                    vmGuru.finalizeStop(profile, answer);
+                }
+            }
+        }
+
+        if (s_logger.isDebugEnabled()) {
+            s_logger.debug(vm + " is stopped on the host.  Proceeding to release resource held.");
+        }
+
+        try {
+            _networkMgr.release(profile, forced);
+            s_logger.debug("Successfully released network resources for the vm " + vm);
+        } catch (Exception e) {
+            s_logger.warn("Unable to release some network resources.", e);
+        }
+
+        try {
+            if (vm.getHypervisorType() != HypervisorType.BareMetal) {
+                this.volumeMgr.release(profile);
+                s_logger.debug("Successfully released storage resources for the vm " + vm);
+            }
+        } catch (Exception e) {
+            s_logger.warn("Unable to release storage resources.", e);
+        }
+
+        try {
+            if (work != null) {
+                if (s_logger.isDebugEnabled()) {
+                    s_logger.debug("Updating the outstanding work item to Done, id:" + work.getId());
+                }
+                _workJobDao.updateStep(work.getId(), VmWorkJobVO.Step.Done);
+            }
+
+            return stateTransitTo(vm, Event.OperationSucceeded, null, null);
+        } catch (NoTransitionException e) {
+            s_logger.warn(e.getMessage());
+            return false;
+        }
+    }
 
+/*    
     @Override
     public <T extends VMInstanceVO> boolean advanceStop(T vm, boolean forced, User user, Account account) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException {
         State state = vm.getState();
@@ -1492,7 +1664,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
             return false;
         }
     }
-
+*/
     private void setStateMachine() {
         _stateMachine = VirtualMachine.State.getStateMachine();
     }
@@ -1504,12 +1676,16 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
 
     @Override
     public boolean stateTransitTo(VMInstanceVO vm, VirtualMachine.Event e, Long hostId) throws NoTransitionException {
+
+/*
+ *  TODO ???
+ *      	
         // if there are active vm snapshots task, state change is not allowed
-        if(_vmSnapshotMgr.hasActiveVMSnapshotTasks(vm.getId())){
+        if(_vmSnapshotMgr.hasActiveVMSnapshotTasks(vm.getId())) {
             s_logger.error("State transit with event: " + e + " failed due to: " + vm.getInstanceName() + " has active VM snapshots tasks");
             return false;
         }
-        
+*/        
         State oldState = vm.getState();
         if (oldState == State.Starting) {
             if (e == Event.OperationSucceeded) {
@@ -1632,9 +1808,12 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
 
     @Override
     public <T extends VMInstanceVO> T migrate(T vm, long srcHostId, DeployDestination dest) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException,
-    VirtualMachineMigrationException {
+    	VirtualMachineMigrationException {
         s_logger.info("Migrating " + vm + " to " + dest);
-
+        
+        return vm;
+        
+/*
         long dstHostId = dest.getHost().getId();
         Host fromHost = _hostDao.findById(srcHostId);
         if (fromHost == null) {
@@ -1779,6 +1958,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
             work.setStep(Step.Done);
             _workDao.update(work.getId(), work);
         }
+*/        
     }
 
     @Override
@@ -1789,6 +1969,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
     	}
 
     protected void cancelWorkItems(long nodeId) {
+/*    	
         GlobalLock scanLock = GlobalLock.getInternLock("vmmgr.cancel.workitem");
 
         try {
@@ -1825,6 +2006,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
         } finally {
             scanLock.releaseRef();
         }
+*/        
     }
 
     @Override
@@ -1911,14 +2093,17 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
     }
 
     protected class CleanupTask implements Runnable {
+    	
         @Override
         public void run() {
             s_logger.trace("VM Operation Thread Running");
+/*            
             try {
                 _workDao.cleanup(_cleanupWait);
             } catch (Exception e) {
                 s_logger.error("VM Operations failed due to ", e);
             }
+*/            
         }
     }
 
@@ -2120,6 +2305,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
 */
 
     public void fullSync(final long clusterId, Map<String, Pair<String, State>> newStates) {
+/*    	
         if (newStates==null)return;
         Map<Long, AgentVmInfo> infos = convertToInfos(newStates);
         Set<VMInstanceVO> set_vms = Collections.synchronizedSet(new HashSet<VMInstanceVO>());
@@ -2224,13 +2410,6 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
                         }
                     }
                 }
-            /* else if(info == null && vm.getState() == State.Stopping) { //Handling CS-13376
-                        s_logger.warn("Marking the VM as Stopped as it was still stopping on the CS" +vm.getName());
-                        vm.setState(State.Stopped); // Setting the VM as stopped on the DB and clearing it from the host
-                        vm.setLastHostId(vm.getHostId());
-                        vm.setHostId(null);
-                        _vmDao.persist(vm);
-                 }*/
         }
 
         for (final AgentVmInfo left : infos.values()) {
@@ -2248,7 +2427,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
                 s_logger.warn("Unable to stop a VM due to " + e.getMessage());
             }
         }
-
+*/
     }
 
 
@@ -2353,6 +2532,8 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
      * 
      */
     protected Command compareState(long hostId, VMInstanceVO vm, final AgentVmInfo info, final boolean fullSync, boolean trackExternalChange) {
+    	return null;
+/*    	
         State agentState = info.state;
         final State serverState = vm.getState();
         final String serverName = vm.getInstanceName();
@@ -2523,10 +2704,12 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
             }
         }
         return command;
+*/        
     }
 
     private void ensureVmRunningContext(long hostId, VMInstanceVO vm, Event cause) throws OperationTimedoutException, ResourceUnavailableException, NoTransitionException, InsufficientAddressCapacityException {
-        VirtualMachineGuru<VMInstanceVO> vmGuru = getVmGuru(vm);
+  /* 
+    	VirtualMachineGuru<VMInstanceVO> vmGuru = getVmGuru(vm);
 
         s_logger.debug("VM state is starting on full sync so updating it to running");
         vm = findByIdAndType(vm.getType(), vm.getId());
@@ -2582,6 +2765,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
             work.setStep(Step.Done);
             _workDao.update(work.getId(), work);
         }
+*/    
     }
 
     @Override
@@ -2623,6 +2807,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
             if (cmd instanceof PingRoutingCommand) {
                 PingRoutingCommand ping = (PingRoutingCommand) cmd;
                 if (ping.getNewStates() != null && ping.getNewStates().size() > 0) {
+                	_syncMgr.processHostVmStatePingReport(agentId, ping.getNewStates());
 
 /* TODO                	
                     Commands commands = deltaHostSync(agentId, ping.getNewStates());
@@ -2657,6 +2842,8 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
             return;
         }
         
+        if(s_logger.isDebugEnabled())
+        	s_logger.debug("Received startup command from hypervisor host. host id: " + agent.getId());
         _syncMgr.resetHostSyncState(agent.getId());
     }
     
@@ -3126,7 +3313,8 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
         public <T extends VMInstanceVO> T migrateForScale(T vm, long srcHostId, DeployDestination dest, Long oldSvcOfferingId) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException,
                 VirtualMachineMigrationException {
             s_logger.info("Migrating " + vm + " to " + dest);
-
+            return vm;
+/*
             Long newSvcOfferingId = vm.getServiceOfferingId();
             long dstHostId = dest.getHost().getId();
             Host fromHost = _hostDao.findById(srcHostId);
@@ -3275,10 +3463,11 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
                 work.setStep(Step.Done);
                 _workDao.update(work.getId(), work);
             }
+*/            
         }
     @Override
     public VMInstanceVO reConfigureVm(VMInstanceVO vm , ServiceOffering oldServiceOffering, boolean reconfiguringOnExistingHost) throws ResourceUnavailableException, ConcurrentOperationException {
-
+/*
         long newServiceofferingId = vm.getServiceOfferingId();
         ServiceOffering newServiceOffering = _configMgr.getServiceOffering(newServiceofferingId);
         ScaleVmCommand reconfigureCmd = new ScaleVmCommand(vm.getInstanceName(), newServiceOffering.getCpu(),
@@ -3319,10 +3508,8 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
                 _capacityMgr.allocateVmCapacity(vm, false); // allocate the old capacity
             }
         }
-
+*/
         return vm;
 
     }
-
-
-    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/vm/VirtualMachinePowerStateSync.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/VirtualMachinePowerStateSync.java b/server/src/com/cloud/vm/VirtualMachinePowerStateSync.java
index ac1ddec..7a23ddd 100644
--- a/server/src/com/cloud/vm/VirtualMachinePowerStateSync.java
+++ b/server/src/com/cloud/vm/VirtualMachinePowerStateSync.java
@@ -19,8 +19,14 @@ package com.cloud.vm;
 import java.util.Map;
 
 import com.cloud.agent.api.HostVmStateReportEntry;
+import com.cloud.vm.VirtualMachine.PowerState;
 
 public interface VirtualMachinePowerStateSync {
+	
 	void resetHostSyncState(long hostId);
+	
 	void processHostVmStateReport(long hostId, Map<String, HostVmStateReportEntry> report);
+	
+	// to adapt legacy ping report
+	void processHostVmStatePingReport(long hostId, Map<String, PowerState> report);
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/vm/VirtualMachinePowerStateSyncImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/VirtualMachinePowerStateSyncImpl.java b/server/src/com/cloud/vm/VirtualMachinePowerStateSyncImpl.java
index f5bedd6..5eb4117 100644
--- a/server/src/com/cloud/vm/VirtualMachinePowerStateSyncImpl.java
+++ b/server/src/com/cloud/vm/VirtualMachinePowerStateSyncImpl.java
@@ -24,10 +24,11 @@ import javax.inject.Inject;
 
 import org.apache.cloudstack.framework.messagebus.MessageBus;
 import org.apache.cloudstack.framework.messagebus.PublishScope;
-import org.apache.cloudstack.messagebus.SubjectConstants;
+import org.apache.cloudstack.messagebus.TopicConstants;
 import org.apache.log4j.Logger;
 
 import com.cloud.agent.api.HostVmStateReportEntry;
+import com.cloud.vm.VirtualMachine.PowerState;
 import com.cloud.vm.dao.VMInstanceDao;
 
 public class VirtualMachinePowerStateSyncImpl implements VirtualMachinePowerStateSync {
@@ -48,44 +49,91 @@ public class VirtualMachinePowerStateSyncImpl implements VirtualMachinePowerStat
     
     @Override
 	public void processHostVmStateReport(long hostId, Map<String, HostVmStateReportEntry> report) {
-    	s_logger.info("Process host VM state report. host: " + hostId);
+    	if(s_logger.isDebugEnabled())
+    		s_logger.debug("Process host VM state report from ping process. host: " + hostId);
     	
     	Map<Long, VirtualMachine.PowerState> translatedInfo = convertToInfos(report);
+    	processReport(hostId, translatedInfo);
+    }
+
+    @Override
+	public void processHostVmStatePingReport(long hostId, Map<String, PowerState> report) {
+    	if(s_logger.isDebugEnabled())
+    		s_logger.debug("Process host VM state report from ping process. host: " + hostId);
+    	
+    	Map<Long, VirtualMachine.PowerState> translatedInfo = convertHostPingInfos(report);
+    	processReport(hostId, translatedInfo);
+    }
+    
+    private void processReport(long hostId, Map<Long, VirtualMachine.PowerState> translatedInfo) {
     	for(Map.Entry<Long, VirtualMachine.PowerState> entry : translatedInfo.entrySet()) {
+    		
+        	if(s_logger.isDebugEnabled())
+        		s_logger.debug("VM state report. host: " + hostId + ", vm id: " + entry.getKey() + ", power state: " + entry.getValue());
+
     		if(_instanceDao.updatePowerState(entry.getKey(), hostId, entry.getValue())) {
-    			_messageBus.publish(null, SubjectConstants.VM_POWER_STATE, PublishScope.GLOBAL, entry.getKey());
+    			
+            	if(s_logger.isDebugEnabled())
+            		s_logger.debug("VM state report is updated. host: " + hostId + ", vm id: " + entry.getKey() + ", power state: " + entry.getValue());
+    			
+    			_messageBus.publish(null, TopicConstants.VM_POWER_STATE, PublishScope.GLOBAL, entry.getKey());
     		}
     	}
     }
-    
-    protected Map<Long, VirtualMachine.PowerState> convertToInfos(Map<String, HostVmStateReportEntry> states) { 
+ 
+    private Map<Long, VirtualMachine.PowerState> convertHostPingInfos(Map<String, PowerState> states) {
         final HashMap<Long, VirtualMachine.PowerState> map = new HashMap<Long, VirtualMachine.PowerState>();
         if (states == null) {
             return map;
         }
-        
-        Collection<VirtualMachineGuru<? extends VMInstanceVO>> vmGurus = _vmMgr.getRegisteredGurus();
+    	
+        for (Map.Entry<String, PowerState> entry : states.entrySet()) {
+        	VMInstanceVO vm = findVM(entry.getKey());
+        	if(vm != null) {
+        		map.put(vm.getId(), entry.getValue());
+        		break;
+        	} else {
+        		s_logger.info("Unable to find matched VM in CloudStack DB. name: " + entry.getKey());
+        	}
+        }
 
+        return map;
+    }
+    	    
+    private Map<Long, VirtualMachine.PowerState> convertToInfos(Map<String, HostVmStateReportEntry> states) { 
+        final HashMap<Long, VirtualMachine.PowerState> map = new HashMap<Long, VirtualMachine.PowerState>();
+        if (states == null) {
+            return map;
+        }
+        
         for (Map.Entry<String, HostVmStateReportEntry> entry : states.entrySet()) {
-            for (VirtualMachineGuru<? extends VMInstanceVO> vmGuru : vmGurus) {
-                String name = entry.getKey();
-                VMInstanceVO vm = vmGuru.findByName(name);
-                if (vm != null) {
-                    map.put(vm.getId(), entry.getValue().getState());
-                    break;
-                }
-                
-                Long id = vmGuru.convertToId(name);
-                if (id != null) {
-                	vm = vmGuru.findById(id);
-                	if(vm != null) {
-	                    map.put(id, entry.getValue().getState());
-	                    break;
-                	}
-                }
-            }
+        	VMInstanceVO vm = findVM(entry.getKey());
+        	if(vm != null) {
+        		map.put(vm.getId(), entry.getValue().getState());
+        		break;
+        	} else {
+        		s_logger.info("Unable to find matched VM in CloudStack DB. name: " + entry.getKey());
+        	}
         }
 
         return map;
     }
+    
+    private VMInstanceVO findVM(String vmName) {
+        Collection<VirtualMachineGuru<? extends VMInstanceVO>> vmGurus = _vmMgr.getRegisteredGurus();
+
+        for (VirtualMachineGuru<? extends VMInstanceVO> vmGuru : vmGurus) {
+            VMInstanceVO vm = vmGuru.findByName(vmName);
+            if (vm != null)
+                return vm;
+            
+            Long id = vmGuru.convertToId(vmName);
+            if (id != null) {
+            	vm = vmGuru.findById(id);
+            	if(vm != null)
+            		return vm;
+            }
+        }
+        return null;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/vm/VmWorkConstants.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/VmWorkConstants.java b/server/src/com/cloud/vm/VmWorkConstants.java
index c435efe..44176ee 100644
--- a/server/src/com/cloud/vm/VmWorkConstants.java
+++ b/server/src/com/cloud/vm/VmWorkConstants.java
@@ -22,6 +22,6 @@ public interface VmWorkConstants {
 	public static final String VM_WORK_JOB_DISPATCHER = "VmWorkJobDispatcher";
 	
 	// work job commands
-	public static final String VM_WORK_START = "doVmStart";
-	public static final String VM_WORK_STOP = "doVmStop";
+	public static final String VM_WORK_START = "vmWorkStart";
+	public static final String VM_WORK_STOP = "vmWorkStop";
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/com/cloud/vm/VmWorkJobDispatcher.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/VmWorkJobDispatcher.java b/server/src/com/cloud/vm/VmWorkJobDispatcher.java
index 4ab4aa0..f033b1e 100644
--- a/server/src/com/cloud/vm/VmWorkJobDispatcher.java
+++ b/server/src/com/cloud/vm/VmWorkJobDispatcher.java
@@ -33,6 +33,7 @@ import com.cloud.user.AccountVO;
 import com.cloud.user.UserContext;
 import com.cloud.user.dao.AccountDao;
 import com.cloud.utils.component.AdapterBase;
+import com.cloud.vm.dao.VMInstanceDao;
 
 public class VmWorkJobDispatcher extends AdapterBase implements AsyncJobDispatcher {
     private static final Logger s_logger = Logger.getLogger(VmWorkJobDispatcher.class);
@@ -40,6 +41,7 @@ public class VmWorkJobDispatcher extends AdapterBase implements AsyncJobDispatch
 	@Inject private VirtualMachineManager _vmMgr;
 	@Inject private AsyncJobManager _asyncJobMgr;
     @Inject private AccountDao _accountDao;
+    @Inject private VMInstanceDao _instanceDao;
     
     private Map<String, Method> _handlerMap = new HashMap<String, Method>();
 	
@@ -55,11 +57,23 @@ public class VmWorkJobDispatcher extends AdapterBase implements AsyncJobDispatch
             AccountVO account = _accountDao.findById(work.getAccountId());
             assert(account != null);
             
+            VMInstanceVO vm = _instanceDao.findById(work.getVmId());
+            assert(vm != null);
+    
+            //
+            // Due to legcy massive generic usage in VirtualMachineManagerImpl, we can't dispatch job handling
+            // directly to VirtualMachineManagerImpl, since most handling method are generic method.
+            //
+            // to solve the problem, we have to go through an instantiated VirtualMachineGuru so that it can carry
+            // down correct type back to VirtualMachineManagerImpl. It is sad that we have to write code like this
+            //
+            VirtualMachineGuru<VMInstanceVO> guru = _vmMgr.getVmGuru(vm);
+            
             UserContext.registerContext(work.getUserId(), account, null, false);
             try {
-            	Method handler = getHandler(cmd);
+            	Method handler = getHandler(guru, cmd);
             	if(handler != null) {
-        			handler.invoke(_vmMgr, work);
+        			handler.invoke(guru, work);
             		_asyncJobMgr.completeAsyncJob(job.getId(), AsyncJobResult.STATUS_SUCCEEDED, 0, null);
             	} else {
             		_asyncJobMgr.completeAsyncJob(job.getId(), AsyncJobResult.STATUS_FAILED, 0, null);
@@ -72,14 +86,15 @@ public class VmWorkJobDispatcher extends AdapterBase implements AsyncJobDispatch
         }
 	}
 	
-	private Method getHandler(String cmd) {
+	private Method getHandler(VirtualMachineGuru<?> guru, String cmd) {
 		
 		synchronized(_handlerMap) {
-			Method method = _handlerMap.get(cmd);
+			Class<?> clz = guru.getClass();
+			String key = clz.getCanonicalName() + cmd;
+			Method method = _handlerMap.get(key);
 			if(method != null)
 				return method;
 			
-			Class<?> clz = _vmMgr.getClass();
 			try {
 				method = clz.getMethod(cmd, VmWork.class);
 				method.setAccessible(true);
@@ -93,7 +108,7 @@ public class VmWorkJobDispatcher extends AdapterBase implements AsyncJobDispatch
 				return null;
 			}
 			
-			_handlerMap.put(cmd, method);
+			_handlerMap.put(key, method);
 			return method;
 		}
 	}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/org/apache/cloudstack/messagebus/SubjectConstants.java
----------------------------------------------------------------------
diff --git a/server/src/org/apache/cloudstack/messagebus/SubjectConstants.java b/server/src/org/apache/cloudstack/messagebus/SubjectConstants.java
deleted file mode 100644
index 0b29b2b..0000000
--- a/server/src/org/apache/cloudstack/messagebus/SubjectConstants.java
+++ /dev/null
@@ -1,26 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License.
-package org.apache.cloudstack.messagebus;
-
-public interface SubjectConstants {
-	// VM power state messages on message bus
-	public static final String VM_POWER_STATE = "vm.powerstate";
-	
-	// job messages on message bus 
-	public static final String JOB_HEARTBEAT = "job.heartbeat";
-	public static final String JOB_STATE = "job.state";
-}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/src/org/apache/cloudstack/messagebus/TopicConstants.java
----------------------------------------------------------------------
diff --git a/server/src/org/apache/cloudstack/messagebus/TopicConstants.java b/server/src/org/apache/cloudstack/messagebus/TopicConstants.java
new file mode 100644
index 0000000..d90f602
--- /dev/null
+++ b/server/src/org/apache/cloudstack/messagebus/TopicConstants.java
@@ -0,0 +1,26 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.messagebus;
+
+public interface TopicConstants {
+	// VM power state messages on message bus
+	public static final String VM_POWER_STATE = "vm.powerstate";
+	
+	// job messages on message bus 
+	public static final String JOB_HEARTBEAT = "job.heartbeat";
+	public static final String JOB_STATE = "job.state";
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/test/com/cloud/async/AsyncJobTestConfiguration.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/async/AsyncJobTestConfiguration.java b/server/test/com/cloud/async/AsyncJobTestConfiguration.java
index 9baff59..97345dd 100644
--- a/server/test/com/cloud/async/AsyncJobTestConfiguration.java
+++ b/server/test/com/cloud/async/AsyncJobTestConfiguration.java
@@ -23,6 +23,8 @@ import org.springframework.context.annotation.Configuration;
 import com.cloud.api.ApiDispatcher;
 import com.cloud.async.dao.AsyncJobDao;
 import com.cloud.async.dao.AsyncJobDaoImpl;
+import com.cloud.async.dao.AsyncJobJournalDao;
+import com.cloud.async.dao.AsyncJobJournalDaoImpl;
 import com.cloud.async.dao.SyncQueueDao;
 import com.cloud.async.dao.SyncQueueDaoImpl;
 import com.cloud.async.dao.SyncQueueItemDao;
@@ -98,4 +100,9 @@ public class AsyncJobTestConfiguration {
 	public VirtualMachineManager virtualMachineManager() {
 		return Mockito.mock(VirtualMachineManager.class);
 	}
+	
+	@Bean
+	public AsyncJobJournalDao asyncJobJournalDao() {
+		return new AsyncJobJournalDaoImpl();
+	}
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a8a09ba2/server/test/com/cloud/async/MockVirtualMachineManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/async/MockVirtualMachineManagerImpl.java b/server/test/com/cloud/async/MockVirtualMachineManagerImpl.java
index 4566e8b..c016ddd 100644
--- a/server/test/com/cloud/async/MockVirtualMachineManagerImpl.java
+++ b/server/test/com/cloud/async/MockVirtualMachineManagerImpl.java
@@ -51,6 +51,7 @@ import com.cloud.vm.NicProfile;
 import com.cloud.vm.NicVO;
 import com.cloud.vm.VMInstanceVO;
 import com.cloud.vm.VirtualMachine;
+import com.cloud.vm.VmWork;
 import com.cloud.vm.VirtualMachine.Event;
 import com.cloud.vm.VirtualMachine.Type;
 import com.cloud.vm.VirtualMachineGuru;
@@ -186,6 +187,11 @@ public class MockVirtualMachineManagerImpl implements VirtualMachineManager {
 		
 	}
 
+	public <T extends VMInstanceVO> VirtualMachineGuru<T> getVmGuru(T vm) {
+		// TODO Auto-generated method stub
+    	return null;
+    }
+
 	@Override
 	public Collection<VirtualMachineGuru<? extends VMInstanceVO>> getRegisteredGurus() {
 		// TODO Auto-generated method stub
@@ -383,5 +389,11 @@ public class MockVirtualMachineManagerImpl implements VirtualMachineManager {
 		// TODO Auto-generated method stub
 		return null;
 	}
-
+	
+	@Override
+    public <T extends VMInstanceVO> T processVmStartWork(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account, DeploymentPlan planToDeploy)
+            throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
+    	
+    	return vm;
+    }
 }


Mime
View raw message