Search in sources :

Example 31 with VmWorkJobVO

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

the class VolumeApiServiceImpl method resizeVolume.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_RESIZE, eventDescription = "resizing volume", async = true)
public VolumeVO resizeVolume(ResizeVolumeCmd cmd) throws ResourceAllocationException {
    Long newSize = null;
    Long newMinIops = null;
    Long newMaxIops = null;
    Integer newHypervisorSnapshotReserve = null;
    boolean shrinkOk = cmd.getShrinkOk();
    VolumeVO volume = _volsDao.findById(cmd.getEntityId());
    if (volume == null) {
        throw new InvalidParameterValueException("No such volume");
    }
    /* Does the caller have authority to act on this volume? */
    _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, volume);
    if (volume.getInstanceId() != null) {
        // Check that Vm to which this volume is attached does not have VM Snapshots
        if (_vmSnapshotDao.findByVm(volume.getInstanceId()).size() > 0) {
            throw new InvalidParameterValueException("Volume cannot be resized which is attached to VM with VM Snapshots");
        }
    }
    DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId());
    DiskOfferingVO newDiskOffering = null;
    if (cmd.getNewDiskOfferingId() != null && volume.getDiskOfferingId() != cmd.getNewDiskOfferingId()) {
        newDiskOffering = _diskOfferingDao.findById(cmd.getNewDiskOfferingId());
    }
    /* Only works for KVM/XenServer/VMware (or "Any") for now, and volumes with 'None' since they're just allocated in DB */
    HypervisorType hypervisorType = _volsDao.getHypervisorType(volume.getId());
    if (hypervisorType != HypervisorType.KVM && hypervisorType != HypervisorType.XenServer && hypervisorType != HypervisorType.VMware && hypervisorType != HypervisorType.Any && hypervisorType != HypervisorType.None) {
        throw new InvalidParameterValueException("Hypervisor " + hypervisorType + " does not support  rootdisksize override");
    }
    if (volume.getState() != Volume.State.Ready && volume.getState() != Volume.State.Allocated) {
        throw new InvalidParameterValueException("Volume should be in ready or allocated state before attempting a resize. Volume " + volume.getUuid() + " is in state " + volume.getState() + ".");
    }
    // if we are to use the existing disk offering
    if (newDiskOffering == null) {
        newSize = cmd.getSize();
        newHypervisorSnapshotReserve = volume.getHypervisorSnapshotReserve();
        // if the caller is looking to change the size of the volume
        if (newSize != null) {
            if (!diskOffering.isCustomized() && !volume.getVolumeType().equals(Volume.Type.ROOT)) {
                throw new InvalidParameterValueException("To change a volume's size without providing a new disk offering, its current disk offering must be " + "customizable or it must be a root volume (if providing a disk offering, make sure it is different from the current disk offering).");
            }
            // convert from bytes to GiB
            newSize = newSize << 30;
        } else {
            // no parameter provided; just use the original size of the volume
            newSize = volume.getSize();
        }
        newMinIops = cmd.getMinIops();
        if (newMinIops != null) {
            if (diskOffering.isCustomizedIops() == null || !diskOffering.isCustomizedIops()) {
                throw new InvalidParameterValueException("The current disk offering does not support customization of the 'Min IOPS' parameter.");
            }
        } else {
            // no parameter provided; just use the original min IOPS of the volume
            newMinIops = volume.getMinIops();
        }
        newMaxIops = cmd.getMaxIops();
        if (newMaxIops != null) {
            if (diskOffering.isCustomizedIops() == null || !diskOffering.isCustomizedIops()) {
                throw new InvalidParameterValueException("The current disk offering does not support customization of the 'Max IOPS' parameter.");
            }
        } else {
            // no parameter provided; just use the original max IOPS of the volume
            newMaxIops = volume.getMaxIops();
        }
        validateIops(newMinIops, newMaxIops);
    } else {
        if (newDiskOffering.getRemoved() != null) {
            throw new InvalidParameterValueException("Requested disk offering has been removed.");
        }
        if (!DiskOfferingVO.Type.Disk.equals(newDiskOffering.getType())) {
            throw new InvalidParameterValueException("Requested disk offering type is invalid.");
        }
        if (diskOffering.getTags() != null) {
            if (!StringUtils.areTagsEqual(diskOffering.getTags(), newDiskOffering.getTags())) {
                throw new InvalidParameterValueException("The tags on the new and old disk offerings must match.");
            }
        } else if (newDiskOffering.getTags() != null) {
            throw new InvalidParameterValueException("There are no tags on the current disk offering. The new disk offering needs to have no tags, as well.");
        }
        if (newDiskOffering.getDomainId() != null) {
            // not a public offering; check access
            _configMgr.checkDiskOfferingAccess(CallContext.current().getCallingAccount(), newDiskOffering);
        }
        if (newDiskOffering.isCustomized()) {
            newSize = cmd.getSize();
            if (newSize == null) {
                throw new InvalidParameterValueException("The new disk offering requires that a size be specified.");
            }
            // convert from bytes to GiB
            newSize = newSize << 30;
        } else {
            newSize = newDiskOffering.getDiskSize();
        }
        if (!volume.getSize().equals(newSize) && !volume.getVolumeType().equals(Volume.Type.DATADISK)) {
            throw new InvalidParameterValueException("Only data volumes can be resized via a new disk offering.");
        }
        if (newDiskOffering.isCustomizedIops() != null && newDiskOffering.isCustomizedIops()) {
            newMinIops = cmd.getMinIops() != null ? cmd.getMinIops() : volume.getMinIops();
            newMaxIops = cmd.getMaxIops() != null ? cmd.getMaxIops() : volume.getMaxIops();
            validateIops(newMinIops, newMaxIops);
        } else {
            newMinIops = newDiskOffering.getMinIops();
            newMaxIops = newDiskOffering.getMaxIops();
        }
        // if the hypervisor snapshot reserve value is null, it must remain null (currently only KVM uses null and null is all KVM uses for a value here)
        newHypervisorSnapshotReserve = volume.getHypervisorSnapshotReserve() != null ? newDiskOffering.getHypervisorSnapshotReserve() : null;
    }
    long currentSize = volume.getSize();
    // if the caller is looking to change the size of the volume
    if (currentSize != newSize) {
        if (!validateVolumeSizeRange(newSize)) {
            throw new InvalidParameterValueException("Requested size out of range");
        }
        /*
             * Let's make certain they (think they) know what they're doing if they
             * want to shrink by forcing them to provide the shrinkok parameter.
             * This will be checked again at the hypervisor level where we can see
             * the actual disk size.
             */
        if (currentSize > newSize && !shrinkOk) {
            throw new InvalidParameterValueException("Going from existing size of " + currentSize + " to size of " + newSize + " would shrink the volume." + "Need to sign off by supplying the shrinkok parameter with value of true.");
        }
        if (newSize > currentSize) {
            /* Check resource limit for this account on primary storage resource */
            _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(volume.getAccountId()), ResourceType.primary_storage, volume.isDisplayVolume(), new Long(newSize - currentSize).longValue());
        }
    }
    /* If this volume has never been beyond allocated state, short circuit everything and simply update the database. */
    if (volume.getState() == Volume.State.Allocated) {
        s_logger.debug("Volume is in the allocated state, but has never been created. Simply updating database with new size and IOPS.");
        volume.setSize(newSize);
        volume.setMinIops(newMinIops);
        volume.setMaxIops(newMaxIops);
        volume.setHypervisorSnapshotReserve(newHypervisorSnapshotReserve);
        if (newDiskOffering != null) {
            volume.setDiskOfferingId(cmd.getNewDiskOfferingId());
        }
        _volsDao.update(volume.getId(), volume);
        return volume;
    }
    UserVmVO userVm = _userVmDao.findById(volume.getInstanceId());
    if (userVm != null) {
        if (volume.getVolumeType().equals(Volume.Type.ROOT) && userVm.getPowerState() != VirtualMachine.PowerState.PowerOff && hypervisorType == HypervisorType.VMware) {
            s_logger.error(" For ROOT volume resize VM should be in Power Off state.");
            throw new InvalidParameterValueException("VM current state is : " + userVm.getPowerState() + ". But VM should be in " + VirtualMachine.PowerState.PowerOff + " state.");
        }
        // serialize VM operation
        AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext();
        if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) {
            // avoid re-entrance
            VmWorkJobVO placeHolder = null;
            placeHolder = createPlaceHolderWork(userVm.getId());
            try {
                return orchestrateResizeVolume(volume.getId(), currentSize, newSize, newMinIops, newMaxIops, newHypervisorSnapshotReserve, newDiskOffering != null ? cmd.getNewDiskOfferingId() : null, shrinkOk);
            } finally {
                _workJobDao.expunge(placeHolder.getId());
            }
        } else {
            Outcome<Volume> outcome = resizeVolumeThroughJobQueue(userVm.getId(), volume.getId(), currentSize, newSize, newMinIops, newMaxIops, newHypervisorSnapshotReserve, newDiskOffering != null ? cmd.getNewDiskOfferingId() : null, shrinkOk);
            try {
                outcome.get();
            } catch (InterruptedException e) {
                throw new RuntimeException("Operation was interrupted", e);
            } catch (java.util.concurrent.ExecutionException e) {
                throw new RuntimeException("Execution exception", e);
            }
            Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob());
            if (jobResult != null) {
                if (jobResult instanceof ConcurrentOperationException) {
                    throw (ConcurrentOperationException) jobResult;
                } else if (jobResult instanceof ResourceAllocationException) {
                    throw (ResourceAllocationException) jobResult;
                } else if (jobResult instanceof RuntimeException) {
                    throw (RuntimeException) jobResult;
                } else if (jobResult instanceof Throwable) {
                    throw new RuntimeException("Unexpected exception", (Throwable) jobResult);
                } else if (jobResult instanceof Long) {
                    return _volsDao.findById((Long) jobResult);
                }
            }
            return volume;
        }
    }
    return orchestrateResizeVolume(volume.getId(), currentSize, newSize, newMinIops, newMaxIops, newHypervisorSnapshotReserve, newDiskOffering != null ? cmd.getNewDiskOfferingId() : null, shrinkOk);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) AsyncJobExecutionContext(org.apache.cloudstack.framework.jobs.AsyncJobExecutionContext) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VmWorkJobVO(org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) ExecutionException(java.util.concurrent.ExecutionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VmWorkDetachVolume(com.cloud.vm.VmWorkDetachVolume) VmWorkMigrateVolume(com.cloud.vm.VmWorkMigrateVolume) VmWorkResizeVolume(com.cloud.vm.VmWorkResizeVolume) VmWorkAttachVolume(com.cloud.vm.VmWorkAttachVolume) VmWorkExtractVolume(com.cloud.vm.VmWorkExtractVolume) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 32 with VmWorkJobVO

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

the class VolumeApiServiceImpl method attachVolumeToVmThroughJobQueue.

public Outcome<Volume> attachVolumeToVmThroughJobQueue(final Long vmId, final Long volumeId, final Long deviceId) {
    final CallContext context = CallContext.current();
    final User callingUser = context.getCallingUser();
    final Account callingAccount = context.getCallingAccount();
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    VmWorkJobVO workJob = new VmWorkJobVO(context.getContextId());
    workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
    workJob.setCmd(VmWorkAttachVolume.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)
    VmWorkAttachVolume workInfo = new VmWorkAttachVolume(callingUser.getId(), callingAccount.getId(), vm.getId(), VolumeApiServiceImpl.VM_WORK_JOB_HANDLER, volumeId, deviceId);
    workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
    _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    AsyncJobVO jobVo = _jobMgr.getAsyncJob(workJob.getId());
    s_logger.debug("New job " + workJob.getId() + ", result field: " + jobVo.getResult());
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmJobVolumeOutcome(workJob, volumeId);
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) VmWorkAttachVolume(com.cloud.vm.VmWorkAttachVolume) VMInstanceVO(com.cloud.vm.VMInstanceVO) CallContext(org.apache.cloudstack.context.CallContext) VmWorkJobVO(org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO) AsyncJobVO(org.apache.cloudstack.framework.jobs.impl.AsyncJobVO)

Example 33 with VmWorkJobVO

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

the class VolumeApiServiceImpl method detachVolumeFromVmThroughJobQueue.

public Outcome<Volume> detachVolumeFromVmThroughJobQueue(final Long vmId, final Long volumeId) {
    final CallContext context = CallContext.current();
    final User callingUser = context.getCallingUser();
    final Account callingAccount = context.getCallingAccount();
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    VmWorkJobVO workJob = new VmWorkJobVO(context.getContextId());
    workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
    workJob.setCmd(VmWorkDetachVolume.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)
    VmWorkDetachVolume workInfo = new VmWorkDetachVolume(callingUser.getId(), callingAccount.getId(), vm.getId(), VolumeApiServiceImpl.VM_WORK_JOB_HANDLER, volumeId);
    workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
    _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmJobVolumeOutcome(workJob, volumeId);
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) VMInstanceVO(com.cloud.vm.VMInstanceVO) VmWorkDetachVolume(com.cloud.vm.VmWorkDetachVolume) CallContext(org.apache.cloudstack.context.CallContext) VmWorkJobVO(org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)

Example 34 with VmWorkJobVO

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

the class VolumeApiServiceImpl method createPlaceHolderWork.

private VmWorkJobVO createPlaceHolderWork(long instanceId) {
    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)

Example 35 with VmWorkJobVO

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

the class VolumeApiServiceImpl method resizeVolumeThroughJobQueue.

public Outcome<Volume> resizeVolumeThroughJobQueue(final Long vmId, final long volumeId, final long currentSize, final long newSize, final Long newMinIops, final Long newMaxIops, final Integer newHypervisorSnapshotReserve, final Long newServiceOfferingId, final boolean shrinkOk) {
    final CallContext context = CallContext.current();
    final User callingUser = context.getCallingUser();
    final Account callingAccount = context.getCallingAccount();
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    VmWorkJobVO workJob = new VmWorkJobVO(context.getContextId());
    workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
    workJob.setCmd(VmWorkResizeVolume.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)
    VmWorkResizeVolume workInfo = new VmWorkResizeVolume(callingUser.getId(), callingAccount.getId(), vm.getId(), VolumeApiServiceImpl.VM_WORK_JOB_HANDLER, volumeId, currentSize, newSize, newMinIops, newMaxIops, newHypervisorSnapshotReserve, newServiceOfferingId, shrinkOk);
    workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
    _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmJobVolumeOutcome(workJob, volumeId);
}
Also used : Account(com.cloud.user.Account) VmWorkResizeVolume(com.cloud.vm.VmWorkResizeVolume) User(com.cloud.user.User) VMInstanceVO(com.cloud.vm.VMInstanceVO) CallContext(org.apache.cloudstack.context.CallContext) 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