Search in sources :

Example 56 with CopyCommandResult

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

the class VolumeServiceImpl method copyVolume.

@Override
public AsyncCallFuture<VolumeApiResult> copyVolume(VolumeInfo srcVolume, DataStore destStore) {
    DataStore srcStore = srcVolume.getDataStore();
    if (s_logger.isDebugEnabled()) {
        String srcRole = (srcStore != null && srcStore.getRole() != null ? srcVolume.getDataStore().getRole().toString() : "<unknown role>");
        String msg = String.format("copying %s(id=%d, role=%s) to %s (id=%d, role=%s)", srcVolume.getName(), srcVolume.getId(), srcRole, destStore.getName(), destStore.getId(), destStore.getRole());
        s_logger.debug(msg);
    }
    if (srcVolume.getState() == Volume.State.Uploaded) {
        return copyVolumeFromImageToPrimary(srcVolume, destStore);
    }
    if (destStore.getRole() == DataStoreRole.Image) {
        return copyVolumeFromPrimaryToImage(srcVolume, destStore);
    }
    if (srcStore.getRole() == DataStoreRole.Primary && destStore.getRole() == DataStoreRole.Primary && ((PrimaryDataStore) destStore).isManaged() && requiresNewManagedVolumeInDestStore((PrimaryDataStore) srcStore, (PrimaryDataStore) destStore)) {
        return copyManagedVolume(srcVolume, destStore);
    }
    // OfflineVmwareMigration: aren't we missing secondary to secondary in this logic?
    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.error("Failed to copy volume:" + e);
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Failed to copy volume.", e);
        }
        res.setResult(e.toString());
        future.complete(res);
    }
    return future;
}
Also used : VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StorageAccessException(com.cloud.exception.StorageAccessException) ExecutionException(java.util.concurrent.ExecutionException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) VolumeVO(com.cloud.storage.VolumeVO) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Example 57 with CopyCommandResult

use of org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult 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) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StorageAccessException(com.cloud.exception.StorageAccessException) ExecutionException(java.util.concurrent.ExecutionException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException)

Example 58 with CopyCommandResult

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

the class VolumeServiceImpl method migrateVolumeCallBack.

protected Void migrateVolumeCallBack(AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback, MigrateVolumeContext<VolumeApiResult> context) {
    VolumeInfo srcVolume = context.srcVolume;
    CopyCommandResult result = callback.getResult();
    AsyncCallFuture<VolumeApiResult> future = context.future;
    VolumeApiResult res = new VolumeApiResult(srcVolume);
    try {
        if (result.isFailed()) {
            res.setResult(result.getResult());
            srcVolume.processEvent(Event.OperationFailed);
            future.complete(res);
        } else {
            srcVolume.processEvent(Event.OperationSuccessed);
            if (srcVolume.getStoragePoolType() == StoragePoolType.PowerFlex) {
                future.complete(res);
                return null;
            }
            snapshotMgr.cleanupSnapshotsByVolume(srcVolume.getId());
            future.complete(res);
        }
    } catch (Exception e) {
        s_logger.error("Failed to process migrate volume callback", e);
        res.setResult(e.toString());
        future.complete(res);
    }
    return null;
}
Also used : VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StorageAccessException(com.cloud.exception.StorageAccessException) ExecutionException(java.util.concurrent.ExecutionException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException)

Example 59 with CopyCommandResult

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

the class VolumeServiceImpl method createManagedVolumeCopyManagedTemplateAsync.

private void createManagedVolumeCopyManagedTemplateAsync(VolumeInfo volumeInfo, PrimaryDataStore destPrimaryDataStore, TemplateInfo srcTemplateOnPrimary, Host destHost, AsyncCallFuture<VolumeApiResult> future) throws StorageAccessException {
    VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.findByPoolTemplate(destPrimaryDataStore.getId(), srcTemplateOnPrimary.getId(), null);
    if (templatePoolRef == null) {
        throw new CloudRuntimeException("Failed to find template " + srcTemplateOnPrimary.getUniqueName() + " in storage pool " + srcTemplateOnPrimary.getId());
    }
    if (templatePoolRef.getDownloadState() == Status.NOT_DOWNLOADED) {
        throw new CloudRuntimeException("Template " + srcTemplateOnPrimary.getUniqueName() + " has not been downloaded to primary storage.");
    }
    String volumeDetailKey = "POOL_TEMPLATE_ID_COPY_ON_HOST_" + destHost.getId();
    try {
        try {
            grantAccess(srcTemplateOnPrimary, destHost, destPrimaryDataStore);
        } catch (Exception e) {
            throw new StorageAccessException("Unable to grant access to src template: " + srcTemplateOnPrimary.getId() + " on host: " + destHost.getId());
        }
        _volumeDetailsDao.addDetail(volumeInfo.getId(), volumeDetailKey, String.valueOf(templatePoolRef.getId()), false);
        // Create a volume on managed storage.
        AsyncCallFuture<VolumeApiResult> createVolumeFuture = createVolumeAsync(volumeInfo, destPrimaryDataStore);
        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(), destPrimaryDataStore);
        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, volumeInfo.get_iScsiName());
        details.put(PrimaryDataStore.MANAGED_STORE_TARGET_ROOT_VOLUME, volumeInfo.getName());
        details.put(PrimaryDataStore.VOLUME_SIZE, String.valueOf(volumeInfo.getSize()));
        details.put(StorageManager.STORAGE_POOL_DISK_WAIT.toString(), String.valueOf(StorageManager.STORAGE_POOL_DISK_WAIT.valueIn(destPrimaryDataStore.getId())));
        destPrimaryDataStore.setDetails(details);
        grantAccess(volumeInfo, destHost, destPrimaryDataStore);
        volumeInfo.processEvent(Event.CreateRequested);
        CreateVolumeFromBaseImageContext<VolumeApiResult> context = new CreateVolumeFromBaseImageContext<>(null, volumeInfo, destPrimaryDataStore, srcTemplateOnPrimary, future, null, null);
        AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().createVolumeFromBaseManagedImageCallBack(null, null));
        caller.setContext(context);
        try {
            motionSrv.copyAsync(srcTemplateOnPrimary, volumeInfo, destHost, caller);
        } finally {
            revokeAccess(volumeInfo, destHost, destPrimaryDataStore);
        }
    } catch (StorageAccessException e) {
        throw e;
    } catch (Throwable e) {
        s_logger.debug("Failed to copy managed template on primary storage", e);
        String errMsg = "Failed due to " + e.toString();
        try {
            destroyAndReallocateManagedVolume(volumeInfo);
        } catch (CloudRuntimeException ex) {
            s_logger.warn("Failed to destroy managed volume: " + volumeInfo.getId());
            errMsg += " : " + ex.getMessage();
        }
        VolumeApiResult result = new VolumeApiResult(volumeInfo);
        result.setResult(errMsg);
        future.complete(result);
    } finally {
        _volumeDetailsDao.removeDetail(volumeInfo.getId(), volumeDetailKey);
        List<VolumeDetailVO> volumeDetails = _volumeDetailsDao.findDetails(volumeDetailKey, String.valueOf(templatePoolRef.getId()), false);
        if (volumeDetails == null || volumeDetails.isEmpty()) {
            revokeAccess(srcTemplateOnPrimary, destHost, destPrimaryDataStore);
        }
    }
}
Also used : HashMap(java.util.HashMap) VolumeDetailVO(com.cloud.storage.VolumeDetailVO) StorageAccessException(com.cloud.exception.StorageAccessException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StorageAccessException(com.cloud.exception.StorageAccessException) ExecutionException(java.util.concurrent.ExecutionException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 60 with CopyCommandResult

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

the class VolumeServiceImpl method copyVolumeFromImageToPrimaryCallback.

protected Void copyVolumeFromImageToPrimaryCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback, CopyVolumeContext<VolumeApiResult> context) {
    VolumeInfo srcVolume = context.srcVolume;
    VolumeInfo destVolume = context.destVolume;
    CopyCommandResult result = callback.getResult();
    AsyncCallFuture<VolumeApiResult> future = context.future;
    VolumeApiResult res = new VolumeApiResult(destVolume);
    try {
        if (result.isFailed()) {
            destVolume.processEvent(Event.OperationFailed);
            srcVolume.processEvent(Event.OperationFailed);
            res.setResult(result.getResult());
            future.complete(res);
            return null;
        }
        srcVolume.processEvent(Event.OperationSuccessed);
        destVolume.processEvent(Event.OperationSuccessed, result.getAnswer());
        srcVolume.getDataStore().delete(srcVolume);
        future.complete(res);
    } catch (Exception e) {
        res.setResult(e.toString());
        future.complete(res);
    }
    return null;
}
Also used : VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StorageAccessException(com.cloud.exception.StorageAccessException) ExecutionException(java.util.concurrent.ExecutionException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException)

Aggregations

CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)68 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)48 ExecutionException (java.util.concurrent.ExecutionException)27 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)21 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)20 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)19 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)17 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)17 StorageAccessException (com.cloud.exception.StorageAccessException)17 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)16 DataObject (org.apache.cloudstack.engine.subsystem.api.storage.DataObject)14 AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)14 HostVO (com.cloud.host.HostVO)12 Answer (com.cloud.agent.api.Answer)11 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)11 HashMap (java.util.HashMap)10 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)9 MigrateWithStorageAnswer (com.cloud.agent.api.MigrateWithStorageAnswer)8 VMInstanceVO (com.cloud.vm.VMInstanceVO)8 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)8