Search in sources :

Example 1 with VmWorkJobVO

use of org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO in project cloudstack by apache.

the class VirtualMachineManagerImpl method migrateWithStorage.

@Override
public void migrateWithStorage(final String vmUuid, final long srcHostId, final long destHostId, final Map<Long, Long> volumeToPool) throws ResourceUnavailableException, ConcurrentOperationException {
    final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext();
    if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) {
        // avoid re-entrance
        VmWorkJobVO placeHolder = null;
        final VirtualMachine vm = _vmDao.findByUuid(vmUuid);
        placeHolder = createPlaceHolderWork(vm.getId());
        try {
            orchestrateMigrateWithStorage(vmUuid, srcHostId, destHostId, volumeToPool);
        } finally {
            if (placeHolder != null) {
                _workJobDao.expunge(placeHolder.getId());
            }
        }
    } else {
        final Outcome<VirtualMachine> outcome = migrateVmWithStorageThroughJobQueue(vmUuid, srcHostId, destHostId, volumeToPool);
        try {
            final VirtualMachine vm = outcome.get();
        } catch (final InterruptedException e) {
            throw new RuntimeException("Operation is interrupted", e);
        } catch (final java.util.concurrent.ExecutionException e) {
            throw new RuntimeException("Execution excetion", e);
        }
        final Object jobException = _jobMgr.unmarshallResultObject(outcome.getJob());
        if (jobException != null) {
            if (jobException instanceof ResourceUnavailableException) {
                throw (ResourceUnavailableException) jobException;
            } else if (jobException instanceof ConcurrentOperationException) {
                throw (ConcurrentOperationException) jobException;
            } else if (jobException instanceof RuntimeException) {
                throw (RuntimeException) jobException;
            } else if (jobException instanceof Throwable) {
                throw new RuntimeException("Unexpected exception", (Throwable) jobException);
            }
        }
    }
}
Also used : AsyncJobExecutionContext(org.apache.cloudstack.framework.jobs.AsyncJobExecutionContext) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VmWorkJobVO(org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException)

Example 2 with VmWorkJobVO

use of org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO in project cloudstack by apache.

the class VirtualMachineManagerImpl method removeVmFromNetworkThroughJobQueue.

public Outcome<VirtualMachine> removeVmFromNetworkThroughJobQueue(final VirtualMachine vm, final Network network, final URI broadcastUri) {
    final CallContext context = CallContext.current();
    final User user = context.getCallingUser();
    final Account account = context.getCallingAccount();
    final List<VmWorkJobVO> pendingWorkJobs = _workJobDao.listPendingWorkJobs(VirtualMachine.Type.Instance, vm.getId(), VmWorkRemoveVmFromNetwork.class.getName());
    VmWorkJobVO workJob = null;
    if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) {
        assert pendingWorkJobs.size() == 1;
        workJob = pendingWorkJobs.get(0);
    } else {
        workJob = new VmWorkJobVO(context.getContextId());
        workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
        workJob.setCmd(VmWorkRemoveVmFromNetwork.class.getName());
        workJob.setAccountId(account.getId());
        workJob.setUserId(user.getId());
        workJob.setVmType(VirtualMachine.Type.Instance);
        workJob.setVmInstanceId(vm.getId());
        workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
        // save work context info (there are some duplications)
        final VmWorkRemoveVmFromNetwork workInfo = new VmWorkRemoveVmFromNetwork(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, network, broadcastUri);
        workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
        _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    }
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmJobVirtualMachineOutcome(workJob, vm.getId());
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) CallContext(org.apache.cloudstack.context.CallContext) VmWorkJobVO(org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)

Example 3 with VmWorkJobVO

use of org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO in project cloudstack by apache.

the class VirtualMachineManagerImpl method rebootVmThroughJobQueue.

public Outcome<VirtualMachine> rebootVmThroughJobQueue(final String vmUuid, final Map<VirtualMachineProfile.Param, Object> params) {
    final CallContext context = CallContext.current();
    final Account account = context.getCallingAccount();
    final User user = context.getCallingUser();
    final VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
    final List<VmWorkJobVO> pendingWorkJobs = _workJobDao.listPendingWorkJobs(VirtualMachine.Type.Instance, vm.getId(), VmWorkReboot.class.getName());
    VmWorkJobVO workJob = null;
    if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) {
        assert pendingWorkJobs.size() == 1;
        workJob = pendingWorkJobs.get(0);
    } else {
        workJob = new VmWorkJobVO(context.getContextId());
        workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
        workJob.setCmd(VmWorkReboot.class.getName());
        workJob.setAccountId(account.getId());
        workJob.setUserId(user.getId());
        workJob.setStep(VmWorkJobVO.Step.Prepare);
        workJob.setVmType(VirtualMachine.Type.Instance);
        workJob.setVmInstanceId(vm.getId());
        workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
        // save work context info (there are some duplications)
        final VmWorkReboot workInfo = new VmWorkReboot(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, params);
        workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
        _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    }
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmJobVirtualMachineOutcome(workJob, vm.getId());
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) CallContext(org.apache.cloudstack.context.CallContext) VmWorkJobVO(org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)

Example 4 with VmWorkJobVO

use of org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO in project cloudstack by apache.

the class VmWorkJobWakeupDispatcher method runJob.

@Override
public void runJob(AsyncJob job) {
    try {
        List<AsyncJobJoinMapVO> joinRecords = _joinMapDao.listJoinRecords(job.getId());
        if (joinRecords.size() != 1) {
            s_logger.warn("AsyncJob-" + job.getId() + " received wakeup call with un-supported joining job number: " + joinRecords.size());
            // if we fail wakeup-execution for any reason, avoid release sync-source if there is any
            job.setSyncSource(null);
            return;
        }
        AsyncJobJoinMapVO joinRecord = joinRecords.get(0);
        VmWorkJobVO joinedJob = _workjobDao.findById(joinRecord.getJoinJobId());
        Class<?> workClz = null;
        try {
            workClz = Class.forName(job.getCmd());
        } catch (ClassNotFoundException e) {
            s_logger.error("VM work class " + job.getCmd() + " is not found", e);
            return;
        }
        // get original work context information from joined job
        VmWork work = VmWorkSerializer.deserialize(workClz, joinedJob.getCmdInfo());
        assert (work != null);
        AccountVO account = _accountDao.findById(work.getAccountId());
        assert (account != null);
        VMInstanceVO vm = _instanceDao.findById(work.getVmId());
        assert (vm != null);
        CallContext.register(work.getUserId(), work.getAccountId(), job.getRelated());
        try {
            Method handler = getHandler(joinRecord.getWakeupHandler());
            if (handler != null) {
                handler.invoke(_vmMgr);
            } else {
                assert (false);
                s_logger.error("Unable to find wakeup handler " + joinRecord.getWakeupHandler() + " when waking up job-" + job.getId());
            }
        } finally {
            CallContext.unregister();
        }
    } catch (Throwable e) {
        s_logger.warn("Unexpected exception in waking up job-" + job.getId());
        // if we fail wakeup-execution for any reason, avoid release sync-source if there is any
        job.setSyncSource(null);
    }
}
Also used : AsyncJobJoinMapVO(org.apache.cloudstack.framework.jobs.impl.AsyncJobJoinMapVO) Method(java.lang.reflect.Method) AccountVO(com.cloud.user.AccountVO) VmWorkJobVO(org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)

Example 5 with VmWorkJobVO

use of org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO in project cloudstack by apache.

the class VirtualMachineManagerImpl method createPlaceHolderWork.

private VmWorkJobVO createPlaceHolderWork(final long instanceId) {
    final VmWorkJobVO workJob = new VmWorkJobVO("");
    workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_PLACEHOLDER);
    workJob.setCmd("");
    workJob.setCmdInfo("");
    workJob.setAccountId(0);
    workJob.setUserId(0);
    workJob.setStep(VmWorkJobVO.Step.Starting);
    workJob.setVmType(VirtualMachine.Type.Instance);
    workJob.setVmInstanceId(instanceId);
    workJob.setInitMsid(ManagementServerNode.getManagementServerId());
    _workJobDao.persist(workJob);
    return workJob;
}
Also used : VmWorkJobVO(org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)

Aggregations

VmWorkJobVO (org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)52 Account (com.cloud.user.Account)27 User (com.cloud.user.User)22 CallContext (org.apache.cloudstack.context.CallContext)22 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)21 AsyncJobExecutionContext (org.apache.cloudstack.framework.jobs.AsyncJobExecutionContext)21 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)20 VMInstanceVO (com.cloud.vm.VMInstanceVO)13 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)10 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)9 ActionEvent (com.cloud.event.ActionEvent)8 ExecutionException (java.util.concurrent.ExecutionException)6 DataObject (org.apache.cloudstack.engine.subsystem.api.storage.DataObject)6 UserVmVO (com.cloud.vm.UserVmVO)5 VmWorkAttachVolume (com.cloud.vm.VmWorkAttachVolume)5 VmWorkDetachVolume (com.cloud.vm.VmWorkDetachVolume)5 VmWorkExtractVolume (com.cloud.vm.VmWorkExtractVolume)5 VmWorkMigrateVolume (com.cloud.vm.VmWorkMigrateVolume)5 VmWorkResizeVolume (com.cloud.vm.VmWorkResizeVolume)5 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)4