server: check if there are active nics before network GC (#8204)

This commit is contained in:
Wei Zhou 2023-11-29 18:55:26 +01:00 committed by GitHub
parent 956efb27d9
commit cb2b6aca45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 16 deletions

View File

@ -79,7 +79,9 @@ public interface NicDao extends GenericDao<NicVO, Long> {
List<NicVO> listByNetworkIdTypeAndGatewayAndBroadcastUri(long networkId, VirtualMachine.Type vmType, String gateway, URI broadcastUri); List<NicVO> listByNetworkIdTypeAndGatewayAndBroadcastUri(long networkId, VirtualMachine.Type vmType, String gateway, URI broadcastUri);
int countNicsForStartingVms(long networkId); int countNicsForNonStoppedVms(long networkId);
int countNicsForNonStoppedRunningVrs(long networkId);
NicVO getControlNicForVM(long vmId); NicVO getControlNicForVM(long vmId);

View File

@ -44,7 +44,7 @@ public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
private GenericSearchBuilder<NicVO, String> IpSearch; private GenericSearchBuilder<NicVO, String> IpSearch;
private SearchBuilder<NicVO> NonReleasedSearch; private SearchBuilder<NicVO> NonReleasedSearch;
private GenericSearchBuilder<NicVO, Integer> deviceIdSearch; private GenericSearchBuilder<NicVO, Integer> deviceIdSearch;
private GenericSearchBuilder<NicVO, Integer> CountByForStartingVms; private GenericSearchBuilder<NicVO, Integer> CountByForNonStoppedVms;
private SearchBuilder<NicVO> PeerRouterSearch; private SearchBuilder<NicVO> PeerRouterSearch;
@Inject @Inject
@ -91,14 +91,16 @@ public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
deviceIdSearch.and("instance", deviceIdSearch.entity().getInstanceId(), Op.EQ); deviceIdSearch.and("instance", deviceIdSearch.entity().getInstanceId(), Op.EQ);
deviceIdSearch.done(); deviceIdSearch.done();
CountByForStartingVms = createSearchBuilder(Integer.class); CountByForNonStoppedVms = createSearchBuilder(Integer.class);
CountByForStartingVms.select(null, Func.COUNT, CountByForStartingVms.entity().getId()); CountByForNonStoppedVms.select(null, Func.COUNT, CountByForNonStoppedVms.entity().getId());
CountByForStartingVms.and("networkId", CountByForStartingVms.entity().getNetworkId(), Op.EQ); CountByForNonStoppedVms.and("vmType", CountByForNonStoppedVms.entity().getVmType(), Op.EQ);
CountByForStartingVms.and("removed", CountByForStartingVms.entity().getRemoved(), Op.NULL); CountByForNonStoppedVms.and("vmTypeNEQ", CountByForNonStoppedVms.entity().getVmType(), Op.NEQ);
CountByForNonStoppedVms.and("networkId", CountByForNonStoppedVms.entity().getNetworkId(), Op.EQ);
CountByForNonStoppedVms.and("removed", CountByForNonStoppedVms.entity().getRemoved(), Op.NULL);
SearchBuilder<VMInstanceVO> join1 = _vmDao.createSearchBuilder(); SearchBuilder<VMInstanceVO> join1 = _vmDao.createSearchBuilder();
join1.and("state", join1.entity().getState(), Op.EQ); join1.and("state", join1.entity().getState(), Op.IN);
CountByForStartingVms.join("vm", join1, CountByForStartingVms.entity().getInstanceId(), join1.entity().getId(), JoinBuilder.JoinType.INNER); CountByForNonStoppedVms.join("vm", join1, CountByForNonStoppedVms.entity().getInstanceId(), join1.entity().getId(), JoinBuilder.JoinType.INNER);
CountByForStartingVms.done(); CountByForNonStoppedVms.done();
PeerRouterSearch = createSearchBuilder(); PeerRouterSearch = createSearchBuilder();
PeerRouterSearch.and("instanceId", PeerRouterSearch.entity().getInstanceId(), Op.NEQ); PeerRouterSearch.and("instanceId", PeerRouterSearch.entity().getInstanceId(), Op.NEQ);
@ -338,10 +340,21 @@ public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
} }
@Override @Override
public int countNicsForStartingVms(long networkId) { public int countNicsForNonStoppedVms(long networkId) {
SearchCriteria<Integer> sc = CountByForStartingVms.create(); SearchCriteria<Integer> sc = CountByForNonStoppedVms.create();
sc.setParameters("networkId", networkId); sc.setParameters("networkId", networkId);
sc.setJoinParameters("vm", "state", VirtualMachine.State.Starting); sc.setParameters("vmType", VirtualMachine.Type.User);
sc.setJoinParameters("vm", "state", new Object[] {VirtualMachine.State.Starting, VirtualMachine.State.Running, VirtualMachine.State.Stopping, VirtualMachine.State.Migrating});
List<Integer> results = customSearch(sc, null);
return results.get(0);
}
@Override
public int countNicsForNonStoppedRunningVrs(long networkId) {
SearchCriteria<Integer> sc = CountByForNonStoppedVms.create();
sc.setParameters("networkId", networkId);
sc.setParameters("vmTypeNEQ", VirtualMachine.Type.User);
sc.setJoinParameters("vm", "state", new Object[] {VirtualMachine.State.Starting, VirtualMachine.State.Stopping, VirtualMachine.State.Migrating});
List<Integer> results = customSearch(sc, null); List<Integer> results = customSearch(sc, null);
return results.get(0); return results.get(0);
} }

View File

@ -2559,10 +2559,11 @@ public class NetworkModelImpl extends ManagerBase implements NetworkModel, Confi
return false; return false;
} }
//if the network has vms in Starting state (nics for those might not be allocated yet as Starting state also used when vm is being Created) // if the network has user vms in Starting/Stopping/Migrating/Running state, or VRs in Starting/Stopping/Migrating state, don't GC
//don't GC // The active nics count (nics_count in op_networks table) might be wrong due to some reasons, should check the state of vms as well.
if (_nicDao.countNicsForStartingVms(networkId) > 0) { // (nics for Starting VMs might not be allocated yet as Starting state also used when vm is being Created)
s_logger.debug("Network id=" + networkId + " is not ready for GC as it has vms that are Starting at the moment"); if (_nicDao.countNicsForNonStoppedVms(networkId) > 0 || _nicDao.countNicsForNonStoppedRunningVrs(networkId) > 0) {
s_logger.debug("Network id=" + networkId + " is not ready for GC as it has vms that are not Stopped at the moment");
return false; return false;
} }