Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.Set;
import java.util.Timer;
import java.util.TreeSet;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.inject.Inject;
Expand Down Expand Up @@ -94,6 +96,7 @@
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
import com.cloud.deploy.DeploymentPlanner.PlannerResourceUsage;
import com.cloud.deploy.dao.PlannerHostReservationDao;
import com.cloud.domain.dao.DomainDao;
import com.cloud.exception.AffinityConflictException;
import com.cloud.exception.ConnectionException;
import com.cloud.exception.InsufficientServerCapacityException;
Expand Down Expand Up @@ -176,6 +179,8 @@ public class DeploymentPlanningManagerImpl extends ManagerBase implements Deploy
@Inject
protected AffinityGroupDomainMapDao _affinityGroupDomainMapDao;
@Inject
protected DomainDao _domainDao;
@Inject
AffinityGroupService _affinityGroupService;
@Inject
DataCenterDao _dcDao;
Expand All @@ -199,6 +204,7 @@ public class DeploymentPlanningManagerImpl extends ManagerBase implements Deploy

private static final long ADMIN_ACCOUNT_ROLE_ID = 1l;
private static final long INITIAL_RESERVATION_RELEASE_CHECKER_DELAY = 30L * 1000L; // thirty seconds expressed in milliseconds
private static final String EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE = "ExplicitDedication";
protected long _nodeId = -1;
protected static final List<HypervisorType> DEPLOYMENT_PLANNING_SKIP_HYPERVISORS = Arrays.asList(
HypervisorType.BareMetal, HypervisorType.External);
Expand Down Expand Up @@ -977,7 +983,7 @@ public void checkForNonDedicatedResources(VirtualMachineProfile vmProfile, DataC

// 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");
List<AffinityGroupVMMapVO> vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE);

if (vmGroupMappings != null && !vmGroupMappings.isEmpty()) {
isExplicit = true;
Expand Down Expand Up @@ -1083,13 +1089,85 @@ private void findAvoiSetForRouterVM(ExcludeList avoids, VirtualMachine vm, List<
}

private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine vm, List<Long> allPodsInDc, List<Long> allClustersInDc, List<Long> allHostsInDc) {
if (allPodsInDc.isEmpty() && allClustersInDc.isEmpty() && allHostsInDc.isEmpty()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pod, cluster and host blocks are identical except for the DAO call (searchDedicatedPods/Clusters/Hosts) and the id getter (getPodId/getClusterId/getHostId). Could we extract them into one helper that takes the search call and id getter as parameters?
Something like:

removeDedicatedIdsAccessibleToVm(allPodsInDc, vmDomainId, vmAccountId, hasDomainExplicitDedicationGroup,
        (domainId, accountId) -> _dedicatedDao.searchDedicatedPods(null, domainId, accountId, null, filter),
        DedicatedResourceVO::getPodId);

// Nothing in this DC is dedicated to anyone, so there is nothing to avoid or to check dedication for.
return;
}

long vmAccountId = vm.getAccountId();
long vmDomainId = vm.getDomainId();

// Domains whose domain-level dedications the VM owner may use: its own domain and the ancestors
// whose ExplicitDedication group grants subdomain access.
Set<Long> accessibleDedicationDomainIds = findDomainsWithAccessibleExplicitDedication(vmDomainId);

// Sort by id ascending, no pagination — retrieve all dedicated resources for this domain/account
Filter filter = new Filter(DedicatedResourceVO.class, "id", true);

// Only query the resource types that have dedicated entries in this DC.
removeDedicatedIdsAccessibleToVm(allPodsInDc, vmDomainId, vmAccountId, accessibleDedicationDomainIds,
(domainId, accountId) -> _dedicatedDao.searchDedicatedPods(null, domainId, accountId, null, filter),
DedicatedResourceVO::getPodId);
removeDedicatedIdsAccessibleToVm(allClustersInDc, vmDomainId, vmAccountId, accessibleDedicationDomainIds,
(domainId, accountId) -> _dedicatedDao.searchDedicatedClusters(null, domainId, accountId, null, filter),
DedicatedResourceVO::getClusterId);
removeDedicatedIdsAccessibleToVm(allHostsInDc, vmDomainId, vmAccountId, accessibleDedicationDomainIds,
(domainId, accountId) -> _dedicatedDao.searchDedicatedHosts(null, domainId, accountId, null, filter),
DedicatedResourceVO::getHostId);

logger.debug(() -> LogUtils.logGsonWithoutException("Adding pods [%s], clusters [%s] and hosts [%s] to the avoid list in the deploy process of user VM [%s], "
+ "because this VM is not explicitly dedicated to these components.", allPodsInDc, allClustersInDc, allHostsInDc, vm));
+ "because this VM is not dedicated to these components.", allPodsInDc, allClustersInDc, allHostsInDc, vm));
avoids.addPodList(allPodsInDc);
avoids.addClusterList(allClustersInDc);
avoids.addHostList(allHostsInDc);
}

/**
* Returns the domains whose domain-level dedications are available to a VM owned in {@code vmDomainId}: the
* VM's own domain if it has an ExplicitDedication domain-level affinity group, and each ancestor domain whose
* ExplicitDedication group grants subdomain access (as in {@code AffinityGroupService.isAffinityGroupAvailableInDomain}).
*/
protected Set<Long> findDomainsWithAccessibleExplicitDedication(long vmDomainId) {
Set<Long> accessibleDomainIds = new HashSet<>();
for (Long domainId : _domainDao.getDomainParentIds(vmDomainId)) {
AffinityGroupVO group = _affinityGroupDao.findDomainLevelGroupByType(domainId, EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE);
if (group == null) {
continue;
}
if (domainId == vmDomainId) {
accessibleDomainIds.add(domainId);
continue;
}
AffinityGroupDomainMapVO domainMap = _affinityGroupDomainMapDao.findByAffinityGroup(group.getId());
if (domainMap != null && Boolean.TRUE.equals(domainMap.isSubdomainAccess())) {
accessibleDomainIds.add(domainId);
}
}
return accessibleDomainIds;
}

/**
* Removes from {@code dedicatedIdsInDc} the ids of the resources the VM owner may use: those dedicated to the
* owner's account and those dedicated (without an account) to any of {@code accessibleDedicationDomainIds}.
* Resources dedicated to other accounts, in the VM's domain or an ancestor, stay in the list.
*/
private void removeDedicatedIdsAccessibleToVm(List<Long> dedicatedIdsInDc, long vmDomainId, long vmAccountId, Set<Long> accessibleDedicationDomainIds,
BiFunction<Long, Long, Pair<List<DedicatedResourceVO>, Integer>> searchDedicated, Function<DedicatedResourceVO, Long> idGetter) {
if (dedicatedIdsInDc.isEmpty()) {
return;
}
Set<Long> allowedIds = new HashSet<>();
for (DedicatedResourceVO vo : searchDedicated.apply(vmDomainId, vmAccountId).first()) {
allowedIds.add(idGetter.apply(vo));
}
for (Long domainId : accessibleDedicationDomainIds) {
for (DedicatedResourceVO vo : searchDedicated.apply(domainId, null).first()) {
allowedIds.add(idGetter.apply(vo));
}
}
dedicatedIdsInDc.removeAll(allowedIds);
}

private void resetAvoidSet(ExcludeList avoidSet, ExcludeList removeSet) {
if (avoidSet.getDataCentersToAvoid() != null && removeSet.getDataCentersToAvoid() != null) {
avoidSet.getDataCentersToAvoid().removeAll(removeSet.getDataCentersToAvoid());
Expand Down
Loading
Loading