Skip to content

Commit 1e47589

Browse files
Jayapalyadvr
authored andcommitted
CLOUDSTACK-7872: network getting shutdown inspite of running VM's in the network
(cherry picked from commit 709bf07) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> Conflicts: engine/schema/src/com/cloud/vm/dao/NicDaoImpl.java
1 parent 23a03ac commit 1e47589

4 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ protected void init() {
226226
GarbageCollectedSearch = createSearchBuilder(Long.class);
227227
GarbageCollectedSearch.selectFields(GarbageCollectedSearch.entity().getId());
228228
SearchBuilder<NetworkOpVO> join7 = _ntwkOpDao.createSearchBuilder();
229-
join7.and("activenics", join7.entity().getActiveNicsCount(), Op.EQ);
230229
join7.and("gc", join7.entity().isGarbageCollected(), Op.EQ);
231230
join7.and("check", join7.entity().isCheckForGc(), Op.EQ);
232231
GarbageCollectedSearch.join("ntwkOpGC", join7, GarbageCollectedSearch.entity().getId(), join7.entity().getId(), JoinBuilder.JoinType.INNER);
@@ -420,7 +419,6 @@ public int getActiveNicsIn(long networkId) {
420419
public List<Long> findNetworksToGarbageCollect() {
421420
SearchCriteria<Long> sc = GarbageCollectedSearch.create();
422421
sc.setJoinParameters("ntwkOffGC", "isPersistent", false);
423-
sc.setJoinParameters("ntwkOpGC", "activenics", 0);
424422
sc.setJoinParameters("ntwkOpGC", "gc", true);
425423
sc.setJoinParameters("ntwkOpGC", "check", true);
426424
return customSearch(sc, null);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,6 @@ public interface NicDao extends GenericDao<NicVO, Long> {
7373
List<NicVO> listByNetworkIdTypeAndGatewayAndBroadcastUri(long networkId, VirtualMachine.Type vmType, String gateway, URI broadcastUri);
7474

7575
int countNicsForStartingVms(long networkId);
76+
77+
int countNicsForRunningVms(long networkId);
7678
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
4646
private SearchBuilder<NicVO> NonReleasedSearch;
4747
private GenericSearchBuilder<NicVO, Integer> CountBy;
4848
private GenericSearchBuilder<NicVO, Integer> CountByForStartingVms;
49+
private GenericSearchBuilder<NicVO, Integer> CountByForRunningVms;
4950

5051
@Inject
5152
VMInstanceDao _vmDao;
@@ -95,6 +96,17 @@ protected void init() {
9596
join1.and("state", join1.entity().getState(), Op.EQ);
9697
CountByForStartingVms.join("vm", join1, CountByForStartingVms.entity().getInstanceId(), join1.entity().getId(), JoinBuilder.JoinType.INNER);
9798
CountByForStartingVms.done();
99+
100+
CountByForRunningVms = createSearchBuilder(Integer.class);
101+
CountByForRunningVms.select(null, Func.COUNT, CountByForRunningVms.entity().getId());
102+
CountByForRunningVms.and("networkId", CountByForRunningVms.entity().getNetworkId(), Op.EQ);
103+
CountByForRunningVms.and("removed", CountByForRunningVms.entity().getRemoved(), Op.NULL);
104+
SearchBuilder<VMInstanceVO> join2 = _vmDao.createSearchBuilder();
105+
join2.and("state", join2.entity().getState(), Op.EQ);
106+
join2.and("type", join2.entity().getType(), Op.EQ);
107+
CountByForRunningVms.join("vm", join2, CountByForRunningVms.entity().getInstanceId(), join2.entity().getId(), JoinBuilder.JoinType.INNER);
108+
CountByForRunningVms.done();
109+
98110
}
99111

100112
@Override
@@ -287,4 +299,13 @@ public int countNicsForStartingVms(long networkId) {
287299
return results.get(0);
288300
}
289301

302+
@Override
303+
public int countNicsForRunningVms(long networkId) {
304+
SearchCriteria<Integer> sc = CountByForRunningVms.create();
305+
sc.setParameters("networkId", networkId);
306+
sc.setJoinParameters("vm", "state", VirtualMachine.State.Running);
307+
sc.setJoinParameters("vm", "type", VirtualMachine.Type.User);
308+
List<Integer> results = customSearch(sc, null);
309+
return results.get(0);
310+
}
290311
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,13 @@ public boolean isNetworkReadyForGc(long networkId) {
21982198
return false;
21992199
}
22002200

2201+
// Due to VMSync issue, there can be cases where nic count is zero, but there can be VM's running in the network
2202+
// so add extra guard to check if network GC is actially required.
2203+
if (_nicDao.countNicsForRunningVms(networkId) > 0) {
2204+
s_logger.debug("Network id=" + networkId + " is not ready for GC as it has vms that are Running at the moment");
2205+
return false;
2206+
}
2207+
22012208
return true;
22022209
}
22032210

0 commit comments

Comments
 (0)