Search in sources :

Example 51 with Snapshot

use of org.ovirt.engine.core.common.businessentities.Snapshot in project ovirt-engine by oVirt.

the class RemoveDiskSnapshotsCommand method removeCinderSnapshotDisks.

private void removeCinderSnapshotDisks() {
    List<CinderDisk> cinderDisks = DisksFilter.filterCinderDisks(getImages());
    if (cinderDisks.isEmpty()) {
        return;
    }
    Future<ActionReturnValue> future = commandCoordinatorUtil.executeAsyncCommand(ActionType.RemoveAllCinderSnapshotDisks, buildRemoveCinderSnapshotDiskParameters(cinderDisks), cloneContextAndDetachFromParent());
    try {
        ActionReturnValue actionReturnValue = future.get();
        if (!actionReturnValue.getSucceeded()) {
            log.error("Error removing snapshots for Cinder disks");
            endWithFailure();
            getParameters().setTaskGroupSuccess(false);
        } else {
            Snapshot snapshotWithoutImage = null;
            Snapshot snapshot = snapshotDao.get(cinderDisks.get(0).getSnapshotId());
            lockVmSnapshotsWithWait(getVm());
            for (CinderDisk cinderDisk : cinderDisks) {
                snapshotWithoutImage = imagesHandler.prepareSnapshotConfigWithoutImageSingleImage(snapshot, cinderDisk.getImageId(), ovfManager);
            }
            snapshotDao.update(snapshotWithoutImage);
            if (getSnapshotsEngineLock() != null) {
                lockManager.releaseLock(getSnapshotsEngineLock());
            }
            endSuccessfully();
            getParameters().setTaskGroupSuccess(true);
        }
    } catch (InterruptedException | ExecutionException e) {
        log.error("Error removing snapshots for Cinder disks");
        endWithFailure();
        getParameters().setTaskGroupSuccess(false);
    }
}
Also used : Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot) ActionReturnValue(org.ovirt.engine.core.common.action.ActionReturnValue) CinderDisk(org.ovirt.engine.core.common.businessentities.storage.CinderDisk) ExecutionException(java.util.concurrent.ExecutionException)

Example 52 with Snapshot

use of org.ovirt.engine.core.common.businessentities.Snapshot in project ovirt-engine by oVirt.

the class SnapshotsManager method updateImagesByConfiguration.

private boolean updateImagesByConfiguration(VM vm, List<DiskImage> images) {
    Map<Guid, VM> snapshotVmConfigurations = new HashMap<>();
    ArrayList<DiskImage> imagesFromVmConf = new ArrayList<>();
    for (DiskImage image : images) {
        Guid vmSnapshotId = image.getVmSnapshotId();
        VM vmFromConf = snapshotVmConfigurations.get(vmSnapshotId);
        if (vmFromConf == null) {
            vmFromConf = new VM();
            Snapshot snapshot = snapshotDao.get(image.getVmSnapshotId());
            if (!updateVmFromConfiguration(vmFromConf, snapshot.getVmConfiguration())) {
                return false;
            }
            snapshotVmConfigurations.put(vmSnapshotId, vmFromConf);
        }
        for (DiskImage imageFromVmConf : vmFromConf.getImages()) {
            if (imageFromVmConf.getId().equals(image.getId())) {
                imageFromVmConf.setStorageIds(image.getStorageIds());
                imagesFromVmConf.add(imageFromVmConf);
                break;
            }
        }
    }
    vm.setImages(imagesFromVmConf);
    return true;
}
Also used : Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot) HashMap(java.util.HashMap) VM(org.ovirt.engine.core.common.businessentities.VM) ArrayList(java.util.ArrayList) Guid(org.ovirt.engine.core.compat.Guid) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage)

Example 53 with Snapshot

use of org.ovirt.engine.core.common.businessentities.Snapshot in project ovirt-engine by oVirt.

the class SnapshotsManager method addSnapshot.

/**
 *addSnapshot
 * Save snapshot to DB with compensation data.
 *
 * @param snapshotId
 *            The snapshot ID.
 * @param description
 *            The snapshot description.
 * @param snapshotStatus
 *            The snapshot status.
 * @param snapshotType
 *            The snapshot type.
 * @param vm
 *            The VM to link to & save configuration for (if necessary).
 * @param saveVmConfiguration
 *            Should VM configuration be generated and saved?
 * @param memoryDumpDiskId
 *            The memory dump disk ID
 * @param memoryConfDiskId
 *            The memory metadata disk ID
 * @param creationDate
 *            predefined creation date for the snapshot, null indicates 'now'
 * @param disks
 *            The disks contained in the snapshot
 * @param vmDevices
 *            The devices contained in the snapshot
 * @param compensationContext
 *            In case compensation is needed.
 * @return the saved snapshot
 */
public Snapshot addSnapshot(Guid snapshotId, String description, SnapshotStatus snapshotStatus, SnapshotType snapshotType, VM vm, boolean saveVmConfiguration, Guid memoryDumpDiskId, Guid memoryConfDiskId, Date creationDate, List<DiskImage> disks, Map<Guid, VmDevice> vmDevices, final CompensationContext compensationContext) {
    final Snapshot snapshot = new Snapshot(snapshotId, snapshotStatus, vm.getId(), saveVmConfiguration ? generateVmConfiguration(vm, disks, vmDevices) : null, snapshotType, description, creationDate != null ? creationDate : new Date(), vm.getAppList(), memoryDumpDiskId, memoryConfDiskId);
    snapshotDao.save(snapshot);
    compensationContext.snapshotNewEntity(snapshot);
    return snapshot;
}
Also used : Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot) Date(java.util.Date)

Example 54 with Snapshot

use of org.ovirt.engine.core.common.businessentities.Snapshot in project ovirt-engine by oVirt.

the class TryBackToAllSnapshotsOfVmCommand method endWithFailure.

@Override
protected void endWithFailure() {
    Snapshot previouslyActiveSnapshot = snapshotDao.get(getVmId(), SnapshotType.PREVIEW, SnapshotStatus.LOCKED);
    snapshotDao.remove(previouslyActiveSnapshot.getId());
    snapshotDao.remove(snapshotDao.getId(getVmId(), SnapshotType.ACTIVE));
    getSnapshotsManager().addActiveSnapshot(previouslyActiveSnapshot.getId(), getVm(), SnapshotStatus.OK, previouslyActiveSnapshot.getMemoryDiskId(), previouslyActiveSnapshot.getMetadataDiskId(), getCompensationContext());
    super.endWithFailure();
}
Also used : Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot)

Example 55 with Snapshot

use of org.ovirt.engine.core.common.businessentities.Snapshot in project ovirt-engine by oVirt.

the class TryBackToAllSnapshotsOfVmCommand method executeVmCommand.

@Override
protected void executeVmCommand() {
    final boolean restoreMemory = isRestoreMemory();
    final Guid newActiveSnapshotId = Guid.newGuid();
    final Snapshot snapshotToBePreviewed = getDstSnapshot();
    final Snapshot previousActiveSnapshot = snapshotDao.get(getVmId(), SnapshotType.ACTIVE);
    final Guid previousActiveSnapshotId = previousActiveSnapshot.getId();
    final List<DiskImage> images = getImagesToPreview();
    // Images list without those that are excluded from preview
    final List<DiskImage> filteredImages = (List<DiskImage>) CollectionUtils.subtract(images, getImagesExcludedFromPreview(images, previousActiveSnapshotId, newActiveSnapshotId));
    if (log.isInfoEnabled()) {
        log.info("Previewing snapshot {} with the disks:\n{}", getSnapshotName(), filteredImages.stream().map(disk -> String.format("%s (%s) to imageId %s", disk.getName(), disk.getId().toString(), disk.getImageId().toString())).collect(Collectors.joining("\n")));
    }
    final List<CinderDisk> cinderDisks = new ArrayList<>();
    TransactionSupport.executeInNewTransaction(() -> {
        getCompensationContext().snapshotEntity(previousActiveSnapshot);
        snapshotDao.remove(previousActiveSnapshotId);
        getSnapshotsManager().addSnapshot(previousActiveSnapshotId, "Active VM before the preview", SnapshotStatus.LOCKED, SnapshotType.PREVIEW, getVm(), true, previousActiveSnapshot.getMemoryDiskId(), previousActiveSnapshot.getMetadataDiskId(), null, null, null, getCompensationContext());
        getSnapshotsManager().addActiveSnapshot(newActiveSnapshotId, getVm(), SnapshotStatus.OK, restoreMemory ? snapshotToBePreviewed.getMemoryDiskId() : null, restoreMemory ? snapshotToBePreviewed.getMetadataDiskId() : null, snapshotToBePreviewed.getCreationDate(), images, getCompensationContext());
        // being executed in the same transaction so we can restore the vm config and end the command.
        if (!filteredImages.isEmpty()) {
            getCompensationContext().stateChanged();
        } else {
            vmStaticDao.incrementDbGeneration(getVm().getId());
            restoreVmConfigFromSnapshot();
        }
        return null;
    });
    initializeSnapshotsLeasesParams();
    if (!filteredImages.isEmpty()) {
        vmHandler.lockVm(getVm().getDynamicData(), getCompensationContext());
        freeLock();
        TransactionSupport.executeInNewTransaction(new TransactionMethod<Void>() {

            @Override
            public Void runInTransaction() {
                for (DiskImage image : filteredImages) {
                    if (image.getDiskStorageType() == DiskStorageType.CINDER) {
                        cinderDisks.add((CinderDisk) image);
                        continue;
                    }
                    ActionReturnValue actionReturnValue = runInternalActionWithTasksContext(ActionType.TryBackToSnapshot, buildTryBackToSnapshotParameters(newActiveSnapshotId, image));
                    if (actionReturnValue.getSucceeded()) {
                        getTaskIdList().addAll(actionReturnValue.getInternalVdsmTaskIdList());
                    } else if (actionReturnValue.getFault() != null) {
                        // if we have a fault, forward it to the user
                        throw new EngineException(actionReturnValue.getFault().getError(), actionReturnValue.getFault().getMessage());
                    } else {
                        log.error("Cannot create snapshot");
                        throw new EngineException(EngineError.IRS_IMAGE_STATUS_ILLEGAL);
                    }
                }
                if (getParameters().getLeaseAction() == LeaseAction.CREATE_NEW_LEASE) {
                    if (!addVmLease(getParameters().getDstLeaseDomainId(), getVm().getId(), false)) {
                        log.error("Failed to create lease for VM '{}' on storage domain '{}'", getVm().getName(), getParameters().getDstLeaseDomainId());
                        throw new EngineException(EngineError.FailedToCreateLease);
                    }
                }
                if (!cinderDisks.isEmpty() && !tryBackAllCinderDisks(cinderDisks, newActiveSnapshotId)) {
                    throw new EngineException(EngineError.CINDER_ERROR, "Failed to preview a snapshot!");
                }
                return null;
            }

            private ImagesContainterParametersBase buildTryBackToSnapshotParameters(final Guid newActiveSnapshotId, DiskImage image) {
                ImagesContainterParametersBase params = new ImagesContainterParametersBase(image.getImageId());
                params.setParentCommand(ActionType.TryBackToAllSnapshotsOfVm);
                params.setVmSnapshotId(newActiveSnapshotId);
                params.setEntityInfo(getParameters().getEntityInfo());
                params.setParentParameters(getParameters());
                params.setQuotaId(image.getQuotaId());
                return params;
            }
        });
    } else {
        // if there are no disks to restore, no compensation context is saved and the VM Configuration
        // (including clusterCompatibilityVersionOrigin) is already restored at this point. Otherwise,
        // if disks are being restored, the VM Configuration is restored later in endSuccessfully()
        updateClusterCompatibilityVersionToOldCluster(true);
    }
    setSucceeded(true);
}
Also used : ImagesContainterParametersBase(org.ovirt.engine.core.common.action.ImagesContainterParametersBase) ArrayList(java.util.ArrayList) EngineException(org.ovirt.engine.core.common.errors.EngineException) Guid(org.ovirt.engine.core.compat.Guid) Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot) ActionReturnValue(org.ovirt.engine.core.common.action.ActionReturnValue) List(java.util.List) ArrayList(java.util.ArrayList) CinderDisk(org.ovirt.engine.core.common.businessentities.storage.CinderDisk) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage)

Aggregations

Snapshot (org.ovirt.engine.core.common.businessentities.Snapshot)96 DiskImage (org.ovirt.engine.core.common.businessentities.storage.DiskImage)33 VM (org.ovirt.engine.core.common.businessentities.VM)32 Guid (org.ovirt.engine.core.compat.Guid)30 Test (org.junit.Test)21 ArrayList (java.util.ArrayList)15 ActionReturnValue (org.ovirt.engine.core.common.action.ActionReturnValue)12 List (java.util.List)11 SnapshotStatus (org.ovirt.engine.core.common.businessentities.Snapshot.SnapshotStatus)9 Collections (java.util.Collections)8 Date (java.util.Date)8 Set (java.util.Set)8 Collectors (java.util.stream.Collectors)8 SnapshotType (org.ovirt.engine.core.common.businessentities.Snapshot.SnapshotType)8 EngineException (org.ovirt.engine.core.common.errors.EngineException)8 ActionType (org.ovirt.engine.core.common.action.ActionType)7 DiskStorageType (org.ovirt.engine.core.common.businessentities.storage.DiskStorageType)6 EngineMessage (org.ovirt.engine.core.common.errors.EngineMessage)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5