Skip to content

Commit beabf59

Browse files
author
Alena Prokharchyk
committed
CLOUDSTACK-4080: don't GC the network when it has vms in Starting state even when the nics are not allocated for them yet (can happen when vm is being created)
1 parent 4c89be1 commit beabf59

3 files changed

Lines changed: 50 additions & 11 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,6 @@ public interface NicDao extends GenericDao<NicVO, Long> {
7171
NicVO findByInstanceIdAndIpAddressAndVmtype(long instanceId, String ipaddress, VirtualMachine.Type type);
7272

7373
List<NicVO> listByNetworkIdTypeAndGatewayAndBroadcastUri(long networkId, VirtualMachine.Type vmType, String gateway, URI broadcastUri);
74+
75+
int countNicsForStartingVms(long networkId);
7476
}

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

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,46 @@
1616
// under the License.
1717
package com.cloud.vm.dao;
1818

19+
import java.net.URI;
20+
import java.util.List;
21+
22+
import javax.annotation.PostConstruct;
23+
import javax.ejb.Local;
24+
import javax.inject.Inject;
25+
26+
import org.springframework.stereotype.Component;
27+
1928
import com.cloud.utils.db.GenericDaoBase;
2029
import com.cloud.utils.db.GenericSearchBuilder;
30+
import com.cloud.utils.db.JoinBuilder;
2131
import com.cloud.utils.db.SearchBuilder;
2232
import com.cloud.utils.db.SearchCriteria;
2333
import com.cloud.utils.db.SearchCriteria.Func;
2434
import com.cloud.utils.db.SearchCriteria.Op;
2535
import com.cloud.vm.Nic;
2636
import com.cloud.vm.Nic.State;
2737
import com.cloud.vm.NicVO;
38+
import com.cloud.vm.VMInstanceVO;
2839
import com.cloud.vm.VirtualMachine;
29-
import org.springframework.stereotype.Component;
30-
31-
import javax.ejb.Local;
32-
import java.net.URI;
33-
import java.util.List;
3440

3541
@Component
3642
@Local(value=NicDao.class)
3743
public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
38-
private final SearchBuilder<NicVO> AllFieldsSearch;
39-
private final GenericSearchBuilder<NicVO, String> IpSearch;
40-
private final SearchBuilder<NicVO> NonReleasedSearch;
41-
final GenericSearchBuilder<NicVO, Integer> CountBy;
44+
private SearchBuilder<NicVO> AllFieldsSearch;
45+
private GenericSearchBuilder<NicVO, String> IpSearch;
46+
private SearchBuilder<NicVO> NonReleasedSearch;
47+
private GenericSearchBuilder<NicVO, Integer> CountBy;
48+
private GenericSearchBuilder<NicVO, Integer> CountByForStartingVms;
4249

50+
@Inject
51+
VMInstanceDao _vmDao;
4352

44-
public NicDaoImpl() {
45-
super();
53+
public NicDaoImpl() {
4654

55+
}
56+
57+
@PostConstruct
58+
protected void init() {
4759
AllFieldsSearch = createSearchBuilder();
4860
AllFieldsSearch.and("instance", AllFieldsSearch.entity().getInstanceId(), Op.EQ);
4961
AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ);
@@ -73,6 +85,15 @@ public NicDaoImpl() {
7385
CountBy.and("vmId", CountBy.entity().getInstanceId(), Op.EQ);
7486
CountBy.and("removed", CountBy.entity().getRemoved(), Op.NULL);
7587
CountBy.done();
88+
89+
CountByForStartingVms = createSearchBuilder(Integer.class);
90+
CountByForStartingVms.select(null, Func.COUNT, CountByForStartingVms.entity().getId());
91+
CountByForStartingVms.and("networkId", CountByForStartingVms.entity().getNetworkId(), Op.EQ);
92+
CountByForStartingVms.and("removed", CountByForStartingVms.entity().getRemoved(), Op.NULL);
93+
SearchBuilder<VMInstanceVO> join1 = _vmDao.createSearchBuilder();
94+
join1.and("state", join1.entity().getState(), Op.EQ);
95+
CountByForStartingVms.join("vm", join1, CountByForStartingVms.entity().getInstanceId(), join1.entity().getId(), JoinBuilder.JoinType.INNER);
96+
CountByForStartingVms.done();
7697
}
7798

7899
@Override
@@ -256,4 +277,13 @@ public List<NicVO> listPlaceholderNicsByNetworkIdAndVmType(long networkId, Virtu
256277
return listBy(sc);
257278
}
258279

280+
@Override
281+
public int countNicsForStartingVms(long networkId) {
282+
SearchCriteria<Integer> sc = CountByForStartingVms.create();
283+
sc.setParameters("networkId", networkId);
284+
sc.setJoinParameters("vm", "state", VirtualMachine.State.Starting);
285+
List<Integer> results = customSearch(sc, null);
286+
return results.get(0);
287+
}
288+
259289
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,6 +2169,13 @@ public boolean isNetworkReadyForGc(long networkId) {
21692169
networkIsConfiguredForExternalNetworking(network.getDataCenterId(), networkId)) {
21702170
return false;
21712171
}
2172+
2173+
//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)
2174+
//don't GC
2175+
if (_nicDao.countNicsForStartingVms(networkId) > 0) {
2176+
s_logger.debug("Network id=" + networkId + " is not ready for GC as it has vms that are Starting at the moment");
2177+
return false;
2178+
}
21722179

21732180
return true;
21742181
}

0 commit comments

Comments
 (0)