use of org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult 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;
}
use of org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult in project cloudstack by apache.
the class TemplateServiceImpl method createTemplateAsync.
@Override
public void createTemplateAsync(TemplateInfo template, DataStore store, AsyncCompletionCallback<TemplateApiResult> callback) {
// persist template_store_ref entry
TemplateObject templateOnStore = (TemplateObject) store.create(template);
// update template_store_ref and template state
try {
templateOnStore.processEvent(ObjectInDataStoreStateMachine.Event.CreateOnlyRequested);
} catch (Exception e) {
TemplateApiResult result = new TemplateApiResult(templateOnStore);
result.setResult(e.toString());
result.setSuccess(false);
if (callback != null) {
callback.complete(result);
}
return;
}
try {
TemplateOpContext<TemplateApiResult> context = new TemplateOpContext<TemplateApiResult>(callback, templateOnStore, null);
AsyncCallbackDispatcher<TemplateServiceImpl, CreateCmdResult> caller = AsyncCallbackDispatcher.create(this);
caller.setCallback(caller.getTarget().createTemplateCallback(null, null)).setContext(context);
store.getDriver().createAsync(store, templateOnStore, caller);
} catch (CloudRuntimeException ex) {
// clean up already persisted template_store_ref entry in case of createTemplateCallback is never called
TemplateDataStoreVO templateStoreVO = _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId());
if (templateStoreVO != null) {
TemplateInfo tmplObj = _templateFactory.getTemplate(template, store);
tmplObj.processEvent(ObjectInDataStoreStateMachine.Event.OperationFailed);
}
TemplateApiResult result = new TemplateApiResult(template);
result.setResult(ex.getMessage());
if (callback != null) {
callback.complete(result);
}
}
}
use of org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult in project cloudstack by apache.
the class DataObjectManagerImpl method deleteAsync.
@Override
public void deleteAsync(DataObject data, AsyncCompletionCallback<CommandResult> callback) {
try {
objectInDataStoreMgr.update(data, Event.DestroyRequested);
} catch (NoTransitionException e) {
s_logger.debug("destroy failed", e);
CreateCmdResult res = new CreateCmdResult(null, null);
callback.complete(res);
} catch (ConcurrentOperationException e) {
s_logger.debug("destroy failed", e);
CreateCmdResult res = new CreateCmdResult(null, null);
callback.complete(res);
}
DeleteContext<CommandResult> context = new DeleteContext<CommandResult>(callback, data);
AsyncCallbackDispatcher<DataObjectManagerImpl, CommandResult> caller = AsyncCallbackDispatcher.create(this);
caller.setCallback(caller.getTarget().deleteAsynCallback(null, null)).setContext(context);
data.getDataStore().getDriver().deleteAsync(data.getDataStore(), data, caller);
return;
}
use of org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult in project cloudstack by apache.
the class DataObjectManagerImpl method createAsynCallback.
protected Void createAsynCallback(AsyncCallbackDispatcher<DataObjectManagerImpl, CreateCmdResult> callback, CreateContext<CreateCmdResult> context) {
CreateCmdResult result = callback.getResult();
DataObject objInStrore = context.objInStrore;
CreateCmdResult upResult = new CreateCmdResult(null, null);
if (result.isFailed()) {
upResult.setResult(result.getResult());
context.getParentCallback().complete(upResult);
return null;
}
try {
objectInDataStoreMgr.update(objInStrore, ObjectInDataStoreStateMachine.Event.OperationSuccessed);
} catch (NoTransitionException e) {
try {
objectInDataStoreMgr.update(objInStrore, ObjectInDataStoreStateMachine.Event.OperationFailed);
} catch (Exception e1) {
s_logger.debug("failed to change state", e1);
}
upResult.setResult(e.toString());
context.getParentCallback().complete(upResult);
return null;
} catch (ConcurrentOperationException e) {
try {
objectInDataStoreMgr.update(objInStrore, ObjectInDataStoreStateMachine.Event.OperationFailed);
} catch (Exception e1) {
s_logger.debug("failed to change state", e1);
}
upResult.setResult(e.toString());
context.getParentCallback().complete(upResult);
return null;
}
context.getParentCallback().complete(result);
return null;
}
use of org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult in project cloudstack by apache.
the class DataObjectManagerImpl method copyCallback.
protected Void copyCallback(AsyncCallbackDispatcher<DataObjectManagerImpl, CopyCommandResult> callback, CopyContext<CreateCmdResult> context) {
CopyCommandResult result = callback.getResult();
DataObject destObj = context.destObj;
if (result.isFailed()) {
try {
objectInDataStoreMgr.update(destObj, Event.OperationFailed);
} catch (NoTransitionException e) {
s_logger.debug("Failed to update copying state", e);
} catch (ConcurrentOperationException e) {
s_logger.debug("Failed to update copying state", e);
}
CreateCmdResult res = new CreateCmdResult(null, null);
res.setResult(result.getResult());
context.getParentCallback().complete(res);
}
try {
objectInDataStoreMgr.update(destObj, ObjectInDataStoreStateMachine.Event.OperationSuccessed);
} catch (NoTransitionException e) {
s_logger.debug("Failed to update copying state: ", e);
try {
objectInDataStoreMgr.update(destObj, ObjectInDataStoreStateMachine.Event.OperationFailed);
} catch (Exception e1) {
s_logger.debug("failed to further change state to OperationFailed", e1);
}
CreateCmdResult res = new CreateCmdResult(null, null);
res.setResult("Failed to update copying state: " + e.toString());
context.getParentCallback().complete(res);
} catch (ConcurrentOperationException e) {
s_logger.debug("Failed to update copying state: ", e);
try {
objectInDataStoreMgr.update(destObj, ObjectInDataStoreStateMachine.Event.OperationFailed);
} catch (Exception e1) {
s_logger.debug("failed to further change state to OperationFailed", e1);
}
CreateCmdResult res = new CreateCmdResult(null, null);
res.setResult("Failed to update copying state: " + e.toString());
context.getParentCallback().complete(res);
}
CreateCmdResult res = new CreateCmdResult(result.getPath(), null);
context.getParentCallback().complete(res);
return null;
}
Aggregations