mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-10-26 08:42:29 +01:00 
			
		
		
		
	bug 14454: simulator: add limitation on how many vms can be created on a host. status 14454: resolved fixed; Reviewed-by: sangeetha
This commit is contained in:
		
							parent
							
								
									1e4893b69c
								
							
						
					
					
						commit
						56dd6e81ac
					
				| @ -5,6 +5,7 @@ | ||||
| package com.cloud.agent.manager; | ||||
| 
 | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| import com.cloud.agent.api.Answer; | ||||
| @ -33,6 +34,7 @@ import com.cloud.agent.api.routing.SavePasswordCommand; | ||||
| import com.cloud.agent.api.routing.SetPortForwardingRulesCommand; | ||||
| import com.cloud.agent.api.routing.SetStaticNatRulesCommand; | ||||
| import com.cloud.agent.api.routing.VmDataCommand; | ||||
| import com.cloud.simulator.MockVMVO; | ||||
| import com.cloud.utils.Pair; | ||||
| import com.cloud.utils.component.Manager; | ||||
| import com.cloud.vm.VirtualMachine.State; | ||||
| @ -73,5 +75,6 @@ public interface MockVmManager extends Manager { | ||||
|     SecurityGroupRuleAnswer AddSecurityGroupRules(SecurityGroupRulesCmd cmd, SimulatorInfo info); | ||||
| 	MigrateAnswer Migrate(MigrateCommand cmd, SimulatorInfo info); | ||||
| 	GetDomRVersionAnswer getDomRVersion(GetDomRVersionCmd cmd); | ||||
| 	Map<String, MockVMVO> getVms(String hostGuid); | ||||
|      | ||||
| } | ||||
|  | ||||
| @ -173,6 +173,16 @@ public class MockVmManagerImpl implements MockVmManager { | ||||
| 	    return true; | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
|     public Map<String, MockVMVO> getVms(String hostGuid) { | ||||
| 		List<MockVMVO> vms = _mockVmDao.findByHostGuid(hostGuid); | ||||
| 		Map<String, MockVMVO> vmMap = new HashMap<String, MockVMVO>(); | ||||
| 		for (MockVMVO vm : vms) { | ||||
| 			vmMap.put(vm.getName(), vm); | ||||
| 		} | ||||
| 		return vmMap; | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
|     public Map<String, State> getVmStates(String hostGuid) { | ||||
| 		Map<String, State> states = new HashMap<String, State>(); | ||||
|  | ||||
| @ -20,11 +20,14 @@ | ||||
| package com.cloud.agent.manager; | ||||
| 
 | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| import com.cloud.agent.api.Answer; | ||||
| import com.cloud.agent.api.Command; | ||||
| import com.cloud.agent.api.StoragePoolInfo; | ||||
| import com.cloud.agent.mockvm.MockVm; | ||||
| import com.cloud.simulator.MockVMVO; | ||||
| import com.cloud.utils.Pair; | ||||
| import com.cloud.utils.component.Manager; | ||||
| import com.cloud.vm.VirtualMachine.State; | ||||
| @ -61,4 +64,6 @@ public interface SimulatorManager extends Manager { | ||||
|     public HashMap<String, Pair<Long, Long>> syncNetworkGroups(String hostGuid); | ||||
| 
 | ||||
|     Map<String, State> getVmStates(String hostGuid); | ||||
| 
 | ||||
| 	Map<String, MockVMVO> getVms(String hostGuid); | ||||
| } | ||||
| @ -3,6 +3,7 @@ package com.cloud.agent.manager; | ||||
| import java.sql.Connection; | ||||
| import java.sql.SQLException; | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| import javax.ejb.Local; | ||||
| @ -62,8 +63,10 @@ import com.cloud.agent.api.storage.DownloadCommand; | ||||
| import com.cloud.agent.api.storage.DownloadProgressCommand; | ||||
| import com.cloud.agent.api.storage.ListTemplateCommand; | ||||
| import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand; | ||||
| import com.cloud.agent.mockvm.MockVm; | ||||
| import com.cloud.simulator.MockConfigurationVO; | ||||
| import com.cloud.simulator.MockHost; | ||||
| import com.cloud.simulator.MockVMVO; | ||||
| import com.cloud.simulator.dao.MockConfigurationDao; | ||||
| import com.cloud.simulator.dao.MockHostDao; | ||||
| import com.cloud.utils.Pair; | ||||
| @ -317,6 +320,18 @@ public class SimulatorManagerImpl implements SimulatorManager { | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     @Override | ||||
|     @DB | ||||
|     public Map<String, MockVMVO> getVms(String hostGuid) { | ||||
|         Transaction txn = Transaction.currentTxn(); | ||||
|         txn.transitToUserManagedConnection(_concierge.conn()); | ||||
|         try { | ||||
|             return _mockVmMgr.getVms(hostGuid); | ||||
|         } finally { | ||||
|             txn.transitToAutoManagedConnection(Transaction.CLOUD_DB); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public HashMap<String, Pair<Long, Long>> syncNetworkGroups(String hostGuid) { | ||||
|     	SimulatorInfo info = new SimulatorInfo(); | ||||
|  | ||||
| @ -8,6 +8,8 @@ import java.util.ArrayList; | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.Map.Entry; | ||||
| import java.util.Set; | ||||
| 
 | ||||
| import javax.naming.ConfigurationException; | ||||
| 
 | ||||
| @ -38,6 +40,7 @@ import com.cloud.host.Host; | ||||
| import com.cloud.host.Host.Type; | ||||
| import com.cloud.hypervisor.Hypervisor.HypervisorType; | ||||
| import com.cloud.network.Networks.RouterPrivateIpStrategy; | ||||
| import com.cloud.simulator.MockVMVO; | ||||
| import com.cloud.storage.Storage.StorageResourceType; | ||||
| import com.cloud.storage.template.TemplateInfo; | ||||
| import com.cloud.utils.Pair; | ||||
| @ -47,6 +50,11 @@ public class AgentRoutingResource extends AgentStorageResource { | ||||
|     private static final Logger s_logger = Logger.getLogger(AgentRoutingResource.class); | ||||
| 
 | ||||
|     protected Map<String, State> _vms = new HashMap<String, State>(); | ||||
|     private Map<String, Pair<Long, Long>> _runningVms = new HashMap<String, Pair<Long, Long>>(); | ||||
|     long usedCpu = 0; | ||||
|     long usedMem = 0; | ||||
|     long totalCpu; | ||||
|     long totalMem; | ||||
|     protected String _mountParent; | ||||
| 
 | ||||
| 
 | ||||
| @ -108,6 +116,16 @@ public class AgentRoutingResource extends AgentStorageResource { | ||||
|             _vms.clear(); | ||||
|         } | ||||
|         Map<String, State> changes = _simMgr.getVmStates(this.hostGuid); | ||||
|         Map<String, MockVMVO> vmsMaps = _simMgr.getVms(this.hostGuid); | ||||
|         totalCpu = agentHost.getCpuCount() * agentHost.getCpuSpeed(); | ||||
|         totalMem = agentHost.getMemorySize(); | ||||
|         for (Map.Entry<String, MockVMVO> entry : vmsMaps.entrySet()) { | ||||
|         	MockVMVO vm = entry.getValue(); | ||||
|         	usedCpu += vm.getCpu(); | ||||
|         	usedMem += vm.getMemory(); | ||||
|         	_runningVms.put(entry.getKey(), new Pair<Long, Long>(Long.valueOf(vm.getCpu()), vm.getMemory())); | ||||
|         } | ||||
|          | ||||
|         List<Object> info = getHostInfo(); | ||||
| 
 | ||||
|         StartupRoutingCommand cmd = new StartupRoutingCommand((Integer) info.get(0), (Long) info.get(1), (Long) info.get(2), (Long) info.get(4), (String) info.get(3), HypervisorType.Simulator, | ||||
| @ -159,7 +177,10 @@ public class AgentRoutingResource extends AgentStorageResource { | ||||
| 			throws IllegalArgumentException { | ||||
| 		VirtualMachineTO vmSpec = cmd.getVirtualMachine(); | ||||
| 		String vmName = vmSpec.getName(); | ||||
| 
 | ||||
| 		if (this.totalCpu < (vmSpec.getCpus() * vmSpec.getSpeed() + this.usedCpu) || | ||||
| 			this.totalMem < (vmSpec.getMaxRam() + this.usedMem)) { | ||||
| 			return new StartAnswer(cmd, "No enough resource to start the vm");  | ||||
| 		} | ||||
| 		State state = State.Stopped; | ||||
| 		synchronized (_vms) { | ||||
| 			_vms.put(vmName, State.Starting); | ||||
| @ -171,6 +192,9 @@ public class AgentRoutingResource extends AgentStorageResource { | ||||
| 		        return new StartAnswer(cmd, result.getDetails()); | ||||
| 		    } | ||||
| 		     | ||||
| 		    this.usedCpu += vmSpec.getCpus() * vmSpec.getSpeed(); | ||||
| 		    this.usedMem += vmSpec.getMaxRam(); | ||||
| 		    _runningVms.put(vmName, new Pair<Long, Long>(Long.valueOf(vmSpec.getCpus() * vmSpec.getSpeed()), vmSpec.getMaxRam())); | ||||
| 		    state = State.Running; | ||||
| 
 | ||||
| 		} finally { | ||||
| @ -201,7 +225,11 @@ public class AgentRoutingResource extends AgentStorageResource { | ||||
| 		    } | ||||
| 		     | ||||
| 			answer = new StopAnswer(cmd, null, 0, new Long(100), new Long(200)); | ||||
| 			 | ||||
| 			Pair<Long, Long> data = _runningVms.get(vmName); | ||||
| 			if (data != null) { | ||||
| 				this.usedCpu -= data.first(); | ||||
| 				this.usedMem -= data.second(); | ||||
| 			} | ||||
| 			state = State.Stopped; | ||||
| 			 | ||||
| 		} finally { | ||||
|  | ||||
| @ -14,6 +14,7 @@ import com.cloud.utils.db.GenericDaoBase; | ||||
| import com.cloud.utils.db.JoinBuilder; | ||||
| import com.cloud.utils.db.SearchBuilder; | ||||
| import com.cloud.utils.db.SearchCriteria; | ||||
| import com.cloud.vm.VirtualMachine; | ||||
| 
 | ||||
| @Local(value={MockVMDao.class}) | ||||
| public class MockVMDaoImpl extends GenericDaoBase<MockVMVO, Long> implements MockVMDao { | ||||
| @ -37,6 +38,7 @@ public class MockVMDaoImpl extends GenericDaoBase<MockVMVO, Long> implements Moc | ||||
|     public List<MockVMVO> findByHostGuid(String guid) { | ||||
|         SearchCriteria<MockVMVO> sc = GuidSearch.create(); | ||||
|         sc.setJoinParameters("host", "guid", guid); | ||||
|         sc.setParameters("state", VirtualMachine.State.Running); | ||||
|         return listBy(sc); | ||||
|     } | ||||
|      | ||||
| @ -55,6 +57,7 @@ public class MockVMDaoImpl extends GenericDaoBase<MockVMVO, Long> implements Moc | ||||
|          | ||||
|         GuidSearch = createSearchBuilder(); | ||||
|         GuidSearch.join("host", host, host.entity().getId(), GuidSearch.entity().getHostId(), JoinBuilder.JoinType.INNER); | ||||
|         GuidSearch.and("state", GuidSearch.entity().getState(), SearchCriteria.Op.EQ); | ||||
|         GuidSearch.done(); | ||||
|          | ||||
|         vmNameSearch = createSearchBuilder(); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user