Skip to content

[AAP-82119] Batch resource migration using bulk-update endpoint - #167

Merged
Dostonbek1 merged 1 commit into
ansible:develfrom
Dostonbek1:AAP-82119/batch-resource-migration
Jul 22, 2026
Merged

[AAP-82119] Batch resource migration using bulk-update endpoint#167
Dostonbek1 merged 1 commit into
ansible:develfrom
Dostonbek1:AAP-82119/batch-resource-migration

Conversation

@Dostonbek1

@Dostonbek1 Dostonbek1 commented Jul 20, 2026

Copy link
Copy Markdown
Member

Requires: ansible/django-ansible-base#1054

Description

  • What: Refactor migrate_resource() to process entire API pages as batches instead of one item at a time, using the new DAB bulk-update endpoint.
  • Why: Each resource triggers an individual HTTP PATCH to the upstream controller. For 3,962 users this means 3,962 sequential round-trips taking 203s (3.4 min). Role assignments (119,721 items) already use bulk optimization and take only 51s.
  • How: New _process_resource_page_batch() method validates all items on the page, reconciles conflicts, bulk-creates gateway resources in a single transaction, then sends one bulk_update_resources() HTTP call per page.

Type of Change

  • New feature (non-breaking change which adds functionality)

Self-Review Checklist

  • I have performed a self-review of my code
  • I have added relevant comments to complex code sections
  • I have updated documentation where needed
  • I have considered the security impact of these changes
  • I have considered performance implications
  • I have thought about error handling and edge cases
  • I have tested the changes in my local environment

Testing Instructions

Prerequisites

Steps to Test

  1. Deploy with both PRs merged
  2. Run manage.py migrate_service_data --username admin
  3. Verify resource migration (users, orgs, teams) completes successfully
  4. At Kyndryl scale (~4k users, ~2k teams, ~130 orgs): total resource migration should complete under 2 minutes

Expected Results

  • Resources migrate correctly (same behavior as before)
  • HTTP round-trips reduced from N (one per resource) to N/page_size (one per page)
  • Transaction rollback on bulk-update failure (no partial state)
  • Progress logging still reports per-page progress

Additional Context

Required Actions

Performance Impact

Metric Before After
HTTP calls (3,962 users) 3,962 ~80 (page_size=50)
HTTP calls (2,288 teams) 2,288 ~46
Projected user migration time 203s <10s
Projected total resource migration ~7 min <2 min

Note: This PR was developed with assistance from Claude AI assistant.

Summary by CodeRabbit

  • New Features

    • Added bulk upstream resource updates during migration to reduce per-item API calls.
    • Improved migration flow with page/batch processing, configurable chunking, transient-error retries, and reconciliation-aware payloads.
    • Added stall detection to abort runs after repeated zero progress.
    • Added warnings when some upstream items fail to update while still progressing.
  • Bug Fixes

    • Updated upstream migration payloads to use the new_service_id field name.
  • Tests

    • Expanded unit coverage for batch processing, bulk update retries/error handling, payload generation, and stalled-migration circuit breakers.

@Dostonbek1
Dostonbek1 requested a review from a team as a code owner July 20, 2026 14:06
@Dostonbek1
Dostonbek1 requested review from jeffh-oss and tznamena and removed request for a team July 20, 2026 14:06
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Dostonbek1, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 17 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: 48a6acdc-143d-4679-bdd5-af9aeef6f485

📥 Commits

Reviewing files that changed from the base of the PR and between d59dfe9 and 8fd8773.

📒 Files selected for processing (3)
  • aap_gateway_api/management/commands/_migrate_service_data/resource_migration.py
  • aap_gateway_api/tests/management/commands/_migrate_service_data/test_resource_migration.py
  • aap_gateway_api/utils/resources_client.py
📝 Walkthrough

Walkthrough

Resource migration now processes upstream resources in batches, creates local Gateway resources transactionally, retries transient bulk-update failures, and tracks progress to detect stalled migrations. Tests cover payloads, retries, partial updates, and circuit breaking.

Changes

Resource migration batching and contracts

Layer / File(s) Summary
Bulk update client and payload contract
aap_gateway_api/management/commands/_migrate_service_data/resource_migration.py, aap_gateway_api/utils/resources_client.py, aap_gateway_api/tests/.../test_resource_migration.py
Adds the bulk-update client endpoint and changes migration payloads to use new_service_id.
Page batch processing and retry handling
aap_gateway_api/management/commands/_migrate_service_data/resource_migration.py, aap_gateway_api/tests/.../test_resource_migration.py
Validates pages, creates local resources transactionally, sends chunked bulk updates with transient retries, and logs per-item failures.
Migration progress and stall detection
aap_gateway_api/management/commands/_migrate_service_data/resource_migration.py, aap_gateway_api/tests/.../test_resource_migration.py
Advances progress by successful updates, logs partial batches, and raises after consecutive zero-progress pages.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ResourceMigration
  participant GatewayDatabase
  participant GWResourceAPIClient
  participant UpstreamService
  ResourceMigration->>GatewayDatabase: Create local resources for page
  ResourceMigration->>GWResourceAPIClient: Send chunked bulk update items
  GWResourceAPIClient->>UpstreamService: POST resources/bulk-update/
  UpstreamService-->>GWResourceAPIClient: Return update results or transient failure
  GWResourceAPIClient-->>ResourceMigration: Return successful update count
  ResourceMigration->>ResourceMigration: Detect zero-progress pages
Loading

Suggested reviewers: john-westcott-iv, jay-steurer, tznamena, bhavenst

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: migrating resources in batches via the bulk-update endpoint.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Dostonbek1
Dostonbek1 marked this pull request as draft July 20, 2026 14:16
@github-actions github-actions Bot added the WIP Work in progress label Jul 20, 2026
@Dostonbek1 Dostonbek1 changed the title [AAP-82119] Batch resource migration using bulk-update endpoint [WIP][AAP-82119] Batch resource migration using bulk-update endpoint Jul 20, 2026
@Dostonbek1
Dostonbek1 force-pushed the AAP-82119/batch-resource-migration branch from 1b15bba to 4260a1d Compare July 20, 2026 14:22
@codecov

codecov Bot commented Jul 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.15385% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.40%. Comparing base (4ff587d) to head (8fd8773).

Files with missing lines Patch % Lines
...mmands/_migrate_service_data/resource_migration.py 96.07% 2 Missing and 2 partials ⚠️
@@            Coverage Diff             @@
##            devel     #167      +/-   ##
==========================================
+ Coverage   94.39%   94.40%   +0.01%     
==========================================
  Files         138      138              
  Lines        6404     6487      +83     
  Branches      792      812      +20     
==========================================
+ Hits         6045     6124      +79     
- Misses        265      267       +2     
- Partials       94       96       +2     
Flag Coverage Δ
unit-tests 94.40% <96.15%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
aap_gateway_api/utils/resources_client.py 86.44% <100.00%> (+0.23%) ⬆️
...mmands/_migrate_service_data/resource_migration.py 97.32% <96.07%> (-1.74%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Dostonbek1
Dostonbek1 force-pushed the AAP-82119/batch-resource-migration branch 2 times, most recently from 8f04a0d to 26f6673 Compare July 20, 2026 15:08
@aap-pde-ci-bot

Copy link
Copy Markdown

✅ Test Results - PASSED

Summary

Metric Count
Total Tests 163
✅ Passed 160
❌ Failed 0
⚠️ Errors 0
⏭️ Skipped 3
⏱️ Duration 367.22s

Pass Rate: 98.2%

@github-actions github-actions Bot added fix-ci CI is failing — fix before review blocked needs something else to complete first before this can be merged labels Jul 20, 2026
@Dostonbek1
Dostonbek1 force-pushed the AAP-82119/batch-resource-migration branch from 26f6673 to 42972a0 Compare July 20, 2026 19:27
@github-actions github-actions Bot removed fix-ci CI is failing — fix before review blocked needs something else to complete first before this can be merged labels Jul 20, 2026
@Dostonbek1
Dostonbek1 force-pushed the AAP-82119/batch-resource-migration branch 2 times, most recently from 1ea1d9c to 1768848 Compare July 20, 2026 20:45
@aap-pde-ci-bot

Copy link
Copy Markdown

✅ Test Results - PASSED

Summary

Metric Count
Total Tests 167
✅ Passed 162
❌ Failed 0
⚠️ Errors 0
⏭️ Skipped 5
⏱️ Duration 351.45s

Pass Rate: 97.0%

@github-actions github-actions Bot added fix-ci CI is failing — fix before review blocked needs something else to complete first before this can be merged labels Jul 20, 2026
@Dostonbek1

Dostonbek1 commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

Testing Results: Production-Scale End-to-End Migration Benchmark

Tested against a local AAP dev instance (AAP 2.7, controller 4.8.3) with production-scale data:

  • 3,962 users
  • 2,288 teams
  • 130 organizations
  • 57 role definitions
  • 6,428 total resources

Performance Results (Controller Service-Index Endpoint)

Benchmark simulates the exact path migrate_service_data takes: Gateway calls the controller's POST /service-index/resources/bulk-update/ with batches of 50 items per page.

Approach Time Rate Speedup
Batch processing (this PR) 9.2s 700 items/sec 52x
Per-item PATCH (old approach) 481.2s (8.0 min) 13.4 items/sec 1x (baseline)

Comparison with Role Assignment Optimization (AAP-80609)

Optimization Items Time Speedup
Role assignments (AAP-80609) 119,721 51s 33x
Resource migration (this PR) 6,428 9.2s 52x

Platform Services Test Suite

Ran make test-atf-run-ps against the updated instance:

166 passed, 2 failed, 1 skipped, 22 deselected, 7 warnings

The 2 failures are pre-existing and unrelated (service_cluster PATCH proxy permissions).

Acceptance Criteria

Resource migration at production-scale should complete in under 2 minutes.

Result: 9.2 seconds — well under target. ✅

The full migrate_service_data command now completes resource migration 52x faster by batching page-level updates through the DAB bulk-update endpoint (PR ansible/django-ansible-base#1054).

@Dostonbek1 Dostonbek1 changed the title [WIP][AAP-82119] Batch resource migration using bulk-update endpoint [AAP-82119] Batch resource migration using bulk-update endpoint Jul 21, 2026
@github-actions github-actions Bot added fix-ci CI is failing — fix before review blocked needs something else to complete first before this can be merged labels Jul 21, 2026
@Dostonbek1
Dostonbek1 force-pushed the AAP-82119/batch-resource-migration branch from 717e871 to d59dfe9 Compare July 22, 2026 13:37
@github-actions github-actions Bot removed fix-ci CI is failing — fix before review blocked needs something else to complete first before this can be merged labels Jul 22, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
aap_gateway_api/utils/resources_client.py (1)

39-43: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Docstring lists service_id, but the codebase uses new_service_id.

The docstring says items may contain service_id, but _build_bulk_update_item (and its tests) build/expect new_service_id. Update the docstring to match the actual contract.

Proposed docstring fix
         Each item must contain 'ansible_id' and one or more fields to update:
-        service_id, new_ansible_id, is_partially_migrated, resource_data.
+        new_service_id, new_ansible_id, is_partially_migrated, resource_data.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@aap_gateway_api/utils/resources_client.py` around lines 39 - 43, Update the
bulk-update method’s docstring to list new_service_id instead of service_id,
matching the field constructed and expected by _build_bulk_update_item and its
tests; leave the other documented fields unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@aap_gateway_api/utils/resources_client.py`:
- Around line 39-43: Update the bulk-update method’s docstring to list
new_service_id instead of service_id, matching the field constructed and
expected by _build_bulk_update_item and its tests; leave the other documented
fields unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: 72175620-905e-461a-9c5c-22c577e840ca

📥 Commits

Reviewing files that changed from the base of the PR and between 1a1feb3 and d59dfe9.

📒 Files selected for processing (3)
  • aap_gateway_api/management/commands/_migrate_service_data/resource_migration.py
  • aap_gateway_api/tests/management/commands/_migrate_service_data/test_resource_migration.py
  • aap_gateway_api/utils/resources_client.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • aap_gateway_api/management/commands/_migrate_service_data/resource_migration.py

@Dostonbek1
Dostonbek1 force-pushed the AAP-82119/batch-resource-migration branch 2 times, most recently from 798bf03 to a23bdb3 Compare July 22, 2026 14:25
@github-actions github-actions Bot added the Ready-for-review PR is ready for review label Jul 22, 2026
@Dostonbek1 Dostonbek1 closed this Jul 22, 2026
@Dostonbek1 Dostonbek1 reopened this Jul 22, 2026
@github-actions github-actions Bot added Review-provided Review has been submitted and removed Ready-for-review PR is ready for review labels Jul 22, 2026
@aap-pde-ci-bot

Copy link
Copy Markdown

✅ Test Results - PASSED

Summary

Metric Count
Total Tests 167
✅ Passed 162
❌ Failed 0
⚠️ Errors 0
⏭️ Skipped 5
⏱️ Duration 269.89s

Pass Rate: 97.0%

@github-actions github-actions Bot added fix-ci CI is failing — fix before review blocked needs something else to complete first before this can be merged labels Jul 22, 2026
Refactor migrate_resource() to process entire API pages as batches
instead of one item at a time. The new _process_resource_page_batch()
method:

1. Validates all items on the page (reuses existing helpers)
2. Reconciles against existing gateway resources
3. Bulk-creates gateway resources in a single transaction
4. Sends a single bulk_update_resources() HTTP call per page

Key improvements:
- Chunk bulk_update_items to MAX_BULK_CHUNK_SIZE (1000) to respect
  upstream endpoint limits
- Add retry with exponential backoff for transient errors (502/503/504,
  network errors) — up to 3 attempts before giving up
- Fail immediately on permanent errors (4xx) without retry
- Treat non-JSON responses as transient (likely proxy errors) and retry
- Circuit breaker: abort after 3 consecutive pages with zero progress
- Align with DAB API rename of service_id → new_service_id
- Fix UUID serialization in bulk-update payload (str() conversion)

This reduces HTTP round-trips from N (one per resource) to N/page_size
(one per page). For Kyndryl-scale data (9,881 resources), this cuts
total migration time from 11m38s to 2m40s — a 4.4x speedup / 77%
time reduction.

Depends on: django-ansible-base AAP-82119/bulk-update-endpoint branch

Assisted-by: Claude AI assistant
@Dostonbek1
Dostonbek1 force-pushed the AAP-82119/batch-resource-migration branch from a23bdb3 to 8fd8773 Compare July 22, 2026 19:32
@github-actions github-actions Bot removed fix-ci CI is failing — fix before review blocked needs something else to complete first before this can be merged labels Jul 22, 2026
@github-actions

Copy link
Copy Markdown

DVCS PR Check Results:

PR appears valid (JIRA key(s) found)

@sonarqubecloud

Copy link
Copy Markdown

@Dostonbek1
Dostonbek1 enabled auto-merge (squash) July 22, 2026 20:17
@aap-pde-ci-bot

Copy link
Copy Markdown

✅ Test Results - PASSED

Summary

Metric Count
Total Tests 167
✅ Passed 162
❌ Failed 0
⚠️ Errors 0
⏭️ Skipped 5
⏱️ Duration 513.72s

Pass Rate: 97.0%

@github-actions github-actions Bot added the ready-to-merge Reviewed, approved, CI checks passing label Jul 22, 2026
@Dostonbek1
Dostonbek1 merged commit 68edeef into ansible:devel Jul 22, 2026
24 checks passed
@github-actions github-actions Bot removed Review-provided Review has been submitted ready-to-merge Reviewed, approved, CI checks passing labels Jul 22, 2026
@Dostonbek1
Dostonbek1 deleted the AAP-82119/batch-resource-migration branch July 23, 2026 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants