From 337dc6e0add164baa70bea690621fe23191d08a9 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 9 Nov 2020 16:00:23 +0100 Subject: [PATCH] Fix updating account counters when account_stat is not yet created (#15108) --- app/models/account_stat.rb | 25 +++++++++++++++---------- spec/models/account_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/app/models/account_stat.rb b/app/models/account_stat.rb index c84e4217c..e70b54d79 100644 --- a/app/models/account_stat.rb +++ b/app/models/account_stat.rb @@ -21,26 +21,26 @@ class AccountStat < ApplicationRecord def increment_count!(key) update(attributes_for_increment(key)) - rescue ActiveRecord::StaleObjectError + rescue ActiveRecord::StaleObjectError, ActiveRecord::RecordNotUnique begin reload_with_id rescue ActiveRecord::RecordNotFound - # Nothing to do - else - retry + return end + + retry end def decrement_count!(key) - update(key => [public_send(key) - 1, 0].max) - rescue ActiveRecord::StaleObjectError + update(attributes_for_decrement(key)) + rescue ActiveRecord::StaleObjectError, ActiveRecord::RecordNotUnique begin reload_with_id rescue ActiveRecord::RecordNotFound - # Nothing to do - else - retry + return end + + retry end private @@ -51,8 +51,13 @@ class AccountStat < ApplicationRecord attrs end + def attributes_for_decrement(key) + attrs = { key => [public_send(key) - 1, 0].max } + attrs + end + def reload_with_id - self.id = find_by!(account: account).id if new_record? + self.id = self.class.find_by!(account: account).id if new_record? reload end end diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 98d29e6f3..75f628076 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -817,4 +817,27 @@ RSpec.describe Account, type: :model do include_examples 'AccountAvatar', :account include_examples 'AccountHeader', :account + + describe '#increment_count!' do + subject { Fabricate(:account) } + + it 'increments the count in multi-threaded an environment when account_stat is not yet initialized' do + subject + + increment_by = 15 + wait_for_start = true + + threads = Array.new(increment_by) do + Thread.new do + true while wait_for_start + Account.find(subject.id).increment_count!(:followers_count) + end + end + + wait_for_start = false + threads.each(&:join) + + expect(subject.reload.followers_count).to eq 15 + end + end end