use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.
the class BaseImageStoreDriverImpl method createTemplateAsyncCallback.
protected Void createTemplateAsyncCallback(AsyncCallbackDispatcher<? extends BaseImageStoreDriverImpl, DownloadAnswer> callback, CreateContext<CreateCmdResult> context) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Performing image store createTemplate async callback");
}
DownloadAnswer answer = callback.getResult();
DataObject obj = context.data;
DataStore store = obj.getDataStore();
TemplateDataStoreVO tmpltStoreVO = _templateStoreDao.findByStoreTemplate(store.getId(), obj.getId());
if (tmpltStoreVO != null) {
if (tmpltStoreVO.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Template is already in DOWNLOADED state, ignore further incoming DownloadAnswer");
}
return null;
}
TemplateDataStoreVO updateBuilder = _templateStoreDao.createForUpdate();
updateBuilder.setDownloadPercent(answer.getDownloadPct());
updateBuilder.setDownloadState(answer.getDownloadStatus());
updateBuilder.setLastUpdated(new Date());
updateBuilder.setErrorString(answer.getErrorString());
updateBuilder.setJobId(answer.getJobId());
updateBuilder.setLocalDownloadPath(answer.getDownloadPath());
updateBuilder.setInstallPath(answer.getInstallPath());
updateBuilder.setSize(answer.getTemplateSize());
updateBuilder.setPhysicalSize(answer.getTemplatePhySicalSize());
_templateStoreDao.update(tmpltStoreVO.getId(), updateBuilder);
// update size in vm_template table
VMTemplateVO tmlptUpdater = _templateDao.createForUpdate();
tmlptUpdater.setSize(answer.getTemplateSize());
_templateDao.update(obj.getId(), tmlptUpdater);
}
AsyncCompletionCallback<CreateCmdResult> caller = context.getParentCallback();
if (answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR || answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.ABANDONED || answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.UNKNOWN) {
CreateCmdResult result = new CreateCmdResult(null, null);
result.setSuccess(false);
result.setResult(answer.getErrorString());
caller.complete(result);
String msg = "Failed to register template: " + obj.getUuid() + " with error: " + answer.getErrorString();
_alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_UPLOAD_FAILED, _vmTemplateZoneDao.listByTemplateId(obj.getId()).get(0).getZoneId(), null, msg, msg);
s_logger.error(msg);
} else if (answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
if (answer.getCheckSum() != null) {
VMTemplateVO templateDaoBuilder = _templateDao.createForUpdate();
templateDaoBuilder.setChecksum(answer.getCheckSum());
_templateDao.update(obj.getId(), templateDaoBuilder);
}
CreateCmdResult result = new CreateCmdResult(null, null);
caller.complete(result);
}
return null;
}
use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.
the class StorageCacheReplacementAlgorithmLRUTest method testSelectObject.
@Test
public void testSelectObject() {
cacheReplacementAlgorithm.setUnusedTimeInterval(1);
try {
VMTemplateVO template = new VMTemplateVO();
template.setTemplateType(Storage.TemplateType.USER);
template.setUrl(UUID.randomUUID().toString());
template.setUniqueName(UUID.randomUUID().toString());
template.setName(UUID.randomUUID().toString());
template.setPublicTemplate(true);
template.setFeatured(true);
template.setRequiresHvm(true);
template.setBits(64);
template.setFormat(Storage.ImageFormat.VHD);
template.setEnablePassword(true);
template.setEnableSshKey(true);
template.setGuestOSId(1);
template.setBootable(true);
template.setPrepopulate(true);
template.setCrossZones(true);
template.setExtractable(true);
template = templateDao.persist(template);
VMTemplateVO template2 = new VMTemplateVO();
template2.setTemplateType(Storage.TemplateType.USER);
template2.setUrl(UUID.randomUUID().toString());
template2.setUniqueName(UUID.randomUUID().toString());
template2.setName(UUID.randomUUID().toString());
template2.setPublicTemplate(true);
template2.setFeatured(true);
template2.setRequiresHvm(true);
template2.setBits(64);
template2.setFormat(Storage.ImageFormat.VHD);
template2.setEnablePassword(true);
template2.setEnableSshKey(true);
template2.setGuestOSId(1);
template2.setBootable(true);
template2.setPrepopulate(true);
template2.setCrossZones(true);
template2.setExtractable(true);
template2 = templateDao.persist(template2);
ImageStoreVO imageStoreVO = new ImageStoreVO();
imageStoreVO.setRole(DataStoreRole.ImageCache);
imageStoreVO.setName(UUID.randomUUID().toString());
imageStoreVO.setProviderName(DataStoreProvider.NFS_IMAGE);
imageStoreVO.setProtocol("nfs");
imageStoreVO.setUrl(UUID.randomUUID().toString());
imageStoreVO = imageStoreDao.persist(imageStoreVO);
Calendar cal = Calendar.getInstance();
cal.setTime(DateUtil.now());
cal.add(Calendar.DAY_OF_MONTH, -2);
Date date = cal.getTime();
TemplateDataStoreVO templateStoreVO1 = new TemplateDataStoreVO();
templateStoreVO1.setLastUpdated(date);
templateStoreVO1.setDataStoreRole(DataStoreRole.ImageCache);
templateStoreVO1.setDataStoreId(imageStoreVO.getId());
templateStoreVO1.setState(ObjectInDataStoreStateMachine.State.Ready);
templateStoreVO1.setCopy(true);
templateStoreVO1.setTemplateId(template.getId());
templateDataStoreDao.persist(templateStoreVO1);
TemplateDataStoreVO templateStoreVO2 = new TemplateDataStoreVO();
templateStoreVO2.setLastUpdated(date);
templateStoreVO2.setDataStoreRole(DataStoreRole.ImageCache);
templateStoreVO2.setDataStoreId(imageStoreVO.getId());
templateStoreVO2.setState(ObjectInDataStoreStateMachine.State.Ready);
templateStoreVO2.setCopy(true);
templateStoreVO2.setTemplateId(template2.getId());
templateDataStoreDao.persist(templateStoreVO2);
DataStore store = dataStoreManager.getDataStore(imageStoreVO.getId(), DataStoreRole.ImageCache);
Assert.assertNotNull(cacheReplacementAlgorithm.chooseOneToBeReplaced(store));
} catch (Exception e) {
Assert.fail();
}
}
use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.
the class TemplateObject method processEvent.
@Override
public void processEvent(ObjectInDataStoreStateMachine.Event event, Answer answer) {
try {
if (getDataStore().getRole() == DataStoreRole.Primary) {
if (answer instanceof CopyCmdAnswer) {
CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
TemplateObjectTO newTemplate = (TemplateObjectTO) cpyAnswer.getNewData();
VMTemplateStoragePoolVO templatePoolRef = templatePoolDao.findByPoolTemplate(getDataStore().getId(), getId());
templatePoolRef.setDownloadPercent(100);
setTemplateSizeIfNeeded(newTemplate, templatePoolRef);
templatePoolRef.setDownloadState(Status.DOWNLOADED);
setDownloadPathIfNeeded(newTemplate, templatePoolRef);
setInstallPathIfNeeded(newTemplate, templatePoolRef);
templatePoolDao.update(templatePoolRef.getId(), templatePoolRef);
}
} else if (getDataStore().getRole() == DataStoreRole.Image || getDataStore().getRole() == DataStoreRole.ImageCache) {
if (answer instanceof CopyCmdAnswer) {
CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
TemplateObjectTO newTemplate = (TemplateObjectTO) cpyAnswer.getNewData();
TemplateDataStoreVO templateStoreRef = templateStoreDao.findByStoreTemplate(getDataStore().getId(), getId());
templateStoreRef.setInstallPath(newTemplate.getPath());
templateStoreRef.setDownloadPercent(100);
templateStoreRef.setDownloadState(Status.DOWNLOADED);
templateStoreRef.setSize(newTemplate.getSize());
if (newTemplate.getPhysicalSize() != null) {
templateStoreRef.setPhysicalSize(newTemplate.getPhysicalSize());
}
templateStoreDao.update(templateStoreRef.getId(), templateStoreRef);
if (getDataStore().getRole() == DataStoreRole.Image) {
VMTemplateVO templateVO = imageDao.findById(getId());
if (newTemplate.getFormat() != null) {
templateVO.setFormat(newTemplate.getFormat());
}
if (newTemplate.getName() != null) {
// For template created from snapshot, template name is determine by resource code.
templateVO.setUniqueName(newTemplate.getName());
}
if (newTemplate.getHypervisorType() != null) {
templateVO.setHypervisorType(newTemplate.getHypervisorType());
}
templateVO.setSize(newTemplate.getSize());
imageDao.update(templateVO.getId(), templateVO);
}
}
}
objectInStoreMgr.update(this, event);
} catch (NoTransitionException e) {
s_logger.debug("failed to update state", e);
throw new CloudRuntimeException("Failed to update state" + e.toString());
} catch (Exception ex) {
s_logger.debug("failed to process event and answer", ex);
objectInStoreMgr.delete(this);
throw new CloudRuntimeException("Failed to process event", ex);
} finally {
// in case of OperationFailed, expunge the entry
if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
objectInStoreMgr.deleteIfNotReady(this);
}
}
}
use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.
the class TemplateObject method decRefCount.
@Override
public void decRefCount() {
if (dataStore == null) {
return;
}
if (dataStore.getRole() == DataStoreRole.Image || dataStore.getRole() == DataStoreRole.ImageCache) {
TemplateDataStoreVO store = templateStoreDao.findByStoreTemplate(dataStore.getId(), getId());
store.decrRefCnt();
store.setLastUpdated(new Date());
templateStoreDao.update(store.getId(), store);
}
}
use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.
the class TemplateDataFactoryImpl method listTemplateOnCache.
@Override
public List<TemplateInfo> listTemplateOnCache(long templateId) {
List<TemplateDataStoreVO> cacheTmpls = templateStoreDao.listOnCache(templateId);
List<TemplateInfo> tmplObjs = new ArrayList<TemplateInfo>();
for (TemplateDataStoreVO cacheTmpl : cacheTmpls) {
long storeId = cacheTmpl.getDataStoreId();
DataStore store = storeMgr.getDataStore(storeId, DataStoreRole.ImageCache);
TemplateInfo tmplObj = getTemplate(templateId, store);
tmplObjs.add(tmplObj);
}
return tmplObjs;
}
Aggregations