Search in sources :

Example 1 with ResourceCountVO

use of com.cloud.configuration.ResourceCountVO in project cloudstack by apache.

the class ResourceCountDaoImpl method listAllRowsToUpdate.

@Override
public Set<Long> listAllRowsToUpdate(long ownerId, ResourceOwnerType ownerType, ResourceType type) {
    Set<Long> rowIds = new HashSet<Long>();
    if (ownerType == ResourceOwnerType.Account) {
        //get records for account
        ResourceCountVO accountCountRecord = findByOwnerAndType(ownerId, ResourceOwnerType.Account, type);
        if (accountCountRecord != null) {
            rowIds.add(accountCountRecord.getId());
        }
        //get records for account's domain and all its parent domains
        rowIds.addAll(listRowsToUpdateForDomain(_accountDao.findByIdIncludingRemoved(ownerId).getDomainId(), type));
    } else if (ownerType == ResourceOwnerType.Domain) {
        return listRowsToUpdateForDomain(ownerId, type);
    }
    return rowIds;
}
Also used : ResourceCountVO(com.cloud.configuration.ResourceCountVO) HashSet(java.util.HashSet)

Example 2 with ResourceCountVO

use of com.cloud.configuration.ResourceCountVO in project cloudstack by apache.

the class ResourceCountDaoImpl method updateById.

@Override
public boolean updateById(long id, boolean increment, long delta) {
    delta = increment ? delta : delta * -1;
    ResourceCountVO resourceCountVO = findById(id);
    resourceCountVO.setCount(resourceCountVO.getCount() + delta);
    return update(resourceCountVO.getId(), resourceCountVO);
}
Also used : ResourceCountVO(com.cloud.configuration.ResourceCountVO)

Example 3 with ResourceCountVO

use of com.cloud.configuration.ResourceCountVO in project cloudstack by apache.

the class ResourceCountDaoImpl method createResourceCounts.

@Override
@DB
public void createResourceCounts(long ownerId, ResourceLimit.ResourceOwnerType ownerType) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    ResourceType[] resourceTypes = Resource.ResourceType.values();
    for (ResourceType resourceType : resourceTypes) {
        if (!resourceType.supportsOwner(ownerType)) {
            continue;
        }
        ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, ownerId, ownerType);
        persist(resourceCountVO);
    }
    txn.commit();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ResourceCountVO(com.cloud.configuration.ResourceCountVO) ResourceType(com.cloud.configuration.Resource.ResourceType) DB(com.cloud.utils.db.DB)

Example 4 with ResourceCountVO

use of com.cloud.configuration.ResourceCountVO in project cloudstack by apache.

the class ConfigurationServerImpl method updateResourceCount.

@DB
public void updateResourceCount() {
    ResourceType[] resourceTypes = Resource.ResourceType.values();
    List<AccountVO> accounts = _accountDao.listAll();
    List<DomainVO> domains = _domainDao.listAll();
    List<ResourceCountVO> domainResourceCount = _resourceCountDao.listResourceCountByOwnerType(ResourceOwnerType.Domain);
    List<ResourceCountVO> accountResourceCount = _resourceCountDao.listResourceCountByOwnerType(ResourceOwnerType.Account);
    final List<ResourceType> accountSupportedResourceTypes = new ArrayList<ResourceType>();
    final List<ResourceType> domainSupportedResourceTypes = new ArrayList<ResourceType>();
    for (ResourceType resourceType : resourceTypes) {
        if (resourceType.supportsOwner(ResourceOwnerType.Account)) {
            accountSupportedResourceTypes.add(resourceType);
        }
        if (resourceType.supportsOwner(ResourceOwnerType.Domain)) {
            domainSupportedResourceTypes.add(resourceType);
        }
    }
    final int accountExpectedCount = accountSupportedResourceTypes.size();
    final int domainExpectedCount = domainSupportedResourceTypes.size();
    if ((domainResourceCount.size() < domainExpectedCount * domains.size())) {
        s_logger.debug("resource_count table has records missing for some domains...going to insert them");
        for (final DomainVO domain : domains) {
            // Lock domain
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    _domainDao.lockRow(domain.getId(), true);
                    List<ResourceCountVO> domainCounts = _resourceCountDao.listByOwnerId(domain.getId(), ResourceOwnerType.Domain);
                    List<String> domainCountStr = new ArrayList<String>();
                    for (ResourceCountVO domainCount : domainCounts) {
                        domainCountStr.add(domainCount.getType().toString());
                    }
                    if (domainCountStr.size() < domainExpectedCount) {
                        for (ResourceType resourceType : domainSupportedResourceTypes) {
                            if (!domainCountStr.contains(resourceType.toString())) {
                                ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, domain.getId(), ResourceOwnerType.Domain);
                                s_logger.debug("Inserting resource count of type " + resourceType + " for domain id=" + domain.getId());
                                _resourceCountDao.persist(resourceCountVO);
                            }
                        }
                    }
                }
            });
        }
    }
    if ((accountResourceCount.size() < accountExpectedCount * accounts.size())) {
        s_logger.debug("resource_count table has records missing for some accounts...going to insert them");
        for (final AccountVO account : accounts) {
            // lock account
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    _accountDao.lockRow(account.getId(), true);
                    List<ResourceCountVO> accountCounts = _resourceCountDao.listByOwnerId(account.getId(), ResourceOwnerType.Account);
                    List<String> accountCountStr = new ArrayList<String>();
                    for (ResourceCountVO accountCount : accountCounts) {
                        accountCountStr.add(accountCount.getType().toString());
                    }
                    if (accountCountStr.size() < accountExpectedCount) {
                        for (ResourceType resourceType : accountSupportedResourceTypes) {
                            if (!accountCountStr.contains(resourceType.toString())) {
                                ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, account.getId(), ResourceOwnerType.Account);
                                s_logger.debug("Inserting resource count of type " + resourceType + " for account id=" + account.getId());
                                _resourceCountDao.persist(resourceCountVO);
                            }
                        }
                    }
                }
            });
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) ResourceType(com.cloud.configuration.Resource.ResourceType) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) AccountVO(com.cloud.user.AccountVO) DomainVO(com.cloud.domain.DomainVO) ResourceCountVO(com.cloud.configuration.ResourceCountVO) ArrayList(java.util.ArrayList) List(java.util.List) DB(com.cloud.utils.db.DB)

Example 5 with ResourceCountVO

use of com.cloud.configuration.ResourceCountVO in project cloudstack by apache.

the class ResourceLimitManagerImpl method recalculateResourceCount.

@Override
public List<ResourceCountVO> recalculateResourceCount(Long accountId, Long domainId, Integer typeId) throws InvalidParameterValueException, CloudRuntimeException, PermissionDeniedException {
    Account callerAccount = CallContext.current().getCallingAccount();
    long count = 0;
    List<ResourceCountVO> counts = new ArrayList<ResourceCountVO>();
    List<ResourceType> resourceTypes = new ArrayList<ResourceType>();
    ResourceType resourceType = null;
    if (typeId != null) {
        for (ResourceType type : Resource.ResourceType.values()) {
            if (type.getOrdinal() == typeId.intValue()) {
                resourceType = type;
            }
        }
        if (resourceType == null) {
            throw new InvalidParameterValueException("Please specify valid resource type");
        }
    }
    DomainVO domain = _domainDao.findById(domainId);
    if (domain == null) {
        throw new InvalidParameterValueException("Please specify a valid domain ID.");
    }
    _accountMgr.checkAccess(callerAccount, domain);
    if (resourceType != null) {
        resourceTypes.add(resourceType);
    } else {
        resourceTypes = Arrays.asList(Resource.ResourceType.values());
    }
    for (ResourceType type : resourceTypes) {
        if (accountId != null) {
            if (type.supportsOwner(ResourceOwnerType.Account)) {
                count = recalculateAccountResourceCount(accountId, type);
                counts.add(new ResourceCountVO(type, count, accountId, ResourceOwnerType.Account));
            }
        } else {
            if (type.supportsOwner(ResourceOwnerType.Domain)) {
                count = recalculateDomainResourceCount(domainId, type);
                counts.add(new ResourceCountVO(type, count, domainId, ResourceOwnerType.Domain));
            }
        }
    }
    return counts;
}
Also used : Account(com.cloud.user.Account) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) ResourceCountVO(com.cloud.configuration.ResourceCountVO) ResourceType(com.cloud.configuration.Resource.ResourceType)

Aggregations

ResourceCountVO (com.cloud.configuration.ResourceCountVO)9 ResourceType (com.cloud.configuration.Resource.ResourceType)3 DomainVO (com.cloud.domain.DomainVO)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 DB (com.cloud.utils.db.DB)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 DedicatedResourceVO (com.cloud.dc.DedicatedResourceVO)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 CloudAuthenticationException (com.cloud.exception.CloudAuthenticationException)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 IpAddress (com.cloud.network.IpAddress)1 VpnUserVO (com.cloud.network.VpnUserVO)1 AccountGuestVlanMapVO (com.cloud.network.dao.AccountGuestVlanMapVO)1 NetworkVO (com.cloud.network.dao.NetworkVO)1 RemoteAccessVpnVO (com.cloud.network.dao.RemoteAccessVpnVO)1 Vpc (com.cloud.network.vpc.Vpc)1