FEATURE: Reintroduce better thread reply counter cache (#21197)

This was reverted in 38cebd3ed5.
The issue was that I was using Discourse.redis.delete_prefixed
which does a slow redis KEYS lookup, which is not advised in
production. This commit removes that, and also ensures the periodical
thread count update only happens if threading is enabled.

I changed to use a redis INCR/DECR for reply count
cache. This avoids a round trip to redis to GET the current
count, and also avoids multi-process issues, where
if there's two processes trying to increment at the
same time, they may both receive the same value, add one
to it, then both write the same value back.
Then, it's only n+1 instead of n+2.

This also prevents almost all chat scheduled jobs from
running if chat is disabled, the only one remaining is
the message retention job.
This commit is contained in:
Martin Brennan
2023-04-24 09:32:04 +10:00
committed by GitHub
parent 21f93731a3
commit 24ec06ff85
27 changed files with 694 additions and 233 deletions
@@ -0,0 +1,55 @@
# frozen_string_literal: true
RSpec.describe Jobs::Chat::UpdateThreadReplyCount do
fab!(:thread) { Fabricate(:chat_thread) }
fab!(:message_1) { Fabricate(:chat_message, thread: thread) }
fab!(:message_2) { Fabricate(:chat_message, thread: thread) }
before do
Chat::Thread.clear_caches!(thread.id)
SiteSetting.enable_experimental_chat_threaded_discussions = true
end
it "does nothing if enable_experimental_chat_threaded_discussions is false" do
SiteSetting.enable_experimental_chat_threaded_discussions = false
Chat::Thread.any_instance.expects(:set_replies_count_cache).never
described_class.new.execute(thread_id: thread.id)
end
it "does not error if the thread is deleted" do
id = thread.id
thread.destroy!
expect { described_class.new.execute(thread_id: id) }.not_to raise_error
end
it "does not set the reply count in the DB if it has been changed recently" do
described_class.new.execute(thread_id: thread.id)
expect(thread.reload.replies_count).to eq(2)
Fabricate(:chat_message, thread: thread)
described_class.new.execute(thread_id: thread.id)
expect(thread.reload.replies_count).to eq(2)
end
it "sets the updated_at cache to the current time" do
freeze_time
described_class.new.execute(thread_id: thread.id)
expect(thread.replies_count_cache_updated_at).to eq_time(
Time.at(Time.zone.now.to_i, in: Time.zone),
)
end
it "publishes the thread original message metadata" do
messages =
MessageBus.track_publish("/chat/#{thread.channel_id}") do
described_class.new.execute(thread_id: thread.id)
end
expect(messages.first.data).to eq(
{
"original_message_id" => thread.original_message_id,
"replies_count" => 2,
"type" => "update_thread_original_message",
},
)
end
end