Skip to content

[GlitchTip] INSIGHTS-HCCM-STAGE-5HT: Fix IntegrityError race in report period save - #6259

Draft
koku-ci-triager-bot wants to merge 1 commit into
mainfrom
glitchtip/4499889-fix-report-period-save-schema-context
Draft

[GlitchTip] INSIGHTS-HCCM-STAGE-5HT: Fix IntegrityError race in report period save#6259
koku-ci-triager-bot wants to merge 1 commit into
mainfrom
glitchtip/4499889-fix-report-period-save-schema-context

Conversation

@koku-ci-triager-bot

@koku-ci-triager-bot koku-ci-triager-bot commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

GlitchTip

https://glitchtip.devshift.net/insights-hccm-stage/issues/4499889

Classification

code_fixable

Summary

distribute_costs_and_update_ui_summary in ocp_cost_model_cost_updater.py calls report_period.save() without a schema_context wrapper and without catching IntegrityError. When a provider is deleted while cost model processing is in-flight, the FK constraint reporting_ocpusagere_provider_id_7348fe66_fk_reporting fires and the exception bubbles up unhandled, causing 11+ error events in GlitchTip.

Root cause: Missing schema_context wrapper (needed for django-tenants ORM routing) and no guard for the provider-delete race condition.

Fix: Wrap report_period.save() in with schema_context(self._schema) and catch IntegrityError with a warning log + early return. This matches the pattern used in ocp_cloud_parquet_summary_updater.py:299.

Generated by GlitchTip Triager (Ambient Code). Review before merge.

Summary by Sourcery

Bug Fixes:

  • Prevent unhandled IntegrityError when a provider is deleted during OCP cost model processing by safely saving the report period within the correct tenant schema and skipping updates on FK violations.

Wrap report_period.save() in schema_context and catch IntegrityError
so that a provider deleted mid-run doesn't surface as an unhandled
exception. Matches the pattern used in ocp_cloud_parquet_summary_updater.

Fixes GlitchTip INSIGHTS-HCCM-STAGE-5HT (issue 4499889).

Co-Authored-By: Claude <noreply@anthropic.com>
@koku-ci-triager-bot koku-ci-triager-bot added the smoke-tests pr_check will run minimal required smokes. Used when changes hit multiple providers. label Aug 12, 2026
@sourcery-ai

sourcery-ai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Wraps report_period.save() with proper tenant schema context and guards against IntegrityError when providers are deleted during cost model processing, preventing noisy GlitchTip errors from a foreign key race condition.

Sequence diagram for report_period.save with schema_context and IntegrityError guard

sequenceDiagram
    participant OcpCostModelCostUpdater
    participant schema_context
    participant ReportPeriod
    participant Database
    participant LOG

    OcpCostModelCostUpdater->>ReportPeriod: distribute_costs_and_update_ui_summary(summary_range)
    OcpCostModelCostUpdater->>ReportPeriod: set derived_cost_datetime
    OcpCostModelCostUpdater->>schema_context: schema_context(_schema)
    activate schema_context
    schema_context->>ReportPeriod: save()
    ReportPeriod->>Database: INSERT/UPDATE report_period
    alt provider_fk_intact
        Database-->>ReportPeriod: success
        ReportPeriod-->>schema_context: return
        schema_context-->>OcpCostModelCostUpdater: continue processing
    else provider_deleted_fk_violation
        Database-->>ReportPeriod: IntegrityError
        ReportPeriod-->>schema_context: raise IntegrityError
        schema_context-->>OcpCostModelCostUpdater: IntegrityError
        OcpCostModelCostUpdater->>LOG: warning(log_json(...))
        OcpCostModelCostUpdater-->>OcpCostModelCostUpdater: return (skip report period update)
    end
    deactivate schema_context
Loading

File-Level Changes

Change Details Files
Guard report period saving with tenant schema context and IntegrityError handling to avoid FK race when providers are deleted mid-processing.
  • Import IntegrityError from django.db.utils to support targeted exception handling.
  • Wrap report_period.save() in a schema_context using the current tenant schema to ensure correct ORM routing.
  • Add try/except around the save call to catch IntegrityError triggered by provider deletion during cost model processing.
  • On IntegrityError, log a structured warning indicating the provider was deleted and skip further report period updates by returning early.
koku/masu/processor/ocp/ocp_cost_model_cost_updater.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions Bot added the smokes-required Label to show that smokes tests should be run against these changes. label Aug 12, 2026
@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 57.14286% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.4%. Comparing base (c6b3086) to head (fafba6f).

❌ Your patch check has failed because the patch coverage (57.1%) is below the target coverage (90.0%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@           Coverage Diff           @@
##            main   #6259     +/-   ##
=======================================
- Coverage   94.4%   94.4%   -0.0%     
=======================================
  Files        369     369             
  Lines      33438   33444      +6     
  Branches    3730    3730             
=======================================
- Hits       31567   31565      -2     
- Misses      1211    1218      +7     
- Partials     660     661      +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

smoke-tests pr_check will run minimal required smokes. Used when changes hit multiple providers. smokes-required Label to show that smokes tests should be run against these changes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant