First pass at multi-database for read replica using Rails native adapter (#25693)

Co-authored-by: emilweth <7402764+emilweth@users.noreply.github.com>
This commit is contained in:
Kurtis Rainbolt-Greene 2023-07-08 10:45:36 -07:00 committed by GitHub
parent 4534498a8e
commit e4cfe4b3db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 26 deletions

View file

@ -809,7 +809,6 @@ Style/FrozenStringLiteralComment:
- 'config/initializers/httplog.rb' - 'config/initializers/httplog.rb'
- 'config/initializers/inflections.rb' - 'config/initializers/inflections.rb'
- 'config/initializers/mail_delivery_job.rb' - 'config/initializers/mail_delivery_job.rb'
- 'config/initializers/makara.rb'
- 'config/initializers/mime_types.rb' - 'config/initializers/mime_types.rb'
- 'config/initializers/oj.rb' - 'config/initializers/oj.rb'
- 'config/initializers/omniauth.rb' - 'config/initializers/omniauth.rb'

View file

@ -11,7 +11,6 @@ gem 'rack', '~> 2.2.7'
gem 'haml-rails', '~>2.0' gem 'haml-rails', '~>2.0'
gem 'pg', '~> 1.5' gem 'pg', '~> 1.5'
gem 'makara', '~> 0.5'
gem 'pghero' gem 'pghero'
gem 'dotenv-rails', '~> 2.8' gem 'dotenv-rails', '~> 2.8'

View file

@ -399,8 +399,6 @@ GEM
net-imap net-imap
net-pop net-pop
net-smtp net-smtp
makara (0.5.1)
activerecord (>= 5.2.0)
marcel (1.0.2) marcel (1.0.2)
mario-redis-lock (1.2.1) mario-redis-lock (1.2.1)
redis (>= 3.0.5) redis (>= 3.0.5)
@ -815,7 +813,6 @@ DEPENDENCIES
letter_opener_web (~> 2.0) letter_opener_web (~> 2.0)
link_header (~> 0.0) link_header (~> 0.0)
lograge (~> 0.12) lograge (~> 0.12)
makara (~> 0.5)
mario-redis-lock (~> 1.2) mario-redis-lock (~> 1.2)
memory_profiler memory_profiler
mime-types (~> 3.4.1) mime-types (~> 3.4.1)

View file

@ -6,11 +6,14 @@ class Api::V1::Timelines::HomeController < Api::BaseController
after_action :insert_pagination_headers, unless: -> { @statuses.empty? } after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
def show def show
@statuses = load_statuses ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
@statuses = load_statuses
@relationships = StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
end
render json: @statuses, render json: @statuses,
each_serializer: REST::StatusSerializer, each_serializer: REST::StatusSerializer,
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id), relationships: @relationships,
status: account_home_feed.regenerating? ? 206 : 200 status: account_home_feed.regenerating? ? 206 : 200
end end

View file

@ -4,19 +4,23 @@ class FeedInsertWorker
include Sidekiq::Worker include Sidekiq::Worker
def perform(status_id, id, type = 'home', options = {}) def perform(status_id, id, type = 'home', options = {})
@type = type.to_sym ApplicationRecord.connected_to(role: :primary) do
@status = Status.find(status_id) @type = type.to_sym
@options = options.symbolize_keys @status = Status.find(status_id)
@options = options.symbolize_keys
case @type case @type
when :home, :tags when :home, :tags
@follower = Account.find(id) @follower = Account.find(id)
when :list when :list
@list = List.find(id) @list = List.find(id)
@follower = @list.account @follower = @list.account
end
end end
check_and_insert ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
check_and_insert
end
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
true true
end end

View file

@ -27,10 +27,20 @@ test:
port: <%= ENV['DB_PORT'] %> port: <%= ENV['DB_PORT'] %>
production: production:
<<: *default primary:
database: <%= ENV['DB_NAME'] || 'mastodon_production' %> <<: *default
username: <%= ENV['DB_USER'] || 'mastodon' %> database: <%= ENV['DB_NAME'] || 'mastodon_production' %>
password: <%= (ENV['DB_PASS'] || '').to_json %> username: <%= ENV['DB_USER'] || 'mastodon' %>
host: <%= ENV['DB_HOST'] || 'localhost' %> password: <%= (ENV['DB_PASS'] || '').to_json %>
port: <%= ENV['DB_PORT'] || 5432 %> host: <%= ENV['DB_HOST'] || 'localhost' %>
prepared_statements: <%= ENV['PREPARED_STATEMENTS'] || 'true' %> port: <%= ENV['DB_PORT'] || 5432 %>
prepared_statements: <%= ENV['PREPARED_STATEMENTS'] || 'true' %>
read:
<<: *default
database: <%= ENV['DB_REPLICA_NAME'] ||ENV['DB_NAME'] || 'mastodon_production' %>
username: <%= ENV['DB_REPLICA_USER'] ||ENV['DB_USER'] || 'mastodon' %>
password: <%= (ENV['DB_REPLICA_PASS'] || ENV['DB_PASS'] || '').to_json %>
host: <%= ENV['DB_REPLICA_HOST'] ||ENV['DB_HOST'] || 'localhost' %>
port: <%= ENV['DB_REPLICA_PORT'] ||ENV['DB_PORT'] || 5432 %>
prepared_statements: <%= ENV['PREPARED_STATEMENTS'] || 'true' %>
replica: true

View file

@ -1,2 +0,0 @@
Makara::Cookie::DEFAULT_OPTIONS[:same_site] = :lax
Makara::Cookie::DEFAULT_OPTIONS[:secure] = Rails.env.production? || ENV['LOCAL_HTTPS'] == 'true'