Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Multi-Shard Databases and Rails Database Selector in Read-Only Mode #214

Closed
Dandush03 opened this issue Sep 7, 2024 · 0 comments · Fixed by #219
Closed

Issue with Multi-Shard Databases and Rails Database Selector in Read-Only Mode #214

Dandush03 opened this issue Sep 7, 2024 · 0 comments · Fixed by #219
Assignees

Comments

@Dandush03
Copy link

When using multi-shard databases with the default Rails configuration for sharding, issues arise when attempting to write to the database while in read-only mode. The default configuration is as follows:

Rails.application.configure do
  config.active_record.database_selector = { delay: 2.seconds }
  config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
  config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
end

The issue occurs because, in read-only mode, the connection attempts to load the following class:

SolidCache::Connections::Unmanaged.new

This leads to a failure since the connection persists in read-only mode, causing the operation to break when a write is attempted.

Solution

To resolve this, I updated the configuration from using database to databases, and enclosed the database name in a single-element array within the solid_cache.yml file. This modification forces the system to load:

SolidCache::Connections::Single.new(names.first)

By doing this, the issue was resolved, as it properly handles the connection in the correct mode.

Before

Here’s the original solid_cache.yml configuration that caused the issue:

default: &default
  database: solid_cache
  store_options:
    max_age: <%= 1.week.to_i %>
    max_size: <%= 256.megabytes %>
    namespace: <%= Rails.env %>

development:
  <<: *default

test:
  <<: *default

production:
  <<: *default

After

This is the updated solid_cache.yml configuration that resolved the issue:

default: &default
  databases: [solid_cache]
  store_options:
    max_age: <%= 1.week.to_i %>
    max_size: <%= 256.megabytes %>
    namespace: <%= Rails.env %>

development:
  <<: *default

test:
  <<: *default

production:
  <<: *default

Reference
You can find the specific lines in question that caused the issue here: https://github.com/rails/solid_cache/blob/main/lib/solid_cache/connections.rb#L5

@Dandush03 Dandush03 changed the title Multi Shards Issue with Multi-Shard Databases and Rails Database Selector in Read-Only Mode Sep 7, 2024
@dhh dhh assigned djmb Sep 9, 2024
djmb added a commit that referenced this issue Sep 9, 2024
When a database is specified, we should always manage connections so
that we can ensure the cache is writable.

But if you just use the default connection for the rest of the app, we
won't attempt to manage that.

Fixes: #214
@djmb djmb closed this as completed in #219 Sep 10, 2024
@djmb djmb closed this as completed in f1d3f57 Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants