Skip to content

Fix local dev #112

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

Merged
merged 11 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion .circleci/pgcat.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ admin_password = "admin_pass"
# pool
# configs are structured as pool.<pool_name>
# the pool_name is what clients use as database name when connecting
# For the example below a client can connect using "postgres://sharding_user:sharding_user@pgcat_host:pgcat_port/sharded"
# For the example below a client can connect using "postgres://sharding_user:sharding_user@pgcat_host:pgcat_port/sharded_db"
[pools.sharded_db]
# Pool mode (see PgBouncer docs for more).
# session: one server connection per connected client
Expand Down Expand Up @@ -80,6 +80,11 @@ password = "sharding_user"
# is the sum of pool_size across all users.
pool_size = 9

[pools.sharded_db.users.1]
username = "other_user"
password = "other_user"
pool_size = 21

# Shard 0
[pools.sharded_db.shards.0]
# [ host, port, role ]
Expand All @@ -103,3 +108,23 @@ servers = [
[ "localhost", 5432, "replica" ],
]
database = "shard2"


[pools.simple_db]
pool_mode = "session"
default_role = "primary"
query_parser_enabled = true
primary_reads_enabled = true
sharding_function = "pg_bigint_hash"

[pools.simple_db.users.0]
username = "simple_user"
password = "simple_user"
pool_size = 5

[pools.simple_db.shards.0]
servers = [
[ "127.0.0.1", 5432, "primary" ],
[ "localhost", 5432, "replica" ]
]
database = "some_db"
20 changes: 10 additions & 10 deletions pgcat.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ port = 6432
# How long to wait before aborting a server connection (ms).
connect_timeout = 5000

# How much time to give `SELECT 1` health check query to return with a result (ms).
# How much time to give the health check query to return with a result (ms).
healthcheck_timeout = 1000

# For how long to ban a server if it fails a health check (seconds).
Expand All @@ -29,14 +29,14 @@ autoreload = false

# Credentials to access the virtual administrative database (pgbouncer or pgcat)
# Connecting to that database allows running commands like `SHOW POOLS`, `SHOW DATABASES`, etc..
admin_username = "user"
admin_password = "pass"
admin_username = "admin_user"
admin_password = "admin_pass"

# pool
# configs are structured as pool.<pool_name>
# the pool_name is what clients use as database name when connecting
# For the example below a client can connect using "postgres://sharding_user:sharding_user@pgcat_host:pgcat_port/sharded"
[pools.sharded]
# For the example below a client can connect using "postgres://sharding_user:sharding_user@pgcat_host:pgcat_port/sharded_db"
[pools.sharded_db]
# Pool mode (see PgBouncer docs for more).
# session: one server connection per connected client
# transaction: one server connection per client transaction
Expand Down Expand Up @@ -72,21 +72,21 @@ primary_reads_enabled = true
sharding_function = "pg_bigint_hash"

# Credentials for users that may connect to this cluster
[pools.sharded.users.0]
[pools.sharded_db.users.0]
username = "sharding_user"
password = "sharding_user"
# Maximum number of server connections that can be established for this user
# The maximum number of connection from a single Pgcat process to any database in the cluster
# is the sum of pool_size across all users.
pool_size = 9

[pools.sharded.users.1]
[pools.sharded_db.users.1]
username = "other_user"
password = "other_user"
pool_size = 21

# Shard 0
[pools.sharded.shards.0]
[pools.sharded_db.shards.0]
# [ host, port, role ]
servers = [
[ "127.0.0.1", 5432, "primary" ],
Expand All @@ -95,14 +95,14 @@ servers = [
# Database name (e.g. "postgres")
database = "shard0"

[pools.sharded.shards.1]
[pools.sharded_db.shards.1]
servers = [
[ "127.0.0.1", 5432, "primary" ],
[ "localhost", 5432, "replica" ],
]
database = "shard1"

[pools.sharded.shards.2]
[pools.sharded_db.shards.2]
servers = [
[ "127.0.0.1", 5432, "primary" ],
[ "localhost", 5432, "replica" ],
Expand Down
21 changes: 12 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,30 +521,33 @@ mod test {

assert_eq!(get_config().general.ban_time, 60);
assert_eq!(get_config().pools.len(), 2);
assert_eq!(get_config().pools["sharded"].shards.len(), 3);
assert_eq!(get_config().pools["sharded_db"].shards.len(), 3);
assert_eq!(get_config().pools["simple_db"].shards.len(), 1);
assert_eq!(get_config().pools["sharded"].users.len(), 2);
assert_eq!(get_config().pools["sharded_db"].users.len(), 2);
assert_eq!(get_config().pools["simple_db"].users.len(), 1);

assert_eq!(
get_config().pools["sharded"].shards["0"].servers[0].0,
get_config().pools["sharded_db"].shards["0"].servers[0].0,
"127.0.0.1"
);
assert_eq!(
get_config().pools["sharded"].shards["1"].servers[0].2,
get_config().pools["sharded_db"].shards["1"].servers[0].2,
"primary"
);
assert_eq!(get_config().pools["sharded"].shards["1"].database, "shard1");
assert_eq!(
get_config().pools["sharded"].users["0"].username,
get_config().pools["sharded_db"].shards["1"].database,
"shard1"
);
assert_eq!(
get_config().pools["sharded_db"].users["0"].username,
"sharding_user"
);
assert_eq!(
get_config().pools["sharded"].users["1"].password,
get_config().pools["sharded_db"].users["1"].password,
"other_user"
);
assert_eq!(get_config().pools["sharded"].users["1"].pool_size, 21);
assert_eq!(get_config().pools["sharded"].default_role, "any");
assert_eq!(get_config().pools["sharded_db"].users["1"].pool_size, 21);
assert_eq!(get_config().pools["sharded_db"].default_role, "any");

assert_eq!(
get_config().pools["simple_db"].shards["0"].servers[0].0,
Expand Down
39 changes: 35 additions & 4 deletions tests/sharding/query_routing_setup.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

DROP DATABASE IF EXISTS shard0;
DROP DATABASE IF EXISTS shard1;
DROP DATABASE IF EXISTS shard2;
DROP DATABASE IF EXISTS some_db;

CREATE DATABASE shard0;
CREATE DATABASE shard1;
CREATE DATABASE shard2;
CREATE DATABASE some_db;

\c shard0

Expand Down Expand Up @@ -41,21 +42,51 @@ CREATE TABLE data (

CREATE TABLE data_shard_2 PARTITION OF data FOR VALUES WITH (MODULUS 3, REMAINDER 2);


\c some_db

DROP TABLE IF EXISTS data CASCADE;

CREATE TABLE data (
id BIGINT,
value VARCHAR
);

DROP ROLE IF EXISTS sharding_user;
DROP ROLE IF EXISTS other_user;
DROP ROLE IF EXISTS simple_user;
CREATE ROLE sharding_user ENCRYPTED PASSWORD 'sharding_user' LOGIN;
CREATE ROLE other_user ENCRYPTED PASSWORD 'other_user' LOGIN;
CREATE ROLE simple_user ENCRYPTED PASSWORD 'simple_user' LOGIN;

GRANT CONNECT ON DATABASE shard0 TO sharding_user;
GRANT CONNECT ON DATABASE shard1 TO sharding_user;
GRANT CONNECT ON DATABASE shard2 TO sharding_user;
GRANT CONNECT ON DATABASE shard0 TO sharding_user;
GRANT CONNECT ON DATABASE shard1 TO sharding_user;
GRANT CONNECT ON DATABASE shard2 TO sharding_user;

GRANT CONNECT ON DATABASE shard0 TO other_user;
GRANT CONNECT ON DATABASE shard1 TO other_user;
GRANT CONNECT ON DATABASE shard2 TO other_user;

GRANT CONNECT ON DATABASE some_db TO simple_user;

\c shard0
GRANT ALL ON SCHEMA public TO sharding_user;
GRANT ALL ON TABLE data TO sharding_user;
GRANT ALL ON SCHEMA public TO other_user;
GRANT ALL ON TABLE data TO other_user;

\c shard1
GRANT ALL ON SCHEMA public TO sharding_user;
GRANT ALL ON TABLE data TO sharding_user;
GRANT ALL ON SCHEMA public TO other_user;
GRANT ALL ON TABLE data TO other_user;

\c shard2
GRANT ALL ON SCHEMA public TO sharding_user;
GRANT ALL ON TABLE data TO sharding_user;
GRANT ALL ON SCHEMA public TO other_user;
GRANT ALL ON TABLE data TO other_user;

\c some_db
GRANT ALL ON SCHEMA public TO simple_user;
GRANT ALL ON TABLE data TO simple_user;