Class: Katello::ContentUnitIndexer

Inherits:
Object
  • Object
show all
Defined in:
app/services/katello/content_unit_indexer.rb

Defined Under Namespace

Classes: RepoAssociationTracker

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_type:, repository: nil, pulp_content_ids: nil, optimized: true) ⇒ ContentUnitIndexer

Returns a new instance of ContentUnitIndexer.



3
4
5
6
7
8
9
10
11
# File 'app/services/katello/content_unit_indexer.rb', line 3

def initialize(content_type:, repository: nil, pulp_content_ids: nil, optimized: true)
  @content_type = content_type
  @model_class = content_type.model_class
  @service_class = SmartProxy.pulp_primary!.content_service(content_type)
  @repository = repository
  @content_type = content_type
  @pulp_content_ids = pulp_content_ids
  @optimized = optimized
end

Class Method Details

.insert_timestamps(model_class, units) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'app/services/katello/content_unit_indexer.rb', line 145

def self.insert_timestamps(model_class, units)
  dates = model_class.columns.map(&:name).include?("created_at")
  return units unless dates
  units.each do |row|
    row[:created_at] = DateTime.now
    row[:updated_at] = DateTime.now
  end
  units
end

.pulp_id_to_id_map(content_type, pulp_ids) ⇒ Object



82
83
84
85
86
87
88
# File 'app/services/katello/content_unit_indexer.rb', line 82

def self.pulp_id_to_id_map(content_type, pulp_ids)
  map = {}
  content_type.model_class.with_pulp_id(pulp_ids).select(:id, :pulp_id).each do |model|
    map[model.pulp_id] = model.id
  end
  map
end

Instance Method Details

#association_class_uniqueness_attributesObject



239
240
241
242
243
244
245
246
247
248
249
# File 'app/services/katello/content_unit_indexer.rb', line 239

def association_class_uniqueness_attributes
  columns = [@model_class.unit_id_field, 'repository_id']
  found = ActiveRecord::Base.connection.indexes(@model_class.repository_association_class.table_name).find do |index|
    index.columns.sort == columns.sort
  end
  if found
    found.columns
  else
    fail "Unable to find unique index for #{columns} on table #{self.repository_association_class.table_name}"
  end
end

#clean_duplicate_docker_tagsObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/services/katello/content_unit_indexer.rb', line 200

def clean_duplicate_docker_tags
  # Deduplicate docker tags by name, keeping the one with the newest updated_at
  deduplicated_tags = @repository.docker_tags.group_by(&:name).map { |_name, tags| tags.max_by(&:updated_at) }
  return if deduplicated_tags.empty?
  valid_names = deduplicated_tags.map(&:name)
  valid_taggable_ids = deduplicated_tags.map(&:id)

  # Remove duplicate tags with same names as valid tags, keeping only the newest ones
  # Lookup by docker_manifest is important; docker_tags usually only contains the newest tag.
  tag_ids_to_remove = @repository.docker_manifests
                        .joins(:docker_tags)
                        .where(katello_docker_tags: { name: valid_names })
                        .where.not(katello_docker_tags: {id: valid_taggable_ids})
                        .pluck('katello_docker_tags.id')

  return if tag_ids_to_remove.empty?
  Rails.logger.info("Removing #{tag_ids_to_remove.count} duplicate docker tag associations in repository '#{@repository.label}'")
  Katello::DockerTag.where(id: tag_ids_to_remove).destroy_all
end

#clean_filter_rules(repo_associations_to_destroy) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/services/katello/content_unit_indexer.rb', line 177

def clean_filter_rules(repo_associations_to_destroy)
  affected_content_view_ids = @repository.content_views.non_default.pluck(:id)
  return false if affected_content_view_ids.empty?
  case @model_class.to_s
  when 'Katello::ModuleStream'
    module_stream_ids = repo_associations_to_destroy.pluck(:module_stream_id)
    filter_rules = ::Katello::ContentViewModuleStreamFilterRule.
      in_content_views(affected_content_view_ids).where(module_stream_id: module_stream_ids)
    filter_rules.delete_all
  when 'Katello::Erratum'
    errata_ids = ::Katello::Erratum.where(id: repo_associations_to_destroy.select(:erratum_id)).pluck(:errata_id)
    filter_rules = ::Katello::ContentViewErratumFilterRule.in_content_views(affected_content_view_ids).where(errata_id: errata_ids)
    filter_rules.delete_all
  when 'Katello::PackageGroup'
    package_group_uuids = ::Katello::PackageGroup.where(id: repo_associations_to_destroy.select(:package_group_id)).pluck(:pulp_id)
    filter_rules = ::Katello::ContentViewPackageGroupFilterRule.
      in_content_views(affected_content_view_ids).where(uuid: package_group_uuids)
    filter_rules.delete_all
  else
    return false
  end
end

#fetch_only_idsObject



155
156
157
158
159
160
# File 'app/services/katello/content_unit_indexer.rb', line 155

def fetch_only_ids
  @optimized && @repository &&
    !@repository.content_view.default? &&
    !@repository.repository_type.unique_content_per_repo &&
    @service_class.supports_id_fetch?
end

#import_all(filtered_indexing = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/katello/content_unit_indexer.rb', line 24

def import_all(filtered_indexing = false)
  association_tracker = RepoAssociationTracker.new(@content_type, @service_class, @repository)
  units_from_pulp.each do |units|
    units.each do |unit|
      association_tracker.push(unit)
      remove_duplicates(unit)
    end

    unless fetch_only_ids
      to_insert = units.map do |unit|
        if @content_type.generic?
          @service_class.generate_model_row(unit, @content_type)
        else
          @service_class.generate_model_row(unit)
        end
      end

      # Even after this bug (https://github.com/pulp/pulp_rpm/issues/2821) is fixed,
      # it is possible to have duplicate errata associated to a repo.
      if @content_type.label == 'erratum'
        to_insert.uniq! { |row| row["pulp_id"] || row[:pulp_id] }
      end

      next if to_insert.empty?
      insert_timestamps(to_insert)
      upsert_with_deadlock_retry(to_insert)
    end

    import_associations(units) if @repository
  end

  if @repository
    sync_repository_associations(association_tracker, additive: filtered_indexing)
  end
end

#import_associations(units) ⇒ Object



69
70
71
72
# File 'app/services/katello/content_unit_indexer.rb', line 69

def import_associations(units)
  pulp_id_to_id = self.class.pulp_id_to_id_map(@content_type, units.map { |unit| unit[@service_class.unit_identifier] })
  @service_class.insert_child_associations(units, pulp_id_to_id) if @service_class.respond_to?(:insert_child_associations)
end

#insert_timestamps(units) ⇒ Object



141
142
143
# File 'app/services/katello/content_unit_indexer.rb', line 141

def insert_timestamps(units)
  self.class.insert_timestamps(@model_class, units)
end

#reimport_unitsObject



60
61
62
63
64
65
66
67
# File 'app/services/katello/content_unit_indexer.rb', line 60

def reimport_units
  units_from_pulp.each do |units|
    to_update = units.map do |unit|
      @service_class.generate_model_row(unit)
    end
    @model_class.upsert_all(to_update, unique_by: :pulp_id)
  end
end

#remove_duplicates(unit) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'app/services/katello/content_unit_indexer.rb', line 13

def remove_duplicates(unit)
  #when we are uploading units, we need to remove any duplicates from our indexed data
  if @content_type.label == 'rpm' && @repository && @pulp_content_ids
    rpms_to_disassociate = ::Katello::Rpm.where(name: unit[:name], version: unit[:version], release: unit[:release],
                                                epoch: unit[:epoch], arch: unit[:arch]).select(:id)
    if rpms_to_disassociate.any?
      ::Katello::RepositoryRpm.where(rpm_id: rpms_to_disassociate, repository_id: @repository.id).destroy_all
    end
  end
end

#sync_repository_associations(association_tracker, additive: false) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/services/katello/content_unit_indexer.rb', line 162

def sync_repository_associations(association_tracker, additive: false)
  unless additive
    ActiveRecord::Base.connection.uncached do
      repo_associations_to_destroy = @model_class.repository_association_class.where(repository_id: @repository.id).where.
        not(@model_class.unit_id_field => association_tracker.unit_ids)
      clean_filter_rules(repo_associations_to_destroy) if repo_associations_to_destroy.present? && [::Katello::ModuleStream, ::Katello::Erratum, ::Katello::PackageGroup].include?(@model_class)
      repo_associations_to_destroy.destroy_all
    end
  end
  return if association_tracker.db_values.empty?
  @model_class.repository_association_class.upsert_all(association_tracker.db_values, :unique_by => association_class_uniqueness_attributes)

  clean_duplicate_docker_tags if @model_class == Katello::DockerTag
end

#units_from_pulp(&block) ⇒ Object



74
75
76
77
78
79
80
# File 'app/services/katello/content_unit_indexer.rb', line 74

def units_from_pulp(&block)
  if @pulp_content_ids
    @service_class.pulp_units_batch_all(@pulp_content_ids, &block)
  elsif @repository
    @service_class.pulp_units_batch_for_repo(@repository, fetch_identifiers: fetch_only_ids, content_type: @content_type, &block)
  end
end

#upsert_with_deadlock_retry(to_insert) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/services/katello/content_unit_indexer.rb', line 220

def upsert_with_deadlock_retry(to_insert)
  retry_count = 0
  begin
    if @content_type.mutable
      @model_class.upsert_all(to_insert, unique_by: :pulp_id)
    else
      @model_class.insert_all(to_insert, unique_by: :pulp_id)
    end
  rescue ActiveRecord::Deadlocked
    retry_count += 1
    if retry_count <= 3
      sleep(rand(1..5))
      retry
    else
      raise
    end
  end
end