Search in sources :

Example 6 with SnapshotResult

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

the class SnapshotServiceImpl method takeSnapshot.

@Override
public SnapshotResult takeSnapshot(SnapshotInfo snap) {
    SnapshotObject snapshot = (SnapshotObject) snap;
    SnapshotObject snapshotOnPrimary = null;
    try {
        snapshotOnPrimary = (SnapshotObject) snap.getDataStore().create(snapshot);
    } catch (Exception e) {
        s_logger.debug("Failed to create snapshot state on data store due to " + e.getMessage());
        throw new CloudRuntimeException(e);
    }
    try {
        snapshotOnPrimary.processEvent(Snapshot.Event.CreateRequested);
    } catch (NoTransitionException e) {
        s_logger.debug("Failed to change snapshot state: " + e.toString());
        throw new CloudRuntimeException(e);
    }
    try {
        snapshotOnPrimary.processEvent(Event.CreateOnlyRequested);
    } catch (Exception e) {
        s_logger.debug("Failed to change snapshot state: " + e.toString());
        try {
            snapshotOnPrimary.processEvent(Snapshot.Event.OperationFailed);
        } catch (NoTransitionException e1) {
            s_logger.debug("Failed to change snapshot state: " + e1.toString());
        }
        throw new CloudRuntimeException(e);
    }
    AsyncCallFuture<SnapshotResult> future = new AsyncCallFuture<SnapshotResult>();
    try {
        CreateSnapshotContext<CommandResult> context = new CreateSnapshotContext<CommandResult>(null, snap.getBaseVolume(), snapshotOnPrimary, future);
        AsyncCallbackDispatcher<SnapshotServiceImpl, CreateCmdResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().createSnapshotAsyncCallback(null, null)).setContext(context);
        PrimaryDataStoreDriver primaryStore = (PrimaryDataStoreDriver) snapshotOnPrimary.getDataStore().getDriver();
        primaryStore.takeSnapshot(snapshot, caller);
    } catch (Exception e) {
        s_logger.debug("Failed to take snapshot: " + snapshot.getId(), e);
        try {
            snapshot.processEvent(Snapshot.Event.OperationFailed);
            snapshot.processEvent(Event.OperationFailed);
        } catch (NoTransitionException e1) {
            s_logger.debug("Failed to change state for event: OperationFailed", e);
        }
        throw new CloudRuntimeException("Failed to take snapshot" + snapshot.getId());
    }
    SnapshotResult result;
    try {
        result = future.get();
        return result;
    } catch (InterruptedException e) {
        s_logger.debug("Failed to create snapshot", e);
        throw new CloudRuntimeException("Failed to create snapshot", e);
    } catch (ExecutionException e) {
        s_logger.debug("Failed to create snapshot", e);
        throw new CloudRuntimeException("Failed to create snapshot", e);
    }
}
Also used : SnapshotResult(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult) CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) CommandResult(org.apache.cloudstack.storage.command.CommandResult) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) PrimaryDataStoreDriver(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with SnapshotResult

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

the class SnapshotServiceImpl method syncSnapshotToRegionStore.

// push one individual snapshots currently on cache store to region store if it is not there already
private void syncSnapshotToRegionStore(long snapshotId, DataStore store) {
    // if snapshot is already on region wide object store, check if it is really downloaded there (by checking install_path). Sync snapshot to region
    // wide store if it is not there physically.
    SnapshotInfo snapOnStore = _snapshotFactory.getSnapshot(snapshotId, store);
    if (snapOnStore == null) {
        throw new CloudRuntimeException("Cannot find an entry in snapshot_store_ref for snapshot " + snapshotId + " on region store: " + store.getName());
    }
    if (snapOnStore.getPath() == null || snapOnStore.getPath().length() == 0) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("sync snapshot " + snapshotId + " from cache to object store...");
        }
        // snapshot is not on region store yet, sync to region store
        SnapshotInfo srcSnapshot = _snapshotFactory.getReadySnapshotOnCache(snapshotId);
        if (srcSnapshot == null) {
            throw new CloudRuntimeException("Cannot find snapshot " + snapshotId + "  on cache store");
        }
        AsyncCallFuture<SnapshotResult> future = syncToRegionStoreAsync(srcSnapshot, store);
        try {
            SnapshotResult result = future.get();
            if (result.isFailed()) {
                throw new CloudRuntimeException("sync snapshot from cache to region wide store failed for image store " + store.getName() + ":" + result.getResult());
            }
            // reduce reference count for template on cache, so it can recycled by schedule
            _cacheMgr.releaseCacheObject(srcSnapshot);
        } catch (Exception ex) {
            throw new CloudRuntimeException("sync snapshot from cache to region wide store failed for image store " + store.getName());
        }
    }
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotResult(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with SnapshotResult

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

the class SnapshotServiceImpl method syncToRegionStoreAsync.

private AsyncCallFuture<SnapshotResult> syncToRegionStoreAsync(SnapshotInfo snapshot, DataStore store) {
    AsyncCallFuture<SnapshotResult> future = new AsyncCallFuture<SnapshotResult>();
    // no need to create entry on snapshot_store_ref here, since entries are already created when updateCloudToUseObjectStore is invoked.
    // But we need to set default install path so that sync can be done in the right s3 path
    SnapshotInfo snapshotOnStore = _snapshotFactory.getSnapshot(snapshot, store);
    String installPath = TemplateConstants.DEFAULT_SNAPSHOT_ROOT_DIR + "/" + snapshot.getAccountId() + "/" + snapshot.getVolumeId();
    ((SnapshotObject) snapshotOnStore).setPath(installPath);
    CopySnapshotContext<CommandResult> context = new CopySnapshotContext<CommandResult>(null, snapshot, snapshotOnStore, future);
    AsyncCallbackDispatcher<SnapshotServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
    caller.setCallback(caller.getTarget().syncSnapshotCallBack(null, null)).setContext(context);
    motionSrv.copyAsync(snapshot, snapshotOnStore, caller);
    return future;
}
Also used : AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) 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) CommandResult(org.apache.cloudstack.storage.command.CommandResult) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 9 with SnapshotResult

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

the class StorageSystemSnapshotStrategy method takeSnapshot.

@Override
@DB
public SnapshotInfo takeSnapshot(SnapshotInfo snapshotInfo) {
    VolumeInfo volumeInfo = snapshotInfo.getBaseVolume();
    if (volumeInfo.getFormat() != ImageFormat.VHD) {
        throw new CloudRuntimeException("Only the " + ImageFormat.VHD.toString() + " image type is currently supported.");
    }
    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;
    SnapshotInfo snapshotOnPrimary = null;
    SnapshotInfo backedUpSnapshot = null;
    try {
        volumeInfo.stateTransit(Volume.Event.SnapshotRequested);
        // only XenServer is currently supported
        HostVO hostVO = getHost(volumeInfo.getId());
        boolean canStorageSystemCreateVolumeFromSnapshot = canStorageSystemCreateVolumeFromSnapshot(volumeInfo.getPoolId());
        boolean computeClusterSupportsResign = clusterDao.getSupportsResigning(hostVO.getClusterId());
        if (canStorageSystemCreateVolumeFromSnapshot && computeClusterSupportsResign) {
            SnapshotDetailsVO snapshotDetail = new SnapshotDetailsVO(snapshotInfo.getId(), "takeSnapshot", Boolean.TRUE.toString(), false);
            snapshotDetailsDao.persist(snapshotDetail);
        }
        result = snapshotSvr.takeSnapshot(snapshotInfo);
        if (result.isFailed()) {
            s_logger.debug("Failed to take a snapshot: " + result.getResult());
            throw new CloudRuntimeException(result.getResult());
        }
        if (!canStorageSystemCreateVolumeFromSnapshot || !computeClusterSupportsResign) {
            performSnapshotAndCopyOnHostSide(volumeInfo, snapshotInfo);
        }
        snapshotOnPrimary = result.getSnapshot();
        backedUpSnapshot = backupSnapshot(snapshotOnPrimary);
        updateLocationTypeInDb(backedUpSnapshot);
    } finally {
        if (result != null && result.isSuccess()) {
            volumeInfo.stateTransit(Volume.Event.OperationSucceeded);
            if (snapshotOnPrimary != null && snapshotInfo.getLocationType() == Snapshot.LocationType.SECONDARY) {
                // remove the snapshot on primary storage
                try {
                    snapshotSvr.deleteSnapshot(snapshotOnPrimary);
                } catch (Exception e) {
                    s_logger.warn("Failed to clean up snapshot on primary Id:" + snapshotOnPrimary.getId() + " " + e.getMessage());
                }
            }
        } else {
            volumeInfo.stateTransit(Volume.Event.OperationFailed);
        }
    }
    snapshotDao.releaseFromLockTable(snapshotInfo.getId());
    return backedUpSnapshot;
}
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) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) SnapshotDetailsVO(com.cloud.storage.dao.SnapshotDetailsVO) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DB(com.cloud.utils.db.DB)

Example 10 with SnapshotResult

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

the class SnapshotTestWithFakeData method testTakeSnapshot.

//@Test
public void testTakeSnapshot() throws URISyntaxException {
    SnapshotVO snapshotVO = createSnapshotInDb();
    DataStore store = createDataStore();
    try {
        SnapshotInfo snapshotInfo = snapshotDataFactory.getSnapshot(snapshotVO.getId(), store);
        SnapshotResult result = snapshotService.takeSnapshot(snapshotInfo);
        Assert.assertTrue(result.isSuccess());
        SnapshotDataStoreVO storeRef = snapshotDataStoreDao.findByStoreSnapshot(store.getRole(), store.getId(), snapshotVO.getId());
        Assert.assertTrue(storeRef != null);
        Assert.assertTrue(storeRef.getState() == ObjectInDataStoreStateMachine.State.Ready);
        snapshotInfo = result.getSnashot();
        boolean deletResult = snapshotService.deleteSnapshot(snapshotInfo);
        Assert.assertTrue(deletResult);
        snapshotDataStoreDao.expunge(storeRef.getId());
    } finally {
        snapshotDao.expunge(snapshotVO.getId());
        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)

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