Search in sources :

Example 36 with ResourceAllocationException

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

the class SnapshotManagerImpl method allocSnapshot.

@Override
public Snapshot allocSnapshot(final Long volumeId, final Long policyId, String snapshotName, final boolean fromVmSnapshot) throws ResourceAllocationException {
    final Account caller = CallContext.current().getCallingAccount();
    final VolumeInfo volume = this.volFactory.getVolume(volumeId);
    if (!fromVmSnapshot) {
        supportedByHypervisor(volume);
    }
    // Verify permissions
    this._accountMgr.checkAccess(caller, null, true, volume);
    final Type snapshotType = Type.MANUAL;
    final Account owner = this._accountMgr.getAccount(volume.getAccountId());
    try {
        this._resourceLimitMgr.checkResourceLimit(owner, ResourceType.snapshot);
        this._resourceLimitMgr.checkResourceLimit(owner, ResourceType.secondary_storage, volume.getSize());
    } catch (final ResourceAllocationException e) {
        throw e;
    }
    // Determine the name for this snapshot
    // Snapshot Name: VMInstancename + volumeName + timeString
    final String timeString = DateUtil.getDateDisplayString(DateUtil.GMT_TIMEZONE, new Date(), DateUtil.YYYYMMDD_FORMAT);
    final VMInstanceVO vmInstance = this._vmDao.findById(volume.getInstanceId());
    String vmDisplayName = "detached";
    if (vmInstance != null) {
        vmDisplayName = vmInstance.getHostName();
    }
    if (snapshotName == null) {
        snapshotName = vmDisplayName + "_" + volume.getName() + "_" + timeString;
    }
    HypervisorType hypervisorType;
    final StoragePoolVO storagePool = this._storagePoolDao.findById(volume.getDataStore().getId());
    if (storagePool.getScope() == ScopeType.ZONE) {
        hypervisorType = storagePool.getHypervisor();
        // at the time being, managed storage only supports XenServer, ESX(i), and KVM (i.e. not Hyper-V), so the VHD file type can be mapped to XenServer
        if (storagePool.isManaged() && HypervisorType.Any.equals(hypervisorType) && ImageFormat.VHD.equals(volume.getFormat())) {
            hypervisorType = HypervisorType.XenServer;
        }
    } else {
        hypervisorType = volume.getHypervisorType();
    }
    final SnapshotVO snapshotVO = new SnapshotVO(volume.getDataCenterId(), volume.getAccountId(), volume.getDomainId(), volume.getId(), volume.getDiskOfferingId(), snapshotName, (short) snapshotType.ordinal(), snapshotType.name(), volume.getSize(), volume.getMinIops(), volume.getMaxIops(), hypervisorType);
    final SnapshotVO snapshot = this._snapshotDao.persist(snapshotVO);
    if (snapshot == null) {
        throw new CloudRuntimeException("Failed to create snapshot for volume: " + volume.getId());
    }
    this._resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.snapshot);
    this._resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.secondary_storage, new Long(volume.getSize()));
    return snapshot;
}
Also used : HypervisorType(com.cloud.model.enumeration.HypervisorType) Account(com.cloud.legacymodel.user.Account) ResourceObjectType(com.cloud.server.ResourceTag.ResourceObjectType) ScopeType(com.cloud.storage.ScopeType) HypervisorType(com.cloud.model.enumeration.HypervisorType) Type(com.cloud.storage.Snapshot.Type) ResourceType(com.cloud.legacymodel.configuration.Resource.ResourceType) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) Date(java.util.Date)

Example 37 with ResourceAllocationException

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

the class VolumeApiServiceImplTest method testResourceLimitCheckForUploadedVolume.

// The resource limit check for primary storage should not be skipped for Volume in 'Uploaded' state.
@Test
public void testResourceLimitCheckForUploadedVolume() throws NoSuchFieldException, IllegalAccessException, ResourceAllocationException {
    doThrow(new ResourceAllocationException("primary storage resource limit check failed", Resource.ResourceType.primary_storage)).when(_svc._resourceLimitMgr).checkResourceLimit(any(AccountVO.class), any(Resource.ResourceType.class), any(Long.class));
    final UserVmVO vm = Mockito.mock(UserVmVO.class);
    final VolumeInfo volumeToAttach = Mockito.mock(VolumeInfo.class);
    when(volumeToAttach.getId()).thenReturn(9L);
    when(volumeToAttach.getDataCenterId()).thenReturn(34L);
    when(volumeToAttach.getVolumeType()).thenReturn(VolumeType.DATADISK);
    when(volumeToAttach.getInstanceId()).thenReturn(null);
    when(_userVmDao.findById(anyLong())).thenReturn(vm);
    when(vm.getType()).thenReturn(VirtualMachineType.User);
    when(vm.getState()).thenReturn(State.Running);
    when(vm.getDataCenterId()).thenReturn(34L);
    when(_svc._volsDao.findByInstanceAndType(anyLong(), any(VolumeType.class))).thenReturn(new ArrayList(10));
    when(_svc.volFactory.getVolume(9L)).thenReturn(volumeToAttach);
    when(volumeToAttach.getState()).thenReturn(Volume.State.Uploaded);
    final DataCenterVO zoneWithDisabledLocalStorage = Mockito.mock(DataCenterVO.class);
    when(_svc._dcDao.findById(anyLong())).thenReturn(zoneWithDisabledLocalStorage);
    try {
        _svc.attachVolumeToVM(2L, 9L, null);
    } catch (final InvalidParameterValueException e) {
        Assert.assertEquals(e.getMessage(), ("primary storage resource limit check failed"));
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) UserVmVO(com.cloud.vm.UserVmVO) VolumeType(com.cloud.model.enumeration.VolumeType) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Matchers.anyLong(org.mockito.Matchers.anyLong) ArrayList(java.util.ArrayList) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Aggregations

ResourceAllocationException (com.cloud.legacymodel.exceptions.ResourceAllocationException)37 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)20 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)19 InsufficientCapacityException (com.cloud.legacymodel.exceptions.InsufficientCapacityException)18 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)18 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)17 ServerApiException (com.cloud.api.ServerApiException)14 DB (com.cloud.utils.db.DB)13 Account (com.cloud.legacymodel.user.Account)11 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)8 ArrayList (java.util.ArrayList)8 ConfigurationException (javax.naming.ConfigurationException)8 InsufficientAddressCapacityException (com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException)7 TransactionStatus (com.cloud.utils.db.TransactionStatus)7 ExecutionException (java.util.concurrent.ExecutionException)6 NetworkRuleConflictException (com.cloud.legacymodel.exceptions.NetworkRuleConflictException)5 Network (com.cloud.legacymodel.network.Network)5 HypervisorType (com.cloud.model.enumeration.HypervisorType)5 TransactionCallbackWithException (com.cloud.utils.db.TransactionCallbackWithException)5 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)4