Search in sources :

Example 1 with AsyncJobExecutionContext

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

the class VolumeApiServiceImplTest method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    _svc._volsDao = _volumeDao;
    _svc._accountMgr = _accountMgr;
    _svc._userVmDao = _userVmDao;
    _svc._storagePoolDao = _storagePoolDao;
    _svc._vmSnapshotDao = _vmSnapshotDao;
    _svc._vmInstanceDao = _vmInstanceDao;
    _svc._jobMgr = _jobMgr;
    _svc.volFactory = _volFactory;
    _svc.volService = volService;
    _svc._dcDao = _dcDao;
    _svc._resourceLimitMgr = _resourceLimitMgr;
    _svc._accountDao = _accountDao;
    _svc._hostDao = _hostDao;
    // mock caller context
    final AccountVO account = new AccountVO("admin", 1L, "networkDomain", Account.ACCOUNT_TYPE_NORMAL, "uuid");
    final AccountVO account2 = new AccountVO("Account2", 2L, "networkDomain", Account.ACCOUNT_TYPE_NORMAL, "uuid");
    final UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user, account);
    // mock async context
    final AsyncJobExecutionContext context = new AsyncJobExecutionContext();
    AsyncJobExecutionContext.init(_svc._jobMgr, _joinMapDao);
    final AsyncJobVO job = new AsyncJobVO();
    context.setJob(job);
    AsyncJobExecutionContext.setCurrentExecutionContext(context);
    final TransactionLegacy txn = TransactionLegacy.open("runVolumeDaoImplTest");
    try {
        // volume of running vm id=1
        final VolumeVO volumeOfRunningVm = new VolumeVO("root", 1L, 1L, 1L, 1L, 1L, "root", "root", Storage.ProvisioningType.THIN, 1, null, null, "root", Volume.Type.ROOT);
        when(_svc._volsDao.findById(1L)).thenReturn(volumeOfRunningVm);
        final UserVmVO runningVm = new UserVmVO(1L, "vm", "vm", 1, HypervisorType.XenServer, 1L, false, false, 1L, 1L, 1, 1L, null, "vm", null);
        runningVm.setState(State.Running);
        runningVm.setDataCenterId(1L);
        when(_svc._userVmDao.findById(1L)).thenReturn(runningVm);
        // volume of stopped vm id=2
        final VolumeVO volumeOfStoppedVm = new VolumeVO("root", 1L, 1L, 1L, 1L, 2L, "root", "root", Storage.ProvisioningType.THIN, 1, null, null, "root", Volume.Type.ROOT);
        volumeOfStoppedVm.setPoolId(1L);
        when(_svc._volsDao.findById(2L)).thenReturn(volumeOfStoppedVm);
        final UserVmVO stoppedVm = new UserVmVO(2L, "vm", "vm", 1, HypervisorType.XenServer, 1L, false, false, 1L, 1L, 1, 1L, null, "vm", null);
        stoppedVm.setState(State.Stopped);
        stoppedVm.setDataCenterId(1L);
        when(_svc._userVmDao.findById(2L)).thenReturn(stoppedVm);
        final StoragePoolVO unmanagedPool = new StoragePoolVO();
        when(_svc._storagePoolDao.findById(1L)).thenReturn(unmanagedPool);
        // volume of managed pool id=4
        final StoragePoolVO managedPool = new StoragePoolVO();
        managedPool.setManaged(true);
        when(_svc._storagePoolDao.findById(2L)).thenReturn(managedPool);
        final VolumeVO managedPoolVolume = new VolumeVO("root", 1L, 1L, 1L, 1L, 2L, "root", "root", Storage.ProvisioningType.THIN, 1, null, null, "root", Volume.Type.ROOT);
        managedPoolVolume.setPoolId(2L);
        when(_svc._volsDao.findById(4L)).thenReturn(managedPoolVolume);
        // non-root non-datadisk volume
        final VolumeInfo volumeWithIncorrectVolumeType = Mockito.mock(VolumeInfo.class);
        when(volumeWithIncorrectVolumeType.getId()).thenReturn(5L);
        when(volumeWithIncorrectVolumeType.getVolumeType()).thenReturn(Volume.Type.ISO);
        when(_svc.volFactory.getVolume(5L)).thenReturn(volumeWithIncorrectVolumeType);
        // correct root volume
        final VolumeInfo correctRootVolume = Mockito.mock(VolumeInfo.class);
        when(correctRootVolume.getId()).thenReturn(6L);
        when(correctRootVolume.getDataCenterId()).thenReturn(1L);
        when(correctRootVolume.getVolumeType()).thenReturn(Volume.Type.ROOT);
        when(correctRootVolume.getInstanceId()).thenReturn(null);
        when(_svc.volFactory.getVolume(6L)).thenReturn(correctRootVolume);
        final VolumeVO correctRootVolumeVO = new VolumeVO("root", 1L, 1L, 1L, 1L, 2L, "root", "root", Storage.ProvisioningType.THIN, 1, null, null, "root", Volume.Type.ROOT);
        when(_svc._volsDao.findById(6L)).thenReturn(correctRootVolumeVO);
        // managed root volume
        final VolumeInfo managedVolume = Mockito.mock(VolumeInfo.class);
        when(managedVolume.getId()).thenReturn(7L);
        when(managedVolume.getDataCenterId()).thenReturn(1L);
        when(managedVolume.getVolumeType()).thenReturn(Volume.Type.ROOT);
        when(managedVolume.getInstanceId()).thenReturn(null);
        when(managedVolume.getPoolId()).thenReturn(2L);
        when(_svc.volFactory.getVolume(7L)).thenReturn(managedVolume);
        final VolumeVO managedVolume1 = new VolumeVO("root", 1L, 1L, 1L, 1L, 2L, "root", "root", Storage.ProvisioningType.THIN, 1, null, null, "root", Volume.Type.ROOT);
        managedVolume1.setPoolId(2L);
        managedVolume1.setDataCenterId(1L);
        when(_svc._volsDao.findById(7L)).thenReturn(managedVolume1);
        // vm having root volume
        final UserVmVO vmHavingRootVolume = new UserVmVO(4L, "vm", "vm", 1, HypervisorType.XenServer, 1L, false, false, 1L, 1L, 1, 1L, null, "vm", null);
        vmHavingRootVolume.setState(State.Stopped);
        vmHavingRootVolume.setDataCenterId(1L);
        when(_svc._userVmDao.findById(4L)).thenReturn(vmHavingRootVolume);
        final List<VolumeVO> vols = new ArrayList<>();
        vols.add(new VolumeVO());
        when(_svc._volsDao.findByInstanceAndDeviceId(4L, 0L)).thenReturn(vols);
        // volume in uploaded state
        final VolumeInfo uploadedVolume = Mockito.mock(VolumeInfo.class);
        when(uploadedVolume.getId()).thenReturn(8L);
        when(uploadedVolume.getDataCenterId()).thenReturn(1L);
        when(uploadedVolume.getVolumeType()).thenReturn(Volume.Type.ROOT);
        when(uploadedVolume.getInstanceId()).thenReturn(null);
        when(uploadedVolume.getPoolId()).thenReturn(1L);
        when(uploadedVolume.getState()).thenReturn(Volume.State.Uploaded);
        when(_svc.volFactory.getVolume(8L)).thenReturn(uploadedVolume);
        final VolumeVO upVolume = new VolumeVO("root", 1L, 1L, 1L, 1L, 2L, "root", "root", Storage.ProvisioningType.THIN, 1, null, null, "root", Volume.Type.ROOT);
        upVolume.setPoolId(1L);
        upVolume.setDataCenterId(1L);
        upVolume.setState(Volume.State.Uploaded);
        when(_svc._volsDao.findById(8L)).thenReturn(upVolume);
        // helper dao methods mock
        when(_svc._vmSnapshotDao.findByVm(any(Long.class))).thenReturn(new ArrayList<>());
        when(_svc._vmInstanceDao.findById(any(Long.class))).thenReturn(stoppedVm);
    } finally {
        txn.close("runVolumeDaoImplTest");
    }
    // helper methods mock
    doNothing().when(_svc._accountMgr).checkAccess(any(Account.class), any(AccessType.class), any(Boolean.class), any(ControlledEntity.class));
    doNothing().when(_svc._jobMgr).updateAsyncJobAttachment(any(Long.class), any(String.class), any(Long.class));
    when(_svc._jobMgr.submitAsyncJob(any(AsyncJobVO.class), any(String.class), any(Long.class))).thenReturn(1L);
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) AsyncJobExecutionContext(com.cloud.framework.jobs.AsyncJobExecutionContext) ArrayList(java.util.ArrayList) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) AccountVO(com.cloud.user.AccountVO) AsyncJobVO(com.cloud.framework.jobs.impl.AsyncJobVO) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) UserVO(com.cloud.user.UserVO) ControlledEntity(com.cloud.acl.ControlledEntity) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) Matchers.anyLong(org.mockito.Matchers.anyLong) AccessType(com.cloud.acl.SecurityChecker.AccessType) Before(org.junit.Before)

Example 2 with AsyncJobExecutionContext

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

the class VirtualMachineManagerImpl method removeNicFromVm.

@Override
public boolean removeNicFromVm(final VirtualMachine vm, final Nic nic) throws ConcurrentOperationException, ResourceUnavailableException {
    final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext();
    if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) {
        // avoid re-entrance
        VmWorkJobVO placeHolder = null;
        placeHolder = createPlaceHolderWork(vm.getId());
        try {
            return orchestrateRemoveNicFromVm(vm, nic);
        } finally {
            if (placeHolder != null) {
                _workJobDao.expunge(placeHolder.getId());
            }
        }
    } else {
        final Outcome<VirtualMachine> outcome = removeNicFromVmThroughJobQueue(vm, nic);
        try {
            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 jobResult = _jobMgr.unmarshallResultObject(outcome.getJob());
        if (jobResult != null) {
            if (jobResult instanceof ResourceUnavailableException) {
                throw (ResourceUnavailableException) jobResult;
            } else if (jobResult instanceof ConcurrentOperationException) {
                throw (ConcurrentOperationException) 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 Boolean) {
                return (Boolean) jobResult;
            }
        }
        throw new RuntimeException("Job failed with un-handled exception");
    }
}
Also used : AsyncJobExecutionContext(com.cloud.framework.jobs.AsyncJobExecutionContext) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException)

Example 3 with AsyncJobExecutionContext

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

the class VirtualMachineManagerImpl method reConfigureVm.

@Override
public VMInstanceVO reConfigureVm(final String vmUuid, final ServiceOffering oldServiceOffering, final boolean reconfiguringOnExistingHost) throws ResourceUnavailableException, InsufficientServerCapacityException, 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 {
            return orchestrateReConfigureVm(vmUuid, oldServiceOffering, reconfiguringOnExistingHost);
        } finally {
            if (placeHolder != null) {
                _workJobDao.expunge(placeHolder.getId());
            }
        }
    } else {
        final Outcome<VirtualMachine> outcome = reconfigureVmThroughJobQueue(vmUuid, oldServiceOffering, reconfiguringOnExistingHost);
        VirtualMachine vm = null;
        try {
            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 jobResult = _jobMgr.unmarshallResultObject(outcome.getJob());
        if (jobResult != null) {
            if (jobResult instanceof ResourceUnavailableException) {
                throw (ResourceUnavailableException) jobResult;
            } else if (jobResult instanceof ConcurrentOperationException) {
                throw (ConcurrentOperationException) jobResult;
            } else if (jobResult instanceof InsufficientServerCapacityException) {
                throw (InsufficientServerCapacityException) jobResult;
            } else if (jobResult instanceof Throwable) {
                s_logger.error("Unhandled exception", (Throwable) jobResult);
                throw new RuntimeException("Unhandled exception", (Throwable) jobResult);
            }
        }
        return (VMInstanceVO) vm;
    }
}
Also used : AsyncJobExecutionContext(com.cloud.framework.jobs.AsyncJobExecutionContext) InsufficientServerCapacityException(com.cloud.exception.InsufficientServerCapacityException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException)

Example 4 with AsyncJobExecutionContext

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

the class VirtualMachineManagerImpl method migrateAway.

@Override
public void migrateAway(final String vmUuid, final long srcHostId) throws InsufficientServerCapacityException {
    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 {
            try {
                orchestrateMigrateAway(vmUuid, srcHostId, null);
            } catch (final InsufficientServerCapacityException e) {
                s_logger.warn("Failed to deploy vm " + vmUuid + " with original planner, sending HAPlanner");
                orchestrateMigrateAway(vmUuid, srcHostId, _haMgr.getHAPlanner());
            }
        } finally {
            _workJobDao.expunge(placeHolder.getId());
        }
    } else {
        final Outcome<VirtualMachine> outcome = migrateVmAwayThroughJobQueue(vmUuid, srcHostId);
        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 InsufficientServerCapacityException) {
                throw (InsufficientServerCapacityException) 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(com.cloud.framework.jobs.AsyncJobExecutionContext) InsufficientServerCapacityException(com.cloud.exception.InsufficientServerCapacityException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 5 with AsyncJobExecutionContext

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

the class VirtualMachineManagerImpl method advanceStart.

@Override
public void advanceStart(final String vmUuid, final Map<VirtualMachineProfile.Param, Object> params, final DeploymentPlan planToDeploy, final DeploymentPlanner planner) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
    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 {
            orchestrateStart(vmUuid, params, planToDeploy, planner);
        } finally {
            if (placeHolder != null) {
                _workJobDao.expunge(placeHolder.getId());
            }
        }
    } else {
        final Outcome<VirtualMachine> outcome = startVmThroughJobQueue(vmUuid, params, planToDeploy, planner);
        try {
            final VirtualMachine vm = outcome.get();
        } catch (final InterruptedException e) {
            throw new CloudRuntimeException("Operation is interrupted", e);
        } catch (final java.util.concurrent.ExecutionException e) {
            throw new CloudRuntimeException("Execution exception", e);
        }
        final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob());
        if (jobResult != null) {
            if (jobResult instanceof ConcurrentOperationException) {
                throw (ConcurrentOperationException) jobResult;
            } else if (jobResult instanceof ResourceUnavailableException) {
                throw (ResourceUnavailableException) jobResult;
            } else if (jobResult instanceof InsufficientCapacityException) {
                throw (InsufficientCapacityException) jobResult;
            } else if (jobResult instanceof RuntimeException) {
                throw (RuntimeException) jobResult;
            } else if (jobResult instanceof Throwable) {
                throw new CloudRuntimeException("Unexpected exception", (Throwable) jobResult);
            }
        }
    }
}
Also used : AsyncJobExecutionContext(com.cloud.framework.jobs.AsyncJobExecutionContext) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException)

Aggregations

AsyncJobExecutionContext (com.cloud.framework.jobs.AsyncJobExecutionContext)23 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)22 VmWorkJobVO (com.cloud.framework.jobs.impl.VmWorkJobVO)21 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)20 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 Account (com.cloud.user.Account)6 UserVmVO (com.cloud.vm.UserVmVO)6 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)4 VmWorkAttachVolume (com.cloud.vm.VmWorkAttachVolume)4 VmWorkDetachVolume (com.cloud.vm.VmWorkDetachVolume)4 VmWorkExtractVolume (com.cloud.vm.VmWorkExtractVolume)4 VmWorkMigrateVolume (com.cloud.vm.VmWorkMigrateVolume)4 VmWorkResizeVolume (com.cloud.vm.VmWorkResizeVolume)4 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)3 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)3 AsyncJob (com.cloud.framework.jobs.AsyncJob)3