Search in sources :

Example 51 with SnapshotVO

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

the class SnapshotDataFactoryImpl method getSnapshot.

@Override
public SnapshotInfo getSnapshot(final long snapshotId, final DataStoreRole role) {
    final SnapshotVO snapshot = snapshotDao.findById(snapshotId);
    SnapshotDataStoreVO snapshotStore = snapshotStoreDao.findBySnapshot(snapshotId, role);
    if (snapshotStore == null) {
        snapshotStore = snapshotStoreDao.findByVolume(snapshot.getVolumeId(), role);
        if (snapshotStore == null) {
            return null;
        }
    }
    final DataStore store = storeMgr.getDataStore(snapshotStore.getDataStoreId(), role);
    final SnapshotObject so = SnapshotObject.getSnapshotObject(snapshot, store);
    return so;
}
Also used : SnapshotVO(com.cloud.storage.SnapshotVO) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO)

Example 52 with SnapshotVO

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

the class SnapshotDataFactoryImpl method getSnapshot.

@Override
public SnapshotInfo getSnapshot(final long snapshotId, final DataStore store) {
    final SnapshotVO snapshot = snapshotDao.findById(snapshotId);
    final SnapshotObject so = SnapshotObject.getSnapshotObject(snapshot, store);
    return so;
}
Also used : SnapshotVO(com.cloud.storage.SnapshotVO)

Example 53 with SnapshotVO

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

the class StorageSystemSnapshotStrategy method takeSnapshot.

@Override
@DB
public SnapshotInfo takeSnapshot(final SnapshotInfo snapshotInfo) {
    final VolumeInfo volumeInfo = snapshotInfo.getBaseVolume();
    if (volumeInfo.getFormat() != ImageFormat.VHD) {
        throw new CloudRuntimeException("Only the " + ImageFormat.VHD.toString() + " image type is currently supported.");
    }
    final SnapshotVO snapshotVO = _snapshotDao.acquireInLockTable(snapshotInfo.getId());
    if (snapshotVO == null) {
        throw new CloudRuntimeException("Failed to acquire lock on the following snapshot: " + snapshotInfo.getId());
    }
    SnapshotResult result = null;
    try {
        volumeInfo.stateTransit(Volume.Event.SnapshotRequested);
        // tell the storage driver to create a back-end volume (eventually used to create a new SR on and to copy the VM snapshot VDI to)
        result = snapshotSvr.takeSnapshot(snapshotInfo);
        if (result.isFailed()) {
            s_logger.debug("Failed to take a snapshot: " + result.getResult());
            throw new CloudRuntimeException(result.getResult());
        }
        // send a command to XenServer to create a VM snapshot on the applicable SR (get back the VDI UUID of the VM snapshot)
        performSnapshotAndCopyOnHostSide(volumeInfo, snapshotInfo);
        markAsBackedUp((SnapshotObject) result.getSnashot());
    } finally {
        if (result != null && result.isSuccess()) {
            volumeInfo.stateTransit(Volume.Event.OperationSucceeded);
        } else {
            volumeInfo.stateTransit(Volume.Event.OperationFailed);
        }
        _snapshotDao.releaseFromLockTable(snapshotInfo.getId());
    }
    return snapshotInfo;
}
Also used : SnapshotVO(com.cloud.storage.SnapshotVO) SnapshotResult(com.cloud.engine.subsystem.api.storage.SnapshotResult) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) DB(com.cloud.utils.db.DB)

Example 54 with SnapshotVO

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

the class XenserverSnapshotStrategy method deleteSnapshot.

@Override
public boolean deleteSnapshot(final Long snapshotId) {
    final SnapshotVO snapshotVO = snapshotDao.findById(snapshotId);
    if (snapshotVO.getState() == Snapshot.State.Allocated) {
        snapshotDao.remove(snapshotId);
        return true;
    }
    if (snapshotVO.getState() == Snapshot.State.Destroyed) {
        return true;
    }
    if (Snapshot.State.Error.equals(snapshotVO.getState())) {
        final List<SnapshotDataStoreVO> storeRefs = snapshotStoreDao.findBySnapshotId(snapshotId);
        for (final SnapshotDataStoreVO ref : storeRefs) {
            snapshotStoreDao.expunge(ref.getId());
        }
        snapshotDao.remove(snapshotId);
        return true;
    }
    if (snapshotVO.getState() == Snapshot.State.CreatedOnPrimary) {
        s_logger.debug("delete snapshot on primary storage:");
        snapshotVO.setState(Snapshot.State.Destroyed);
        snapshotDao.update(snapshotId, snapshotVO);
        return true;
    }
    if (!Snapshot.State.BackedUp.equals(snapshotVO.getState()) && !Snapshot.State.Error.equals(snapshotVO.getState())) {
        throw new InvalidParameterValueException("Can't delete snapshotshot " + snapshotId + " due to it is in " + snapshotVO.getState() + " Status");
    }
    // first mark the snapshot as destroyed, so that ui can't see it, but we
    // may not destroy the snapshot on the storage, as other snapshots may
    // depend on it.
    final SnapshotInfo snapshotOnImage = snapshotDataFactory.getSnapshot(snapshotId, DataStoreRole.Image);
    if (snapshotOnImage == null) {
        s_logger.debug("Can't find snapshot on backup storage, delete it in db");
        snapshotDao.remove(snapshotId);
        return true;
    }
    final SnapshotObject obj = (SnapshotObject) snapshotOnImage;
    try {
        obj.processEvent(Snapshot.Event.DestroyRequested);
    } catch (final NoTransitionException e) {
        s_logger.debug("Failed to set the state to destroying: ", e);
        return false;
    }
    try {
        final boolean result = deleteSnapshotChain(snapshotOnImage);
        obj.processEvent(Snapshot.Event.OperationSucceeded);
        if (result) {
            // snapshot is deleted on backup storage, need to delete it on primary storage
            final SnapshotDataStoreVO snapshotOnPrimary = snapshotStoreDao.findBySnapshot(snapshotId, DataStoreRole.Primary);
            if (snapshotOnPrimary != null) {
                snapshotOnPrimary.setState(State.Destroyed);
                snapshotStoreDao.update(snapshotOnPrimary.getId(), snapshotOnPrimary);
            }
        }
    } catch (final Exception e) {
        s_logger.debug("Failed to delete snapshot: ", e);
        try {
            obj.processEvent(Snapshot.Event.OperationFailed);
        } catch (final NoTransitionException e1) {
            s_logger.debug("Failed to change snapshot state: " + e.toString());
        }
        return false;
    }
    return true;
}
Also used : SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 55 with SnapshotVO

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

the class TemplateManagerImpl method createPrivateTemplate.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", async = true)
public VirtualMachineTemplate createPrivateTemplate(final CreateTemplateCmd command) throws CloudRuntimeException {
    final long templateId = command.getEntityId();
    final Long volumeId = command.getVolumeId();
    final Long snapshotId = command.getSnapshotId();
    VMTemplateVO privateTemplate = null;
    final Long accountId = CallContext.current().getCallingAccountId();
    SnapshotVO snapshot = null;
    VolumeVO volume = null;
    try {
        final TemplateInfo tmplInfo = _tmplFactory.getTemplate(templateId, DataStoreRole.Image);
        long zoneId = 0;
        if (snapshotId != null) {
            snapshot = _snapshotDao.findById(snapshotId);
            zoneId = snapshot.getDataCenterId();
        } else if (volumeId != null) {
            volume = _volumeDao.findById(volumeId);
            zoneId = volume.getDataCenterId();
        }
        DataStore store = _dataStoreMgr.getImageStore(zoneId);
        if (store == null) {
            throw new CloudRuntimeException("cannot find an image store for zone " + zoneId);
        }
        final AsyncCallFuture<TemplateApiResult> future;
        if (snapshotId != null) {
            final DataStoreRole dataStoreRole = ApiResponseHelper.getDataStoreRole(snapshot, _snapshotStoreDao, _dataStoreMgr);
            SnapshotInfo snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
            if (dataStoreRole == DataStoreRole.Image) {
                if (snapInfo == null) {
                    snapInfo = _snapshotFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
                    if (snapInfo == null) {
                        throw new CloudRuntimeException("Cannot find snapshot " + snapshotId);
                    }
                    // We need to copy the snapshot onto secondary.
                    final SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
                    snapshotStrategy.backupSnapshot(snapInfo);
                    // Attempt to grab it again.
                    snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
                    if (snapInfo == null) {
                        throw new CloudRuntimeException("Cannot find snapshot " + snapshotId + " on secondary and could not create backup");
                    }
                }
                final DataStore snapStore = snapInfo.getDataStore();
                if (snapStore != null) {
                    // pick snapshot image store to create template
                    store = snapStore;
                }
            }
            future = _tmpltSvr.createTemplateFromSnapshotAsync(snapInfo, tmplInfo, store);
        } else if (volumeId != null) {
            final VolumeInfo volInfo = _volFactory.getVolume(volumeId);
            future = _tmpltSvr.createTemplateFromVolumeAsync(volInfo, tmplInfo, store);
        } else {
            throw new CloudRuntimeException("Creating private Template need to specify snapshotId or volumeId");
        }
        final CommandResult result;
        try {
            result = future.get();
            if (result.isFailed()) {
                privateTemplate = null;
                s_logger.debug("Failed to create template" + result.getResult());
                throw new CloudRuntimeException("Failed to create template" + result.getResult());
            }
            // create entries in template_zone_ref table
            if (_dataStoreMgr.isRegionStore(store)) {
                // template created on region store
                _tmpltSvr.associateTemplateToZone(templateId, null);
            } else {
                final VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
                _tmpltZoneDao.persist(templateZone);
            }
            privateTemplate = _tmpltDao.findById(templateId);
        } catch (final InterruptedException | ExecutionException e) {
            s_logger.debug("Failed to create template", e);
            throw new CloudRuntimeException("Failed to create template", e);
        }
    } finally {
        if (privateTemplate == null) {
            final VolumeVO volumeFinal = volume;
            final SnapshotVO snapshotFinal = snapshot;
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(final TransactionStatus status) {
                    // template_store_ref entries should have been removed using our
                    // DataObject.processEvent command in case of failure, but clean
                    // it up here to avoid
                    // some leftovers which will cause removing template from
                    // vm_template table fail.
                    _tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
                    // Remove the template_zone_ref record
                    _tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
                    // Remove the template record
                    _tmpltDao.expunge(templateId);
                    // decrement resource count
                    if (accountId != null) {
                        _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
                        _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(volumeFinal != null ? volumeFinal.getSize() : snapshotFinal.getSize()));
                    }
                }
            });
        }
    }
    if (privateTemplate != null) {
        return privateTemplate;
    } else {
        throw new CloudRuntimeException("Failed to create a template");
    }
}
Also used : VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) TemplateApiResult(com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult) Date(java.util.Date) CommandResult(com.cloud.storage.command.CommandResult) DataStoreRole(com.cloud.storage.DataStoreRole) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) ExecutionException(java.util.concurrent.ExecutionException) SnapshotStrategy(com.cloud.engine.subsystem.api.storage.SnapshotStrategy) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

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