From e4cfe4b3db2e71cef43c7b8f7ce42ba46f7aa0b5 Mon Sep 17 00:00:00 2001 From: Kurtis Rainbolt-Greene Date: Sat, 8 Jul 2023 10:45:36 -0700 Subject: [PATCH] First pass at multi-database for read replica using Rails native adapter (#25693) Co-authored-by: emilweth <7402764+emilweth@users.noreply.github.com> --- .rubocop_todo.yml | 1 - Gemfile | 1 - Gemfile.lock | 3 --- .../api/v1/timelines/home_controller.rb | 7 ++++-- app/workers/feed_insert_worker.rb | 24 +++++++++++-------- config/database.yml | 24 +++++++++++++------ config/initializers/makara.rb | 2 -- 7 files changed, 36 insertions(+), 26 deletions(-) delete mode 100644 config/initializers/makara.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 975c9d28fb..24f02d4d3b 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -809,7 +809,6 @@ Style/FrozenStringLiteralComment: - 'config/initializers/httplog.rb' - 'config/initializers/inflections.rb' - 'config/initializers/mail_delivery_job.rb' - - 'config/initializers/makara.rb' - 'config/initializers/mime_types.rb' - 'config/initializers/oj.rb' - 'config/initializers/omniauth.rb' diff --git a/Gemfile b/Gemfile index 3feb3f9548..24cb43e657 100644 --- a/Gemfile +++ b/Gemfile @@ -11,7 +11,6 @@ gem 'rack', '~> 2.2.7' gem 'haml-rails', '~>2.0' gem 'pg', '~> 1.5' -gem 'makara', '~> 0.5' gem 'pghero' gem 'dotenv-rails', '~> 2.8' diff --git a/Gemfile.lock b/Gemfile.lock index 985e36c20c..9bd708d61e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -399,8 +399,6 @@ GEM net-imap net-pop net-smtp - makara (0.5.1) - activerecord (>= 5.2.0) marcel (1.0.2) mario-redis-lock (1.2.1) redis (>= 3.0.5) @@ -815,7 +813,6 @@ DEPENDENCIES letter_opener_web (~> 2.0) link_header (~> 0.0) lograge (~> 0.12) - makara (~> 0.5) mario-redis-lock (~> 1.2) memory_profiler mime-types (~> 3.4.1) diff --git a/app/controllers/api/v1/timelines/home_controller.rb b/app/controllers/api/v1/timelines/home_controller.rb index ae6dbcb8b3..0ee28ef042 100644 --- a/app/controllers/api/v1/timelines/home_controller.rb +++ b/app/controllers/api/v1/timelines/home_controller.rb @@ -6,11 +6,14 @@ class Api::V1::Timelines::HomeController < Api::BaseController after_action :insert_pagination_headers, unless: -> { @statuses.empty? } 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, each_serializer: REST::StatusSerializer, - relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id), + relationships: @relationships, status: account_home_feed.regenerating? ? 206 : 200 end diff --git a/app/workers/feed_insert_worker.rb b/app/workers/feed_insert_worker.rb index 758cebd4ba..47826c2111 100644 --- a/app/workers/feed_insert_worker.rb +++ b/app/workers/feed_insert_worker.rb @@ -4,19 +4,23 @@ class FeedInsertWorker include Sidekiq::Worker def perform(status_id, id, type = 'home', options = {}) - @type = type.to_sym - @status = Status.find(status_id) - @options = options.symbolize_keys + ApplicationRecord.connected_to(role: :primary) do + @type = type.to_sym + @status = Status.find(status_id) + @options = options.symbolize_keys - case @type - when :home, :tags - @follower = Account.find(id) - when :list - @list = List.find(id) - @follower = @list.account + case @type + when :home, :tags + @follower = Account.find(id) + when :list + @list = List.find(id) + @follower = @list.account + end end - check_and_insert + ApplicationRecord.connected_to(role: :read, prevent_writes: true) do + check_and_insert + end rescue ActiveRecord::RecordNotFound true end diff --git a/config/database.yml b/config/database.yml index 34acf2f19a..f7ecbd9814 100644 --- a/config/database.yml +++ b/config/database.yml @@ -27,10 +27,20 @@ test: port: <%= ENV['DB_PORT'] %> production: - <<: *default - database: <%= ENV['DB_NAME'] || 'mastodon_production' %> - username: <%= ENV['DB_USER'] || 'mastodon' %> - password: <%= (ENV['DB_PASS'] || '').to_json %> - host: <%= ENV['DB_HOST'] || 'localhost' %> - port: <%= ENV['DB_PORT'] || 5432 %> - prepared_statements: <%= ENV['PREPARED_STATEMENTS'] || 'true' %> + primary: + <<: *default + database: <%= ENV['DB_NAME'] || 'mastodon_production' %> + username: <%= ENV['DB_USER'] || 'mastodon' %> + password: <%= (ENV['DB_PASS'] || '').to_json %> + host: <%= ENV['DB_HOST'] || 'localhost' %> + 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 diff --git a/config/initializers/makara.rb b/config/initializers/makara.rb deleted file mode 100644 index dc88fa63cd..0000000000 --- a/config/initializers/makara.rb +++ /dev/null @@ -1,2 +0,0 @@ -Makara::Cookie::DEFAULT_OPTIONS[:same_site] = :lax -Makara::Cookie::DEFAULT_OPTIONS[:secure] = Rails.env.production? || ENV['LOCAL_HTTPS'] == 'true'