-
Notifications
You must be signed in to change notification settings - Fork 42
Trim recents table via table swap and add cleanup worker cronjob #4613
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
Open
zwolf
wants to merge
12
commits into
master
Choose a base branch
from
recents-refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+374
−6
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
718ee67
Recents table swap
zwolf e183999
Recents cleanup worker & spec
zwolf 74c7827
newlines
zwolf f3638e5
FSL
zwolf 9c9c0d6
FSL
zwolf fc2d8f8
Hound cleanup
zwolf 08b5172
woof
zwolf 57b9192
Keep recents per user per project, increase keep time to 90 days
zwolf e270d9e
Update rubocop rails version, loosen rspec guidelines
zwolf 39662f9
Hound
zwolf ef2682e
Fix cutoff date in migration
zwolf 550c0ac
Merge branch 'master' into recents-refactor
zwolf 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class RecentsCleanupWorker | ||
| include Sidekiq::Worker | ||
|
|
||
| sidekiq_options queue: :default, | ||
| retry: 0, | ||
| congestion: { | ||
| interval: 1.hour, | ||
| max_in_interval: 1, | ||
| reject_with: :cancel | ||
| } | ||
|
|
||
| def perform | ||
| # Delete all older than 90 days | ||
| Recent.where('created_at < ?', 90.days.ago).in_batches(of: 5000).delete_all | ||
|
|
||
| # Identify users active in the past 2 hours | ||
| recently_active_pairs = Recent.where('created_at > ?', 2.hours.ago) | ||
| .distinct | ||
| .pluck(:user_id, :project_id) | ||
|
|
||
| # Clean up any recents over 20 per user/project for recently active users | ||
| recently_active_pairs.each do |user_id, project_id| | ||
| scope = Recent.where(user_id: user_id, project_id: project_id) | ||
|
|
||
| next unless scope.count > 20 | ||
|
|
||
| ids_to_keep = scope.order(created_at: :desc) | ||
| .limit(20) | ||
| .pluck(:id) | ||
|
|
||
| scope.where.not(id: ids_to_keep).delete_all | ||
| end | ||
| end | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class DeleteOldRecents < ActiveRecord::Migration[7.2] | ||
| disable_ddl_transaction! | ||
|
|
||
| def up | ||
|
zwolf marked this conversation as resolved.
|
||
| safety_assured do | ||
|
zwolf marked this conversation as resolved.
|
||
| cutoff_date = 90.days.ago.to_fs(:db) | ||
| current_time = Time.current.to_fs(:db) | ||
|
|
||
| say 'Step 1: Creating new table from existing and loading recent recents...' | ||
|
|
||
| execute <<-SQL | ||
|
zwolf marked this conversation as resolved.
|
||
| CREATE TABLE recents_new (LIKE recents INCLUDING DEFAULTS INCLUDING CONSTRAINTS); | ||
|
|
||
| INSERT INTO recents_new | ||
| SELECT * FROM recents | ||
| WHERE created_at >= '#{cutoff_date}' | ||
| AND created_at < '#{current_time}'; | ||
| SQL | ||
|
|
||
| say 'Step 2: Building indexes and FKs on new table with temporary names...' | ||
|
|
||
| execute 'ALTER TABLE recents_new ADD PRIMARY KEY (id);' | ||
|
|
||
| execute 'CREATE INDEX index_recents_new_on_workflow_id ON recents_new (workflow_id);' | ||
| execute 'CREATE INDEX index_recents_new_on_project_id ON recents_new (project_id);' | ||
| execute 'CREATE INDEX index_recents_new_on_user_id ON recents_new (user_id);' | ||
| execute 'CREATE INDEX index_recents_new_on_subject_id ON recents_new (subject_id);' | ||
| execute 'CREATE INDEX index_recents_new_on_created_at ON recents_new (created_at);' | ||
|
|
||
| # New compound index for user/project/created_at lookups | ||
| execute 'CREATE INDEX index_recents_on_user_project_and_created ON recents_new (user_id, project_id, created_at DESC);' | ||
|
|
||
| execute <<-SQL | ||
|
zwolf marked this conversation as resolved.
|
||
| ALTER TABLE recents_new | ||
| ADD CONSTRAINT fk_recents_classifications | ||
| FOREIGN KEY (classification_id) REFERENCES classifications(id); | ||
|
|
||
| ALTER TABLE recents_new | ||
| ADD CONSTRAINT fk_recents_subjects | ||
| FOREIGN KEY (subject_id) REFERENCES subjects(id); | ||
| SQL | ||
|
|
||
| say 'Step 3: Executing the table swap...' | ||
|
|
||
| execute <<-SQL | ||
|
zwolf marked this conversation as resolved.
|
||
| BEGIN; | ||
|
|
||
| -- Lock table prevent incoming writes | ||
| LOCK TABLE recents IN ACCESS EXCLUSIVE MODE; | ||
|
|
||
| -- Catch up any records created during above operations | ||
| INSERT INTO recents_new | ||
| SELECT * FROM recents | ||
| WHERE created_at >= '#{current_time}'; | ||
|
|
||
| -- Swap the tables | ||
| ALTER TABLE recents RENAME TO recents_old; | ||
| ALTER TABLE recents_new RENAME TO recents; | ||
|
|
||
| -- Clean up index names so structure.sql looks untouched | ||
| ALTER INDEX recents_pkey RENAME TO recents_old_pkey; | ||
| ALTER INDEX recents_new_pkey RENAME TO recents_pkey; | ||
|
|
||
| ALTER INDEX index_recents_on_workflow_id RENAME TO index_recents_old_on_workflow_id; | ||
| ALTER INDEX index_recents_new_on_workflow_id RENAME TO index_recents_on_workflow_id; | ||
|
|
||
| ALTER INDEX index_recents_on_project_id RENAME TO index_recents_old_on_project_id; | ||
| ALTER INDEX index_recents_new_on_project_id RENAME TO index_recents_on_project_id; | ||
|
|
||
| ALTER INDEX index_recents_on_user_id RENAME TO index_recents_old_on_user_id; | ||
| ALTER INDEX index_recents_new_on_user_id RENAME TO index_recents_on_user_id; | ||
|
|
||
| ALTER INDEX index_recents_on_subject_id RENAME TO index_recents_old_on_subject_id; | ||
| ALTER INDEX index_recents_new_on_subject_id RENAME TO index_recents_on_subject_id; | ||
|
|
||
| ALTER INDEX index_recents_on_created_at RENAME TO index_recents_old_on_created_at; | ||
| ALTER INDEX index_recents_new_on_created_at RENAME TO index_recents_on_created_at; | ||
|
|
||
| -- Transfer sequence ownership | ||
| ALTER SEQUENCE recents_id_seq OWNED BY recents.id; | ||
|
|
||
| COMMIT; | ||
| SQL | ||
|
|
||
| say 'Step 4: Updating database statistics for the new table...' | ||
| execute 'ANALYZE recents;' | ||
|
|
||
| say 'Recents swap complete.' | ||
| end | ||
| end | ||
|
|
||
| def down | ||
|
zwolf marked this conversation as resolved.
|
||
| safety_assured do | ||
| execute <<-SQL | ||
|
zwolf marked this conversation as resolved.
|
||
| BEGIN; | ||
| LOCK TABLE recents IN ACCESS EXCLUSIVE MODE; | ||
|
|
||
| -- Swap tables back | ||
| ALTER TABLE recents RENAME TO recents_new; | ||
| ALTER TABLE recents_old RENAME TO recents; | ||
|
|
||
| -- Revert Sequence | ||
| ALTER SEQUENCE recents_id_seq OWNED BY recents.id; | ||
|
|
||
| -- Revert index names to original state | ||
| ALTER INDEX recents_pkey RENAME TO recents_new_pkey; | ||
| ALTER INDEX recents_old_pkey RENAME TO recents_pkey; | ||
|
|
||
| ALTER INDEX index_recents_on_workflow_id RENAME TO index_recents_new_on_workflow_id; | ||
| ALTER INDEX index_recents_old_on_workflow_id RENAME TO index_recents_on_workflow_id; | ||
|
|
||
| ALTER INDEX index_recents_on_project_id RENAME TO index_recents_new_on_project_id; | ||
| ALTER INDEX index_recents_old_on_project_id RENAME TO index_recents_on_project_id; | ||
|
|
||
| ALTER INDEX index_recents_on_user_id RENAME TO index_recents_new_on_user_id; | ||
| ALTER INDEX index_recents_old_on_user_id RENAME TO index_recents_on_user_id; | ||
|
|
||
| ALTER INDEX index_recents_on_subject_id RENAME TO index_recents_new_on_subject_id; | ||
| ALTER INDEX index_recents_old_on_subject_id RENAME TO index_recents_on_subject_id; | ||
|
|
||
| ALTER INDEX index_recents_on_created_at RENAME TO index_recents_new_on_created_at; | ||
| ALTER INDEX index_recents_old_on_created_at RENAME TO index_recents_on_created_at; | ||
|
|
||
| COMMIT; | ||
| SQL | ||
|
|
||
| execute 'DROP TABLE recents_new;' | ||
| end | ||
| end | ||
| 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
Oops, something went wrong.
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.