Search in sources :

Example 46 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cosmic by MissionCriticalCloud.

the class DomainManagerImpl method cleanupDomain.

private boolean cleanupDomain(final Long domainId, final Long ownerId) throws ConcurrentOperationException, ResourceUnavailableException {
    s_logger.debug("Cleaning up domain id=" + domainId);
    boolean success = true;
    {
        final DomainVO domainHandle = _domainDao.findById(domainId);
        domainHandle.setState(Domain.State.Inactive);
        _domainDao.update(domainId, domainHandle);
        final SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
        sc.addAnd("parent", SearchCriteria.Op.EQ, domainId);
        final List<DomainVO> domains = _domainDao.search(sc, null);
        final SearchCriteria<DomainVO> sc1 = _domainDao.createSearchCriteria();
        sc1.addAnd("path", SearchCriteria.Op.LIKE, "%" + domainHandle.getPath() + "%");
        final List<DomainVO> domainsToBeInactivated = _domainDao.search(sc1, null);
        // update all subdomains to inactive so no accounts/users can be created
        for (final DomainVO domain : domainsToBeInactivated) {
            domain.setState(Domain.State.Inactive);
            _domainDao.update(domain.getId(), domain);
        }
        // cleanup sub-domains first
        for (final DomainVO domain : domains) {
            success = (success && cleanupDomain(domain.getId(), domain.getAccountId()));
            if (!success) {
                s_logger.warn("Failed to cleanup domain id=" + domain.getId());
            }
        }
    }
    // delete users which will also delete accounts and release resources for those accounts
    final SearchCriteria<AccountVO> sc = _accountDao.createSearchCriteria();
    sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId);
    final List<AccountVO> accounts = _accountDao.search(sc, null);
    for (final AccountVO account : accounts) {
        if (account.getType() != Account.ACCOUNT_TYPE_PROJECT) {
            s_logger.debug("Deleting account " + account + " as a part of domain id=" + domainId + " cleanup");
            final boolean deleteAccount = _accountMgr.deleteAccount(account, CallContext.current().getCallingUserId(), CallContext.current().getCallingAccount());
            if (!deleteAccount) {
                s_logger.warn("Failed to cleanup account id=" + account.getId() + " as a part of domain cleanup");
            }
            success = (success && deleteAccount);
        } else {
            final ProjectVO project = _projectDao.findByProjectAccountId(account.getId());
            s_logger.debug("Deleting project " + project + " as a part of domain id=" + domainId + " cleanup");
            final boolean deleteProject = _projectMgr.deleteProject(CallContext.current().getCallingAccount(), CallContext.current().getCallingUserId(), project);
            if (!deleteProject) {
                s_logger.warn("Failed to cleanup project " + project + " as a part of domain cleanup");
            }
            success = (success && deleteProject);
        }
    }
    // delete the domain shared networks
    boolean networksDeleted = true;
    s_logger.debug("Deleting networks for domain id=" + domainId);
    final List<Long> networkIds = _networkDomainDao.listNetworkIdsByDomain(domainId);
    final CallContext ctx = CallContext.current();
    final ReservationContext context = new ReservationContextImpl(null, null, _accountMgr.getActiveUser(ctx.getCallingUserId()), ctx.getCallingAccount());
    for (final Long networkId : networkIds) {
        s_logger.debug("Deleting network id=" + networkId + " as a part of domain id=" + domainId + " cleanup");
        if (!_networkMgr.destroyNetwork(networkId, context, false)) {
            s_logger.warn("Unable to destroy network id=" + networkId + " as a part of domain id=" + domainId + " cleanup.");
            networksDeleted = false;
        } else {
            s_logger.debug("Network " + networkId + " successfully deleted as a part of domain id=" + domainId + " cleanup.");
        }
    }
    // don't proceed if networks failed to cleanup. The cleanup will be performed for inactive domain once again
    if (!networksDeleted) {
        s_logger.debug("Failed to delete the shared networks as a part of domain id=" + domainId + " clenaup");
        return false;
    }
    // don't remove the domain if there are accounts required cleanup
    final boolean deleteDomainSuccess;
    final List<AccountVO> accountsForCleanup = _accountDao.findCleanupsForRemovedAccounts(domainId);
    if (accountsForCleanup.isEmpty()) {
        // release dedication if any, before deleting the domain
        final List<DedicatedResourceVO> dedicatedResources = _dedicatedDao.listByDomainId(domainId);
        if (dedicatedResources != null && !dedicatedResources.isEmpty()) {
            s_logger.debug("Releasing dedicated resources for domain" + domainId);
            for (final DedicatedResourceVO dr : dedicatedResources) {
                if (!_dedicatedDao.remove(dr.getId())) {
                    s_logger.warn("Fail to release dedicated resources for domain " + domainId);
                    return false;
                }
            }
        }
        // delete domain
        deleteDomainSuccess = _domainDao.remove(domainId);
        // Delete resource count and resource limits entries set for this domain (if there are any).
        _resourceCountDao.removeEntriesByOwner(domainId, ResourceOwnerType.Domain);
        _resourceLimitDao.removeEntriesByOwner(domainId, ResourceOwnerType.Domain);
    } else {
        s_logger.debug("Can't delete the domain yet because it has " + accountsForCleanup.size() + "accounts that need a cleanup");
        return false;
    }
    return success && deleteDomainSuccess;
}
Also used : CallContext(com.cloud.context.CallContext) ReservationContextImpl(com.cloud.vm.ReservationContextImpl) SearchCriteria(com.cloud.utils.db.SearchCriteria) ProjectVO(com.cloud.projects.ProjectVO) ReservationContext(com.cloud.vm.ReservationContext) DomainVO(com.cloud.domain.DomainVO) List(java.util.List) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO)

Example 47 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cloudstack by apache.

the class HostDetailsDaoImpl method deleteDetails.

@Override
public void deleteDetails(long hostId) {
    SearchCriteria sc = HostSearch.create();
    sc.setParameters("hostId", hostId);
    List<DetailVO> results = search(sc, null);
    for (DetailVO result : results) {
        remove(result.getId());
    }
}
Also used : DetailVO(com.cloud.host.DetailVO) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 48 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cloudstack by apache.

the class DeploymentPlanningManagerImpl method finalizeReservation.

@DB
@Override
public String finalizeReservation(final DeployDestination plannedDestination, final VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoids, final DeploymentPlanner planner) throws InsufficientServerCapacityException, AffinityConflictException {
    final VirtualMachine vm = vmProfile.getVirtualMachine();
    final long vmGroupCount = _affinityGroupVMMapDao.countAffinityGroupsForVm(vm.getId());
    return Transaction.execute(new TransactionCallback<String>() {

        @Override
        public String doInTransaction(TransactionStatus status) {
            boolean saveReservation = true;
            if (vmGroupCount > 0) {
                List<Long> groupIds = _affinityGroupVMMapDao.listAffinityGroupIdsByVmId(vm.getId());
                SearchCriteria<AffinityGroupVO> criteria = _affinityGroupDao.createSearchCriteria();
                criteria.addAnd("id", SearchCriteria.Op.IN, groupIds.toArray(new Object[groupIds.size()]));
                _affinityGroupDao.lockRows(criteria, null, true);
                for (AffinityGroupProcessor processor : _affinityProcessors) {
                    if (!processor.check(vmProfile, plannedDestination)) {
                        saveReservation = false;
                        break;
                    }
                }
            }
            if (saveReservation) {
                VMReservationVO vmReservation = new VMReservationVO(vm.getId(), plannedDestination.getDataCenter().getId(), plannedDestination.getPod().getId(), plannedDestination.getCluster().getId(), plannedDestination.getHost().getId());
                if (planner != null) {
                    vmReservation.setDeploymentPlanner(planner.getName());
                }
                Map<Long, Long> volumeReservationMap = new HashMap<Long, Long>();
                if (vm.getHypervisorType() != HypervisorType.BareMetal) {
                    for (Volume vo : plannedDestination.getStorageForDisks().keySet()) {
                        volumeReservationMap.put(vo.getId(), plannedDestination.getStorageForDisks().get(vo).getId());
                    }
                    vmReservation.setVolumeReservation(volumeReservationMap);
                }
                _reservationDao.persist(vmReservation);
                return vmReservation.getUuid();
            }
            return null;
        }
    });
}
Also used : TransactionStatus(com.cloud.utils.db.TransactionStatus) SearchCriteria(com.cloud.utils.db.SearchCriteria) VMReservationVO(org.apache.cloudstack.engine.cloud.entity.api.db.VMReservationVO) Volume(com.cloud.storage.Volume) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) AffinityGroupProcessor(org.apache.cloudstack.affinity.AffinityGroupProcessor) Map(java.util.Map) HashMap(java.util.HashMap) VirtualMachine(com.cloud.vm.VirtualMachine) DB(com.cloud.utils.db.DB)

Example 49 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cloudstack by apache.

the class UserVmManagerImpl method collectVmDiskStatistics.

@Override
public void collectVmDiskStatistics(final UserVm userVm) {
    // Only supported for KVM and VMware
    if (!(userVm.getHypervisorType().equals(HypervisorType.KVM) || userVm.getHypervisorType().equals(HypervisorType.VMware))) {
        return;
    }
    s_logger.debug("Collect vm disk statistics from host before stopping VM");
    if (userVm.getHostId() == null) {
        s_logger.error("Unable to collect vm disk statistics for VM as the host is null, skipping VM disk statistics collection");
        return;
    }
    long hostId = userVm.getHostId();
    List<String> vmNames = new ArrayList<String>();
    vmNames.add(userVm.getInstanceName());
    final HostVO host = _hostDao.findById(hostId);
    GetVmDiskStatsAnswer diskStatsAnswer = null;
    try {
        diskStatsAnswer = (GetVmDiskStatsAnswer) _agentMgr.easySend(hostId, new GetVmDiskStatsCommand(vmNames, host.getGuid(), host.getName()));
    } catch (Exception e) {
        s_logger.warn("Error while collecting disk stats for vm: " + userVm.getInstanceName() + " from host: " + host.getName(), e);
        return;
    }
    if (diskStatsAnswer != null) {
        if (!diskStatsAnswer.getResult()) {
            s_logger.warn("Error while collecting disk stats vm: " + userVm.getInstanceName() + " from host: " + host.getName() + "; details: " + diskStatsAnswer.getDetails());
            return;
        }
        try {
            final GetVmDiskStatsAnswer diskStatsAnswerFinal = diskStatsAnswer;
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    HashMap<String, List<VmDiskStatsEntry>> vmDiskStatsByName = diskStatsAnswerFinal.getVmDiskStatsMap();
                    if (vmDiskStatsByName == null) {
                        return;
                    }
                    List<VmDiskStatsEntry> vmDiskStats = vmDiskStatsByName.get(userVm.getInstanceName());
                    if (vmDiskStats == null) {
                        return;
                    }
                    for (VmDiskStatsEntry vmDiskStat : vmDiskStats) {
                        SearchCriteria<VolumeVO> sc_volume = _volsDao.createSearchCriteria();
                        sc_volume.addAnd("path", SearchCriteria.Op.EQ, vmDiskStat.getPath());
                        List<VolumeVO> volumes = _volsDao.search(sc_volume, null);
                        if ((volumes == null) || (volumes.size() == 0)) {
                            break;
                        }
                        VolumeVO volume = volumes.get(0);
                        VmDiskStatisticsVO previousVmDiskStats = _vmDiskStatsDao.findBy(userVm.getAccountId(), userVm.getDataCenterId(), userVm.getId(), volume.getId());
                        VmDiskStatisticsVO vmDiskStat_lock = _vmDiskStatsDao.lock(userVm.getAccountId(), userVm.getDataCenterId(), userVm.getId(), volume.getId());
                        if ((vmDiskStat.getIORead() == 0) && (vmDiskStat.getIOWrite() == 0) && (vmDiskStat.getBytesRead() == 0) && (vmDiskStat.getBytesWrite() == 0)) {
                            s_logger.debug("Read/Write of IO and Bytes are both 0. Not updating vm_disk_statistics");
                            continue;
                        }
                        if (vmDiskStat_lock == null) {
                            s_logger.warn("unable to find vm disk stats from host for account: " + userVm.getAccountId() + " with vmId: " + userVm.getId() + " and volumeId:" + volume.getId());
                            continue;
                        }
                        if (previousVmDiskStats != null && ((previousVmDiskStats.getCurrentIORead() != vmDiskStat_lock.getCurrentIORead()) || ((previousVmDiskStats.getCurrentIOWrite() != vmDiskStat_lock.getCurrentIOWrite()) || (previousVmDiskStats.getCurrentBytesRead() != vmDiskStat_lock.getCurrentBytesRead()) || (previousVmDiskStats.getCurrentBytesWrite() != vmDiskStat_lock.getCurrentBytesWrite())))) {
                            s_logger.debug("vm disk stats changed from the time GetVmDiskStatsCommand was sent. " + "Ignoring current answer. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " IO Read: " + vmDiskStat.getIORead() + " IO Write: " + vmDiskStat.getIOWrite() + " Bytes Read: " + vmDiskStat.getBytesRead() + " Bytes Write: " + vmDiskStat.getBytesWrite());
                            continue;
                        }
                        if (vmDiskStat_lock.getCurrentIORead() > vmDiskStat.getIORead()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Read # of IO that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " Reported: " + vmDiskStat.getIORead() + " Stored: " + vmDiskStat_lock.getCurrentIORead());
                            }
                            vmDiskStat_lock.setNetIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead());
                        }
                        vmDiskStat_lock.setCurrentIORead(vmDiskStat.getIORead());
                        if (vmDiskStat_lock.getCurrentIOWrite() > vmDiskStat.getIOWrite()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Write # of IO that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " Reported: " + vmDiskStat.getIOWrite() + " Stored: " + vmDiskStat_lock.getCurrentIOWrite());
                            }
                            vmDiskStat_lock.setNetIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite());
                        }
                        vmDiskStat_lock.setCurrentIOWrite(vmDiskStat.getIOWrite());
                        if (vmDiskStat_lock.getCurrentBytesRead() > vmDiskStat.getBytesRead()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Read # of Bytes that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " Reported: " + toHumanReadableSize(vmDiskStat.getBytesRead()) + " Stored: " + toHumanReadableSize(vmDiskStat_lock.getCurrentBytesRead()));
                            }
                            vmDiskStat_lock.setNetBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead());
                        }
                        vmDiskStat_lock.setCurrentBytesRead(vmDiskStat.getBytesRead());
                        if (vmDiskStat_lock.getCurrentBytesWrite() > vmDiskStat.getBytesWrite()) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Write # of Bytes that's less than the last one.  " + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + " Reported: " + toHumanReadableSize(vmDiskStat.getBytesWrite()) + " Stored: " + toHumanReadableSize(vmDiskStat_lock.getCurrentBytesWrite()));
                            }
                            vmDiskStat_lock.setNetBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite());
                        }
                        vmDiskStat_lock.setCurrentBytesWrite(vmDiskStat.getBytesWrite());
                        if (!_dailyOrHourly) {
                            // update agg bytes
                            vmDiskStat_lock.setAggIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead());
                            vmDiskStat_lock.setAggIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite());
                            vmDiskStat_lock.setAggBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead());
                            vmDiskStat_lock.setAggBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite());
                        }
                        _vmDiskStatsDao.update(vmDiskStat_lock.getId(), vmDiskStat_lock);
                    }
                }
            });
        } catch (Exception e) {
            s_logger.warn(String.format("Unable to update VM disk statistics for %s from %s", userVm.getInstanceName(), host), e);
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VmDiskStatisticsVO(com.cloud.user.VmDiskStatisticsVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) VmDiskStatsEntry(com.cloud.agent.api.VmDiskStatsEntry) HostVO(com.cloud.host.HostVO) ExecutionException(com.cloud.utils.exception.ExecutionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientServerCapacityException(com.cloud.exception.InsufficientServerCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) IOException(java.io.IOException) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudException(com.cloud.exception.CloudException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) AffinityConflictException(com.cloud.exception.AffinityConflictException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) SearchCriteria(com.cloud.utils.db.SearchCriteria) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) GetVmDiskStatsCommand(com.cloud.agent.api.GetVmDiskStatsCommand) VolumeVO(com.cloud.storage.VolumeVO) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List)

Example 50 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cloudstack by apache.

the class DomainManagerImplTest method validateUniqueDomainNameTestExistingNameThrowsException.

@Test(expected = InvalidParameterValueException.class)
public void validateUniqueDomainNameTestExistingNameThrowsException() {
    SearchCriteria scMock = Mockito.mock(SearchCriteria.class);
    ArrayList<DomainVO> listMock = Mockito.mock(ArrayList.class);
    Mockito.doReturn(scMock).when(domainDaoMock).createSearchCriteria();
    Mockito.doReturn(listMock).when(domainDaoMock).search(Mockito.any(SearchCriteria.class), Mockito.any());
    Mockito.doReturn(false).when(listMock).isEmpty();
    domainManager.validateUniqueDomainName("test", 1L);
}
Also used : DomainVO(com.cloud.domain.DomainVO) SearchCriteria(com.cloud.utils.db.SearchCriteria) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SearchCriteria (com.cloud.utils.db.SearchCriteria)54 List (java.util.List)40 ArrayList (java.util.ArrayList)31 Account (com.cloud.user.Account)23 TransactionStatus (com.cloud.utils.db.TransactionStatus)18 DB (com.cloud.utils.db.DB)13 DomainVO (com.cloud.domain.DomainVO)12 Filter (com.cloud.utils.db.Filter)12 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)11 HashMap (java.util.HashMap)11 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)10 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)9 Map (java.util.Map)8 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)7 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)7 HostVO (com.cloud.host.HostVO)7 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)7 Pair (com.cloud.utils.Pair)6 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)6