use of org.ovirt.engine.core.common.scheduling.AffinityGroup in project ovirt-engine by oVirt.
the class AffinityGroupDaoTest method testRemoveVdsFromExistingAffinityGroup.
@Test
public void testRemoveVdsFromExistingAffinityGroup() {
AffinityGroup existing = dao.get(FixturesTool.EXISTING_AFFINITY_GROUP_ID);
assertFalse(existing.getVdsEntityNames().isEmpty());
existing.getVdsIds().clear();
dao.update(existing);
AffinityGroup fetched = dao.get(existing.getId());
assertTrue(fetched.getVdsEntityNames().isEmpty());
}
use of org.ovirt.engine.core.common.scheduling.AffinityGroup in project ovirt-engine by oVirt.
the class AffinityRulesEnforcer method checkForVMAffinityGroupViolations.
/**
* Detect whether the current VM to VDS assignment violates current Affinity Groups.
*
* @param affinityGroups Unified affinity groups
* @param vmToHost Mapping of VM to currently assigned VDS
* @return broken AffinityGroups
*/
protected static Set<AffinityGroup> checkForVMAffinityGroupViolations(Iterable<AffinityGroup> affinityGroups, Map<Guid, Guid> vmToHost, FailMode mode) {
Set<AffinityGroup> broken = new HashSet<>();
for (AffinityGroup affinity : affinityGroups) {
// Negative groups
if (affinity.isVmNegative()) {
// Record all hosts that are already occupied by VMs from this group
Map<Guid, Guid> usedHosts = new HashMap<>();
for (Guid vm : affinity.getVmIds()) {
Guid host = vmToHost.get(vm);
if (host == null) {
continue;
}
// Report a violation when any host has more than one VM from this group
if (usedHosts.containsKey(host)) {
log.debug("Negative affinity rule violated between VMs {} and {} on host {}", vm, usedHosts.get(host), host);
broken.add(affinity);
if (mode.equals(FailMode.IMMEDIATELY)) {
return broken;
}
} else {
usedHosts.put(host, vm);
}
}
// Positive groups
} else if (affinity.isVmPositive()) {
// All VMs from this group have to be running on a single host
Guid targetHost = null;
for (Guid vm : affinity.getVmIds()) {
Guid host = vmToHost.get(vm);
if (host == null) {
continue;
}
// Report a violation when two VMs do not share a common host
if (targetHost != null && !targetHost.equals(host)) {
log.debug("Positive affinity rule violated by VM {} running at {} when other VM(s) are at {}", vm, host, targetHost);
broken.add(affinity);
if (mode.equals(FailMode.IMMEDIATELY)) {
return broken;
}
} else if (targetHost == null) {
targetHost = host;
}
}
}
}
return broken;
}
use of org.ovirt.engine.core.common.scheduling.AffinityGroup in project ovirt-engine by oVirt.
the class AffinityRulesEnforcer method chooseNextVmToMigrateFromVMsToHostsAffinity.
/**
* Choose a VM to migrate by applying VM to host affinity rules.
* Candidate VMs will selected in the following order:
* <p>
* 1.Candidate VMs violating enforcing affinity to hosts.
* 2.Candidate VMs violating non enforcing affinity to hosts.
*
* @param cluster Current cluster
* @param allAffinityGroups All affinity groups for the current cluster.
* @return Valid VM for migration by VM to host affinity, empty result otherwise
*/
private Optional<VM> chooseNextVmToMigrateFromVMsToHostsAffinity(Cluster cluster, List<AffinityGroup> allAffinityGroups) {
List<AffinityGroup> allVmToHostsAffinityGroups = getAllAffinityGroupsForVMsToHostsAffinity(allAffinityGroups);
if (allVmToHostsAffinityGroups.isEmpty()) {
return Optional.empty();
}
Map<Guid, VM> vmsMap = getRunningVMsMap(allVmToHostsAffinityGroups);
List<Guid> candidateVMs = getVmToHostsAffinityGroupCandidates(allVmToHostsAffinityGroups, vmsMap, true);
if (candidateVMs.isEmpty()) {
log.debug("No vm to hosts hard-affinity group violation detected");
} else {
List<AffinityRulesUtils.AffinityGroupConflicts> conflicts = AffinityRulesUtils.checkForAffinityGroupHostsConflict(allVmToHostsAffinityGroups);
for (AffinityRulesUtils.AffinityGroupConflicts conflict : conflicts) {
if (conflict.isVmToVmAffinity()) {
log.warn(conflict.getType().getMessage(), conflict.getVms().stream().map(id -> id.toString()).collect(Collectors.joining(",")), AffinityRulesUtils.getAffinityGroupsNames(conflict.getAffinityGroups()), conflict.getNegativeVms().stream().map(id -> id.toString()).collect(Collectors.joining(",")));
} else {
log.warn(conflict.getType().getMessage(), AffinityRulesUtils.getAffinityGroupsNames(conflict.getAffinityGroups()), conflict.getHosts().stream().map(id -> id.toString()).collect(Collectors.joining(",")), conflict.getVms().stream().map(id -> id.toString()).collect(Collectors.joining(",")));
}
}
}
for (Guid id : candidateVMs) {
VM candidateVM = vmsMap.get(id);
if (isVmMigrationValid(cluster, candidateVM)) {
return Optional.of(candidateVM);
}
}
candidateVMs = getVmToHostsAffinityGroupCandidates(allVmToHostsAffinityGroups, vmsMap, false);
if (candidateVMs.isEmpty()) {
log.debug("No vm to hosts soft-affinity group violation detected");
}
for (Guid id : candidateVMs) {
VM candidateVM = vmsMap.get(id);
if (isVmMigrationValid(cluster, candidateVM)) {
return Optional.of(candidateVM);
}
}
return Optional.empty();
}
use of org.ovirt.engine.core.common.scheduling.AffinityGroup in project ovirt-engine by oVirt.
the class AffinityGroupCRUDCommand method affinityGroupsWithoutConflict.
private boolean affinityGroupsWithoutConflict(AffinityGroup affinityGroup) {
List<AffinityGroup> affinityGroups = affinityGroupDao.getAllAffinityGroupsByClusterId(affinityGroup.getClusterId());
// Replace the existing affinity group by the updated copy
for (Iterator<AffinityGroup> it = affinityGroups.iterator(); it.hasNext(); ) {
AffinityGroup g = it.next();
if (g.getId().equals(affinityGroup.getId())) {
it.remove();
}
}
affinityGroups.add(affinityGroup);
List<AffinityRulesUtils.AffinityGroupConflicts> conflicts = AffinityRulesUtils.checkForAffinityGroupHostsConflict(affinityGroups);
for (AffinityRulesUtils.AffinityGroupConflicts conflict : conflicts) {
String affinityGroupsNames = AffinityRulesUtils.getAffinityGroupsNames(conflict.getAffinityGroups());
String hosts = conflict.getHosts().stream().map(id -> id.toString()).collect(Collectors.joining(","));
String vms = conflict.getVms().stream().map(id -> id.toString()).collect(Collectors.joining(","));
if (conflict.getType().canBeSaved()) {
addCustomValue("AffinityGroups", affinityGroupsNames);
addCustomValue("Hosts", hosts);
addCustomValue("Vms", vms);
auditLogDirector.log(this, conflict.getAuditLogType());
} else {
if (conflict.isVmToVmAffinity()) {
return failValidation(EngineMessage.ACTION_TYPE_FAILED_AFFINITY_RULES_COLLISION, String.format("$UnifiedAffinityGroups %1$s", vms), String.format("$negativeAR %1$s", affinityGroupsNames), String.format("$Vms %1$s", conflict.getNegativeVms().stream().map(id -> id.toString()).collect(Collectors.joining(","))));
} else {
List<EngineMessage> engineMessages = new ArrayList<>();
engineMessages.add(EngineMessage.ACTION_TYPE_FAILED_AFFINITY_HOSTS_RULES_COLLISION);
engineMessages.add(EngineMessage.AFFINITY_GROUPS_LIST);
engineMessages.add(EngineMessage.HOSTS_LIST);
engineMessages.add(EngineMessage.VMS_LIST);
List<String> variableReplacements = new ArrayList<>();
variableReplacements.add(String.format("$affinityGroups %1$s", affinityGroupsNames));
variableReplacements.add(String.format("$hostsList %1$s", hosts));
variableReplacements.add(String.format("$vmsList %1$s", vms));
return failValidation(engineMessages, variableReplacements);
}
}
}
return true;
}
use of org.ovirt.engine.core.common.scheduling.AffinityGroup in project ovirt-engine by oVirt.
the class AffinityGroupDaoTest method testSave.
@Test
public void testSave() {
AffinityGroup ag = new AffinityGroup();
ag.setId(Guid.newGuid());
ag.setName("testAG");
ag.setDescription("desc");
ag.setClusterId(FixturesTool.CLUSTER_RHEL6_NFS);
ag.setVmEnforcing(false);
ag.setVmAffinityRule(EntityAffinityRule.NEGATIVE);
ag.setVdsEnforcing(false);
ag.setVdsAffinityRule(EntityAffinityRule.POSITIVE);
ag.setVmIds(new ArrayList<>());
ag.getVmIds().add(FixturesTool.VM_RHEL5_POOL_50);
ag.setVmEntityNames(new ArrayList<>());
ag.getVmEntityNames().add(FixturesTool.VM_RHEL5_POOL_50_NAME);
ag.setVdsIds(new ArrayList<>());
ag.getVdsIds().add(FixturesTool.VDS_RHEL6_NFS_SPM);
ag.setVdsEntityNames(new ArrayList<>());
ag.getVdsEntityNames().add(FixturesTool.GLUSTER_SERVER_NAME3);
dao.save(ag);
AffinityGroup fetched = dao.get(ag.getId());
assertTrue(equals(ag, fetched));
}
Aggregations