Search in sources :

Example 16 with StoragePoolAllocator

use of org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator in project cloudstack by apache.

the class DeploymentPlanningManagerImpl method findSuitablePoolsForVolumes.

protected Pair<Map<Volume, List<StoragePool>>, List<Volume>> findSuitablePoolsForVolumes(VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoid, int returnUpTo) {
    List<VolumeVO> volumesTobeCreated = _volsDao.findUsableVolumesForInstance(vmProfile.getId());
    Map<Volume, List<StoragePool>> suitableVolumeStoragePools = new HashMap<Volume, List<StoragePool>>();
    List<Volume> readyAndReusedVolumes = new ArrayList<Volume>();
    // There should be atleast the ROOT volume of the VM in usable state
    if (volumesTobeCreated.isEmpty()) {
        // OfflineVmwareMigration: find out what is wrong with the id of the vm we try to start
        throw new CloudRuntimeException("Unable to create deployment, no usable volumes found for the VM: " + vmProfile.getId());
    }
    // don't allow to start vm that doesn't have a root volume
    if (_volsDao.findByInstanceAndType(vmProfile.getId(), Volume.Type.ROOT).isEmpty()) {
        throw new CloudRuntimeException("Unable to prepare volumes for vm as ROOT volume is missing");
    }
    // for each volume find list of suitable storage pools by calling the
    // allocators
    Set<Long> originalAvoidPoolSet = avoid.getPoolsToAvoid();
    if (originalAvoidPoolSet == null) {
        originalAvoidPoolSet = new HashSet<Long>();
    }
    Set<Long> poolsToAvoidOutput = new HashSet<Long>(originalAvoidPoolSet);
    for (VolumeVO toBeCreated : volumesTobeCreated) {
        s_logger.debug("Checking suitable pools for volume (Id, Type): (" + toBeCreated.getId() + "," + toBeCreated.getVolumeType().name() + ")");
        // be reused.
        if (plan.getPoolId() != null || (toBeCreated.getVolumeType() == Volume.Type.DATADISK && toBeCreated.getPoolId() != null && toBeCreated.getState() == Volume.State.Ready)) {
            s_logger.debug("Volume has pool already allocated, checking if pool can be reused, poolId: " + toBeCreated.getPoolId());
            List<StoragePool> suitablePools = new ArrayList<StoragePool>();
            StoragePool pool = null;
            if (toBeCreated.getPoolId() != null) {
                pool = (StoragePool) dataStoreMgr.getPrimaryDataStore(toBeCreated.getPoolId());
            } else {
                pool = (StoragePool) dataStoreMgr.getPrimaryDataStore(plan.getPoolId());
            }
            if (!pool.isInMaintenance()) {
                if (!avoid.shouldAvoid(pool)) {
                    long exstPoolDcId = pool.getDataCenterId();
                    long exstPoolPodId = pool.getPodId() != null ? pool.getPodId() : -1;
                    long exstPoolClusterId = pool.getClusterId() != null ? pool.getClusterId() : -1;
                    boolean canReusePool = false;
                    if (plan.getDataCenterId() == exstPoolDcId && plan.getPodId() == exstPoolPodId && plan.getClusterId() == exstPoolClusterId) {
                        canReusePool = true;
                    } else if (plan.getDataCenterId() == exstPoolDcId) {
                        DataStore dataStore = dataStoreMgr.getPrimaryDataStore(pool.getId());
                        if (dataStore != null && dataStore.getScope() != null && dataStore.getScope().getScopeType() == ScopeType.ZONE) {
                            canReusePool = true;
                        }
                    } else {
                        s_logger.debug("Pool of the volume does not fit the specified plan, need to reallocate a pool for this volume");
                        canReusePool = false;
                    }
                    if (canReusePool) {
                        s_logger.debug("Planner need not allocate a pool for this volume since its READY");
                        suitablePools.add(pool);
                        suitableVolumeStoragePools.put(toBeCreated, suitablePools);
                        if (!(toBeCreated.getState() == Volume.State.Allocated || toBeCreated.getState() == Volume.State.Creating)) {
                            readyAndReusedVolumes.add(toBeCreated);
                        }
                        continue;
                    }
                } else {
                    s_logger.debug("Pool of the volume is in avoid set, need to reallocate a pool for this volume");
                }
            } else {
                s_logger.debug("Pool of the volume is in maintenance, need to reallocate a pool for this volume");
            }
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("We need to allocate new storagepool for this volume");
        }
        if (!isRootAdmin(vmProfile)) {
            if (!isEnabledForAllocation(plan.getDataCenterId(), plan.getPodId(), plan.getClusterId())) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Cannot allocate new storagepool for this volume in this cluster, allocation state is disabled");
                    s_logger.debug("Cannot deploy to this specified plan, allocation state is disabled, returning.");
                }
                // Cannot find suitable storage pools under this cluster for
                // this volume since allocation_state is disabled.
                // - remove any suitable pools found for other volumes.
                // All volumes should get suitable pools under this cluster;
                // else we cant use this cluster.
                suitableVolumeStoragePools.clear();
                break;
            }
        }
        s_logger.debug("Calling StoragePoolAllocators to find suitable pools");
        DiskOfferingVO diskOffering = _diskOfferingDao.findById(toBeCreated.getDiskOfferingId());
        // FR123 check how is it different for service offering getTagsArray and disk offering's
        // if ((vmProfile.getTemplate().getFormat() == Storage.ImageFormat.ISO || toBeCreated.getVolumeType() == Volume.Type.ROOT)
        // && vmProfile.getServiceOffering().getTagsArray().length != 0) {
        // diskOffering.setTagsArray(Arrays.asList(vmProfile.getServiceOffering().getTagsArray()));
        // }
        DiskProfile diskProfile = new DiskProfile(toBeCreated, diskOffering, vmProfile.getHypervisorType());
        boolean useLocalStorage = false;
        if (vmProfile.getType() != VirtualMachine.Type.User) {
            DataCenterVO zone = _dcDao.findById(plan.getDataCenterId());
            assert (zone != null) : "Invalid zone in deployment plan";
            Boolean useLocalStorageForSystemVM = ConfigurationManagerImpl.SystemVMUseLocalStorage.valueIn(zone.getId());
            if (useLocalStorageForSystemVM != null) {
                useLocalStorage = useLocalStorageForSystemVM.booleanValue();
                s_logger.debug("System VMs will use " + (useLocalStorage ? "local" : "shared") + " storage for zone id=" + plan.getDataCenterId());
            }
        } else {
            useLocalStorage = diskOffering.isUseLocalStorage();
        }
        diskProfile.setUseLocalStorage(useLocalStorage);
        boolean foundPotentialPools = false;
        for (StoragePoolAllocator allocator : _storagePoolAllocators) {
            final List<StoragePool> suitablePools = allocator.allocateToPool(diskProfile, vmProfile, plan, avoid, returnUpTo);
            if (suitablePools != null && !suitablePools.isEmpty()) {
                checkForPreferredStoragePool(suitablePools, vmProfile.getVirtualMachine(), suitableVolumeStoragePools, toBeCreated);
                foundPotentialPools = true;
                break;
            }
        }
        if (avoid.getPoolsToAvoid() != null) {
            poolsToAvoidOutput.addAll(avoid.getPoolsToAvoid());
            avoid.getPoolsToAvoid().retainAll(originalAvoidPoolSet);
        }
        if (!foundPotentialPools) {
            s_logger.debug("No suitable pools found for volume: " + toBeCreated + " under cluster: " + plan.getClusterId());
            // No suitable storage pools found under this cluster for this
            // volume. - remove any suitable pools found for other volumes.
            // All volumes should get suitable pools under this cluster;
            // else we cant use this cluster.
            suitableVolumeStoragePools.clear();
            break;
        }
    }
    HashSet<Long> toRemove = new HashSet<Long>();
    for (List<StoragePool> lsp : suitableVolumeStoragePools.values()) {
        for (StoragePool sp : lsp) {
            toRemove.add(sp.getId());
        }
    }
    poolsToAvoidOutput.removeAll(toRemove);
    if (avoid.getPoolsToAvoid() != null) {
        avoid.getPoolsToAvoid().addAll(poolsToAvoidOutput);
    }
    if (suitableVolumeStoragePools.isEmpty()) {
        s_logger.debug("No suitable pools found");
    }
    return new Pair<Map<Volume, List<StoragePool>>, List<Volume>>(suitableVolumeStoragePools, readyAndReusedVolumes);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) StoragePool(com.cloud.storage.StoragePool) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DiskProfile(com.cloud.vm.DiskProfile) VolumeVO(com.cloud.storage.VolumeVO) Volume(com.cloud.storage.Volume) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) StoragePoolAllocator(org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator) HashSet(java.util.HashSet) Pair(com.cloud.utils.Pair)

Example 17 with StoragePoolAllocator

use of org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator in project cloudstack by apache.

the class StorageAllocatorTest method testCLOUDSTACK3481.

@Test
public void testCLOUDSTACK3481() {
    try {
        createDb();
        StoragePoolVO pool = storagePoolDao.findById(storagePoolId);
        pool.setHypervisor(HypervisorType.KVM);
        pool.setScope(ScopeType.ZONE);
        pool.setClusterId(null);
        pool.setPodId(null);
        storagePoolDao.update(pool.getId(), pool);
        DiskProfile profile = new DiskProfile(volume, diskOffering, HypervisorType.KVM);
        VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class);
        Account account = Mockito.mock(Account.class);
        Mockito.when(account.getAccountId()).thenReturn(1L);
        Mockito.when(vmProfile.getHypervisorType()).thenReturn(HypervisorType.KVM);
        Mockito.when(vmProfile.getOwner()).thenReturn(account);
        Mockito.when(storageMgr.storagePoolHasEnoughSpace(Matchers.anyListOf(Volume.class), Matchers.any(StoragePool.class))).thenReturn(true);
        Mockito.when(storageMgr.storagePoolHasEnoughIops(Matchers.anyListOf(Volume.class), Matchers.any(StoragePool.class))).thenReturn(true);
        DeploymentPlan plan = new DataCenterDeployment(dcId, podId, clusterId, null, null, null);
        int foundAcct = 0;
        for (StoragePoolAllocator allocator : allocators) {
            List<StoragePool> pools = allocator.allocateToPool(profile, vmProfile, plan, new ExcludeList(), 1);
            if (!pools.isEmpty()) {
                Assert.assertEquals(pools.get(0).getId(), storage.getId());
                foundAcct++;
            }
        }
        if (foundAcct > 1 || foundAcct == 0) {
            Assert.fail();
        }
    } catch (Exception e) {
        cleanDb();
        Assert.fail();
    }
}
Also used : ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) Account(com.cloud.user.Account) StoragePool(com.cloud.storage.StoragePool) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) DiskProfile(com.cloud.vm.DiskProfile) Volume(com.cloud.storage.Volume) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) DeploymentPlan(com.cloud.deploy.DeploymentPlan) StoragePoolAllocator(org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator) Test(org.junit.Test)

Example 18 with StoragePoolAllocator

use of org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator in project cloudstack by apache.

the class StorageAllocatorTest method testClusterAllocatorWithWrongTag.

@Test
public void testClusterAllocatorWithWrongTag() {
    try {
        createDb();
        StoragePoolDetailVO detailVO = new StoragePoolDetailVO(this.storagePoolId, "high", "true", true);
        poolDetailsDao.persist(detailVO);
        DiskOfferingVO diskOff = this.diskOfferingDao.findById(diskOffering.getId());
        List<String> tags = new ArrayList<String>();
        tags.add("low");
        diskOff.setTagsArray(tags);
        diskOfferingDao.update(diskOff.getId(), diskOff);
        DiskProfile profile = new DiskProfile(volume, diskOff, HypervisorType.XenServer);
        VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class);
        Mockito.when(storageMgr.storagePoolHasEnoughSpace(Matchers.anyListOf(Volume.class), Matchers.any(StoragePool.class))).thenReturn(true);
        DeploymentPlan plan = new DataCenterDeployment(dcId, podId, clusterId, null, null, null);
        int foundAcct = 0;
        for (StoragePoolAllocator allocator : allocators) {
            List<StoragePool> pools = allocator.allocateToPool(profile, vmProfile, plan, new ExcludeList(), 1);
            if (!pools.isEmpty()) {
                foundAcct++;
            }
        }
        if (foundAcct != 0) {
            Assert.fail();
        }
    } catch (Exception e) {
        cleanDb();
        Assert.fail();
    }
}
Also used : ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) StoragePool(com.cloud.storage.StoragePool) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) ArrayList(java.util.ArrayList) StoragePoolDetailVO(org.apache.cloudstack.storage.datastore.db.StoragePoolDetailVO) DiskProfile(com.cloud.vm.DiskProfile) Volume(com.cloud.storage.Volume) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) DeploymentPlan(com.cloud.deploy.DeploymentPlan) StoragePoolAllocator(org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator) Test(org.junit.Test)

Example 19 with StoragePoolAllocator

use of org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator in project cloudstack by apache.

the class StorageAllocatorTest method testPoolStateIsNotUp.

@Test
public void testPoolStateIsNotUp() {
    try {
        createDb();
        StoragePoolVO pool = storagePoolDao.findById(storagePoolId);
        pool.setScope(ScopeType.ZONE);
        pool.setStatus(StoragePoolStatus.Maintenance);
        storagePoolDao.update(pool.getId(), pool);
        DiskProfile profile = new DiskProfile(volume, diskOffering, HypervisorType.XenServer);
        VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class);
        Mockito.when(storageMgr.storagePoolHasEnoughSpace(Matchers.anyListOf(Volume.class), Matchers.any(StoragePool.class))).thenReturn(true);
        DeploymentPlan plan = new DataCenterDeployment(dcId, podId, clusterId, null, null, null);
        int foundAcct = 0;
        for (StoragePoolAllocator allocator : allocators) {
            List<StoragePool> pools = allocator.allocateToPool(profile, vmProfile, plan, new ExcludeList(), 1);
            if (!pools.isEmpty()) {
                Assert.assertEquals(pools.get(0).getId(), storage.getId());
                foundAcct++;
            }
        }
        if (foundAcct == 1) {
            Assert.fail();
        }
    } catch (Exception e) {
        cleanDb();
        Assert.fail();
    }
}
Also used : ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) StoragePool(com.cloud.storage.StoragePool) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) Volume(com.cloud.storage.Volume) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) DeploymentPlan(com.cloud.deploy.DeploymentPlan) DiskProfile(com.cloud.vm.DiskProfile) StoragePoolAllocator(org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator) Test(org.junit.Test)

Aggregations

StoragePoolAllocator (org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator)19 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)17 StoragePool (com.cloud.storage.StoragePool)17 DataCenterDeployment (com.cloud.deploy.DataCenterDeployment)14 VirtualMachineProfile (com.cloud.vm.VirtualMachineProfile)12 DiskProfile (com.cloud.vm.DiskProfile)11 Volume (com.cloud.storage.Volume)10 DeploymentPlan (com.cloud.deploy.DeploymentPlan)9 DiskOfferingVO (com.cloud.storage.DiskOfferingVO)9 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)9 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)8 VirtualMachineProfileImpl (com.cloud.vm.VirtualMachineProfileImpl)4 Cluster (com.cloud.org.Cluster)3 VolumeVO (com.cloud.storage.VolumeVO)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 HashMap (java.util.HashMap)2 StoragePoolDetailVO (org.apache.cloudstack.storage.datastore.db.StoragePoolDetailVO)2 DataCenterVO (com.cloud.dc.DataCenterVO)1 Account (com.cloud.user.Account)1