mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
FIX: in readonly mode don't double count pages
This commit is contained in:
parent
d9a9ad3edb
commit
877b7be579
4 changed files with 53 additions and 12 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
class ApplicationRequest < ActiveRecord::Base
|
class ApplicationRequest < ActiveRecord::Base
|
||||||
enum req_type: %i(http_total
|
enum req_type: %i(http_total
|
||||||
http_2xx
|
http_2xx
|
||||||
|
@ -36,6 +37,12 @@ class ApplicationRequest < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
GET_AND_RESET = <<~LUA
|
||||||
|
local val = redis.call('get', KEYS[1])
|
||||||
|
redis.call('set', KEYS[1], '0')
|
||||||
|
return val
|
||||||
|
LUA
|
||||||
|
|
||||||
def self.write_cache!(date = nil)
|
def self.write_cache!(date = nil)
|
||||||
if date.nil?
|
if date.nil?
|
||||||
write_cache!(Time.now.utc)
|
write_cache!(Time.now.utc)
|
||||||
|
@ -51,20 +58,12 @@ class ApplicationRequest < ActiveRecord::Base
|
||||||
# for concurrent calls without double counting
|
# for concurrent calls without double counting
|
||||||
req_types.each do |req_type, _|
|
req_types.each do |req_type, _|
|
||||||
key = redis_key(req_type, date)
|
key = redis_key(req_type, date)
|
||||||
val = $redis.get(key).to_i
|
|
||||||
|
|
||||||
|
namespaced_key = $redis.namespace_key(key)
|
||||||
|
val = $redis.eval(GET_AND_RESET, keys: [namespaced_key]).to_i
|
||||||
next if val == 0
|
next if val == 0
|
||||||
|
|
||||||
new_val = $redis.incrby(key, -val).to_i
|
|
||||||
|
|
||||||
if new_val < 0
|
|
||||||
# undo and flush next time
|
|
||||||
$redis.incrby(key, val)
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
id = req_id(date, req_type)
|
id = req_id(date, req_type)
|
||||||
|
|
||||||
where(id: id).update_all(["count = count + ?", val])
|
where(id: id).update_all(["count = count + ?", val])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -159,6 +159,7 @@ class DiscourseRedis
|
||||||
|
|
||||||
fallback_handler.verify_master if !fallback_handler.master
|
fallback_handler.verify_master if !fallback_handler.master
|
||||||
Discourse.received_readonly!
|
Discourse.received_readonly!
|
||||||
|
nil
|
||||||
else
|
else
|
||||||
raise ex
|
raise ex
|
||||||
end
|
end
|
||||||
|
@ -231,6 +232,14 @@ class DiscourseRedis
|
||||||
@redis.client.reconnect
|
@redis.client.reconnect
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def namespace_key(key)
|
||||||
|
if @namespace
|
||||||
|
"#{namespace}:#{key}"
|
||||||
|
else
|
||||||
|
key
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def namespace
|
def namespace
|
||||||
RailsMultisite::ConnectionManagement.current_db
|
RailsMultisite::ConnectionManagement.current_db
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,6 +10,11 @@ describe DiscourseRedis do
|
||||||
|
|
||||||
let(:fallback_handler) { DiscourseRedis::FallbackHandler.instance }
|
let(:fallback_handler) { DiscourseRedis::FallbackHandler.instance }
|
||||||
|
|
||||||
|
it "ignore_readonly returns nil from a pure exception" do
|
||||||
|
result = DiscourseRedis.ignore_readonly { raise Redis::CommandError.new("READONLY") }
|
||||||
|
expect(result).to eq(nil)
|
||||||
|
end
|
||||||
|
|
||||||
describe 'redis commands' do
|
describe 'redis commands' do
|
||||||
let(:raw_redis) { Redis.new(DiscourseRedis.config) }
|
let(:raw_redis) { Redis.new(DiscourseRedis.config) }
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ require 'rails_helper'
|
||||||
|
|
||||||
describe ApplicationRequest do
|
describe ApplicationRequest do
|
||||||
before do
|
before do
|
||||||
|
ApplicationRequest.last_flush = Time.now.utc
|
||||||
ApplicationRequest.clear_cache!
|
ApplicationRequest.clear_cache!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,19 +14,46 @@ describe ApplicationRequest do
|
||||||
ApplicationRequest.increment!(key, opts)
|
ApplicationRequest.increment!(key, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def disable_date_flush!
|
||||||
|
freeze_time(Time.now)
|
||||||
|
ApplicationRequest.last_flush = Time.now.utc
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'works even if redis is in readonly' do
|
||||||
|
disable_date_flush!
|
||||||
|
|
||||||
|
inc(:http_total)
|
||||||
|
inc(:http_total)
|
||||||
|
|
||||||
|
$redis.slaveof("127.0.0.1", 666)
|
||||||
|
|
||||||
|
# flush will be deferred no error raised
|
||||||
|
inc(:http_total, autoflush: 3)
|
||||||
|
$redis.slaveof("no", "one")
|
||||||
|
|
||||||
|
inc(:http_total, autoflush: 3)
|
||||||
|
expect(ApplicationRequest.http_total.first.count).to eq(3)
|
||||||
|
end
|
||||||
|
|
||||||
it 'logs nothing for an unflushed increment' do
|
it 'logs nothing for an unflushed increment' do
|
||||||
ApplicationRequest.increment!(:anon)
|
ApplicationRequest.increment!(:anon)
|
||||||
expect(ApplicationRequest.count).to eq(0)
|
expect(ApplicationRequest.count).to eq(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'can automatically flush' do
|
it 'can automatically flush' do
|
||||||
t1 = Time.now.utc.at_midnight
|
disable_date_flush!
|
||||||
freeze_time(t1)
|
|
||||||
inc(:http_total)
|
inc(:http_total)
|
||||||
inc(:http_total)
|
inc(:http_total)
|
||||||
inc(:http_total, autoflush: 3)
|
inc(:http_total, autoflush: 3)
|
||||||
|
|
||||||
expect(ApplicationRequest.http_total.first.count).to eq(3)
|
expect(ApplicationRequest.http_total.first.count).to eq(3)
|
||||||
|
|
||||||
|
inc(:http_total)
|
||||||
|
inc(:http_total)
|
||||||
|
inc(:http_total, autoflush: 3)
|
||||||
|
|
||||||
|
expect(ApplicationRequest.http_total.first.count).to eq(6)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'can flush based on time' do
|
it 'can flush based on time' do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue