Search in sources :

Example 1 with AsyncCallFuture

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

the class SecondaryStorageServiceImpl method migrateData.

@Override
public AsyncCallFuture<DataObjectResult> migrateData(DataObject srcDataObject, DataStore srcDatastore, DataStore destDatastore, Map<DataObject, Pair<List<SnapshotInfo>, Long>> snapshotChain, Map<DataObject, Pair<List<TemplateInfo>, Long>> templateChain) {
    AsyncCallFuture<DataObjectResult> future = new AsyncCallFuture<DataObjectResult>();
    DataObjectResult res = new DataObjectResult(srcDataObject);
    DataObject destDataObject = null;
    try {
        if (srcDataObject instanceof SnapshotInfo && snapshotChain != null && snapshotChain.containsKey(srcDataObject)) {
            List<String> parentSnapshotPaths = new ArrayList<>();
            for (SnapshotInfo snapshotInfo : snapshotChain.get(srcDataObject).first()) {
                destDataObject = null;
                if (!parentSnapshotPaths.isEmpty() && parentSnapshotPaths.contains(snapshotInfo.getPath())) {
                    parentSnapshotPaths.add(snapshotInfo.getPath());
                    SnapshotDataStoreVO snapshotStore = snapshotStoreDao.findByStoreSnapshot(DataStoreRole.Image, srcDatastore.getId(), snapshotInfo.getSnapshotId());
                    if (snapshotStore == null) {
                        res.setResult("Failed to find snapshot " + snapshotInfo.getUuid() + " on store: " + srcDatastore.getName());
                        res.setSuccess(false);
                        future.complete(res);
                        break;
                    }
                    snapshotStore.setDataStoreId(destDatastore.getId());
                    snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
                    continue;
                }
                parentSnapshotPaths.add(snapshotInfo.getPath());
                destDataObject = destDatastore.create(snapshotInfo);
                snapshotInfo.processEvent(ObjectInDataStoreStateMachine.Event.MigrateDataRequested);
                destDataObject.processEvent(ObjectInDataStoreStateMachine.Event.MigrateDataRequested);
                migrateJob(future, snapshotInfo, destDataObject, destDatastore);
                if (future.get() != null && future.get().isFailed()) {
                    break;
                }
            }
        } else if (srcDataObject instanceof TemplateInfo && templateChain != null && templateChain.containsKey(srcDataObject)) {
            for (TemplateInfo templateInfo : templateChain.get(srcDataObject).first()) {
                destDataObject = destDatastore.create(templateInfo);
                templateInfo.processEvent(ObjectInDataStoreStateMachine.Event.MigrateDataRequested);
                destDataObject.processEvent(ObjectInDataStoreStateMachine.Event.MigrateDataRequested);
                migrateJob(future, templateInfo, destDataObject, destDatastore);
            }
        } else {
            // Check if template in destination store, if yes, do not proceed
            if (srcDataObject instanceof TemplateInfo) {
                s_logger.debug("Checking if template present at destination");
                TemplateDataStoreVO templateStoreVO = templateStoreDao.findByStoreTemplate(destDatastore.getId(), srcDataObject.getId());
                if (templateStoreVO != null) {
                    String msg = "Template already exists in destination store";
                    s_logger.debug(msg);
                    res.setResult(msg);
                    res.setSuccess(true);
                    future.complete(res);
                    return future;
                }
            }
            destDataObject = destDatastore.create(srcDataObject);
            srcDataObject.processEvent(ObjectInDataStoreStateMachine.Event.MigrateDataRequested);
            destDataObject.processEvent(ObjectInDataStoreStateMachine.Event.MigrateDataRequested);
            migrateJob(future, srcDataObject, destDataObject, destDatastore);
        }
    } catch (Exception e) {
        s_logger.debug("Failed to copy Data", e);
        if (destDataObject != null) {
            s_logger.info("Deleting data on destination store: " + destDataObject.getDataStore().getName());
            destDataObject.getDataStore().delete(destDataObject);
        }
        if (!(srcDataObject instanceof VolumeInfo)) {
            srcDataObject.processEvent(ObjectInDataStoreStateMachine.Event.OperationFailed);
        } else {
            ((VolumeInfo) srcDataObject).processEventOnly(ObjectInDataStoreStateMachine.Event.OperationFailed);
        }
        res.setResult(e.toString());
        future.complete(res);
    }
    return future;
}
Also used : ArrayList(java.util.ArrayList) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) ExecutionException(java.util.concurrent.ExecutionException) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject)

Example 2 with AsyncCallFuture

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

the class TemplateServiceImpl method deleteTemplateOnPrimary.

@Override
public AsyncCallFuture<TemplateApiResult> deleteTemplateOnPrimary(TemplateInfo template, StoragePool pool) {
    TemplateObject templateObject = (TemplateObject) _templateFactory.getTemplateOnPrimaryStorage(template.getId(), (DataStore) pool, template.getDeployAsIsConfiguration());
    templateObject.processEvent(ObjectInDataStoreStateMachine.Event.DestroyRequested);
    DataStore dataStore = _storeMgr.getPrimaryDataStore(pool.getId());
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<>();
    TemplateOpContext<TemplateApiResult> context = new TemplateOpContext<>(null, templateObject, future);
    AsyncCallbackDispatcher<TemplateServiceImpl, CommandResult> caller = AsyncCallbackDispatcher.create(this);
    caller.setCallback(caller.getTarget().deleteTemplateCallback(null, null)).setContext(context);
    dataStore.getDriver().deleteAsync(dataStore, templateObject, caller);
    return future;
}
Also used : AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) TemplateObject(org.apache.cloudstack.storage.image.store.TemplateObject) CommandResult(org.apache.cloudstack.storage.command.CommandResult) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 3 with AsyncCallFuture

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

the class TemplateServiceImpl method copyTemplate.

@Override
public AsyncCallFuture<TemplateApiResult> copyTemplate(TemplateInfo srcTemplate, DataStore destStore) {
    // Need to understand what is the background to use two different urls for copy and extract.
    if (srcTemplate.getFormat() == ImageFormat.OVA) {
        ImageStoreEntity tmpltStore = (ImageStoreEntity) srcTemplate.getDataStore();
        tmpltStore.createEntityExtractUrl(srcTemplate.getInstallPath(), srcTemplate.getFormat(), srcTemplate);
    }
    // generate a URL from source template ssvm to download to destination data store
    String url = generateCopyUrl(srcTemplate);
    if (url == null) {
        s_logger.warn("Unable to start/resume copy of template " + srcTemplate.getUniqueName() + " to " + destStore.getName() + ", no secondary storage vm in running state in source zone");
        throw new CloudRuntimeException("No secondary VM in running state in source template zone ");
    }
    TemplateObject tmplForCopy = (TemplateObject) _templateFactory.getTemplate(srcTemplate, destStore, null);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Setting source template url to " + url);
    }
    tmplForCopy.setUrl(url);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Mark template_store_ref entry as Creating");
    }
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>();
    DataObject templateOnStore = destStore.create(tmplForCopy);
    templateOnStore.processEvent(Event.CreateOnlyRequested);
    if (templateOnStore instanceof TemplateObject) {
        ((TemplateObject) templateOnStore).getImage().setChecksum(null);
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Invoke datastore driver createAsync to create template on destination store");
    }
    try {
        TemplateOpContext<TemplateApiResult> context = new TemplateOpContext<TemplateApiResult>(null, (TemplateObject) templateOnStore, future);
        AsyncCallbackDispatcher<TemplateServiceImpl, CreateCmdResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().copyTemplateCrossZoneCallBack(null, null)).setContext(context);
        destStore.getDriver().createAsync(destStore, templateOnStore, caller);
    } catch (CloudRuntimeException ex) {
        // clean up already persisted template_store_ref entry in case of createTemplateCallback is never called
        TemplateDataStoreVO templateStoreVO = _vmTemplateStoreDao.findByStoreTemplate(destStore.getId(), srcTemplate.getId());
        if (templateStoreVO != null) {
            TemplateInfo tmplObj = _templateFactory.getTemplate(srcTemplate, destStore, null);
            tmplObj.processEvent(ObjectInDataStoreStateMachine.Event.OperationFailed);
        }
        TemplateApiResult res = new TemplateApiResult((TemplateObject) templateOnStore);
        res.setResult(ex.getMessage());
        future.complete(res);
    }
    return future;
}
Also used : CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) TemplateObject(org.apache.cloudstack.storage.image.store.TemplateObject) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ImageStoreEntity(org.apache.cloudstack.storage.image.datastore.ImageStoreEntity)

Example 4 with AsyncCallFuture

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

the class TemplateServiceImpl method deleteTemplateAsync.

@Override
public AsyncCallFuture<TemplateApiResult> deleteTemplateAsync(TemplateInfo template) {
    TemplateObject to = (TemplateObject) template;
    // update template_store_ref status
    to.processEvent(ObjectInDataStoreStateMachine.Event.DestroyRequested);
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>();
    TemplateOpContext<TemplateApiResult> context = new TemplateOpContext<TemplateApiResult>(null, to, future);
    AsyncCallbackDispatcher<TemplateServiceImpl, CommandResult> caller = AsyncCallbackDispatcher.create(this);
    caller.setCallback(caller.getTarget().deleteTemplateCallback(null, null)).setContext(context);
    if (to.canBeDeletedFromDataStore()) {
        to.getDataStore().getDriver().deleteAsync(to.getDataStore(), to, caller);
    } else {
        CommandResult result = new CommandResult();
        caller.complete(result);
    }
    return future;
}
Also used : AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateObject(org.apache.cloudstack.storage.image.store.TemplateObject) CommandResult(org.apache.cloudstack.storage.command.CommandResult) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 5 with AsyncCallFuture

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

the class TemplateServiceImpl method copyAsync.

private AsyncCallFuture<TemplateApiResult> copyAsync(DataObject source, TemplateInfo template, DataStore store) {
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>();
    DataObject templateOnStore = store.create(template);
    templateOnStore.processEvent(Event.CreateOnlyRequested);
    TemplateOpContext<TemplateApiResult> context = new TemplateOpContext<TemplateApiResult>(null, (TemplateObject) templateOnStore, future);
    AsyncCallbackDispatcher<TemplateServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
    caller.setCallback(caller.getTarget().copyTemplateCallBack(null, null)).setContext(context);
    _motionSrv.copyAsync(source, templateOnStore, caller);
    return future;
}
Also used : AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Aggregations

AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)32 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)20 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)20 ExecutionException (java.util.concurrent.ExecutionException)18 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)11 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)10 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)9 StorageAccessException (com.cloud.exception.StorageAccessException)9 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)9 CommandResult (org.apache.cloudstack.storage.command.CommandResult)9 CreateCmdResult (org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult)8 DataObject (org.apache.cloudstack.engine.subsystem.api.storage.DataObject)8 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)5 SnapshotResult (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult)5 TemplateObject (org.apache.cloudstack.storage.image.store.TemplateObject)4 VolumeVO (com.cloud.storage.VolumeVO)3 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)3