Skip to content
Snippets Groups Projects
Verified Commit 66988cd1 authored by Shubham Kumar's avatar Shubham Kumar :four: Committed by GitLab
Browse files

Add and backfill namespace_id for resource_weight_events

Add and backfill namespace_id for resource_weight_events.

This table has a
[desired sharding key](https://docs.gitlab.com/ee/development/database/multiple_databases.html#define-a-desired_sharding_key-to-automatically-backfill-a-sharding_key)
configured ([view configuration](https://gitlab.com/gitlab-org/gitlab/-/blob/master/db/docs/resource_weight_events.yml)).

This merge request is the first step towards transforming the desired sharding key into a
[sharding key](https://docs.gitlab.com/ee/development/database/multiple_databases.html#defining-a-sharding-key-for-all-cell-local-tables).

This involves three changes:

- Adding a new column that will serve as the sharding key (along with the relevant index and foreign key).
- Populating the sharding key when new records are created by adding a database function and trigger.
- Scheduling a [batched background migration](https://docs.gitlab.com/ee/development/database/batched_background_migrations.html)
  to set the sharding key for existing records.

Once the background migration has completed, a second merge request will be created to finalize the background
migration and validate the not null constraint.

We have assigned a random backend engineer from ~"group::project management" to review these changes. Please review this merge
request from a ~backend perspective. The main thing we are looking to verify is that the added column and association
match the values specified by the [desired sharding key](https://gitlab.com/gitlab-org/gitlab/-/blob/master/db/docs/resource_weight_events.yml)
configuration and that backfilling the column from this other table makes sense in the context of this feature.

When you are finished, please:

1. Trigger the [database testing pipeline](https://docs.gitlab.com/ee/development/database/database_migration_pipeline.html)
   as instructed by Danger.
1. Request a review from the ~backend maintainer and ~database reviewer suggested by Danger.

If you have any questions or concerns, reach out to `@tigerwnz` or @shubhamkrai.

This merge request was generated by a once off keep implemented in
!143774

This change was generated by
[gitlab-housekeeper](https://gitlab.com/gitlab-org/gitlab/-/tree/master/gems/gitlab-housekeeper)
using the Keeps::BackfillDesiredShardingKeySmallTable keep.

To provide feedback on your experience with `gitlab-housekeeper` please create an issue with the
label ~"GitLab Housekeeper" and consider pinging the author of this keep.

Changelog: other
parent fd5625c9
No related branches found
No related tags found
No related merge requests found
Showing
with 203 additions and 1 deletion
---
migration_job_name: BackfillResourceWeightEventsNamespaceId
description: Backfills sharding key `resource_weight_events.namespace_id` from `issues`.
feature_category: team_planning
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/174405
milestone: '17.7'
queued_migration_version: 20241202142254
finalized_by: # version of the migration that finalized this BBM
......@@ -18,3 +18,4 @@ desired_sharding_key:
sharding_key: namespace_id
belongs_to: issue
table_size: small
desired_sharding_key_migration_job_name: BackfillResourceWeightEventsNamespaceId
# frozen_string_literal: true
class AddNamespaceIdToResourceWeightEvents < Gitlab::Database::Migration[2.2]
milestone '17.7'
def change
add_column :resource_weight_events, :namespace_id, :bigint
end
end
# frozen_string_literal: true
class IndexResourceWeightEventsOnNamespaceId < Gitlab::Database::Migration[2.2]
milestone '17.7'
disable_ddl_transaction!
INDEX_NAME = 'index_resource_weight_events_on_namespace_id'
def up
add_concurrent_index :resource_weight_events, :namespace_id, name: INDEX_NAME
end
def down
remove_concurrent_index_by_name :resource_weight_events, INDEX_NAME
end
end
# frozen_string_literal: true
class AddResourceWeightEventsNamespaceIdFk < Gitlab::Database::Migration[2.2]
milestone '17.7'
disable_ddl_transaction!
def up
add_concurrent_foreign_key :resource_weight_events, :namespaces, column: :namespace_id, on_delete: :cascade
end
def down
with_lock_retries do
remove_foreign_key :resource_weight_events, column: :namespace_id
end
end
end
# frozen_string_literal: true
class AddResourceWeightEventsNamespaceIdTrigger < Gitlab::Database::Migration[2.2]
milestone '17.7'
def up
install_sharding_key_assignment_trigger(
table: :resource_weight_events,
sharding_key: :namespace_id,
parent_table: :issues,
parent_sharding_key: :namespace_id,
foreign_key: :issue_id
)
end
def down
remove_sharding_key_assignment_trigger(
table: :resource_weight_events,
sharding_key: :namespace_id,
parent_table: :issues,
parent_sharding_key: :namespace_id,
foreign_key: :issue_id
)
end
end
# frozen_string_literal: true
class QueueBackfillResourceWeightEventsNamespaceId < Gitlab::Database::Migration[2.2]
milestone '17.7'
restrict_gitlab_migration gitlab_schema: :gitlab_main_cell
MIGRATION = "BackfillResourceWeightEventsNamespaceId"
DELAY_INTERVAL = 2.minutes
BATCH_SIZE = 1000
SUB_BATCH_SIZE = 100
def up
queue_batched_background_migration(
MIGRATION,
:resource_weight_events,
:id,
:namespace_id,
:issues,
:namespace_id,
:issue_id,
job_interval: DELAY_INTERVAL,
batch_size: BATCH_SIZE,
sub_batch_size: SUB_BATCH_SIZE
)
end
def down
delete_batched_background_migration(
MIGRATION,
:resource_weight_events,
:id,
[
:namespace_id,
:issues,
:namespace_id,
:issue_id
]
)
end
end
9424cfaf0ef1901c77d2be035812764811d78ea903975370ef55d58005af0c7b
\ No newline at end of file
f0a1dbd97945d7a6302de20b5603812aacff11f063d0be57f289adee15f75e9b
\ No newline at end of file
b2b931e7b644a07bb468b5b4cf8f822094c08f44696044360db83e16bdee5c51
\ No newline at end of file
30f26b3bea60cb0a03e0623cc825c6641a3155ab327b2aaafb330e823b256536
\ No newline at end of file
fb67017012babaa58f32b3ea301123cf46a606b96967f4dca21e97c6660a3679
\ No newline at end of file
......@@ -2945,6 +2945,22 @@ RETURN NEW;
END
$$;
 
CREATE FUNCTION trigger_e4a6cde57b42() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF NEW."namespace_id" IS NULL THEN
SELECT "namespace_id"
INTO NEW."namespace_id"
FROM "issues"
WHERE "issues"."id" = NEW."issue_id";
END IF;
RETURN NEW;
END
$$;
CREATE FUNCTION trigger_e815625b59fa() RETURNS trigger
LANGUAGE plpgsql
AS $$
......@@ -19205,7 +19221,8 @@ CREATE TABLE resource_weight_events (
issue_id bigint NOT NULL,
weight integer,
created_at timestamp with time zone NOT NULL,
previous_weight integer
previous_weight integer,
namespace_id bigint
);
 
CREATE SEQUENCE resource_weight_events_id_seq
......@@ -32354,6 +32371,8 @@ CREATE INDEX index_resource_weight_events_on_issue_id_and_created_at ON resource
 
CREATE INDEX index_resource_weight_events_on_issue_id_and_weight ON resource_weight_events USING btree (issue_id, weight);
 
CREATE INDEX index_resource_weight_events_on_namespace_id ON resource_weight_events USING btree (namespace_id);
CREATE INDEX index_resource_weight_events_on_user_id ON resource_weight_events USING btree (user_id);
 
CREATE INDEX index_reviews_on_author_id ON reviews USING btree (author_id);
......@@ -35738,6 +35757,8 @@ CREATE TRIGGER trigger_e1da4a738230 BEFORE INSERT OR UPDATE ON vulnerability_ext
 
CREATE TRIGGER trigger_e49ab4d904a0 BEFORE INSERT OR UPDATE ON vulnerability_finding_links FOR EACH ROW EXECUTE FUNCTION trigger_e49ab4d904a0();
 
CREATE TRIGGER trigger_e4a6cde57b42 BEFORE INSERT OR UPDATE ON resource_weight_events FOR EACH ROW EXECUTE FUNCTION trigger_e4a6cde57b42();
CREATE TRIGGER trigger_e815625b59fa BEFORE INSERT OR UPDATE ON resource_link_events FOR EACH ROW EXECUTE FUNCTION trigger_e815625b59fa();
 
CREATE TRIGGER trigger_ebab34f83f1d BEFORE INSERT OR UPDATE ON packages_debian_publications FOR EACH ROW EXECUTE FUNCTION trigger_ebab34f83f1d();
......@@ -36778,6 +36799,9 @@ ALTER TABLE ONLY dependency_proxy_blob_states
ALTER TABLE ONLY issues
ADD CONSTRAINT fk_96b1dd429c FOREIGN KEY (milestone_id) REFERENCES milestones(id) ON DELETE SET NULL;
 
ALTER TABLE ONLY resource_weight_events
ADD CONSTRAINT fk_97c7849ca4 FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE;
ALTER TABLE ONLY agent_user_access_group_authorizations
ADD CONSTRAINT fk_97ce8e8284 FOREIGN KEY (agent_id) REFERENCES cluster_agents(id) ON DELETE CASCADE;
 
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
class BackfillResourceWeightEventsNamespaceId < BackfillDesiredShardingKeyJob
operation_name :backfill_resource_weight_events_namespace_id
feature_category :team_planning
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::BackfillResourceWeightEventsNamespaceId,
feature_category: :team_planning,
schema: 20241202142250 do
include_examples 'desired sharding key backfill job' do
let(:batch_table) { :resource_weight_events }
let(:backfill_column) { :namespace_id }
let(:backfill_via_table) { :issues }
let(:backfill_via_column) { :namespace_id }
let(:backfill_via_foreign_key) { :issue_id }
end
end
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe QueueBackfillResourceWeightEventsNamespaceId, feature_category: :team_planning do
let!(:batched_migration) { described_class::MIGRATION }
it 'schedules a new batched migration' do
reversible_migration do |migration|
migration.before -> {
expect(batched_migration).not_to have_scheduled_batched_migration
}
migration.after -> {
expect(batched_migration).to have_scheduled_batched_migration(
table_name: :resource_weight_events,
column_name: :id,
interval: described_class::DELAY_INTERVAL,
batch_size: described_class::BATCH_SIZE,
sub_batch_size: described_class::SUB_BATCH_SIZE,
gitlab_schema: :gitlab_main_cell,
job_arguments: [
:namespace_id,
:issues,
:namespace_id,
:issue_id
]
)
}
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment