Skip to content

Commit 3c780a5

Browse files
committed
Start and stop VM now works for CPVM and SSVM with new VMSync
1 parent 006b2f8 commit 3c780a5

10 files changed

Lines changed: 263 additions & 303 deletions

‎client/tomcatconf/applicationContext.xml.in‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@
794794
<bean id="asyncJobDaoImpl" class="com.cloud.async.dao.AsyncJobDaoImpl" />
795795
<bean id="asyncJobJoinDaoImpl" class="com.cloud.api.query.dao.AsyncJobJoinDaoImpl" />
796796
<bean id="asyncJobJournalDaoImpl" class="com.cloud.async.dao.AsyncJobJournalDaoImpl" />
797+
<bean id="asyncJobJoinMapDaoImpl" class="com.cloud.async.dao.AsyncJobJoinMapDaoImpl" />
797798
<bean id="asyncJobManagerImpl" class="com.cloud.async.AsyncJobManagerImpl">
798799
<property name="defaultDispatcher" value="ApiAsyncJobDispatcher" />
799800
</bean>

‎server/src/com/cloud/async/AsyncJobExecutionContext.java‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@
1818

1919
import javax.inject.Inject;
2020

21+
import com.cloud.async.dao.AsyncJobJoinMapDao;
22+
import com.cloud.exception.ConcurrentOperationException;
23+
import com.cloud.exception.InsufficientCapacityException;
24+
import com.cloud.exception.ResourceUnavailableException;
25+
import com.cloud.serializer.SerializerHelper;
26+
2127
public class AsyncJobExecutionContext {
2228
private AsyncJob _job;
2329

24-
@Inject private AsyncJobManager _jobMgr;
30+
@Inject private AsyncJobManager _jobMgr;
31+
@Inject private AsyncJobJoinMapDao _joinMapDao;
2532

2633
private static ThreadLocal<AsyncJobExecutionContext> s_currentExectionContext = new ThreadLocal<AsyncJobExecutionContext>();
2734

@@ -65,6 +72,48 @@ public void logJobJournal(AsyncJob.JournalType journalType, String
6572
_jobMgr.logJobJournal(_job.getId(), journalType, journalText, journalObjJson);
6673
}
6774

75+
public void joinJob(long joinJobId) {
76+
assert(_job != null);
77+
_jobMgr.joinJob(_job.getId(), joinJobId);
78+
}
79+
80+
//
81+
// check failure exception before we disjoin the worker job
82+
// TODO : it is ugly and this will become unnecessary after we switch to full-async mode
83+
//
84+
public void disjoinJob(long joinedJobId) throws InsufficientCapacityException,
85+
ConcurrentOperationException, ResourceUnavailableException {
86+
assert(_job != null);
87+
88+
AsyncJobJoinMapVO record = _joinMapDao.getJoinRecord(_job.getId(), joinedJobId);
89+
if(record.getJoinStatus() == AsyncJobResult.STATUS_FAILED && record.getJoinResult() != null) {
90+
Object exception = SerializerHelper.fromObjectSerializedString(record.getJoinResult());
91+
if(exception != null && exception instanceof Exception) {
92+
if(exception instanceof InsufficientCapacityException)
93+
throw (InsufficientCapacityException)exception;
94+
else if(exception instanceof ConcurrentOperationException)
95+
throw (ConcurrentOperationException)exception;
96+
else if(exception instanceof ResourceUnavailableException)
97+
throw (ResourceUnavailableException)exception;
98+
else
99+
throw new RuntimeException((Exception)exception);
100+
}
101+
}
102+
103+
_jobMgr.disjoinJob(_job.getId(), joinedJobId);
104+
}
105+
106+
public void completeJoin(int joinStatus, String joinResult) {
107+
assert(_job != null);
108+
_jobMgr.completeJoin(_job.getId(), joinStatus, joinResult);
109+
}
110+
111+
public void completeJobAndJoin(int joinStatus, String joinResult) {
112+
assert(_job != null);
113+
_jobMgr.completeJoin(_job.getId(), joinStatus, joinResult);
114+
_jobMgr.completeAsyncJob(_job.getId(), joinStatus, 0, null);
115+
}
116+
68117
public static AsyncJobExecutionContext getCurrentExecutionContext() {
69118
return s_currentExectionContext.get();
70119
}

‎server/src/com/cloud/async/AsyncJobManager.java‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,35 @@
2525

2626
public interface AsyncJobManager extends Manager {
2727

28-
public AsyncJobVO getAsyncJob(long jobId);
28+
AsyncJobVO getAsyncJob(long jobId);
2929

30-
public List<? extends AsyncJob> findInstancePendingAsyncJobs(String instanceType, Long accountId);
30+
List<? extends AsyncJob> findInstancePendingAsyncJobs(String instanceType, Long accountId);
3131

32-
public long submitAsyncJob(AsyncJob job);
33-
public long submitAsyncJob(AsyncJob job, boolean scheduleJobExecutionInContext);
34-
public long submitAsyncJob(AsyncJob job, String syncObjType, long syncObjId);
35-
public AsyncJobResult queryAsyncJobResult(long jobId);
32+
long submitAsyncJob(AsyncJob job);
33+
long submitAsyncJob(AsyncJob job, boolean scheduleJobExecutionInContext);
34+
long submitAsyncJob(AsyncJob job, String syncObjType, long syncObjId);
35+
AsyncJobResult queryAsyncJobResult(long jobId);
3636

37-
public void completeAsyncJob(long jobId, int jobStatus, int resultCode, Object resultObject);
38-
public void updateAsyncJobStatus(long jobId, int processStatus, Object resultObject);
39-
public void updateAsyncJobAttachment(long jobId, String instanceType, Long instanceId);
40-
public void logJobJournal(long jobId, AsyncJob.JournalType journalType, String
37+
void completeAsyncJob(long jobId, int jobStatus, int resultCode, Object resultObject);
38+
void updateAsyncJobStatus(long jobId, int processStatus, Object resultObject);
39+
void updateAsyncJobAttachment(long jobId, String instanceType, Long instanceId);
40+
void logJobJournal(long jobId, AsyncJob.JournalType journalType, String
4141
journalText, String journalObjJson);
42+
43+
void joinJob(long jobId, long joinJobId);
44+
void disjoinJob(long jobId, long joinedJobId);
45+
void completeJoin(long joinJobId, int joinStatus, String joinResult);
4246

43-
public void releaseSyncSource();
44-
public void syncAsyncJobExecution(AsyncJob job, String syncObjType, long syncObjId, long queueSizeLimit);
47+
void releaseSyncSource();
48+
void syncAsyncJobExecution(AsyncJob job, String syncObjType, long syncObjId, long queueSizeLimit);
4549

46-
public boolean waitAndCheck(String[] wakupSubjects, long checkIntervalInMilliSeconds,
50+
boolean waitAndCheck(String[] wakupSubjects, long checkIntervalInMilliSeconds,
4751
long timeoutInMiliseconds, Predicate predicate);
4852

4953
/**
5054
* Queries for the status or final result of an async job.
5155
* @param cmd the command that specifies the job id
5256
* @return an async-call result object
5357
*/
54-
public AsyncJob queryAsyncJobResult(QueryAsyncJobResultCmd cmd);
58+
AsyncJob queryAsyncJobResult(QueryAsyncJobResultCmd cmd);
5559
}

‎server/src/com/cloud/async/AsyncJobManagerImpl.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@
3838
import org.apache.cloudstack.api.response.ExceptionResponse;
3939
import org.apache.cloudstack.framework.messagebus.MessageBus;
4040
import org.apache.cloudstack.framework.messagebus.MessageDetector;
41+
import org.apache.cloudstack.framework.messagebus.PublishScope;
42+
import org.apache.cloudstack.messagebus.TopicConstants;
4143
import org.apache.log4j.Logger;
4244
import org.apache.log4j.NDC;
4345

4446
import com.cloud.api.ApiSerializerHelper;
47+
import com.cloud.api.query.dao.AsyncJobJoinDao;
4548
import com.cloud.async.dao.AsyncJobDao;
49+
import com.cloud.async.dao.AsyncJobJoinMapDao;
4650
import com.cloud.async.dao.AsyncJobJournalDao;
4751
import com.cloud.cluster.ClusterManager;
4852
import com.cloud.cluster.ClusterManagerListener;
@@ -86,6 +90,7 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
8690
@Inject private AsyncJobDao _jobDao;
8791
@Inject private AsyncJobJournalDao _journalDao;
8892
@Inject private ConfigurationDao _configDao;
93+
@Inject private AsyncJobJoinMapDao _joinMapDao;
8994
@Inject private List<AsyncJobDispatcher> _jobDispatchers;
9095
@Inject private MessageBus _messageBus;
9196

@@ -216,6 +221,8 @@ public void completeAsyncJob(long jobId, int jobStatus, int resultCode, Object r
216221
job.setLastUpdated(DateUtil.currentGMTTime());
217222
_jobDao.update(jobId, job);
218223
txn.commit();
224+
225+
_messageBus.publish(null, TopicConstants.JOB_STATE, PublishScope.GLOBAL, (Long)jobId);
219226
} catch(Exception e) {
220227
s_logger.error("Unexpected exception while completing async job-" + jobId, e);
221228
txn.rollback();
@@ -291,6 +298,21 @@ public void logJobJournal(long jobId, AsyncJob.JournalType journalType, String
291298
_journalDao.persist(journal);
292299
}
293300

301+
@DB
302+
public void joinJob(long jobId, long joinJobId) {
303+
_joinMapDao.joinJob(jobId, joinJobId, this.getMsid());
304+
}
305+
306+
@DB
307+
public void disjoinJob(long jobId, long joinedJobId) {
308+
_joinMapDao.disjoinJob(jobId, joinedJobId);
309+
}
310+
311+
@DB
312+
public void completeJoin(long joinJobId, int joinStatus, String joinResult) {
313+
_joinMapDao.completeJoin(joinJobId, joinStatus, joinResult, this.getMsid());
314+
}
315+
294316
@Override
295317
public void syncAsyncJobExecution(AsyncJob job, String syncObjType, long syncObjId, long queueSizeLimit) {
296318
if(s_logger.isDebugEnabled()) {

‎server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
import com.cloud.vm.VirtualMachineName;
160160
import com.cloud.vm.VirtualMachineProfile;
161161
import com.cloud.vm.VmWorkStart;
162+
import com.cloud.vm.VmWorkStop;
162163
import com.cloud.vm.dao.ConsoleProxyDao;
163164
import com.cloud.vm.dao.UserVmDetailsDao;
164165
import com.cloud.vm.dao.VMInstanceDao;
@@ -2052,15 +2053,32 @@ public void vmWorkStart(VmWork work) {
20522053
try {
20532054
_itMgr.processVmStartWork(vm, ((VmWorkStart)work).getParams(),
20542055
user, account, ((VmWorkStart)work).getPlan());
2056+
2057+
AsyncJobExecutionContext.getCurrentExecutionContext().completeJobAndJoin(AsyncJobResult.STATUS_SUCCEEDED, null);
20552058
} catch(Exception e) {
20562059
s_logger.error("Exception in process VM-start work", e);
20572060
String result = SerializerHelper.toObjectSerializedString(e);
2058-
AsyncJobExecutionContext.getCurrentExecutionContext().completeAsyncJob(AsyncJobResult.STATUS_FAILED, 0, result);
2061+
AsyncJobExecutionContext.getCurrentExecutionContext().completeJobAndJoin(AsyncJobResult.STATUS_FAILED, result);
20592062
}
20602063
}
20612064

20622065
@Override
20632066
public void vmWorkStop(VmWork work) {
2064-
// TODO
2067+
assert(work instanceof VmWorkStop);
2068+
2069+
ConsoleProxyVO vm = findById(work.getVmId());
2070+
2071+
UserVO user = _entityMgr.findById(UserVO.class, work.getUserId());
2072+
AccountVO account = _entityMgr.findById(AccountVO.class, work.getAccountId());
2073+
2074+
try {
2075+
_itMgr.processVmStopWork(vm, ((VmWorkStop)work).isForceStop(), user, account);
2076+
2077+
AsyncJobExecutionContext.getCurrentExecutionContext().completeJobAndJoin(AsyncJobResult.STATUS_SUCCEEDED, null);
2078+
} catch(Exception e) {
2079+
s_logger.error("Exception in process VM-stop work", e);
2080+
String result = SerializerHelper.toObjectSerializedString(e);
2081+
AsyncJobExecutionContext.getCurrentExecutionContext().completeJobAndJoin(AsyncJobResult.STATUS_FAILED, result);
2082+
}
20652083
}
20662084
}

‎server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
import com.cloud.vm.VirtualMachineName;
145145
import com.cloud.vm.VirtualMachineProfile;
146146
import com.cloud.vm.VmWorkStart;
147+
import com.cloud.vm.VmWorkStop;
147148
import com.cloud.vm.dao.SecondaryStorageVmDao;
148149
import com.cloud.vm.dao.UserVmDetailsDao;
149150
import com.cloud.vm.dao.VMInstanceDao;
@@ -1495,16 +1496,33 @@ public void vmWorkStart(VmWork work) {
14951496

14961497
try {
14971498
_itMgr.processVmStartWork(vm, ((VmWorkStart)work).getParams(),
1498-
user, account, ((VmWorkStart)work).getPlan());
1499+
user, account, ((VmWorkStart)work).getPlan());
1500+
1501+
AsyncJobExecutionContext.getCurrentExecutionContext().completeJobAndJoin(AsyncJobResult.STATUS_SUCCEEDED, null);
14991502
} catch(Exception e) {
15001503
s_logger.error("Exception in process VM-start work", e);
15011504
String result = SerializerHelper.toObjectSerializedString(e);
1502-
AsyncJobExecutionContext.getCurrentExecutionContext().completeAsyncJob(AsyncJobResult.STATUS_FAILED, 0, result);
1505+
AsyncJobExecutionContext.getCurrentExecutionContext().completeJobAndJoin(AsyncJobResult.STATUS_FAILED, result);
15031506
}
15041507
}
15051508

15061509
@Override
15071510
public void vmWorkStop(VmWork work) {
1508-
// TODO
1511+
assert(work instanceof VmWorkStop);
1512+
1513+
SecondaryStorageVmVO vm = findById(work.getVmId());
1514+
1515+
UserVO user = _entityMgr.findById(UserVO.class, work.getUserId());
1516+
AccountVO account = _entityMgr.findById(AccountVO.class, work.getAccountId());
1517+
1518+
try {
1519+
_itMgr.processVmStopWork(vm, ((VmWorkStop)work).isForceStop(), user, account);
1520+
1521+
AsyncJobExecutionContext.getCurrentExecutionContext().completeJobAndJoin(AsyncJobResult.STATUS_SUCCEEDED, null);
1522+
} catch(Exception e) {
1523+
s_logger.error("Exception in process VM-stop work", e);
1524+
String result = SerializerHelper.toObjectSerializedString(e);
1525+
AsyncJobExecutionContext.getCurrentExecutionContext().completeJobAndJoin(AsyncJobResult.STATUS_FAILED, result);
1526+
}
15091527
}
15101528
}

‎server/src/com/cloud/vm/VirtualMachineManager.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,7 @@ <T extends VMInstanceVO> T migrateForScale(T vm, long srcHostId, DeployDestinati
208208
//
209209
<T extends VMInstanceVO> T processVmStartWork(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account, DeploymentPlan planToDeploy)
210210
throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException;
211+
212+
<T extends VMInstanceVO> boolean processVmStopWork(T vm, boolean forced, User user, Account account)
213+
throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException;
211214
}

0 commit comments

Comments
 (0)