Search in sources :

Example 21 with AffinityGroup

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());
}
Also used : AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup) Test(org.junit.Test)

Example 22 with AffinityGroup

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;
}
Also used : HashMap(java.util.HashMap) Guid(org.ovirt.engine.core.compat.Guid) AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup) HashSet(java.util.HashSet)

Example 23 with AffinityGroup

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();
}
Also used : VM(org.ovirt.engine.core.common.businessentities.VM) Guid(org.ovirt.engine.core.compat.Guid) AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup)

Example 24 with AffinityGroup

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;
}
Also used : VdsStatic(org.ovirt.engine.core.common.businessentities.VdsStatic) Guid(org.ovirt.engine.core.compat.Guid) AffinityRulesUtils(org.ovirt.engine.core.bll.scheduling.arem.AffinityRulesUtils) ArrayList(java.util.ArrayList) AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup) VmStaticDao(org.ovirt.engine.core.dao.VmStaticDao) HashSet(java.util.HashSet) Inject(javax.inject.Inject) CommandContext(org.ovirt.engine.core.bll.context.CommandContext) Map(java.util.Map) AffinityGroupDao(org.ovirt.engine.core.dao.scheduling.AffinityGroupDao) AffinityGroupCRUDParameters(org.ovirt.engine.core.common.scheduling.parameters.AffinityGroupCRUDParameters) VmStatic(org.ovirt.engine.core.common.businessentities.VmStatic) Iterator(java.util.Iterator) VmBase(org.ovirt.engine.core.common.businessentities.VmBase) AuditLogDirector(org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogDirector) EngineMessage(org.ovirt.engine.core.common.errors.EngineMessage) Set(java.util.Set) CommandBase(org.ovirt.engine.core.bll.CommandBase) Collectors(java.util.stream.Collectors) VdsStaticDao(org.ovirt.engine.core.dao.VdsStaticDao) Objects(java.util.Objects) List(java.util.List) PermissionSubject(org.ovirt.engine.core.bll.utils.PermissionSubject) VdcObjectType(org.ovirt.engine.core.common.VdcObjectType) Collections(java.util.Collections) ArrayList(java.util.ArrayList) AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup) EngineMessage(org.ovirt.engine.core.common.errors.EngineMessage) AffinityRulesUtils(org.ovirt.engine.core.bll.scheduling.arem.AffinityRulesUtils)

Example 25 with AffinityGroup

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));
}
Also used : AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup) Test(org.junit.Test)

Aggregations

AffinityGroup (org.ovirt.engine.core.common.scheduling.AffinityGroup)49 Test (org.junit.Test)20 Guid (org.ovirt.engine.core.compat.Guid)12 ArrayList (java.util.ArrayList)11 VM (org.ovirt.engine.core.common.businessentities.VM)11 HashSet (java.util.HashSet)9 HashMap (java.util.HashMap)7 List (java.util.List)5 Map (java.util.Map)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 Inject (javax.inject.Inject)5 PerHostMessages (org.ovirt.engine.core.common.scheduling.PerHostMessages)5 AffinityGroupDao (org.ovirt.engine.core.dao.scheduling.AffinityGroupDao)5 Cluster (org.ovirt.engine.core.common.businessentities.Cluster)4 VDS (org.ovirt.engine.core.common.businessentities.VDS)4 AffinityGroupCRUDParameters (org.ovirt.engine.core.common.scheduling.parameters.AffinityGroupCRUDParameters)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 Collections (java.util.Collections)3