Should generate consumpiton when freeze

Change-Id: Id6d3ef30badc7ca0f7c7e51e61837f1b5390ec7c
This commit is contained in:
lvdongbing 2016-08-08 23:29:53 -04:00
parent a6fff8d084
commit 673374761e
7 changed files with 31 additions and 36 deletions

View File

@ -733,7 +733,7 @@ def action_acquire(context, action_id, owner, timestamp):
if action.status != consts.ACTION_READY:
msg = _('The action is not in an executable status: '
'%s') % action.status
LOG.warning(msg)
LOG.warn(msg)
return None
action.owner = owner
action.start_time = timestamp

View File

@ -175,12 +175,12 @@ def warning(context, entity, action, status=None, status_reason=None,
action=action, status=status, status_reason=status_reason,
user_id=context.project)
event.store(context)
LOG.warning(_LW('%(name)s [%(id)s] %(action)s - %(status)s: %(reason)s'),
{'name': event.obj_name,
'id': event.obj_id and event.obj_id[:8],
'action': action,
'status': status,
'reason': status_reason})
LOG.warn(_LW('%(name)s [%(id)s] %(action)s - %(status)s: %(reason)s'),
{'name': event.obj_name,
'id': event.obj_id and event.obj_id[:8],
'action': action,
'status': status,
'reason': status_reason})
def info(context, entity, action, status=None, status_reason=None,

View File

@ -29,6 +29,7 @@ from bilean.common import exception
from bilean.common.i18n import _
from bilean.common.i18n import _LE
from bilean.common.i18n import _LI
from bilean.common.i18n import _LW
from bilean.common import messaging as rpc_messaging
from bilean.common import schema
from bilean.common import utils
@ -487,8 +488,8 @@ class EngineService(service.Service):
try:
plugin_base.Resource.load(cnxt, resource_id=resource_id)
except exception.ResourceNotFound:
LOG.error(_LE('The resource(%s) trying to delete not found.'),
resource_id)
LOG.warn(_LW('The resource(%s) trying to delete not found.'),
resource_id)
return
params = {

View File

@ -223,8 +223,7 @@ class User(object):
"""
# Settle account before update rate
self._settle_account(context, delta_rate=delta_rate,
timestamp=timestamp)
self._settle_account(context, timestamp=timestamp)
old_rate = self.rate
new_rate = old_rate + delta_rate
@ -296,30 +295,23 @@ class User(object):
db_api.user_delete(context, self.id)
return True
def _settle_account(self, context, delta_rate=0, timestamp=None):
def _settle_account(self, context, timestamp=None):
if self.rate == 0:
LOG.info(_LI("Ignore settlement action because user is in '%s' "
"status."), self.status)
return
# Calculate user's cost between last_bill and now
now = utils.make_decimal(wallclock())
delayed_cost = 0
if delta_rate != 0:
delayed_seconds = now - timestamp
delayed_cost = delayed_seconds * utils.make_decimal(delta_rate)
now = timestamp or utils.make_decimal(wallclock())
usage_seconds = now - self.last_bill
cost = self.rate * usage_seconds
total_cost = cost + delayed_cost
self.balance -= total_cost
self.balance -= cost
self.last_bill = now
def settle_account(self, context, task=None):
'''Settle account for user.'''
notifier = bilean_notifier.Notifier()
self._settle_account(context)
timestamp = utils.make_decimal(wallclock())
self._settle_account(context, timestamp=timestamp)
if task == 'notify' and self._notify_or_not():
self.status_reason = "The balance is almost used up"
@ -334,7 +326,7 @@ class User(object):
resources = plugin_base.Resource.load_all(
context, user_id=self.id, project_safe=False)
for resource in resources:
resource.do_delete(context)
resource.do_delete(context, timestamp=timestamp)
self.rate = 0
self.status = self.FREEZE
self.status_reason = reason

View File

@ -293,9 +293,9 @@ class Resource(object):
return self.id
def delete(self, context, soft_delete=True):
def delete(self, context, timestamp=None, soft_delete=True):
'''Delete resource from db.'''
self._delete(context, soft_delete=soft_delete)
self._delete(context, timestamp=timestamp, soft_delete=soft_delete)
def _create(self, context, values):
self.delta_rate = self.rate
@ -341,14 +341,14 @@ class Resource(object):
values.update(last_bill=utils.format_decimal(updated_at))
db_api.resource_update(context, self.id, values)
def _delete(self, context, soft_delete=True):
def _delete(self, context, timestamp=None, soft_delete=True):
self.delta_rate = - self.rate
if self.delta_rate == 0:
db_api.resource_delete(context, self.id, soft_delete=soft_delete)
return
deleted_at = timestamp or utils.make_decimal(wallclock())
delete_time = self.properties.get('deleted_at')
deleted_at = utils.make_decimal(wallclock())
if delete_time is not None:
sec = utils.format_time_to_seconds(delete_time)
deleted_at = utils.make_decimal(sec)

View File

@ -18,7 +18,6 @@ from bilean.common import exception
from bilean.common.i18n import _
from bilean.common.i18n import _LE
from bilean.common import schema
from bilean.db import api as db_api
from bilean.drivers import base as driver_base
from bilean.plugins import base
@ -106,11 +105,13 @@ class VolumeResource(base.Resource):
# TODO(ldb)
return NotImplemented
def do_delete(self, context, ignore_missing=True, timeout=None):
def do_delete(self, context, timestamp=None, ignore_missing=True,
timeout=None):
'''Delete resource from other services.'''
# Delete resource from db
db_api.resource_delete(context, self.id)
# Delete resource from db and generate consumption
self.delete(context, timestamp=timestamp)
self.consumption.store(context)
# Delete resource from cinder
cinderclient = driver_base.BileanDriver().block_store()

View File

@ -18,7 +18,6 @@ from bilean.common import exception
from bilean.common.i18n import _
from bilean.common.i18n import _LE
from bilean.common import schema
from bilean.db import api as db_api
from bilean.drivers import base as driver_base
from bilean.plugins import base
@ -105,11 +104,13 @@ class ServerResource(base.Resource):
# TODO(ldb)
return NotImplemented
def do_delete(self, context, ignore_missing=True, timeout=None):
def do_delete(self, context, timestamp=None, ignore_missing=True,
timeout=None):
'''Delete resource from other services.'''
# Delete resource from db
db_api.resource_delete(context, self.id)
# Delete resource from db and generate consumption
self.delete(context, timestamp=timestamp)
self.consumption.store(context)
# Delete resource from nova
novaclient = driver_base.BileanDriver().compute()