use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.
the class ImplicitPlannerTest method initializeForImplicitPlannerTest.
private void initializeForImplicitPlannerTest(final boolean preferred) {
String plannerMode = new String("Strict");
if (preferred) {
plannerMode = new String("Preferred");
}
final Map<String, String> details = new HashMap<>();
details.put("ImplicitDedicationMode", plannerMode);
when(serviceOfferingDetailsDao.listDetailsKeyPairs(offeringId)).thenReturn(details);
// Initialize hosts in clusters
final HostVO host1 = mock(HostVO.class);
when(host1.getId()).thenReturn(5L);
final HostVO host2 = mock(HostVO.class);
when(host2.getId()).thenReturn(6L);
final HostVO host3 = mock(HostVO.class);
when(host3.getId()).thenReturn(7L);
final List<HostVO> hostsInCluster1 = new ArrayList<>();
final List<HostVO> hostsInCluster2 = new ArrayList<>();
final List<HostVO> hostsInCluster3 = new ArrayList<>();
hostsInCluster1.add(host1);
hostsInCluster2.add(host2);
hostsInCluster3.add(host3);
when(resourceMgr.listAllHostsInCluster(1)).thenReturn(hostsInCluster1);
when(resourceMgr.listAllHostsInCluster(2)).thenReturn(hostsInCluster2);
when(resourceMgr.listAllHostsInCluster(3)).thenReturn(hostsInCluster3);
// Mock vms on each host.
final long offeringIdForVmsOfThisAccount = 15L;
final long offeringIdForVmsOfOtherAccount = 16L;
final UserVmVO vm1 = mock(UserVmVO.class);
when(vm1.getAccountId()).thenReturn(accountId);
when(vm1.getServiceOfferingId()).thenReturn(offeringIdForVmsOfThisAccount);
final UserVmVO vm2 = mock(UserVmVO.class);
when(vm2.getAccountId()).thenReturn(accountId);
when(vm2.getServiceOfferingId()).thenReturn(offeringIdForVmsOfThisAccount);
// Vm from different account
final UserVmVO vm3 = mock(UserVmVO.class);
when(vm3.getAccountId()).thenReturn(201L);
when(vm3.getServiceOfferingId()).thenReturn(offeringIdForVmsOfOtherAccount);
final List<VMInstanceVO> vmsForHost1 = new ArrayList<>();
final List<VMInstanceVO> vmsForHost2 = new ArrayList<>();
final List<VMInstanceVO> vmsForHost3 = new ArrayList<>();
final List<VMInstanceVO> stoppedVmsForHost = new ArrayList<>();
// Host 2 is empty.
vmsForHost1.add(vm1);
vmsForHost1.add(vm2);
vmsForHost3.add(vm3);
when(vmInstanceDao.listUpByHostId(5L)).thenReturn(vmsForHost1);
when(vmInstanceDao.listUpByHostId(6L)).thenReturn(vmsForHost2);
when(vmInstanceDao.listUpByHostId(7L)).thenReturn(vmsForHost3);
when(vmInstanceDao.listByLastHostId(5L)).thenReturn(stoppedVmsForHost);
when(vmInstanceDao.listByLastHostId(6L)).thenReturn(stoppedVmsForHost);
when(vmInstanceDao.listByLastHostId(7L)).thenReturn(stoppedVmsForHost);
// Mock the offering with which the vm was created.
final ServiceOfferingVO offeringForVmOfThisAccount = mock(ServiceOfferingVO.class);
when(serviceOfferingDao.findByIdIncludingRemoved(offeringIdForVmsOfThisAccount)).thenReturn(offeringForVmOfThisAccount);
when(offeringForVmOfThisAccount.getDeploymentPlanner()).thenReturn(planner.getName());
final ServiceOfferingVO offeringForVMOfOtherAccount = mock(ServiceOfferingVO.class);
when(serviceOfferingDao.findByIdIncludingRemoved(offeringIdForVmsOfOtherAccount)).thenReturn(offeringForVMOfOtherAccount);
when(offeringForVMOfOtherAccount.getDeploymentPlanner()).thenReturn("FirstFitPlanner");
}
use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.
the class CapacityManagerImpl method postStateTransitionEvent.
@Override
public boolean postStateTransitionEvent(final StateMachine2.Transition<State, Event> transition, final VirtualMachine vm, final boolean status, final Object opaque) {
if (!status) {
return false;
}
final Pair<Long, Long> hosts = (Pair<Long, Long>) opaque;
final Long oldHostId = hosts.first();
final State oldState = transition.getCurrentState();
final State newState = transition.getToState();
final Event event = transition.getEvent();
s_logger.debug("VM state transitted from :" + oldState + " to " + newState + " with event: " + event + "vm's original host id: " + vm.getLastHostId() + " new host id: " + vm.getHostId() + " host id before state transition: " + oldHostId);
if (oldState == State.Starting) {
if (newState != State.Running) {
releaseVmCapacity(vm, false, false, oldHostId);
}
} else if (oldState == State.Running) {
if (event == Event.AgentReportStopped) {
releaseVmCapacity(vm, false, true, oldHostId);
} else if (event == Event.AgentReportMigrated) {
releaseVmCapacity(vm, false, false, oldHostId);
}
} else if (oldState == State.Migrating) {
if (event == Event.AgentReportStopped) {
/* Release capacity from original host */
releaseVmCapacity(vm, false, false, vm.getLastHostId());
releaseVmCapacity(vm, false, false, oldHostId);
} else if (event == Event.OperationFailed) {
/* Release from dest host */
releaseVmCapacity(vm, false, false, oldHostId);
} else if (event == Event.OperationSucceeded) {
releaseVmCapacity(vm, false, false, vm.getLastHostId());
}
} else if (oldState == State.Stopping) {
if (event == Event.OperationSucceeded) {
releaseVmCapacity(vm, false, true, oldHostId);
} else if (event == Event.AgentReportStopped) {
releaseVmCapacity(vm, false, false, oldHostId);
} else if (event == Event.AgentReportMigrated) {
releaseVmCapacity(vm, false, false, oldHostId);
}
} else if (oldState == State.Stopped) {
if (event == Event.DestroyRequested || event == Event.ExpungeOperation) {
releaseVmCapacity(vm, true, false, vm.getLastHostId());
} else if (event == Event.AgentReportMigrated) {
releaseVmCapacity(vm, false, false, oldHostId);
}
}
if ((newState == State.Starting || newState == State.Migrating || event == Event.AgentReportMigrated) && vm.getHostId() != null) {
boolean fromLastHost = false;
if (vm.getHostId().equals(vm.getLastHostId())) {
s_logger.debug("VM starting again on the last host it was stopped on");
fromLastHost = true;
}
allocateVmCapacity(vm, fromLastHost);
}
if (newState == State.Stopped) {
if (vm.getType() == VirtualMachine.Type.User) {
final UserVmVO userVM = _userVMDao.findById(vm.getId());
_userVMDao.loadDetails(userVM);
// free the message sent flag if it exists
userVM.setDetail(MESSAGE_RESERVED_CAPACITY_FREED_FLAG, "false");
_userVMDao.saveDetails(userVM);
}
}
return true;
}
use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.
the class CapacityManagerImpl method updateCapacityForHost.
@DB
@Override
public void updateCapacityForHost(final Host host) {
// prepare the service offerings
final List<ServiceOfferingVO> offerings = _offeringsDao.listAllIncludingRemoved();
final Map<Long, ServiceOfferingVO> offeringsMap = new HashMap<>();
for (final ServiceOfferingVO offering : offerings) {
offeringsMap.put(offering.getId(), offering);
}
long usedCpu = 0;
long usedMemory = 0;
long reservedMemory = 0;
long reservedCpu = 0;
final CapacityState capacityState = (host.getResourceState() == ResourceState.Enabled) ? CapacityState.Enabled : CapacityState.Disabled;
final List<VMInstanceVO> vms = _vmDao.listUpByHostId(host.getId());
if (s_logger.isDebugEnabled()) {
s_logger.debug("Found " + vms.size() + " VMs on host " + host.getId());
}
for (final VMInstanceVO vm : vms) {
final ServiceOffering so = offeringsMap.get(vm.getServiceOfferingId());
usedMemory += (so.getRamSize() * 1024L * 1024L);
usedCpu += so.getCpu();
}
final List<VMInstanceVO> vmsByLastHostId = _vmDao.listByLastHostId(host.getId());
if (s_logger.isDebugEnabled()) {
s_logger.debug("Found " + vmsByLastHostId.size() + " VM, not running on host " + host.getId());
}
for (final VMInstanceVO vm : vmsByLastHostId) {
final long secondsSinceLastUpdate = (DateUtil.currentGMTTime().getTime() - vm.getUpdateTime().getTime()) / 1000;
if (secondsSinceLastUpdate < _vmCapacityReleaseInterval) {
final ServiceOffering so = offeringsMap.get(vm.getServiceOfferingId());
reservedMemory += (so.getRamSize() * 1024L * 1024L);
reservedCpu += so.getCpu();
} else {
// signal if not done already, that the VM has been stopped for skip.counting.hours,
// hence capacity will not be reserved anymore.
final UserVmDetailVO messageSentFlag = _userVmDetailsDao.findDetail(vm.getId(), MESSAGE_RESERVED_CAPACITY_FREED_FLAG);
if (messageSentFlag == null || !Boolean.valueOf(messageSentFlag.getValue())) {
_messageBus.publish(_name, "VM_ReservedCapacity_Free", PublishScope.LOCAL, vm);
if (vm.getType() == VirtualMachine.Type.User) {
final UserVmVO userVM = _userVMDao.findById(vm.getId());
_userVMDao.loadDetails(userVM);
userVM.setDetail(MESSAGE_RESERVED_CAPACITY_FREED_FLAG, "true");
_userVMDao.saveDetails(userVM);
}
}
}
}
final CapacityVO cpuCap = _capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_CPU);
final CapacityVO memCap = _capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_MEMORY);
if (cpuCap != null && memCap != null) {
if (host.getTotalMemory() != null) {
memCap.setTotalCapacity(host.getTotalMemory());
}
final long hostTotalCpu = host.getCpus().longValue();
if (cpuCap.getTotalCapacity() != hostTotalCpu) {
s_logger.debug("Calibrate total cpu for host: " + host.getId() + " old total CPU:" + cpuCap.getTotalCapacity() + " new total CPU:" + hostTotalCpu);
cpuCap.setTotalCapacity(hostTotalCpu);
}
// Set the capacity state as per the host allocation state.
if (capacityState != cpuCap.getCapacityState()) {
s_logger.debug("Calibrate cpu capacity state for host: " + host.getId() + " old capacity state:" + cpuCap.getTotalCapacity() + " new capacity state:" + hostTotalCpu);
cpuCap.setCapacityState(capacityState);
}
memCap.setCapacityState(capacityState);
if (cpuCap.getUsedCapacity() == usedCpu && cpuCap.getReservedCapacity() == reservedCpu) {
s_logger.debug("No need to calibrate cpu capacity, host:" + host.getId() + " usedCpu: " + cpuCap.getUsedCapacity() + " reservedCpu: " + cpuCap.getReservedCapacity());
} else {
if (cpuCap.getReservedCapacity() != reservedCpu) {
s_logger.debug("Calibrate reserved cpu for host: " + host.getId() + " old reservedCpu:" + cpuCap.getReservedCapacity() + " new reservedCpu:" + reservedCpu);
cpuCap.setReservedCapacity(reservedCpu);
}
if (cpuCap.getUsedCapacity() != usedCpu) {
s_logger.debug("Calibrate used cpu for host: " + host.getId() + " old usedCpu:" + cpuCap.getUsedCapacity() + " new usedCpu:" + usedCpu);
cpuCap.setUsedCapacity(usedCpu);
}
}
if (memCap.getTotalCapacity() != host.getTotalMemory()) {
s_logger.debug("Calibrate total memory for host: " + host.getId() + " old total memory:" + memCap.getTotalCapacity() + " new total memory:" + host.getTotalMemory());
memCap.setTotalCapacity(host.getTotalMemory());
}
// Set the capacity state as per the host allocation state.
if (capacityState != memCap.getCapacityState()) {
s_logger.debug("Calibrate memory capacity state for host: " + host.getId() + " old capacity state:" + memCap.getTotalCapacity() + " new capacity state:" + hostTotalCpu);
memCap.setCapacityState(capacityState);
}
if (memCap.getUsedCapacity() == usedMemory && memCap.getReservedCapacity() == reservedMemory) {
s_logger.debug("No need to calibrate memory capacity, host:" + host.getId() + " usedMem: " + memCap.getUsedCapacity() + " reservedMem: " + memCap.getReservedCapacity());
} else {
if (memCap.getReservedCapacity() != reservedMemory) {
s_logger.debug("Calibrate reserved memory for host: " + host.getId() + " old reservedMem:" + memCap.getReservedCapacity() + " new reservedMem:" + reservedMemory);
memCap.setReservedCapacity(reservedMemory);
}
if (memCap.getUsedCapacity() != usedMemory) {
/*
* Didn't calibrate for used memory, because VMs can be in
* state(starting/migrating) that I don't know on which host
* they are allocated
*/
s_logger.debug("Calibrate used memory for host: " + host.getId() + " old usedMem: " + memCap.getUsedCapacity() + " new usedMem: " + usedMemory);
memCap.setUsedCapacity(usedMemory);
}
}
try {
_capacityDao.update(cpuCap.getId(), cpuCap);
_capacityDao.update(memCap.getId(), memCap);
} catch (final Exception e) {
s_logger.error("Caught exception while updating cpu/memory capacity for the host " + host.getId(), e);
}
} else {
final long usedMemoryFinal = usedMemory;
final long reservedMemoryFinal = reservedMemory;
final long usedCpuFinal = usedCpu;
final long reservedCpuFinal = reservedCpu;
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) {
CapacityVO capacity = new CapacityVO(host.getId(), host.getDataCenterId(), host.getPodId(), host.getClusterId(), usedMemoryFinal, host.getTotalMemory(), Capacity.CAPACITY_TYPE_MEMORY);
capacity.setReservedCapacity(reservedMemoryFinal);
capacity.setCapacityState(capacityState);
_capacityDao.persist(capacity);
capacity = new CapacityVO(host.getId(), host.getDataCenterId(), host.getPodId(), host.getClusterId(), usedCpuFinal, host.getCpus().longValue(), Capacity.CAPACITY_TYPE_CPU);
capacity.setReservedCapacity(reservedCpuFinal);
capacity.setCapacityState(capacityState);
_capacityDao.persist(capacity);
}
});
}
}
use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.
the class QueryManagerImpl method searchForAffinityGroupsInternal.
public Pair<List<AffinityGroupJoinVO>, Integer> searchForAffinityGroupsInternal(final ListAffinityGroupsCmd cmd) {
final Long affinityGroupId = cmd.getId();
final String affinityGroupName = cmd.getAffinityGroupName();
final String affinityGroupType = cmd.getAffinityGroupType();
final Long vmId = cmd.getVirtualMachineId();
final String accountName = cmd.getAccountName();
Long domainId = cmd.getDomainId();
final Long projectId = cmd.getProjectId();
Boolean isRecursive = cmd.isRecursive();
final Boolean listAll = cmd.listAll();
final Long startIndex = cmd.getStartIndex();
final Long pageSize = cmd.getPageSizeVal();
final String keyword = cmd.getKeyword();
final Account caller = CallContext.current().getCallingAccount();
if (vmId != null) {
final UserVmVO userVM = _userVmDao.findById(vmId);
if (userVM == null) {
throw new InvalidParameterValueException("Unable to list affinity groups for virtual machine instance " + vmId + "; instance not found.");
}
_accountMgr.checkAccess(caller, null, true, userVM);
return listAffinityGroupsByVM(vmId.longValue(), startIndex, pageSize);
}
final List<Long> permittedAccounts = new ArrayList<>();
final Ternary<Long, Boolean, ListProjectResourcesCriteria> ternary = new Ternary<>(domainId, isRecursive, null);
_accountMgr.buildACLSearchParameters(caller, affinityGroupId, accountName, projectId, permittedAccounts, ternary, listAll, false);
domainId = ternary.first();
isRecursive = ternary.second();
final ListProjectResourcesCriteria listProjectResourcesCriteria = ternary.third();
final Filter searchFilter = new Filter(AffinityGroupJoinVO.class, ID_FIELD, true, startIndex, pageSize);
final SearchCriteria<AffinityGroupJoinVO> sc = buildAffinityGroupSearchCriteria(domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria, affinityGroupId, affinityGroupName, affinityGroupType, keyword);
final Pair<List<AffinityGroupJoinVO>, Integer> uniqueGroupsPair = _affinityGroupJoinDao.searchAndCount(sc, searchFilter);
// search group details by ids
List<AffinityGroupJoinVO> affinityGroups = new ArrayList<>();
final Integer count = uniqueGroupsPair.second();
if (count.intValue() != 0) {
final List<AffinityGroupJoinVO> uniqueGroups = uniqueGroupsPair.first();
final Long[] vrIds = new Long[uniqueGroups.size()];
int i = 0;
for (final AffinityGroupJoinVO v : uniqueGroups) {
vrIds[i++] = v.getId();
}
affinityGroups = _affinityGroupJoinDao.searchByIds(vrIds);
}
if (!permittedAccounts.isEmpty()) {
// add domain level affinity groups
if (domainId != null) {
final SearchCriteria<AffinityGroupJoinVO> scDomain = buildAffinityGroupSearchCriteria(null, isRecursive, new ArrayList<>(), listProjectResourcesCriteria, affinityGroupId, affinityGroupName, affinityGroupType, keyword);
affinityGroups.addAll(listDomainLevelAffinityGroups(scDomain, searchFilter, domainId));
} else {
for (final Long permAcctId : permittedAccounts) {
final Account permittedAcct = _accountDao.findById(permAcctId);
final SearchCriteria<AffinityGroupJoinVO> scDomain = buildAffinityGroupSearchCriteria(null, isRecursive, new ArrayList<>(), listProjectResourcesCriteria, affinityGroupId, affinityGroupName, affinityGroupType, keyword);
affinityGroups.addAll(listDomainLevelAffinityGroups(scDomain, searchFilter, permittedAcct.getDomainId()));
}
}
} else if (permittedAccounts.isEmpty() && domainId != null && isRecursive) {
// list all domain level affinity groups for the domain admin case
final SearchCriteria<AffinityGroupJoinVO> scDomain = buildAffinityGroupSearchCriteria(null, isRecursive, new ArrayList<>(), listProjectResourcesCriteria, affinityGroupId, affinityGroupName, affinityGroupType, keyword);
affinityGroups.addAll(listDomainLevelAffinityGroups(scDomain, searchFilter, domainId));
}
return new Pair<>(affinityGroups, affinityGroups.size());
}
use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.
the class AgentBasedConsoleProxyManager method assignProxy.
@Override
public ConsoleProxyInfo assignProxy(final long dataCenterId, final long userVmId) {
final UserVmVO userVm = _userVmDao.findById(userVmId);
if (userVm == null) {
s_logger.warn("User VM " + userVmId + " no longer exists, return a null proxy for user vm:" + userVmId);
return null;
}
final HostVO host = findHost(userVm);
if (host != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Assign embedded console proxy running at " + host.getName() + " to user vm " + userVmId + " with public IP " + host.getPublicIpAddress());
}
// only private IP, public IP, host id have meaningful values, rest
// of all are place-holder values
String publicIp = host.getPublicIpAddress();
if (publicIp == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Host " + host.getName() + "/" + host.getPrivateIpAddress() + " does not have public interface, we will return its private IP for cosole proxy.");
}
publicIp = host.getPrivateIpAddress();
}
int urlPort = _consoleProxyUrlPort;
if (host.getProxyPort() != null && host.getProxyPort().intValue() > 0) {
urlPort = host.getProxyPort().intValue();
}
return new ConsoleProxyInfo(_sslEnabled, publicIp, _consoleProxyPort, urlPort, _consoleProxyUrlDomain);
} else {
s_logger.warn("Host that VM is running is no longer available, console access to VM " + userVmId + " will be temporarily unavailable.");
}
return null;
}
Aggregations