feat(import): add --minimize-reads for type-scoped state loading#559
Open
riyazsh wants to merge 1 commit into
Open
feat(import): add --minimize-reads for type-scoped state loading#559riyazsh wants to merge 1 commit into
riyazsh wants to merge 1 commit into
Conversation
Contributor
Author
Self-review notesPosting these before external review so reviewers can focus on what I missed. ReachabilityValidation at
No dead branches added. TestsNo new fakes/mocks. Follow-ups (not in this PR)
Tightened in-PR
|
Mirrors the --minimize-reads option from sync onto import. The flag, validation, and loading-strategy code already live in shared code (utils/configuration.py:412-450, utils/state.py:88-90) — only the click decorator needed adding to the import subcommand. When enabled, import scopes the bucket state-load to the requested resource types (type-scoped) or to filter-extracted exact IDs (ID-targeted), instead of scanning every file under both source and destination prefixes. For populated buckets where state-load dominates per-run wall-clock, this is a significant speedup. Constraints (unchanged, enforced in configuration.py:412-417): - requires --resource-per-file - requires --resources The "must not be combined with --cleanup" rule on sync's flag is enforced by sync's validation path because sync accepts --cleanup. The import command does not register --cleanup at all (it lives in _diffs_options, applied only to sync/diffs), so click rejects "import --cleanup=..." at parse time, before configuration validation runs. A behavioural test pins this invariant down. Tests: - flipped pre-existing test_minimize_reads_not_available_on_import_command to positive test_minimize_reads_accepted_on_import_command (now in --help). - added test_import_minimize_reads_requires_resource_per_file and test_import_minimize_reads_requires_resources_flag, mirroring the sync-side validation tests. - added test_import_does_not_accept_cleanup_option that asserts on the Error: line specifically, so a future click message reword doesn't silently mask a regression that accepts --cleanup on import. - removed the now-stale "Only available on the sync command." line from sync's --minimize-reads help text. - import's --minimize-reads help omits the --cleanup caveat (since --cleanup isn't an import flag, mentioning it would be misleading). No behavioural change for the sync command. No new shared options.
36b6ec3 to
06d0f17
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR adds
This PR adds
--minimize-readsto thedatadog-sync importsubcommand. The option, its validation, and the underlying loading-strategy code already exist (introduced forsyncin #544 / #545) — only the click decorator was missing on import.When enabled, import scopes the bucket state-load to the requested resource types (type-scoped) or to filter-extracted exact IDs (ID-targeted), instead of scanning every file under both source and destination prefixes.
The problem this solves
State.__init__callsload_state(Origin.ALL), which reads every file under both prefixes (utils/state.py:77,utils/storage/gcs_bucket.py:56-84). On reasonably-populated buckets the state-load dominates per-run wall-clock regardless of how many records the run actually fetches — the boot-time read of prior runs' files becomes the bottleneck.Observed shape on a ~22k-file source bucket, importing one resource type at a time:
Three records, 41 minutes — nearly all of it the unscoped bucket scan.
--minimize-readsfixes this forsyncalready; this PR makes the same fix reachable fromimport, which is the entry point most automation uses for incremental refreshes.How to enable
Constraints enforced in
utils/configuration.py:412-417(unchanged in this PR):--resource-per-file--resources--cleanupis not registered on the import command (it lives in_diffs_options, applied only tosync/diffs), so the--minimize-reads + --cleanuprule cannot be violated through the import CLI. Click rejectsimport --cleanup=...at parse time. Locked in bytest_import_does_not_accept_cleanup_option.What does not change
synccommand behaviourutils/configuration.pyvalidation logicStateconstructor or storage layerdefault=False)Code summary
commands/_import.py: add--minimize-reads@option(...)decorator; importoptionandCustomOptionClass.commands/sync.py: drop now-stale"Only available on the sync command."line from help text.tests/unit/test_minimize_reads_type_scoped.py: flip the pre-existing negative test that asserted import rejected the flag; add positive--helpcheck, two validation-mirror tests, and a click-level guard for--cleanupon import.Test plan
pytest tests/unit/— 500 passed--minimize-readslisted indatadog-sync import --helpimport --minimize-readswithout--resource-per-file→ UsageErrorimport --minimize-readswithout--resources→ UsageErrorimport --minimize-reads --cleanup=Force→ click "no such option" (parse-time rejection)sync --helpstill renders correctly after help-text editRollout
Flag is opt-in (
default=False). Reverting the commit removes the option fromimport --helpbut doesn't break any caller that wasn't using it. No on-disk or wire-format effect.Out of scope
migratesubcommand still lacks--minimize-reads. Adding it requires confirming that the apply phase's existing minimize-reads handling covers the migrate flow. Follow-up._list_and_load(the per-type file walk is still sequential). Separate effort.