Search in sources :

Example 11 with AsyncCallFuture

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

the class VolumeServiceImpl method migrateVolumes.

@Override
public AsyncCallFuture<CommandResult> migrateVolumes(Map<VolumeInfo, DataStore> volumeMap, VirtualMachineTO vmTo, Host srcHost, Host destHost) {
    AsyncCallFuture<CommandResult> future = new AsyncCallFuture<CommandResult>();
    CommandResult res = new CommandResult();
    try {
        // Check to make sure there are no snapshot operations on a volume
        // and
        // put it in the migrating state.
        List<VolumeInfo> volumesMigrating = new ArrayList<VolumeInfo>();
        for (Map.Entry<VolumeInfo, DataStore> entry : volumeMap.entrySet()) {
            VolumeInfo volume = entry.getKey();
            if (!snapshotMgr.canOperateOnVolume(volume)) {
                s_logger.debug("Snapshots are being created on a volume. Volumes cannot be migrated now.");
                res.setResult("Snapshots are being created on a volume. Volumes cannot be migrated now.");
                future.complete(res);
                // to be put back in ready state.
                for (VolumeInfo volumeMigrating : volumesMigrating) {
                    volumeMigrating.processEvent(Event.OperationFailed);
                }
                return future;
            } else {
                volume.processEvent(Event.MigrationRequested);
                volumesMigrating.add(volume);
            }
        }
        MigrateVmWithVolumesContext<CommandResult> context = new MigrateVmWithVolumesContext<CommandResult>(null, future, volumeMap);
        AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().migrateVmWithVolumesCallBack(null, null)).setContext(context);
        motionSrv.copyAsync(volumeMap, vmTo, srcHost, destHost, caller);
    } catch (Exception e) {
        s_logger.debug("Failed to copy volume", e);
        res.setResult(e.toString());
        future.complete(res);
    }
    return future;
}
Also used : ArrayList(java.util.ArrayList) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CommandResult(org.apache.cloudstack.storage.command.CommandResult) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) Map(java.util.Map) HashMap(java.util.HashMap) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 12 with AsyncCallFuture

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

the class VolumeServiceImpl method copyVolumeFromImageToPrimary.

protected AsyncCallFuture<VolumeApiResult> copyVolumeFromImageToPrimary(VolumeInfo srcVolume, DataStore destStore) {
    AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
    VolumeApiResult res = new VolumeApiResult(srcVolume);
    VolumeInfo destVolume = null;
    try {
        destVolume = (VolumeInfo) destStore.create(srcVolume);
        destVolume.processEvent(Event.CopyingRequested);
        srcVolume.processEvent(Event.CopyingRequested);
        CopyVolumeContext<VolumeApiResult> context = new CopyVolumeContext<VolumeApiResult>(null, future, srcVolume, destVolume, destStore);
        AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().copyVolumeFromImageToPrimaryCallback(null, null)).setContext(context);
        motionSrv.copyAsync(srcVolume, destVolume, caller);
        return future;
    } catch (Exception e) {
        s_logger.error("failed to copy volume from image store", e);
        if (destVolume != null) {
            destVolume.processEvent(Event.OperationFailed);
        }
        srcVolume.processEvent(Event.OperationFailed);
        res.setResult(e.toString());
        future.complete(res);
        return future;
    }
}
Also used : AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) 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 13 with AsyncCallFuture

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

the class VolumeServiceImpl method copyTemplateToManagedTemplateVolume.

/**
     * This function copies a template from secondary storage to a template volume
     * created on managed storage. This template volume will be used as a cache.
     * Instead of copying the template to a ROOT volume every time, a clone is performed instead.
     *
     * @param srcTemplateInfo Source from which to copy the template
     * @param templateOnPrimary Dest to copy to
     * @param templatePoolRef Template reference on primary storage (entry in the template_spool_ref)
     * @param destPrimaryDataStore The managed primary storage
     * @param destHost The host that we will use for the copy
     */
private void copyTemplateToManagedTemplateVolume(TemplateInfo srcTemplateInfo, TemplateInfo templateOnPrimary, VMTemplateStoragePoolVO templatePoolRef, PrimaryDataStore destPrimaryDataStore, Host destHost) {
    AsyncCallFuture<VolumeApiResult> copyTemplateFuture = new AsyncCallFuture<>();
    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);
    }
    if (templatePoolRef.getDownloadState() == Status.DOWNLOADED) {
        // There can be cases where we acquired the lock, but the template
        // was already copied by a previous thread. Just return in that case.
        s_logger.debug("Template already downloaded, nothing to do");
        return;
    }
    try {
        // copy the template from sec storage to the created volume
        CreateBaseImageContext<CreateCmdResult> copyContext = new CreateBaseImageContext<>(null, null, destPrimaryDataStore, srcTemplateInfo, copyTemplateFuture, templateOnPrimary, templatePoolRefId);
        AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> copyCaller = AsyncCallbackDispatcher.create(this);
        copyCaller.setCallback(copyCaller.getTarget().copyManagedTemplateCallback(null, null)).setContext(copyContext);
        // Populate details which will be later read by the storage subsystem.
        Map<String, String> details = new HashMap<String, String>();
        details.put(PrimaryDataStore.MANAGED, Boolean.TRUE.toString());
        details.put(PrimaryDataStore.STORAGE_HOST, destPrimaryDataStore.getHostAddress());
        details.put(PrimaryDataStore.STORAGE_PORT, String.valueOf(destPrimaryDataStore.getPort()));
        details.put(PrimaryDataStore.MANAGED_STORE_TARGET, ((TemplateObject) templateOnPrimary).getInstallPath());
        details.put(PrimaryDataStore.MANAGED_STORE_TARGET_ROOT_VOLUME, srcTemplateInfo.getUniqueName());
        details.put(PrimaryDataStore.REMOVE_AFTER_COPY, Boolean.TRUE.toString());
        details.put(PrimaryDataStore.VOLUME_SIZE, String.valueOf(templateOnPrimary.getSize()));
        ChapInfo chapInfo = getChapInfo(templateOnPrimary, destPrimaryDataStore);
        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());
        }
        templateOnPrimary.processEvent(Event.CopyingRequested);
        destPrimaryDataStore.setDetails(details);
        grantAccess(templateOnPrimary, destHost, destPrimaryDataStore);
        VolumeApiResult result = null;
        try {
            motionSrv.copyAsync(srcTemplateInfo, templateOnPrimary, destHost, copyCaller);
            result = copyTemplateFuture.get();
        } finally {
            revokeAccess(templateOnPrimary, destHost, destPrimaryDataStore);
        }
        if (result.isFailed()) {
            throw new CloudRuntimeException("Failed to copy template " + templateOnPrimary.getId() + " to primary storage " + destPrimaryDataStore.getId() + ": " + result.getResult());
        // XXX: I find it is useful to destroy the volume on primary storage instead of another thread trying the copy again because I've seen
        // something weird happens to the volume (XenServer creates an SR, but the VDI copy can fail).
        // For now, I just retry the copy.
        }
    } catch (Throwable e) {
        s_logger.debug("Failed to create a template on primary storage", e);
        templateOnPrimary.processEvent(Event.OperationFailed);
        throw new CloudRuntimeException(e.getMessage());
    } finally {
        _tmpltPoolDao.releaseFromLockTable(templatePoolRefId);
    }
}
Also used : HashMap(java.util.HashMap) ChapInfo(org.apache.cloudstack.engine.subsystem.api.storage.ChapInfo) CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(org.apache.cloudstack.storage.RemoteHostEndPoint) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 14 with AsyncCallFuture

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

the class VolumeServiceImpl method migrateVolume.

@Override
public AsyncCallFuture<VolumeApiResult> migrateVolume(VolumeInfo srcVolume, DataStore destStore) {
    AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
    VolumeApiResult res = new VolumeApiResult(srcVolume);
    try {
        if (!snapshotMgr.canOperateOnVolume(srcVolume)) {
            s_logger.debug("Snapshots are being created on this volume. This volume cannot be migrated now.");
            res.setResult("Snapshots are being created on this volume. This volume cannot be migrated now.");
            future.complete(res);
            return future;
        }
        VolumeInfo destVolume = volFactory.getVolume(srcVolume.getId(), destStore);
        srcVolume.processEvent(Event.MigrationRequested);
        MigrateVolumeContext<VolumeApiResult> context = new MigrateVolumeContext<VolumeApiResult>(null, future, srcVolume, destVolume, destStore);
        AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().migrateVolumeCallBack(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) 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 15 with AsyncCallFuture

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

the class VolumeServiceImpl method registerVolume.

@Override
public AsyncCallFuture<VolumeApiResult> registerVolume(VolumeInfo volume, DataStore store) {
    AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
    DataObject volumeOnStore = store.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().registerVolumeCallback(null, null));
        caller.setContext(context);
        store.getDriver().createAsync(store, volumeOnStore, caller);
    } catch (CloudRuntimeException ex) {
        // clean up already persisted volume_store_ref entry in case of createVolumeCallback is never called
        VolumeDataStoreVO volStoreVO = _volumeStoreDao.findByStoreVolume(store.getId(), volume.getId());
        if (volStoreVO != null) {
            VolumeInfo volObj = volFactory.getVolume(volume, store);
            volObj.processEvent(ObjectInDataStoreStateMachine.Event.OperationFailed);
        }
        VolumeApiResult res = new VolumeApiResult((VolumeObject) volumeOnStore);
        res.setResult(ex.getMessage());
        future.complete(res);
    }
    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)

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