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;
}
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;
}
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);
}
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());
}
}
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);
}
Aggregations