From 312eec8e40cdbfc8948d6ea7f1d21bd5a72d3123 Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Sun, 17 Jan 2021 15:26:10 +0000 Subject: [PATCH 1/2] server: fix cannot create vm if another vm with same name has been added and removed on the network steps to reproduce the issue (1) create vm-1 on network-1 (2) add vm-1 to network-2 (3) remove vm-1 from network-2 (4) create another vm with same name vm-1 on network-2 expected result: operation succeed actual result: operation failed. --- .../com/cloud/vm/dao/VMInstanceDaoImpl.java | 2 +- .../java/com/cloud/vm/UserVmManagerImpl.java | 19 ++++++++----------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java b/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java index 1d7d4440566c..d66e884696d3 100755 --- a/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java +++ b/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java @@ -274,12 +274,12 @@ protected void init() { SearchBuilder nicSearch = _nicDao.createSearchBuilder(); nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); + nicSearch.and("removed", nicSearch.entity().getRemoved(), SearchCriteria.Op.NULL); DistinctHostNameSearch = createSearchBuilder(String.class); DistinctHostNameSearch.selectFields(DistinctHostNameSearch.entity().getHostName()); DistinctHostNameSearch.and("types", DistinctHostNameSearch.entity().getType(), SearchCriteria.Op.IN); - DistinctHostNameSearch.and("removed", DistinctHostNameSearch.entity().getRemoved(), SearchCriteria.Op.NULL); DistinctHostNameSearch.join("nicSearch", nicSearch, DistinctHostNameSearch.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER); DistinctHostNameSearch.done(); diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java index fe8d8a45bcb6..9af48e9589d5 100644 --- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java @@ -1245,17 +1245,14 @@ public UserVm addNicToVirtualMachine(AddNicToVMCmd cmd) throws InvalidParameterV throw new CloudRuntimeException(vmInstance + " is in zone:" + vmInstance.getDataCenterId() + " but " + network + " is in zone:" + network.getDataCenterId()); } - // Get all vms hostNames in the network - List hostNames = _vmInstanceDao.listDistinctHostNames(network.getId()); - // verify that there are no duplicates, listDistictHostNames could return hostNames even if the NIC - //in the network is removed, so also check if the NIC is present and then throw an exception. - //This will also check if there are multiple nics of same vm in the network - if (hostNames.contains(vmInstance.getHostName())) { - for (String hostName : hostNames) { - VMInstanceVO vm = _vmInstanceDao.findVMByHostName(hostName); - if (_networkModel.getNicInNetwork(vm.getId(), network.getId()) != null && vm.getHostName().equals(vmInstance.getHostName())) { - throw new CloudRuntimeException(network + " already has a vm with host name: " + vmInstance.getHostName()); - } + if(_networkModel.getNicInNetwork(vmInstance.getId(),network.getId()) != null){ + s_logger.debug("VM " + vmInstance.getHostName() + " already in network " + network.getName() + " going to add another NIC"); + } else { + //* get all vms hostNames in the network + List hostNames = _vmInstanceDao.listDistinctHostNames(network.getId()); + //* verify that there are no duplicates + if (hostNames.contains(vmInstance.getHostName())) { + throw new CloudRuntimeException("Network " + network.getName() + " already has a vm with host name: " + vmInstance.getHostName()); } } From c538aaae8d38df39091b84c83852b06834e25e23 Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Tue, 26 Jan 2021 07:09:09 +0000 Subject: [PATCH 2/2] #4600: add back a removed line --- .../src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java b/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java index d66e884696d3..97a505d7c8f6 100755 --- a/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java +++ b/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java @@ -274,12 +274,13 @@ protected void init() { SearchBuilder nicSearch = _nicDao.createSearchBuilder(); nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); - nicSearch.and("removed", nicSearch.entity().getRemoved(), SearchCriteria.Op.NULL); + nicSearch.and("removedNic", nicSearch.entity().getRemoved(), SearchCriteria.Op.NULL); DistinctHostNameSearch = createSearchBuilder(String.class); DistinctHostNameSearch.selectFields(DistinctHostNameSearch.entity().getHostName()); DistinctHostNameSearch.and("types", DistinctHostNameSearch.entity().getType(), SearchCriteria.Op.IN); + DistinctHostNameSearch.and("removed", DistinctHostNameSearch.entity().getRemoved(), SearchCriteria.Op.NULL); DistinctHostNameSearch.join("nicSearch", nicSearch, DistinctHostNameSearch.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER); DistinctHostNameSearch.done();