-
Notifications
You must be signed in to change notification settings - Fork 7
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
Flexible switching #1
base: development
Are you sure you want to change the base?
Conversation
When the gem is initialised and a schema adapter is being used, the adapter will initialise the schema_search_path which involves touching the database, and thus checking out a connection for the current thread. For threaded web servers, it is not expected that the main thread has a database connection, so you typically set the connection pool size equal to the number of web server threads. This patch checks the connection back in to the pool after init so that a web server thread can pick it up and use it.
* Use public_suffix for smarter subdomain parsing * Check validity of domain and add tests for localhost and IP * Remove unnecessary TLD length test
This is a complete rewrite of the adapters & of the test suite. - Test suite rewritten in minitest - Adapters handle separate physical tenant hosts seamlessly - Supports Rails 5.1 and above only - Dropped all adapters other than PG and MySQL (didn't get time, will accept PRs) - Added 'tenant resolvers' for generating full database configurations from tenant names - Added `tenant_decorator` option for using a proc to decorate your tenant database/schema names with Rails.env or anything else - All excluded models share a connection pool - All adapters & switching strategies are threadsafe
Still some issues around multithreading, I'm not confident it's correct. Need to improve tests and fix postgresql database drop problem :/.
Use a Thread local in the connection_specification_name to isolate each thread's connection config.
@mikecmpbll - is this PR meant to resolve issues like influitive#615 and influitive#311? Looking to use Apartment with Puma and Postgres connection-switching, and running into issues running tests. |
That right, yep. A lot has happened recently actually. We’ve been using this branch in production across multiple hosts, using threads, and serving approximately 50-100 requests per second, for about three weeks now. We’ve not experienced any major issues so far. We’ve fixed a number of small bugs over the last week or two. |
@mikecmpbll this is really hopeful for what I'm doing, having threadsafe issues with Rails 6 + Postgres. Do you have any example for what you are running instead of On development we often will do db:drop, db:create, db:migrate but the drop of course says not implemented. |
hey @cgratigny . the postgres adapter isn't as well tested as the mysql which i use in production, although i did attempt to replicate all the existing tests for pg. because there's two ways to separate tenents on pg (schemas and databases), there's two methods there's no config option anymore for whether you're using schemas or databases and you can use both at the same time, so i guess there's no way for apartment to know whether you're trying to drop the tenant's database or schema without explicitly calling one of those methods. not sure what the best approach should be for the |
@mikecmpbll - Thanks so much for putting in so much work into this project. Do you have plans to eventually merge this PR into the main Apartment repo? If so, any idea when that might happen? We're deciding between schema-based and connection-switching approaches (Rails 6 + Postgres + Puma), so any ETA would be super helpful before we commit to one of the approaches. |
@rubendinho I have a very strong interest in making this work with Postgres and database switching, we are limping along the best we can with some hacks. |
Given there's demand I'll do a proper fork of this and release it to rubygems this week. I plan to merge it in bit by bit to apartment but seeing as it's a complete rewrite I don't think it'd be right by existing users to merge wholesale, especially in a domain as important as tenant data separation.
…________________________________
From: Chris Gratigny <[email protected]>
Sent: Monday, October 14, 2019 10:08:05 PM
To: mikecmpbll/apartment <[email protected]>
Cc: Mike Campbell <[email protected]>; Mention <[email protected]>
Subject: Re: [mikecmpbll/apartment] Flexible switching (#1)
@rubendinho<https://github.com/rubendinho> I have a very strong interest in making this work with Postgres and database switching, we are limping along the best we can with some hacks.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#1?email_source=notifications&email_token=AAO7GMNSIH5JNE7D47ZYM7DQOTNTLA5CNFSM4DQWGPP2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBGRJVY#issuecomment-541922519>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAO7GMNLLET7IEQGYG4JBOLQOTNTLANCNFSM4DQWGPPQ>.
|
@mikecmpbll I'm willing to contribute to the Postgres database side of things, test etc. |
Super. I'll get that done tomorrow.
…________________________________
From: Chris Gratigny <[email protected]>
Sent: Monday, October 14, 2019 11:52:11 PM
To: mikecmpbll/apartment <[email protected]>
Cc: Mike Campbell <[email protected]>; Mention <[email protected]>
Subject: Re: [mikecmpbll/apartment] Flexible switching (#1)
@mikecmpbll<https://github.com/mikecmpbll> I'm willing to contribute to the Postgres database side of things, test etc.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#1?email_source=notifications&email_token=AAO7GMP5WHKLKKDNPSMC2X3QOTZZXA5CNFSM4DQWGPP2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBG3TUI#issuecomment-541964753>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAO7GMLTFUW5YC2PVD3H5ULQOTZZXANCNFSM4DQWGPPQ>.
|
@mikecmpbll and @cgratigny - our use-case requires physical data separation (PG schemas are not sufficient), and I'd also be happy to contribute to the PG side of things, including using Heroku's API to manage database provision, if that's of interest to folks. |
because it was no different to the other subdomain elevator as far as i could tell. i've forked our active branch here; https://github.com/cpoms/innkeeper and released to rubygems https://rubygems.org/gems/innkeeper. i've not fully tested it after renaming the whole thing. |
@rubendinho great! The of side needs some love on this for sure. I'll be physically separating in the future as well, just not there yet. I'm not on Heroku but know a lot of people use it. |
@mikecmpbll any update regarding this #PR? |
Hi @mikecmpbll - I'm using this gem from your branch 'flexible-switching' in my old application got some compatible version issue. My Dependencies: My errors when tried to do the bundle update: Bundler could not find compatible versions for gem "actionmailer":
Bundler could not find compatible versions for gem "actionpack":
Bundler could not find compatible versions for gem "activerecord":
Bundler could not find compatible versions for gem "acts_as_list": Bundler could not find compatible versions for gem "json":
Bundler could not find compatible versions for gem "listen":
Bundler could not find compatible versions for gem "rails":
Bundler could not find compatible versions for gem "redis":
Bundler could not find compatible versions for gem "ruby":
Bundler could not find compatible versions for gem "web-console": Help me to solve this issue |
When the initial connection is to an invalid tenant (non-existant db, for example), the app process can never successfully reconnect to a valid tenant.
Ludicrous amount of rescues.
If it does, we lose the consistency of @current, which seems to be what's happening.
When the process is forked or a new thread is started, set-up our ActiveRecord connection_specification_name overrides again because programs like Passenger will now call `ActiveRecord::Base.establish_connection` when they spawn new app processes, which in turn cause `ActiveRecord::Base.connection_specification_name` to be reset to whatever is specified in database.yml (or :primary as default). I can't see that this could have any adverse affects. If the app sets `connection_specification_name` on anything other than `Apartment.connection_class` (`ActiveRecord::Base` by default) that should be unaffected and preserved. This is a more generic solution that hooking into some Passenger-specific after-spawn hook.
When the threading issue with Puma will be resolved in Apartment Gem ? |
No description provided.