Search in sources :

Example 26 with VolumeDataStoreVO

use of com.cloud.storage.datastore.db.VolumeDataStoreVO in project cosmic by MissionCriticalCloud.

the class ObjectInDataStoreManagerImpl method create.

@Override
public DataObject create(final DataObject obj, final DataStore dataStore) {
    if (dataStore.getRole() == DataStoreRole.Primary) {
        if (obj.getType() == DataObjectType.TEMPLATE) {
            VMTemplateStoragePoolVO vo = new VMTemplateStoragePoolVO(dataStore.getId(), obj.getId());
            vo = templatePoolDao.persist(vo);
        } else if (obj.getType() == DataObjectType.SNAPSHOT) {
            final SnapshotInfo snapshotInfo = (SnapshotInfo) obj;
            SnapshotDataStoreVO ss = new SnapshotDataStoreVO();
            ss.setSnapshotId(obj.getId());
            ss.setDataStoreId(dataStore.getId());
            ss.setRole(dataStore.getRole());
            ss.setVolumeId(snapshotInfo.getVolumeId());
            // this is the virtual size of snapshot in primary storage.
            ss.setSize(snapshotInfo.getSize());
            // this physical size will get updated with actual size once the snapshot backup is done.
            ss.setPhysicalSize(snapshotInfo.getSize());
            final SnapshotDataStoreVO snapshotDataStoreVO = snapshotDataStoreDao.findParent(dataStore.getRole(), dataStore.getId(), snapshotInfo.getVolumeId());
            if (snapshotDataStoreVO != null) {
                // Double check the snapshot is removed or not
                final SnapshotVO parentSnap = snapshotDao.findById(snapshotDataStoreVO.getSnapshotId());
                if (parentSnap != null) {
                    ss.setParentSnapshotId(snapshotDataStoreVO.getSnapshotId());
                } else {
                    s_logger.debug("find inconsistent db for snapshot " + snapshotDataStoreVO.getSnapshotId());
                }
            }
            ss.setState(ObjectInDataStoreStateMachine.State.Allocated);
            ss = snapshotDataStoreDao.persist(ss);
        }
    } else {
        // Image store
        switch(obj.getType()) {
            case TEMPLATE:
                TemplateDataStoreVO ts = new TemplateDataStoreVO();
                ts.setTemplateId(obj.getId());
                ts.setDataStoreId(dataStore.getId());
                ts.setDataStoreRole(dataStore.getRole());
                String installPath = TemplateConstants.DEFAULT_TMPLT_ROOT_DIR + "/" + TemplateConstants.DEFAULT_TMPLT_FIRST_LEVEL_DIR + templateDao.findById(obj.getId()).getAccountId() + "/" + obj.getId();
                ts.setInstallPath(installPath);
                ts.setState(ObjectInDataStoreStateMachine.State.Allocated);
                ts = templateDataStoreDao.persist(ts);
                break;
            case SNAPSHOT:
                final SnapshotInfo snapshot = (SnapshotInfo) obj;
                SnapshotDataStoreVO ss = new SnapshotDataStoreVO();
                ss.setSnapshotId(obj.getId());
                ss.setDataStoreId(dataStore.getId());
                ss.setRole(dataStore.getRole());
                ss.setSize(snapshot.getSize());
                ss.setVolumeId(snapshot.getVolumeId());
                final SnapshotDataStoreVO snapshotDataStoreVO = snapshotDataStoreDao.findParent(dataStore.getRole(), dataStore.getId(), snapshot.getVolumeId());
                if (snapshotDataStoreVO != null) {
                    ss.setParentSnapshotId(snapshotDataStoreVO.getSnapshotId());
                }
                ss.setInstallPath(TemplateConstants.DEFAULT_SNAPSHOT_ROOT_DIR + "/" + snapshotDao.findById(obj.getId()).getAccountId() + "/" + snapshot.getVolumeId());
                ss.setState(ObjectInDataStoreStateMachine.State.Allocated);
                ss = snapshotDataStoreDao.persist(ss);
                break;
            case VOLUME:
                VolumeDataStoreVO vs = new VolumeDataStoreVO();
                vs.setVolumeId(obj.getId());
                vs.setDataStoreId(dataStore.getId());
                vs.setInstallPath(TemplateConstants.DEFAULT_VOLUME_ROOT_DIR + "/" + volumeDao.findById(obj.getId()).getAccountId() + "/" + obj.getId());
                vs.setState(ObjectInDataStoreStateMachine.State.Allocated);
                vs = volumeDataStoreDao.persist(vs);
                break;
        }
    }
    return this.get(obj, dataStore);
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) VolumeDataStoreVO(com.cloud.storage.datastore.db.VolumeDataStoreVO) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO)

Example 27 with VolumeDataStoreVO

use of com.cloud.storage.datastore.db.VolumeDataStoreVO in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method orchestrateExtractVolume.

private String orchestrateExtractVolume(final long volumeId, final long zoneId) {
    // get latest volume state to make sure that it is not updated by other parallel operations
    final VolumeVO volume = _volsDao.findById(volumeId);
    if (volume == null || volume.getState() != Volume.State.Ready) {
        throw new InvalidParameterValueException("Volume to be extracted has been removed or not in right state!");
    }
    // perform extraction
    final ImageStoreEntity secStore = (ImageStoreEntity) dataStoreMgr.getImageStore(zoneId);
    final String value = _configDao.getValue(Config.CopyVolumeWait.toString());
    NumbersUtil.parseInt(value, Integer.parseInt(Config.CopyVolumeWait.getDefaultValue()));
    // Copy volume from primary to secondary storage
    final VolumeInfo srcVol = volFactory.getVolume(volumeId);
    final AsyncCallFuture<VolumeApiResult> cvAnswer = volService.copyVolume(srcVol, secStore);
    // Check if you got a valid answer.
    final VolumeApiResult cvResult;
    try {
        cvResult = cvAnswer.get();
    } catch (final InterruptedException e1) {
        s_logger.debug("failed copy volume", e1);
        throw new CloudRuntimeException("Failed to copy volume", e1);
    } catch (final ExecutionException e1) {
        s_logger.debug("failed copy volume", e1);
        throw new CloudRuntimeException("Failed to copy volume", e1);
    }
    if (cvResult == null || cvResult.isFailed()) {
        final String errorString = "Failed to copy the volume from the source primary storage pool to secondary storage.";
        throw new CloudRuntimeException(errorString);
    }
    final VolumeInfo vol = cvResult.getVolume();
    final String extractUrl = secStore.createEntityExtractUrl(vol.getPath(), vol.getFormat(), vol);
    final VolumeDataStoreVO volumeStoreRef = _volumeStoreDao.findByVolume(volumeId);
    volumeStoreRef.setExtractUrl(extractUrl);
    volumeStoreRef.setExtractUrlCreated(DateUtil.now());
    _volumeStoreDao.update(volumeStoreRef.getId(), volumeStoreRef);
    return extractUrl;
}
Also used : InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeDataStoreVO(com.cloud.storage.datastore.db.VolumeDataStoreVO) ImageStoreEntity(com.cloud.storage.image.datastore.ImageStoreEntity) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) VolumeApiResult(com.cloud.engine.subsystem.api.storage.VolumeService.VolumeApiResult) ExecutionException(java.util.concurrent.ExecutionException)

Example 28 with VolumeDataStoreVO

use of com.cloud.storage.datastore.db.VolumeDataStoreVO in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method extractVolume.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_EXTRACT, eventDescription = "extracting volume", async = true)
public String extractVolume(final ExtractVolumeCmd cmd) {
    final Long volumeId = cmd.getId();
    final Long zoneId = cmd.getZoneId();
    final String mode = cmd.getMode();
    final Account account = CallContext.current().getCallingAccount();
    if (!_accountMgr.isRootAdmin(account.getId()) && ApiDBUtils.isExtractionDisabled()) {
        throw new PermissionDeniedException("Extraction has been disabled by admin");
    }
    final VolumeVO volume = _volsDao.findById(volumeId);
    if (volume == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find volume with specified volumeId");
        ex.addProxyObject(volumeId.toString(), "volumeId");
        throw ex;
    }
    // perform permission check
    _accountMgr.checkAccess(account, null, true, volume);
    if (_dcDao.findById(zoneId) == null) {
        throw new InvalidParameterValueException("Please specify a valid zone.");
    }
    if (volume.getPoolId() == null) {
        throw new InvalidParameterValueException("The volume doesnt belong to a storage pool so cant extract it");
    }
    // instance is stopped
    if (volume.getInstanceId() != null && ApiDBUtils.findVMInstanceById(volume.getInstanceId()).getState() != State.Stopped) {
        s_logger.debug("Invalid state of the volume with ID: " + volumeId + ". It should be either detached or the VM should be in stopped state.");
        final PermissionDeniedException ex = new PermissionDeniedException("Invalid state of the volume with specified ID. It should be either detached or the VM should be in stopped state.");
        ex.addProxyObject(volume.getUuid(), "volumeId");
        throw ex;
    }
    if (volume.getVolumeType() != Volume.Type.DATADISK) {
        // Datadisk dont have any template dependence.
        final VMTemplateVO template = ApiDBUtils.findTemplateById(volume.getTemplateId());
        if (template != null) {
            // For ISO based volumes template = null and
            // we allow extraction of all ISO based
            // volumes
            final boolean isExtractable = template.isExtractable() && template.getTemplateType() != Storage.TemplateType.SYSTEM;
            if (!isExtractable && account != null && !_accountMgr.isRootAdmin(account.getId())) {
                // Global admins are always allowed to extract
                final PermissionDeniedException ex = new PermissionDeniedException("The volume with specified volumeId is not allowed to be extracted");
                ex.addProxyObject(volume.getUuid(), "volumeId");
                throw ex;
            }
        }
    }
    if (mode == null || !mode.equals(Upload.Mode.FTP_UPLOAD.toString()) && !mode.equals(Upload.Mode.HTTP_DOWNLOAD.toString())) {
        throw new InvalidParameterValueException("Please specify a valid extract Mode ");
    }
    // Check if the url already exists
    final VolumeDataStoreVO volumeStoreRef = _volumeStoreDao.findByVolume(volumeId);
    if (volumeStoreRef != null && volumeStoreRef.getExtractUrl() != null) {
        return volumeStoreRef.getExtractUrl();
    }
    VMInstanceVO vm = null;
    if (volume.getInstanceId() != null) {
        vm = _vmInstanceDao.findById(volume.getInstanceId());
    }
    if (vm != null) {
        // serialize VM operation
        final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext();
        if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) {
            // avoid re-entrance
            final VmWorkJobVO placeHolder;
            placeHolder = createPlaceHolderWork(vm.getId());
            try {
                return orchestrateExtractVolume(volume.getId(), zoneId);
            } finally {
                _workJobDao.expunge(placeHolder.getId());
            }
        } else {
            final Outcome<String> outcome = extractVolumeThroughJobQueue(vm.getId(), volume.getId(), zoneId);
            try {
                outcome.get();
            } catch (final InterruptedException e) {
                throw new RuntimeException("Operation is interrupted", e);
            } catch (final java.util.concurrent.ExecutionException e) {
                throw new RuntimeException("Execution excetion", e);
            }
            final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob());
            if (jobResult != null) {
                if (jobResult instanceof ConcurrentOperationException) {
                    throw (ConcurrentOperationException) jobResult;
                } else if (jobResult instanceof RuntimeException) {
                    throw (RuntimeException) jobResult;
                } else if (jobResult instanceof Throwable) {
                    throw new RuntimeException("Unexpected exception", (Throwable) jobResult);
                }
            }
            // retrieve the entity url from job result
            if (jobResult != null && jobResult instanceof String) {
                return (String) jobResult;
            }
            return null;
        }
    }
    return orchestrateExtractVolume(volume.getId(), zoneId);
}
Also used : Account(com.cloud.user.Account) AsyncJobExecutionContext(com.cloud.framework.jobs.AsyncJobExecutionContext) VMInstanceVO(com.cloud.vm.VMInstanceVO) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO) ExecutionException(java.util.concurrent.ExecutionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) VolumeDataStoreVO(com.cloud.storage.datastore.db.VolumeDataStoreVO) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) DataObject(com.cloud.engine.subsystem.api.storage.DataObject) ActionEvent(com.cloud.event.ActionEvent)

Example 29 with VolumeDataStoreVO

use of com.cloud.storage.datastore.db.VolumeDataStoreVO in project cosmic by MissionCriticalCloud.

the class StorageManagerImpl method cleanupSecondaryStorage.

@Override
@DB
public void cleanupSecondaryStorage(final boolean recurring) {
    // so here we don't need to issue DeleteCommand to resource anymore, only need to remove db entry.
    try {
        // Cleanup templates in template_store_ref
        final List<DataStore> imageStores = _dataStoreMgr.getImageStoresByScope(new ZoneScope(null));
        for (final DataStore store : imageStores) {
            try {
                final long storeId = store.getId();
                final List<TemplateDataStoreVO> destroyedTemplateStoreVOs = _templateStoreDao.listDestroyed(storeId);
                s_logger.debug("Secondary storage garbage collector found " + destroyedTemplateStoreVOs.size() + " templates to cleanup on template_store_ref for store: " + store.getName());
                for (final TemplateDataStoreVO destroyedTemplateStoreVO : destroyedTemplateStoreVOs) {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Deleting template store DB entry: " + destroyedTemplateStoreVO);
                    }
                    _templateStoreDao.remove(destroyedTemplateStoreVO.getId());
                }
            } catch (final Exception e) {
                s_logger.warn("problem cleaning up templates in template_store_ref for store: " + store.getName(), e);
            }
        }
        // CleanUp snapshots on snapshot_store_ref
        for (final DataStore store : imageStores) {
            try {
                final List<SnapshotDataStoreVO> destroyedSnapshotStoreVOs = _snapshotStoreDao.listDestroyed(store.getId());
                s_logger.debug("Secondary storage garbage collector found " + destroyedSnapshotStoreVOs.size() + " snapshots to cleanup on snapshot_store_ref for store: " + store.getName());
                for (final SnapshotDataStoreVO destroyedSnapshotStoreVO : destroyedSnapshotStoreVOs) {
                    // check if this snapshot has child
                    final SnapshotInfo snap = snapshotFactory.getSnapshot(destroyedSnapshotStoreVO.getSnapshotId(), store);
                    if (snap.getChild() != null) {
                        s_logger.debug("Skip snapshot on store: " + destroyedSnapshotStoreVO + " , because it has child");
                        continue;
                    }
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Deleting snapshot store DB entry: " + destroyedSnapshotStoreVO);
                    }
                    _snapshotDao.remove(destroyedSnapshotStoreVO.getSnapshotId());
                    final SnapshotDataStoreVO snapshotOnPrimary = _snapshotStoreDao.findBySnapshot(destroyedSnapshotStoreVO.getSnapshotId(), DataStoreRole.Primary);
                    if (snapshotOnPrimary != null) {
                        _snapshotStoreDao.remove(snapshotOnPrimary.getId());
                    }
                    _snapshotStoreDao.remove(destroyedSnapshotStoreVO.getId());
                }
            } catch (final Exception e2) {
                s_logger.warn("problem cleaning up snapshots in snapshot_store_ref for store: " + store.getName(), e2);
            }
        }
        // CleanUp volumes on volume_store_ref
        for (final DataStore store : imageStores) {
            try {
                final List<VolumeDataStoreVO> destroyedStoreVOs = _volumeStoreDao.listDestroyed(store.getId());
                s_logger.debug("Secondary storage garbage collector found " + destroyedStoreVOs.size() + " volumes to cleanup on volume_store_ref for store: " + store.getName());
                for (final VolumeDataStoreVO destroyedStoreVO : destroyedStoreVOs) {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Deleting volume store DB entry: " + destroyedStoreVO);
                    }
                    _volumeStoreDao.remove(destroyedStoreVO.getId());
                }
            } catch (final Exception e2) {
                s_logger.warn("problem cleaning up volumes in volume_store_ref for store: " + store.getName(), e2);
            }
        }
    } catch (final Exception e3) {
        s_logger.warn("problem cleaning up secondary storage DB entries. ", e3);
    }
}
Also used : ZoneScope(com.cloud.engine.subsystem.api.storage.ZoneScope) SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) VolumeDataStoreVO(com.cloud.storage.datastore.db.VolumeDataStoreVO) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) ConnectionException(com.cloud.exception.ConnectionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) StorageConflictException(com.cloud.exception.StorageConflictException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) DB(com.cloud.utils.db.DB)

Example 30 with VolumeDataStoreVO

use of com.cloud.storage.datastore.db.VolumeDataStoreVO in project cosmic by MissionCriticalCloud.

the class StorageManagerImpl method cleanupStorage.

@Override
public void cleanupStorage(final boolean recurring) {
    final GlobalLock scanLock = GlobalLock.getInternLock("storagemgr.cleanup");
    try {
        if (scanLock.lock(3)) {
            try {
                // Cleanup primary storage pools
                if (_templateCleanupEnabled) {
                    final List<StoragePoolVO> storagePools = _storagePoolDao.listAll();
                    for (final StoragePoolVO pool : storagePools) {
                        try {
                            final List<VMTemplateStoragePoolVO> unusedTemplatesInPool = _tmpltMgr.getUnusedTemplatesInPool(pool);
                            s_logger.debug("Storage pool garbage collector found " + unusedTemplatesInPool.size() + " templates to clean up in storage pool: " + pool.getName());
                            for (final VMTemplateStoragePoolVO templatePoolVO : unusedTemplatesInPool) {
                                if (templatePoolVO.getDownloadState() != VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
                                    s_logger.debug("Storage pool garbage collector is skipping template with ID: " + templatePoolVO.getTemplateId() + " on pool " + templatePoolVO.getPoolId() + " because it is not completely downloaded.");
                                    continue;
                                }
                                if (!templatePoolVO.getMarkedForGC()) {
                                    templatePoolVO.setMarkedForGC(true);
                                    _vmTemplatePoolDao.update(templatePoolVO.getId(), templatePoolVO);
                                    s_logger.debug("Storage pool garbage collector has marked template with ID: " + templatePoolVO.getTemplateId() + " on pool " + templatePoolVO.getPoolId() + " for garbage collection.");
                                    continue;
                                }
                                _tmpltMgr.evictTemplateFromStoragePool(templatePoolVO);
                            }
                        } catch (final Exception e) {
                            s_logger.warn("Problem cleaning up primary storage pool " + pool, e);
                        }
                    }
                }
                cleanupSecondaryStorage(recurring);
                final List<VolumeVO> vols = _volsDao.listNonRootVolumesToBeDestroyed(new Date(System.currentTimeMillis() - ((long) StorageCleanupDelay.value() << 10)));
                for (final VolumeVO vol : vols) {
                    try {
                        VolumeInfo volumeInfo = volFactory.getVolume(vol.getId());
                        if (volumeInfo != null) {
                            volService.expungeVolumeAsync(volumeInfo);
                        } else {
                            s_logger.debug("Volume " + vol.getUuid() + " is already destroyed");
                        }
                    } catch (final Exception e) {
                        s_logger.warn("Unable to destroy volume " + vol.getUuid(), e);
                    }
                }
                // remove snapshots in Error state
                final List<SnapshotVO> snapshots = _snapshotDao.listAllByStatus(Snapshot.State.Error);
                for (final SnapshotVO snapshotVO : snapshots) {
                    try {
                        final List<SnapshotDataStoreVO> storeRefs = _snapshotStoreDao.findBySnapshotId(snapshotVO.getId());
                        for (final SnapshotDataStoreVO ref : storeRefs) {
                            _snapshotStoreDao.expunge(ref.getId());
                        }
                        _snapshotDao.expunge(snapshotVO.getId());
                    } catch (final Exception e) {
                        s_logger.warn("Unable to destroy snapshot " + snapshotVO.getUuid(), e);
                    }
                }
                // destroy uploaded volumes in abandoned/error state
                final List<VolumeDataStoreVO> volumeDataStores = _volumeDataStoreDao.listByVolumeState(Volume.State.UploadError, Volume.State.UploadAbandoned);
                for (final VolumeDataStoreVO volumeDataStore : volumeDataStores) {
                    final VolumeVO volume = _volumeDao.findById(volumeDataStore.getVolumeId());
                    if (volume == null) {
                        s_logger.warn("Uploaded volume with id " + volumeDataStore.getVolumeId() + " not found, so cannot be destroyed");
                        continue;
                    }
                    try {
                        final DataStore dataStore = _dataStoreMgr.getDataStore(volumeDataStore.getDataStoreId(), DataStoreRole.Image);
                        final EndPoint ep = _epSelector.select(dataStore, volumeDataStore.getExtractUrl());
                        if (ep == null) {
                            s_logger.warn("There is no secondary storage VM for image store " + dataStore.getName() + ", cannot destroy uploaded volume " + volume.getUuid());
                            continue;
                        }
                        final Host host = _hostDao.findById(ep.getId());
                        if (host != null && host.getManagementServerId() != null) {
                            if (_serverId == host.getManagementServerId().longValue()) {
                                if (!volService.destroyVolume(volume.getId())) {
                                    s_logger.warn("Unable to destroy uploaded volume " + volume.getUuid());
                                    continue;
                                }
                                // decrement volume resource count
                                _resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.volume, volume.isDisplayVolume());
                                // expunge volume from secondary if volume is on image store
                                final VolumeInfo volOnSecondary = volFactory.getVolume(volume.getId(), DataStoreRole.Image);
                                if (volOnSecondary != null) {
                                    s_logger.info("Expunging volume " + volume.getUuid() + " uploaded using HTTP POST from secondary data store");
                                    final AsyncCallFuture<VolumeApiResult> future = volService.expungeVolumeAsync(volOnSecondary);
                                    final VolumeApiResult result = future.get();
                                    if (!result.isSuccess()) {
                                        s_logger.warn("Failed to expunge volume " + volume.getUuid() + " from the image store " + dataStore.getName() + " due to: " + result.getResult());
                                    }
                                }
                            }
                        }
                    } catch (InterruptedException | ExecutionException e) {
                        s_logger.warn("Unable to destroy uploaded volume " + volume.getUuid() + ". Error details: " + e.getMessage());
                    }
                }
                // destroy uploaded templates in abandoned/error state
                final List<TemplateDataStoreVO> templateDataStores = _templateStoreDao.listByTemplateState(VirtualMachineTemplate.State.UploadError, VirtualMachineTemplate.State.UploadAbandoned);
                for (final TemplateDataStoreVO templateDataStore : templateDataStores) {
                    final VMTemplateVO template = _templateDao.findById(templateDataStore.getTemplateId());
                    if (template == null) {
                        s_logger.warn("Uploaded template with id " + templateDataStore.getTemplateId() + " not found, so cannot be destroyed");
                        continue;
                    }
                    try {
                        final DataStore dataStore = _dataStoreMgr.getDataStore(templateDataStore.getDataStoreId(), DataStoreRole.Image);
                        final EndPoint ep = _epSelector.select(dataStore, templateDataStore.getExtractUrl());
                        if (ep == null) {
                            s_logger.warn("There is no secondary storage VM for image store " + dataStore.getName() + ", cannot destroy uploaded template " + template.getUuid());
                            continue;
                        }
                        final Host host = _hostDao.findById(ep.getId());
                        if (host != null && host.getManagementServerId() != null) {
                            if (_serverId == host.getManagementServerId().longValue()) {
                                final AsyncCallFuture<TemplateApiResult> future = _imageSrv.deleteTemplateAsync(tmplFactory.getTemplate(template.getId(), dataStore));
                                final TemplateApiResult result = future.get();
                                if (!result.isSuccess()) {
                                    s_logger.warn("Failed to delete template " + template.getUuid() + " from the image store " + dataStore.getName() + " due to: " + result.getResult());
                                    continue;
                                }
                                // remove from template_zone_ref
                                final List<VMTemplateZoneVO> templateZones = _vmTemplateZoneDao.listByZoneTemplate(((ImageStoreEntity) dataStore).getDataCenterId(), template.getId());
                                if (templateZones != null) {
                                    for (final VMTemplateZoneVO templateZone : templateZones) {
                                        _vmTemplateZoneDao.remove(templateZone.getId());
                                    }
                                }
                                // mark all the occurrences of this template in the given store as destroyed
                                _templateStoreDao.removeByTemplateStore(template.getId(), dataStore.getId());
                                // find all eligible image stores for this template
                                final List<DataStore> imageStores = _tmpltMgr.getImageStoreByTemplate(template.getId(), null);
                                if (imageStores == null || imageStores.size() == 0) {
                                    template.setState(VirtualMachineTemplate.State.Inactive);
                                    _templateDao.update(template.getId(), template);
                                    // decrement template resource count
                                    _resourceLimitMgr.decrementResourceCount(template.getAccountId(), ResourceType.template);
                                }
                            }
                        }
                    } catch (InterruptedException | ExecutionException e) {
                        s_logger.warn("Unable to destroy uploaded template " + template.getUuid() + ". Error details: " + e.getMessage());
                    }
                }
            } finally {
                scanLock.unlock();
            }
        }
    } finally {
        scanLock.releaseRef();
    }
}
Also used : VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) VolumeApiResult(com.cloud.engine.subsystem.api.storage.VolumeService.VolumeApiResult) GlobalLock(com.cloud.utils.db.GlobalLock) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) VolumeDataStoreVO(com.cloud.storage.datastore.db.VolumeDataStoreVO) ExecutionException(java.util.concurrent.ExecutionException) SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) Host(com.cloud.host.Host) ManagementServerHost(com.cloud.cluster.ManagementServerHost) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) ConnectionException(com.cloud.exception.ConnectionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) StorageConflictException(com.cloud.exception.StorageConflictException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) Date(java.util.Date) TemplateApiResult(com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult)

Aggregations

VolumeDataStoreVO (com.cloud.storage.datastore.db.VolumeDataStoreVO)32 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)12 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)10 VolumeVO (com.cloud.storage.VolumeVO)9 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)8 SnapshotDataStoreVO (com.cloud.storage.datastore.db.SnapshotDataStoreVO)8 TemplateDataStoreVO (com.cloud.storage.datastore.db.TemplateDataStoreVO)7 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)7 Date (java.util.Date)7 ExecutionException (java.util.concurrent.ExecutionException)6 DataObject (com.cloud.engine.subsystem.api.storage.DataObject)5 CreateCmdResult (com.cloud.engine.subsystem.api.storage.CreateCmdResult)4 EndPoint (com.cloud.engine.subsystem.api.storage.EndPoint)4 DownloadAnswer (com.cloud.agent.api.storage.DownloadAnswer)3 VolumeApiResult (com.cloud.engine.subsystem.api.storage.VolumeService.VolumeApiResult)3 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)3 AsyncCallFuture (com.cloud.framework.async.AsyncCallFuture)3 CopyCmdAnswer (com.cloud.storage.command.CopyCmdAnswer)3 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)3 DB (com.cloud.utils.db.DB)3