Search in sources :

Example 6 with VolumeApiResult

use of com.cloud.engine.subsystem.api.storage.VolumeService.VolumeApiResult in project cosmic by MissionCriticalCloud.

the class StorageManagerImpl method deletePool.

@Override
@DB
public boolean deletePool(final DeletePoolCmd cmd) {
    final Long id = cmd.getId();
    final boolean forced = cmd.isForced();
    final StoragePoolVO sPool = _storagePoolDao.findById(id);
    if (sPool == null) {
        s_logger.warn("Unable to find pool:" + id);
        throw new InvalidParameterValueException("Unable to find pool by id " + id);
    }
    if (sPool.getStatus() != StoragePoolStatus.Maintenance) {
        s_logger.warn("Unable to delete storage id: " + id + " due to it is not in Maintenance state");
        throw new InvalidParameterValueException("Unable to delete storage due to it is not in Maintenance state, id: " + id);
    }
    if (sPool.isLocal()) {
        s_logger.warn("Unable to delete local storage id:" + id);
        throw new InvalidParameterValueException("Unable to delete local storage id: " + id);
    }
    final Pair<Long, Long> vlms = _volsDao.getCountAndTotalByPool(id);
    if (forced) {
        if (vlms.first() > 0) {
            final Pair<Long, Long> nonDstrdVlms = _volsDao.getNonDestroyedCountAndTotalByPool(id);
            if (nonDstrdVlms.first() > 0) {
                throw new CloudRuntimeException("Cannot delete pool " + sPool.getName() + " as there are associated " + "non-destroyed vols for this pool");
            }
            // force expunge non-destroyed volumes
            final List<VolumeVO> vols = _volsDao.listVolumesToBeDestroyed();
            for (final VolumeVO vol : vols) {
                final AsyncCallFuture<VolumeApiResult> future = volService.expungeVolumeAsync(volFactory.getVolume(vol.getId()));
                try {
                    future.get();
                } catch (final InterruptedException e) {
                    s_logger.debug("expunge volume failed:" + vol.getId(), e);
                } catch (final ExecutionException e) {
                    s_logger.debug("expunge volume failed:" + vol.getId(), e);
                }
            }
        }
    } else {
        // If it does , then you cannot delete the pool
        if (vlms.first() > 0) {
            throw new CloudRuntimeException("Cannot delete pool " + sPool.getName() + " as there are associated volumes for this pool");
        }
    }
    // First get the host_id from storage_pool_host_ref for given pool id
    final StoragePoolVO lock = _storagePoolDao.acquireInLockTable(sPool.getId());
    if (lock == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Failed to acquire lock when deleting PrimaryDataStoreVO with ID: " + sPool.getId());
        }
        return false;
    }
    _storagePoolDao.releaseFromLockTable(lock.getId());
    s_logger.trace("Released lock for storage pool " + id);
    final DataStoreProvider storeProvider = _dataStoreProviderMgr.getDataStoreProvider(sPool.getStorageProviderName());
    final DataStoreLifeCycle lifeCycle = storeProvider.getDataStoreLifeCycle();
    final DataStore store = _dataStoreMgr.getDataStore(sPool.getId(), DataStoreRole.Primary);
    return lifeCycle.deleteDataStore(store);
}
Also used : DataStoreProvider(com.cloud.engine.subsystem.api.storage.DataStoreProvider) VolumeApiResult(com.cloud.engine.subsystem.api.storage.VolumeService.VolumeApiResult) DataStoreLifeCycle(com.cloud.engine.subsystem.api.storage.DataStoreLifeCycle) PrimaryDataStoreLifeCycle(com.cloud.engine.subsystem.api.storage.PrimaryDataStoreLifeCycle) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) ExecutionException(java.util.concurrent.ExecutionException) DB(com.cloud.utils.db.DB)

Example 7 with VolumeApiResult

use of com.cloud.engine.subsystem.api.storage.VolumeService.VolumeApiResult 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

VolumeApiResult (com.cloud.engine.subsystem.api.storage.VolumeService.VolumeApiResult)7 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 ExecutionException (java.util.concurrent.ExecutionException)6 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)5 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)5 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)3 StoragePoolVO (com.cloud.storage.datastore.db.StoragePoolVO)3 VolumeDataStoreVO (com.cloud.storage.datastore.db.VolumeDataStoreVO)3 DB (com.cloud.utils.db.DB)3 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)2 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)2 HostVO (com.cloud.host.HostVO)2 ManagementServerHost (com.cloud.cluster.ManagementServerHost)1 CopyCommandResult (com.cloud.engine.subsystem.api.storage.CopyCommandResult)1 DataStoreLifeCycle (com.cloud.engine.subsystem.api.storage.DataStoreLifeCycle)1 DataStoreProvider (com.cloud.engine.subsystem.api.storage.DataStoreProvider)1 EndPoint (com.cloud.engine.subsystem.api.storage.EndPoint)1 PrimaryDataStoreLifeCycle (com.cloud.engine.subsystem.api.storage.PrimaryDataStoreLifeCycle)1 TemplateApiResult (com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult)1 ActionEvent (com.cloud.event.ActionEvent)1