Search in sources :

Example 11 with SnapshotVO

use of com.cloud.storage.SnapshotVO in project cosmic by MissionCriticalCloud.

the class SnapshotManagerImpl method getParentSnapshot.

@Override
public SnapshotVO getParentSnapshot(final VolumeInfo volume) {
    final long preId = _snapshotDao.getLastSnapshot(volume.getId(), DataStoreRole.Primary);
    SnapshotVO preSnapshotVO = null;
    if (preId != 0 && !(volume.getLastPoolId() != null && !volume.getLastPoolId().equals(volume.getPoolId()))) {
        preSnapshotVO = _snapshotDao.findByIdIncludingRemoved(preId);
    }
    return preSnapshotVO;
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO)

Example 12 with SnapshotVO

use of com.cloud.storage.SnapshotVO in project cosmic by MissionCriticalCloud.

the class VMSnapshotManagerImpl method allocVMSnapshot.

@Override
public VMSnapshot allocVMSnapshot(final Long vmId, String vsDisplayName, final String vsDescription) throws ResourceAllocationException {
    final Account caller = getCaller();
    // check if VM exists
    final UserVmVO userVmVo = _userVMDao.findById(vmId);
    if (userVmVo == null) {
        throw new InvalidParameterValueException("Creating VM snapshot failed due to VM:" + vmId + " is a system VM or does not exist");
    }
    if (_snapshotDao.listByInstanceId(vmId, Snapshot.State.BackedUp).size() > 0 && !HypervisorType.KVM.equals(userVmVo.getHypervisorType())) {
        throw new InvalidParameterValueException("VM snapshot for this VM is not allowed. This VM has volumes attached which has snapshots, please remove all snapshots " + "before taking VM snapshot");
    }
    // VM snapshot with memory is not supported for vGPU Vms
    if (_serviceOfferingDetailsDao.findDetail(userVmVo.getServiceOfferingId(), GPU.Keys.vgpuType.toString()) != null) {
        throw new InvalidParameterValueException("VM snapshot with MEMORY is not supported for vGPU enabled VMs.");
    }
    // check hypervisor capabilities
    if (!_hypervisorCapabilitiesDao.isVmSnapshotEnabled(userVmVo.getHypervisorType(), "default")) {
        throw new InvalidParameterValueException("VM snapshot is not enabled for hypervisor type: " + userVmVo.getHypervisorType());
    }
    // parameter length check
    if (vsDisplayName != null && vsDisplayName.length() > 255) {
        throw new InvalidParameterValueException("Creating VM snapshot failed due to length of VM snapshot vsDisplayName should not exceed 255");
    }
    if (vsDescription != null && vsDescription.length() > 255) {
        throw new InvalidParameterValueException("Creating VM snapshot failed due to length of VM snapshot vsDescription should not exceed 255");
    }
    // VM snapshot display name must be unique for a VM
    final String timeString = DateUtil.getDateDisplayString(DateUtil.GMT_TIMEZONE, new Date(), DateUtil.YYYYMMDD_FORMAT);
    final String vmSnapshotName = userVmVo.getInstanceName() + "_VS_" + timeString;
    if (vsDisplayName == null) {
        vsDisplayName = vmSnapshotName;
    }
    if (_vmSnapshotDao.findByName(vmId, vsDisplayName) != null) {
        throw new InvalidParameterValueException("Creating VM snapshot failed due to VM snapshot with name '" + vsDisplayName + "' already exists");
    }
    if (userVmVo.getState() != VirtualMachine.State.Running) {
        throw new InvalidParameterValueException("You can only take VM snapshots when the VM is in 'Running' state. Try taking a volume snapshot instead.");
    }
    // check access
    _accountMgr.checkAccess(caller, null, true, userVmVo);
    // check max snapshot limit for per VM
    if (_vmSnapshotDao.findByVm(vmId).size() >= _vmSnapshotMax) {
        throw new CloudRuntimeException("Creating vm snapshot failed due to a VM can just have : " + _vmSnapshotMax + " VM snapshots. Please delete old ones");
    }
    // check if there are active volume snapshots tasks
    final List<VolumeVO> listVolumes = _volumeDao.findByInstance(vmId);
    for (final VolumeVO volume : listVolumes) {
        final List<SnapshotVO> activeSnapshots = _snapshotDao.listByInstanceId(volume.getInstanceId(), Snapshot.State.Creating, Snapshot.State.CreatedOnPrimary, Snapshot.State.BackingUp);
        if (activeSnapshots.size() > 0) {
            throw new CloudRuntimeException("There is other active volume snapshot tasks on the instance to which the volume is attached, please try again later.");
        }
        if (userVmVo.getHypervisorType() == HypervisorType.KVM && volume.getFormat() != ImageFormat.QCOW2) {
            throw new CloudRuntimeException("We only support create vm snapshots from vm with QCOW2 image");
        }
    }
    // check if there are other active VM snapshot tasks
    if (hasActiveVMSnapshotTasks(vmId)) {
        throw new CloudRuntimeException("There is other active vm snapshot tasks on the instance, please try again later");
    }
    try {
        final VMSnapshotVO vmSnapshotVo = new VMSnapshotVO(userVmVo.getAccountId(), userVmVo.getDomainId(), vmId, vsDescription, vmSnapshotName, vsDisplayName, userVmVo.getServiceOfferingId(), VMSnapshot.Type.DiskAndMemory, null);
        final VMSnapshot vmSnapshot = _vmSnapshotDao.persist(vmSnapshotVo);
        if (vmSnapshot == null) {
            throw new CloudRuntimeException("Failed to create snapshot for vm: " + vmId);
        }
        return vmSnapshot;
    } catch (final Exception e) {
        final String msg = e.getMessage();
        s_logger.error("Create vm snapshot record failed for vm: " + vmId + " due to: " + msg);
    }
    return null;
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) Date(java.util.Date) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) CloudException(com.cloud.exception.CloudException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 13 with SnapshotVO

use of com.cloud.storage.SnapshotVO in project cloudstack by apache.

the class SnapshotDaoImpl method updateVolumeIds.

@Override
public void updateVolumeIds(long oldVolId, long newVolId) {
    SearchCriteria<SnapshotVO> sc = VolumeIdSearch.create();
    sc.setParameters("volumeId", oldVolId);
    SnapshotVO snapshot = createForUpdate();
    snapshot.setVolumeId(newVolId);
    UpdateBuilder ub = getUpdateBuilder(snapshot);
    update(ub, sc, null);
}
Also used : SnapshotVO(com.cloud.storage.SnapshotVO) UpdateBuilder(com.cloud.utils.db.UpdateBuilder)

Example 14 with SnapshotVO

use of com.cloud.storage.SnapshotVO in project cloudstack by apache.

the class SnapshotTestWithFakeData method testConcurrentSnapshot.

@Test
public void testConcurrentSnapshot() throws URISyntaxException, InterruptedException, ExecutionException {
    DataStore store = createDataStore();
    final FakePrimaryDataStoreDriver dataStoreDriver = (FakePrimaryDataStoreDriver) store.getDriver();
    dataStoreDriver.makeTakeSnapshotSucceed(true);
    final VolumeInfo volumeInfo = createVolume(1L, store);
    Assert.assertTrue(volumeInfo.getState() == Volume.State.Ready);
    vol = volumeInfo;
    // final SnapshotPolicyVO policyVO = createSnapshotPolicy(vol.getId());
    ExecutorService pool = Executors.newFixedThreadPool(2);
    boolean result = false;
    List<Future<Boolean>> future = new ArrayList<Future<Boolean>>();
    for (int i = 0; i < 12; i++) {
        final int cnt = i;
        Future<Boolean> task = pool.submit(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                boolean r = true;
                try {
                    SnapshotVO snapshotVO = createSnapshotInDb(vol.getId());
                    VolumeObject volumeObject = (VolumeObject) vol;
                    Account account = mock(Account.class);
                    when(account.getId()).thenReturn(1L);
                    CreateSnapshotPayload createSnapshotPayload = mock(CreateSnapshotPayload.class);
                    when(createSnapshotPayload.getAccount()).thenReturn(account);
                    when(createSnapshotPayload.getSnapshotId()).thenReturn(snapshotVO.getId());
                    when(createSnapshotPayload.getSnapshotPolicyId()).thenReturn(0L);
                    volumeObject.addPayload(createSnapshotPayload);
                    if (cnt > 8) {
                        mockStorageMotionStrategy.makeBackupSnapshotSucceed(false);
                    }
                    SnapshotInfo newSnapshot = volumeService.takeSnapshot(vol);
                    if (newSnapshot == null) {
                        r = false;
                    }
                } catch (Exception e) {
                    r = false;
                }
                return r;
            }
        });
        Assert.assertTrue(task.get());
    }
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) CreateSnapshotPayload(com.cloud.storage.CreateSnapshotPayload) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) VolumeObject(org.apache.cloudstack.storage.volume.VolumeObject) URISyntaxException(java.net.URISyntaxException) ExecutionException(java.util.concurrent.ExecutionException) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 15 with SnapshotVO

use of com.cloud.storage.SnapshotVO in project cloudstack by apache.

the class SnapshotTestWithFakeData method createSnapshotInDb.

private SnapshotVO createSnapshotInDb(Long volumeId) {
    Snapshot.Type snapshotType = Snapshot.Type.DAILY;
    SnapshotVO snapshotVO = new SnapshotVO(dcId, 2, 1, 1L, 1L, UUID.randomUUID().toString(), (short) snapshotType.ordinal(), snapshotType.name(), 100, 1L, 100L, Hypervisor.HypervisorType.XenServer, LocationType.PRIMARY);
    return snapshotDao.persist(snapshotVO);
}
Also used : Snapshot(com.cloud.storage.Snapshot) SnapshotVO(com.cloud.storage.SnapshotVO)

Aggregations

SnapshotVO (com.cloud.storage.SnapshotVO)105 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)46 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)29 VolumeVO (com.cloud.storage.VolumeVO)28 Account (com.cloud.user.Account)22 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)22 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)18 ArrayList (java.util.ArrayList)17 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)16 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)16 SnapshotDataStoreVO (org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)16 HostVO (com.cloud.host.HostVO)15 VMTemplateVO (com.cloud.storage.VMTemplateVO)15 DB (com.cloud.utils.db.DB)14 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)12 ConfigurationException (javax.naming.ConfigurationException)12 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)11 UserVmVO (com.cloud.vm.UserVmVO)10 VMInstanceVO (com.cloud.vm.VMInstanceVO)10 ActionEvent (com.cloud.event.ActionEvent)9