Skip to content

Commit cb2b6ac

Browse files
server: check if there are active nics before network GC (#8204)
1 parent 956efb2 commit cb2b6ac

3 files changed

Lines changed: 32 additions & 16 deletions

File tree

engine/schema/src/main/java/com/cloud/vm/dao/NicDao.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ public interface NicDao extends GenericDao<NicVO, Long> {
7979

8080
List<NicVO> listByNetworkIdTypeAndGatewayAndBroadcastUri(long networkId, VirtualMachine.Type vmType, String gateway, URI broadcastUri);
8181

82-
int countNicsForStartingVms(long networkId);
82+
int countNicsForNonStoppedVms(long networkId);
83+
84+
int countNicsForNonStoppedRunningVrs(long networkId);
8385

8486
NicVO getControlNicForVM(long vmId);
8587

engine/schema/src/main/java/com/cloud/vm/dao/NicDaoImpl.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
4444
private GenericSearchBuilder<NicVO, String> IpSearch;
4545
private SearchBuilder<NicVO> NonReleasedSearch;
4646
private GenericSearchBuilder<NicVO, Integer> deviceIdSearch;
47-
private GenericSearchBuilder<NicVO, Integer> CountByForStartingVms;
47+
private GenericSearchBuilder<NicVO, Integer> CountByForNonStoppedVms;
4848
private SearchBuilder<NicVO> PeerRouterSearch;
4949

5050
@Inject
@@ -91,14 +91,16 @@ protected void init() {
9191
deviceIdSearch.and("instance", deviceIdSearch.entity().getInstanceId(), Op.EQ);
9292
deviceIdSearch.done();
9393

94-
CountByForStartingVms = createSearchBuilder(Integer.class);
95-
CountByForStartingVms.select(null, Func.COUNT, CountByForStartingVms.entity().getId());
96-
CountByForStartingVms.and("networkId", CountByForStartingVms.entity().getNetworkId(), Op.EQ);
97-
CountByForStartingVms.and("removed", CountByForStartingVms.entity().getRemoved(), Op.NULL);
94+
CountByForNonStoppedVms = createSearchBuilder(Integer.class);
95+
CountByForNonStoppedVms.select(null, Func.COUNT, CountByForNonStoppedVms.entity().getId());
96+
CountByForNonStoppedVms.and("vmType", CountByForNonStoppedVms.entity().getVmType(), Op.EQ);
97+
CountByForNonStoppedVms.and("vmTypeNEQ", CountByForNonStoppedVms.entity().getVmType(), Op.NEQ);
98+
CountByForNonStoppedVms.and("networkId", CountByForNonStoppedVms.entity().getNetworkId(), Op.EQ);
99+
CountByForNonStoppedVms.and("removed", CountByForNonStoppedVms.entity().getRemoved(), Op.NULL);
98100
SearchBuilder<VMInstanceVO> join1 = _vmDao.createSearchBuilder();
99-
join1.and("state", join1.entity().getState(), Op.EQ);
100-
CountByForStartingVms.join("vm", join1, CountByForStartingVms.entity().getInstanceId(), join1.entity().getId(), JoinBuilder.JoinType.INNER);
101-
CountByForStartingVms.done();
101+
join1.and("state", join1.entity().getState(), Op.IN);
102+
CountByForNonStoppedVms.join("vm", join1, CountByForNonStoppedVms.entity().getInstanceId(), join1.entity().getId(), JoinBuilder.JoinType.INNER);
103+
CountByForNonStoppedVms.done();
102104

103105
PeerRouterSearch = createSearchBuilder();
104106
PeerRouterSearch.and("instanceId", PeerRouterSearch.entity().getInstanceId(), Op.NEQ);
@@ -338,10 +340,21 @@ public List<NicVO> listPlaceholderNicsByNetworkIdAndVmType(long networkId, Virtu
338340
}
339341

340342
@Override
341-
public int countNicsForStartingVms(long networkId) {
342-
SearchCriteria<Integer> sc = CountByForStartingVms.create();
343+
public int countNicsForNonStoppedVms(long networkId) {
344+
SearchCriteria<Integer> sc = CountByForNonStoppedVms.create();
343345
sc.setParameters("networkId", networkId);
344-
sc.setJoinParameters("vm", "state", VirtualMachine.State.Starting);
346+
sc.setParameters("vmType", VirtualMachine.Type.User);
347+
sc.setJoinParameters("vm", "state", new Object[] {VirtualMachine.State.Starting, VirtualMachine.State.Running, VirtualMachine.State.Stopping, VirtualMachine.State.Migrating});
348+
List<Integer> results = customSearch(sc, null);
349+
return results.get(0);
350+
}
351+
352+
@Override
353+
public int countNicsForNonStoppedRunningVrs(long networkId) {
354+
SearchCriteria<Integer> sc = CountByForNonStoppedVms.create();
355+
sc.setParameters("networkId", networkId);
356+
sc.setParameters("vmTypeNEQ", VirtualMachine.Type.User);
357+
sc.setJoinParameters("vm", "state", new Object[] {VirtualMachine.State.Starting, VirtualMachine.State.Stopping, VirtualMachine.State.Migrating});
345358
List<Integer> results = customSearch(sc, null);
346359
return results.get(0);
347360
}

server/src/main/java/com/cloud/network/NetworkModelImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,10 +2559,11 @@ public boolean isNetworkReadyForGc(long networkId) {
25592559
return false;
25602560
}
25612561

2562-
//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)
2563-
//don't GC
2564-
if (_nicDao.countNicsForStartingVms(networkId) > 0) {
2565-
s_logger.debug("Network id=" + networkId + " is not ready for GC as it has vms that are Starting at the moment");
2562+
// if the network has user vms in Starting/Stopping/Migrating/Running state, or VRs in Starting/Stopping/Migrating state, don't GC
2563+
// 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.
2564+
// (nics for Starting VMs might not be allocated yet as Starting state also used when vm is being Created)
2565+
if (_nicDao.countNicsForNonStoppedVms(networkId) > 0 || _nicDao.countNicsForNonStoppedRunningVrs(networkId) > 0) {
2566+
s_logger.debug("Network id=" + networkId + " is not ready for GC as it has vms that are not Stopped at the moment");
25662567
return false;
25672568
}
25682569

0 commit comments

Comments
 (0)