Search in sources :

Example 16 with AsyncCallFuture

use of org.apache.cloudstack.framework.async.AsyncCallFuture in project cloudstack by apache.

the class VolumeServiceImpl method copyVolume.

@Override
public AsyncCallFuture<VolumeApiResult> copyVolume(VolumeInfo srcVolume, DataStore destStore) {
    if (srcVolume.getState() == Volume.State.Uploaded) {
        return copyVolumeFromImageToPrimary(srcVolume, destStore);
    }
    if (destStore.getRole() == DataStoreRole.Image) {
        return copyVolumeFromPrimaryToImage(srcVolume, destStore);
    }
    AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
    VolumeApiResult res = new VolumeApiResult(srcVolume);
    try {
        if (!snapshotMgr.canOperateOnVolume(srcVolume)) {
            s_logger.debug("There are snapshots creating on this volume, can not move this volume");
            res.setResult("There are snapshots creating on this volume, can not move this volume");
            future.complete(res);
            return future;
        }
        VolumeVO destVol = duplicateVolumeOnAnotherStorage(srcVolume, (StoragePool) destStore);
        VolumeInfo destVolume = volFactory.getVolume(destVol.getId(), destStore);
        destVolume.processEvent(Event.MigrationCopyRequested);
        srcVolume.processEvent(Event.MigrationRequested);
        CopyVolumeContext<VolumeApiResult> context = new CopyVolumeContext<VolumeApiResult>(null, future, srcVolume, destVolume, destStore);
        AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().copyVolumeCallBack(null, null)).setContext(context);
        motionSrv.copyAsync(srcVolume, destVolume, caller);
    } catch (Exception e) {
        s_logger.debug("Failed to copy volume" + e);
        res.setResult(e.toString());
        future.complete(res);
    }
    return future;
}
Also used : AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) VolumeVO(com.cloud.storage.VolumeVO) 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 17 with AsyncCallFuture

use of org.apache.cloudstack.framework.async.AsyncCallFuture in project cloudstack by apache.

the class VolumeServiceImpl method createManagedVolumeCopyTemplateAsync.

private void createManagedVolumeCopyTemplateAsync(VolumeInfo volumeInfo, PrimaryDataStore primaryDataStore, TemplateInfo srcTemplateInfo, Host destHost, AsyncCallFuture<VolumeApiResult> future) {
    try {
        // Create a volume on managed storage.
        TemplateInfo destTemplateInfo = (TemplateInfo) primaryDataStore.create(srcTemplateInfo, false);
        AsyncCallFuture<VolumeApiResult> createVolumeFuture = createVolumeAsync(volumeInfo, primaryDataStore);
        VolumeApiResult createVolumeResult = createVolumeFuture.get();
        if (createVolumeResult.isFailed()) {
            throw new CloudRuntimeException("Creation of a volume failed: " + createVolumeResult.getResult());
        }
        // Refresh the volume info from the DB.
        volumeInfo = volFactory.getVolume(volumeInfo.getId(), primaryDataStore);
        ManagedCreateBaseImageContext<CreateCmdResult> context = new ManagedCreateBaseImageContext<CreateCmdResult>(null, volumeInfo, primaryDataStore, srcTemplateInfo, future);
        AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().managedCopyBaseImageCallback(null, null)).setContext(context);
        Map<String, String> details = new HashMap<String, String>();
        details.put(PrimaryDataStore.MANAGED, Boolean.TRUE.toString());
        details.put(PrimaryDataStore.STORAGE_HOST, primaryDataStore.getHostAddress());
        details.put(PrimaryDataStore.STORAGE_PORT, String.valueOf(primaryDataStore.getPort()));
        // for managed storage, the storage repository (XenServer) or datastore (ESX) name is based off of the iScsiName property of a volume
        details.put(PrimaryDataStore.MANAGED_STORE_TARGET, volumeInfo.get_iScsiName());
        details.put(PrimaryDataStore.MANAGED_STORE_TARGET_ROOT_VOLUME, volumeInfo.getName());
        details.put(PrimaryDataStore.VOLUME_SIZE, String.valueOf(volumeInfo.getSize()));
        ChapInfo chapInfo = getChapInfo(volumeInfo, primaryDataStore);
        if (chapInfo != null) {
            details.put(PrimaryDataStore.CHAP_INITIATOR_USERNAME, chapInfo.getInitiatorUsername());
            details.put(PrimaryDataStore.CHAP_INITIATOR_SECRET, chapInfo.getInitiatorSecret());
            details.put(PrimaryDataStore.CHAP_TARGET_USERNAME, chapInfo.getTargetUsername());
            details.put(PrimaryDataStore.CHAP_TARGET_SECRET, chapInfo.getTargetSecret());
        }
        primaryDataStore.setDetails(details);
        grantAccess(volumeInfo, destHost, primaryDataStore);
        try {
            motionSrv.copyAsync(srcTemplateInfo, destTemplateInfo, destHost, caller);
        } finally {
            revokeAccess(volumeInfo, destHost, primaryDataStore);
        }
    } catch (Throwable t) {
        String errMsg = t.toString();
        volumeInfo.processEvent(Event.DestroyRequested);
        try {
            AsyncCallFuture<VolumeApiResult> expungeVolumeFuture = expungeVolumeAsync(volumeInfo);
            VolumeApiResult expungeVolumeResult = expungeVolumeFuture.get();
            if (expungeVolumeResult.isFailed()) {
                errMsg += " : Failed to expunge a volume that was created";
            }
        } catch (Exception ex) {
            errMsg += " : " + ex.getMessage();
        }
        VolumeApiResult result = new VolumeApiResult(volumeInfo);
        result.setResult(errMsg);
        future.complete(result);
    }
}
Also used : HashMap(java.util.HashMap) ChapInfo(org.apache.cloudstack.engine.subsystem.api.storage.ChapInfo) CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 18 with AsyncCallFuture

use of org.apache.cloudstack.framework.async.AsyncCallFuture in project cloudstack by apache.

the class VolumeServiceImpl method createManagedStorageVolumeFromTemplateAsync.

@Override
public AsyncCallFuture<VolumeApiResult> createManagedStorageVolumeFromTemplateAsync(VolumeInfo volumeInfo, long destDataStoreId, TemplateInfo srcTemplateInfo, long destHostId) {
    PrimaryDataStore destPrimaryDataStore = dataStoreMgr.getPrimaryDataStore(destDataStoreId);
    Host destHost = _hostDao.findById(destHostId);
    if (destHost == null) {
        throw new CloudRuntimeException("Destination host should not be null.");
    }
    Boolean storageCanCloneVolume = new Boolean(destPrimaryDataStore.getDriver().getCapabilities().get(DataStoreCapabilities.CAN_CREATE_VOLUME_FROM_VOLUME.toString()));
    boolean computeZoneSupportsResign = computeZoneSupportsResign(destHost.getDataCenterId(), destHost.getHypervisorType());
    AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<>();
    if (storageCanCloneVolume && computeZoneSupportsResign) {
        s_logger.debug("Storage " + destDataStoreId + " can support cloning using a cached template and host cluster can perform UUID resigning.");
        TemplateInfo templateOnPrimary = destPrimaryDataStore.getTemplate(srcTemplateInfo.getId());
        if (templateOnPrimary == null) {
            templateOnPrimary = createManagedTemplateVolume(srcTemplateInfo, destPrimaryDataStore);
            if (templateOnPrimary == null) {
                throw new CloudRuntimeException("Failed to create template " + srcTemplateInfo.getUniqueName() + " on primary storage: " + destDataStoreId);
            }
        }
        // Copy the template to the template volume.
        VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.findByPoolTemplate(destPrimaryDataStore.getId(), templateOnPrimary.getId());
        if (templatePoolRef == null) {
            throw new CloudRuntimeException("Failed to find template " + srcTemplateInfo.getUniqueName() + " in storage pool " + destPrimaryDataStore.getId());
        }
        if (templatePoolRef.getDownloadState() == Status.NOT_DOWNLOADED) {
            copyTemplateToManagedTemplateVolume(srcTemplateInfo, templateOnPrimary, templatePoolRef, destPrimaryDataStore, destHost);
        }
        // We have a template on primary storage. Clone it to new volume.
        s_logger.debug("Creating a clone from template on primary storage " + destDataStoreId);
        createManagedVolumeCloneTemplateAsync(volumeInfo, templateOnPrimary, destPrimaryDataStore, future);
    } else {
        s_logger.debug("Primary storage does not support cloning or no support for UUID resigning on the host side; copying the template normally");
        createManagedVolumeCopyTemplateAsync(volumeInfo, destPrimaryDataStore, srcTemplateInfo, destHost, future);
    }
    return future;
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Host(com.cloud.host.Host) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Example 19 with AsyncCallFuture

use of org.apache.cloudstack.framework.async.AsyncCallFuture in project cloudstack by apache.

the class VolumeServiceImpl method createVolumeAsync.

@Override
public AsyncCallFuture<VolumeApiResult> createVolumeAsync(VolumeInfo volume, DataStore dataStore) {
    AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
    DataObject volumeOnStore = dataStore.create(volume);
    volumeOnStore.processEvent(Event.CreateOnlyRequested);
    try {
        CreateVolumeContext<VolumeApiResult> context = new CreateVolumeContext<VolumeApiResult>(null, volumeOnStore, future);
        AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().createVolumeCallback(null, null)).setContext(context);
        dataStore.getDriver().createAsync(dataStore, volumeOnStore, caller);
    } catch (CloudRuntimeException ex) {
        // clean up already persisted volume_store_ref entry in case of createVolumeCallback is never called
        VolumeDataStoreVO volStoreVO = _volumeStoreDao.findByStoreVolume(dataStore.getId(), volume.getId());
        if (volStoreVO != null) {
            VolumeInfo volObj = volFactory.getVolume(volume, dataStore);
            volObj.processEvent(ObjectInDataStoreStateMachine.Event.OperationFailed);
        }
        VolumeApiResult volResult = new VolumeApiResult((VolumeObject) volumeOnStore);
        volResult.setResult(ex.getMessage());
        future.complete(volResult);
    }
    return future;
}
Also used : VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)

Example 20 with AsyncCallFuture

use of org.apache.cloudstack.framework.async.AsyncCallFuture in project cloudstack by apache.

the class VolumeServiceImpl method createManagedTemplateVolume.

/**
     * Creates a template volume on managed storage, which will be used for creating ROOT volumes by cloning.
     *
     * @param srcTemplateInfo Source template on secondary storage
     * @param destPrimaryDataStore Managed storage on which we need to create the volume
     */
private TemplateInfo createManagedTemplateVolume(TemplateInfo srcTemplateInfo, PrimaryDataStore destPrimaryDataStore) {
    // create a template volume on primary storage
    AsyncCallFuture<VolumeApiResult> createTemplateFuture = new AsyncCallFuture<>();
    TemplateInfo templateOnPrimary = (TemplateInfo) destPrimaryDataStore.create(srcTemplateInfo);
    VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.findByPoolTemplate(destPrimaryDataStore.getId(), templateOnPrimary.getId());
    if (templatePoolRef == null) {
        throw new CloudRuntimeException("Failed to find template " + srcTemplateInfo.getUniqueName() + " in storage pool " + destPrimaryDataStore.getId());
    }
    // At this point, we have an entry in the DB that points to our cached template.
    // We need to lock it as there may be other VMs that may get started using the same template.
    // We want to avoid having to create multiple cache copies of the same template.
    int storagePoolMaxWaitSeconds = NumbersUtil.parseInt(configDao.getValue(Config.StoragePoolMaxWaitSeconds.key()), 3600);
    long templatePoolRefId = templatePoolRef.getId();
    templatePoolRef = _tmpltPoolDao.acquireInLockTable(templatePoolRefId, storagePoolMaxWaitSeconds);
    if (templatePoolRef == null) {
        throw new CloudRuntimeException("Unable to acquire lock on VMTemplateStoragePool: " + templatePoolRefId);
    }
    // Template already exists
    if (templatePoolRef.getState() == ObjectInDataStoreStateMachine.State.Ready) {
        _tmpltPoolDao.releaseFromLockTable(templatePoolRefId);
        return templateOnPrimary;
    }
    try {
        // create a cache volume on the back-end
        templateOnPrimary.processEvent(Event.CreateOnlyRequested);
        CreateVolumeContext<CreateCmdResult> createContext = new CreateVolumeContext<>(null, templateOnPrimary, createTemplateFuture);
        AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> createCaller = AsyncCallbackDispatcher.create(this);
        createCaller.setCallback(createCaller.getTarget().createManagedTemplateImageCallback(null, null)).setContext(createContext);
        destPrimaryDataStore.getDriver().createAsync(destPrimaryDataStore, templateOnPrimary, createCaller);
        VolumeApiResult result = createTemplateFuture.get();
        if (result.isFailed()) {
            String errMesg = result.getResult();
            throw new CloudRuntimeException("Unable to create template " + templateOnPrimary.getId() + " on primary storage " + destPrimaryDataStore.getId() + ":" + errMesg);
        }
    } catch (Throwable e) {
        s_logger.debug("Failed to create template volume on storage", e);
        templateOnPrimary.processEvent(Event.OperationFailed);
        throw new CloudRuntimeException(e.getMessage());
    } finally {
        _tmpltPoolDao.releaseFromLockTable(templatePoolRefId);
    }
    return templateOnPrimary;
}
Also used : CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(org.apache.cloudstack.storage.RemoteHostEndPoint) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Aggregations

AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)28 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)19 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)18 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)9 CommandResult (org.apache.cloudstack.storage.command.CommandResult)9 ExecutionException (java.util.concurrent.ExecutionException)8 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)7 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)7 CreateCmdResult (org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult)7 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)7 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)7 DataObject (org.apache.cloudstack.engine.subsystem.api.storage.DataObject)6 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)6 SnapshotResult (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult)5 TemplateObject (org.apache.cloudstack.storage.image.store.TemplateObject)4 HashMap (java.util.HashMap)3 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)3 TemplateApiResult (org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult)3 VolumeDataStoreVO (org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)3 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)2