More fixes on starting from jobs

This commit is contained in:
Alex Huang 2013-06-04 20:10:03 -07:00
parent 2c75352f7c
commit 2e1e2babcb
12 changed files with 70 additions and 79 deletions

View File

@ -95,7 +95,7 @@ public class CallContext {
CallContext callingContext = new CallContext(callingUser, callingAccount, sessionId);
s_currentContext.set(callingContext);
if (sessionId != null) {
NDC.push(sessionId);
NDC.push("job-" + sessionId);
}
s_logger.debug("Setting calling context: " + s_currentContext.get());
return callingContext;

View File

@ -27,7 +27,6 @@ import com.cloud.agent.api.to.NicTO;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.deploy.DeployDestination;
import com.cloud.deploy.DeploymentPlan;
import com.cloud.exception.AgentUnavailableException;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.InsufficientServerCapacityException;
@ -201,14 +200,5 @@ public interface VirtualMachineManager extends Manager {
throws ResourceUnavailableException, ConcurrentOperationException,
ManagementServerException, VirtualMachineMigrationException;
//
// VM work handlers
//
VirtualMachine processVmStartWork(String vmUuid, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account, DeploymentPlan planToDeploy)
throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException;
boolean processVmStopWork(String vmUuid, boolean forced, User user, Account account)
throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException;
NicTO toNicTO(NicProfile nic, HypervisorType hypervisorType);
}

View File

@ -90,8 +90,6 @@ public interface AsyncJob extends Job {
@Override
Date getLastPolled();
Date getRemoved();
@Override
String getInstanceType();

View File

@ -125,12 +125,12 @@ public class AsyncJobVO implements AsyncJob, Job {
uuid = UUID.randomUUID().toString();
}
public AsyncJobVO(long userId, long accountId, String cmd, String cmdInfo, Long instanceId, String instanceType) {
public AsyncJobVO(String uuid, long userId, long accountId, String cmd, String cmdInfo, Long instanceId, String instanceType) {
this.userId = userId;
this.accountId = accountId;
this.cmd = cmd;
this.cmdInfo = cmdInfo;
uuid = UUID.randomUUID().toString();
this.uuid = uuid;
this.instanceId = instanceId;
this.instanceType = instanceType;
}
@ -313,15 +313,6 @@ public class AsyncJobVO implements AsyncJob, Job {
this.lastPolled = lastPolled;
}
@Override
public Date getRemoved() {
return removed;
}
public void setRemoved(Date removed) {
this.removed = removed;
}
@Override
public String getInstanceType() {
return instanceType;

View File

@ -520,7 +520,7 @@ public class ApiServer extends ManagerBase implements HttpRequestHandler, ApiSer
params.put("ctxStartEventId", String.valueOf(startEventId));
Long instanceId = (objectId == null) ? asyncCmd.getInstanceId() : objectId;
AsyncJobVO job = new AsyncJobVO(callerUserId, caller.getId(), cmdObj.getClass().getName(),
AsyncJobVO job = new AsyncJobVO(ctx.getSessionId(), callerUserId, caller.getId(), cmdObj.getClass().getName(),
ApiGsonHelper.getBuilder().create().toJson(params), instanceId,
asyncCmd.getInstanceType() != null ? asyncCmd.getInstanceType().toString() : null);
job.setDispatcher(_asyncDispatcher.getName());

View File

@ -147,7 +147,20 @@ public class AsyncJobExecutionContext {
return context;
}
public static void setCurrentExecutionContext(AsyncJobExecutionContext currentContext) {
public static AsyncJobExecutionContext registerPseudoExecutionContext() {
AsyncJobExecutionContext context = s_currentExectionContext.get();
if (context == null) {
context = new AsyncJobExecutionContext();
context = ComponentContext.inject(context);
context.getJob();
setCurrentExecutionContext(context);
}
return context;
}
// This is intended to be package level access for AsyncJobManagerImpl only.
static void setCurrentExecutionContext(AsyncJobExecutionContext currentContext) {
s_currentExectionContext.set(currentContext);
}
}

View File

@ -34,7 +34,6 @@ import javax.inject.Inject;
import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.command.user.job.QueryAsyncJobResultCmd;
@ -517,8 +516,6 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
//
// setup execution environment
//
NDC.push("job-" + job.getId());
txn = Transaction.open(Transaction.CLOUD_DB);
try {
@ -588,7 +585,6 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
if(txn != null)
txn.close();
NDC.pop();
} catch(Throwable e) {
s_logger.error("Double exception", e);
}

View File

@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import javax.ejb.Local;
import javax.inject.Inject;
@ -247,7 +248,7 @@ public class SnapshotSchedulerImpl extends ManagerBase implements SnapshotSchedu
params.put("id", ""+cmd.getEntityId());
params.put("ctxStartEventId", "1");
AsyncJobVO job = new AsyncJobVO(User.UID_SYSTEM, volume.getAccountId(), CreateSnapshotCmd.class.getName(),
AsyncJobVO job = new AsyncJobVO(UUID.randomUUID().toString(), User.UID_SYSTEM, volume.getAccountId(), CreateSnapshotCmd.class.getName(),
ApiGsonHelper.getBuilder().create().toJson(params), cmd.getEntityId(),
cmd.getInstanceType() != null ? cmd.getInstanceType().toString() : null);

View File

@ -24,6 +24,7 @@ import org.apache.log4j.Logger;
import org.apache.cloudstack.context.CallContext;
import com.cloud.async.AsyncJobExecutionContext;
import com.cloud.exception.CloudAuthenticationException;
import com.cloud.utils.Pair;
import com.cloud.utils.concurrency.NamedThreadFactory;
@ -72,6 +73,7 @@ public class SystemVmLoadScanner<T> {
public void run() {
try {
CallContext.registerOnceOnly();
AsyncJobExecutionContext.registerPseudoExecutionContext();
} catch (CloudAuthenticationException e) {
s_logger.error("Unable to start the capacity scan task", e);
System.exit(1);

View File

@ -266,20 +266,24 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
protected AffinityGroupVMMapDao _affinityGroupVMMapDao;
protected List<DeploymentPlanner> _planners;
public List<DeploymentPlanner> getPlanners() {
return _planners;
}
public void setPlanners(List<DeploymentPlanner> _planners) {
this._planners = _planners;
}
return _planners;
}
public void setPlanners(List<DeploymentPlanner> _planners) {
this._planners = _planners;
}
protected List<HostAllocator> _hostAllocators;
public List<HostAllocator> getHostAllocators() {
return _hostAllocators;
}
public void setHostAllocators(List<HostAllocator> _hostAllocators) {
this._hostAllocators = _hostAllocators;
}
return _hostAllocators;
}
public void setHostAllocators(List<HostAllocator> _hostAllocators) {
this._hostAllocators = _hostAllocators;
}
@Inject
protected List<StoragePoolAllocator> _storagePoolAllocators;
@ -804,9 +808,12 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
return;
}
@Override
public VirtualMachine processVmStartWork(String vmUuid, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account, DeploymentPlan planToDeploy)
public void processVmStartWork(String vmUuid, Map<VirtualMachineProfile.Param, Object> params, DeploymentPlan planToDeploy)
throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
CallContext context = CallContext.current();
User caller = context.getCallingUser();
Account account = context.getCallingAccount();
VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
VirtualMachineGuru vmGuru = getVmGuru(vm);
@ -997,7 +1004,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
if (s_logger.isDebugEnabled()) {
s_logger.debug("Start completed for VM " + vm);
}
return startedVm;
return;
} else {
if (s_logger.isDebugEnabled()) {
s_logger.info("The guru did not like the answers so stopping " + vm);
@ -1073,8 +1080,6 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
throw new CloudRuntimeException("Unable to start instance '" + vm.getHostName()
+ "' (" + vm.getUuid() + "), see management server log for details");
}
return startedVm;
}
@Override
@ -1259,9 +1264,12 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
}
}
@Override
public boolean processVmStopWork(String vmUuid, boolean forced, User user, Account account) throws AgentUnavailableException,
public void processVmStopWork(String vmUuid, boolean forced) throws AgentUnavailableException,
OperationTimedoutException, ConcurrentOperationException {
CallContext context = CallContext.current();
User user = context.getCallingUser();
Account account = context.getCallingAccount();
VmWorkJobVO work = _workJobDao.findById(AsyncJobExecutionContext.getCurrentExecutionContext().getJob().getId());
final VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
@ -1271,7 +1279,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
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;
return;
}
try {
@ -1281,7 +1289,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
}
_workJobDao.updateStep(work.getId(), VmWorkJobVO.Step.Done);
return true;
return;
}
VirtualMachineGuru vmGuru = getVmGuru(vm);
@ -1313,10 +1321,11 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
if (s_logger.isDebugEnabled()) {
s_logger.debug("Updating work item to Done, id:" + work.getId());
}
return changeState(vm, Event.AgentReportStopped, null, work, Step.Done);
changeState(vm, Event.AgentReportStopped, null, work, Step.Done);
return;
} catch (NoTransitionException e) {
s_logger.warn("Unable to cleanup " + vm);
return false;
return;
}
} else {
if (s_logger.isDebugEnabled()) {
@ -1351,7 +1360,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
} catch (NoTransitionException e) {
s_logger.warn("Unable to transition the state " + vm);
}
return false;
return;
} else {
s_logger.warn("Unable to actually stop " + vm + " but continue with release because it's a force stop");
vmGuru.finalizeStop(profile, answer);
@ -1387,10 +1396,11 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
_workJobDao.updateStep(work.getId(), VmWorkJobVO.Step.Done);
}
return stateTransitTo(vm, Event.OperationSucceeded, null, null);
stateTransitTo(vm, Event.OperationSucceeded, null, null);
return;
} catch (NoTransitionException e) {
s_logger.warn(e.getMessage());
return false;
return;
}
}

View File

@ -41,7 +41,8 @@ public class VmWorkJobDispatcher extends AdapterBase implements AsyncJobDispatch
public final static String Start = "start";
public final static String Stop = "stop";
@Inject private VirtualMachineManager _vmMgr;
@Inject
private VirtualMachineManagerImpl _vmMgr;
@Inject private AsyncJobManager _asyncJobMgr;
@Inject private AccountDao _accountDao;
@Inject private VMInstanceDao _instanceDao;
@ -50,25 +51,32 @@ public class VmWorkJobDispatcher extends AdapterBase implements AsyncJobDispatch
@Override
public void runJob(AsyncJob job) {
VmWork work = null;
try {
String cmd = job.getCmd();
assert(cmd != null);
VmWork work = (VmWork)ApiSerializerHelper.fromSerializedString(job.getCmdInfo());
work = (VmWork)ApiSerializerHelper.fromSerializedString(job.getCmdInfo());
assert(work != null);
CallContext context = CallContext.register(work.getUserId(), work.getAccountId(), "job-" + job.getShortUuid());
VMInstanceVO vm = _instanceDao.findById(work.getVmId());
if (vm == null) {
s_logger.info("Unable to find vm " + work.getVmId());
}
assert(vm != null);
CallContext context = CallContext.register(work.getUserId(), work.getAccountId(), "job-" + job.getShortUuid());
if (cmd.equals(Start)) {
_vmMgr.start(vm.getUuid(), null, context.getCallingUser(), context.getCallingAccount());
VmWorkStart start = (VmWorkStart)work;
_vmMgr.processVmStartWork(vm.getUuid(), start.getParams(), start.getPlan());
} else if (cmd.equals(Stop)) {
_vmMgr.stop(vm.getUuid(), context.getCallingUser(), context.getCallingAccount());
VmWorkStop stop = (VmWorkStop)work;
_vmMgr.processVmStopWork(vm.getUuid(), stop.isForceStop());
}
_asyncJobMgr.completeAsyncJob(job.getId(), AsyncJobConstants.STATUS_SUCCEEDED, 0, null);
} catch(Throwable e) {
s_logger.error("Unexpected exception", e);
s_logger.error("Unable to complete " + job, e);
_asyncJobMgr.completeAsyncJob(job.getId(), AsyncJobConstants.STATUS_FAILED, 0, e);
} finally {
CallContext.unregister();

View File

@ -341,18 +341,6 @@ public class VmWorkMockVirtualMachineManagerImpl implements VirtualMachineManage
return false;
}
@Override
public VirtualMachine processVmStartWork(String vmUuid, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account, DeploymentPlan planToDeploy)
throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
try {
Thread.sleep(120000);
} catch (InterruptedException e) {
}
return null;
}
int wakeupCount = 0;
public void processVmStartWakeup() {
s_logger.info("processVmStartWakeup. job-" + AsyncJobExecutionContext.getCurrentExecutionContext().getJob().getId());
@ -364,12 +352,6 @@ public class VmWorkMockVirtualMachineManagerImpl implements VirtualMachineManage
}
}
@Override
public boolean processVmStopWork(String vmUuid, boolean forced, User user, Account account)
throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException {
return true;
}
@Override
public VirtualMachine migrateWithStorage(String vmUuid, long srcId, long destId, Map<VolumeVO, StoragePoolVO> volumeToPool) throws ResourceUnavailableException,
ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException {