Skip to content

Part 3: Multi-db Improvements, identifying replica configurations#33770

Merged
eileencodes merged 3 commits intorails:masterfrom
eileencodes:multi-db-improvements-part-3
Sep 1, 2018
Merged

Part 3: Multi-db Improvements, identifying replica configurations#33770
eileencodes merged 3 commits intorails:masterfrom
eileencodes:multi-db-improvements-part-3

Conversation

@eileencodes
Copy link
Member

I'm going to start opening smaller PR's now that the refactoring is merged. This PR adds a few things:

  1. Adds a replica option to the configurations
  2. Adds a check on HashConfig and UrlConfig for replica? that checks for config["replica"] key
  3. Updates configs_for to take kwargs
  4. Updates configs_for to take a kwarg for include_replicas that defaults to false. When you're creating/dropping/migrating dbs you don't need to run the create/drop/migrate for the replicas as well. I defaulted to not including replicas because 99% of the time when you're iterating over the dbs (like creating the tasks, running the tasks etc) you don't actually want the replicas. This prevents messages like "database already exists" when running the create command.

cc/ @tenderlove @matthewd @rafaelfranca

This allows the user to add `replica: true` to the database config to
signify the connection should be treated as readonly. This will be
useful so we can ignore structure dumps or migrations (or creating /
deleting etc) the readonly connection for the databases. These are
paired with a write database which is where the create/drop/migrate
should be run. This allows us to ask the connection if it's for a
replica readonly db or a primary write db.
Checks if the config has a "replica" key, if so the configuration is for
a replica database. This is useful for excluding replicas from the
configurations list when creating the rake tasks or running rake tasks.
For example, we don't want to create the primary and primary_readonly.
They're the same database.
@eileencodes eileencodes added this to the 6.0.0 milestone Aug 31, 2018
@eileencodes eileencodes self-assigned this Aug 31, 2018
Changes the `configs_for` method from using traditional arguments to
using kwargs. This is so I can add the `include_replicas` kwarg without
having to always include `env_name` and `spec_name` in the method call.

`include_replicas` defaults to false because everywhere internally in
Rails we don't want replicas. `configs_for` is for iterating over
configurations to create / run rake tasks, so we really don't ever need
replicas in that case.
@eileencodes eileencodes force-pushed the multi-db-improvements-part-3 branch from 5a59643 to a572d22 Compare August 31, 2018 20:07
end

def replica?
@config[:replica] || false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileencodes Is it in the scope of this PR for each database adapter to verify if the replica database is indeed a replica to help catch user errors? We have this guard in place at Discourse whenever we switch over to a replica database:

  value = connection.exec_query("SELECT pg_is_in_recovery()").rows[0][0]
  raise "Replica database server is not in recovery mode." if !value

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At GitHub we have rw databases (primary) and ro databases (replica). The replicas have different users than the primary ones. In addition we also have readonly connections - a rw database can be connected to in a ro way. This change is for the first part - to be able to identify the replica database in the configurations list. Later when I implement the connection switching I'll handle your case as well since we need that at GitHub, but that's going to be a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is great to hear :) Thank you for sharing.

@eileencodes eileencodes merged commit d8ac08f into rails:master Sep 1, 2018
@eileencodes eileencodes deleted the multi-db-improvements-part-3 branch September 1, 2018 13:49
@mPanasiewicz
Copy link

@eileencodes Awesome!

bogdanvlviv added a commit to bogdanvlviv/rails that referenced this pull request Sep 9, 2018
… the CHANGELOG

Since rails#33770 `#configs_for` changed method signature and it isn't
supposed to work with a passed block.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants