Move after_commit hook from AntiAbuse::TrustScore to a worker
What does this MR do and why?
Related to https://gitlab.com/gitlab-org/modelops/anti-abuse/team-tasks/-/issues/849
Move after_commit hook from AntiAbuse::TrustScore
to a worker
To avoid deadlock conditions when many trust scores are created for
a single user, the after_commit hook in AntiAbuse::TrustScore
is moved
to a sidekiq job that will only execute the cleanup once every 5 minutes
for a given user.
MR acceptance checklist
Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Screenshots or screen recordings
Screenshots are required for UI changes, and strongly recommended for all other merge requests.
Before | After |
---|---|
How to set up and validate locally
Numbered steps to set up and validate the change are strongly suggested.
- Apply a patch to lower the trust score limit and cache timeout for easier testing.
diff --git a/app/models/anti_abuse/user_trust_score.rb b/app/models/anti_abuse/user_trust_score.rb index a2add9d4c9e3..9bd4973f06bc 100644 --- a/app/models/anti_abuse/user_trust_score.rb +++ b/app/models/anti_abuse/user_trust_score.rb @@ -2,7 +2,7 @@ module AntiAbuse class UserTrustScore - MAX_EVENTS = 100 + MAX_EVENTS = 3 SPAMCHECK_HAM_THRESHOLD = 0.5 def initialize(user) diff --git a/app/workers/anti_abuse/trust_score_cleanup_worker.rb b/app/workers/anti_abuse/trust_score_cleanup_worker.rb index 77b1289fae63..4a30b5441902 100644 --- a/app/workers/anti_abuse/trust_score_cleanup_worker.rb +++ b/app/workers/anti_abuse/trust_score_cleanup_worker.rb @@ -18,7 +18,7 @@ def perform(user_id, source) return if Rails.cache.exist?(cache_key) AntiAbuse::UserTrustScore.new(user).remove_old_scores(source) - Rails.cache.write(cache_key, true, expires_in: 5.minutes) + Rails.cache.write(cache_key, true, expires_in: 1.minutes) end end end
- Tail sidekiq logs to observe that the workers are executing.
tail -f log/sidekiq.log | grep -i trustscore
- Execute the
Abuse::TrustScoreWorker
4 times. You should see theAbuse::TrustScoreWorker
and theAntiAbuse::TrustScoreCleanupWorker
being queued.user = User.last Abuse::TrustScoreWorker.perform_async(user.id, Enums::Abuse::Source.sources[:spamcheck], rand(0.0...1.0)) Abuse::TrustScoreWorker.perform_async(user.id, Enums::Abuse::Source.sources[:spamcheck], rand(0.0...1.0)) Abuse::TrustScoreWorker.perform_async(user.id, Enums::Abuse::Source.sources[:spamcheck], rand(0.0...1.0)) Abuse::TrustScoreWorker.perform_async(user.id, Enums::Abuse::Source.sources[:spamcheck], rand(0.0...1.0))
- Check the count of abuse trust score records for the user. There should be 4 records since the cleanup worker was executed before the limit was exceeded.
user.abuse_trust_scores.where(source: Enums::Abuse::Source.sources[:spamcheck]).count
- Wait for 1 minute and execute the
Abuse::TrustScoreWorker
once more. At this point, the cache should have expired and the count of trust score records should be reduced to 3.
Edited by Ian Anderson