Search in sources :

Example 36 with SnapshotInfo

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

the class SnapshotTest method createVolumeFromSnapshot.

@Test
public void createVolumeFromSnapshot() throws InterruptedException, ExecutionException {
    VolumeInfo vol = createCopyBaseImage();
    SnapshotVO snapshotVO = createSnapshotInDb(vol);
    SnapshotInfo snapshot = this.snapshotFactory.getSnapshot(snapshotVO.getId(), vol.getDataStore());
    boolean result = false;
    SnapshotStrategy snapshotStrategy = storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.TAKE);
    if (snapshotStrategy != null) {
        snapshot = snapshotStrategy.takeSnapshot(snapshot);
        result = true;
    }
    AssertJUnit.assertTrue(result);
    VolumeVO volVO = createVolume(vol.getTemplateId(), vol.getPoolId());
    VolumeInfo newVol = this.volFactory.getVolume(volVO.getId());
    AsyncCallFuture<VolumeApiResult> volFuture = this.volumeService.createVolumeFromSnapshot(newVol, newVol.getDataStore(), snapshot);
    VolumeApiResult apiResult = volFuture.get();
    Assert.assertTrue(apiResult.isSuccess());
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) VolumeApiResult(org.apache.cloudstack.engine.subsystem.api.storage.VolumeService.VolumeApiResult) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy) Test(org.testng.annotations.Test)

Example 37 with SnapshotInfo

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

the class VolumeOrchestrator method createVolumeFromSnapshot.

@DB
@Override
public VolumeInfo createVolumeFromSnapshot(Volume volume, Snapshot snapshot, UserVm vm) throws StorageUnavailableException {
    Account account = _entityMgr.findById(Account.class, volume.getAccountId());
    final HashSet<StoragePool> poolsToAvoid = new HashSet<StoragePool>();
    StoragePool pool = null;
    Set<Long> podsToAvoid = new HashSet<Long>();
    Pair<Pod, Long> pod = null;
    DiskOffering diskOffering = _entityMgr.findById(DiskOffering.class, volume.getDiskOfferingId());
    DataCenter dc = _entityMgr.findById(DataCenter.class, volume.getDataCenterId());
    DiskProfile dskCh = new DiskProfile(volume, diskOffering, snapshot.getHypervisorType());
    String msg = "There are no available storage pools to store the volume in";
    if (vm != null) {
        Pod podofVM = _entityMgr.findById(Pod.class, vm.getPodIdToDeployIn());
        if (podofVM != null) {
            pod = new Pair<Pod, Long>(podofVM, podofVM.getId());
        }
    }
    if (vm != null && pod != null) {
        //if VM is running use the hostId to find the clusterID. If it is stopped, refer the cluster where the ROOT volume of the VM exists.
        Long hostId = null;
        Long clusterId = null;
        if (vm.getState() == State.Running) {
            hostId = vm.getHostId();
            if (hostId != null) {
                Host vmHost = _entityMgr.findById(Host.class, hostId);
                clusterId = vmHost.getClusterId();
            }
        } else {
            List<VolumeVO> rootVolumesOfVm = _volsDao.findByInstanceAndType(vm.getId(), Volume.Type.ROOT);
            if (rootVolumesOfVm.size() != 1) {
                throw new CloudRuntimeException("The VM " + vm.getHostName() + " has more than one ROOT volume and is in an invalid state. Please contact Cloud Support.");
            } else {
                VolumeVO rootVolumeOfVm = rootVolumesOfVm.get(0);
                StoragePoolVO rootDiskPool = _storagePoolDao.findById(rootVolumeOfVm.getPoolId());
                clusterId = (rootDiskPool == null ? null : rootDiskPool.getClusterId());
            }
        }
        // Determine what storage pool to store the volume in
        while ((pool = findStoragePool(dskCh, dc, pod.first(), clusterId, hostId, vm, poolsToAvoid)) != null) {
            break;
        }
        if (pool == null) {
            //pool could not be found in the VM's pod/cluster.
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Could not find any storage pool to create Volume in the pod/cluster of the provided VM " + vm.getUuid());
            }
            StringBuilder addDetails = new StringBuilder(msg);
            addDetails.append(", Could not find any storage pool to create Volume in the pod/cluster of the VM ");
            addDetails.append(vm.getUuid());
            msg = addDetails.toString();
        }
    } else {
        // Determine what pod to store the volume in
        while ((pod = findPod(null, null, dc, account.getId(), podsToAvoid)) != null) {
            podsToAvoid.add(pod.first().getId());
            // Determine what storage pool to store the volume in
            while ((pool = findStoragePool(dskCh, dc, pod.first(), null, null, null, poolsToAvoid)) != null) {
                break;
            }
            if (pool != null) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Found a suitable pool for create volume: " + pool.getId());
                }
                break;
            }
        }
    }
    if (pool == null) {
        s_logger.info(msg);
        throw new StorageUnavailableException(msg, -1);
    }
    VolumeInfo vol = volFactory.getVolume(volume.getId());
    DataStore store = dataStoreMgr.getDataStore(pool.getId(), DataStoreRole.Primary);
    DataStoreRole dataStoreRole = getDataStoreRole(snapshot);
    SnapshotInfo snapInfo = snapshotFactory.getSnapshot(snapshot.getId(), dataStoreRole);
    if (snapInfo == null && dataStoreRole == DataStoreRole.Image) {
        // snapshot is not backed up to secondary, let's do that now.
        snapInfo = snapshotFactory.getSnapshot(snapshot.getId(), DataStoreRole.Primary);
        if (snapInfo == null) {
            throw new CloudRuntimeException("Cannot find snapshot " + snapshot.getId());
        }
        // We need to copy the snapshot onto secondary.
        SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
        snapshotStrategy.backupSnapshot(snapInfo);
        // Attempt to grab it again.
        snapInfo = snapshotFactory.getSnapshot(snapshot.getId(), dataStoreRole);
        if (snapInfo == null) {
            throw new CloudRuntimeException("Cannot find snapshot " + snapshot.getId() + " on secondary and could not create backup");
        }
    }
    // don't try to perform a sync if the DataStoreRole of the snapshot is equal to DataStoreRole.Primary
    if (!DataStoreRole.Primary.equals(dataStoreRole)) {
        try {
            // sync snapshot to region store if necessary
            DataStore snapStore = snapInfo.getDataStore();
            long snapVolId = snapInfo.getVolumeId();
            _snapshotSrv.syncVolumeSnapshotsToRegionStore(snapVolId, snapStore);
        } catch (Exception ex) {
            // log but ignore the sync error to avoid any potential S3 down issue, it should be sync next time
            s_logger.warn(ex.getMessage(), ex);
        }
    }
    // create volume on primary from snapshot
    AsyncCallFuture<VolumeApiResult> future = volService.createVolumeFromSnapshot(vol, store, snapInfo);
    try {
        VolumeApiResult result = future.get();
        if (result.isFailed()) {
            s_logger.debug("Failed to create volume from snapshot:" + result.getResult());
            throw new CloudRuntimeException("Failed to create volume from snapshot:" + result.getResult());
        }
        return result.getVolume();
    } catch (InterruptedException e) {
        s_logger.debug("Failed to create volume from snapshot", e);
        throw new CloudRuntimeException("Failed to create volume from snapshot", e);
    } catch (ExecutionException e) {
        s_logger.debug("Failed to create volume from snapshot", e);
        throw new CloudRuntimeException("Failed to create volume from snapshot", e);
    }
}
Also used : Account(com.cloud.user.Account) StoragePool(com.cloud.storage.StoragePool) DiskOffering(com.cloud.offering.DiskOffering) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) VolumeApiResult(org.apache.cloudstack.engine.subsystem.api.storage.VolumeService.VolumeApiResult) DataStoreRole(com.cloud.storage.DataStoreRole) VolumeVO(com.cloud.storage.VolumeVO) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) ExecutionException(java.util.concurrent.ExecutionException) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy) HashSet(java.util.HashSet) Pod(com.cloud.dc.Pod) Host(com.cloud.host.Host) DiskProfile(com.cloud.vm.DiskProfile) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) InsufficientStorageCapacityException(com.cloud.exception.InsufficientStorageCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) DataCenter(com.cloud.dc.DataCenter) DB(com.cloud.utils.db.DB)

Example 38 with SnapshotInfo

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

the class AncientDataMotionStrategy method copySnapshot.

protected Answer copySnapshot(DataObject srcData, DataObject destData) {
    String value = configDao.getValue(Config.BackupSnapshotWait.toString());
    int _backupsnapshotwait = NumbersUtil.parseInt(value, Integer.parseInt(Config.BackupSnapshotWait.getDefaultValue()));
    DataObject cacheData = null;
    SnapshotInfo snapshotInfo = (SnapshotInfo) srcData;
    Object payload = snapshotInfo.getPayload();
    Boolean fullSnapshot = true;
    if (payload != null) {
        fullSnapshot = (Boolean) payload;
    }
    Map<String, String> options = new HashMap<String, String>();
    options.put("fullSnapshot", fullSnapshot.toString());
    Answer answer = null;
    try {
        if (needCacheStorage(srcData, destData)) {
            Scope selectedScope = pickCacheScopeForCopy(srcData, destData);
            cacheData = cacheMgr.getCacheObject(srcData, selectedScope);
            CopyCommand cmd = new CopyCommand(srcData.getTO(), addFullCloneFlagOnVMwareDest(destData.getTO()), _backupsnapshotwait, VirtualMachineManager.ExecuteInSequence.value());
            cmd.setCacheTO(cacheData.getTO());
            cmd.setOptions(options);
            EndPoint ep = selector.select(srcData, destData);
            if (ep == null) {
                String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
                s_logger.error(errMsg);
                answer = new Answer(cmd, false, errMsg);
            } else {
                answer = ep.sendMessage(cmd);
            }
        } else {
            addFullCloneFlagOnVMwareDest(destData.getTO());
            CopyCommand cmd = new CopyCommand(srcData.getTO(), destData.getTO(), _backupsnapshotwait, VirtualMachineManager.ExecuteInSequence.value());
            cmd.setOptions(options);
            EndPoint ep = selector.select(srcData, destData, StorageAction.BACKUPSNAPSHOT);
            if (ep == null) {
                String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
                s_logger.error(errMsg);
                answer = new Answer(cmd, false, errMsg);
            } else {
                answer = ep.sendMessage(cmd);
            }
        }
        // clean up cache entry
        if (cacheData != null) {
            cacheMgr.deleteCacheObject(cacheData);
        }
        return answer;
    } catch (Exception e) {
        s_logger.debug("copy snasphot failed: " + e.toString());
        if (cacheData != null) {
            cacheMgr.deleteCacheObject(cacheData);
        }
        throw new CloudRuntimeException(e.toString());
    }
}
Also used : HashMap(java.util.HashMap) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(org.apache.cloudstack.storage.RemoteHostEndPoint) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(org.apache.cloudstack.storage.RemoteHostEndPoint) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Answer(com.cloud.agent.api.Answer) MigrateVolumeAnswer(com.cloud.agent.api.storage.MigrateVolumeAnswer) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) Scope(org.apache.cloudstack.engine.subsystem.api.storage.Scope) HostScope(org.apache.cloudstack.engine.subsystem.api.storage.HostScope) ClusterScope(org.apache.cloudstack.engine.subsystem.api.storage.ClusterScope) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject)

Example 39 with SnapshotInfo

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

the class AncientDataMotionStrategy method createTemplateFromSnapshot.

@DB
protected Answer createTemplateFromSnapshot(DataObject srcData, DataObject destData) {
    String value = configDao.getValue(Config.CreatePrivateTemplateFromSnapshotWait.toString());
    int _createprivatetemplatefromsnapshotwait = NumbersUtil.parseInt(value, Integer.parseInt(Config.CreatePrivateTemplateFromSnapshotWait.getDefaultValue()));
    boolean needCache = false;
    if (needCacheStorage(srcData, destData)) {
        needCache = true;
        SnapshotInfo snapshot = (SnapshotInfo) srcData;
        srcData = cacheSnapshotChain(snapshot, snapshot.getDataStore().getScope());
    }
    EndPoint ep = null;
    if (srcData.getDataStore().getRole() == DataStoreRole.Primary) {
        ep = selector.select(destData);
    } else {
        ep = selector.select(srcData, destData);
    }
    CopyCommand cmd = new CopyCommand(srcData.getTO(), addFullCloneFlagOnVMwareDest(destData.getTO()), _createprivatetemplatefromsnapshotwait, VirtualMachineManager.ExecuteInSequence.value());
    Answer answer = null;
    if (ep == null) {
        String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
        s_logger.error(errMsg);
        answer = new Answer(cmd, false, errMsg);
    } else {
        answer = ep.sendMessage(cmd);
    }
    // clean up snapshot copied to staging
    if (needCache && srcData != null) {
        // reduce ref count, but keep it there on cache which is converted from previous secondary storage
        cacheMgr.releaseCacheObject(srcData);
    }
    return answer;
}
Also used : Answer(com.cloud.agent.api.Answer) MigrateVolumeAnswer(com.cloud.agent.api.storage.MigrateVolumeAnswer) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(org.apache.cloudstack.storage.RemoteHostEndPoint) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(org.apache.cloudstack.storage.RemoteHostEndPoint) DB(com.cloud.utils.db.DB)

Example 40 with SnapshotInfo

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

the class SolidFirePrimaryDataStoreDriver method createAsync.

@Override
public void createAsync(DataStore dataStore, DataObject dataObject, AsyncCompletionCallback<CreateCmdResult> callback) {
    String iqn = null;
    String errMsg = null;
    try {
        if (dataObject.getType() == DataObjectType.VOLUME) {
            iqn = createVolume((VolumeInfo) dataObject, dataStore.getId());
        } else if (dataObject.getType() == DataObjectType.SNAPSHOT) {
            createTempVolume((SnapshotInfo) dataObject, dataStore.getId());
        } else if (dataObject.getType() == DataObjectType.TEMPLATE) {
            iqn = createTemplateVolume((TemplateInfo) dataObject, dataStore.getId());
        } else {
            errMsg = "Invalid DataObjectType (" + dataObject.getType() + ") passed to createAsync";
            LOGGER.error(errMsg);
        }
    } catch (Exception ex) {
        errMsg = ex.getMessage();
        LOGGER.error(errMsg);
        if (callback == null) {
            throw ex;
        }
    }
    if (callback != null) {
        // path = iqn
        // size is pulled from DataObject instance, if errMsg is null
        CreateCmdResult result = new CreateCmdResult(iqn, new Answer(null, errMsg == null, errMsg));
        result.setResult(errMsg);
        callback.complete(result);
    }
}
Also used : CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) Answer(com.cloud.agent.api.Answer) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

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