Search in sources :

Example 1 with SnapshotInfo

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

the class VolumeServiceImpl method createVolumeFromSnapshotCallback.

protected Void createVolumeFromSnapshotCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback, CreateVolumeFromBaseImageContext<VolumeApiResult> context) {
    CopyCommandResult result = callback.getResult();
    VolumeInfo volume = (VolumeInfo) context.templateOnStore;
    SnapshotInfo snapshot = context.snapshot;
    VolumeApiResult apiResult = new VolumeApiResult(volume);
    Event event = null;
    if (result.isFailed()) {
        apiResult.setResult(result.getResult());
        event = Event.OperationFailed;
    } else {
        event = Event.OperationSuccessed;
    }
    try {
        if (result.isSuccess()) {
            volume.processEvent(event, result.getAnswer());
        } else {
            volume.processEvent(event);
        }
        _volumeDetailsDao.removeDetail(volume.getId(), SNAPSHOT_ID);
    } catch (Exception e) {
        s_logger.debug("create volume from snapshot failed", e);
        apiResult.setResult(e.toString());
    }
    AsyncCallFuture<VolumeApiResult> future = context.future;
    future.complete(apiResult);
    return null;
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) Event(org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine.Event) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 2 with SnapshotInfo

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

the class XenserverSnapshotStrategy method deleteSnapshot.

@Override
public boolean deleteSnapshot(Long snapshotId) {
    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())) {
        List<SnapshotDataStoreVO> storeRefs = snapshotStoreDao.findBySnapshotId(snapshotId);
        for (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.
    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;
    }
    SnapshotObject obj = (SnapshotObject) snapshotOnImage;
    try {
        obj.processEvent(Snapshot.Event.DestroyRequested);
    } catch (NoTransitionException e) {
        s_logger.debug("Failed to set the state to destroying: ", e);
        return false;
    }
    try {
        boolean result = deleteSnapshotChain(snapshotOnImage);
        obj.processEvent(Snapshot.Event.OperationSucceeded);
        if (result) {
            //snapshot is deleted on backup storage, need to delete it on primary storage
            SnapshotDataStoreVO snapshotOnPrimary = snapshotStoreDao.findBySnapshot(snapshotId, DataStoreRole.Primary);
            if (snapshotOnPrimary != null) {
                SnapshotInfo snapshotOnPrimaryInfo = snapshotDataFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
                long volumeId = snapshotOnPrimary.getVolumeId();
                VolumeVO volumeVO = volumeDao.findById(volumeId);
                if (((PrimaryDataStoreImpl) snapshotOnPrimaryInfo.getDataStore()).getPoolType() == StoragePoolType.RBD && volumeVO != null) {
                    snapshotSvr.deleteSnapshot(snapshotOnPrimaryInfo);
                }
                snapshotOnPrimary.setState(State.Destroyed);
                snapshotStoreDao.update(snapshotOnPrimary.getId(), snapshotOnPrimary);
            }
        }
    } catch (Exception e) {
        s_logger.debug("Failed to delete snapshot: ", e);
        try {
            obj.processEvent(Snapshot.Event.OperationFailed);
        } catch (NoTransitionException e1) {
            s_logger.debug("Failed to change snapshot state: " + e.toString());
        }
        return false;
    }
    return true;
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PrimaryDataStoreImpl(org.apache.cloudstack.storage.datastore.PrimaryDataStoreImpl) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 3 with SnapshotInfo

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo 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 4 with SnapshotInfo

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

the class SnapshotTestWithFakeData method testConcurrentSnapshot.

@Test
public void testConcurrentSnapshot() throws URISyntaxException, InterruptedException, ExecutionException {
    DataStore store = createDataStore();
    final FakePrimaryDataStoreDriver dataStoreDriver = (FakePrimaryDataStoreDriver) store.getDriver();
    dataStoreDriver.makeTakeSnapshotSucceed(true);
    final VolumeInfo volumeInfo = createVolume(1L, store);
    Assert.assertTrue(volumeInfo.getState() == Volume.State.Ready);
    vol = volumeInfo;
    // final SnapshotPolicyVO policyVO = createSnapshotPolicy(vol.getId());
    ExecutorService pool = Executors.newFixedThreadPool(2);
    boolean result = false;
    List<Future<Boolean>> future = new ArrayList<Future<Boolean>>();
    for (int i = 0; i < 12; i++) {
        final int cnt = i;
        Future<Boolean> task = pool.submit(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                boolean r = true;
                try {
                    SnapshotVO snapshotVO = createSnapshotInDb(vol.getId());
                    VolumeObject volumeObject = (VolumeObject) vol;
                    Account account = mock(Account.class);
                    when(account.getId()).thenReturn(1L);
                    CreateSnapshotPayload createSnapshotPayload = mock(CreateSnapshotPayload.class);
                    when(createSnapshotPayload.getAccount()).thenReturn(account);
                    when(createSnapshotPayload.getSnapshotId()).thenReturn(snapshotVO.getId());
                    when(createSnapshotPayload.getSnapshotPolicyId()).thenReturn(0L);
                    volumeObject.addPayload(createSnapshotPayload);
                    if (cnt > 8) {
                        mockStorageMotionStrategy.makeBackupSnapshotSucceed(false);
                    }
                    SnapshotInfo newSnapshot = volumeService.takeSnapshot(vol);
                    if (newSnapshot == null) {
                        r = false;
                    }
                } catch (Exception e) {
                    r = false;
                }
                return r;
            }
        });
        Assert.assertTrue(task.get());
    }
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) CreateSnapshotPayload(com.cloud.storage.CreateSnapshotPayload) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) VolumeObject(org.apache.cloudstack.storage.volume.VolumeObject) URISyntaxException(java.net.URISyntaxException) ExecutionException(java.util.concurrent.ExecutionException) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 5 with SnapshotInfo

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo 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)

Aggregations

SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)40 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)23 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)17 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)16 SnapshotVO (com.cloud.storage.SnapshotVO)15 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)11 SnapshotResult (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult)11 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)10 ExecutionException (java.util.concurrent.ExecutionException)10 SnapshotDataStoreVO (org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)10 SnapshotStrategy (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy)9 DB (com.cloud.utils.db.DB)8 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)8 VolumeVO (com.cloud.storage.VolumeVO)7 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)6 CommandResult (org.apache.cloudstack.storage.command.CommandResult)6 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)5 Answer (com.cloud.agent.api.Answer)4 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)4 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)4