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 @@ -199,6 +201,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 +980,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 +1086,61 @@ 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();

// If the VM owner's domain has an ExplicitDedication domain-level affinity group,
// resources dedicated to that domain are accessible to the VM owner as well.
boolean hasDomainExplicitDedicationGroup =
_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE) != null;
Comment on lines +1099 to +1100

// 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, hasDomainExplicitDedicationGroup,
(domainId, accountId) -> _dedicatedDao.searchDedicatedPods(null, domainId, accountId, null, filter),
DedicatedResourceVO::getPodId);
removeDedicatedIdsAccessibleToVm(allClustersInDc, vmDomainId, vmAccountId, hasDomainExplicitDedicationGroup,
(domainId, accountId) -> _dedicatedDao.searchDedicatedClusters(null, domainId, accountId, null, filter),
DedicatedResourceVO::getClusterId);
removeDedicatedIdsAccessibleToVm(allHostsInDc, vmDomainId, vmAccountId, hasDomainExplicitDedicationGroup,
(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);
}

/**
* Removes from {@code dedicatedIdsInDc} the ids of the resources the VM owner may use: those dedicated to the
* owner's account and, if {@code includeDomainDedications} is set, those dedicated to the owner's domain.
*/
private void removeDedicatedIdsAccessibleToVm(List<Long> dedicatedIdsInDc, long vmDomainId, long vmAccountId, boolean includeDomainDedications,
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));
}
if (includeDomainDedications) {
for (DedicatedResourceVO vo : searchDedicated.apply(vmDomainId, 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