Return-Path: X-Original-To: apmail-cloudstack-dev-archive@www.apache.org Delivered-To: apmail-cloudstack-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8E4EB17341 for ; Wed, 26 Aug 2015 06:53:43 +0000 (UTC) Received: (qmail 53803 invoked by uid 500); 26 Aug 2015 06:53:43 -0000 Delivered-To: apmail-cloudstack-dev-archive@cloudstack.apache.org Received: (qmail 53748 invoked by uid 500); 26 Aug 2015 06:53:43 -0000 Mailing-List: contact dev-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list dev@cloudstack.apache.org Received: (qmail 53737 invoked by uid 99); 26 Aug 2015 06:53:42 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Aug 2015 06:53:42 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C5EC5E1145; Wed, 26 Aug 2015 06:53:42 +0000 (UTC) From: jburwell To: dev@cloudstack.apache.org Reply-To: dev@cloudstack.apache.org References: In-Reply-To: Subject: [GitHub] cloudstack pull request: Quota master Content-Type: text/plain Message-Id: <20150826065342.C5EC5E1145@git1-us-west.apache.org> Date: Wed, 26 Aug 2015 06:53:42 +0000 (UTC) Github user jburwell commented on a diff in the pull request: https://github.com/apache/cloudstack/pull/689#discussion_r37951028 --- Diff: usage/src/org/apache/cloudstack/quota/QuotaManagerImpl.java --- @@ -0,0 +1,465 @@ +//Licensed to the Apache Software Foundation (ASF) under one +//or more contributor license agreements. See the NOTICE file +//distributed with this work for additional information +//regarding copyright ownership. The ASF licenses this file +//to you under the Apache License, Version 2.0 (the +//"License"); you may not use this file except in compliance +//with the License. You may obtain a copy of the License at +// +//http://www.apache.org/licenses/LICENSE-2.0 +// +//Unless required by applicable law or agreed to in writing, +//software distributed under the License is distributed on an +//"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +//KIND, either express or implied. See the License for the +//specific language governing permissions and limitations +//under the License. +package org.apache.cloudstack.quota; + +import com.cloud.usage.UsageVO; +import com.cloud.usage.dao.UsageDao; +import com.cloud.user.Account; +//import com.cloud.user.AccountManager; +import com.cloud.user.AccountVO; +import com.cloud.user.Account.State; +import com.cloud.user.dao.AccountDao; +import com.cloud.utils.Pair; +import com.cloud.utils.component.ManagerBase; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.TransactionLegacy; + +import org.apache.cloudstack.framework.config.dao.ConfigurationDao; +import org.apache.cloudstack.quota.constant.QuotaTypes; +import org.apache.cloudstack.quota.dao.QuotaAccountDao; +import org.apache.cloudstack.quota.dao.QuotaBalanceDao; +import org.apache.cloudstack.quota.dao.QuotaTariffDao; +import org.apache.cloudstack.quota.dao.QuotaUsageDao; +import org.apache.cloudstack.quota.vo.QuotaAccountVO; +import org.apache.cloudstack.quota.vo.QuotaBalanceVO; +import org.apache.cloudstack.quota.vo.QuotaTariffVO; +import org.apache.cloudstack.quota.vo.QuotaUsageVO; +import org.apache.cloudstack.quota.vo.ServiceOfferingVO; +import org.apache.cloudstack.quota.dao.ServiceOfferingDao; +import org.apache.cloudstack.utils.usage.UsageUtils; +import org.apache.log4j.Logger; +import org.springframework.stereotype.Component; + +import javax.ejb.Local; +import javax.inject.Inject; +import javax.naming.ConfigurationException; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.TimeZone; + +@Component +@Local(value = QuotaManager.class) +public class QuotaManagerImpl extends ManagerBase implements QuotaManager { + private static final Logger s_logger = Logger.getLogger(QuotaManagerImpl.class.getName()); + + @Inject + private AccountDao _accountDao; + @Inject + private QuotaAccountDao _quotaAcc; + @Inject + private UsageDao _usageDao; + @Inject + private QuotaTariffDao _quotaTariffDao; + @Inject + private QuotaUsageDao _quotaUsageDao; + @Inject + private ServiceOfferingDao _serviceOfferingDao; + @Inject + private QuotaBalanceDao _quotaBalanceDao; + @Inject + private ConfigurationDao _configDao; + + private TimeZone _usageTimezone; + private int _aggregationDuration = 0; + + final static BigDecimal s_hoursInMonth = new BigDecimal(30 * 24); + final static BigDecimal s_minutesInMonth = new BigDecimal(30 * 24 * 60); + final static BigDecimal s_gb = new BigDecimal(1024 * 1024 * 1024); + + int _pid = 0; + + public QuotaManagerImpl() { + super(); + } + + private void mergeConfigs(Map dbParams, Map xmlParams) { + for (Map.Entry param : xmlParams.entrySet()) { + dbParams.put(param.getKey(), (String) param.getValue()); + } + } + + @Override + public boolean configure(String name, Map params) throws ConfigurationException { + super.configure(name, params); + + Map configs = _configDao.getConfiguration(params); + + if (params != null) { + mergeConfigs(configs, params); + } + + String aggregationRange = configs.get("usage.stats.job.aggregation.range"); + String timeZoneStr = configs.get("usage.aggregation.timezone"); + + if (timeZoneStr == null) { + timeZoneStr = "GMT"; + } + _usageTimezone = TimeZone.getTimeZone(timeZoneStr); + + _aggregationDuration = Integer.parseInt(aggregationRange); + if (_aggregationDuration < UsageUtils.USAGE_AGGREGATION_RANGE_MIN) { + s_logger.warn("Usage stats job aggregation range is to small, using the minimum value of " + UsageUtils.USAGE_AGGREGATION_RANGE_MIN); + _aggregationDuration = UsageUtils.USAGE_AGGREGATION_RANGE_MIN; + } + s_logger.info("Usage timezone = " + _usageTimezone + " AggregationDuration=" + _aggregationDuration); + + return true; + } + + @Override + public boolean start() { + if (s_logger.isInfoEnabled()) { + s_logger.info("Starting Quota Manager"); + } + _pid = Integer.parseInt(System.getProperty("pid")); + return true; + } + + @Override + public boolean stop() { + if (s_logger.isInfoEnabled()) { + s_logger.info("Stopping Quota Manager"); + } + return true; + } + + @Override + public boolean calculateQuotaUsage() { + final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); + boolean jobResult = false; + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); + try { + // get all the active accounts for which there is usage + List accounts = _accountDao.listAll(); + for (AccountVO account : accounts) { // START ACCOUNT + Pair, Integer> usageRecords = null; + List quotalistforaccount = new ArrayList(); + do { + s_logger.info("Account =" + account.getAccountName()); + usageRecords = _usageDao.getUsageRecordsPendingQuotaAggregation(account.getAccountId(), account.getDomainId()); + s_logger.debug("Usage records found " + usageRecords.second()); + for (UsageVO usageRecord : usageRecords.first()) { + BigDecimal aggregationRatio = new BigDecimal(_aggregationDuration).divide(s_minutesInMonth, 8, RoundingMode.HALF_EVEN); + switch (usageRecord.getUsageType()) { + case QuotaTypes.RUNNING_VM: + quotalistforaccount.addAll(updateQuotaRunningVMUsage(usageRecord, aggregationRatio)); + break; + case QuotaTypes.ALLOCATED_VM: + quotalistforaccount.add(updateQuotaAllocatedVMUsage(usageRecord, aggregationRatio)); + break; + case QuotaTypes.SNAPSHOT: + quotalistforaccount.add(updateQuotaDiskUsage(usageRecord, aggregationRatio, QuotaTypes.SNAPSHOT)); + break; + case QuotaTypes.TEMPLATE: + quotalistforaccount.add(updateQuotaDiskUsage(usageRecord, aggregationRatio, QuotaTypes.TEMPLATE)); + break; + case QuotaTypes.ISO: + quotalistforaccount.add(updateQuotaDiskUsage(usageRecord, aggregationRatio, QuotaTypes.ISO)); + break; + case QuotaTypes.VOLUME: + quotalistforaccount.add(updateQuotaDiskUsage(usageRecord, aggregationRatio, QuotaTypes.VOLUME)); + break; + case QuotaTypes.VM_SNAPSHOT: + quotalistforaccount.add(updateQuotaDiskUsage(usageRecord, aggregationRatio, QuotaTypes.VM_SNAPSHOT)); + break; + case QuotaTypes.LOAD_BALANCER_POLICY: + quotalistforaccount.add(updateQuotaRaw(usageRecord, aggregationRatio, QuotaTypes.LOAD_BALANCER_POLICY)); + break; + case QuotaTypes.PORT_FORWARDING_RULE: + quotalistforaccount.add(updateQuotaRaw(usageRecord, aggregationRatio, QuotaTypes.PORT_FORWARDING_RULE)); + break; + case QuotaTypes.IP_ADDRESS: + quotalistforaccount.add(updateQuotaRaw(usageRecord, aggregationRatio, QuotaTypes.IP_ADDRESS)); + break; + case QuotaTypes.NETWORK_OFFERING: + quotalistforaccount.add(updateQuotaRaw(usageRecord, aggregationRatio, QuotaTypes.NETWORK_OFFERING)); + break; + case QuotaTypes.SECURITY_GROUP: + quotalistforaccount.add(updateQuotaRaw(usageRecord, aggregationRatio, QuotaTypes.SECURITY_GROUP)); + break; + case QuotaTypes.VPN_USERS: + quotalistforaccount.add(updateQuotaRaw(usageRecord, aggregationRatio, QuotaTypes.VPN_USERS)); + break; + case QuotaTypes.NETWORK_BYTES_RECEIVED: + quotalistforaccount.add(updateQuotaNetwork(usageRecord, QuotaTypes.NETWORK_BYTES_RECEIVED)); + break; + case QuotaTypes.NETWORK_BYTES_SENT: + quotalistforaccount.add(updateQuotaNetwork(usageRecord, QuotaTypes.NETWORK_BYTES_SENT)); + break; + case QuotaTypes.VM_DISK_IO_READ: + case QuotaTypes.VM_DISK_IO_WRITE: + case QuotaTypes.VM_DISK_BYTES_READ: + case QuotaTypes.VM_DISK_BYTES_WRITE: + default: + break; + } + } + } while ((usageRecords != null) && !usageRecords.first().isEmpty()); + // list of quotas for this account + s_logger.info("Quota entries size = " + quotalistforaccount.size() + ", accId" + account.getAccountId() + ", domId" + account.getDomainId()); + if (quotalistforaccount.size() > 0) { // balance to be processed + quotalistforaccount.add(new QuotaUsageVO()); + Date startDate = quotalistforaccount.get(0).getStartDate(); + Date endDate = quotalistforaccount.get(0).getEndDate(); + BigDecimal aggrUsage = new BigDecimal(0); + for (QuotaUsageVO entry : quotalistforaccount) { + if (startDate.compareTo(entry.getStartDate()) != 0) { + QuotaBalanceVO lastrealbalanceentry = _quotaBalanceDao.findLastBalanceEntry(account.getAccountId(), account.getDomainId(), startDate); + Date lastbalancedate; + if (lastrealbalanceentry != null) { + lastbalancedate = lastrealbalanceentry.getUpdatedOn(); + aggrUsage = aggrUsage.add(lastrealbalanceentry.getCreditBalance()); + } else { + lastbalancedate = new Date(0); + } + + List creditsrcvd = _quotaBalanceDao.findCreditBalance(account.getAccountId(), account.getDomainId(), lastbalancedate, endDate); + for (QuotaBalanceVO credit : creditsrcvd) { + aggrUsage = aggrUsage.add(credit.getCreditBalance()); + } + + QuotaBalanceVO newbalance = new QuotaBalanceVO(account.getAccountId(), account.getDomainId(), aggrUsage, endDate); + // s_logger.info("Balance entry=" + aggrUsage + " on Date=" + endDate); --- End diff -- Please remove commented code as it builds up cruft. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---