Search in sources :

Example 1 with CreateSnapshotPayload

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

the class CloudStackPrimaryDataStoreDriverImpl method takeSnapshot.

@Override
public void takeSnapshot(SnapshotInfo snapshot, AsyncCompletionCallback<CreateCmdResult> callback) {
    CreateCmdResult result = null;
    try {
        SnapshotObjectTO snapshotTO = (SnapshotObjectTO) snapshot.getTO();
        Object payload = snapshot.getPayload();
        if (payload != null && payload instanceof CreateSnapshotPayload) {
            CreateSnapshotPayload snapshotPayload = (CreateSnapshotPayload) payload;
            snapshotTO.setQuiescevm(snapshotPayload.getQuiescevm());
        }
        CreateObjectCommand cmd = new CreateObjectCommand(snapshotTO);
        EndPoint ep = epSelector.select(snapshot, StorageAction.TAKESNAPSHOT);
        Answer answer = null;
        if (ep == null) {
            String errMsg = "No remote endpoint to send createObjectCommand, check if host or ssvm is down?";
            s_logger.error(errMsg);
            answer = new Answer(cmd, false, errMsg);
        } else {
            answer = ep.sendMessage(cmd);
        }
        result = new CreateCmdResult(null, answer);
        if (answer != null && !answer.getResult()) {
            result.setResult(answer.getDetails());
        }
        callback.complete(result);
        return;
    } catch (Exception e) {
        s_logger.debug("Failed to take snapshot: " + snapshot.getId(), e);
        result = new CreateCmdResult(null, null);
        result.setResult(e.toString());
    }
    callback.complete(result);
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) ResizeVolumeAnswer(com.cloud.agent.api.storage.ResizeVolumeAnswer) Answer(com.cloud.agent.api.Answer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) CreateSnapshotPayload(com.cloud.storage.CreateSnapshotPayload) VolumeObject(org.apache.cloudstack.storage.volume.VolumeObject) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) CreateObjectCommand(org.apache.cloudstack.storage.command.CreateObjectCommand) StorageUnavailableException(com.cloud.exception.StorageUnavailableException)

Example 2 with CreateSnapshotPayload

use of com.cloud.storage.CreateSnapshotPayload 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 3 with CreateSnapshotPayload

use of com.cloud.storage.CreateSnapshotPayload 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 4 with CreateSnapshotPayload

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

the class SnapshotManagerImpl method takeSnapshot.

@Override
@DB
public SnapshotInfo takeSnapshot(VolumeInfo volume) throws ResourceAllocationException {
    CreateSnapshotPayload payload = (CreateSnapshotPayload) volume.getpayload();
    updateSnapshotPayload(volume.getPoolId(), payload);
    Long snapshotId = payload.getSnapshotId();
    Account snapshotOwner = payload.getAccount();
    SnapshotInfo snapshot = snapshotFactory.getSnapshot(snapshotId, volume.getDataStore());
    snapshot.addPayload(payload);
    try {
        SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.TAKE);
        if (snapshotStrategy == null) {
            throw new CloudRuntimeException("Can't find snapshot strategy to deal with snapshot:" + snapshotId);
        }
        snapshotStrategy.takeSnapshot(snapshot);
        try {
            postCreateSnapshot(volume.getId(), snapshotId, payload.getSnapshotPolicyId());
            DataStoreRole dataStoreRole = getDataStoreRole(snapshot, _snapshotStoreDao, dataStoreMgr);
            SnapshotDataStoreVO snapshotStoreRef = _snapshotStoreDao.findBySnapshot(snapshotId, dataStoreRole);
            if (snapshotStoreRef == null) {
                // The snapshot was not backed up to secondary.  Find the snap on primary
                snapshotStoreRef = _snapshotStoreDao.findBySnapshot(snapshotId, DataStoreRole.Primary);
                if (snapshotStoreRef == null) {
                    throw new CloudRuntimeException("Could not find snapshot");
                }
            }
            UsageEventUtils.publishUsageEvent(EventTypes.EVENT_SNAPSHOT_CREATE, snapshot.getAccountId(), snapshot.getDataCenterId(), snapshotId, snapshot.getName(), null, null, snapshotStoreRef.getPhysicalSize(), volume.getSize(), snapshot.getClass().getName(), snapshot.getUuid());
            // Correct the resource count of snapshot in case of delta snapshots.
            _resourceLimitMgr.decrementResourceCount(snapshotOwner.getId(), ResourceType.secondary_storage, new Long(volume.getSize() - snapshotStoreRef.getPhysicalSize()));
        } catch (Exception e) {
            s_logger.debug("post process snapshot failed", e);
        }
    } catch (CloudRuntimeException cre) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Failed to create snapshot" + cre.getLocalizedMessage());
        }
        _resourceLimitMgr.decrementResourceCount(snapshotOwner.getId(), ResourceType.snapshot);
        _resourceLimitMgr.decrementResourceCount(snapshotOwner.getId(), ResourceType.secondary_storage, new Long(volume.getSize()));
        throw cre;
    } catch (Exception e) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Failed to create snapshot", e);
        }
        _resourceLimitMgr.decrementResourceCount(snapshotOwner.getId(), ResourceType.snapshot);
        _resourceLimitMgr.decrementResourceCount(snapshotOwner.getId(), ResourceType.secondary_storage, new Long(volume.getSize()));
        throw new CloudRuntimeException("Failed to create snapshot", e);
    }
    return snapshot;
}
Also used : Account(com.cloud.user.Account) DataStoreRole(com.cloud.storage.DataStoreRole) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) CreateSnapshotPayload(com.cloud.storage.CreateSnapshotPayload) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) DB(com.cloud.utils.db.DB)

Example 5 with CreateSnapshotPayload

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

the class StorageSystemSnapshotStrategy method updateLocationTypeInDb.

private void updateLocationTypeInDb(SnapshotInfo snapshotInfo) {
    Object objPayload = snapshotInfo.getPayload();
    if (objPayload instanceof CreateSnapshotPayload) {
        CreateSnapshotPayload payload = (CreateSnapshotPayload) objPayload;
        SnapshotVO snapshot = snapshotDao.findById(snapshotInfo.getId());
        snapshot.setLocationType(payload.getLocationType());
        snapshotDao.update(snapshotInfo.getId(), snapshot);
    }
}
Also used : SnapshotVO(com.cloud.storage.SnapshotVO) CreateSnapshotPayload(com.cloud.storage.CreateSnapshotPayload)

Aggregations

CreateSnapshotPayload (com.cloud.storage.CreateSnapshotPayload)5 SnapshotVO (com.cloud.storage.SnapshotVO)3 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)3 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)2 Account (com.cloud.user.Account)2 DB (com.cloud.utils.db.DB)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)2 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)2 SnapshotDataStoreVO (org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)2 VolumeObject (org.apache.cloudstack.storage.volume.VolumeObject)2 Answer (com.cloud.agent.api.Answer)1 ResizeVolumeAnswer (com.cloud.agent.api.storage.ResizeVolumeAnswer)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)1 DataStoreRole (com.cloud.storage.DataStoreRole)1 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1