Search in sources :

Example 21 with SnapshotDataStoreVO

use of com.cloud.storage.datastore.db.SnapshotDataStoreVO in project cosmic by MissionCriticalCloud.

the class XenserverSnapshotStrategy method backupSnapshot.

@Override
public SnapshotInfo backupSnapshot(final SnapshotInfo snapshot) {
    final SnapshotInfo parentSnapshot = snapshot.getParent();
    if (parentSnapshot != null && snapshot.getPath().equalsIgnoreCase(parentSnapshot.getPath())) {
        s_logger.debug("backup an empty snapshot");
        // don't need to backup this snapshot
        final SnapshotDataStoreVO parentSnapshotOnBackupStore = snapshotStoreDao.findBySnapshot(parentSnapshot.getId(), DataStoreRole.Image);
        if (parentSnapshotOnBackupStore != null && parentSnapshotOnBackupStore.getState() == State.Ready) {
            final DataStore store = dataStoreMgr.getDataStore(parentSnapshotOnBackupStore.getDataStoreId(), parentSnapshotOnBackupStore.getRole());
            final SnapshotInfo snapshotOnImageStore = (SnapshotInfo) store.create(snapshot);
            snapshotOnImageStore.processEvent(Event.CreateOnlyRequested);
            final SnapshotObjectTO snapTO = new SnapshotObjectTO();
            snapTO.setPath(parentSnapshotOnBackupStore.getInstallPath());
            final CreateObjectAnswer createSnapshotAnswer = new CreateObjectAnswer(snapTO);
            snapshotOnImageStore.processEvent(Event.OperationSuccessed, createSnapshotAnswer);
            final SnapshotObject snapObj = (SnapshotObject) snapshot;
            try {
                snapObj.processEvent(Snapshot.Event.OperationNotPerformed);
            } catch (final NoTransitionException e) {
                s_logger.debug("Failed to change state: " + snapshot.getId() + ": " + e.toString());
                throw new CloudRuntimeException(e.toString());
            }
            return snapshotDataFactory.getSnapshot(snapObj.getId(), store);
        } else {
            s_logger.debug("parent snapshot hasn't been backed up yet");
        }
    }
    // determine full snapshot backup or not
    boolean fullBackup = true;
    SnapshotDataStoreVO parentSnapshotOnBackupStore = snapshotStoreDao.findLatestSnapshotForVolume(snapshot.getVolumeId(), DataStoreRole.Image);
    final SnapshotDataStoreVO parentSnapshotOnPrimaryStore = snapshotStoreDao.findLatestSnapshotForVolume(snapshot.getVolumeId(), DataStoreRole.Primary);
    final HypervisorType hypervisorType = snapshot.getBaseVolume().getHypervisorType();
    if (parentSnapshotOnPrimaryStore != null && parentSnapshotOnBackupStore != null && hypervisorType == Hypervisor.HypervisorType.XenServer) {
        // CS does incremental backup
        // only for XenServer
        // In case of volume migration from one pool to other pool, CS should take full snapshot to avoid any issues with delta chain,
        // to check if this is a migrated volume, compare the current pool id of volume and store_id of oldest snapshot on primary for this volume.
        // Why oldest? Because at this point CS has two snapshot on primary entries for same volume, one with old pool_id and other one with
        // current pool id. So, verify and if volume found to be migrated, delete snapshot entry with previous pool store_id.
        final SnapshotDataStoreVO oldestSnapshotOnPrimary = snapshotStoreDao.findOldestSnapshotForVolume(snapshot.getVolumeId(), DataStoreRole.Primary);
        final VolumeVO volume = volumeDao.findById(snapshot.getVolumeId());
        if (oldestSnapshotOnPrimary != null) {
            if (oldestSnapshotOnPrimary.getDataStoreId() == volume.getPoolId()) {
                final int _deltaSnapshotMax = NumbersUtil.parseInt(configDao.getValue("snapshot.delta.max"), SnapshotManager.DELTAMAX);
                final int deltaSnap = _deltaSnapshotMax;
                int i;
                for (i = 1; i < deltaSnap; i++) {
                    final Long prevBackupId = parentSnapshotOnBackupStore.getParentSnapshotId();
                    if (prevBackupId == 0) {
                        break;
                    }
                    parentSnapshotOnBackupStore = snapshotStoreDao.findBySnapshot(prevBackupId, DataStoreRole.Image);
                    if (parentSnapshotOnBackupStore == null) {
                        break;
                    }
                }
                if (i >= deltaSnap) {
                    fullBackup = true;
                } else {
                    fullBackup = false;
                }
            } else {
                // if there is an snapshot entry for previousPool(primary storage) of migrated volume, delete it becasue CS created one more snapshot entry for current pool
                snapshotStoreDao.remove(oldestSnapshotOnPrimary.getId());
            }
        }
    }
    snapshot.addPayload(fullBackup);
    return snapshotSvr.backupSnapshot(snapshot);
}
Also used : SnapshotObjectTO(com.cloud.storage.to.SnapshotObjectTO) SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) CreateObjectAnswer(com.cloud.storage.command.CreateObjectAnswer) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) NoTransitionException(com.cloud.utils.fsm.NoTransitionException)

Example 22 with SnapshotDataStoreVO

use of com.cloud.storage.datastore.db.SnapshotDataStoreVO in project cosmic by MissionCriticalCloud.

the class XenserverSnapshotStrategy method takeSnapshot.

@Override
@DB
public SnapshotInfo takeSnapshot(SnapshotInfo snapshot) {
    final Object payload = snapshot.getPayload();
    if (payload != null) {
        final CreateSnapshotPayload createSnapshotPayload = (CreateSnapshotPayload) payload;
        if (createSnapshotPayload.getQuiescevm()) {
            throw new InvalidParameterValueException("can't handle quiescevm equal true for volume snapshot");
        }
    }
    final SnapshotVO snapshotVO = snapshotDao.acquireInLockTable(snapshot.getId());
    if (snapshotVO == null) {
        throw new CloudRuntimeException("Failed to get lock on snapshot:" + snapshot.getId());
    }
    try {
        final VolumeInfo volumeInfo = snapshot.getBaseVolume();
        volumeInfo.stateTransit(Volume.Event.SnapshotRequested);
        SnapshotResult result = null;
        try {
            result = snapshotSvr.takeSnapshot(snapshot);
            if (result.isFailed()) {
                s_logger.debug("Failed to take snapshot: " + result.getResult());
                throw new CloudRuntimeException(result.getResult());
            }
        } finally {
            if (result != null && result.isSuccess()) {
                volumeInfo.stateTransit(Volume.Event.OperationSucceeded);
            } else {
                volumeInfo.stateTransit(Volume.Event.OperationFailed);
            }
        }
        snapshot = result.getSnashot();
        final DataStore primaryStore = snapshot.getDataStore();
        final boolean backupFlag = Boolean.parseBoolean(configDao.getValue(Config.BackupSnapshotAfterTakingSnapshot.toString()));
        final SnapshotInfo backupedSnapshot;
        if (backupFlag) {
            backupedSnapshot = backupSnapshot(snapshot);
        } else {
            // Fake it to get the transitions to fire in the proper order
            s_logger.debug("skipping backup of snapshot due to configuration " + Config.BackupSnapshotAfterTakingSnapshot.toString());
            final SnapshotObject snapObj = (SnapshotObject) snapshot;
            try {
                snapObj.processEvent(Snapshot.Event.OperationNotPerformed);
            } catch (final NoTransitionException e) {
                s_logger.debug("Failed to change state: " + snapshot.getId() + ": " + e.toString());
                throw new CloudRuntimeException(e.toString());
            }
            backupedSnapshot = snapshot;
        }
        try {
            final SnapshotInfo parent = snapshot.getParent();
            if (backupedSnapshot != null && parent != null) {
                Long parentSnapshotId = parent.getId();
                while (parentSnapshotId != null && parentSnapshotId != 0L) {
                    final SnapshotDataStoreVO snapshotDataStoreVO = snapshotStoreDao.findByStoreSnapshot(primaryStore.getRole(), primaryStore.getId(), parentSnapshotId);
                    if (snapshotDataStoreVO != null) {
                        parentSnapshotId = snapshotDataStoreVO.getParentSnapshotId();
                        snapshotStoreDao.remove(snapshotDataStoreVO.getId());
                    } else {
                        parentSnapshotId = null;
                    }
                }
                final SnapshotDataStoreVO snapshotDataStoreVO = snapshotStoreDao.findByStoreSnapshot(primaryStore.getRole(), primaryStore.getId(), snapshot.getId());
                if (snapshotDataStoreVO != null) {
                    snapshotDataStoreVO.setParentSnapshotId(0L);
                    snapshotStoreDao.update(snapshotDataStoreVO.getId(), snapshotDataStoreVO);
                }
            }
        } catch (final Exception e) {
            s_logger.debug("Failed to clean up snapshots on primary storage", e);
        }
        return backupedSnapshot;
    } finally {
        if (snapshotVO != null) {
            snapshotDao.releaseFromLockTable(snapshot.getId());
        }
    }
}
Also used : SnapshotResult(com.cloud.engine.subsystem.api.storage.SnapshotResult) SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) CreateSnapshotPayload(com.cloud.storage.CreateSnapshotPayload) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) DB(com.cloud.utils.db.DB)

Example 23 with SnapshotDataStoreVO

use of com.cloud.storage.datastore.db.SnapshotDataStoreVO in project cosmic by MissionCriticalCloud.

the class SnapshotDataFactoryImpl method listSnapshotOnCache.

@Override
public List<SnapshotInfo> listSnapshotOnCache(final long snapshotId) {
    final List<SnapshotDataStoreVO> cacheSnapshots = snapshotStoreDao.listOnCache(snapshotId);
    final List<SnapshotInfo> snapObjs = new ArrayList<>();
    for (final SnapshotDataStoreVO cacheSnap : cacheSnapshots) {
        final long storeId = cacheSnap.getDataStoreId();
        final DataStore store = storeMgr.getDataStore(storeId, DataStoreRole.ImageCache);
        final SnapshotInfo tmplObj = getSnapshot(snapshotId, store);
        snapObjs.add(tmplObj);
    }
    return snapObjs;
}
Also used : SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) ArrayList(java.util.ArrayList)

Example 24 with SnapshotDataStoreVO

use of com.cloud.storage.datastore.db.SnapshotDataStoreVO in project cosmic by MissionCriticalCloud.

the class SnapshotObject method decRefCount.

@Override
public void decRefCount() {
    if (store == null) {
        return;
    }
    if (store.getRole() == DataStoreRole.Image || store.getRole() == DataStoreRole.ImageCache) {
        final SnapshotDataStoreVO store = snapshotStoreDao.findByStoreSnapshot(this.store.getRole(), this.store.getId(), getId());
        store.decrRefCnt();
        store.setLastUpdated(new Date());
        snapshotStoreDao.update(store.getId(), store);
    }
}
Also used : SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) Date(java.util.Date)

Example 25 with SnapshotDataStoreVO

use of com.cloud.storage.datastore.db.SnapshotDataStoreVO in project cosmic by MissionCriticalCloud.

the class SnapshotObject method incRefCount.

@Override
public void incRefCount() {
    if (store == null) {
        return;
    }
    if (store.getRole() == DataStoreRole.Image || store.getRole() == DataStoreRole.ImageCache) {
        final SnapshotDataStoreVO store = snapshotStoreDao.findByStoreSnapshot(this.store.getRole(), this.store.getId(), getId());
        store.incrRefCnt();
        store.setLastUpdated(new Date());
        snapshotStoreDao.update(store.getId(), store);
    }
}
Also used : SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) Date(java.util.Date)

Aggregations

SnapshotDataStoreVO (com.cloud.storage.datastore.db.SnapshotDataStoreVO)34 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)12 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)10 SnapshotInfo (com.cloud.engine.subsystem.api.storage.SnapshotInfo)9 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)9 SnapshotVO (com.cloud.storage.SnapshotVO)8 VolumeDataStoreVO (com.cloud.storage.datastore.db.VolumeDataStoreVO)8 TemplateDataStoreVO (com.cloud.storage.datastore.db.TemplateDataStoreVO)6 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)5 Date (java.util.Date)5 ConfigurationException (javax.naming.ConfigurationException)5 SnapshotStrategy (com.cloud.engine.subsystem.api.storage.SnapshotStrategy)4 DB (com.cloud.utils.db.DB)4 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)3 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)3 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)3 VolumeVO (com.cloud.storage.VolumeVO)3 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)3 EndPoint (com.cloud.engine.subsystem.api.storage.EndPoint)2 ZoneScope (com.cloud.engine.subsystem.api.storage.ZoneScope)2