-
Notifications
You must be signed in to change notification settings - Fork 728
fix: chunk rank_packages() apply to avoid Sequin replication slot growth [CM-1374] #4510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+181
−16
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ad6189e
fix: chunk rank_packages() apply to avoid Sequin replication slot growth
mbani01 39d6688
fix: code review comments
mbani01 e4fabc2
fix: bind statement_timeout to the same connection as rank_packages_c…
mbani01 3b90a20
fix: unreleased lock
mbani01 2944235
Merge branch 'main' into fix/chunk_rank_packages
mbani01 9c44ee5
Merge branch 'main' into fix/chunk_rank_packages
mbani01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
backend/src/osspckgs/migrations/V1787733800__rank_packages_chunked_apply.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| -- rank_packages() applied its ranking to all of `packages` in one UPDATE (9.66M | ||
| -- rows), which under REPLICA IDENTITY FULL blew up the Sequin replication slot. | ||
| -- rank_packages_chunked() stages the same ranking into an UNLOGGED table, then | ||
| -- applies it in committed keyset chunks so the slot advances continuously. | ||
| -- rank_packages() is left in place until the new worker deploys. | ||
|
|
||
| CREATE UNLOGGED TABLE IF NOT EXISTS staging.package_rank ( | ||
| package_id bigint PRIMARY KEY, | ||
| impact numeric(10, 4), | ||
| is_critical bool NOT NULL, | ||
| rank_in_ecosystem int NOT NULL | ||
| ); | ||
|
|
||
| CREATE OR REPLACE PROCEDURE rank_packages_chunked( | ||
| coverage_cutoff numeric DEFAULT 0.90, | ||
| ecosystems text[] DEFAULT NULL, | ||
| chunk_size int DEFAULT 25000, | ||
| INOUT applied_rows int DEFAULT 0 | ||
| ) | ||
| LANGUAGE plpgsql AS $$ | ||
| DECLARE | ||
| effective_ecosystems text[]; | ||
| staged_count int; | ||
| batch_rows int; | ||
| cursor_id bigint := 0; | ||
| BEGIN | ||
| SET LOCAL max_parallel_workers_per_gather = 4; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| IF chunk_size IS NULL OR chunk_size <= 0 THEN | ||
| RAISE EXCEPTION 'rank_packages_chunked: chunk_size must be a positive integer, got %', chunk_size; | ||
| END IF; | ||
|
|
||
| -- Session-level: survives the internal COMMITs below | ||
| IF NOT pg_try_advisory_lock(hashtextextended('rank_packages_chunked', 0)) THEN | ||
| RAISE EXCEPTION 'rank_packages_chunked: another execution is already in progress'; | ||
|
mbani01 marked this conversation as resolved.
|
||
| END IF; | ||
|
mbani01 marked this conversation as resolved.
|
||
|
|
||
| applied_rows := 0; | ||
|
mbani01 marked this conversation as resolved.
|
||
|
|
||
| IF ecosystems IS NULL THEN | ||
| SELECT ARRAY_AGG(DISTINCT ecosystem) | ||
| INTO effective_ecosystems | ||
| FROM packages; | ||
| ELSE | ||
| effective_ecosystems := ecosystems; | ||
| END IF; | ||
|
|
||
| TRUNCATE staging.package_rank; | ||
|
mbani01 marked this conversation as resolved.
mbani01 marked this conversation as resolved.
|
||
|
|
||
| -- Scoring CTE chain, unchanged from rank_packages() (V1783123201). | ||
| INSERT INTO staging.package_rank (package_id, impact, is_critical, rank_in_ecosystem) | ||
| WITH base AS ( | ||
| SELECT | ||
| id, | ||
| ecosystem, | ||
| COALESCE(downloads_last_30d, 0) AS downloads, | ||
| COALESCE(dependent_count, 0) AS direct_dependents, | ||
| COALESCE(transitive_dependent_count, 0) AS transitive_dependents, | ||
| COALESCE(sonatype_popularity_score, 0) AS sonatype_popularity, | ||
| SUM(COALESCE(downloads_last_30d, 0)) OVER (PARTITION BY ecosystem) AS ecosystem_total_downloads, | ||
| SUM(COALESCE(dependent_count, 0)) OVER (PARTITION BY ecosystem) AS ecosystem_total_direct_dependents, | ||
| SUM(COALESCE(transitive_dependent_count, 0)) OVER (PARTITION BY ecosystem) AS ecosystem_total_transitive_dependents, | ||
| SUM(COALESCE(sonatype_popularity_score, 0)) OVER (PARTITION BY ecosystem) AS ecosystem_total_sonatype | ||
| FROM packages | ||
| WHERE ecosystem = ANY(effective_ecosystems) | ||
| ), | ||
| walked AS ( | ||
| SELECT | ||
| id, | ||
| ecosystem, | ||
| SUM(signal_value) OVER coverage_window / ecosystem_signal_total::numeric AS cumulative_share_inclusive, | ||
| (SUM(signal_value) OVER coverage_window - signal_value) / ecosystem_signal_total::numeric AS cumulative_share_exclusive | ||
| FROM base | ||
| CROSS JOIN LATERAL (VALUES | ||
| ('downloads', downloads, ecosystem_total_downloads), | ||
| ('direct_dependents', direct_dependents, ecosystem_total_direct_dependents), | ||
| ('transitive_dependents', transitive_dependents, ecosystem_total_transitive_dependents), | ||
| ('sonatype_popularity', sonatype_popularity, ecosystem_total_sonatype) | ||
| ) AS signal(signal_name, signal_value, ecosystem_signal_total) | ||
| WHERE ecosystem_signal_total > 0 | ||
| WINDOW coverage_window AS ( | ||
| PARTITION BY ecosystem, signal_name | ||
| ORDER BY signal_value DESC, id | ||
| ROWS UNBOUNDED PRECEDING | ||
| ) | ||
| ), | ||
| combined AS ( | ||
| SELECT | ||
| id, | ||
| ecosystem, | ||
| AVG(1.0 - cumulative_share_inclusive)::numeric(10, 4) AS new_impact, | ||
| BOOL_OR(cumulative_share_exclusive < coverage_cutoff) AS new_is_critical | ||
| FROM walked | ||
| GROUP BY id, ecosystem | ||
| ), | ||
| final AS ( | ||
| SELECT | ||
| combined.id, | ||
| combined.new_impact, | ||
| combined.new_is_critical OR (spotlight.package_id IS NOT NULL) AS new_is_critical, | ||
| ROW_NUMBER() OVER ( | ||
| PARTITION BY combined.ecosystem | ||
| ORDER BY combined.new_impact DESC NULLS LAST, combined.id | ||
| ) AS new_rank_in_ecosystem | ||
| FROM combined | ||
| LEFT JOIN package_criticality_spotlight spotlight ON spotlight.package_id = combined.id | ||
| ) | ||
| SELECT id, new_impact, new_is_critical, new_rank_in_ecosystem::int | ||
| FROM final; | ||
|
|
||
| GET DIAGNOSTICS staged_count = ROW_COUNT; | ||
|
|
||
| IF staged_count = 0 THEN | ||
| RAISE EXCEPTION 'rank_packages_chunked: computed 0 rows, refusing to apply an empty ranking'; | ||
| END IF; | ||
|
|
||
| ANALYZE staging.package_rank; | ||
|
|
||
| COMMIT; | ||
|
|
||
| LOOP | ||
| WITH batch AS ( | ||
| SELECT package_id, impact, is_critical, rank_in_ecosystem | ||
| FROM staging.package_rank | ||
| WHERE package_id > cursor_id | ||
| ORDER BY package_id | ||
| LIMIT chunk_size | ||
| ), | ||
| updated AS ( | ||
| UPDATE packages p | ||
| SET impact = b.impact, | ||
| is_critical = b.is_critical, | ||
| rank_in_ecosystem = b.rank_in_ecosystem, | ||
| last_rank_pass_at = NOW(), | ||
| last_synced_at = NOW() | ||
| FROM batch b | ||
| WHERE p.id = b.package_id | ||
| RETURNING p.id | ||
| ) | ||
| SELECT COUNT(*), COALESCE(MAX(b.package_id), cursor_id) | ||
| INTO batch_rows, cursor_id | ||
| FROM batch b; | ||
|
|
||
| applied_rows := applied_rows + batch_rows; | ||
|
|
||
| COMMIT; | ||
|
|
||
| EXIT WHEN batch_rows < chunk_size; | ||
| END LOOP; | ||
|
|
||
| PERFORM pg_advisory_unlock(hashtextextended('rank_packages_chunked', 0)); | ||
| END; | ||
| $$; | ||
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.