Search in sources :

Example 61 with ConcurrentOperationException

use of com.cloud.legacymodel.exceptions.ConcurrentOperationException in project cosmic by MissionCriticalCloud.

the class VMSnapshotManagerImpl method deleteVMSnapshot.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_SNAPSHOT_DELETE, eventDescription = "delete vm snapshots", async = true)
public boolean deleteVMSnapshot(final Long vmSnapshotId) {
    final Account caller = getCaller();
    final VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(vmSnapshotId);
    if (vmSnapshot == null) {
        throw new InvalidParameterValueException("unable to find the vm snapshot with id " + vmSnapshotId);
    }
    _accountMgr.checkAccess(caller, null, true, vmSnapshot);
    // check VM snapshot states, only allow to delete vm snapshots in created and error state
    if (VMSnapshot.State.Ready != vmSnapshot.getState() && VMSnapshot.State.Expunging != vmSnapshot.getState() && VMSnapshot.State.Error != vmSnapshot.getState()) {
        throw new InvalidParameterValueException("Can't delete the vm snapshotshot " + vmSnapshotId + " due to it is not in Created or Error, or Expunging State");
    }
    // check if there are other active VM snapshot tasks
    if (hasActiveVMSnapshotTasks(vmSnapshot.getVmId())) {
        final List<VMSnapshotVO> expungingSnapshots = _vmSnapshotDao.listByInstanceId(vmSnapshot.getVmId(), VMSnapshot.State.Expunging);
        if (expungingSnapshots.size() > 0 && expungingSnapshots.get(0).getId() == vmSnapshot.getId()) {
            s_logger.debug("Target VM snapshot already in expunging state, go on deleting it: " + vmSnapshot.getDisplayName());
        } else {
            throw new InvalidParameterValueException("There is other active vm snapshot tasks on the instance, please try again later");
        }
    }
    // serialize VM operation
    final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext();
    if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) {
        // avoid re-entrance
        final VmWorkJobVO placeHolder;
        placeHolder = createPlaceHolderWork(vmSnapshot.getVmId());
        try {
            return orchestrateDeleteVMSnapshot(vmSnapshotId);
        } finally {
            _workJobDao.expunge(placeHolder.getId());
        }
    } else {
        final Outcome<VMSnapshot> outcome = deleteVMSnapshotThroughJobQueue(vmSnapshot.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 Throwable) {
                throw new RuntimeException("Unexpected exception", (Throwable) jobResult);
            }
        }
        if (jobResult instanceof Boolean) {
            return (Boolean) jobResult;
        }
        return false;
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) AsyncJobExecutionContext(com.cloud.framework.jobs.AsyncJobExecutionContext) VMSnapshot(com.cloud.legacymodel.storage.VMSnapshot) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ExecutionException(java.util.concurrent.ExecutionException) ActionEvent(com.cloud.event.ActionEvent)

Example 62 with ConcurrentOperationException

use of com.cloud.legacymodel.exceptions.ConcurrentOperationException 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.legacymodel.user.Account) UserVmVO(com.cloud.vm.UserVmVO) AsyncJobExecutionContext(com.cloud.framework.jobs.AsyncJobExecutionContext) VMSnapshot(com.cloud.legacymodel.storage.VMSnapshot) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) ExecutionException(java.util.concurrent.ExecutionException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) ActionEvent(com.cloud.event.ActionEvent)

Example 63 with ConcurrentOperationException

use of com.cloud.legacymodel.exceptions.ConcurrentOperationException in project cosmic by MissionCriticalCloud.

the class VMSnapshotManagerImpl method createVMSnapshot.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_SNAPSHOT_CREATE, eventDescription = "creating VM snapshot", async = true)
public VMSnapshot createVMSnapshot(final Long vmId, final Long vmSnapshotId, final Boolean quiescevm) {
    final UserVmVO userVm = _userVMDao.findById(vmId);
    if (userVm == null) {
        throw new InvalidParameterValueException("Create vm to snapshot failed due to vm: " + vmId + " is not found");
    }
    final VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(vmSnapshotId);
    if (vmSnapshot == null) {
        throw new CloudRuntimeException("VM snapshot id: " + vmSnapshotId + " can not be found");
    }
    // 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 orchestrateCreateVMSnapshot(vmId, vmSnapshotId, quiescevm);
        } finally {
            _workJobDao.expunge(placeHolder.getId());
        }
    } else {
        final Outcome<VMSnapshot> outcome = createVMSnapshotThroughJobQueue(vmId, vmSnapshotId, quiescevm);
        final VMSnapshot result;
        try {
            result = outcome.get();
        } catch (final InterruptedException e) {
            throw new RuntimeException("Operation is interrupted", e);
        } catch (final ExecutionException e) {
            throw new RuntimeException("Execution exception", e);
        }
        final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob());
        if (jobResult != null) {
            if (jobResult instanceof ConcurrentOperationException) {
                throw (ConcurrentOperationException) jobResult;
            } else if (jobResult instanceof Throwable) {
                throw new RuntimeException("Unexpected exception", (Throwable) jobResult);
            }
        }
        return result;
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) AsyncJobExecutionContext(com.cloud.framework.jobs.AsyncJobExecutionContext) VMSnapshot(com.cloud.legacymodel.storage.VMSnapshot) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) ActionEvent(com.cloud.event.ActionEvent)

Example 64 with ConcurrentOperationException

use of com.cloud.legacymodel.exceptions.ConcurrentOperationException in project cosmic by MissionCriticalCloud.

the class VMSnapshotManagerImpl method orchestrateRevertToVMSnapshot.

private UserVm orchestrateRevertToVMSnapshot(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 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.");
    }
    final UserVmVO vm;
    Long hostId = null;
    // start or stop VM first, if revert from stopped state to running state, or from running to stopped
    if (userVm.getState() == VirtualMachine.State.Stopped && vmSnapshotVo.getType() == VMSnapshot.Type.DiskAndMemory) {
        try {
            _itMgr.advanceStart(userVm.getUuid(), new HashMap<>(), null);
            vm = _userVMDao.findById(userVm.getId());
            hostId = vm.getHostId();
        } catch (final Exception e) {
            s_logger.error("Start VM " + userVm.getInstanceName() + " before reverting failed due to " + e.getMessage());
            throw new CloudRuntimeException(e.getMessage());
        }
    } else {
        if (userVm.getState() == VirtualMachine.State.Running && vmSnapshotVo.getType() == VMSnapshot.Type.Disk) {
            try {
                _itMgr.advanceStop(userVm.getUuid(), true);
            } catch (final Exception e) {
                s_logger.error("Stop VM " + userVm.getInstanceName() + " before reverting failed due to " + e.getMessage());
                throw new CloudRuntimeException(e.getMessage());
            }
        }
    }
    // check if there are other active VM snapshot tasks
    if (hasActiveVMSnapshotTasks(userVm.getId())) {
        throw new InvalidParameterValueException("There is other active vm snapshot tasks on the instance, please try again later");
    }
    try {
        final VMSnapshotStrategy strategy = findVMSnapshotStrategy(vmSnapshotVo);
        strategy.revertVMSnapshot(vmSnapshotVo);
        return userVm;
    } catch (final Exception e) {
        s_logger.debug("Failed to revert vmsnapshot: " + vmSnapshotId, e);
        throw new CloudRuntimeException(e.getMessage());
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) UserVmVO(com.cloud.vm.UserVmVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VMSnapshotStrategy(com.cloud.engine.subsystem.api.storage.VMSnapshotStrategy) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) ConfigurationException(javax.naming.ConfigurationException) CloudException(com.cloud.legacymodel.exceptions.CloudException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException)

Example 65 with ConcurrentOperationException

use of com.cloud.legacymodel.exceptions.ConcurrentOperationException in project cosmic by MissionCriticalCloud.

the class CreatePrivateNetworkTest method createInvalidlyHostedPrivateNetwork.

@Test
@DB
public void createInvalidlyHostedPrivateNetwork() {
    final TransactionLegacy __txn;
    __txn = TransactionLegacy.open("createInvalidlyHostedPrivateNetworkTest");
    /* Network nw; */
    try {
        /* nw = */
        networkService.createPrivateNetwork("bla", "fake", 1L, "vlan:1", "10.1.1.2", null, "10.1.1.1", "255.255.255.0", 1L, 1L, true, 1L);
        /* nw = */
        networkService.createPrivateNetwork("bla", "fake", 1L, "lswitch:3", "10.1.1.2", null, "10.1.1.1", "255.255.255.0", 1L, 1L, false, 1L);
        boolean invalid = false;
        boolean unsupported = false;
        try {
            /* nw = */
            networkService.createPrivateNetwork("bla", "fake", 1, "bla:2", "10.1.1.2", null, "10.1.1.1", "255.255.255.0", 1, 1L, true, 1L);
        } catch (final CloudRuntimeException e) {
            Assert.assertEquals("unexpected parameter exception", "string 'bla:2' has an unknown BroadcastDomainType.", e.getMessage());
            invalid = true;
        }
        try {
            /* nw = */
            networkService.createPrivateNetwork("bla", "fake", 1, "mido://4", "10.1.1.2", null, "10.1.1.1", "255.255.255.0", 1, 1L, false, 1L);
        } catch (final InvalidParameterValueException e) {
            Assert.assertEquals("unexpected parameter exception", "unsupported type of broadcastUri specified: mido://4", e.getMessage());
            unsupported = true;
        }
        Assert.assertEquals("'bla' should not be accepted as scheme", true, invalid);
        Assert.assertEquals("'mido' should not yet be supported as scheme", true, unsupported);
    } catch (final ResourceAllocationException e) {
        s_logger.error("no resources", e);
        fail("no resources");
    } catch (final ConcurrentOperationException e) {
        s_logger.error("another one is in the way", e);
        fail("another one is in the way");
    } catch (final InsufficientCapacityException e) {
        s_logger.error("no capacity", e);
        fail("no capacity");
    } finally {
        __txn.close("createInvalidlyHostedPrivateNetworkTest");
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) Test(org.junit.Test) DB(com.cloud.utils.db.DB)

Aggregations

ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)101 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)63 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)58 InsufficientCapacityException (com.cloud.legacymodel.exceptions.InsufficientCapacityException)50 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)37 ServerApiException (com.cloud.api.ServerApiException)30 ResourceAllocationException (com.cloud.legacymodel.exceptions.ResourceAllocationException)28 Account (com.cloud.legacymodel.user.Account)23 AsyncJobExecutionContext (com.cloud.framework.jobs.AsyncJobExecutionContext)20 VmWorkJobVO (com.cloud.framework.jobs.impl.VmWorkJobVO)20 VirtualMachine (com.cloud.legacymodel.vm.VirtualMachine)20 DB (com.cloud.utils.db.DB)19 InsufficientAddressCapacityException (com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException)17 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)17 ConfigurationException (javax.naming.ConfigurationException)16 OperationTimedoutException (com.cloud.legacymodel.exceptions.OperationTimedoutException)14 Network (com.cloud.legacymodel.network.Network)14 ActionEvent (com.cloud.event.ActionEvent)13 NetworkVO (com.cloud.network.dao.NetworkVO)13 VirtualMachineMigrationException (com.cloud.legacymodel.exceptions.VirtualMachineMigrationException)12