use of com.cloud.engine.subsystem.api.storage.DataObject in project cosmic by MissionCriticalCloud.
the class VolumeServiceImpl method createBaseImageAsync.
@DB
protected void createBaseImageAsync(final VolumeInfo volume, final PrimaryDataStore dataStore, final TemplateInfo template, final AsyncCallFuture<VolumeApiResult> future) {
final DataObject templateOnPrimaryStoreObj = dataStore.create(template);
VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.findByPoolTemplate(dataStore.getId(), template.getId());
if (templatePoolRef == null) {
throw new CloudRuntimeException("Failed to find template " + template.getUniqueName() + " in storage pool " + dataStore.getId());
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Found template " + template.getUniqueName() + " in storage pool " + dataStore.getId() + " with VMTemplateStoragePool id: " + templatePoolRef.getId());
}
}
final long templatePoolRefId = templatePoolRef.getId();
final CreateBaseImageContext<CreateCmdResult> context = new CreateBaseImageContext<>(null, volume, dataStore, template, future, templateOnPrimaryStoreObj, templatePoolRefId);
final AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
caller.setCallback(caller.getTarget().copyBaseImageCallback(null, null)).setContext(context);
final int storagePoolMaxWaitSeconds = NumbersUtil.parseInt(configDao.getValue(Config.StoragePoolMaxWaitSeconds.key()), 3600);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Acquire lock on VMTemplateStoragePool " + templatePoolRefId + " with timeout " + storagePoolMaxWaitSeconds + " seconds");
}
templatePoolRef = _tmpltPoolDao.acquireInLockTable(templatePoolRefId, storagePoolMaxWaitSeconds);
if (templatePoolRef == null) {
if (s_logger.isDebugEnabled()) {
s_logger.info("Unable to acquire lock on VMTemplateStoragePool " + templatePoolRefId);
}
templatePoolRef = _tmpltPoolDao.findByPoolTemplate(dataStore.getId(), template.getId());
if (templatePoolRef != null && templatePoolRef.getState() == ObjectInDataStoreStateMachine.State.Ready) {
s_logger.info("Unable to acquire lock on VMTemplateStoragePool " + templatePoolRefId + ", But Template " + template.getUniqueName() + " is already copied to primary storage, skip copying");
createVolumeFromBaseImageAsync(volume, templateOnPrimaryStoreObj, dataStore, future);
return;
}
throw new CloudRuntimeException("Unable to acquire lock on VMTemplateStoragePool: " + templatePoolRefId);
}
if (s_logger.isDebugEnabled()) {
s_logger.info("lock is acquired for VMTemplateStoragePool " + templatePoolRefId);
}
try {
if (templatePoolRef.getState() == ObjectInDataStoreStateMachine.State.Ready) {
s_logger.info("Template " + template.getUniqueName() + " is already copied to primary storage, skip copying");
createVolumeFromBaseImageAsync(volume, templateOnPrimaryStoreObj, dataStore, future);
return;
}
templateOnPrimaryStoreObj.processEvent(Event.CreateOnlyRequested);
motionSrv.copyAsync(template, templateOnPrimaryStoreObj, caller);
} finally {
if (s_logger.isDebugEnabled()) {
s_logger.info("releasing lock for VMTemplateStoragePool " + templatePoolRefId);
}
_tmpltPoolDao.releaseFromLockTable(templatePoolRefId);
}
return;
}
use of com.cloud.engine.subsystem.api.storage.DataObject in project cosmic by MissionCriticalCloud.
the class CloudStackPrimaryDataStoreDriverImpl method copyAsync.
@Override
public void copyAsync(final DataObject srcdata, final DataObject destData, final AsyncCompletionCallback<CopyCommandResult> callback) {
final DataStore store = destData.getDataStore();
if (store.getRole() == DataStoreRole.Primary) {
if ((srcdata.getType() == DataObjectType.TEMPLATE && destData.getType() == DataObjectType.TEMPLATE)) {
// For CLVM, we need to copy template to primary storage at all, just fake the copy result.
final TemplateObjectTO templateObjectTO = new TemplateObjectTO();
templateObjectTO.setPath(UUID.randomUUID().toString());
templateObjectTO.setSize(srcdata.getSize());
templateObjectTO.setPhysicalSize(srcdata.getSize());
templateObjectTO.setFormat(Storage.ImageFormat.RAW);
final CopyCmdAnswer answer = new CopyCmdAnswer(templateObjectTO);
final CopyCommandResult result = new CopyCommandResult("", answer);
callback.complete(result);
} else if (srcdata.getType() == DataObjectType.TEMPLATE && destData.getType() == DataObjectType.VOLUME) {
// For CLVM, we need to pass template on secondary storage to hypervisor
final String value = configDao.getValue(Config.PrimaryStorageDownloadWait.toString());
final int _primaryStorageDownloadWait = NumbersUtil.parseInt(value, Integer.parseInt(Config.PrimaryStorageDownloadWait.getDefaultValue()));
final StoragePoolVO storagePoolVO = primaryStoreDao.findById(store.getId());
final DataStore imageStore = templateManager.getImageStore(storagePoolVO.getDataCenterId(), srcdata.getId());
final DataObject srcData = templateDataFactory.getTemplate(srcdata.getId(), imageStore);
final CopyCommand cmd = new CopyCommand(srcData.getTO(), destData.getTO(), _primaryStorageDownloadWait, true);
final EndPoint ep = epSelector.select(srcData, destData);
Answer answer = null;
if (ep == null) {
final String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
s_logger.error(errMsg);
answer = new Answer(cmd, false, errMsg);
} else {
answer = ep.sendMessage(cmd);
}
final CopyCommandResult result = new CopyCommandResult("", answer);
callback.complete(result);
}
}
}
use of com.cloud.engine.subsystem.api.storage.DataObject in project cosmic by MissionCriticalCloud.
the class BaseImageStoreDriverImpl method createTemplateAsyncCallback.
protected Void createTemplateAsyncCallback(final AsyncCallbackDispatcher<? extends BaseImageStoreDriverImpl, DownloadAnswer> callback, final CreateContext<CreateCmdResult> context) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Performing image store createTemplate async callback");
}
final DownloadAnswer answer = callback.getResult();
final DataObject obj = context.data;
final DataStore store = obj.getDataStore();
final 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;
}
final 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
final VMTemplateVO tmlptUpdater = _templateDao.createForUpdate();
tmlptUpdater.setSize(answer.getTemplateSize());
_templateDao.update(obj.getId(), tmlptUpdater);
}
final AsyncCompletionCallback<CreateCmdResult> caller = context.getParentCallback();
if (answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR || answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.ABANDONED || answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.UNKNOWN) {
final CreateCmdResult result = new CreateCmdResult(null, null);
result.setSuccess(false);
result.setResult(answer.getErrorString());
caller.complete(result);
final String msg = "Failed to register template: " + obj.getUuid() + " with error: " + answer.getErrorString();
s_logger.error(msg);
} else if (answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
if (answer.getCheckSum() != null) {
final VMTemplateVO templateDaoBuilder = _templateDao.createForUpdate();
templateDaoBuilder.setChecksum(answer.getCheckSum());
_templateDao.update(obj.getId(), templateDaoBuilder);
}
final CreateCmdResult result = new CreateCmdResult(null, null);
caller.complete(result);
}
return null;
}
use of com.cloud.engine.subsystem.api.storage.DataObject in project cosmic by MissionCriticalCloud.
the class BaseImageStoreDriverImpl method createVolumeAsyncCallback.
protected Void createVolumeAsyncCallback(final AsyncCallbackDispatcher<? extends BaseImageStoreDriverImpl, DownloadAnswer> callback, final CreateContext<CreateCmdResult> context) {
final DownloadAnswer answer = callback.getResult();
final DataObject obj = context.data;
final DataStore store = obj.getDataStore();
final VolumeDataStoreVO volStoreVO = _volumeStoreDao.findByStoreVolume(store.getId(), obj.getId());
if (volStoreVO != null) {
if (volStoreVO.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Volume is already in DOWNLOADED state, ignore further incoming DownloadAnswer");
}
return null;
}
final VolumeDataStoreVO updateBuilder = _volumeStoreDao.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());
_volumeStoreDao.update(volStoreVO.getId(), updateBuilder);
// update size in volume table
final VolumeVO volUpdater = volumeDao.createForUpdate();
volUpdater.setSize(answer.getTemplateSize());
volumeDao.update(obj.getId(), volUpdater);
}
final AsyncCompletionCallback<CreateCmdResult> caller = context.getParentCallback();
if (answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR || answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.ABANDONED || answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.UNKNOWN) {
final CreateCmdResult result = new CreateCmdResult(null, null);
result.setSuccess(false);
result.setResult(answer.getErrorString());
caller.complete(result);
final String msg = "Failed to upload volume: " + obj.getUuid() + " with error: " + answer.getErrorString();
_alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_UPLOAD_FAILED, (volStoreVO == null ? -1L : volStoreVO.getZoneId()), null, msg, msg);
s_logger.error(msg);
} else if (answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
final CreateCmdResult result = new CreateCmdResult(null, null);
caller.complete(result);
}
return null;
}
use of com.cloud.engine.subsystem.api.storage.DataObject in project cosmic by MissionCriticalCloud.
the class StorageCacheManagerImpl method createCacheObject.
@Override
public DataObject createCacheObject(final DataObject data, final DataStore store) {
DataObject objOnCacheStore = null;
final Object lock;
final DataObjectType type = data.getType();
final String typeName;
final long storeId = store.getId();
final long dataId = data.getId();
/*
* Make sure any thread knows own lock type.
*/
if (type == DataObjectType.TEMPLATE) {
lock = templateLock;
typeName = "template";
} else if (type == DataObjectType.VOLUME) {
lock = volumeLock;
typeName = "volume";
} else if (type == DataObjectType.SNAPSHOT) {
lock = snapshotLock;
typeName = "snapshot";
} else {
final String msg = "unsupported DataObject comes, then can't acquire correct lock object";
throw new CloudRuntimeException(msg);
}
s_logger.debug("check " + typeName + " cache entry(id: " + dataId + ") on store(id: " + storeId + ")");
DataObject existingDataObj = null;
synchronized (lock) {
DataObjectInStore obj = objectInStoreMgr.findObject(data, store);
if (obj != null) {
State st = obj.getState();
final long miliSeconds = 10000;
final long timeoutSeconds = 3600;
final long timeoutMiliSeconds = timeoutSeconds * 1000;
Date now = new Date();
final long expiredEpoch = now.getTime() + timeoutMiliSeconds;
final Date expiredDate = new Date(expiredEpoch);
/*
* Waiting for completion of cache copy.
*/
while (st == ObjectInDataStoreStateMachine.State.Allocated || st == ObjectInDataStoreStateMachine.State.Creating || st == ObjectInDataStoreStateMachine.State.Copying) {
/*
* Threads must release lock within waiting for cache copy and
* must be waken up at completion.
*/
s_logger.debug("waiting cache copy completion type: " + typeName + ", id: " + obj.getObjectId() + ", lock: " + lock.hashCode());
try {
lock.wait(miliSeconds);
} catch (final InterruptedException e) {
s_logger.debug("[ignored] interupted while waiting for cache copy completion.");
}
s_logger.debug("waken up");
now = new Date();
if (now.after(expiredDate)) {
final String msg = "Waiting time exceeds timeout limit(" + timeoutSeconds + " s)";
throw new CloudRuntimeException(msg);
}
obj = objectInStoreMgr.findObject(data, store);
st = obj.getState();
}
if (st == ObjectInDataStoreStateMachine.State.Ready) {
s_logger.debug("there is already one in the cache store");
final DataObject dataObj = objectInStoreMgr.get(data, store);
dataObj.incRefCount();
existingDataObj = dataObj;
}
}
if (existingDataObj == null) {
s_logger.debug("create " + typeName + " cache entry(id: " + dataId + ") on store(id: " + storeId + ")");
objOnCacheStore = store.create(data);
}
lock.notifyAll();
}
if (existingDataObj != null) {
return existingDataObj;
}
if (objOnCacheStore == null) {
s_logger.error("create " + typeName + " cache entry(id: " + dataId + ") on store(id: " + storeId + ") failed");
return null;
}
final AsyncCallFuture<CopyCommandResult> future = new AsyncCallFuture<>();
CopyCommandResult result = null;
try {
objOnCacheStore.processEvent(Event.CreateOnlyRequested);
dataMotionSvr.copyAsync(data, objOnCacheStore, future);
result = future.get();
if (result.isFailed()) {
objOnCacheStore.processEvent(Event.OperationFailed);
} else {
objOnCacheStore.processEvent(Event.OperationSuccessed, result.getAnswer());
objOnCacheStore.incRefCount();
return objOnCacheStore;
}
} catch (final InterruptedException e) {
s_logger.debug("create cache storage failed: " + e.toString());
throw new CloudRuntimeException(e);
} catch (final ExecutionException e) {
s_logger.debug("create cache storage failed: " + e.toString());
throw new CloudRuntimeException(e);
} finally {
if (result == null) {
objOnCacheStore.processEvent(Event.OperationFailed);
}
synchronized (lock) {
/*
* Wake up all threads waiting for cache copy.
*/
s_logger.debug("wake up all waiting threads(lock: " + lock.hashCode() + ")");
lock.notifyAll();
}
}
return null;
}
Aggregations