def test_bw_usage_calls(self): ctxt = context.get_admin_context() now = timeutils.utcnow() timeutils.set_time_override(now) start_period = now - datetime.timedelta(seconds=10) uuid3_refreshed = now - datetime.timedelta(seconds=5) expected_bw_usages = [ { "uuid": "fake_uuid1", "mac": "fake_mac1", "start_period": start_period, "bw_in": 100, "bw_out": 200, "last_refreshed": now, }, { "uuid": "fake_uuid2", "mac": "fake_mac2", "start_period": start_period, "bw_in": 200, "bw_out": 300, "last_refreshed": now, }, { "uuid": "fake_uuid3", "mac": "fake_mac3", "start_period": start_period, "bw_in": 400, "bw_out": 500, "last_refreshed": uuid3_refreshed, }, ] def _compare(bw_usage, expected): for key, value in expected.items(): self.assertEqual(bw_usage[key], value) bw_usages = db.bw_usage_get_by_uuids(ctxt, ["fake_uuid1", "fake_uuid2"], start_period) # No matches self.assertEqual(len(bw_usages), 0) # Add 3 entries db.bw_usage_update(ctxt, "fake_uuid1", "fake_mac1", start_period, 100, 200) db.bw_usage_update(ctxt, "fake_uuid2", "fake_mac2", start_period, 100, 200) # Test explicit refreshed time db.bw_usage_update(ctxt, "fake_uuid3", "fake_mac3", start_period, 400, 500, last_refreshed=uuid3_refreshed) # Update 2nd entry db.bw_usage_update(ctxt, "fake_uuid2", "fake_mac2", start_period, 200, 300) bw_usages = db.bw_usage_get_by_uuids(ctxt, ["fake_uuid1", "fake_uuid2", "fake_uuid3"], start_period) self.assertEqual(len(bw_usages), 3) _compare(bw_usages[0], expected_bw_usages[0]) _compare(bw_usages[1], expected_bw_usages[1]) _compare(bw_usages[2], expected_bw_usages[2]) timeutils.clear_time_override()
def _db_bw_usage_get_by_uuids(context, uuids, start_period, use_slave=False): return db.bw_usage_get_by_uuids(context, uuids=uuids, start_period=start_period)
def notify_usage_exists(context, instance_ref, current_period=False, ignore_missing_network_data=True): """Generates 'exists' notification for an instance for usage auditing purposes. :param current_period: if True, this will generate a usage for the current usage period; if False, this will generate a usage for the previous audit period. :param ignore_missing_network_data: if True, log any exceptions generated while getting network info; if False, raise the exception. """ admin_context = nova.context.get_admin_context(read_deleted='yes') begin, end = utils.last_completed_audit_period() bw = {} if current_period: audit_start = end audit_end = utils.utcnow() else: audit_start = begin audit_end = end if (instance_ref.get('info_cache') and instance_ref['info_cache'].get('network_info')): cached_info = instance_ref['info_cache']['network_info'] nw_info = network_model.NetworkInfo.hydrate(cached_info) else: try: nw_info = network.API().get_instance_nw_info(admin_context, instance_ref) except Exception: LOG.exception('Failed to get nw_info', instance=instance_ref) if ignore_missing_network_data: return raise macs = [vif['address'] for vif in nw_info] uuids = [instance_ref.uuid] bw_usages = db.bw_usage_get_by_uuids(admin_context, uuids, audit_start) bw_usages = [b for b in bw_usages if b.mac in macs] for b in bw_usages: label = 'net-name-not-found-%s' % b['mac'] for vif in nw_info: if vif['address'] == b['mac']: label = vif['network']['label'] break bw[label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) extra_usage_info = dict(audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw) notify_about_instance_usage( context, instance_ref, 'exists', extra_usage_info=extra_usage_info)
def bandwidth_usage(instance_ref, audit_start, ignore_missing_network_data=True): """Get bandwidth usage information for the instance for the specified audit period. """ admin_context = nova.context.get_admin_context(read_deleted='yes') def _get_nwinfo_old_skool(): """Support for getting network info without objects.""" if (instance_ref.get('info_cache') and instance_ref['info_cache'].get('network_info') is not None): cached_info = instance_ref['info_cache']['network_info'] if isinstance(cached_info, network_model.NetworkInfo): return cached_info return network_model.NetworkInfo.hydrate(cached_info) try: return network.API().get_instance_nw_info(admin_context, instance_ref) except Exception: try: with excutils.save_and_reraise_exception(): LOG.exception(_('Failed to get nw_info'), instance=instance_ref) except Exception: if ignore_missing_network_data: return raise # FIXME(comstud): Temporary as we transition to objects. This import # is here to avoid circular imports. from nova.objects import instance as instance_obj if isinstance(instance_ref, instance_obj.Instance): nw_info = instance_ref.info_cache.network_info if nw_info is None: nw_info = network_model.NetworkInfo() else: nw_info = _get_nwinfo_old_skool() macs = [vif['address'] for vif in nw_info] uuids = [instance_ref["uuid"]] bw_usages = db.bw_usage_get_by_uuids(admin_context, uuids, audit_start) bw_usages = [b for b in bw_usages if b.mac in macs] bw = {} for b in bw_usages: label = 'net-name-not-found-%s' % b['mac'] for vif in nw_info: if vif['address'] == b['mac']: label = vif['network']['label'] break bw[label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) return bw
def bandwidth_usage(instance_ref, audit_start, ignore_missing_network_data=True): """Get bandwidth usage information for the instance for the specified audit period. """ admin_context = nova.context.get_admin_context(read_deleted="yes") def _get_nwinfo_old_skool(): """Support for getting network info without objects.""" if instance_ref.get("info_cache") and instance_ref["info_cache"].get("network_info") is not None: cached_info = instance_ref["info_cache"]["network_info"] if isinstance(cached_info, network_model.NetworkInfo): return cached_info return network_model.NetworkInfo.hydrate(cached_info) try: return network.API().get_instance_nw_info(admin_context, instance_ref) except Exception: try: with excutils.save_and_reraise_exception(): LOG.exception(_("Failed to get nw_info"), instance=instance_ref) except Exception: if ignore_missing_network_data: return raise # FIXME(comstud): Temporary as we transition to objects. if isinstance(instance_ref, obj_base.NovaObject): nw_info = instance_ref.info_cache.network_info if nw_info is None: nw_info = network_model.NetworkInfo() else: nw_info = _get_nwinfo_old_skool() macs = [vif["address"] for vif in nw_info] uuids = [instance_ref["uuid"]] bw_usages = db.bw_usage_get_by_uuids(admin_context, uuids, audit_start) bw_usages = [b for b in bw_usages if b.mac in macs] bw = {} for b in bw_usages: label = "net-name-not-found-%s" % b["mac"] for vif in nw_info: if vif["address"] == b["mac"]: label = vif["network"]["label"] break bw[label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) return bw
def bandwidth_usage(instance_ref, audit_start, ignore_missing_network_data=True): """Get bandwidth usage information for the instance for the specified audit period. """ admin_context = nova.context.get_admin_context(read_deleted='yes') if (instance_ref.get('info_cache') and instance_ref['info_cache'].get('network_info') is not None): cached_info = instance_ref['info_cache']['network_info'] nw_info = network_model.NetworkInfo.hydrate(cached_info) else: try: nw_info = network.API().get_instance_nw_info( admin_context, instance_ref) except Exception: try: with excutils.save_and_reraise_exception(): LOG.exception(_('Failed to get nw_info'), instance=instance_ref) except Exception: if ignore_missing_network_data: return raise macs = [vif['address'] for vif in nw_info] uuids = [instance_ref["uuid"]] bw_usages = db.bw_usage_get_by_uuids(admin_context, uuids, audit_start) bw_usages = [b for b in bw_usages if b.mac in macs] bw = {} for b in bw_usages: label = 'net-name-not-found-%s' % b['mac'] for vif in nw_info: if vif['address'] == b['mac']: label = vif['network']['label'] break bw[label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) return bw
def notify_usage_exists(context, instance_ref, current_period=False): """ Generates 'exists' notification for an instance for usage auditing purposes. Generates usage for last completed period, unless 'current_period' is True.""" admin_context = nova.context.get_admin_context(read_deleted='yes') begin, end = utils.last_completed_audit_period() bw = {} if current_period: audit_start = end audit_end = utils.utcnow() else: audit_start = begin audit_end = end if (instance_ref.get('info_cache') and instance_ref['info_cache'].get('network_info')): cached_info = instance_ref['info_cache']['network_info'] nw_info = network_model.NetworkInfo.hydrate(cached_info) else: nw_info = network.API().get_instance_nw_info(admin_context, instance_ref) macs = [vif['address'] for vif in nw_info] uuids = [instance_ref.uuid] bw_usages = db.bw_usage_get_by_uuids(admin_context, uuids, audit_start) bw_usages = [b for b in bw_usages if b.mac in macs] for b in bw_usages: label = 'net-name-not-found-%s' % b['mac'] for vif in nw_info: if vif['address'] == b['mac']: label = vif['network']['label'] break bw[label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) extra_usage_info = dict(audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw) notify_about_instance_usage( context, instance_ref, 'exists', extra_usage_info=extra_usage_info)
def bandwidth_usage(instance_ref, audit_start, ignore_missing_network_data=True): """Get bandwidth usage information for the instance for the specified audit period. """ admin_context = nova.context.get_admin_context(read_deleted='yes') if (instance_ref.get('info_cache') and instance_ref['info_cache'].get('network_info') is not None): cached_info = instance_ref['info_cache']['network_info'] nw_info = network_model.NetworkInfo.hydrate(cached_info) else: try: nw_info = network.API().get_instance_nw_info(admin_context, instance_ref) except Exception: try: with excutils.save_and_reraise_exception(): LOG.exception(_('Failed to get nw_info'), instance=instance_ref) except Exception: if ignore_missing_network_data: return raise macs = [vif['address'] for vif in nw_info] uuids = [instance_ref["uuid"]] bw_usages = db.bw_usage_get_by_uuids(admin_context, uuids, audit_start) bw_usages = [b for b in bw_usages if b.mac in macs] bw = {} for b in bw_usages: label = 'net-name-not-found-%s' % b['mac'] for vif in nw_info: if vif['address'] == b['mac']: label = vif['network']['label'] break bw[label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) return bw
def bandwidth_usage(instance_ref, audit_start, ignore_missing_network_data=True): """Get bandwidth usage information for the instance for the specified audit period. """ admin_context = nova.context.get_admin_context(read_deleted="yes") if instance_ref.get("info_cache") and instance_ref["info_cache"].get("network_info") is not None: cached_info = instance_ref["info_cache"]["network_info"] nw_info = network_model.NetworkInfo.hydrate(cached_info) else: try: nw_info = network.API().get_instance_nw_info(admin_context, instance_ref) except Exception: LOG.exception(_("Failed to get nw_info"), instance=instance_ref) if ignore_missing_network_data: return raise macs = [vif["address"] for vif in nw_info] uuids = [instance_ref["uuid"]] bw_usages = db.bw_usage_get_by_uuids(admin_context, uuids, audit_start) bw_usages = [b for b in bw_usages if b.mac in macs] bw = {} for b in bw_usages: label = "net-name-not-found-%s" % b["mac"] for vif in nw_info: if vif["address"] == b["mac"]: label = vif["network"]["label"] break bw[label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) return bw
def get_by_uuids(cls, context, uuids, start_period=None): db_bw_usages = db.bw_usage_get_by_uuids(context, uuids=uuids, start_period=start_period) return base.obj_make_list(context, cls(), BandwidthUsage, db_bw_usages)
def get_by_uuids(cls, context, uuids, start_period=None, use_slave=False): db_bw_usages = db.bw_usage_get_by_uuids(context, uuids=uuids, start_period=start_period, use_slave=use_slave) return base.obj_make_list(context, cls(), BandwidthUsage, db_bw_usages)
def notify_usage_exists(context, instance_ref, current_period=False, ignore_missing_network_data=True, system_metadata=None, extra_usage_info=None): """Generates 'exists' notification for an instance for usage auditing purposes. :param current_period: if True, this will generate a usage for the current usage period; if False, this will generate a usage for the previous audit period. :param ignore_missing_network_data: if True, log any exceptions generated while getting network info; if False, raise the exception. :param system_metadata: system_metadata DB entries for the instance, if not None. *NOTE*: Currently unused here in trunk, but needed for potential custom modifications. :param extra_usage_info: Dictionary containing extra values to add or override in the notification if not None. """ admin_context = nova.context.get_admin_context(read_deleted='yes') begin, end = utils.last_completed_audit_period() bw = {} if current_period: audit_start = end audit_end = utils.utcnow() else: audit_start = begin audit_end = end if (instance_ref.get('info_cache') and instance_ref['info_cache'].get('network_info')): cached_info = instance_ref['info_cache']['network_info'] nw_info = network_model.NetworkInfo.hydrate(cached_info) else: try: nw_info = network.API().get_instance_nw_info( admin_context, instance_ref) except Exception: LOG.exception('Failed to get nw_info', instance=instance_ref) if ignore_missing_network_data: return raise macs = [vif['address'] for vif in nw_info] uuids = [instance_ref.uuid] bw_usages = db.bw_usage_get_by_uuids(admin_context, uuids, audit_start) bw_usages = [b for b in bw_usages if b.mac in macs] for b in bw_usages: label = 'net-name-not-found-%s' % b['mac'] for vif in nw_info: if vif['address'] == b['mac']: label = vif['network']['label'] break bw[label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) if system_metadata is None: try: system_metadata = db.instance_system_metadata_get( context, instance_ref.uuid) except exception.NotFound: system_metadata = {} # add image metadata to the notification: image_meta = {} for md_key, md_value in system_metadata.iteritems(): if md_key.startswith('image_'): image_meta[md_key[6:]] = md_value extra_info = dict(audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw, image_meta=image_meta) if extra_usage_info: extra_info.update(extra_usage_info) notify_about_instance_usage(context, instance_ref, 'exists', system_metadata=system_metadata, extra_usage_info=extra_info)
def test_bw_usage_calls(self): ctxt = context.get_admin_context() now = timeutils.utcnow() timeutils.set_time_override(now) start_period = now - datetime.timedelta(seconds=10) uuid3_refreshed = now - datetime.timedelta(seconds=5) expected_bw_usages = [{'uuid': 'fake_uuid1', 'mac': 'fake_mac1', 'start_period': start_period, 'bw_in': 100, 'bw_out': 200, 'last_ctr_in': 12345, 'last_ctr_out': 67890, 'last_refreshed': now}, {'uuid': 'fake_uuid2', 'mac': 'fake_mac2', 'start_period': start_period, 'bw_in': 200, 'bw_out': 300, 'last_ctr_in': 22345, 'last_ctr_out': 77890, 'last_refreshed': now}, {'uuid': 'fake_uuid3', 'mac': 'fake_mac3', 'start_period': start_period, 'bw_in': 400, 'bw_out': 500, 'last_ctr_in': 32345, 'last_ctr_out': 87890, 'last_refreshed': uuid3_refreshed}] def _compare(bw_usage, expected): for key, value in expected.items(): self.assertEqual(bw_usage[key], value) bw_usages = db.bw_usage_get_by_uuids(ctxt, ['fake_uuid1', 'fake_uuid2'], start_period) # No matches self.assertEqual(len(bw_usages), 0) # Add 3 entries db.bw_usage_update(ctxt, 'fake_uuid1', 'fake_mac1', start_period, 100, 200, 12345, 67890) db.bw_usage_update(ctxt, 'fake_uuid2', 'fake_mac2', start_period, 100, 200, 42, 42) # Test explicit refreshed time db.bw_usage_update(ctxt, 'fake_uuid3', 'fake_mac3', start_period, 400, 500, 32345, 87890, last_refreshed=uuid3_refreshed) # Update 2nd entry db.bw_usage_update(ctxt, 'fake_uuid2', 'fake_mac2', start_period, 200, 300, 22345, 77890) bw_usages = db.bw_usage_get_by_uuids(ctxt, ['fake_uuid1', 'fake_uuid2', 'fake_uuid3'], start_period) self.assertEqual(len(bw_usages), 3) _compare(bw_usages[0], expected_bw_usages[0]) _compare(bw_usages[1], expected_bw_usages[1]) _compare(bw_usages[2], expected_bw_usages[2]) timeutils.clear_time_override()
def notify_usage_exists(context, instance_ref, current_period=False, ignore_missing_network_data=True, system_metadata=None, extra_usage_info=None): """Generates 'exists' notification for an instance for usage auditing purposes. :param current_period: if True, this will generate a usage for the current usage period; if False, this will generate a usage for the previous audit period. :param ignore_missing_network_data: if True, log any exceptions generated while getting network info; if False, raise the exception. :param system_metadata: system_metadata DB entries for the instance, if not None. *NOTE*: Currently unused here in trunk, but needed for potential custom modifications. :param extra_usage_info: Dictionary containing extra values to add or override in the notification if not None. """ admin_context = nova.context.get_admin_context(read_deleted='yes') begin, end = utils.last_completed_audit_period() bw = {} if current_period: audit_start = end audit_end = utils.utcnow() else: audit_start = begin audit_end = end if (instance_ref.get('info_cache') and instance_ref['info_cache'].get('network_info')): cached_info = instance_ref['info_cache']['network_info'] nw_info = network_model.NetworkInfo.hydrate(cached_info) else: try: nw_info = network.API().get_instance_nw_info(admin_context, instance_ref) except Exception: LOG.exception('Failed to get nw_info', instance=instance_ref) if ignore_missing_network_data: return raise macs = [vif['address'] for vif in nw_info] uuids = [instance_ref.uuid] bw_usages = db.bw_usage_get_by_uuids(admin_context, uuids, audit_start) bw_usages = [b for b in bw_usages if b.mac in macs] for b in bw_usages: label = 'net-name-not-found-%s' % b['mac'] for vif in nw_info: if vif['address'] == b['mac']: label = vif['network']['label'] break bw[label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) if system_metadata is None: try: system_metadata = db.instance_system_metadata_get( context, instance_ref.uuid) except exception.NotFound: system_metadata = {} # add image metadata to the notification: image_meta = {} for md_key, md_value in system_metadata.iteritems(): if md_key.startswith('image_'): image_meta[md_key[6:]] = md_value extra_info = dict(audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw, image_meta=image_meta) if extra_usage_info: extra_info.update(extra_usage_info) notify_about_instance_usage(context, instance_ref, 'exists', system_metadata=system_metadata, extra_usage_info=extra_info)