Search in sources :

Example 1 with DataStoreRole

use of com.cloud.model.enumeration.DataStoreRole in project cosmic by MissionCriticalCloud.

the class VolumeOrchestrator method createVolumeFromSnapshot.

@DB
@Override
public VolumeInfo createVolumeFromSnapshot(final Volume volume, final Snapshot snapshot, final UserVm vm) throws StorageUnavailableException {
    final Account account = this._entityMgr.findById(Account.class, volume.getAccountId());
    final HashSet<StoragePool> poolsToAvoid = new HashSet<>();
    StoragePool pool = null;
    final Set<Long> podsToAvoid = new HashSet<>();
    Pair<Pod, Long> pod = null;
    final DiskOffering diskOffering = this._entityMgr.findById(DiskOffering.class, volume.getDiskOfferingId());
    final DataCenter dc = this._entityMgr.findById(DataCenter.class, volume.getDataCenterId());
    final DiskProfile dskCh = new DiskProfile(volume, diskOffering, snapshot.getHypervisorType());
    String msg = "There are no available storage pools to store the volume in";
    if (vm != null) {
        final Pod podofVM = this._entityMgr.findById(Pod.class, vm.getPodIdToDeployIn());
        if (podofVM != null) {
            pod = new Pair<>(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) {
                final Host vmHost = this._entityMgr.findById(Host.class, hostId);
                clusterId = vmHost.getClusterId();
            }
        } else {
            final List<VolumeVO> rootVolumesOfVm = this._volsDao.findByInstanceAndType(vm.getId(), VolumeType.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 {
                final VolumeVO rootVolumeOfVm = rootVolumesOfVm.get(0);
                final StoragePoolVO rootDiskPool = this._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());
            }
            final 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);
    }
    final VolumeInfo vol = this.volFactory.getVolume(volume.getId());
    final DataStore store = this.dataStoreMgr.getDataStore(pool.getId(), DataStoreRole.Primary);
    final DataStoreRole dataStoreRole = getDataStoreRole(snapshot);
    SnapshotInfo snapInfo = this.snapshotFactory.getSnapshot(snapshot.getId(), dataStoreRole);
    if (snapInfo == null && dataStoreRole == DataStoreRole.Image) {
        // snapshot is not backed up to secondary, let's do that now.
        snapInfo = this.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.
        final SnapshotStrategy snapshotStrategy = this._storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
        snapshotStrategy.backupSnapshot(snapInfo);
        // Attempt to grab it again.
        snapInfo = this.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
            final DataStore snapStore = snapInfo.getDataStore();
            final long snapVolId = snapInfo.getVolumeId();
            this._snapshotSrv.syncVolumeSnapshotsToRegionStore(snapVolId, snapStore);
        } catch (final 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
    final AsyncCallFuture<VolumeService.VolumeApiResult> future = this.volService.createVolumeFromSnapshot(vol, store, snapInfo);
    try {
        final VolumeService.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 (final InterruptedException e) {
        s_logger.debug("Failed to create volume from snapshot", e);
        throw new CloudRuntimeException("Failed to create volume from snapshot", e);
    } catch (final 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.legacymodel.user.Account) StoragePool(com.cloud.legacymodel.storage.StoragePool) DiskOffering(com.cloud.legacymodel.storage.DiskOffering) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) DataStoreRole(com.cloud.model.enumeration.DataStoreRole) VolumeVO(com.cloud.storage.VolumeVO) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeService(com.cloud.engine.subsystem.api.storage.VolumeService) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) PrimaryDataStore(com.cloud.engine.subsystem.api.storage.PrimaryDataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) ExecutionException(java.util.concurrent.ExecutionException) SnapshotStrategy(com.cloud.engine.subsystem.api.storage.SnapshotStrategy) HashSet(java.util.HashSet) Pod(com.cloud.legacymodel.dc.Pod) Host(com.cloud.legacymodel.dc.Host) DiskProfile(com.cloud.legacymodel.storage.DiskProfile) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) ExecutionException(java.util.concurrent.ExecutionException) ConfigurationException(javax.naming.ConfigurationException) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) DataCenter(com.cloud.legacymodel.dc.DataCenter) DB(com.cloud.utils.db.DB)

Example 2 with DataStoreRole

use of com.cloud.model.enumeration.DataStoreRole in project cosmic by MissionCriticalCloud.

the class CloudStackImageStoreLifeCycleImpl method initialize.

@Override
public DataStore initialize(final Map<String, Object> dsInfos) {
    final Long dcId = (Long) dsInfos.get("zoneId");
    final String url = (String) dsInfos.get("url");
    String name = (String) dsInfos.get("name");
    if (name == null) {
        name = url;
    }
    final String providerName = (String) dsInfos.get("providerName");
    final DataStoreRole role = (DataStoreRole) dsInfos.get("role");
    final Map<String, String> details = (Map<String, String>) dsInfos.get("details");
    String logString = "";
    if (url.contains("cifs")) {
        logString = cleanPassword(url);
    } else {
        logString = StringUtils.cleanString(url);
    }
    s_logger.info("Trying to add a new data store at " + logString + " to data center " + dcId);
    URI uri = null;
    try {
        uri = new URI(UriUtils.encodeURIComponent(url));
        if (uri.getScheme() == null) {
            throw new InvalidParameterValueException("uri.scheme is null " + StringUtils.cleanString(url) + ", add nfs:// (or cifs://) as a prefix");
        } else if (uri.getScheme().equalsIgnoreCase("nfs")) {
            if (uri.getHost() == null || uri.getHost().equalsIgnoreCase("") || uri.getPath() == null || uri.getPath().equalsIgnoreCase("")) {
                throw new InvalidParameterValueException("Your host and/or path is wrong.  Make sure it's of the format nfs://hostname/path");
            }
        } else if (uri.getScheme().equalsIgnoreCase("cifs")) {
            // Don't validate against a URI encoded URI.
            final URI cifsUri = new URI(url);
            final String warnMsg = UriUtils.getCifsUriParametersProblems(cifsUri);
            if (warnMsg != null) {
                throw new InvalidParameterValueException(warnMsg);
            }
        }
    } catch (final URISyntaxException e) {
        throw new InvalidParameterValueException(url + " is not a valid uri");
    }
    if (dcId == null) {
        throw new InvalidParameterValueException("DataCenter id is null, and cloudstack default image store has to be associated with a data center");
    }
    final Map<String, Object> imageStoreParameters = new HashMap<>();
    imageStoreParameters.put("name", name);
    imageStoreParameters.put("zoneId", dcId);
    imageStoreParameters.put("url", url);
    imageStoreParameters.put("protocol", uri.getScheme().toLowerCase());
    // default cloudstack provider only supports zone-wide image store
    imageStoreParameters.put("scope", ScopeType.ZONE);
    imageStoreParameters.put("providerName", providerName);
    imageStoreParameters.put("role", role);
    final ImageStoreVO ids = imageStoreHelper.createImageStore(imageStoreParameters, details);
    return imageStoreMgr.getImageStore(ids.getId());
}
Also used : DataStoreRole(com.cloud.model.enumeration.DataStoreRole) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) HashMap(java.util.HashMap) URISyntaxException(java.net.URISyntaxException) ImageStoreVO(com.cloud.storage.datastore.db.ImageStoreVO) HashMap(java.util.HashMap) Map(java.util.Map) URI(java.net.URI)

Example 3 with DataStoreRole

use of com.cloud.model.enumeration.DataStoreRole in project cosmic by MissionCriticalCloud.

the class DefaultEndPointSelector method moveBetweenCacheAndImage.

protected boolean moveBetweenCacheAndImage(final DataStore srcStore, final DataStore destStore) {
    final DataStoreRole srcRole = srcStore.getRole();
    final DataStoreRole destRole = destStore.getRole();
    if (srcRole == DataStoreRole.Image && destRole == DataStoreRole.ImageCache || srcRole == DataStoreRole.ImageCache && destRole == DataStoreRole.Image) {
        return true;
    } else {
        return false;
    }
}
Also used : DataStoreRole(com.cloud.model.enumeration.DataStoreRole)

Example 4 with DataStoreRole

use of com.cloud.model.enumeration.DataStoreRole in project cosmic by MissionCriticalCloud.

the class DefaultEndPointSelector method moveBetweenPrimaryImage.

protected boolean moveBetweenPrimaryImage(final DataStore srcStore, final DataStore destStore) {
    final DataStoreRole srcRole = srcStore.getRole();
    final DataStoreRole destRole = destStore.getRole();
    if (srcRole == DataStoreRole.Primary && destRole.isImageStore() || srcRole.isImageStore() && destRole == DataStoreRole.Primary) {
        return true;
    } else {
        return false;
    }
}
Also used : DataStoreRole(com.cloud.model.enumeration.DataStoreRole)

Example 5 with DataStoreRole

use of com.cloud.model.enumeration.DataStoreRole in project cosmic by MissionCriticalCloud.

the class DefaultEndPointSelector method moveBetweenImages.

protected boolean moveBetweenImages(final DataStore srcStore, final DataStore destStore) {
    final DataStoreRole srcRole = srcStore.getRole();
    final DataStoreRole destRole = destStore.getRole();
    if (srcRole == DataStoreRole.Image && destRole == DataStoreRole.Image) {
        return true;
    } else {
        return false;
    }
}
Also used : DataStoreRole(com.cloud.model.enumeration.DataStoreRole)

Aggregations

DataStoreRole (com.cloud.model.enumeration.DataStoreRole)7 SnapshotInfo (com.cloud.engine.subsystem.api.storage.SnapshotInfo)3 VolumeVO (com.cloud.storage.VolumeVO)3 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)2 SnapshotStrategy (com.cloud.engine.subsystem.api.storage.SnapshotStrategy)2 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)2 DataCenter (com.cloud.legacymodel.dc.DataCenter)2 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)2 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)2 DB (com.cloud.utils.db.DB)2 ExecutionException (java.util.concurrent.ExecutionException)2 ResourceTagResponse (com.cloud.api.response.ResourceTagResponse)1 SnapshotResponse (com.cloud.api.response.SnapshotResponse)1 VMSnapshotResponse (com.cloud.api.response.VMSnapshotResponse)1 PrimaryDataStore (com.cloud.engine.subsystem.api.storage.PrimaryDataStore)1 TemplateInfo (com.cloud.engine.subsystem.api.storage.TemplateInfo)1 TemplateApiResult (com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult)1 VolumeService (com.cloud.engine.subsystem.api.storage.VolumeService)1 ActionEvent (com.cloud.event.ActionEvent)1 Host (com.cloud.legacymodel.dc.Host)1