perf(#11088): paginate _users queries in purging getRoles()#11089
Open
YASHSHARMAOFFICIALLY wants to merge 2 commits into
Open
perf(#11088): paginate _users queries in purging getRoles()#11089YASHSHARMAOFFICIALLY wants to merge 2 commits into
YASHSHARMAOFFICIALLY wants to merge 2 commits into
Conversation
getRoles() previously loaded the entire _users database into memory
with a single allDocs({ include_docs: true }) call. In deployments
with thousands of community health workers this creates unnecessary
memory pressure during each purging cycle.
This change paginates the query in batches of 1000 documents,
consistent with how the rest of the purging module handles large
datasets.
… in getRoles Extract fetchUsersBatch and getOfflineRoles helpers from getRoles to reduce nesting and cognitive complexity flagged by SonarCloud.
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.
Summary
getRoles()insentinel/src/lib/purging.jsto process the_usersdatabase in batches of 1000 documents instead of loading every user document into memory at once.Fixes #11088
Context
getRoles()callsdb.users.allDocs({ include_docs: true })without alimit, which loads the entire_usersdatabase in a single allocation. In deployments with thousands of community health workers, this creates a significant memory spike during each purging cycle. The rest of the purging module carefully paginates large data operations (e.g.,batchedContactsPurge,purgeUnallocatedRecords), andgetRoles()was the remaining outlier.Changes
sentinel/src/lib/purging.jsUSERS_BATCH_SIZE = 1000constant.getRoles()to iterate through_usersin paginated batches usingstartkeyandskipfor cursor progression.sentinel/tests/unit/lib/purging.spec.jslimitparameter.should paginate through users in batchesthat verifies multi-batch iteration, correct query parameters per batch, and correct role deduplication across batches.Test plan
getRolestests pass with updated assertions.