Search in sources :

Example 46 with VmWorkJobVO

use of com.cloud.framework.jobs.impl.VmWorkJobVO in project cosmic by MissionCriticalCloud.

the class VMSnapshotManagerImpl method revertToSnapshot.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_SNAPSHOT_REVERT, eventDescription = "revert to VM snapshot", async = true)
public UserVm revertToSnapshot(final Long vmSnapshotId) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException {
    // check if VM snapshot exists in DB
    final VMSnapshotVO vmSnapshotVo = _vmSnapshotDao.findById(vmSnapshotId);
    if (vmSnapshotVo == null) {
        throw new InvalidParameterValueException("unable to find the vm snapshot with id " + vmSnapshotId);
    }
    final Long vmId = vmSnapshotVo.getVmId();
    final UserVmVO userVm = _userVMDao.findById(vmId);
    // check if VM exists
    if (userVm == null) {
        throw new InvalidParameterValueException("Revert vm to snapshot: " + vmSnapshotId + " failed due to vm: " + vmId + " is not found");
    }
    // check if there are other active VM snapshot tasks
    if (hasActiveVMSnapshotTasks(vmId)) {
        throw new InvalidParameterValueException("There is other active vm snapshot tasks on the instance, please try again later");
    }
    final Account caller = getCaller();
    _accountMgr.checkAccess(caller, null, true, vmSnapshotVo);
    // VM should be in running or stopped states
    if (userVm.getState() != VirtualMachine.State.Running && userVm.getState() != VirtualMachine.State.Stopped) {
        throw new InvalidParameterValueException("VM Snapshot reverting failed due to vm is not in the state of Running or Stopped.");
    }
    if (userVm.getState() == VirtualMachine.State.Running && vmSnapshotVo.getType() == VMSnapshot.Type.Disk || userVm.getState() == VirtualMachine.State.Stopped && vmSnapshotVo.getType() == VMSnapshot.Type.DiskAndMemory) {
        throw new InvalidParameterValueException("VM Snapshot revert not allowed. This will result in VM state change. You can revert running VM to disk and memory type snapshot and stopped VM to disk type" + " snapshot");
    }
    // if snapshot is not created, error out
    if (vmSnapshotVo.getState() != VMSnapshot.State.Ready) {
        throw new InvalidParameterValueException("VM Snapshot reverting failed due to vm snapshot is not in the state of Created.");
    }
    // serialize VM operation
    final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext();
    if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) {
        // avoid re-entrance
        final VmWorkJobVO placeHolder;
        placeHolder = createPlaceHolderWork(vmSnapshotVo.getVmId());
        try {
            return orchestrateRevertToVMSnapshot(vmSnapshotId);
        } finally {
            _workJobDao.expunge(placeHolder.getId());
        }
    } else {
        final Outcome<VMSnapshot> outcome = revertToVMSnapshotThroughJobQueue(vmSnapshotVo.getVmId(), vmSnapshotId);
        VMSnapshot result = null;
        try {
            result = outcome.get();
        } catch (final InterruptedException e) {
            throw new RuntimeException("Operation is interrupted", e);
        } catch (final ExecutionException e) {
            throw new RuntimeException("Execution excetion", e);
        }
        final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob());
        if (jobResult != null) {
            if (jobResult instanceof ConcurrentOperationException) {
                throw (ConcurrentOperationException) jobResult;
            } else if (jobResult instanceof InsufficientCapacityException) {
                throw (InsufficientCapacityException) jobResult;
            } else if (jobResult instanceof ResourceUnavailableException) {
                throw (ResourceUnavailableException) jobResult;
            } else if (jobResult instanceof Throwable) {
                throw new RuntimeException("Unexpected exception", (Throwable) jobResult);
            }
        }
        return userVm;
    }
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) AsyncJobExecutionContext(com.cloud.framework.jobs.AsyncJobExecutionContext) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ExecutionException(java.util.concurrent.ExecutionException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ActionEvent(com.cloud.event.ActionEvent)

Example 47 with VmWorkJobVO

use of com.cloud.framework.jobs.impl.VmWorkJobVO in project cosmic by MissionCriticalCloud.

the class VMSnapshotManagerImpl method deleteAllVMSnapshots.

@Override
public boolean deleteAllVMSnapshots(final long vmId, final VMSnapshot.Type type) {
    // serialize VM operation
    final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext();
    if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) {
        // avoid re-entrance
        final VmWorkJobVO placeHolder;
        placeHolder = createPlaceHolderWork(vmId);
        try {
            return orchestrateDeleteAllVMSnapshots(vmId, type);
        } finally {
            if (placeHolder != null) {
                _workJobDao.expunge(placeHolder.getId());
            }
        }
    } else {
        final Outcome<VirtualMachine> outcome = deleteAllVMSnapshotsThroughJobQueue(vmId, type);
        try {
            outcome.get();
        } catch (final InterruptedException e) {
            throw new RuntimeException("Operation is interrupted", e);
        } catch (final ExecutionException e) {
            throw new RuntimeException("Execution excetion", e);
        }
        final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob());
        if (jobResult != null) {
            if (jobResult instanceof ConcurrentOperationException) {
                throw (ConcurrentOperationException) jobResult;
            } else if (jobResult instanceof InvalidParameterValueException) {
                throw (InvalidParameterValueException) jobResult;
            } else if (jobResult instanceof Throwable) {
                throw new RuntimeException("Unexpected exception", (Throwable) jobResult);
            }
        }
        if (jobResult instanceof Boolean) {
            return (Boolean) jobResult;
        }
        return false;
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) AsyncJobExecutionContext(com.cloud.framework.jobs.AsyncJobExecutionContext) ExecutionException(java.util.concurrent.ExecutionException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 48 with VmWorkJobVO

use of com.cloud.framework.jobs.impl.VmWorkJobVO in project cosmic by MissionCriticalCloud.

the class VMSnapshotManagerImpl method deleteVMSnapshotThroughJobQueue.

public Outcome<VMSnapshot> deleteVMSnapshotThroughJobQueue(final Long vmId, final Long vmSnapshotId) {
    final CallContext context = CallContext.current();
    final User callingUser = context.getCallingUser();
    final Account callingAccount = context.getCallingAccount();
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    final VmWorkJobVO workJob = new VmWorkJobVO(context.getContextId());
    workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
    workJob.setCmd(VmWorkDeleteVMSnapshot.class.getName());
    workJob.setAccountId(callingAccount.getId());
    workJob.setUserId(callingUser.getId());
    workJob.setStep(VmWorkJobVO.Step.Starting);
    workJob.setVmType(VirtualMachine.Type.Instance);
    workJob.setVmInstanceId(vm.getId());
    workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
    // save work context info (there are some duplications)
    final VmWorkDeleteVMSnapshot workInfo = new VmWorkDeleteVMSnapshot(callingUser.getId(), callingAccount.getId(), vm.getId(), VMSnapshotManagerImpl.VM_WORK_JOB_HANDLER, vmSnapshotId);
    workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
    _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmJobVMSnapshotOutcome(workJob, vmSnapshotId);
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) VMInstanceVO(com.cloud.vm.VMInstanceVO) CallContext(com.cloud.context.CallContext) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO)

Example 49 with VmWorkJobVO

use of com.cloud.framework.jobs.impl.VmWorkJobVO in project cosmic by MissionCriticalCloud.

the class VMSnapshotManagerImpl 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(com.cloud.framework.jobs.impl.VmWorkJobVO)

Example 50 with VmWorkJobVO

use of com.cloud.framework.jobs.impl.VmWorkJobVO in project cosmic by MissionCriticalCloud.

the class VMSnapshotManagerImpl method createVMSnapshotThroughJobQueue.

public Outcome<VMSnapshot> createVMSnapshotThroughJobQueue(final Long vmId, final Long vmSnapshotId, final boolean quiesceVm) {
    final CallContext context = CallContext.current();
    final User callingUser = context.getCallingUser();
    final Account callingAccount = context.getCallingAccount();
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    final VmWorkJobVO workJob = new VmWorkJobVO(context.getContextId());
    workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
    workJob.setCmd(VmWorkCreateVMSnapshot.class.getName());
    workJob.setAccountId(callingAccount.getId());
    workJob.setUserId(callingUser.getId());
    workJob.setStep(VmWorkJobVO.Step.Starting);
    workJob.setVmType(VirtualMachine.Type.Instance);
    workJob.setVmInstanceId(vm.getId());
    workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
    // save work context info (there are some duplications)
    final VmWorkCreateVMSnapshot workInfo = new VmWorkCreateVMSnapshot(callingUser.getId(), callingAccount.getId(), vm.getId(), VMSnapshotManagerImpl.VM_WORK_JOB_HANDLER, vmSnapshotId, quiesceVm);
    workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
    _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmJobVMSnapshotOutcome(workJob, vmSnapshotId);
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) VMInstanceVO(com.cloud.vm.VMInstanceVO) CallContext(com.cloud.context.CallContext) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO)

Aggregations

VmWorkJobVO (com.cloud.framework.jobs.impl.VmWorkJobVO)58 Account (com.cloud.user.Account)27 CallContext (com.cloud.context.CallContext)22 User (com.cloud.user.User)22 AsyncJobExecutionContext (com.cloud.framework.jobs.AsyncJobExecutionContext)21 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)21 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)20 VMInstanceVO (com.cloud.vm.VMInstanceVO)13 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)10 ExecutionException (java.util.concurrent.ExecutionException)10 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)9 ActionEvent (com.cloud.event.ActionEvent)8 DataObject (com.cloud.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