<commit_before>---
title: Affinity Rules Enforcement Manager
category: feature
authors: doron, maroshi, tsaban
wiki_category: Feature|Affinity Group Enforcement Manager
wiki_title: Affinity Rules Enforcement Manager
wiki_revision_count: 33
wiki_last_updated: 2015-05-19
feature_name: Affinity Rules Enforcement Manager
feature_modules: engine
feature_status: In Development
---

# Affinity Rules Enforcement Manager

### Summary

A new manager that will enforce affinity rules for running VMs. The manager will periodically check each cluster for affinity rule conflicts and will try to resolve those conflicts by migrating problematic VMs. Each interval only one VM will be migrated by the manager for each cluster(As long as there are conflicts).

The following picture, explains AR (Affinity Rules), before enforcement and after enforcement
(The green boxes represent VMs belonging to positive affinity groups and the red ones are VMs belonging to negative affinity groups.
See VM-Affinity page for more details <http://www.ovirt.org/Features/VM-Affinity>). ![](Affinity_Rule_Enforcement.png "fig:Affinity_Rule_Enforcement.png")

### Owner

<http://www.ovirt.org/User:Tsaban>

*   Name: [ <NAME>](User:Tsaban)
*   Email: <<EMAIL>>

### Procedure

![](ARES_Life_Cycle.png "fig:ARES_Life_Cycle.png")
[1][2]
[The following method identify broken affinity rule, designate vm that breaks the rule and migrates the vm.]
**\1**

1.  Find migration free clusters
2.  for each migration free cluster:
    1.  **choose next VM to migrate** and add to VM candidates.

3.  for each vm candidate for migrations:
    1.  migrate VM.

### Related methods

**\1**

1.  Get all hard affinity groups.
2.  Get unified affinity groups().

The following picture explains UAG (Unified Affinity Group) algorithm

![](UAG_Algorithm.png "fig:UAG_Algorithm.png")

1.  Loop over all unified affinity groups(order by size and than by lowest VM id[4]):
    1.  if affinity group positive:
        1.  find VM violating positive affinity group

    2.  Else:
        1.  find VM violating negative affinity group

    3.  if found candidate VM check if can migrate VM:
        1.  If can migrate VM then return VM.

 **\1**

1.  UAG = {{vm} for each vm}
2.  For each (+) affinity group(Sorted by group id):
    1.  unify VMs from the group in UAG(Sorted by vm id).
    2.  For each (-) affinity group(Sorted by group id):
        1.  [10]For each group in UAG(Sorted by first vm uuid):
            1.  if size of the intersection of group from UAG and the negative group is > 1:
                1.  throw exception “Affinity group contradiction detected” (With associated groups).

### Footnotes

[1]Methods are written in the section “related methods”.
[2] If no class is written(As for most methods/fields) assume it’s in the manager itself.
[3] Procedure here happens for every cluster separately.
[4] if no vm is found for migration, In this situation we decide to make the manager sleep for long interval because, affinity groups are enforced at that point.
[5] The migration command will be done automatically to take into account other policies that might be in use.
[6] It’s important to keep getting the same groups in the same order each time. That’s why we will use SortedSet. The order is kept because affinity group are not supposed to change during migrations and if an affinity group will change then it can be fixed by emptying the last migrations list(thus temporarily allowing opposite migration and preventing the manager from shutting down unnecessarily).
[7] candidate host is the host we think is going to be the host that the next vm should migrate to. But, we are using the scheduler’s automatic migration thus the scheduler may decide to migrate the vm somewhere else.
[8] Vms in error state can’t migrate.
[9] The migration might fail. But, because we don’t want to cause migration loop we prefer to assume that the migration will succeed when we decide which host should be the one that the vms will migrate to.
[10] VMs that don’t belong to any positive affinity group(In the UAG - Unified Affinity group). Still has group of their own. So, in the next step that group still need to be on a separate host(It is still possible to put two positive affinity groups on the same host but we let the scheduler decide that for us).
[11] This if statement checks that a migration in the opposite direction doesn’t occur(Replacing target host with source host in the last migration).

### Related Data Structures

**\1**

1.  UUID vm.
2.  UUID sourceHost.<commit_msg>Footnotes<commit_after>---
title: Affinity Rules Enforcement Manager
category: feature
authors: doron, maroshi, tsaban
wiki_category: Feature|Affinity Group Enforcement Manager
wiki_title: Affinity Rules Enforcement Manager
wiki_revision_count: 33
wiki_last_updated: 2015-05-19
feature_name: Affinity Rules Enforcement Manager
feature_modules: engine
feature_status: In Development
---

# Affinity Rules Enforcement Manager

### Summary

A new manager that will enforce affinity rules for running VMs. The manager will periodically check each cluster for affinity rule conflicts and will try to resolve those conflicts by migrating problematic VMs. Each interval only one VM will be migrated by the manager for each cluster(As long as there are conflicts).

The following picture, explains AR (Affinity Rules), before enforcement and after enforcement
(The green boxes represent VMs belonging to positive affinity groups and the red ones are VMs belonging to negative affinity groups.
See VM-Affinity page for more details <http://www.ovirt.org/Features/VM-Affinity>). ![](Affinity_Rule_Enforcement.png "fig:Affinity_Rule_Enforcement.png")

### Owner

<http://www.ovirt.org/User:Tsaban>

*   Name: [ <NAME>](User:Tsaban)
*   Email: <<EMAIL>>

### Procedure

![](ARES_Life_Cycle.png "fig:ARES_Life_Cycle.png")
[1][2]
[The following method identify broken affinity rule, designate vm that breaks the rule and migrates the vm.]
**\1**

1.  Find migration free clusters
2.  for each migration free cluster:
    1.  **choose next VM to migrate** and add to VM candidates.

3.  for each vm candidate for migrations:
    1.  migrate VM.

### Related methods

**\1**

1.  Get all hard affinity groups.
2.  Get unified affinity groups().

The following picture explains UAG (Unified Affinity Group) algorithm

![](UAG_Algorithm.png "fig:UAG_Algorithm.png")

1.  Loop over all unified affinity groups(order by size and than by lowest VM id[4]):
    1.  if affinity group positive:
        1.  find VM violating positive affinity group

    2.  Else:
        1.  find VM violating negative affinity group

    3.  if found candidate VM check if can migrate VM:
        1.  If can migrate VM then return VM.

 **\1**

1.  UAG = {{vm} for each vm}
2.  For each (+) affinity group(Sorted by group id):
    1.  unify VMs from the group in UAG(Sorted by vm id).
    2.  For each (-) affinity group(Sorted by group id):
        1.  [10]For each group in UAG(Sorted by first vm uuid):
            1.  if size of the intersection of group from UAG and the negative group is > 1:
                1.  throw exception “Affinity group contradiction detected” (With associated groups).

### Footnotes

[1]Methods are written in the section “related methods”.
[2] If no class is written(As for most methods/fields) assume it’s in the manager itself.
[3] The migration command will be done automatically to let the scheduler decide where to migrate the VM based on other policies as well.
[4] It’s important to keep getting the same groups in the same order each time. The order is kept because affinity group are assumed not to change very often during enforcement.
[5] VMs in error state can’t migrate.

### Related Data Structures

**\1**

1.  UUID vm.
2.  UUID sourceHost.
{'fraction_non_alphanumeric': 0.07017543859649122, 'fraction_numerical': 0.013317966448969138, 'mean_word_length': 3.9274447949526814, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6957899', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2069, 'n_words': 1104}