From 2c6567b5d276e4876b1b49599002b9bb1e7f58d8 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 22 Oct 2019 12:15:46 -0500 Subject: [PATCH] Remove uses of the read replica database. https://danbooru.donmai.us/forum_topics/9127?page=283#forum_post_160508 There was a recent outage that was caused by the read replica (yukinoshita.donmai.us) being temporarily unavailable. The pg driver in rails got hardstuck trying to connect to the replica, which brought down the whole site. The app servers stopped responding and could only be brought down with SIGKILL. Even try to boot the rails console didn't work. We only really used this to calculate tag counts inside Post.fast_count, which wasn't really beneficial since the read replica is slower than the main database. --- app/jobs/tag_batch_change_job.rb | 2 +- app/logical/post_query_builder.rb | 7 +++---- app/models/post.rb | 8 ++++---- app/models/post_read_only.rb | 13 ------------- config/docker/compose.yml | 1 - script/install/database.yml.templ | 21 --------------------- 6 files changed, 8 insertions(+), 44 deletions(-) delete mode 100644 app/models/post_read_only.rb diff --git a/app/jobs/tag_batch_change_job.rb b/app/jobs/tag_batch_change_job.rb index 1a91cfeb1..fd03c6247 100644 --- a/app/jobs/tag_batch_change_job.rb +++ b/app/jobs/tag_batch_change_job.rb @@ -22,7 +22,7 @@ class TagBatchChangeJob < ApplicationJob def self.estimate_update_count(antecedent, consequent) CurrentUser.without_safe_mode do - PostReadOnly.tag_match(antecedent).count + Post.tag_match(antecedent).count end end diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index d28d05407..cacad9d82 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -1,9 +1,8 @@ class PostQueryBuilder - attr_accessor :query_string, :read_only + attr_accessor :query_string - def initialize(query_string, read_only: false) + def initialize(query_string) @query_string = query_string - @read_only = read_only end def add_range_relation(arr, field, relation) @@ -117,7 +116,7 @@ class PostQueryBuilder q = Tag.parse_query(query_string) end - relation = read_only ? PostReadOnly.all : Post.all + relation = Post.all if q[:tag_count].to_i > Danbooru.config.tag_query_limit raise ::Post::SearchError diff --git a/app/models/post.rb b/app/models/post.rb index 9c0a68d20..52bbb2dca 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1129,8 +1129,8 @@ class Post < ApplicationRecord end def fast_count_search(tags, timeout:, raise_on_timeout:) - count = PostReadOnly.with_timeout(timeout, nil, tags: tags) do - PostReadOnly.tag_match(tags).count + count = Post.with_timeout(timeout, nil, tags: tags) do + Post.tag_match(tags).count end if count.nil? @@ -1645,8 +1645,8 @@ class Post < ApplicationRecord where("posts.tag_index @@ to_tsquery('danbooru', E?)", tag.to_escaped_for_tsquery) end - def tag_match(query, read_only: false) - PostQueryBuilder.new(query, read_only: read_only).build + def tag_match(query) + PostQueryBuilder.new(query).build end end diff --git a/app/models/post_read_only.rb b/app/models/post_read_only.rb deleted file mode 100644 index 96d0a1f3d..000000000 --- a/app/models/post_read_only.rb +++ /dev/null @@ -1,13 +0,0 @@ -class PostReadOnly < Post - establish_connection (ENV["RO_DATABASE_URL"] || "ro_#{Rails.env}".to_sym) - attr_readonly *column_names - - def with_timeout(n, default_value = nil) - connection.execute("SET STATEMENT_TIMEOUT = #{n}") unless Rails.env == "test" - yield - rescue ::ActiveRecord::StatementInvalid => x - return default_value - ensure - connection.execute("SET STATEMENT_TIMEOUT = 0") unless Rails.env == "test" - end -end diff --git a/config/docker/compose.yml b/config/docker/compose.yml index ca82669a7..f912d6165 100644 --- a/config/docker/compose.yml +++ b/config/docker/compose.yml @@ -31,7 +31,6 @@ services: - SESSION_SECRET_KEY - RAILS_ENV - DATABASE_URL - - RO_DATABASE_URL - DEBUG - ARCHIVE_DATABASE_URL - DANBOORU_AWS_SQS_ARCHIVE_URL diff --git a/script/install/database.yml.templ b/script/install/database.yml.templ index 5caf077ef..fb4795943 100755 --- a/script/install/database.yml.templ +++ b/script/install/database.yml.templ @@ -24,27 +24,6 @@ production: timeout: 5000 url: <%= ENV['DATABASE_URL'] %> -# read only databases, just point to local copies if you have no need -ro_development: - adapter: postgresql - database: danbooru2 - url: <%= ENV['RO_DATABASE_URL'] %> - -ro_test: - adapter: postgresql - database: danbooru2_test - url: <%= ENV['RO_DATABASE_URL'] %> - -ro_production: - adapter: postgresql - database: danbooru2 - url: <%= ENV['RO_DATABASE_URL'] %> - -ro_staging: - adapter: postgresql - database: danbooru2 - url: <%= ENV['RO_DATABASE_URL'] %> - archive_development: adapter: postgresql database: archive_development