Search in sources :

Example 1 with AffinityGroupVMMapVO

use of org.apache.cloudstack.affinity.AffinityGroupVMMapVO in project cloudstack by apache.

the class DeploymentPlanningManagerImpl method checkForNonDedicatedResources.

private void checkForNonDedicatedResources(VirtualMachineProfile vmProfile, DataCenter dc, ExcludeList avoids) {
    boolean isExplicit = false;
    VirtualMachine vm = vmProfile.getVirtualMachine();
    // check if zone is dedicated. if yes check if vm owner has access to it.
    DedicatedResourceVO dedicatedZone = _dedicatedDao.findByZoneId(dc.getId());
    if (dedicatedZone != null && !_accountMgr.isRootAdmin(vmProfile.getOwner().getId())) {
        long accountDomainId = vmProfile.getOwner().getDomainId();
        long accountId = vmProfile.getOwner().getAccountId();
        // account will be to use explicit dedication affinity group.
        if (dedicatedZone.getAccountId() != null) {
            if (dedicatedZone.getAccountId().equals(accountId)) {
                return;
            } else {
                throw new CloudRuntimeException("Failed to deploy VM, Zone " + dc.getName() + " not available for the user account " + vmProfile.getOwner());
            }
        }
        // domain level dedication group
        if (!_affinityGroupService.isAffinityGroupAvailableInDomain(dedicatedZone.getAffinityGroupId(), accountDomainId)) {
            throw new CloudRuntimeException("Failed to deploy VM, Zone " + dc.getName() + " not available for the user domain " + vmProfile.getOwner());
        }
    }
    // check affinity group of type Explicit dedication exists. If No put
    // dedicated pod/cluster/host in avoid list
    List<AffinityGroupVMMapVO> vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), "ExplicitDedication");
    if (vmGroupMappings != null && !vmGroupMappings.isEmpty()) {
        isExplicit = true;
    }
    List<Long> allPodsInDc = _podDao.listAllPods(dc.getId());
    List<Long> allDedicatedPods = _dedicatedDao.listAllPods();
    allPodsInDc.retainAll(allDedicatedPods);
    List<Long> allClustersInDc = _clusterDao.listAllClusters(dc.getId());
    List<Long> allDedicatedClusters = _dedicatedDao.listAllClusters();
    allClustersInDc.retainAll(allDedicatedClusters);
    List<Long> allHostsInDc = _hostDao.listAllHosts(dc.getId());
    List<Long> allDedicatedHosts = _dedicatedDao.listAllHosts();
    allHostsInDc.retainAll(allDedicatedHosts);
    //Only when the type is instance VM and not explicitly dedicated.
    if (vm.getType() == VirtualMachine.Type.User && !isExplicit) {
        //add explicitly dedicated resources in avoidList
        avoids.addPodList(allPodsInDc);
        avoids.addClusterList(allClustersInDc);
        avoids.addHostList(allHostsInDc);
    }
    //No need to check the isExplicit. As both the cases are handled.
    if (vm.getType() == VirtualMachine.Type.DomainRouter) {
        long vmAccountId = vm.getAccountId();
        long vmDomainId = vm.getDomainId();
        //Lists all explicitly dedicated resources from vm account ID or domain ID.
        List<Long> allPodsFromDedicatedID = new ArrayList<Long>();
        List<Long> allClustersFromDedicatedID = new ArrayList<Long>();
        List<Long> allHostsFromDedicatedID = new ArrayList<Long>();
        //Whether the dedicated resources belong to Domain or not. If not, it may belongs to Account or no dedication.
        List<AffinityGroupDomainMapVO> domainGroupMappings = _affinityGroupDomainMapDao.listByDomain(vmDomainId);
        //For temporary storage and indexing.
        List<DedicatedResourceVO> tempStorage;
        if (domainGroupMappings == null || domainGroupMappings.isEmpty()) {
            //The dedicated resource belongs to VM Account ID.
            tempStorage = _dedicatedDao.searchDedicatedPods(null, vmDomainId, vmAccountId, null).first();
            for (DedicatedResourceVO vo : tempStorage) {
                allPodsFromDedicatedID.add(vo.getPodId());
            }
            tempStorage.clear();
            tempStorage = _dedicatedDao.searchDedicatedClusters(null, vmDomainId, vmAccountId, null).first();
            for (DedicatedResourceVO vo : tempStorage) {
                allClustersFromDedicatedID.add(vo.getClusterId());
            }
            tempStorage.clear();
            tempStorage = _dedicatedDao.searchDedicatedHosts(null, vmDomainId, vmAccountId, null).first();
            for (DedicatedResourceVO vo : tempStorage) {
                allHostsFromDedicatedID.add(vo.getHostId());
            }
            //Remove the dedicated ones from main list
            allPodsInDc.removeAll(allPodsFromDedicatedID);
            allClustersInDc.removeAll(allClustersFromDedicatedID);
            allHostsInDc.removeAll(allHostsFromDedicatedID);
        } else {
            //The dedicated resource belongs to VM Domain ID or No dedication.
            tempStorage = _dedicatedDao.searchDedicatedPods(null, vmDomainId, null, null).first();
            for (DedicatedResourceVO vo : tempStorage) {
                allPodsFromDedicatedID.add(vo.getPodId());
            }
            tempStorage.clear();
            tempStorage = _dedicatedDao.searchDedicatedClusters(null, vmDomainId, null, null).first();
            for (DedicatedResourceVO vo : tempStorage) {
                allClustersFromDedicatedID.add(vo.getClusterId());
            }
            tempStorage.clear();
            tempStorage = _dedicatedDao.searchDedicatedHosts(null, vmDomainId, null, null).first();
            for (DedicatedResourceVO vo : tempStorage) {
                allHostsFromDedicatedID.add(vo.getHostId());
            }
            //Remove the dedicated ones from main list
            allPodsInDc.removeAll(allPodsFromDedicatedID);
            allClustersInDc.removeAll(allClustersFromDedicatedID);
            allHostsInDc.removeAll(allHostsFromDedicatedID);
        }
        //Add in avoid list or no addition if no dedication
        avoids.addPodList(allPodsInDc);
        avoids.addClusterList(allClustersInDc);
        avoids.addHostList(allHostsInDc);
    }
}
Also used : AffinityGroupVMMapVO(org.apache.cloudstack.affinity.AffinityGroupVMMapVO) ArrayList(java.util.ArrayList) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) AffinityGroupDomainMapVO(org.apache.cloudstack.affinity.AffinityGroupDomainMapVO) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 2 with AffinityGroupVMMapVO

use of org.apache.cloudstack.affinity.AffinityGroupVMMapVO in project cloudstack by apache.

the class QueryManagerImpl method listAffinityGroupsByVM.

private Pair<List<AffinityGroupJoinVO>, Integer> listAffinityGroupsByVM(long vmId, long pageInd, long pageSize) {
    Filter sf = new Filter(SecurityGroupVMMapVO.class, null, true, pageInd, pageSize);
    Pair<List<AffinityGroupVMMapVO>, Integer> agVmMappingPair = _affinityGroupVMMapDao.listByInstanceId(vmId, sf);
    Integer count = agVmMappingPair.second();
    if (count.intValue() == 0) {
        // handle empty result cases
        return new Pair<List<AffinityGroupJoinVO>, Integer>(new ArrayList<AffinityGroupJoinVO>(), count);
    }
    List<AffinityGroupVMMapVO> agVmMappings = agVmMappingPair.first();
    Long[] agIds = new Long[agVmMappings.size()];
    int i = 0;
    for (AffinityGroupVMMapVO agVm : agVmMappings) {
        agIds[i++] = agVm.getAffinityGroupId();
    }
    List<AffinityGroupJoinVO> ags = _affinityGroupJoinDao.searchByIds(agIds);
    return new Pair<List<AffinityGroupJoinVO>, Integer>(ags, count);
}
Also used : AffinityGroupVMMapVO(org.apache.cloudstack.affinity.AffinityGroupVMMapVO) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) ArrayList(java.util.ArrayList) List(java.util.List) AffinityGroupJoinVO(com.cloud.api.query.vo.AffinityGroupJoinVO) Pair(com.cloud.utils.Pair)

Example 3 with AffinityGroupVMMapVO

use of org.apache.cloudstack.affinity.AffinityGroupVMMapVO in project cloudstack by apache.

the class AffinityGroupVMMapDaoImpl method updateMap.

@Override
public void updateMap(Long vmId, List<Long> affinityGroupIds) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    SearchCriteria<AffinityGroupVMMapVO> sc = createSearchCriteria();
    sc.addAnd("instanceId", SearchCriteria.Op.EQ, vmId);
    expunge(sc);
    for (Long groupId : affinityGroupIds) {
        AffinityGroupVMMapVO vo = new AffinityGroupVMMapVO(groupId, vmId);
        persist(vo);
    }
    txn.commit();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) AffinityGroupVMMapVO(org.apache.cloudstack.affinity.AffinityGroupVMMapVO)

Aggregations

AffinityGroupVMMapVO (org.apache.cloudstack.affinity.AffinityGroupVMMapVO)3 ArrayList (java.util.ArrayList)2 AffinityGroupJoinVO (com.cloud.api.query.vo.AffinityGroupJoinVO)1 DedicatedResourceVO (com.cloud.dc.DedicatedResourceVO)1 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)1 Pair (com.cloud.utils.Pair)1 Filter (com.cloud.utils.db.Filter)1 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 VirtualMachine (com.cloud.vm.VirtualMachine)1 List (java.util.List)1 AffinityGroupDomainMapVO (org.apache.cloudstack.affinity.AffinityGroupDomainMapVO)1