Skip to content
Snippets Groups Projects

Use direct SQL to generate internal ids

Merged Patrick Bair requested to merge pb-generate-iid-in-db-behind-ff into master
All threads resolved!

What does this MR do?

Related issue: #325861 (closed)

Reimplement changes from !64240 (closed) that removes database round-trips and explicit locking when updating internal_ids. We make the assumption on generating a new value that most operations will modify existing records, so we prefer to attempt to update the row first. After some discussion, we decided to put consider putting the changes behind a feature flag, so this MR was created to do that (rather than the direct cutover in the previous MR).

Example of creation of object with internal_id, where no existing record exists (create_record!):

  Update InternalId (0.7ms)  UPDATE "internal_ids" SET "last_value" = ("internal_ids"."last_value" + 1) WHERE "internal_ids"."project_id" = 7 AND "internal_ids"."usage" = 0 RETURNING "last_value" /*application:console,line:/app/models/internal_id.rb:91:in `update_record!'*/
  TRANSACTION (0.1ms)  SAVEPOINT active_record_1 /*application:console,line:/app/models/concerns/atomic_internal_id.rb:211:in `block in project_init'*/
   (2.1ms)  SELECT MAX("issues"."iid") FROM "issues" WHERE "issues"."project_id" = 7 /*application:console,line:/app/models/concerns/atomic_internal_id.rb:211:in `block in project_init'*/
  InternalId Create (0.7ms)  INSERT INTO "internal_ids" ("project_id", "usage", "last_value") VALUES (7, 0, 1) RETURNING "id" /*application:console,line:/app/models/internal_id.rb:102:in `block in create_record!'*/

First attempts to update the existing record, and because none is found, it creates a new record. If the new record were to conflict with a concurrent insert, it retries the update.

Example of update of object with internal_id, where a record exists (update_record!):

 Update InternalId (0.3ms)  UPDATE "internal_ids" SET "last_value" = ("internal_ids"."last_value" + 1) WHERE "internal_ids"."project_id" = 7 AND "internal_ids"."usage" = 0 RETURNING "last_value" /*application:console,line:/app/models/internal_id.rb:91:in `update_record!'*/

It updates the record only.

Screenshots or Screencasts (strongly suggested)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

Does this MR contain changes to processing or storing of credentials or tokens, authorization and authentication methods or other items described in the security review guidelines? If not, then delete this Security section.

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team
Edited by Patrick Bair

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Patrick Bair
  • Author Contributor

    @abrandl created a new MR to put these changes behind a feature flag as we mentioned. I can't think of a reason we can roll this out this way, as the new and approach should work together (and have to anyhow, because of our deployment process). Otherwise, it should be the same approach as the previous MR. can you review?

  • Patrick Bair requested review from @abrandl

    requested review from @abrandl

  • Andreas Brandl
  • Andreas Brandl
  • Andreas Brandl removed review request for @abrandl

    removed review request for @abrandl

  • Patrick Bair added 620 commits

    added 620 commits

    Compare with previous version

  • Patrick Bair mentioned in merge request !64240 (closed)

    mentioned in merge request !64240 (closed)

  • Patrick Bair requested review from @abrandl and @alinamihaila

    requested review from @abrandl and @alinamihaila

  • Alina Mihaila
  • Alina Mihaila approved this merge request

    approved this merge request

  • Alina Mihaila removed review request for @alinamihaila

    removed review request for @alinamihaila

  • Andreas Brandl approved this merge request

    approved this merge request

  • Nice @pbair, LGTM! 👍

  • Andreas Brandl removed review request for @abrandl

    removed review request for @abrandl

  • Patrick Bair added 166 commits

    added 166 commits

    Compare with previous version

  • Patrick Bair requested review from @rspeicher

    requested review from @rspeicher

  • Robert Speicher approved this merge request

    approved this merge request

  • @pbair @alinamihaila LGTM, thanks. 👍 Left one non-blocking question, but I've approved.

  • Patrick Bair added 643 commits

    added 643 commits

    Compare with previous version

  • Patrick Bair resolved all threads

    resolved all threads

  • Author Contributor

    Since it's gotten the required approvals and is behind a feature flag, I think it's safe to go forward with this. Setting MWPS

  • Patrick Bair added 1 commit

    added 1 commit

    • 56134706 - Use direct SQL to generate internal ids

    Compare with previous version

  • Patrick Bair changed milestone to %14.2

    changed milestone to %14.2

  • Robert Speicher enabled an automatic merge when the pipeline for 4837ded4 succeeds

    enabled an automatic merge when the pipeline for 4837ded4 succeeds

  • Robert Speicher mentioned in commit 40735f54

    mentioned in commit 40735f54

  • added workflowcanary label and removed workflowstaging label

  • added workflowproduction label and removed workflowcanary label

  • mentioned in issue #325861 (closed)

  • Patrick Bair mentioned in merge request !69769 (merged)

    mentioned in merge request !69769 (merged)

  • added devopsdata stores label and removed devopssystems label

  • Please register or sign in to reply
    Loading