Skip to content

Handle InvalidForeignKey errors when indexing deleted projects

Background

Related to #360581 (closed)

The error described in the issue doesn't happen often (and the instances I've seen are only related to QA data, but it is possible to occur in rare instances).

What's happening is:

  1. new project is created and queued for indexing
  2. the @indexer variable is not set in the initialization of Gitlab::Elastic::Indexer
  3. within the update_index_status method, Project.exists? passes
  4. project is deleted
  5. @index_status is set using IndexStatus.find_or_safe_create_by! which throws an ActiveRecord::InvalidForeignKey error because the project was deleted in the small window between the check and the create call

What does this MR do and why?

  • Adds a rescue for ActiveRecord::InvalidForeignKey in update_index_status and logs a warning if it occurs
  • Change indexer.rb to use the build_structured_payload method so class is filled out in all the logs
  • Add (and refactor a little) the indexer specs

Screenshots or screen recordings

N/A

How to set up and validate locally

I was unable to verify this manually by indexing b/c it's next to impossible to delete a project in between those checks without a breakpoint

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Merge request reports

Loading