Search in sources :

Example 16 with VolumeDataStoreVO

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);
        }
    }
}
Also used : DownloadCommand(org.apache.cloudstack.storage.command.DownloadCommand) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RegisterVolumePayload(com.cloud.storage.RegisterVolumePayload) UploadListener(com.cloud.storage.upload.UploadListener) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ImageFormat(com.cloud.storage.Storage.ImageFormat) DownloadProgressCommand(org.apache.cloudstack.storage.command.DownloadProgressCommand) Volume(com.cloud.storage.Volume) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)

Example 17 with VolumeDataStoreVO

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;
}
Also used : VMInstanceVO(com.cloud.vm.VMInstanceVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) VolumeApiResult(org.apache.cloudstack.engine.subsystem.api.storage.VolumeService.VolumeApiResult) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO) ExecutionException(java.util.concurrent.ExecutionException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 18 with VolumeDataStoreVO

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;
}
Also used : VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)

Example 19 with VolumeDataStoreVO

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;
    }
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) Scope(org.apache.cloudstack.engine.subsystem.api.storage.Scope) Volume(com.cloud.storage.Volume) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO) CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 20 with VolumeDataStoreVO

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;
}
Also used : VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)

Aggregations

VolumeDataStoreVO (org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)33 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)12 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 VolumeVO (com.cloud.storage.VolumeVO)9 Date (java.util.Date)8 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)8 SnapshotDataStoreVO (org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)8 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)7 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)7 ExecutionException (java.util.concurrent.ExecutionException)5 DataObject (org.apache.cloudstack.engine.subsystem.api.storage.DataObject)5 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)4 DownloadAnswer (com.cloud.agent.api.storage.DownloadAnswer)3 ActionEvent (com.cloud.event.ActionEvent)3 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)3 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)3 DB (com.cloud.utils.db.DB)3 ArrayList (java.util.ArrayList)3 CreateCmdResult (org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult)3 AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)3