Просмотр исходного кода

ref(hc): Add org and integration ids to denormalize cross silo lookups (#47088)

In preparation for breaking the OrgIntegration foreign key, adding
columns to denormalize the org and integration ids.
Begins dual writing into the columns. Ultimately goal will not be a
unique index -- this is intentional because with the eventual
consistency of org integration deletions, it may be possible to end up
with multiple entries with the same (org, integration) pairing for a
short duration (after org int is created, deleted, then another one
created, you can have two org int that share the same pair and still be
fair play).
Zach Collins 1 год назад
Родитель
Сommit
2ce108e606

+ 1 - 1
migrations_lockfile.txt

@@ -6,5 +6,5 @@ To resolve this, rebase against latest master and regenerate your migration. Thi
 will then be regenerated, and you should be able to merge without conflicts.
 
 nodestore: 0002_nodestore_no_dictfield
-sentry: 0411_remove_until_escalating_column_from_groupsnooze
+sentry: 0412_org_integration_denormalization
 social_auth: 0001_initial

+ 47 - 0
src/sentry/migrations/0412_org_integration_denormalization.py

@@ -0,0 +1,47 @@
+# Generated by Django 2.2.28 on 2023-04-07 22:09
+
+from django.db import migrations
+
+import sentry.db.models.fields.bounded
+from sentry.new_migrations.migrations import CheckedMigration
+
+
+class Migration(CheckedMigration):
+    # This flag is used to mark that a migration shouldn't be automatically run in production. For
+    # the most part, this should only be used for operations where it's safe to run the migration
+    # after your code has deployed. So this should not be used for most operations that alter the
+    # schema of a table.
+    # Here are some things that make sense to mark as dangerous:
+    # - Large data migrations. Typically we want these to be run manually by ops so that they can
+    #   be monitored and not block the deploy for a long period of time while they run.
+    # - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
+    #   have ops run this and not block the deploy. Note that while adding an index is a schema
+    #   change, it's completely safe to run the operation after the code has deployed.
+    is_dangerous = False
+
+    dependencies = [
+        ("sentry", "0411_remove_until_escalating_column_from_groupsnooze"),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name="pagerdutyservice",
+            name="integration_id",
+            field=sentry.db.models.fields.bounded.BoundedBigIntegerField(null=True),
+        ),
+        migrations.AddField(
+            model_name="pagerdutyservice",
+            name="organization_id",
+            field=sentry.db.models.fields.bounded.BoundedBigIntegerField(null=True),
+        ),
+        migrations.AddField(
+            model_name="repositoryprojectpathconfig",
+            name="integration_id",
+            field=sentry.db.models.fields.bounded.BoundedBigIntegerField(null=True),
+        ),
+        migrations.AddField(
+            model_name="repositoryprojectpathconfig",
+            name="organization_id",
+            field=sentry.db.models.fields.bounded.BoundedBigIntegerField(null=True),
+        ),
+    ]

+ 26 - 0
src/sentry/models/integrations/organization_integrity_backfill_mixin.py

@@ -0,0 +1,26 @@
+# Mixin intended to be attached to Model classes that belong to an opposing silo.
+# Allows looking up and storing the organization_id and integration_id of an external
+# OrganizationIntegration, and powers backfill associated with each.
+from __future__ import annotations
+
+from typing import Any
+
+from sentry.services.hybrid_cloud.integration import integration_service
+
+
+class OrganizationIntegrityBackfillMixin:
+    organization_integration_id: Any
+    organization_id: Any
+    integration_id: Any
+
+    def save(self, *args, **kwds) -> None:
+        if self.organization_id is None or self.integration_id is None:
+            # Find the original org integration instance, backfill in the identifiers.
+            org_integrations = integration_service.get_organization_integrations(
+                org_integration_ids=[self.organization_integration_id],
+            )
+            assert org_integrations, "Could not find org integration!"
+            org_integration = org_integrations[0]
+            self.organization_id = org_integration.organization_id
+            self.integration_id = org_integration.integration_id
+        super().save(*args, **kwds)

+ 13 - 2
src/sentry/models/integrations/pagerduty_service.py

@@ -1,14 +1,25 @@
 from django.db import models
 from django.utils import timezone
 
-from sentry.db.models import DefaultFieldsModel, FlexibleForeignKey, region_silo_only_model
+from sentry.db.models import (
+    BoundedBigIntegerField,
+    DefaultFieldsModel,
+    FlexibleForeignKey,
+    region_silo_only_model,
+)
+from sentry.models.integrations.organization_integrity_backfill_mixin import (
+    OrganizationIntegrityBackfillMixin,
+)
 
 
 @region_silo_only_model
-class PagerDutyService(DefaultFieldsModel):
+class PagerDutyService(DefaultFieldsModel, OrganizationIntegrityBackfillMixin):
     __include_in_export__ = False
 
     organization_integration = FlexibleForeignKey("sentry.OrganizationIntegration")
+    organization_id = BoundedBigIntegerField(null=True, db_index=False)
+    # From a region point of view, you really only have per organization scoping.
+    integration_id = BoundedBigIntegerField(null=True, db_index=False)
     integration_key = models.CharField(max_length=255)
     service_name = models.CharField(max_length=255)
     date_added = models.DateTimeField(default=timezone.now)

+ 15 - 5
src/sentry/models/integrations/repository_project_path_config.py

@@ -1,18 +1,28 @@
 from django.db import models, transaction
 from django.db.models.signals import post_save
 
-from sentry.db.models import DefaultFieldsModel, FlexibleForeignKey, region_silo_only_model
+from sentry.db.models import (
+    BoundedBigIntegerField,
+    DefaultFieldsModel,
+    FlexibleForeignKey,
+    region_silo_only_model,
+)
+from sentry.models.integrations.organization_integrity_backfill_mixin import (
+    OrganizationIntegrityBackfillMixin,
+)
 
 
 @region_silo_only_model
-class RepositoryProjectPathConfig(DefaultFieldsModel):
+class RepositoryProjectPathConfig(DefaultFieldsModel, OrganizationIntegrityBackfillMixin):
     __include_in_export__ = False
 
     repository = FlexibleForeignKey("sentry.Repository")
     project = FlexibleForeignKey("sentry.Project", db_constraint=False)
-    organization_integration = FlexibleForeignKey(
-        "sentry.OrganizationIntegration", on_delete=models.CASCADE
-    )
+
+    organization_integration = FlexibleForeignKey("sentry.OrganizationIntegration")
+    organization_id = BoundedBigIntegerField(null=True, db_index=False)
+    # From a region point of view, you really only have per organization scoping.
+    integration_id = BoundedBigIntegerField(null=True, db_index=False)
     stack_root = models.TextField()
     source_root = models.TextField()
     default_branch = models.TextField(null=True)