Search in sources :

Example 76 with StoragePoolVO

use of com.cloud.storage.datastore.db.StoragePoolVO 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 77 with StoragePoolVO

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

the class StorageManagerImpl method storagePoolHasEnoughSpace.

@Override
public boolean storagePoolHasEnoughSpace(final List<Volume> volumes, final StoragePool pool) {
    if (volumes == null || volumes.isEmpty()) {
        return false;
    }
    if (!checkUsagedSpace(pool)) {
        return false;
    }
    // allocated space includes template of specified volume
    final StoragePoolVO poolVO = _storagePoolDao.findById(pool.getId());
    long allocatedSizeWithtemplate = _capacityMgr.getAllocatedPoolCapacity(poolVO, null);
    long totalAskingSize = 0;
    for (final Volume volume : volumes) {
        // refreshing the volume from the DB to get latest hv_ss_reserve (hypervisor snapshot reserve) field
        // I could have just assigned this to "volume", but decided to make a new variable for it so that it
        // might be clearer that this "volume" in "volumes" still might have an old value for hv_ss_reverse.
        VolumeVO volumeVO = _volumeDao.findById(volume.getId());
        if (volumeVO.getHypervisorSnapshotReserve() == null) {
            // update the volume's hv_ss_reserve (hypervisor snapshot reserve) from a disk offering (used for managed storage)
            volService.updateHypervisorSnapshotReserveForVolume(getDiskOfferingVO(volumeVO), volumeVO.getId(), getHypervisorType(volumeVO));
            // hv_ss_reserve field might have been updated; refresh from DB to make use of it in getVolumeSizeIncludingHypervisorSnapshotReserve
            volumeVO = _volumeDao.findById(volume.getId());
        }
        if (volumeVO.getTemplateId() != null) {
            final VMTemplateVO tmpl = _templateDao.findByIdIncludingRemoved(volumeVO.getTemplateId());
            if (tmpl != null && tmpl.getFormat() != ImageFormat.ISO) {
                allocatedSizeWithtemplate = _capacityMgr.getAllocatedPoolCapacity(poolVO, tmpl);
            }
        }
        if (volumeVO.getState() != Volume.State.Ready) {
            totalAskingSize = totalAskingSize + getVolumeSizeIncludingHypervisorSnapshotReserve(volumeVO, pool);
        }
    }
    final long totalOverProvCapacity;
    if (pool.getPoolType() == StoragePoolType.NetworkFilesystem || pool.getPoolType() == StoragePoolType.Filesystem) {
        final BigDecimal overProvFactor = getStorageOverProvisioningFactor(pool.getId());
        totalOverProvCapacity = overProvFactor.multiply(new BigDecimal(pool.getCapacityBytes())).longValue();
        s_logger.debug("Found storage pool " + poolVO.getName() + " of type " + pool.getPoolType().toString() + " with overprovisioning factor " + overProvFactor.toString());
        s_logger.debug("Total over provisioned capacity calculated is " + overProvFactor + " * " + pool.getCapacityBytes());
    } else {
        totalOverProvCapacity = pool.getCapacityBytes();
        s_logger.debug("Found storage pool " + poolVO.getName() + " of type " + pool.getPoolType().toString());
    }
    s_logger.debug("Total capacity of the pool " + poolVO.getName() + " id: " + pool.getId() + " is " + totalOverProvCapacity);
    final double storageAllocatedThreshold = CapacityManager.StorageAllocatedCapacityDisableThreshold.valueIn(pool.getDataCenterId());
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Checking pool: " + pool.getId() + " for volume allocation " + volumes.toString() + ", maxSize : " + totalOverProvCapacity + ", totalAllocatedSize : " + allocatedSizeWithtemplate + ", askingSize : " + totalAskingSize + ", allocated disable threshold: " + storageAllocatedThreshold);
    }
    final double usedPercentage = (allocatedSizeWithtemplate + totalAskingSize) / (double) totalOverProvCapacity;
    if (usedPercentage > storageAllocatedThreshold) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Insufficient un-allocated capacity on: " + pool.getId() + " for volume allocation: " + volumes.toString() + " since its allocated percentage: " + usedPercentage + " has crossed the allocated pool.storage.allocated.capacity.disablethreshold: " + storageAllocatedThreshold + ", skipping this pool");
        }
        return false;
    }
    if (totalOverProvCapacity < allocatedSizeWithtemplate + totalAskingSize) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Insufficient un-allocated capacity on: " + pool.getId() + " for volume allocation: " + volumes.toString() + ", not enough storage, maxSize : " + totalOverProvCapacity + ", totalAllocatedSize : " + allocatedSizeWithtemplate + ", askingSize : " + totalAskingSize);
        }
        return false;
    }
    return true;
}
Also used : StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) BigDecimal(java.math.BigDecimal)

Example 78 with StoragePoolVO

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

the class StorageManagerImpl method updateStoragePool.

@Override
public PrimaryDataStoreInfo updateStoragePool(final UpdateStoragePoolCmd cmd) throws IllegalArgumentException {
    // Input validation
    final Long id = cmd.getId();
    final List<String> tags = cmd.getTags();
    final StoragePoolVO pool = _storagePoolDao.findById(id);
    if (pool == null) {
        throw new IllegalArgumentException("Unable to find storage pool with ID: " + id);
    }
    final Map<String, String> updatedDetails = new HashMap<>();
    if (tags != null) {
        final Map<String, String> existingDetails = _storagePoolDetailsDao.listDetailsKeyPairs(id);
        final Set<String> existingKeys = existingDetails.keySet();
        final Map<String, String> existingDetailsToKeep = new HashMap<>();
        for (final String existingKey : existingKeys) {
            final String existingValue = existingDetails.get(existingKey);
            if (!Boolean.TRUE.toString().equalsIgnoreCase(existingValue)) {
                existingDetailsToKeep.put(existingKey, existingValue);
            }
        }
        final Map<String, String> details = new HashMap<>();
        for (String tag : tags) {
            tag = tag.trim();
            if (tag.length() > 0 && !details.containsKey(tag)) {
                details.put(tag, "true");
            }
        }
        final Set<String> existingKeysToKeep = existingDetailsToKeep.keySet();
        for (final String existingKeyToKeep : existingKeysToKeep) {
            final String existingValueToKeep = existingDetailsToKeep.get(existingKeyToKeep);
            if (details.containsKey(existingKeyToKeep)) {
                throw new CloudRuntimeException("Storage tag '" + existingKeyToKeep + "' conflicts with a stored property of this primary storage. No changes were made.");
            }
            details.put(existingKeyToKeep, existingValueToKeep);
        }
        updatedDetails.putAll(details);
    }
    Long updatedCapacityBytes = null;
    final Long capacityBytes = cmd.getCapacityBytes();
    if (capacityBytes != null) {
        if (capacityBytes != pool.getCapacityBytes()) {
            updatedCapacityBytes = capacityBytes;
        }
    }
    Long updatedCapacityIops = null;
    final Long capacityIops = cmd.getCapacityIops();
    if (capacityIops != null) {
        if (!capacityIops.equals(pool.getCapacityIops())) {
            updatedCapacityIops = capacityIops;
        }
    }
    if (updatedCapacityBytes != null || updatedCapacityIops != null) {
        final StoragePoolVO storagePool = _storagePoolDao.findById(id);
        final DataStoreProvider dataStoreProvider = _dataStoreProviderMgr.getDataStoreProvider(storagePool.getStorageProviderName());
        final DataStoreLifeCycle dataStoreLifeCycle = dataStoreProvider.getDataStoreLifeCycle();
        if (dataStoreLifeCycle instanceof PrimaryDataStoreLifeCycle) {
            final Map<String, String> details = new HashMap<>();
            details.put(PrimaryDataStoreLifeCycle.CAPACITY_BYTES, updatedCapacityBytes != null ? String.valueOf(updatedCapacityBytes) : null);
            details.put(PrimaryDataStoreLifeCycle.CAPACITY_IOPS, updatedCapacityIops != null ? String.valueOf(updatedCapacityIops) : null);
            ((PrimaryDataStoreLifeCycle) dataStoreLifeCycle).updateStoragePool(storagePool, details);
        }
    }
    final Boolean enabled = cmd.getEnabled();
    if (enabled != null) {
        if (enabled) {
            enablePrimaryStoragePool(pool);
        } else {
            disablePrimaryStoragePool(pool);
        }
    } else if (updatedDetails.size() >= 0) {
        _storagePoolDao.updateDetails(id, updatedDetails);
    }
    if (updatedCapacityBytes != null) {
        _storagePoolDao.updateCapacityBytes(id, capacityBytes);
    }
    if (updatedCapacityIops != null) {
        _storagePoolDao.updateCapacityIops(id, capacityIops);
    }
    return (PrimaryDataStoreInfo) _dataStoreMgr.getDataStore(pool.getId(), DataStoreRole.Primary);
}
Also used : PrimaryDataStoreInfo(com.cloud.engine.subsystem.api.storage.PrimaryDataStoreInfo) HashMap(java.util.HashMap) DataStoreProvider(com.cloud.engine.subsystem.api.storage.DataStoreProvider) DataStoreLifeCycle(com.cloud.engine.subsystem.api.storage.DataStoreLifeCycle) PrimaryDataStoreLifeCycle(com.cloud.engine.subsystem.api.storage.PrimaryDataStoreLifeCycle) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PrimaryDataStoreLifeCycle(com.cloud.engine.subsystem.api.storage.PrimaryDataStoreLifeCycle) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO)

Example 79 with StoragePoolVO

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

the class StorageManagerImpl method createLocalStorage.

@DB
@Override
public DataStore createLocalStorage(final Host host, final StoragePoolInfo pInfo) throws ConnectionException {
    final DataCenterVO dc = _dcDao.findById(host.getDataCenterId());
    if (dc == null) {
        return null;
    }
    boolean useLocalStorageForSystemVM = false;
    final Boolean isLocal = ConfigurationManagerImpl.SystemVMUseLocalStorage.valueIn(dc.getId());
    if (isLocal != null) {
        useLocalStorageForSystemVM = isLocal.booleanValue();
    }
    if (!(dc.isLocalStorageEnabled() || useLocalStorageForSystemVM)) {
        return null;
    }
    final DataStore store;
    try {
        final String hostAddress = pInfo.getHost();
        StoragePoolVO pool = _storagePoolDao.findPoolByHostPath(host.getDataCenterId(), host.getPodId(), hostAddress, pInfo.getHostPath(), pInfo.getUuid());
        if (pool == null) {
            // the path can be different, but if they have the same uuid, assume they are the same storage
            pool = _storagePoolDao.findPoolByHostPath(host.getDataCenterId(), host.getPodId(), hostAddress, null, pInfo.getUuid());
            if (pool != null) {
                s_logger.debug("Found a storage pool: " + pInfo.getUuid() + ", but with different hostpath " + pInfo.getHostPath() + ", still treat it as the same pool");
            }
        }
        final DataStoreProvider provider = _dataStoreProviderMgr.getDefaultPrimaryDataStoreProvider();
        final DataStoreLifeCycle lifeCycle = provider.getDataStoreLifeCycle();
        if (pool == null) {
            final Map<String, Object> params = new HashMap<>();
            final String name = host.getName() + " Local Storage";
            params.put("zoneId", host.getDataCenterId());
            params.put("clusterId", host.getClusterId());
            params.put("podId", host.getPodId());
            params.put("url", pInfo.getPoolType().toString() + "://" + pInfo.getHost() + "/" + pInfo.getHostPath());
            params.put("name", name);
            params.put("localStorage", true);
            params.put("details", pInfo.getDetails());
            params.put("uuid", pInfo.getUuid());
            params.put("providerName", provider.getName());
            store = lifeCycle.initialize(params);
        } else {
            store = _dataStoreMgr.getDataStore(pool.getId(), DataStoreRole.Primary);
        }
        pool = _storagePoolDao.findById(store.getId());
        if (pool.getStatus() != StoragePoolStatus.Maintenance && pool.getStatus() != StoragePoolStatus.Removed) {
            final HostScope scope = new HostScope(host.getId(), host.getClusterId(), host.getDataCenterId());
            lifeCycle.attachHost(store, scope, pInfo);
        }
    } catch (final Exception e) {
        s_logger.warn("Unable to setup the local storage pool for " + host, e);
        throw new ConnectionException(true, "Unable to setup the local storage pool for " + host, e);
    }
    return _dataStoreMgr.getDataStore(store.getId(), DataStoreRole.Primary);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) HashMap(java.util.HashMap) DataStoreProvider(com.cloud.engine.subsystem.api.storage.DataStoreProvider) HostScope(com.cloud.engine.subsystem.api.storage.HostScope) 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) DataStoreLifeCycle(com.cloud.engine.subsystem.api.storage.DataStoreLifeCycle) PrimaryDataStoreLifeCycle(com.cloud.engine.subsystem.api.storage.PrimaryDataStoreLifeCycle) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) ConnectionException(com.cloud.exception.ConnectionException) DB(com.cloud.utils.db.DB)

Example 80 with StoragePoolVO

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

the class StorageManagerImpl method getPrimaryStorageNameLabel.

@Override
public String getPrimaryStorageNameLabel(final VolumeVO volume) {
    final Long poolId = volume.getPoolId();
    // before.
    assert poolId != null;
    final StoragePoolVO PrimaryDataStoreVO = _storagePoolDao.findById(poolId);
    assert PrimaryDataStoreVO != null;
    return PrimaryDataStoreVO.getUuid();
}
Also used : StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO)

Aggregations

StoragePoolVO (com.cloud.storage.datastore.db.StoragePoolVO)86 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)29 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)22 Test (org.junit.Test)18 HostVO (com.cloud.host.HostVO)15 VolumeVO (com.cloud.storage.VolumeVO)15 ArrayList (java.util.ArrayList)15 Answer (com.cloud.agent.api.Answer)14 Account (com.cloud.user.Account)13 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)12 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)11 StoragePool (com.cloud.storage.StoragePool)10 AttachAnswer (com.cloud.storage.command.AttachAnswer)10 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)9 VMInstanceVO (com.cloud.vm.VMInstanceVO)9 RebootAnswer (com.cloud.agent.api.RebootAnswer)8 PrimaryDataStore (com.cloud.engine.subsystem.api.storage.PrimaryDataStore)8 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)8 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)8 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)8