-
Notifications
You must be signed in to change notification settings - Fork 231
Allow shard setting with comments #293
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
Conversation
Hey there John, nice to be working together again! I think you're on the right track with this PR. We've been thinking about this functionality for a while now and this is definitely a welcome change. A few pointers to make this work:
|
…rable, and include sharding_key setting
I did some cleanup on this to move the logic somewhere it made more sense. I also made it configurable and optional by default. It now also support setting a sharding_key to help support #168. If other special commands are needed via comments it should be easy to expand this to include other options like role. Let me know how this looks. |
…f sharding commands are found
@@ -85,6 +85,12 @@ query_parser_enabled = true | |||
# queries. The primary can always be explicitly selected with our custom protocol. | |||
primary_reads_enabled = true | |||
|
|||
# Allow sharding commands to be passed as statement comments instead of | |||
# separate commands. If these are unset this functionality is disabled. | |||
# sharding_key_regex = '/\* sharding_key: (\d+) \*/' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are commented out because when they were uncommented it caused the Ruby TOML parser to fail when loading the file. These aren't used in tests right now, so it should be fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will need to figure this out because otherwise this feature won't be tested in end-to-end tests we have in Ruby. I think it's fine to test it with Bash to start, but the Ruby test suit protects us against bad things and we want it to run there too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The LOC LB test is flakey, rerunning it usually helps. We have an action item to fix this. The PR looks good to me to merge. Eventually we would want some tests in bash/Ruby/Python so we don't get hit with a regression. I'll let @drdrsh take a look before merging.
// Check for any sharding regex matches in any queries | ||
match code as char { | ||
// For Parse and Query messages peek to see if they specify a shard_id as a comment early in the statement |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can avoid this entire branch when the number of shards is 1, we can get that from self.pool_settings.shards
. This will make this check even cheaper for the base, unsharded use case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scratch that, the lack of the regex in the confs should be enough of a gate
We'll test this change internally. I will merge it once we are done @levkk |
pub shards: BTreeMap<String, Shard>, | ||
pub users: BTreeMap<String, User>, | ||
// Note, don't put simple fields below these configs. There's a compatability issue with TOML that makes it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having the new fields below user and shard broke our internal tool that generates the TOML config. Since this is a pretty odd error that we hit I'm leaving behind the breadcrumb that got us to fix it.
What
Allows shard selection by the client to come in via comments like
/* shard_id: 1 */ select * from foo;
Why
We're using a setup in Ruby that makes it tough or impossible to inject commands on the connection to set the shard before it gets to the "real" SQL being run. Instead we have an updated PG adapter that allows injection of comments before each executed SQL statement. We need this support in pgcat in order to keep some complex shard picking logic in Ruby code while using pgcat for connection management.
Local Testing
Run postgres and pgcat with the default options. Run
psql < tests/sharding/query_routing_setup.sql
to setup the database for the tests and run./tests/pgbench/external_shard_test.sh
as often as needed to exercise the shard setting comment test.This was developed with @drdrsh