original_docstring: This fills missing VirtualInterface Objects in Nova DB
docstring: This fills missing VirtualInterface Objects in Nova DB
docstring_tokens: This fills missing VirtualInterface Objects in Nova DB
code: def fill_virtual_interface_list(context, max_count):
      """This fills missing VirtualInterface Objects in Nova DB"""
      count_hit = 0
      count_all = 0
  
      def _regenerate_vif_list_base_on_cache(context,
                                             instance,
                                             old_vif_list,
                                             nw_info):
          # Set old VirtualInterfaces as deleted.
          for vif in old_vif_list:
              vif.destroy()
  
          # Generate list based on current cache:
          for vif in nw_info:
              vif_obj = objects.VirtualInterface(context)
              vif_obj.uuid = vif['id']
              vif_obj.address = "%s/%s" % (vif['address'], vif['id'])
              vif_obj.instance_uuid = instance['uuid']
              # Find tag from previous VirtualInterface object if exist.
              old_vif = [x for x in old_vif_list if x.uuid == vif['id']]
              vif_obj.tag = old_vif[0].tag if len(old_vif) > 0 else None
              vif_obj.create()
  
      cells = objects.CellMappingList.get_all(context)
      for cell in cells:
          if count_all == max_count:
              # We reached the limit of checked instances per
              # this function run.
              # Stop, do not go to other cell.
              break
  
          with nova_context.target_cell(context, cell) as cctxt:
              marker = _get_marker_for_migrate_instances(cctxt)
              filters = {'deleted': False}
  
              # Adjust the limit of migrated instances.
              # If user wants to process a total of 100 instances
              # and we did a 75 in cell1, then we only need to
              # verify 25 more in cell2, no more.
              adjusted_limit = max_count - count_all
  
              instances = objects.InstanceList.get_by_filters(
                  cctxt,
                  filters=filters,
                  sort_key='created_at',
                  sort_dir='asc',
                  marker=marker,
                  limit=adjusted_limit)
  
              for instance in instances:
                  # We don't want to fill vif for FAKE instance.
                  if instance.uuid == FAKE_UUID:
                      continue
  
                  try:
                      info_cache = objects.InstanceInfoCache.\
                          get_by_instance_uuid(cctxt, instance.get('uuid'))
                      if not info_cache.network_info:
                          LOG.info('InstanceInfoCache object has not set '
                                   'NetworkInfo field. '
                                   'Skipping build of VirtualInterfaceList.')
                          continue
                  except exception.InstanceInfoCacheNotFound:
                      LOG.info('Instance has no InstanceInfoCache object. '
                               'Skipping build of VirtualInterfaceList for it.')
                      continue
  
                  # It by design filters out deleted vifs.
                  vif_list = VirtualInterfaceList.\
                      get_by_instance_uuid(cctxt, instance.get('uuid'))
  
                  nw_info = info_cache.network_info
                  # This should be list with proper order of vifs,
                  # but we're not sure about that.
                  cached_vif_ids = [vif['id'] for vif in nw_info]
                  # This is ordered list of vifs taken from db.
                  db_vif_ids = [vif.uuid for vif in vif_list]
  
                  count_all += 1
                  if cached_vif_ids == db_vif_ids:
                      # The list of vifs and its order in cache and in
                      # virtual_interfaces is the same. So we could end here.
                      continue
                  elif len(db_vif_ids) < len(cached_vif_ids):
                      # Seems to be an instance from release older than
                      # Newton and we don't have full VirtualInterfaceList for
                      # it. Rewrite whole VirtualInterfaceList using interface
                      # order from InstanceInfoCache.
                      count_hit += 1
                      LOG.info('Got an instance %s with less VIFs defined in DB '
                               'than in cache. Could be Pre-Newton instance. '
                               'Building new VirtualInterfaceList for it.',
                               instance.uuid)
                      _regenerate_vif_list_base_on_cache(cctxt,
                                                         instance,
                                                         vif_list,
                                                         nw_info)
                  elif len(db_vif_ids) > len(cached_vif_ids):
                      # Seems vif list is inconsistent with cache.
                      # it could be a broken cache or interface
                      # during attach. Do nothing.
                      LOG.info('Got an unexpected number of VIF records in the '
                               'database compared to what was stored in the '
                               'instance_info_caches table for instance %s. '
                               'Perhaps it is an instance during interface '
                               'attach. Do nothing.', instance.uuid)
                      continue
                  else:
                      # The order is different between lists.
                      # We need a source of truth, so rebuild order
                      # from cache.
                      count_hit += 1
                      LOG.info('Got an instance %s with different order of '
                               'VIFs between DB and cache. '
                               'We need a source of truth, so rebuild order '
                               'from cache.', instance.uuid)
                      _regenerate_vif_list_base_on_cache(cctxt,
                                                         instance,
                                                         vif_list,
                                                         nw_info)
  
              # Set marker to point last checked instance.
              if instances:
                  marker = instances[-1].uuid
                  _set_or_delete_marker_for_migrate_instances(cctxt, marker)
  
      return count_all, count_hit
code_tokens: def fill_virtual_interface_list ( context , max_count ) : count_hit = 0 count_all = 0 def _regenerate_vif_list_base_on_cache ( context , instance , old_vif_list , nw_info ) : for vif in old_vif_list : vif . destroy ( ) for vif in nw_info : vif_obj = objects . VirtualInterface ( context ) vif_obj . uuid = vif [ 'id' ] vif_obj . address = "%s/%s" % ( vif [ 'address' ] , vif [ 'id' ] ) vif_obj . instance_uuid = instance [ 'uuid' ] old_vif = [ x for x in old_vif_list if x . uuid == vif [ 'id' ] ] vif_obj . tag = old_vif [ 0 ] . tag if len ( old_vif ) > 0 else None vif_obj . create ( ) cells = objects . CellMappingList . get_all ( context ) for cell in cells : if count_all == max_count : break with nova_context . target_cell ( context , cell ) as cctxt : marker = _get_marker_for_migrate_instances ( cctxt ) filters = { 'deleted' : False } adjusted_limit = max_count - count_all instances = objects . InstanceList . get_by_filters ( cctxt , filters = filters , sort_key = 'created_at' , sort_dir = 'asc' , marker = marker , limit = adjusted_limit ) for instance in instances : if instance . uuid == FAKE_UUID : continue try : info_cache = objects . InstanceInfoCache . get_by_instance_uuid ( cctxt , instance . get ( 'uuid' ) ) if not info_cache . network_info : LOG . info ( 'InstanceInfoCache object has not set ' 'NetworkInfo field. ' 'Skipping build of VirtualInterfaceList.' ) continue except exception . InstanceInfoCacheNotFound : LOG . info ( 'Instance has no InstanceInfoCache object. ' 'Skipping build of VirtualInterfaceList for it.' ) continue vif_list = VirtualInterfaceList . get_by_instance_uuid ( cctxt , instance . get ( 'uuid' ) ) nw_info = info_cache . network_info cached_vif_ids = [ vif [ 'id' ] for vif in nw_info ] db_vif_ids = [ vif . uuid for vif in vif_list ] count_all += 1 if cached_vif_ids == db_vif_ids : continue elif len ( db_vif_ids ) < len ( cached_vif_ids ) : count_hit += 1 LOG . info ( 'Got an instance %s with less VIFs defined in DB ' 'than in cache. Could be Pre-Newton instance. ' 'Building new VirtualInterfaceList for it.' , instance . uuid ) _regenerate_vif_list_base_on_cache ( cctxt , instance , vif_list , nw_info ) elif len ( db_vif_ids ) > len ( cached_vif_ids ) : LOG . info ( 'Got an unexpected number of VIF records in the ' 'database compared to what was stored in the ' 'instance_info_caches table for instance %s. ' 'Perhaps it is an instance during interface ' 'attach. Do nothing.' , instance . uuid ) continue else : count_hit += 1 LOG . info ( 'Got an instance %s with different order of ' 'VIFs between DB and cache. ' 'We need a source of truth, so rebuild order ' 'from cache.' , instance . uuid ) _regenerate_vif_list_base_on_cache ( cctxt , instance , vif_list , nw_info ) if instances : marker = instances [ - 1 ] . uuid _set_or_delete_marker_for_migrate_instances ( cctxt , marker ) return count_all , count_hit
short_docstring: This fills missing VirtualInterface Objects in Nova DB
short_docstring_tokens: This fills missing VirtualInterface Objects in Nova DB
comment: """This fills missing VirtualInterface Objects in Nova DB"""$SEP$# Set old VirtualInterfaces as deleted.$SEP$# Generate list based on current cache:$SEP$# Find tag from previous VirtualInterface object if exist.$SEP$# We reached the limit of checked instances per$SEP$# this function run.$SEP$# Stop, do not go to other cell.$SEP$# Adjust the limit of migrated instances.$SEP$# If user wants to process a total of 100 instances$SEP$# and we did a 75 in cell1, then we only need to$SEP$# verify 25 more in cell2, no more.$SEP$# We don't want to fill vif for FAKE instance.$SEP$# It by design filters out deleted vifs.$SEP$# This should be list with proper order of vifs,$SEP$# but we're not sure about that.$SEP$# This is ordered list of vifs taken from db.$SEP$# The list of vifs and its order in cache and in$SEP$# virtual_interfaces is the same. So we could end here.$SEP$# Seems to be an instance from release older than$SEP$# Newton and we don't have full VirtualInterfaceList for$SEP$# it. Rewrite whole VirtualInterfaceList using interface$SEP$# order from InstanceInfoCache.$SEP$# Seems vif list is inconsistent with cache.$SEP$# it could be a broken cache or interface$SEP$# during attach. Do nothing.$SEP$# The order is different between lists.$SEP$# We need a source of truth, so rebuild order$SEP$# from cache.$SEP$# Set marker to point last checked instance.