Search in sources :

Example 41 with Snapshot

use of com.cloud.storage.Snapshot in project cloudstack by apache.

the class SnapshotServiceImpl method backupSnapshot.

@Override
public SnapshotInfo backupSnapshot(SnapshotInfo snapshot) {
    SnapshotObject snapObj = (SnapshotObject) snapshot;
    AsyncCallFuture<SnapshotResult> future = new AsyncCallFuture<SnapshotResult>();
    SnapshotResult result = new SnapshotResult(snapshot, null);
    Snapshot.State origState = snapObj.getState();
    try {
        snapObj.processEvent(Snapshot.Event.BackupToSecondary);
        DataStore imageStore = findSnapshotImageStore(snapshot);
        if (imageStore == null) {
            throw new CloudRuntimeException("can not find an image stores");
        }
        SnapshotInfo snapshotOnImageStore = (SnapshotInfo) imageStore.create(snapshot);
        snapshotOnImageStore.processEvent(Event.CreateOnlyRequested);
        CopySnapshotContext<CommandResult> context = new CopySnapshotContext<CommandResult>(null, snapshot, snapshotOnImageStore, future);
        AsyncCallbackDispatcher<SnapshotServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().copySnapshotAsyncCallback(null, null)).setContext(context);
        motionSrv.copyAsync(snapshot, snapshotOnImageStore, caller);
    } catch (Exception e) {
        s_logger.debug("Failed to copy snapshot", e);
        result.setResult("Failed to copy snapshot:" + e.toString());
        try {
            // This will ensure that the original snapshot does not get deleted
            if (origState.equals(Snapshot.State.BackedUp)) {
                snapObj.processEvent(Snapshot.Event.OperationNotPerformed);
            } else {
                snapObj.processEvent(Snapshot.Event.OperationFailed);
            }
        } catch (NoTransitionException e1) {
            s_logger.debug("Failed to change state: " + e1.toString());
        }
        future.complete(result);
    }
    try {
        SnapshotResult res = future.get();
        if (res.isFailed()) {
            throw new CloudRuntimeException(res.getResult());
        }
        SnapshotInfo destSnapshot = res.getSnapshot();
        return destSnapshot;
    } catch (InterruptedException e) {
        s_logger.debug("failed copy snapshot", e);
        throw new CloudRuntimeException("Failed to copy snapshot", e);
    } catch (ExecutionException e) {
        s_logger.debug("Failed to copy snapshot", e);
        throw new CloudRuntimeException("Failed to copy snapshot", e);
    }
}
Also used : SnapshotResult(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult) 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) Snapshot(com.cloud.storage.Snapshot) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) ExecutionException(java.util.concurrent.ExecutionException) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 42 with Snapshot

use of com.cloud.storage.Snapshot in project cloudstack by apache.

the class CephSnapshotStrategyTest method configureAndVerifyCanHandle.

private void configureAndVerifyCanHandle(Date removed, boolean isSnapshotStoredOnRbdStoragePool) {
    Snapshot snapshot = Mockito.mock(Snapshot.class);
    SnapshotOperation[] snapshotOps = SnapshotOperation.values();
    Mockito.when(snapshot.getVolumeId()).thenReturn(0l);
    VolumeVO volumeVO = Mockito.mock(VolumeVO.class);
    Mockito.when(volumeVO.getRemoved()).thenReturn(removed);
    Mockito.when(volumeDao.findByIdIncludingRemoved(Mockito.anyLong())).thenReturn(volumeVO);
    Mockito.lenient().doReturn(isSnapshotStoredOnRbdStoragePool).when(cephSnapshotStrategy).isSnapshotStoredOnRbdStoragePool(Mockito.any());
    for (int i = 0; i < snapshotOps.length - 1; i++) {
        StrategyPriority strategyPriority = cephSnapshotStrategy.canHandle(snapshot, snapshotOps[i]);
        if (snapshotOps[i] == SnapshotOperation.REVERT && isSnapshotStoredOnRbdStoragePool) {
            Assert.assertEquals(StrategyPriority.HIGHEST, strategyPriority);
        } else {
            Assert.assertEquals(StrategyPriority.CANT_HANDLE, strategyPriority);
        }
    }
}
Also used : Snapshot(com.cloud.storage.Snapshot) SnapshotOperation(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy.SnapshotOperation) VolumeVO(com.cloud.storage.VolumeVO) StrategyPriority(org.apache.cloudstack.engine.subsystem.api.storage.StrategyPriority)

Example 43 with Snapshot

use of com.cloud.storage.Snapshot in project cloudstack by apache.

the class StorageSystemSnapshotStrategy method canHandle.

@Override
public StrategyPriority canHandle(Snapshot snapshot, SnapshotOperation op) {
    Snapshot.LocationType locationType = snapshot.getLocationType();
    // If the snapshot exists on Secondary Storage, we can't delete it.
    if (SnapshotOperation.DELETE.equals(op)) {
        if (Snapshot.LocationType.SECONDARY.equals(locationType)) {
            return StrategyPriority.CANT_HANDLE;
        }
        SnapshotDataStoreVO snapshotStore = snapshotStoreDao.findBySnapshot(snapshot.getId(), DataStoreRole.Image);
        // If the snapshot exists on Secondary Storage, we can't delete it.
        if (snapshotStore != null) {
            return StrategyPriority.CANT_HANDLE;
        }
        snapshotStore = snapshotStoreDao.findBySnapshot(snapshot.getId(), DataStoreRole.Primary);
        if (snapshotStore == null) {
            return StrategyPriority.CANT_HANDLE;
        }
        long snapshotStoragePoolId = snapshotStore.getDataStoreId();
        boolean storageSystemSupportsCapability = storageSystemSupportsCapability(snapshotStoragePoolId, DataStoreCapabilities.STORAGE_SYSTEM_SNAPSHOT.toString());
        return storageSystemSupportsCapability ? StrategyPriority.HIGHEST : StrategyPriority.CANT_HANDLE;
    }
    long volumeId = snapshot.getVolumeId();
    VolumeVO volumeVO = volumeDao.findByIdIncludingRemoved(volumeId);
    long volumeStoragePoolId = volumeVO.getPoolId();
    if (SnapshotOperation.REVERT.equals(op)) {
        boolean baseVolumeExists = volumeVO.getRemoved() == null;
        if (baseVolumeExists) {
            boolean acceptableFormat = isAcceptableRevertFormat(volumeVO);
            if (acceptableFormat) {
                SnapshotDataStoreVO snapshotStore = snapshotStoreDao.findBySnapshot(snapshot.getId(), DataStoreRole.Primary);
                boolean usingBackendSnapshot = usingBackendSnapshotFor(snapshot.getId());
                if (usingBackendSnapshot) {
                    if (snapshotStore != null) {
                        long snapshotStoragePoolId = snapshotStore.getDataStoreId();
                        boolean storageSystemSupportsCapability = storageSystemSupportsCapability(snapshotStoragePoolId, DataStoreCapabilities.CAN_REVERT_VOLUME_TO_SNAPSHOT.toString());
                        if (storageSystemSupportsCapability) {
                            return StrategyPriority.HIGHEST;
                        }
                        storageSystemSupportsCapability = storageSystemSupportsCapability(volumeStoragePoolId, DataStoreCapabilities.CAN_REVERT_VOLUME_TO_SNAPSHOT.toString());
                        if (storageSystemSupportsCapability) {
                            return StrategyPriority.HIGHEST;
                        }
                    }
                } else {
                    if (snapshotStore != null) {
                        long snapshotStoragePoolId = snapshotStore.getDataStoreId();
                        StoragePoolVO storagePoolVO = storagePoolDao.findById(snapshotStoragePoolId);
                        if (storagePoolVO.isManaged()) {
                            return StrategyPriority.HIGHEST;
                        }
                    }
                    StoragePoolVO storagePoolVO = storagePoolDao.findById(volumeStoragePoolId);
                    if (storagePoolVO.isManaged()) {
                        return StrategyPriority.HIGHEST;
                    }
                }
            }
        }
        return StrategyPriority.CANT_HANDLE;
    }
    boolean storageSystemSupportsCapability = storageSystemSupportsCapability(volumeStoragePoolId, DataStoreCapabilities.STORAGE_SYSTEM_SNAPSHOT.toString());
    return storageSystemSupportsCapability ? StrategyPriority.HIGHEST : StrategyPriority.CANT_HANDLE;
}
Also used : VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) Snapshot(com.cloud.storage.Snapshot) VolumeVO(com.cloud.storage.VolumeVO) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO)

Aggregations

Snapshot (com.cloud.storage.Snapshot)43 VMSnapshot (com.cloud.vm.snapshot.VMSnapshot)19 Test (org.junit.Test)11 ServerApiException (org.apache.cloudstack.api.ServerApiException)10 ServerApiException (com.cloud.api.ServerApiException)8 Volume (com.cloud.storage.Volume)8 VolumeVO (com.cloud.storage.VolumeVO)8 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)7 SnapshotResponse (com.cloud.api.response.SnapshotResponse)6 Account (com.cloud.user.Account)6 SnapshotResponse (org.apache.cloudstack.api.response.SnapshotResponse)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)4 DataStoreRole (com.cloud.storage.DataStoreRole)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)3