Search in sources :

Example 31 with AffinityGroup

use of org.ovirt.engine.core.common.scheduling.AffinityGroup in project ovirt-engine by oVirt.

the class AffinityGroupListModel method remove.

private void remove() {
    if (getWindow() != null) {
        return;
    }
    ConfirmationModel model = new ConfirmationModel();
    setConfirmWindow(model);
    model.setTitle(ConstantsManager.getInstance().getConstants().removeAffinityGroupsTitle());
    model.setHelpTag(HelpTag.remove_affinity_groups);
    // $NON-NLS-1$
    model.setHashName("remove_affinity_groups");
    ArrayList<String> list = new ArrayList<>();
    for (AffinityGroup affinityGroup : getSelectedItems()) {
        list.add(affinityGroup.getName());
    }
    model.setItems(list);
    // $NON-NLS-1$
    UICommand command = UICommand.createDefaultOkUiCommand("OnRemove", this);
    model.getCommands().add(command);
    // $NON-NLS-1$
    model.getCommands().add(UICommand.createCancelUiCommand("Cancel", this));
}
Also used : ArrayList(java.util.ArrayList) UICommand(org.ovirt.engine.ui.uicommonweb.UICommand) ConfirmationModel(org.ovirt.engine.ui.uicommonweb.models.ConfirmationModel) AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup)

Example 32 with AffinityGroup

use of org.ovirt.engine.core.common.scheduling.AffinityGroup in project ovirt-engine by oVirt.

the class AffinityRulesEnforcer method chooseNextVmToMigrateFromVMsAffinity.

private VM chooseNextVmToMigrateFromVMsAffinity(Cluster cluster, List<AffinityGroup> allAffinityGroups) {
    List<AffinityGroup> allHardAffinityGroups = getAllHardAffinityGroupsForVMsAffinity(allAffinityGroups);
    Set<Set<Guid>> unifiedPositiveAffinityGroups = AffinityRulesUtils.getUnifiedPositiveAffinityGroups(allHardAffinityGroups);
    List<AffinityGroup> unifiedAffinityGroups = AffinityRulesUtils.setsToAffinityGroups(unifiedPositiveAffinityGroups);
    // Add negative affinity groups
    for (AffinityGroup ag : allHardAffinityGroups) {
        if (ag.isVmNegative()) {
            unifiedAffinityGroups.add(ag);
        }
    }
    // Create a set of all VMs in affinity groups
    Set<Guid> allVms = new HashSet<>();
    for (AffinityGroup group : unifiedAffinityGroups) {
        allVms.addAll(group.getVmIds());
    }
    Map<Guid, VM> vmsMap = vmDao.getVmsByIds(new ArrayList<>(allVms)).stream().collect(Collectors.toMap(VM::getId, vm -> vm));
    Map<Guid, Guid> vmToHost = vmsMap.values().stream().filter(vm -> vm.getRunOnVds() != null).collect(Collectors.toMap(VM::getId, VM::getRunOnVds));
    // There is no need to migrate when no collision was detected
    Set<AffinityGroup> violatedAffinityGroups = checkForVMAffinityGroupViolations(unifiedAffinityGroups, vmToHost, FailMode.GET_ALL);
    if (violatedAffinityGroups.isEmpty()) {
        log.debug("No affinity group collision detected for cluster {}. Standing by.", cluster.getId());
        return null;
    }
    // Find a VM that is breaking the affinityGroup and can be theoretically migrated
    // - start with bigger Affinity Groups
    List<AffinityGroup> affGroupsBySize = new ArrayList<>(violatedAffinityGroups);
    Collections.sort(affGroupsBySize, Collections.reverseOrder(new AffinityGroupComparator()));
    for (AffinityGroup affinityGroup : affGroupsBySize) {
        final List<List<Guid>> candidateVmGroups;
        if (affinityGroup.isVmPositive()) {
            candidateVmGroups = groupVmsViolatingPositiveAg(affinityGroup, vmsMap);
            log.info("Positive affinity group violation detected");
        } else if (affinityGroup.isVmNegative()) {
            candidateVmGroups = groupVmsViolatingNegativeAg(affinityGroup, vmsMap);
            log.info("Negative affinity group violation detected");
        } else {
            continue;
        }
        // Look for a valid VM to migrate on all hosts
        for (List<Guid> group : candidateVmGroups) {
            Collections.shuffle(group);
            for (Guid vmId : group) {
                VM vm = vmsMap.get(vmId);
                if (isVmMigrationValid(cluster, vm)) {
                    return vm;
                }
            }
        }
    }
    // No possible migration..
    return null;
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Guid(org.ovirt.engine.core.compat.Guid) Collections.min(java.util.Collections.min) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) VmDao(org.ovirt.engine.core.dao.VmDao) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup) HashSet(java.util.HashSet) Inject(javax.inject.Inject) List(java.util.List) VM(org.ovirt.engine.core.common.businessentities.VM) Cluster(org.ovirt.engine.core.common.businessentities.Cluster) SchedulingManager(org.ovirt.engine.core.bll.scheduling.SchedulingManager) Map(java.util.Map) AffinityGroupDao(org.ovirt.engine.core.dao.scheduling.AffinityGroupDao) Optional(java.util.Optional) MigrationSupport(org.ovirt.engine.core.common.businessentities.MigrationSupport) Comparator(java.util.Comparator) Collections(java.util.Collections) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) Guid(org.ovirt.engine.core.compat.Guid) AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup) VM(org.ovirt.engine.core.common.businessentities.VM) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 33 with AffinityGroup

use of org.ovirt.engine.core.common.scheduling.AffinityGroup in project ovirt-engine by oVirt.

the class AffinityRulesEnforcer method getVmToHostsAffinityGroupCandidates.

/**
 * Get a list of candidate VMs (by VM ids) from the VM to host affinity groups.
 * This list will contain all VMs that violate the host affinity policies
 * sorted according to the number of violations (descending).
 *
 * @param allVMtoHostsAffinityGroups VM to Host affinity groups.
 * @param vmsMap                     VMs map with key: vm id , value: associated vm object.
 * @param isVdsAffinityEnforcing     true - Hard affinity constraint, false - Soft affinity constraint.
 * @return list of candidate VMs for migration by VM to Host affinities.
 */
private List<Guid> getVmToHostsAffinityGroupCandidates(List<AffinityGroup> allVMtoHostsAffinityGroups, Map<Guid, VM> vmsMap, boolean isVdsAffinityEnforcing) {
    Map<Guid, Integer> vmToHostsAffinityMap = new HashMap<>();
    // Iterate over all affinity groups and check the currently running
    // VMs for compliance, record violations per VM
    allVMtoHostsAffinityGroups.stream().filter(AffinityGroup::isVdsAffinityEnabled).filter(ag -> ag.isVdsEnforcing() == isVdsAffinityEnforcing).forEach(g -> {
        Set<Guid> affHosts = new HashSet<>(g.getVdsIds());
        g.getVmIds().forEach(vm_id -> {
            VM vm = vmsMap.get(vm_id);
            if (vm == null) {
                return;
            }
            if (affHosts.contains(vm.getRunOnVds()) && !g.isVdsPositive()) {
                // Negative affinity violated
                vmToHostsAffinityMap.put(vm_id, 1 + vmToHostsAffinityMap.getOrDefault(vm_id, 0));
            } else if (!affHosts.contains(vm.getRunOnVds()) && g.isVdsPositive()) {
                // Positive affinity violated
                vmToHostsAffinityMap.put(vm_id, 1 + vmToHostsAffinityMap.getOrDefault(vm_id, 0));
            }
        });
    });
    // Sort according the to the number of violations
    return vmToHostsAffinityMap.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue)).map(Map.Entry::getKey).collect(Collectors.toList());
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Guid(org.ovirt.engine.core.compat.Guid) Collections.min(java.util.Collections.min) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) VmDao(org.ovirt.engine.core.dao.VmDao) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup) HashSet(java.util.HashSet) Inject(javax.inject.Inject) List(java.util.List) VM(org.ovirt.engine.core.common.businessentities.VM) Cluster(org.ovirt.engine.core.common.businessentities.Cluster) SchedulingManager(org.ovirt.engine.core.bll.scheduling.SchedulingManager) Map(java.util.Map) AffinityGroupDao(org.ovirt.engine.core.dao.scheduling.AffinityGroupDao) Optional(java.util.Optional) MigrationSupport(org.ovirt.engine.core.common.businessentities.MigrationSupport) Comparator(java.util.Comparator) Collections(java.util.Collections) HashMap(java.util.HashMap) VM(org.ovirt.engine.core.common.businessentities.VM) Guid(org.ovirt.engine.core.compat.Guid) HashMap(java.util.HashMap) Map(java.util.Map) AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup) HashSet(java.util.HashSet)

Example 34 with AffinityGroup

use of org.ovirt.engine.core.common.scheduling.AffinityGroup in project ovirt-engine by oVirt.

the class AffinityRulesUtils method getUnifiedPositiveAffinityGroups.

/**
 * Take all positive affinity groups and merge all groups that reference the same VM
 * together.
 *
 * Ex. groups A+B, B+C, D+E, A+D, F+G are in fact just two bigger groups:
 *     A+B+C+D+E and F+G
 *
 * The algorithm starts by creating single element groups from all VMs
 * It then goes through all affinity groups and merges all VM groups that contain
 * VMs from the currently processed AffinityGroup.
 */
public static Set<Set<Guid>> getUnifiedPositiveAffinityGroups(List<AffinityGroup> affinityGroups) {
    Set<Set<Guid>> uag = new HashSet<>();
    Map<Guid, Set<Guid>> vmIndex = new HashMap<>();
    /**
     * Initialize the single element groups by taking all VMs that are referenced
     * from any affinity group
     */
    for (Iterator<AffinityGroup> it = affinityGroups.iterator(); it.hasNext(); ) {
        AffinityGroup ag = it.next();
        for (Guid id : ag.getVmIds()) {
            Set<Guid> temp = new HashSet<>();
            temp.add(id);
            uag.add(temp);
            vmIndex.put(id, temp);
        }
    }
    // that contain the referenced VMs into one.
    for (AffinityGroup ag : affinityGroups) {
        if (ag.isVmPositive()) {
            Set<Guid> mergedGroup = new HashSet<>();
            for (Guid id : ag.getVmIds()) {
                // Get the current groups VM(id) belongs to
                Set<Guid> existingGroup = vmIndex.get(id);
                // Merge it with the currently computed mergeGroup
                mergedGroup.addAll(existingGroup);
                // And remove it from the valid groups
                // (it will be re-added as part of a bigger group)
                uag.remove(existingGroup);
                // Update the per-VM index
                for (Guid vm : existingGroup) {
                    vmIndex.put(vm, mergedGroup);
                }
            }
            uag.add(mergedGroup);
        }
    }
    return uag;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Guid(org.ovirt.engine.core.compat.Guid) AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup) HashSet(java.util.HashSet)

Example 35 with AffinityGroup

use of org.ovirt.engine.core.common.scheduling.AffinityGroup in project ovirt-engine by oVirt.

the class AffinityRulesUtils method setsToAffinityGroups.

/**
 * Convert positive affinity group sets to a proper AffinityGroup objects.
 * Use lists so the order is defined as we want an deterministic algorithm further on.
 *
 * @param uag A set of sets containing the unified POSITIVE affinity groups.
 * @return List of AffinityGroup objects representing the provided unified affinity groups
 */
static List<AffinityGroup> setsToAffinityGroups(Set<Set<Guid>> uag) {
    List<AffinityGroup> output = new ArrayList<>();
    for (Set<Guid> s : uag) {
        AffinityGroup temp = new AffinityGroup();
        temp.setVmAffinityRule(EntityAffinityRule.POSITIVE);
        List<Guid> entities = new ArrayList<>();
        entities.addAll(s);
        temp.setVmIds(entities);
        output.add(temp);
    }
    return output;
}
Also used : ArrayList(java.util.ArrayList) Guid(org.ovirt.engine.core.compat.Guid) AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup)

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