use of org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO in project cloudstack by apache.
the class DownloadMonitorImpl method downloadVolumeToStorage.
@Override
public void downloadVolumeToStorage(DataObject volume, AsyncCompletionCallback<DownloadAnswer> callback) {
boolean downloadJobExists = false;
VolumeDataStoreVO volumeHost = null;
DataStore store = volume.getDataStore();
VolumeInfo volInfo = (VolumeInfo) volume;
RegisterVolumePayload payload = (RegisterVolumePayload) volInfo.getpayload();
String url = payload.getUrl();
String checkSum = payload.getChecksum();
ImageFormat format = ImageFormat.valueOf(payload.getFormat());
volumeHost = _volumeStoreDao.findByStoreVolume(store.getId(), volume.getId());
if (volumeHost == null) {
volumeHost = new VolumeDataStoreVO(store.getId(), volume.getId(), new Date(), 0, Status.NOT_DOWNLOADED, null, null, "jobid0000", null, url, checkSum);
_volumeStoreDao.persist(volumeHost);
} else if ((volumeHost.getJobId() != null) && (volumeHost.getJobId().length() > 2)) {
downloadJobExists = true;
} else {
// persit url and checksum
volumeHost.setDownloadUrl(url);
volumeHost.setChecksum(checkSum);
_volumeStoreDao.update(volumeHost.getId(), volumeHost);
}
Long maxVolumeSizeInBytes = getMaxVolumeSizeInBytes();
if (volumeHost != null) {
start();
Volume vol = _volumeDao.findById(volume.getId());
DownloadCommand dcmd = new DownloadCommand((VolumeObjectTO) (volume.getTO()), maxVolumeSizeInBytes, checkSum, url, format);
dcmd.setProxy(getHttpProxy());
if (downloadJobExists) {
dcmd = new DownloadProgressCommand(dcmd, volumeHost.getJobId(), RequestType.GET_OR_RESTART);
dcmd.setResourceType(ResourceType.VOLUME);
}
EndPoint ep = _epSelector.select(volume);
if (ep == null) {
s_logger.warn("There is no secondary storage VM for image store " + store.getName());
return;
}
DownloadListener dl = new DownloadListener(ep, store, volume, _timer, this, dcmd, callback);
// auto-wired those injected fields in DownloadListener
ComponentContext.inject(dl);
if (downloadJobExists) {
dl.setCurrState(volumeHost.getDownloadState());
}
try {
ep.sendMessageAsync(dcmd, new UploadListener.Callback(ep.getId(), dl));
} catch (Exception e) {
s_logger.warn("Unable to start /resume download of volume " + volume.getId() + " to " + store.getName(), e);
dl.setDisconnected();
dl.scheduleStatusCheck(RequestType.GET_OR_RESTART);
}
}
}
use of org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO in project cloudstack by apache.
the class VolumeApiServiceImpl method deleteVolume.
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_DELETE, eventDescription = "deleting volume")
public boolean deleteVolume(long volumeId, Account caller) throws ConcurrentOperationException {
VolumeVO volume = _volsDao.findById(volumeId);
if (volume == null) {
throw new InvalidParameterValueException("Unable to find volume with ID: " + volumeId);
}
if (!_snapshotMgr.canOperateOnVolume(volume)) {
throw new InvalidParameterValueException("There are snapshot operations in progress on the volume, unable to delete it");
}
_accountMgr.checkAccess(caller, null, true, volume);
if (volume.getInstanceId() != null) {
throw new InvalidParameterValueException("Please specify a volume that is not attached to any VM.");
}
if (volume.getState() == Volume.State.UploadOp) {
VolumeDataStoreVO volumeStore = _volumeStoreDao.findByVolume(volume.getId());
if (volumeStore.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOAD_IN_PROGRESS) {
throw new InvalidParameterValueException("Please specify a volume that is not uploading");
}
}
if (volume.getState() == Volume.State.NotUploaded || volume.getState() == Volume.State.UploadInProgress) {
throw new InvalidParameterValueException("The volume is either getting uploaded or it may be initiated shortly, please wait for it to be completed");
}
try {
if (volume.getState() != Volume.State.Destroy && volume.getState() != Volume.State.Expunging && volume.getState() != Volume.State.Expunged) {
Long instanceId = volume.getInstanceId();
if (!volService.destroyVolume(volume.getId())) {
return false;
}
VMInstanceVO vmInstance = _vmInstanceDao.findById(instanceId);
if (instanceId == null || (vmInstance.getType().equals(VirtualMachine.Type.User))) {
// Decrement the resource count for volumes and primary storage belonging user VM's only
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.volume, volume.isDisplayVolume());
}
}
// Mark volume as removed if volume has not been created on primary or secondary
if (volume.getState() == Volume.State.Allocated) {
_volsDao.remove(volumeId);
stateTransitTo(volume, Volume.Event.DestroyRequested);
return true;
}
// expunge volume from primary if volume is on primary
VolumeInfo volOnPrimary = volFactory.getVolume(volume.getId(), DataStoreRole.Primary);
if (volOnPrimary != null) {
s_logger.info("Expunging volume " + volume.getId() + " from primary data store");
AsyncCallFuture<VolumeApiResult> future = volService.expungeVolumeAsync(volOnPrimary);
future.get();
//decrement primary storage count
_resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(), ResourceType.primary_storage.getOrdinal());
}
// expunge volume from secondary if volume is on image store
VolumeInfo volOnSecondary = volFactory.getVolume(volume.getId(), DataStoreRole.Image);
if (volOnSecondary != null) {
s_logger.info("Expunging volume " + volume.getId() + " from secondary data store");
AsyncCallFuture<VolumeApiResult> future2 = volService.expungeVolumeAsync(volOnSecondary);
future2.get();
//decrement secondary storage count
_resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(), ResourceType.secondary_storage.getOrdinal());
}
// delete all cache entries for this volume
List<VolumeInfo> cacheVols = volFactory.listVolumeOnCache(volume.getId());
for (VolumeInfo volOnCache : cacheVols) {
s_logger.info("Delete volume from image cache store: " + volOnCache.getDataStore().getName());
volOnCache.delete();
}
} catch (InterruptedException | ExecutionException | NoTransitionException e) {
s_logger.warn("Failed to expunge volume:", e);
return false;
}
return true;
}
use of org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO in project cloudstack by apache.
the class VolumeServiceImpl method registerVolume.
@Override
public AsyncCallFuture<VolumeApiResult> registerVolume(VolumeInfo volume, DataStore store) {
AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
DataObject volumeOnStore = store.create(volume);
volumeOnStore.processEvent(Event.CreateOnlyRequested);
try {
CreateVolumeContext<VolumeApiResult> context = new CreateVolumeContext<VolumeApiResult>(null, volumeOnStore, future);
AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> caller = AsyncCallbackDispatcher.create(this);
caller.setCallback(caller.getTarget().registerVolumeCallback(null, null));
caller.setContext(context);
store.getDriver().createAsync(store, volumeOnStore, caller);
} catch (CloudRuntimeException ex) {
// clean up already persisted volume_store_ref entry in case of createVolumeCallback is never called
VolumeDataStoreVO volStoreVO = _volumeStoreDao.findByStoreVolume(store.getId(), volume.getId());
if (volStoreVO != null) {
VolumeInfo volObj = volFactory.getVolume(volume, store);
volObj.processEvent(ObjectInDataStoreStateMachine.Event.OperationFailed);
}
VolumeApiResult res = new VolumeApiResult((VolumeObject) volumeOnStore);
res.setResult(ex.getMessage());
future.complete(res);
}
return future;
}
use of org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO in project cloudstack by apache.
the class VolumeServiceImpl method registerVolumeCallback.
protected Void registerVolumeCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> callback, CreateVolumeContext<VolumeApiResult> context) {
CreateCmdResult result = callback.getResult();
VolumeObject vo = (VolumeObject) context.volume;
try {
if (result.isFailed()) {
vo.processEvent(Event.OperationFailed);
// delete the volume entry from volumes table in case of failure
VolumeVO vol = volDao.findById(vo.getId());
if (vol != null) {
volDao.remove(vo.getId());
}
} else {
vo.processEvent(Event.OperationSuccessed, result.getAnswer());
if (vo.getSize() != null) {
// publish usage events
// get physical size from volume_store_ref table
long physicalSize = 0;
DataStore ds = vo.getDataStore();
VolumeDataStoreVO volStore = _volumeStoreDao.findByStoreVolume(ds.getId(), vo.getId());
if (volStore != null) {
physicalSize = volStore.getPhysicalSize();
} else {
s_logger.warn("No entry found in volume_store_ref for volume id: " + vo.getId() + " and image store id: " + ds.getId() + " at the end of uploading volume!");
}
Scope dsScope = ds.getScope();
if (dsScope.getScopeType() == ScopeType.ZONE) {
if (dsScope.getScopeId() != null) {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_UPLOAD, vo.getAccountId(), dsScope.getScopeId(), vo.getId(), vo.getName(), null, null, physicalSize, vo.getSize(), Volume.class.getName(), vo.getUuid());
} else {
s_logger.warn("Zone scope image store " + ds.getId() + " has a null scope id");
}
} else if (dsScope.getScopeType() == ScopeType.REGION) {
// publish usage event for region-wide image store using a -1 zoneId for 4.2, need to revisit post-4.2
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_UPLOAD, vo.getAccountId(), -1, vo.getId(), vo.getName(), null, null, physicalSize, vo.getSize(), Volume.class.getName(), vo.getUuid());
_resourceLimitMgr.incrementResourceCount(vo.getAccountId(), ResourceType.secondary_storage, vo.getSize());
}
}
}
VolumeApiResult res = new VolumeApiResult(vo);
context.future.complete(res);
return null;
} catch (Exception e) {
s_logger.error("register volume failed: ", e);
// delete the volume entry from volumes table in case of failure
VolumeVO vol = volDao.findById(vo.getId());
if (vol != null) {
volDao.remove(vo.getId());
}
VolumeApiResult res = new VolumeApiResult(null);
context.future.complete(res);
return null;
}
}
use of org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO in project cloudstack by apache.
the class VolumeServiceImpl method createVolumeAsync.
@Override
public AsyncCallFuture<VolumeApiResult> createVolumeAsync(VolumeInfo volume, DataStore dataStore) {
AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
DataObject volumeOnStore = dataStore.create(volume);
volumeOnStore.processEvent(Event.CreateOnlyRequested);
try {
CreateVolumeContext<VolumeApiResult> context = new CreateVolumeContext<VolumeApiResult>(null, volumeOnStore, future);
AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> caller = AsyncCallbackDispatcher.create(this);
caller.setCallback(caller.getTarget().createVolumeCallback(null, null)).setContext(context);
dataStore.getDriver().createAsync(dataStore, volumeOnStore, caller);
} catch (CloudRuntimeException ex) {
// clean up already persisted volume_store_ref entry in case of createVolumeCallback is never called
VolumeDataStoreVO volStoreVO = _volumeStoreDao.findByStoreVolume(dataStore.getId(), volume.getId());
if (volStoreVO != null) {
VolumeInfo volObj = volFactory.getVolume(volume, dataStore);
volObj.processEvent(ObjectInDataStoreStateMachine.Event.OperationFailed);
}
VolumeApiResult volResult = new VolumeApiResult((VolumeObject) volumeOnStore);
volResult.setResult(ex.getMessage());
future.complete(volResult);
}
return future;
}
Aggregations