Skip to content

Commit 0f10ac4

Browse files
weizhouapachedhslove
authored andcommitted
server: check if there are active nics before network GC (apache#8204)
1 parent aeae5b5 commit 0f10ac4

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);
@@ -346,10 +348,21 @@ public List<NicVO> listPlaceholderNicsByNetworkIdAndVmType(long networkId, Virtu
346348
}
347349

348350
@Override
349-
public int countNicsForStartingVms(long networkId) {
350-
SearchCriteria<Integer> sc = CountByForStartingVms.create();
351+
public int countNicsForNonStoppedVms(long networkId) {
352+
SearchCriteria<Integer> sc = CountByForNonStoppedVms.create();
351353
sc.setParameters("networkId", networkId);
352-
sc.setJoinParameters("vm", "state", VirtualMachine.State.Starting);
354+
sc.setParameters("vmType", VirtualMachine.Type.User);
355+
sc.setJoinParameters("vm", "state", new Object[] {VirtualMachine.State.Starting, VirtualMachine.State.Running, VirtualMachine.State.Stopping, VirtualMachine.State.Migrating});
356+
List<Integer> results = customSearch(sc, null);
357+
return results.get(0);
358+
}
359+
360+
@Override
361+
public int countNicsForNonStoppedRunningVrs(long networkId) {
362+
SearchCriteria<Integer> sc = CountByForNonStoppedVms.create();
363+
sc.setParameters("networkId", networkId);
364+
sc.setParameters("vmTypeNEQ", VirtualMachine.Type.User);
365+
sc.setJoinParameters("vm", "state", new Object[] {VirtualMachine.State.Starting, VirtualMachine.State.Stopping, VirtualMachine.State.Migrating});
353366
List<Integer> results = customSearch(sc, null);
354367
return results.get(0);
355368
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,10 +2569,11 @@ public boolean isNetworkReadyForGc(long networkId) {
25692569
return false;
25702570
}
25712571

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

0 commit comments

Comments
 (0)