Search in sources :

Example 1 with SnapshotResult

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult in project cloudstack by apache.

the class XenserverSnapshotStrategy method takeSnapshot.

@Override
@DB
public SnapshotInfo takeSnapshot(SnapshotInfo snapshot) {
    Object payload = snapshot.getPayload();
    if (payload != null) {
        CreateSnapshotPayload createSnapshotPayload = (CreateSnapshotPayload) payload;
        if (createSnapshotPayload.getQuiescevm()) {
            throw new InvalidParameterValueException("can't handle quiescevm equal true for volume snapshot");
        }
    }
    SnapshotVO snapshotVO = snapshotDao.acquireInLockTable(snapshot.getId());
    if (snapshotVO == null) {
        throw new CloudRuntimeException("Failed to get lock on snapshot:" + snapshot.getId());
    }
    try {
        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.getSnapshot();
        DataStore primaryStore = snapshot.getDataStore();
        boolean backupFlag = Boolean.parseBoolean(configDao.getValue(Config.BackupSnapshotAfterTakingSnapshot.toString()));
        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());
            SnapshotObject snapObj = (SnapshotObject) snapshot;
            try {
                snapObj.processEvent(Snapshot.Event.OperationNotPerformed);
            } catch (NoTransitionException e) {
                s_logger.debug("Failed to change state: " + snapshot.getId() + ": " + e.toString());
                throw new CloudRuntimeException(e.toString());
            }
            backupedSnapshot = snapshot;
        }
        try {
            SnapshotInfo parent = snapshot.getParent();
            if (backupedSnapshot != null && parent != null && primaryStore instanceof PrimaryDataStoreImpl) {
                if (((PrimaryDataStoreImpl) primaryStore).getPoolType() != StoragePoolType.RBD) {
                    Long parentSnapshotId = parent.getId();
                    while (parentSnapshotId != null && parentSnapshotId != 0L) {
                        SnapshotDataStoreVO snapshotDataStoreVO = snapshotStoreDao.findByStoreSnapshot(primaryStore.getRole(), primaryStore.getId(), parentSnapshotId);
                        if (snapshotDataStoreVO != null) {
                            parentSnapshotId = snapshotDataStoreVO.getParentSnapshotId();
                            snapshotStoreDao.remove(snapshotDataStoreVO.getId());
                        } else {
                            parentSnapshotId = null;
                        }
                    }
                }
                SnapshotDataStoreVO snapshotDataStoreVO = snapshotStoreDao.findByStoreSnapshot(primaryStore.getRole(), primaryStore.getId(), snapshot.getId());
                if (snapshotDataStoreVO != null) {
                    snapshotDataStoreVO.setParentSnapshotId(0L);
                    snapshotStoreDao.update(snapshotDataStoreVO.getId(), snapshotDataStoreVO);
                }
            }
        } catch (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(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) CreateSnapshotPayload(com.cloud.storage.CreateSnapshotPayload) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PrimaryDataStoreImpl(org.apache.cloudstack.storage.datastore.PrimaryDataStoreImpl) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) DB(com.cloud.utils.db.DB)

Example 2 with SnapshotResult

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult in project cloudstack by apache.

the class SnapshotTestWithFakeData method testTakeSnapshotWithFailed.

//@Test
public void testTakeSnapshotWithFailed() throws URISyntaxException {
    SnapshotVO snapshotVO = createSnapshotInDb();
    DataStore store = null;
    try {
        store = createDataStore();
        FakePrimaryDataStoreDriver dataStoreDriver = (FakePrimaryDataStoreDriver) store.getDriver();
        dataStoreDriver.makeTakeSnapshotSucceed(false);
        SnapshotInfo snapshotInfo = snapshotDataFactory.getSnapshot(snapshotVO.getId(), store);
        SnapshotResult result = snapshotService.takeSnapshot(snapshotInfo);
        Assert.assertFalse(result.isSuccess());
        SnapshotDataStoreVO storeRef = snapshotDataStoreDao.findByStoreSnapshot(store.getRole(), store.getId(), snapshotVO.getId());
        Assert.assertTrue(storeRef == null);
    } finally {
        snapshotDao.expunge(snapshotVO.getId());
        if (store != null) {
            primaryDataStoreDao.remove(store.getId());
        }
    }
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) SnapshotResult(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)

Example 3 with SnapshotResult

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult in project cloudstack by apache.

the class SnapshotServiceImpl method copySnapshotAsyncCallback.

protected Void copySnapshotAsyncCallback(AsyncCallbackDispatcher<SnapshotServiceImpl, CopyCommandResult> callback, CopySnapshotContext<CommandResult> context) {
    CopyCommandResult result = callback.getResult();
    SnapshotInfo destSnapshot = context.destSnapshot;
    SnapshotObject srcSnapshot = (SnapshotObject) context.srcSnapshot;
    AsyncCallFuture<SnapshotResult> future = context.future;
    SnapshotResult snapResult = new SnapshotResult(destSnapshot, result.getAnswer());
    if (result.isFailed()) {
        try {
            destSnapshot.processEvent(Event.OperationFailed);
            //if backup snapshot failed, mark srcSnapshot in snapshot_store_ref as failed also
            srcSnapshot.processEvent(Event.DestroyRequested);
            srcSnapshot.processEvent(Event.OperationSuccessed);
            srcSnapshot.processEvent(Snapshot.Event.OperationFailed);
            _snapshotDao.remove(srcSnapshot.getId());
        } catch (NoTransitionException e) {
            s_logger.debug("Failed to update state: " + e.toString());
        }
        snapResult.setResult(result.getResult());
        future.complete(snapResult);
        return null;
    }
    try {
        CopyCmdAnswer copyCmdAnswer = (CopyCmdAnswer) result.getAnswer();
        destSnapshot.processEvent(Event.OperationSuccessed, copyCmdAnswer);
        srcSnapshot.processEvent(Snapshot.Event.OperationSucceeded);
        snapResult = new SnapshotResult(_snapshotFactory.getSnapshot(destSnapshot.getId(), destSnapshot.getDataStore()), copyCmdAnswer);
        future.complete(snapResult);
    } catch (Exception e) {
        s_logger.debug("Failed to update snapshot state", e);
        snapResult.setResult(e.toString());
        future.complete(snapResult);
    }
    return null;
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotResult(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with SnapshotResult

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult in project cloudstack by apache.

the class SnapshotServiceImpl method syncSnapshotCallBack.

protected Void syncSnapshotCallBack(AsyncCallbackDispatcher<SnapshotServiceImpl, CopyCommandResult> callback, CopySnapshotContext<CommandResult> context) {
    CopyCommandResult result = callback.getResult();
    SnapshotInfo destSnapshot = context.destSnapshot;
    SnapshotResult res = new SnapshotResult(destSnapshot, null);
    AsyncCallFuture<SnapshotResult> future = context.future;
    try {
        if (result.isFailed()) {
            res.setResult(result.getResult());
        // no change to existing snapshot_store_ref, will try to re-sync later if other call triggers this sync operation
        } else {
            // this will update install path properly, next time it will not sync anymore.
            destSnapshot.processEvent(Event.OperationSuccessed, result.getAnswer());
        }
        future.complete(res);
    } catch (Exception e) {
        s_logger.debug("Failed to process sync snapshot callback", e);
        res.setResult(e.toString());
        future.complete(res);
    }
    return null;
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotResult(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with SnapshotResult

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult in project cloudstack by apache.

the class SnapshotServiceImpl method revertSnapshot.

@Override
public boolean revertSnapshot(SnapshotInfo snapshot) {
    SnapshotInfo snapshotOnPrimaryStore = _snapshotFactory.getSnapshot(snapshot.getId(), DataStoreRole.Primary);
    if (snapshotOnPrimaryStore == null) {
        throw new CloudRuntimeException("Cannot find an entry for snapshot " + snapshot.getId() + " on primary storage pools");
    }
    PrimaryDataStore store = (PrimaryDataStore) snapshotOnPrimaryStore.getDataStore();
    AsyncCallFuture<SnapshotResult> future = new AsyncCallFuture<SnapshotResult>();
    RevertSnapshotContext<CommandResult> context = new RevertSnapshotContext<CommandResult>(null, snapshot, future);
    AsyncCallbackDispatcher<SnapshotServiceImpl, CommandResult> caller = AsyncCallbackDispatcher.create(this);
    caller.setCallback(caller.getTarget().revertSnapshotCallback(null, null)).setContext(context);
    ((PrimaryDataStoreDriver) store.getDriver()).revertSnapshot(snapshot, snapshotOnPrimaryStore, caller);
    SnapshotResult result = null;
    try {
        result = future.get();
        if (result.isFailed()) {
            throw new CloudRuntimeException(result.getResult());
        }
        return true;
    } catch (InterruptedException e) {
        s_logger.debug("revert snapshot is failed: " + e.toString());
    } catch (ExecutionException e) {
        s_logger.debug("revert snapshot is failed: " + e.toString());
    }
    return false;
}
Also used : SnapshotResult(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult) CommandResult(org.apache.cloudstack.storage.command.CommandResult) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) PrimaryDataStoreDriver(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Aggregations

SnapshotResult (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)11 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)10 ExecutionException (java.util.concurrent.ExecutionException)10 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)9 CommandResult (org.apache.cloudstack.storage.command.CommandResult)7 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)5 AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)5 SnapshotVO (com.cloud.storage.SnapshotVO)4 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)3 SnapshotDataStoreVO (org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)3 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 DB (com.cloud.utils.db.DB)2 CreateCmdResult (org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult)2 PrimaryDataStoreDriver (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver)2 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)2 HostVO (com.cloud.host.HostVO)1 CreateSnapshotPayload (com.cloud.storage.CreateSnapshotPayload)1 SnapshotDetailsVO (com.cloud.storage.dao.SnapshotDetailsVO)1