Alona Kaplan has uploaded a new change for review.

Change subject: engine: RefreshHostDevicesCommand- remove entries from table 
before adding
......................................................................

engine: RefreshHostDevicesCommand- remove entries from table before adding

Removing entries from the tables (host_device, host_nic_vfs_config) should
be done before adding new ones-
to avoid potential duplicate error of unique columns.

Change-Id: Id4e6f88f0e3e6ce43d68709e0ec80b44d951bf96
Signed-off-by: Alona Kaplan <alkap...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/hostdev/RefreshHostDevicesCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelper.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelperImpl.java
M 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelperImplTest.java
4 files changed, 52 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/21/41521/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/hostdev/RefreshHostDevicesCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/hostdev/RefreshHostDevicesCommand.java
index 4bb9f38..3d992ea 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/hostdev/RefreshHostDevicesCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/hostdev/RefreshHostDevicesCommand.java
@@ -14,18 +14,18 @@
 import org.ovirt.engine.core.common.action.VdsActionParameters;
 import org.ovirt.engine.core.common.businessentities.Entities;
 import org.ovirt.engine.core.common.businessentities.HostDevice;
-import org.ovirt.engine.core.common.businessentities.network.HostNicVfsConfig;
-import 
org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface;
 import org.ovirt.engine.core.common.businessentities.VmDevice;
 import org.ovirt.engine.core.common.businessentities.VmDeviceGeneralType;
+import org.ovirt.engine.core.common.businessentities.network.HostNicVfsConfig;
+import 
org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface;
 import org.ovirt.engine.core.common.errors.VdcBllMessages;
 import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
 import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
 import 
org.ovirt.engine.core.common.vdscommands.VdsIdAndVdsVDSCommandParametersBase;
 import org.ovirt.engine.core.compat.Guid;
-import org.ovirt.engine.core.dao.network.HostNicVfsConfigDao;
 import org.ovirt.engine.core.dao.HostDeviceDao;
 import org.ovirt.engine.core.dao.VmDeviceDAO;
+import org.ovirt.engine.core.dao.network.HostNicVfsConfigDao;
 import org.ovirt.engine.core.utils.transaction.TransactionMethod;
 import org.ovirt.engine.core.utils.transaction.TransactionSupport;
 import org.ovirt.engine.core.vdsbroker.ResourceManager;
@@ -51,6 +51,10 @@
     @Inject
     private HostNicVfsConfigHelper hostNicVfsConfigHelper;
 
+    private Map<String, HostDevice> oldMap;
+
+    private Map<String, HostDevice> fetchedMap;
+
     public RefreshHostDevicesCommand(T parameters) {
         super(parameters);
     }
@@ -71,8 +75,8 @@
         List<HostDevice> oldDevices = 
hostDeviceDao.getHostDevicesByHostId(getVdsId());
         List<VmDevice> vmDevices = 
vmDeviceDao.getVmDeviceByType(VmDeviceGeneralType.HOSTDEV);
 
-        Map<String, HostDevice> fetchedMap = 
Entities.entitiesByName(fetchedDevices);
-        final Map<String, HostDevice> oldMap = 
Entities.entitiesByName(oldDevices);
+        fetchedMap = Entities.entitiesByName(fetchedDevices);
+        oldMap = Entities.entitiesByName(oldDevices);
         Map<String, VmDevice> vmDeviceMap = 
Entities.vmDevicesByDevice(vmDevices);
 
         final List<HostDevice> newDevices = new ArrayList<>();
@@ -110,12 +114,13 @@
             TransactionSupport.executeInNewTransaction(new 
TransactionMethod<Void>() {
                 @Override
                 public Void runInTransaction() {
+
+                    hostDeviceDao.removeAllInBatch(removedDevices);
                     hostDeviceDao.saveAllInBatch(newDevices);
                     hostDeviceDao.updateAllInBatch(changedDevices);
 
-                    handleHostNicVfsConfigUpdate(oldMap, newDevices, 
changedDevices, removedDevices);
+                    handleHostNicVfsConfigUpdate(newDevices, changedDevices, 
removedDevices);
 
-                    hostDeviceDao.removeAllInBatch(removedDevices);
                     vmDeviceDao.removeAllInBatch(removedVmDevices);
 
                     return null;
@@ -128,8 +133,7 @@
         setSucceeded(true);
     }
 
-    private void handleHostNicVfsConfigUpdate(final Map<String, HostDevice> 
oldMap,
-            final List<HostDevice> newDevices,
+    private void handleHostNicVfsConfigUpdate(final List<HostDevice> 
newDevices,
             final List<HostDevice> changedDevices,
             final List<HostDevice> removedDevices) {
         final List<HostNicVfsConfig> newHostNicVfsConfigs = new ArrayList<>();
@@ -161,12 +165,12 @@
             }
         }
 
-        if (!newHostNicVfsConfigs.isEmpty()) {
-            hostNicVfsConfigDao.saveAllInBatch(newHostNicVfsConfigs);
-        }
-
         if (!removedHostNicVfsConfigs.isEmpty()) {
             hostNicVfsConfigDao.removeAllInBatch(removedHostNicVfsConfigs);
+        }
+
+        if (!newHostNicVfsConfigs.isEmpty()) {
+            hostNicVfsConfigDao.saveAllInBatch(newHostNicVfsConfigs);
         }
     }
 
@@ -177,7 +181,7 @@
     }
 
     private HostNicVfsConfig getHostNicVfsConfigToAdd(HostDevice device) {
-        VdsNetworkInterface nic = 
hostNicVfsConfigHelper.getNicByPciDevice(device);
+        VdsNetworkInterface nic = 
hostNicVfsConfigHelper.getNicByPciDevice(device, fetchedMap.values());
 
         if (nic == null) {
             return null;
@@ -192,7 +196,7 @@
     }
 
     private HostNicVfsConfig getHostNicVfsConfigToRemove(HostDevice device) {
-        VdsNetworkInterface nic = 
hostNicVfsConfigHelper.getNicByPciDevice(device);
+        VdsNetworkInterface nic = 
hostNicVfsConfigHelper.getNicByPciDevice(device, oldMap.values());
         if (nic == null) {
             return null;
         }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelper.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelper.java
index 5e86c99..cfcb9e1 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelper.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelper.java
@@ -1,5 +1,6 @@
 package org.ovirt.engine.core.bll.network.host;
 
+import java.util.Collection;
 import java.util.List;
 import java.util.Set;
 
@@ -21,6 +22,18 @@
     public VdsNetworkInterface getNicByPciDevice(final HostDevice pciDevice);
 
     /**
+     * Retrieves the <code>VdsNetworkInterface</code> that the specified 
<code>pciDevice</code> represents.
+     * This method uses the specified <code>devices</code> and doesn't fetch 
data from the DB.
+     *
+     * @param pciDevice
+     * @param devices collection of all the devices.
+     * @return the <code>VdsNetworkInterface</code> that the specified 
<code>pciDevice</code> represents. If the device
+     *         is not parent of network interface device or doesn't exist in 
the VdsInterface table a <code>null</code>
+     *         is returned.
+     */
+    public VdsNetworkInterface getNicByPciDevice(final HostDevice pciDevice, 
Collection<HostDevice> devices);
+
+    /**
      * Retrieves whether the specified <code>device</code> is SR-IOV enabled.
      *
      * @param device
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelperImpl.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelperImpl.java
index ed01f9d..d80603c 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelperImpl.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelperImpl.java
@@ -1,5 +1,6 @@
 package org.ovirt.engine.core.bll.network.host;
 
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Objects;
@@ -38,7 +39,12 @@
 
     @Override
     public VdsNetworkInterface getNicByPciDevice(final HostDevice pciDevice) {
-        final HostDevice netDevice = getNetDeviceByPciDevice(pciDevice);
+        return getNicByPciDevice(pciDevice, null);
+    }
+
+    @Override
+    public VdsNetworkInterface getNicByPciDevice(final HostDevice pciDevice, 
Collection<HostDevice> devices) {
+        final HostDevice netDevice = getNetDeviceByPciDevice(pciDevice, 
devices);
 
         if (netDevice == null || !isNetworkDevice(netDevice)) {
             return null;
@@ -55,8 +61,9 @@
         });
     }
 
-    private HostDevice getNetDeviceByPciDevice(final HostDevice pciDevice) {
-        return 
LinqUtils.firstOrNull(getDevicesByHostId(pciDevice.getHostId()), new 
Predicate<HostDevice>() {
+    private HostDevice getNetDeviceByPciDevice(final HostDevice pciDevice, 
Collection<HostDevice> devices) {
+        Collection<HostDevice> hostDevices = devices == null ? 
getDevicesByHostId(pciDevice.getHostId()) : devices;
+        return LinqUtils.firstOrNull(hostDevices, new Predicate<HostDevice>() {
 
             @Override
             public boolean eval(HostDevice device) {
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelperImplTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelperImplTest.java
index 2117332..686f1c5 100644
--- 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelperImplTest.java
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/HostNicVfsConfigHelperImplTest.java
@@ -13,6 +13,7 @@
 import static org.mockito.Mockito.when;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 
@@ -121,6 +122,15 @@
     }
 
     @Test
+    public void getNicByNetDeviceWithNonDbDevicesNoNetDevice() {
+        mockNics(Collections.<VdsNetworkInterface> emptyList(), true);
+        Collection<HostDevice> devices = new ArrayList<>();
+        devices.add(pciDevice);
+
+        assertNull(hostNicVfsConfigHelper.getNicByPciDevice(pciDevice, 
devices));
+    }
+
+    @Test
     public void isSriovNetworkDeviceNotSriov() {
         commonIsSriovDevice(false);
     }


-- 
To view, visit https://gerrit.ovirt.org/41521
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id4e6f88f0e3e6ce43d68709e0ec80b44d951bf96
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Alona Kaplan <alkap...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to