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)

This commit is contained in:
Alena Prokharchyk 2013-08-05 14:12:25 -07:00
parent 4c89be144a
commit beabf596ba
3 changed files with 50 additions and 11 deletions

View File

@ -71,4 +71,6 @@ public interface NicDao extends GenericDao<NicVO, Long> {
NicVO findByInstanceIdAndIpAddressAndVmtype(long instanceId, String ipaddress, VirtualMachine.Type type);
List<NicVO> listByNetworkIdTypeAndGatewayAndBroadcastUri(long networkId, VirtualMachine.Type vmType, String gateway, URI broadcastUri);
int countNicsForStartingVms(long networkId);
}

View File

@ -16,8 +16,18 @@
// under the License.
package com.cloud.vm.dao;
import java.net.URI;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Local;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.GenericSearchBuilder;
import com.cloud.utils.db.JoinBuilder;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.SearchCriteria.Func;
@ -25,25 +35,27 @@ import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.vm.Nic;
import com.cloud.vm.Nic.State;
import com.cloud.vm.NicVO;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.VirtualMachine;
import org.springframework.stereotype.Component;
import javax.ejb.Local;
import java.net.URI;
import java.util.List;
@Component
@Local(value=NicDao.class)
public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
private final SearchBuilder<NicVO> AllFieldsSearch;
private final GenericSearchBuilder<NicVO, String> IpSearch;
private final SearchBuilder<NicVO> NonReleasedSearch;
final GenericSearchBuilder<NicVO, Integer> CountBy;
private SearchBuilder<NicVO> AllFieldsSearch;
private GenericSearchBuilder<NicVO, String> IpSearch;
private SearchBuilder<NicVO> NonReleasedSearch;
private GenericSearchBuilder<NicVO, Integer> CountBy;
private GenericSearchBuilder<NicVO, Integer> CountByForStartingVms;
@Inject
VMInstanceDao _vmDao;
public NicDaoImpl() {
super();
public NicDaoImpl() {
}
@PostConstruct
protected void init() {
AllFieldsSearch = createSearchBuilder();
AllFieldsSearch.and("instance", AllFieldsSearch.entity().getInstanceId(), Op.EQ);
AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ);
@ -73,6 +85,15 @@ public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
CountBy.and("vmId", CountBy.entity().getInstanceId(), Op.EQ);
CountBy.and("removed", CountBy.entity().getRemoved(), Op.NULL);
CountBy.done();
CountByForStartingVms = createSearchBuilder(Integer.class);
CountByForStartingVms.select(null, Func.COUNT, CountByForStartingVms.entity().getId());
CountByForStartingVms.and("networkId", CountByForStartingVms.entity().getNetworkId(), Op.EQ);
CountByForStartingVms.and("removed", CountByForStartingVms.entity().getRemoved(), Op.NULL);
SearchBuilder<VMInstanceVO> join1 = _vmDao.createSearchBuilder();
join1.and("state", join1.entity().getState(), Op.EQ);
CountByForStartingVms.join("vm", join1, CountByForStartingVms.entity().getInstanceId(), join1.entity().getId(), JoinBuilder.JoinType.INNER);
CountByForStartingVms.done();
}
@Override
@ -256,4 +277,13 @@ public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
return listBy(sc);
}
@Override
public int countNicsForStartingVms(long networkId) {
SearchCriteria<Integer> sc = CountByForStartingVms.create();
sc.setParameters("networkId", networkId);
sc.setJoinParameters("vm", "state", VirtualMachine.State.Starting);
List<Integer> results = customSearch(sc, null);
return results.get(0);
}
}

View File

@ -2169,6 +2169,13 @@ public class NetworkModelImpl extends ManagerBase implements NetworkModel {
networkIsConfiguredForExternalNetworking(network.getDataCenterId(), networkId)) {
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)
//don't GC
if (_nicDao.countNicsForStartingVms(networkId) > 0) {
s_logger.debug("Network id=" + networkId + " is not ready for GC as it has vms that are Starting at the moment");
return false;
}
return true;
}