Martin Sivák has uploaded a new change for review.

Change subject: Add scheduling filter that prevents migration to same host
......................................................................

Add scheduling filter that prevents migration to same host

Inhibit migration if destination hostname resolves
to source host. In that case, libvirt will abort
anyway, so this check just prevents libvirt error
from happening.

In addition, avoid migrations to a host which
resolves to localhost, which is rarely, if ever,
a good thing.

Change-Id: I42021452205f3dc070388a43de92c8fd5ed6dbf2
Bug-Url: https://bugzilla.redhat.com/1107650
Signed-off-by: Martin Sivák <[email protected]>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/PolicyUnitImpl.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/MigrationPolicyUnit.java
A packaging/dbscripts/upgrade/03_06_520_add_migration_policy_unit.sql
3 files changed, 78 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/00/34700/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/PolicyUnitImpl.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/PolicyUnitImpl.java
index 2c63000..01aebd8 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/PolicyUnitImpl.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/PolicyUnitImpl.java
@@ -16,6 +16,7 @@
 import 
org.ovirt.engine.core.bll.scheduling.policyunits.HostedEngineHAClusterFilterPolicyUnit;
 import 
org.ovirt.engine.core.bll.scheduling.policyunits.HostedEngineHAClusterWeightPolicyUnit;
 import org.ovirt.engine.core.bll.scheduling.policyunits.MemoryPolicyUnit;
+import org.ovirt.engine.core.bll.scheduling.policyunits.MigrationPolicyUnit;
 import org.ovirt.engine.core.bll.scheduling.policyunits.NetworkPolicyUnit;
 import org.ovirt.engine.core.bll.scheduling.policyunits.NoneBalancePolicyUnit;
 import org.ovirt.engine.core.bll.scheduling.policyunits.NoneWeightPolicyUnit;
@@ -45,6 +46,8 @@
 
     public static PolicyUnitImpl getPolicyUnitImpl(PolicyUnit policyUnit) {
         switch (policyUnit.getName()) {
+        case "Migration":
+            return new MigrationPolicyUnit(policyUnit);
         case "NUMA":
             return new NumaFilterPolicyUnit(policyUnit);
         case "PinToHost":
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/MigrationPolicyUnit.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/MigrationPolicyUnit.java
index 001d123..ed6a68c 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/MigrationPolicyUnit.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/MigrationPolicyUnit.java
@@ -1,5 +1,8 @@
 package org.ovirt.engine.core.bll.scheduling.policyunits;
 
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -8,6 +11,8 @@
 import org.ovirt.engine.core.common.businessentities.VM;
 import org.ovirt.engine.core.common.scheduling.PerHostMessages;
 import org.ovirt.engine.core.common.scheduling.PolicyUnit;
+import org.ovirt.engine.core.dal.dbbroker.DbFacade;
+import org.ovirt.engine.core.dao.VdsDAO;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -18,17 +23,72 @@
         super(policyUnit);
     }
 
-    @Override
-    public List<VDS> filter(List<VDS> hosts, VM vm, Map<String, String> 
parameters, PerHostMessages messages) {
-        if (vm.getRunOnVds() != null) {
-            for (VDS host : hosts) {
-                if (host.getId().equals(vm.getRunOnVds())) {
-                    log.debug("Vm '{}' run on host '{}', filtering host", 
vm.getName(), host.getName());
-                    hosts.remove(host);
-                    break;
-                }
+    private boolean validateDestinationVdsAddress(VDS srcVds, VDS dstVds) {
+        InetAddress dstAddress = null;
+
+        try {
+            dstAddress = InetAddress.getByName(dstVds.getHostName());
+        } catch (UnknownHostException e) {
+            // no clues, better to not guess. Let libvirt sort out the mess,
+            // as it already did before.
+            return true;
+        }
+
+        if (dstAddress != null && dstAddress.isLoopbackAddress()) {
+            log.warn("cannot migrate to VDS {}: resolved to loopback address", 
dstVds.getHostName());
+            return false;
+        }
+
+        if (srcVds != null) { // on runVm will be null
+            InetAddress srcAddress = null;
+
+            if (srcVds.getHostName().equals(dstVds.getHostName())) {
+                log.warn("cannot migrate to VDS {}: same hostname as source", 
dstVds.getHostName());
+                return false;
+            }
+
+            try {
+                srcAddress = InetAddress.getByName(srcVds.getHostName());
+            } catch (UnknownHostException e) {
+                return true; // same as per dst address
+            }
+
+            if (srcAddress != null && dstAddress != null && 
srcAddress.equals(dstAddress)) {
+                log.warn("cannot migrate to VDS {}: resolved address equal to 
source", dstVds.getHostName());
+                return false;
             }
         }
+
+        return true;
+    }
+
+    @Override
+    public List<VDS> filter(List<VDS> hosts, VM vm, Map<String, String> 
parameters, PerHostMessages messages) {
+
+        if (vm.getRunOnVds() != null) {
+            List<VDS> hostsToRunOn = new ArrayList<>();
+            VDS srcVds = getVdsDAO().get(vm.getRunOnVds());
+
+            for (VDS host : hosts) {
+                if (host.getId().equals(vm.getRunOnVds())) {
+                    log.debug("Vm '{}' already runs on host '{}', filtering 
host", vm.getName(), host.getName());
+                    continue;
+                }
+
+                if (!validateDestinationVdsAddress(srcVds, host)) {
+                    continue;
+                }
+
+                hostsToRunOn.add(host);
+            }
+
+            return hostsToRunOn;
+        }
+
         return hosts;
     }
+
+    protected VdsDAO getVdsDAO() {
+        return DbFacade.getInstance().getVdsDao();
+    }
 }
diff --git 
a/packaging/dbscripts/upgrade/03_06_520_add_migration_policy_unit.sql 
b/packaging/dbscripts/upgrade/03_06_520_add_migration_policy_unit.sql
new file mode 100644
index 0000000..3d1d398
--- /dev/null
+++ b/packaging/dbscripts/upgrade/03_06_520_add_migration_policy_unit.sql
@@ -0,0 +1,6 @@
+INSERT INTO policy_units (id, name, is_internal, custom_properties_regex, 
type, enabled, description) VALUES ('e659c871-0bf1-4ccc-b748-f28f5d08ddda', 
'Migration', true, NULL, 0, true, 'Prevent migration to the same host.');
+
+INSERT INTO cluster_policy_units (cluster_policy_id, policy_unit_id, 
filter_sequence, factor) VALUES ('20d25257-b4bd-4589-92a6-c4c5c5d3fd1a', 
'e659c871-0bf1-4ccc-b748-f28f5d08ddda', 0, 0);
+INSERT INTO cluster_policy_units (cluster_policy_id, policy_unit_id, 
filter_sequence, factor) VALUES ('5a2b0939-7d46-4b73-a469-e9c2c7fc6a53', 
'e659c871-0bf1-4ccc-b748-f28f5d08ddda', 0, 0);
+INSERT INTO cluster_policy_units (cluster_policy_id, policy_unit_id, 
filter_sequence, factor) VALUES ('b4ed2332-a7ac-4d5f-9596-99a439cb2812', 
'e659c871-0bf1-4ccc-b748-f28f5d08ddda', 0, 0);
+INSERT INTO cluster_policy_units (cluster_policy_id, policy_unit_id, 
filter_sequence, factor) VALUES ('8d5d7bec-68de-4a67-b53e-0ac54686d579', 
'e659c871-0bf1-4ccc-b748-f28f5d08ddda', 0, 0);
\ No newline at end of file


-- 
To view, visit http://gerrit.ovirt.org/34700
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I42021452205f3dc070388a43de92c8fd5ed6dbf2
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Martin Sivák <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to