mirror of
https://github.com/postgresml/pgcat.git
synced 2026-03-24 17:56:29 +00:00
Compare commits
1 Commits
v0.1.0-bet
...
query-rout
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba57a6896f |
@@ -29,6 +29,9 @@ healthcheck_timeout = 100
|
|||||||
# For how long to ban a server if it fails a health check (seconds).
|
# For how long to ban a server if it fails a health check (seconds).
|
||||||
ban_time = 60 # Seconds
|
ban_time = 60 # Seconds
|
||||||
|
|
||||||
|
# Stats will be sent here
|
||||||
|
statsd_address = "127.0.0.1:8125"
|
||||||
|
|
||||||
#
|
#
|
||||||
# User to use for authentication against the server.
|
# User to use for authentication against the server.
|
||||||
[user]
|
[user]
|
||||||
|
|||||||
@@ -13,9 +13,6 @@ function start_pgcat() {
|
|||||||
|
|
||||||
# Setup the database with shards and user
|
# Setup the database with shards and user
|
||||||
psql -e -h 127.0.0.1 -p 5432 -U postgres -f tests/sharding/query_routing_setup.sql
|
psql -e -h 127.0.0.1 -p 5432 -U postgres -f tests/sharding/query_routing_setup.sql
|
||||||
PGPASSWORD=sharding_user pgbench -h 127.0.0.1 -U sharding_user shard0 -i
|
|
||||||
PGPASSWORD=sharding_user pgbench -h 127.0.0.1 -U sharding_user shard1 -i
|
|
||||||
PGPASSWORD=sharding_user pgbench -h 127.0.0.1 -U sharding_user shard2 -i
|
|
||||||
|
|
||||||
# Install Toxiproxy to simulate a downed/slow database
|
# Install Toxiproxy to simulate a downed/slow database
|
||||||
wget -O toxiproxy-2.1.4.deb https://github.com/Shopify/toxiproxy/releases/download/v2.1.4/toxiproxy_2.1.4_amd64.deb
|
wget -O toxiproxy-2.1.4.deb https://github.com/Shopify/toxiproxy/releases/download/v2.1.4/toxiproxy_2.1.4_amd64.deb
|
||||||
@@ -31,9 +28,9 @@ toxiproxy-cli create -l 127.0.0.1:5433 -u 127.0.0.1:5432 postgres_replica
|
|||||||
start_pgcat "info"
|
start_pgcat "info"
|
||||||
|
|
||||||
# pgbench test
|
# pgbench test
|
||||||
pgbench -i -h 127.0.0.1 -p 6432
|
pgbench -i -h 127.0.0.1 -p 6432 && \
|
||||||
pgbench -h 127.0.0.1 -p 6432 -t 500 -c 2 --protocol simple -f tests/pgbench/simple.sql
|
pgbench -h 127.0.0.1 -p 6432 -t 500 -c 2 --protocol simple && \
|
||||||
pgbench -h 127.0.0.1 -p 6432 -t 500 -c 2 --protocol extended
|
pgbench -h 127.0.0.1 -p 6432 -t 500 -c 2 --protocol extended
|
||||||
|
|
||||||
# COPY TO STDOUT test
|
# COPY TO STDOUT test
|
||||||
psql -h 127.0.0.1 -p 6432 -c 'COPY (SELECT * FROM pgbench_accounts LIMIT 15) TO STDOUT;' > /dev/null
|
psql -h 127.0.0.1 -p 6432 -c 'COPY (SELECT * FROM pgbench_accounts LIMIT 15) TO STDOUT;' > /dev/null
|
||||||
@@ -60,17 +57,6 @@ cd tests/ruby && \
|
|||||||
ruby tests.rb && \
|
ruby tests.rb && \
|
||||||
cd ../..
|
cd ../..
|
||||||
|
|
||||||
# Admin tests
|
|
||||||
psql -e -h 127.0.0.1 -p 6432 -d pgbouncer -c 'SHOW STATS' > /dev/null
|
|
||||||
psql -h 127.0.0.1 -p 6432 -d pgbouncer -c 'RELOAD' > /dev/null
|
|
||||||
psql -h 127.0.0.1 -p 6432 -d pgbouncer -c 'SHOW CONFIG' > /dev/null
|
|
||||||
psql -h 127.0.0.1 -p 6432 -d pgbouncer -c 'SHOW DATABASES' > /dev/null
|
|
||||||
psql -h 127.0.0.1 -p 6432 -d pgbouncer -c 'SHOW LISTS' > /dev/null
|
|
||||||
psql -h 127.0.0.1 -p 6432 -d pgbouncer -c 'SHOW POOLS' > /dev/null
|
|
||||||
psql -h 127.0.0.1 -p 6432 -d pgbouncer -c 'SHOW VERSION' > /dev/null
|
|
||||||
psql -h 127.0.0.1 -p 6432 -d pgbouncer -c "SET client_encoding TO 'utf8'" > /dev/null # will ignore
|
|
||||||
(! psql -e -h 127.0.0.1 -p 6432 -d random_db -c 'SHOW STATS' > /dev/null)
|
|
||||||
|
|
||||||
# Start PgCat in debug to demonstrate failover better
|
# Start PgCat in debug to demonstrate failover better
|
||||||
start_pgcat "debug"
|
start_pgcat "debug"
|
||||||
|
|
||||||
|
|||||||
12
Cargo.lock
generated
12
Cargo.lock
generated
@@ -352,7 +352,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pgcat"
|
name = "pgcat"
|
||||||
version = "0.1.0-beta2"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arc-swap",
|
"arc-swap",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
@@ -371,6 +371,7 @@ dependencies = [
|
|||||||
"serde_derive",
|
"serde_derive",
|
||||||
"sha-1",
|
"sha-1",
|
||||||
"sqlparser",
|
"sqlparser",
|
||||||
|
"statsd",
|
||||||
"tokio",
|
"tokio",
|
||||||
"toml",
|
"toml",
|
||||||
]
|
]
|
||||||
@@ -541,6 +542,15 @@ dependencies = [
|
|||||||
"log",
|
"log",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "statsd"
|
||||||
|
version = "0.15.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "df1efceb4bf2c0b5ebec94354285a43bbbed1375605bdf2ebe4132299434a330"
|
||||||
|
dependencies = [
|
||||||
|
"rand",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.86"
|
version = "1.0.86"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "pgcat"
|
name = "pgcat"
|
||||||
version = "0.1.0-beta2"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
@@ -20,6 +20,7 @@ serde_derive = "1"
|
|||||||
regex = "1"
|
regex = "1"
|
||||||
num_cpus = "1"
|
num_cpus = "1"
|
||||||
once_cell = "1"
|
once_cell = "1"
|
||||||
|
statsd = "0.15"
|
||||||
sqlparser = "0.14"
|
sqlparser = "0.14"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
arc-swap = "1"
|
arc-swap = "1"
|
||||||
|
|||||||
@@ -8,4 +8,4 @@ COPY --from=builder /app/target/release/pgcat /usr/bin/pgcat
|
|||||||
COPY --from=builder /app/pgcat.toml /etc/pgcat/pgcat.toml
|
COPY --from=builder /app/pgcat.toml /etc/pgcat/pgcat.toml
|
||||||
WORKDIR /etc/pgcat
|
WORKDIR /etc/pgcat
|
||||||
ENV RUST_LOG=info
|
ENV RUST_LOG=info
|
||||||
CMD ["pgcat"]
|
ENTRYPOINT ["/usr/bin/pgcat"]
|
||||||
|
|||||||
33
README.md
33
README.md
@@ -4,9 +4,9 @@
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
PostgreSQL pooler (like PgBouncer) with sharding, load balancing and failover support.
|
Meow. PgBouncer rewritten in Rust, with sharding, load balancing and failover support.
|
||||||
|
|
||||||
**Beta**: looking for beta testers, see [#35](https://github.com/levkk/pgcat/issues/35).
|
**Alpha**: looking for alpha testers, see [#35](https://github.com/levkk/pgcat/issues/35).
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
| **Feature** | **Status** | **Comments** |
|
| **Feature** | **Status** | **Comments** |
|
||||||
@@ -18,23 +18,14 @@ PostgreSQL pooler (like PgBouncer) with sharding, load balancing and failover su
|
|||||||
| Load balancing of read queries | :white_check_mark: | Using round-robin between replicas. Primary is included when `primary_reads_enabled` is enabled (default). |
|
| Load balancing of read queries | :white_check_mark: | Using round-robin between replicas. Primary is included when `primary_reads_enabled` is enabled (default). |
|
||||||
| Sharding | :white_check_mark: | Transactions are sharded using `SET SHARD TO` and `SET SHARDING KEY TO` syntax extensions; see examples below. |
|
| Sharding | :white_check_mark: | Transactions are sharded using `SET SHARD TO` and `SET SHARDING KEY TO` syntax extensions; see examples below. |
|
||||||
| Failover | :white_check_mark: | Replicas are tested with a health check. If a health check fails, remaining replicas are attempted; see below for algorithm description and examples. |
|
| Failover | :white_check_mark: | Replicas are tested with a health check. If a health check fails, remaining replicas are attempted; see below for algorithm description and examples. |
|
||||||
| Statistics | :white_check_mark: | Statistics available in the admin database (`pgcat` and `pgbouncer`) with `SHOW STATS`, `SHOW POOLS` and others. |
|
| Statistics reporting | :white_check_mark: | Statistics similar to PgBouncers are reported via StatsD. |
|
||||||
| Live configuration reloading | :white_check_mark: | Reload supported settings with a `SIGHUP` to the process, e.g. `kill -s SIGHUP $(pgrep pgcat)` or `RELOAD` query issued to the admin database. |
|
| Live configuration reloading | :construction_worker: | Reload config with a `SIGHUP` to the process, e.g. `kill -s SIGHUP $(pgrep pgcat)`. Not all settings can be reloaded without a restart. |
|
||||||
| Client authentication | :x: :wrench: | On the roadmap; currently all clients are allowed to connect and one user is used to connect to Postgres. |
|
| Client authentication | :x: :wrench: | On the roadmap; currently all clients are allowed to connect and one user is used to connect to Postgres. |
|
||||||
| Admin database | :white_check_mark: | The admin database, similar to PgBouncer's, allows to query for statistics and reload the configuration. |
|
|
||||||
|
|
||||||
## Deployment
|
## Deployment
|
||||||
|
|
||||||
See `Dockerfile` for example deployment using Docker. The pooler is configured to spawn 4 workers so 4 CPUs are recommended for optimal performance. That setting can be adjusted to spawn as many (or as little) workers as needed.
|
See `Dockerfile` for example deployment using Docker. The pooler is configured to spawn 4 workers so 4 CPUs are recommended for optimal performance.
|
||||||
|
That setting can be adjusted to spawn as many (or as little) workers as needed.
|
||||||
For quick local example, use the Docker Compose environment provided:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
docker-compose up
|
|
||||||
|
|
||||||
# In a new terminal:
|
|
||||||
psql -h 127.0.0.1 -p 6432 -c 'SELECT 1'
|
|
||||||
```
|
|
||||||
|
|
||||||
### Config
|
### Config
|
||||||
|
|
||||||
@@ -48,6 +39,7 @@ psql -h 127.0.0.1 -p 6432 -c 'SELECT 1'
|
|||||||
| `connect_timeout` | Maximum time to establish a connection to a server (milliseconds). If reached, the server is banned and the next target is attempted. | `5000` |
|
| `connect_timeout` | Maximum time to establish a connection to a server (milliseconds). If reached, the server is banned and the next target is attempted. | `5000` |
|
||||||
| `healthcheck_timeout` | Maximum time to pass a health check (`SELECT 1`, milliseconds). If reached, the server is banned and the next target is attempted. | `1000` |
|
| `healthcheck_timeout` | Maximum time to pass a health check (`SELECT 1`, milliseconds). If reached, the server is banned and the next target is attempted. | `1000` |
|
||||||
| `ban_time` | Ban time for a server (seconds). It won't be allowed to serve transactions until the ban expires; failover targets will be used instead. | `60` |
|
| `ban_time` | Ban time for a server (seconds). It won't be allowed to serve transactions until the ban expires; failover targets will be used instead. | `60` |
|
||||||
|
| `statsd_address` | StatsD host and port. Statistics will be sent there every 15 seconds. | `127.0.0.1:8125` |
|
||||||
| | | |
|
| | | |
|
||||||
| **`user`** | | |
|
| **`user`** | | |
|
||||||
| `name` | The user name. | `sharding_user` |
|
| `name` | The user name. | `sharding_user` |
|
||||||
@@ -90,7 +82,7 @@ See [sharding README](./tests/sharding/README.md) for sharding logic testing.
|
|||||||
| Load balancing | :white_check_mark: | :white_check_mark: | We could test this by emitting statistics for each replica and compare them. |
|
| Load balancing | :white_check_mark: | :white_check_mark: | We could test this by emitting statistics for each replica and compare them. |
|
||||||
| Failover | :white_check_mark: | :white_check_mark: | Misconfigure a replica in `pgcat.toml` and watch it forward queries to spares. CI testing is using Toxiproxy. |
|
| Failover | :white_check_mark: | :white_check_mark: | Misconfigure a replica in `pgcat.toml` and watch it forward queries to spares. CI testing is using Toxiproxy. |
|
||||||
| Sharding | :white_check_mark: | :white_check_mark: | See `tests/sharding` and `tests/ruby` for an Rails/ActiveRecord example. |
|
| Sharding | :white_check_mark: | :white_check_mark: | See `tests/sharding` and `tests/ruby` for an Rails/ActiveRecord example. |
|
||||||
| Statistics | :white_check_mark: | :white_check_mark: | Query the admin database with `psql -h 127.0.0.1 -p 6432 -d pgbouncer -c 'SHOW STATS'`. |
|
| Statistics reporting | :x: | :white_check_mark: | Run `nc -l -u 8125` and watch the stats come in every 15 seconds. |
|
||||||
| Live config reloading | :white_check_mark: | :white_check_mark: | Run `kill -s SIGHUP $(pgrep pgcat)` and watch the config reload. |
|
| Live config reloading | :white_check_mark: | :white_check_mark: | Run `kill -s SIGHUP $(pgrep pgcat)` and watch the config reload. |
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -233,15 +225,11 @@ SELECT * FROM users WHERE email = 'test@example.com'; -- shard setting lasts unt
|
|||||||
|
|
||||||
### Statistics reporting
|
### Statistics reporting
|
||||||
|
|
||||||
The stats are very similar to what Pgbouncer reports and the names are kept to be comparable. They are accessible by querying the admin database `pgcat`, and `pgbouncer` for compatibility.
|
Stats are reported using StatsD every 15 seconds. The address is configurable with `statsd_address`, the default is `127.0.0.1:8125`. The stats are very similar to what Pgbouncer reports and the names are kept to be comparable.
|
||||||
|
|
||||||
```
|
|
||||||
psql -h 127.0.0.1 -p 6432 -d pgbouncer -c 'SHOW DATABASES'
|
|
||||||
```
|
|
||||||
|
|
||||||
### Live configuration reloading
|
### Live configuration reloading
|
||||||
|
|
||||||
The config can be reloaded by sending a `kill -s SIGHUP` to the process or by querying `RELOAD` to the admin database. Not all settings are currently supported by live reload:
|
The config can be reloaded by sending a `kill -s SIGHUP` to the process. Not all settings are currently supported by live reload:
|
||||||
|
|
||||||
| **Config** | **Requires restart** |
|
| **Config** | **Requires restart** |
|
||||||
|-------------------------|----------------------|
|
|-------------------------|----------------------|
|
||||||
@@ -251,6 +239,7 @@ The config can be reloaded by sending a `kill -s SIGHUP` to the process or by qu
|
|||||||
| `connect_timeout` | yes |
|
| `connect_timeout` | yes |
|
||||||
| `healthcheck_timeout` | no |
|
| `healthcheck_timeout` | no |
|
||||||
| `ban_time` | no |
|
| `ban_time` | no |
|
||||||
|
| `statsd_address` | yes |
|
||||||
| `user` | yes |
|
| `user` | yes |
|
||||||
| `shards` | yes |
|
| `shards` | yes |
|
||||||
| `default_role` | no |
|
| `default_role` | no |
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
version: "3"
|
|
||||||
services:
|
|
||||||
postgres:
|
|
||||||
image: postgres:13
|
|
||||||
environment:
|
|
||||||
POSTGRES_PASSWORD: postgres
|
|
||||||
POSTGRES_HOST_AUTH_METHOD: md5
|
|
||||||
pgcat:
|
|
||||||
build: .
|
|
||||||
command:
|
|
||||||
- "pgcat"
|
|
||||||
- "/etc/pgcat/pgcat.toml"
|
|
||||||
volumes:
|
|
||||||
- "${PWD}/examples/docker/pgcat.toml:/etc/pgcat/pgcat.toml"
|
|
||||||
ports:
|
|
||||||
- "6432:6432"
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
#
|
|
||||||
# PgCat config example.
|
|
||||||
#
|
|
||||||
|
|
||||||
#
|
|
||||||
# General pooler settings
|
|
||||||
[general]
|
|
||||||
|
|
||||||
# What IP to run on, 0.0.0.0 means accessible from everywhere.
|
|
||||||
host = "0.0.0.0"
|
|
||||||
|
|
||||||
# Port to run on, same as PgBouncer used in this example.
|
|
||||||
port = 6432
|
|
||||||
|
|
||||||
# How many connections to allocate per server.
|
|
||||||
pool_size = 15
|
|
||||||
|
|
||||||
# Pool mode (see PgBouncer docs for more).
|
|
||||||
# session: one server connection per connected client
|
|
||||||
# transaction: one server connection per client transaction
|
|
||||||
pool_mode = "transaction"
|
|
||||||
|
|
||||||
# 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).
|
|
||||||
healthcheck_timeout = 1000
|
|
||||||
|
|
||||||
# For how long to ban a server if it fails a health check (seconds).
|
|
||||||
ban_time = 60 # Seconds
|
|
||||||
|
|
||||||
#
|
|
||||||
# User to use for authentication against the server.
|
|
||||||
[user]
|
|
||||||
name = "postgres"
|
|
||||||
password = "postgres"
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# Shards in the cluster
|
|
||||||
[shards]
|
|
||||||
|
|
||||||
# Shard 0
|
|
||||||
[shards.0]
|
|
||||||
|
|
||||||
# [ host, port, role ]
|
|
||||||
servers = [
|
|
||||||
[ "postgres", 5432, "primary" ],
|
|
||||||
[ "postgres", 5432, "replica" ],
|
|
||||||
# [ "127.0.1.1", 5432, "replica" ],
|
|
||||||
]
|
|
||||||
# Database name (e.g. "postgres")
|
|
||||||
database = "postgres"
|
|
||||||
|
|
||||||
[shards.1]
|
|
||||||
# [ host, port, role ]
|
|
||||||
servers = [
|
|
||||||
[ "postgres", 5432, "primary" ],
|
|
||||||
[ "postgres", 5432, "replica" ],
|
|
||||||
# [ "127.0.1.1", 5432, "replica" ],
|
|
||||||
]
|
|
||||||
database = "postgres"
|
|
||||||
|
|
||||||
[shards.2]
|
|
||||||
# [ host, port, role ]
|
|
||||||
servers = [
|
|
||||||
[ "postgres", 5432, "primary" ],
|
|
||||||
[ "postgres", 5432, "replica" ],
|
|
||||||
# [ "127.0.1.1", 5432, "replica" ],
|
|
||||||
]
|
|
||||||
database = "postgres"
|
|
||||||
|
|
||||||
|
|
||||||
# Settings for our query routing layer.
|
|
||||||
[query_router]
|
|
||||||
|
|
||||||
# If the client doesn't specify, route traffic to
|
|
||||||
# this role by default.
|
|
||||||
#
|
|
||||||
# any: round-robin between primary and replicas,
|
|
||||||
# replica: round-robin between replicas only without touching the primary,
|
|
||||||
# primary: all queries go to the primary unless otherwise specified.
|
|
||||||
default_role = "any"
|
|
||||||
|
|
||||||
|
|
||||||
# Query parser. If enabled, we'll attempt to parse
|
|
||||||
# every incoming query to determine if it's a read or a write.
|
|
||||||
# If it's a read query, we'll direct it to a replica. Otherwise, if it's a write,
|
|
||||||
# we'll direct it to the primary.
|
|
||||||
query_parser_enabled = false
|
|
||||||
|
|
||||||
# If the query parser is enabled and this setting is enabled, the primary will be part of the pool of databases used for
|
|
||||||
# load balancing of read queries. Otherwise, the primary will only be used for write
|
|
||||||
# queries. The primary can always be explicitely selected with our custom protocol.
|
|
||||||
primary_reads_enabled = true
|
|
||||||
|
|
||||||
# So what if you wanted to implement a different hashing function,
|
|
||||||
# or you've already built one and you want this pooler to use it?
|
|
||||||
#
|
|
||||||
# Current options:
|
|
||||||
#
|
|
||||||
# pg_bigint_hash: PARTITION BY HASH (Postgres hashing function)
|
|
||||||
# sha1: A hashing function based on SHA1
|
|
||||||
#
|
|
||||||
sharding_function = "pg_bigint_hash"
|
|
||||||
@@ -29,6 +29,9 @@ healthcheck_timeout = 1000
|
|||||||
# For how long to ban a server if it fails a health check (seconds).
|
# For how long to ban a server if it fails a health check (seconds).
|
||||||
ban_time = 60 # Seconds
|
ban_time = 60 # Seconds
|
||||||
|
|
||||||
|
# Stats will be sent here
|
||||||
|
statsd_address = "127.0.0.1:8125"
|
||||||
|
|
||||||
#
|
#
|
||||||
# User to use for authentication against the server.
|
# User to use for authentication against the server.
|
||||||
[user]
|
[user]
|
||||||
|
|||||||
388
src/admin.rs
388
src/admin.rs
@@ -1,388 +0,0 @@
|
|||||||
/// Admin database.
|
|
||||||
use bytes::{Buf, BufMut, BytesMut};
|
|
||||||
use log::{info, trace};
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use tokio::net::tcp::OwnedWriteHalf;
|
|
||||||
|
|
||||||
use crate::config::{get_config, parse};
|
|
||||||
use crate::errors::Error;
|
|
||||||
use crate::messages::*;
|
|
||||||
use crate::pool::ConnectionPool;
|
|
||||||
use crate::stats::get_stats;
|
|
||||||
|
|
||||||
/// Handle admin client.
|
|
||||||
pub async fn handle_admin(
|
|
||||||
stream: &mut OwnedWriteHalf,
|
|
||||||
mut query: BytesMut,
|
|
||||||
pool: ConnectionPool,
|
|
||||||
) -> Result<(), Error> {
|
|
||||||
let code = query.get_u8() as char;
|
|
||||||
|
|
||||||
if code != 'Q' {
|
|
||||||
return Err(Error::ProtocolSyncError);
|
|
||||||
}
|
|
||||||
|
|
||||||
let len = query.get_i32() as usize;
|
|
||||||
let query = String::from_utf8_lossy(&query[..len - 5])
|
|
||||||
.to_string()
|
|
||||||
.to_ascii_uppercase();
|
|
||||||
|
|
||||||
trace!("Admin query: {}", query);
|
|
||||||
|
|
||||||
if query.starts_with("SHOW STATS") {
|
|
||||||
trace!("SHOW STATS");
|
|
||||||
show_stats(stream, &pool).await
|
|
||||||
} else if query.starts_with("RELOAD") {
|
|
||||||
trace!("RELOAD");
|
|
||||||
reload(stream).await
|
|
||||||
} else if query.starts_with("SHOW CONFIG") {
|
|
||||||
trace!("SHOW CONFIG");
|
|
||||||
show_config(stream).await
|
|
||||||
} else if query.starts_with("SHOW DATABASES") {
|
|
||||||
trace!("SHOW DATABASES");
|
|
||||||
show_databases(stream, &pool).await
|
|
||||||
} else if query.starts_with("SHOW POOLS") {
|
|
||||||
trace!("SHOW POOLS");
|
|
||||||
show_pools(stream, &pool).await
|
|
||||||
} else if query.starts_with("SHOW LISTS") {
|
|
||||||
trace!("SHOW LISTS");
|
|
||||||
show_lists(stream, &pool).await
|
|
||||||
} else if query.starts_with("SHOW VERSION") {
|
|
||||||
trace!("SHOW VERSION");
|
|
||||||
show_version(stream).await
|
|
||||||
} else if query.starts_with("SET ") {
|
|
||||||
trace!("SET");
|
|
||||||
ignore_set(stream).await
|
|
||||||
} else {
|
|
||||||
error_response(stream, "Unsupported query against the admin database").await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Column-oriented statistics.
|
|
||||||
async fn show_lists(stream: &mut OwnedWriteHalf, pool: &ConnectionPool) -> Result<(), Error> {
|
|
||||||
let stats = get_stats();
|
|
||||||
|
|
||||||
let columns = vec![("list", DataType::Text), ("items", DataType::Int4)];
|
|
||||||
|
|
||||||
let mut res = BytesMut::new();
|
|
||||||
res.put(row_description(&columns));
|
|
||||||
res.put(data_row(&vec![
|
|
||||||
"databases".to_string(),
|
|
||||||
(pool.databases() + 1).to_string(), // see comment below
|
|
||||||
]));
|
|
||||||
res.put(data_row(&vec!["users".to_string(), "1".to_string()]));
|
|
||||||
res.put(data_row(&vec![
|
|
||||||
"pools".to_string(),
|
|
||||||
(pool.databases() + 1).to_string(), // +1 for the pgbouncer admin db pool which isn't real
|
|
||||||
])); // but admin tools that work with pgbouncer want this
|
|
||||||
res.put(data_row(&vec![
|
|
||||||
"free_clients".to_string(),
|
|
||||||
stats
|
|
||||||
.keys()
|
|
||||||
.map(|address_id| stats[&address_id]["cl_idle"])
|
|
||||||
.sum::<i64>()
|
|
||||||
.to_string(),
|
|
||||||
]));
|
|
||||||
res.put(data_row(&vec![
|
|
||||||
"used_clients".to_string(),
|
|
||||||
stats
|
|
||||||
.keys()
|
|
||||||
.map(|address_id| stats[&address_id]["cl_active"])
|
|
||||||
.sum::<i64>()
|
|
||||||
.to_string(),
|
|
||||||
]));
|
|
||||||
res.put(data_row(&vec![
|
|
||||||
"login_clients".to_string(),
|
|
||||||
"0".to_string(),
|
|
||||||
]));
|
|
||||||
res.put(data_row(&vec![
|
|
||||||
"free_servers".to_string(),
|
|
||||||
stats
|
|
||||||
.keys()
|
|
||||||
.map(|address_id| stats[&address_id]["sv_idle"])
|
|
||||||
.sum::<i64>()
|
|
||||||
.to_string(),
|
|
||||||
]));
|
|
||||||
res.put(data_row(&vec![
|
|
||||||
"used_servers".to_string(),
|
|
||||||
stats
|
|
||||||
.keys()
|
|
||||||
.map(|address_id| stats[&address_id]["sv_active"])
|
|
||||||
.sum::<i64>()
|
|
||||||
.to_string(),
|
|
||||||
]));
|
|
||||||
res.put(data_row(&vec!["dns_names".to_string(), "0".to_string()]));
|
|
||||||
res.put(data_row(&vec!["dns_zones".to_string(), "0".to_string()]));
|
|
||||||
res.put(data_row(&vec!["dns_queries".to_string(), "0".to_string()]));
|
|
||||||
res.put(data_row(&vec!["dns_pending".to_string(), "0".to_string()]));
|
|
||||||
|
|
||||||
res.put(command_complete("SHOW"));
|
|
||||||
|
|
||||||
res.put_u8(b'Z');
|
|
||||||
res.put_i32(5);
|
|
||||||
res.put_u8(b'I');
|
|
||||||
|
|
||||||
write_all_half(stream, res).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Show PgCat version.
|
|
||||||
async fn show_version(stream: &mut OwnedWriteHalf) -> Result<(), Error> {
|
|
||||||
let mut res = BytesMut::new();
|
|
||||||
|
|
||||||
res.put(row_description(&vec![("version", DataType::Text)]));
|
|
||||||
res.put(data_row(&vec!["PgCat 0.1.0".to_string()]));
|
|
||||||
res.put(command_complete("SHOW"));
|
|
||||||
|
|
||||||
res.put_u8(b'Z');
|
|
||||||
res.put_i32(5);
|
|
||||||
res.put_u8(b'I');
|
|
||||||
|
|
||||||
write_all_half(stream, res).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Show utilization of connection pools for each shard and replicas.
|
|
||||||
async fn show_pools(stream: &mut OwnedWriteHalf, pool: &ConnectionPool) -> Result<(), Error> {
|
|
||||||
let stats = get_stats();
|
|
||||||
let config = {
|
|
||||||
let guard = get_config();
|
|
||||||
&*guard.clone()
|
|
||||||
};
|
|
||||||
|
|
||||||
let columns = vec![
|
|
||||||
("database", DataType::Text),
|
|
||||||
("user", DataType::Text),
|
|
||||||
("cl_active", DataType::Numeric),
|
|
||||||
("cl_waiting", DataType::Numeric),
|
|
||||||
("cl_cancel_req", DataType::Numeric),
|
|
||||||
("sv_active", DataType::Numeric),
|
|
||||||
("sv_idle", DataType::Numeric),
|
|
||||||
("sv_used", DataType::Numeric),
|
|
||||||
("sv_tested", DataType::Numeric),
|
|
||||||
("sv_login", DataType::Numeric),
|
|
||||||
("maxwait", DataType::Numeric),
|
|
||||||
("maxwait_us", DataType::Numeric),
|
|
||||||
("pool_mode", DataType::Text),
|
|
||||||
];
|
|
||||||
|
|
||||||
let mut res = BytesMut::new();
|
|
||||||
res.put(row_description(&columns));
|
|
||||||
|
|
||||||
for shard in 0..pool.shards() {
|
|
||||||
for server in 0..pool.servers(shard) {
|
|
||||||
let address = pool.address(shard, server);
|
|
||||||
let stats = match stats.get(&address.id) {
|
|
||||||
Some(stats) => stats.clone(),
|
|
||||||
None => HashMap::new(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut row = vec![address.name(), config.user.name.clone()];
|
|
||||||
|
|
||||||
for column in &columns[2..columns.len() - 1] {
|
|
||||||
let value = stats.get(column.0).unwrap_or(&0).to_string();
|
|
||||||
row.push(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
row.push(config.general.pool_mode.to_string());
|
|
||||||
res.put(data_row(&row));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res.put(command_complete("SHOW"));
|
|
||||||
|
|
||||||
// ReadyForQuery
|
|
||||||
res.put_u8(b'Z');
|
|
||||||
res.put_i32(5);
|
|
||||||
res.put_u8(b'I');
|
|
||||||
|
|
||||||
write_all_half(stream, res).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Show shards and replicas.
|
|
||||||
async fn show_databases(stream: &mut OwnedWriteHalf, pool: &ConnectionPool) -> Result<(), Error> {
|
|
||||||
let guard = get_config();
|
|
||||||
let config = &*guard.clone();
|
|
||||||
drop(guard);
|
|
||||||
|
|
||||||
// Columns
|
|
||||||
let columns = vec![
|
|
||||||
("name", DataType::Text),
|
|
||||||
("host", DataType::Text),
|
|
||||||
("port", DataType::Text),
|
|
||||||
("database", DataType::Text),
|
|
||||||
("force_user", DataType::Text),
|
|
||||||
("pool_size", DataType::Int4),
|
|
||||||
("min_pool_size", DataType::Int4),
|
|
||||||
("reserve_pool", DataType::Int4),
|
|
||||||
("pool_mode", DataType::Text),
|
|
||||||
("max_connections", DataType::Int4),
|
|
||||||
("current_connections", DataType::Int4),
|
|
||||||
("paused", DataType::Int4),
|
|
||||||
("disabled", DataType::Int4),
|
|
||||||
];
|
|
||||||
|
|
||||||
let mut res = BytesMut::new();
|
|
||||||
|
|
||||||
res.put(row_description(&columns));
|
|
||||||
|
|
||||||
for shard in 0..pool.shards() {
|
|
||||||
let database_name = &config.shards[&shard.to_string()].database;
|
|
||||||
|
|
||||||
for server in 0..pool.servers(shard) {
|
|
||||||
let address = pool.address(shard, server);
|
|
||||||
let pool_state = pool.pool_state(shard, server);
|
|
||||||
|
|
||||||
res.put(data_row(&vec![
|
|
||||||
address.name(), // name
|
|
||||||
address.host.to_string(), // host
|
|
||||||
address.port.to_string(), // port
|
|
||||||
database_name.to_string(), // database
|
|
||||||
config.user.name.to_string(), // force_user
|
|
||||||
config.general.pool_size.to_string(), // pool_size
|
|
||||||
"0".to_string(), // min_pool_size
|
|
||||||
"0".to_string(), // reserve_pool
|
|
||||||
config.general.pool_mode.to_string(), // pool_mode
|
|
||||||
config.general.pool_size.to_string(), // max_connections
|
|
||||||
pool_state.connections.to_string(), // current_connections
|
|
||||||
"0".to_string(), // paused
|
|
||||||
"0".to_string(), // disabled
|
|
||||||
]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res.put(command_complete("SHOW"));
|
|
||||||
|
|
||||||
// ReadyForQuery
|
|
||||||
res.put_u8(b'Z');
|
|
||||||
res.put_i32(5);
|
|
||||||
res.put_u8(b'I');
|
|
||||||
|
|
||||||
write_all_half(stream, res).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Ignore any SET commands the client sends.
|
|
||||||
/// This is common initialization done by ORMs.
|
|
||||||
async fn ignore_set(stream: &mut OwnedWriteHalf) -> Result<(), Error> {
|
|
||||||
custom_protocol_response_ok(stream, "SET").await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Reload the configuration file without restarting the process.
|
|
||||||
async fn reload(stream: &mut OwnedWriteHalf) -> Result<(), Error> {
|
|
||||||
info!("Reloading config");
|
|
||||||
|
|
||||||
let config = get_config();
|
|
||||||
let path = config.path.clone().unwrap();
|
|
||||||
|
|
||||||
parse(&path).await?;
|
|
||||||
|
|
||||||
let config = get_config();
|
|
||||||
|
|
||||||
config.show();
|
|
||||||
|
|
||||||
let mut res = BytesMut::new();
|
|
||||||
|
|
||||||
res.put(command_complete("RELOAD"));
|
|
||||||
|
|
||||||
// ReadyForQuery
|
|
||||||
res.put_u8(b'Z');
|
|
||||||
res.put_i32(5);
|
|
||||||
res.put_u8(b'I');
|
|
||||||
|
|
||||||
write_all_half(stream, res).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Shows current configuration.
|
|
||||||
async fn show_config(stream: &mut OwnedWriteHalf) -> Result<(), Error> {
|
|
||||||
let guard = get_config();
|
|
||||||
let config = &*guard.clone();
|
|
||||||
let config: HashMap<String, String> = config.into();
|
|
||||||
drop(guard);
|
|
||||||
|
|
||||||
// Configs that cannot be changed without restarting.
|
|
||||||
let immutables = ["host", "port", "connect_timeout"];
|
|
||||||
|
|
||||||
// Columns
|
|
||||||
let columns = vec![
|
|
||||||
("key", DataType::Text),
|
|
||||||
("value", DataType::Text),
|
|
||||||
("default", DataType::Text),
|
|
||||||
("changeable", DataType::Text),
|
|
||||||
];
|
|
||||||
|
|
||||||
// Response data
|
|
||||||
let mut res = BytesMut::new();
|
|
||||||
res.put(row_description(&columns));
|
|
||||||
|
|
||||||
// DataRow rows
|
|
||||||
for (key, value) in config {
|
|
||||||
let changeable = if immutables.iter().filter(|col| *col == &key).count() == 1 {
|
|
||||||
"no".to_string()
|
|
||||||
} else {
|
|
||||||
"yes".to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
let row = vec![key, value, "-".to_string(), changeable];
|
|
||||||
|
|
||||||
res.put(data_row(&row));
|
|
||||||
}
|
|
||||||
|
|
||||||
res.put(command_complete("SHOW"));
|
|
||||||
|
|
||||||
// ReadyForQuery
|
|
||||||
res.put_u8(b'Z');
|
|
||||||
res.put_i32(5);
|
|
||||||
res.put_u8(b'I');
|
|
||||||
|
|
||||||
write_all_half(stream, res).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Show shard and replicas statistics.
|
|
||||||
async fn show_stats(stream: &mut OwnedWriteHalf, pool: &ConnectionPool) -> Result<(), Error> {
|
|
||||||
let columns = vec![
|
|
||||||
("database", DataType::Text),
|
|
||||||
("total_xact_count", DataType::Numeric),
|
|
||||||
("total_query_count", DataType::Numeric),
|
|
||||||
("total_received", DataType::Numeric),
|
|
||||||
("total_sent", DataType::Numeric),
|
|
||||||
("total_xact_time", DataType::Numeric),
|
|
||||||
("total_query_time", DataType::Numeric),
|
|
||||||
("total_wait_time", DataType::Numeric),
|
|
||||||
("avg_xact_count", DataType::Numeric),
|
|
||||||
("avg_query_count", DataType::Numeric),
|
|
||||||
("avg_recv", DataType::Numeric),
|
|
||||||
("avg_sent", DataType::Numeric),
|
|
||||||
("avg_xact_time", DataType::Numeric),
|
|
||||||
("avg_query_time", DataType::Numeric),
|
|
||||||
("avg_wait_time", DataType::Numeric),
|
|
||||||
];
|
|
||||||
|
|
||||||
let stats = get_stats();
|
|
||||||
let mut res = BytesMut::new();
|
|
||||||
res.put(row_description(&columns));
|
|
||||||
|
|
||||||
for shard in 0..pool.shards() {
|
|
||||||
for server in 0..pool.servers(shard) {
|
|
||||||
let address = pool.address(shard, server);
|
|
||||||
let stats = match stats.get(&address.id) {
|
|
||||||
Some(stats) => stats.clone(),
|
|
||||||
None => HashMap::new(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut row = vec![address.name()];
|
|
||||||
|
|
||||||
for column in &columns[1..] {
|
|
||||||
row.push(stats.get(column.0).unwrap_or(&0).to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
res.put(data_row(&row));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res.put(command_complete("SHOW"));
|
|
||||||
|
|
||||||
// ReadyForQuery
|
|
||||||
res.put_u8(b'Z');
|
|
||||||
res.put_i32(5);
|
|
||||||
res.put_u8(b'I');
|
|
||||||
|
|
||||||
write_all_half(stream, res).await
|
|
||||||
}
|
|
||||||
187
src/client.rs
187
src/client.rs
@@ -1,14 +1,16 @@
|
|||||||
/// Handle clients by pretending to be a PostgreSQL server.
|
/// Implementation of the PostgreSQL client.
|
||||||
|
/// We are pretending to the server in this scenario,
|
||||||
|
/// and this module implements that.
|
||||||
use bytes::{Buf, BufMut, BytesMut};
|
use bytes::{Buf, BufMut, BytesMut};
|
||||||
use log::{debug, error, trace};
|
use log::{debug, error, trace};
|
||||||
use std::collections::HashMap;
|
|
||||||
use tokio::io::{AsyncReadExt, BufReader};
|
use tokio::io::{AsyncReadExt, BufReader};
|
||||||
use tokio::net::{
|
use tokio::net::{
|
||||||
tcp::{OwnedReadHalf, OwnedWriteHalf},
|
tcp::{OwnedReadHalf, OwnedWriteHalf},
|
||||||
TcpStream,
|
TcpStream,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::admin::handle_admin;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::config::get_config;
|
use crate::config::get_config;
|
||||||
use crate::constants::*;
|
use crate::constants::*;
|
||||||
use crate::errors::Error;
|
use crate::errors::Error;
|
||||||
@@ -20,52 +22,44 @@ use crate::stats::Reporter;
|
|||||||
|
|
||||||
/// The client state. One of these is created per client.
|
/// The client state. One of these is created per client.
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
/// The reads are buffered (8K by default).
|
// The reads are buffered (8K by default).
|
||||||
read: BufReader<OwnedReadHalf>,
|
read: BufReader<OwnedReadHalf>,
|
||||||
|
|
||||||
/// We buffer the writes ourselves because we know the protocol
|
// We buffer the writes ourselves because we know the protocol
|
||||||
/// better than a stock buffer.
|
// better than a stock buffer.
|
||||||
write: OwnedWriteHalf,
|
write: OwnedWriteHalf,
|
||||||
|
|
||||||
/// Internal buffer, where we place messages until we have to flush
|
// Internal buffer, where we place messages until we have to flush
|
||||||
/// them to the backend.
|
// them to the backend.
|
||||||
buffer: BytesMut,
|
buffer: BytesMut,
|
||||||
|
|
||||||
/// The client was started with the sole reason to cancel another running query.
|
// The client was started with the sole reason to cancel another running query.
|
||||||
cancel_mode: bool,
|
cancel_mode: bool,
|
||||||
|
|
||||||
/// In transaction mode, the connection is released after each transaction.
|
// In transaction mode, the connection is released after each transaction.
|
||||||
/// Session mode has slightly higher throughput per client, but lower capacity.
|
// Session mode has slightly higher throughput per client, but lower capacity.
|
||||||
transaction_mode: bool,
|
transaction_mode: bool,
|
||||||
|
|
||||||
/// For query cancellation, the client is given a random process ID and secret on startup.
|
// For query cancellation, the client is given a random process ID and secret on startup.
|
||||||
process_id: i32,
|
process_id: i32,
|
||||||
secret_key: i32,
|
secret_key: i32,
|
||||||
|
|
||||||
/// Clients are mapped to servers while they use them. This allows a client
|
// Clients are mapped to servers while they use them. This allows a client
|
||||||
/// to connect and cancel a query.
|
// to connect and cancel a query.
|
||||||
client_server_map: ClientServerMap,
|
client_server_map: ClientServerMap,
|
||||||
|
|
||||||
/// Client parameters, e.g. user, client_encoding, etc.
|
// Client parameters, e.g. user, client_encoding, etc.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
parameters: HashMap<String, String>,
|
parameters: HashMap<String, String>,
|
||||||
|
|
||||||
/// Statistics
|
// Statistics
|
||||||
stats: Reporter,
|
stats: Reporter,
|
||||||
|
|
||||||
/// Clients want to talk to admin database.
|
|
||||||
admin: bool,
|
|
||||||
|
|
||||||
/// Last address the client talked to.
|
|
||||||
last_address_id: Option<usize>,
|
|
||||||
|
|
||||||
/// Last server process id we talked to.
|
|
||||||
last_server_id: Option<i32>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
/// Perform client startup sequence.
|
/// Given a TCP socket, trick the client into thinking we are
|
||||||
/// See docs: <https://www.postgresql.org/docs/12/protocol-flow.html#id-1.10.5.7.3>
|
/// the Postgres server. Perform the authentication and place
|
||||||
|
/// the client in query-ready mode.
|
||||||
pub async fn startup(
|
pub async fn startup(
|
||||||
mut stream: TcpStream,
|
mut stream: TcpStream,
|
||||||
client_server_map: ClientServerMap,
|
client_server_map: ClientServerMap,
|
||||||
@@ -78,12 +72,14 @@ impl Client {
|
|||||||
loop {
|
loop {
|
||||||
trace!("Waiting for StartupMessage");
|
trace!("Waiting for StartupMessage");
|
||||||
|
|
||||||
// Could be StartupMessage, SSLRequest or CancelRequest.
|
// Could be StartupMessage or SSLRequest
|
||||||
|
// which makes this variable length.
|
||||||
let len = match stream.read_i32().await {
|
let len = match stream.read_i32().await {
|
||||||
Ok(len) => len,
|
Ok(len) => len,
|
||||||
Err(_) => return Err(Error::ClientBadStartup),
|
Err(_) => return Err(Error::ClientBadStartup),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Read whatever is left.
|
||||||
let mut startup = vec![0u8; len as usize - 4];
|
let mut startup = vec![0u8; len as usize - 4];
|
||||||
|
|
||||||
match stream.read_exact(&mut startup).await {
|
match stream.read_exact(&mut startup).await {
|
||||||
@@ -122,15 +118,6 @@ impl Client {
|
|||||||
ready_for_query(&mut stream).await?;
|
ready_for_query(&mut stream).await?;
|
||||||
trace!("Startup OK");
|
trace!("Startup OK");
|
||||||
|
|
||||||
let database = parameters
|
|
||||||
.get("database")
|
|
||||||
.unwrap_or(parameters.get("user").unwrap());
|
|
||||||
let admin = ["pgcat", "pgbouncer"]
|
|
||||||
.iter()
|
|
||||||
.filter(|db| *db == &database)
|
|
||||||
.count()
|
|
||||||
== 1;
|
|
||||||
|
|
||||||
// Split the read and write streams
|
// Split the read and write streams
|
||||||
// so we can control buffering.
|
// so we can control buffering.
|
||||||
let (read, write) = stream.into_split();
|
let (read, write) = stream.into_split();
|
||||||
@@ -146,9 +133,6 @@ impl Client {
|
|||||||
client_server_map: client_server_map,
|
client_server_map: client_server_map,
|
||||||
parameters: parameters,
|
parameters: parameters,
|
||||||
stats: stats,
|
stats: stats,
|
||||||
admin: admin,
|
|
||||||
last_address_id: None,
|
|
||||||
last_server_id: None,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,9 +154,6 @@ impl Client {
|
|||||||
client_server_map: client_server_map,
|
client_server_map: client_server_map,
|
||||||
parameters: HashMap::new(),
|
parameters: HashMap::new(),
|
||||||
stats: stats,
|
stats: stats,
|
||||||
admin: false,
|
|
||||||
last_address_id: None,
|
|
||||||
last_server_id: None,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,7 +164,7 @@ impl Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a connected and authenticated client.
|
/// Client loop. We handle all messages between the client and the database here.
|
||||||
pub async fn handle(&mut self, mut pool: ConnectionPool) -> Result<(), Error> {
|
pub async fn handle(&mut self, mut pool: ConnectionPool) -> Result<(), Error> {
|
||||||
// The client wants to cancel a query it has issued previously.
|
// The client wants to cancel a query it has issued previously.
|
||||||
if self.cancel_mode {
|
if self.cancel_mode {
|
||||||
@@ -219,14 +200,17 @@ impl Client {
|
|||||||
|
|
||||||
// Our custom protocol loop.
|
// Our custom protocol loop.
|
||||||
// We expect the client to either start a transaction with regular queries
|
// We expect the client to either start a transaction with regular queries
|
||||||
// or issue commands for our sharding and server selection protocol.
|
// or issue commands for our sharding and server selection protocols.
|
||||||
loop {
|
loop {
|
||||||
trace!("Client idle, waiting for message");
|
trace!("Client idle, waiting for message");
|
||||||
|
|
||||||
|
// Client idle, waiting for messages.
|
||||||
|
self.stats.client_idle(self.process_id);
|
||||||
|
|
||||||
// Read a complete message from the client, which normally would be
|
// Read a complete message from the client, which normally would be
|
||||||
// either a `Q` (query) or `P` (prepare, extended protocol).
|
// either a `Q` (query) or `P` (prepare, extended protocol).
|
||||||
// We can parse it here before grabbing a server from the pool,
|
// We can parse it here before grabbing a server from the pool,
|
||||||
// in case the client is sending some custom protocol messages, e.g.
|
// in case the client is sending some control messages, e.g.
|
||||||
// SET SHARDING KEY TO 'bigint';
|
// SET SHARDING KEY TO 'bigint';
|
||||||
let mut message = read_message(&mut self.read).await?;
|
let mut message = read_message(&mut self.read).await?;
|
||||||
|
|
||||||
@@ -236,48 +220,36 @@ impl Client {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle admin database queries.
|
// Handle all custom protocol commands here.
|
||||||
if self.admin {
|
|
||||||
trace!("Handling admin command");
|
|
||||||
handle_admin(&mut self.write, message, pool.clone()).await?;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle all custom protocol commands, if any.
|
|
||||||
match query_router.try_execute_command(message.clone()) {
|
match query_router.try_execute_command(message.clone()) {
|
||||||
// Normal query, not a custom command.
|
// Normal query
|
||||||
None => {
|
None => {
|
||||||
// Attempt to infer which server we want to query, i.e. primary or replica.
|
|
||||||
if query_router.query_parser_enabled() && query_router.role() == None {
|
if query_router.query_parser_enabled() && query_router.role() == None {
|
||||||
query_router.infer_role(message.clone());
|
query_router.infer_role(message.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SET SHARD TO
|
|
||||||
Some((Command::SetShard, _)) => {
|
Some((Command::SetShard, _)) => {
|
||||||
custom_protocol_response_ok(&mut self.write, "SET SHARD").await?;
|
custom_protocol_response_ok(&mut self.write, &format!("SET SHARD")).await?;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// SET SHARDING KEY TO
|
|
||||||
Some((Command::SetShardingKey, _)) => {
|
Some((Command::SetShardingKey, _)) => {
|
||||||
custom_protocol_response_ok(&mut self.write, "SET SHARDING KEY").await?;
|
custom_protocol_response_ok(&mut self.write, &format!("SET SHARDING KEY"))
|
||||||
|
.await?;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// SET SERVER ROLE TO
|
|
||||||
Some((Command::SetServerRole, _)) => {
|
Some((Command::SetServerRole, _)) => {
|
||||||
custom_protocol_response_ok(&mut self.write, "SET SERVER ROLE").await?;
|
custom_protocol_response_ok(&mut self.write, "SET SERVER ROLE").await?;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// SHOW SERVER ROLE
|
|
||||||
Some((Command::ShowServerRole, value)) => {
|
Some((Command::ShowServerRole, value)) => {
|
||||||
show_response(&mut self.write, "server role", &value).await?;
|
show_response(&mut self.write, "server role", &value).await?;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// SHOW SHARD
|
|
||||||
Some((Command::ShowShard, value)) => {
|
Some((Command::ShowShard, value)) => {
|
||||||
show_response(&mut self.write, "shard", &value).await?;
|
show_response(&mut self.write, "shard", &value).await?;
|
||||||
continue;
|
continue;
|
||||||
@@ -289,7 +261,7 @@ impl Client {
|
|||||||
error_response(
|
error_response(
|
||||||
&mut self.write,
|
&mut self.write,
|
||||||
&format!(
|
&format!(
|
||||||
"shard {} is more than configured {}",
|
"shard '{}' is more than configured '{}'",
|
||||||
query_router.shard(),
|
query_router.shard(),
|
||||||
pool.shards()
|
pool.shards()
|
||||||
),
|
),
|
||||||
@@ -298,13 +270,13 @@ impl Client {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Waiting for server connection.
|
||||||
|
self.stats.client_waiting(self.process_id);
|
||||||
|
|
||||||
debug!("Waiting for connection from pool");
|
debug!("Waiting for connection from pool");
|
||||||
|
|
||||||
// Grab a server from the pool.
|
// Grab a server from the pool: the client issued a regular query.
|
||||||
let connection = match pool
|
let connection = match pool.get(query_router.shard(), query_router.role()).await {
|
||||||
.get(query_router.shard(), query_router.role(), self.process_id)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(conn) => {
|
Ok(conn) => {
|
||||||
debug!("Got connection from pool");
|
debug!("Got connection from pool");
|
||||||
conn
|
conn
|
||||||
@@ -318,23 +290,15 @@ impl Client {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut reference = connection.0;
|
let mut reference = connection.0;
|
||||||
let address = connection.1;
|
let _address = connection.1;
|
||||||
let server = &mut *reference;
|
let server = &mut *reference;
|
||||||
|
|
||||||
// Server is assigned to the client in case the client wants to
|
// Claim this server as mine for query cancellation.
|
||||||
// cancel a query later.
|
|
||||||
server.claim(self.process_id, self.secret_key);
|
server.claim(self.process_id, self.secret_key);
|
||||||
|
|
||||||
// Update statistics.
|
// Client active & server active
|
||||||
if let Some(last_address_id) = self.last_address_id {
|
self.stats.client_active(self.process_id);
|
||||||
self.stats
|
self.stats.server_active(server.process_id());
|
||||||
.client_disconnecting(self.process_id, last_address_id);
|
|
||||||
}
|
|
||||||
self.stats.client_active(self.process_id, address.id);
|
|
||||||
self.stats.server_active(server.process_id(), address.id);
|
|
||||||
|
|
||||||
self.last_address_id = Some(address.id);
|
|
||||||
self.last_server_id = Some(server.process_id());
|
|
||||||
|
|
||||||
debug!(
|
debug!(
|
||||||
"Client {:?} talking to server {:?}",
|
"Client {:?} talking to server {:?}",
|
||||||
@@ -345,9 +309,6 @@ impl Client {
|
|||||||
// Transaction loop. Multiple queries can be issued by the client here.
|
// Transaction loop. Multiple queries can be issued by the client here.
|
||||||
// The connection belongs to the client until the transaction is over,
|
// The connection belongs to the client until the transaction is over,
|
||||||
// or until the client disconnects if we are in session mode.
|
// or until the client disconnects if we are in session mode.
|
||||||
//
|
|
||||||
// If the client is in session mode, no more custom protocol
|
|
||||||
// commands will be accepted.
|
|
||||||
loop {
|
loop {
|
||||||
let mut message = if message.len() == 0 {
|
let mut message = if message.len() == 0 {
|
||||||
trace!("Waiting for message inside transaction or in session mode");
|
trace!("Waiting for message inside transaction or in session mode");
|
||||||
@@ -355,12 +316,11 @@ impl Client {
|
|||||||
match read_message(&mut self.read).await {
|
match read_message(&mut self.read).await {
|
||||||
Ok(message) => message,
|
Ok(message) => message,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
// Client disconnected inside a transaction.
|
// Client disconnected without warning.
|
||||||
// Clean up the server and re-use it.
|
|
||||||
// This prevents connection thrashing by bad clients.
|
|
||||||
if server.in_transaction() {
|
if server.in_transaction() {
|
||||||
server.query("ROLLBACK").await?;
|
// Client left dirty server. Clean up and proceed
|
||||||
server.query("DISCARD ALL").await?;
|
// without thrashing this connection.
|
||||||
|
server.query("ROLLBACK; DISCARD ALL;").await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Err(err);
|
return Err(err);
|
||||||
@@ -386,11 +346,13 @@ impl Client {
|
|||||||
'Q' => {
|
'Q' => {
|
||||||
debug!("Sending query to server");
|
debug!("Sending query to server");
|
||||||
|
|
||||||
|
// TODO: implement retries here for read-only transactions.
|
||||||
server.send(original).await?;
|
server.send(original).await?;
|
||||||
|
|
||||||
// Read all data the server has to offer, which can be multiple messages
|
// Read all data the server has to offer, which can be multiple messages
|
||||||
// buffered in 8196 bytes chunks.
|
// buffered in 8196 bytes chunks.
|
||||||
loop {
|
loop {
|
||||||
|
// TODO: implement retries here for read-only transactions.
|
||||||
let response = server.recv().await?;
|
let response = server.recv().await?;
|
||||||
|
|
||||||
// Send server reply to the client.
|
// Send server reply to the client.
|
||||||
@@ -408,16 +370,17 @@ impl Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Report query executed statistics.
|
// Report query executed statistics.
|
||||||
self.stats.query(self.process_id, address.id);
|
self.stats.query();
|
||||||
|
|
||||||
|
// The transaction is over, we can release the connection back to the pool.
|
||||||
if !server.in_transaction() {
|
if !server.in_transaction() {
|
||||||
// Report transaction executed statistics.
|
// Report transaction executed statistics.
|
||||||
self.stats.transaction(self.process_id, address.id);
|
self.stats.transaction();
|
||||||
|
|
||||||
// Release server back to the pool if we are in transaction mode.
|
// Release server back to the pool if we are in transaction mode.
|
||||||
// If we are in session mode, we keep the server until the client disconnects.
|
// If we are in session mode, we keep the server until the client disconnects.
|
||||||
if self.transaction_mode {
|
if self.transaction_mode {
|
||||||
self.stats.server_idle(server.process_id(), address.id);
|
self.stats.server_idle(server.process_id());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -429,13 +392,11 @@ impl Client {
|
|||||||
// connection before releasing into the pool.
|
// connection before releasing into the pool.
|
||||||
// Pgbouncer closes the connection which leads to
|
// Pgbouncer closes the connection which leads to
|
||||||
// connection thrashing when clients misbehave.
|
// connection thrashing when clients misbehave.
|
||||||
|
// This pool will protect the database. :salute:
|
||||||
if server.in_transaction() {
|
if server.in_transaction() {
|
||||||
server.query("ROLLBACK").await?;
|
server.query("ROLLBACK; DISCARD ALL;").await?;
|
||||||
server.query("DISCARD ALL").await?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.release();
|
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -470,6 +431,7 @@ impl Client {
|
|||||||
|
|
||||||
self.buffer.put(&original[..]);
|
self.buffer.put(&original[..]);
|
||||||
|
|
||||||
|
// TODO: retries for read-only transactions.
|
||||||
server.send(self.buffer.clone()).await?;
|
server.send(self.buffer.clone()).await?;
|
||||||
|
|
||||||
self.buffer.clear();
|
self.buffer.clear();
|
||||||
@@ -477,6 +439,7 @@ impl Client {
|
|||||||
// Read all data the server has to offer, which can be multiple messages
|
// Read all data the server has to offer, which can be multiple messages
|
||||||
// buffered in 8196 bytes chunks.
|
// buffered in 8196 bytes chunks.
|
||||||
loop {
|
loop {
|
||||||
|
// TODO: retries for read-only transactions
|
||||||
let response = server.recv().await?;
|
let response = server.recv().await?;
|
||||||
|
|
||||||
match write_all_half(&mut self.write, response).await {
|
match write_all_half(&mut self.write, response).await {
|
||||||
@@ -493,15 +456,15 @@ impl Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Report query executed statistics.
|
// Report query executed statistics.
|
||||||
self.stats.query(self.process_id, address.id);
|
self.stats.query();
|
||||||
|
|
||||||
|
// Release server back to the pool if we are in transaction mode.
|
||||||
|
// If we are in session mode, we keep the server until the client disconnects.
|
||||||
if !server.in_transaction() {
|
if !server.in_transaction() {
|
||||||
self.stats.transaction(self.process_id, address.id);
|
self.stats.transaction();
|
||||||
|
|
||||||
// Release server back to the pool if we are in transaction mode.
|
|
||||||
// If we are in session mode, we keep the server until the client disconnects.
|
|
||||||
if self.transaction_mode {
|
if self.transaction_mode {
|
||||||
self.stats.server_idle(server.process_id(), address.id);
|
self.stats.server_idle(server.process_id());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -529,13 +492,13 @@ impl Client {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Release server back to the pool if we are in transaction mode.
|
||||||
|
// If we are in session mode, we keep the server until the client disconnects.
|
||||||
if !server.in_transaction() {
|
if !server.in_transaction() {
|
||||||
self.stats.transaction(self.process_id, address.id);
|
self.stats.transaction();
|
||||||
|
|
||||||
// Release server back to the pool if we are in transaction mode.
|
|
||||||
// If we are in session mode, we keep the server until the client disconnects.
|
|
||||||
if self.transaction_mode {
|
if self.transaction_mode {
|
||||||
self.stats.server_idle(server.process_id(), address.id);
|
self.stats.server_idle(server.process_id());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -552,11 +515,10 @@ impl Client {
|
|||||||
// The server is no longer bound to us, we can't cancel it's queries anymore.
|
// The server is no longer bound to us, we can't cancel it's queries anymore.
|
||||||
debug!("Releasing server back into the pool");
|
debug!("Releasing server back into the pool");
|
||||||
self.release();
|
self.release();
|
||||||
self.stats.client_idle(self.process_id, address.id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Release the server from the client: it can't cancel its queries anymore.
|
/// Release the server from being mine. I can't cancel its queries anymore.
|
||||||
pub fn release(&self) {
|
pub fn release(&self) {
|
||||||
let mut guard = self.client_server_map.lock();
|
let mut guard = self.client_server_map.lock();
|
||||||
guard.remove(&(self.process_id, self.secret_key));
|
guard.remove(&(self.process_id, self.secret_key));
|
||||||
@@ -565,13 +527,6 @@ impl Client {
|
|||||||
|
|
||||||
impl Drop for Client {
|
impl Drop for Client {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
// Update statistics.
|
self.stats.client_disconnecting(self.process_id);
|
||||||
if let Some(address_id) = self.last_address_id {
|
|
||||||
self.stats.client_disconnecting(self.process_id, address_id);
|
|
||||||
|
|
||||||
if let Some(process_id) = self.last_server_id {
|
|
||||||
self.stats.server_idle(process_id, address_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,35 +1,24 @@
|
|||||||
/// Parse the configuration file.
|
|
||||||
use arc_swap::{ArcSwap, Guard};
|
use arc_swap::{ArcSwap, Guard};
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
use std::collections::{HashMap, HashSet};
|
|
||||||
use std::sync::Arc;
|
|
||||||
use tokio::fs::File;
|
use tokio::fs::File;
|
||||||
use tokio::io::AsyncReadExt;
|
use tokio::io::AsyncReadExt;
|
||||||
use toml;
|
use toml;
|
||||||
|
|
||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::errors::Error;
|
use crate::errors::Error;
|
||||||
|
|
||||||
/// Globally available configuration.
|
|
||||||
static CONFIG: Lazy<ArcSwap<Config>> = Lazy::new(|| ArcSwap::from_pointee(Config::default()));
|
static CONFIG: Lazy<ArcSwap<Config>> = Lazy::new(|| ArcSwap::from_pointee(Config::default()));
|
||||||
|
|
||||||
/// Server role: primary or replica.
|
|
||||||
#[derive(Clone, PartialEq, Deserialize, Hash, std::cmp::Eq, Debug, Copy)]
|
#[derive(Clone, PartialEq, Deserialize, Hash, std::cmp::Eq, Debug, Copy)]
|
||||||
pub enum Role {
|
pub enum Role {
|
||||||
Primary,
|
Primary,
|
||||||
Replica,
|
Replica,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for Role {
|
|
||||||
fn to_string(&self) -> String {
|
|
||||||
match *self {
|
|
||||||
Role::Primary => "primary".to_string(),
|
|
||||||
Role::Replica => "replica".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq<Option<Role>> for Role {
|
impl PartialEq<Option<Role>> for Role {
|
||||||
fn eq(&self, other: &Option<Role>) -> bool {
|
fn eq(&self, other: &Option<Role>) -> bool {
|
||||||
match other {
|
match other {
|
||||||
@@ -48,42 +37,25 @@ impl PartialEq<Role> for Option<Role> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Address identifying a PostgreSQL server uniquely.
|
|
||||||
#[derive(Clone, PartialEq, Hash, std::cmp::Eq, Debug)]
|
#[derive(Clone, PartialEq, Hash, std::cmp::Eq, Debug)]
|
||||||
pub struct Address {
|
pub struct Address {
|
||||||
pub id: usize,
|
|
||||||
pub host: String,
|
pub host: String,
|
||||||
pub port: String,
|
pub port: String,
|
||||||
pub shard: usize,
|
pub shard: usize,
|
||||||
pub role: Role,
|
pub role: Role,
|
||||||
pub replica_number: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Address {
|
impl Default for Address {
|
||||||
fn default() -> Address {
|
fn default() -> Address {
|
||||||
Address {
|
Address {
|
||||||
id: 0,
|
|
||||||
host: String::from("127.0.0.1"),
|
host: String::from("127.0.0.1"),
|
||||||
port: String::from("5432"),
|
port: String::from("5432"),
|
||||||
shard: 0,
|
shard: 0,
|
||||||
replica_number: 0,
|
|
||||||
role: Role::Replica,
|
role: Role::Replica,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Address {
|
|
||||||
/// Address name (aka database) used in `SHOW STATS`, `SHOW DATABASES`, and `SHOW POOLS`.
|
|
||||||
pub fn name(&self) -> String {
|
|
||||||
match self.role {
|
|
||||||
Role::Primary => format!("shard_{}_primary", self.shard),
|
|
||||||
|
|
||||||
Role::Replica => format!("shard_{}_replica_{}", self.shard, self.replica_number),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// PostgreSQL user.
|
|
||||||
#[derive(Clone, PartialEq, Hash, std::cmp::Eq, Deserialize, Debug)]
|
#[derive(Clone, PartialEq, Hash, std::cmp::Eq, Deserialize, Debug)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@@ -99,7 +71,6 @@ impl Default for User {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// General configuration.
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct General {
|
pub struct General {
|
||||||
pub host: String,
|
pub host: String,
|
||||||
@@ -109,6 +80,7 @@ pub struct General {
|
|||||||
pub connect_timeout: u64,
|
pub connect_timeout: u64,
|
||||||
pub healthcheck_timeout: u64,
|
pub healthcheck_timeout: u64,
|
||||||
pub ban_time: i64,
|
pub ban_time: i64,
|
||||||
|
pub statsd_address: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for General {
|
impl Default for General {
|
||||||
@@ -121,11 +93,11 @@ impl Default for General {
|
|||||||
connect_timeout: 5000,
|
connect_timeout: 5000,
|
||||||
healthcheck_timeout: 1000,
|
healthcheck_timeout: 1000,
|
||||||
ban_time: 60,
|
ban_time: 60,
|
||||||
|
statsd_address: String::from("127.0.0.1:8125"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shard configuration.
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct Shard {
|
pub struct Shard {
|
||||||
pub servers: Vec<(String, u16, String)>,
|
pub servers: Vec<(String, u16, String)>,
|
||||||
@@ -141,7 +113,6 @@ impl Default for Shard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Query Router configuration.
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct QueryRouter {
|
pub struct QueryRouter {
|
||||||
pub default_role: String,
|
pub default_role: String,
|
||||||
@@ -161,10 +132,8 @@ impl Default for QueryRouter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Configuration wrapper.
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub path: Option<String>,
|
|
||||||
pub general: General,
|
pub general: General,
|
||||||
pub user: User,
|
pub user: User,
|
||||||
pub shards: HashMap<String, Shard>,
|
pub shards: HashMap<String, Shard>,
|
||||||
@@ -174,7 +143,6 @@ pub struct Config {
|
|||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
fn default() -> Config {
|
fn default() -> Config {
|
||||||
Config {
|
Config {
|
||||||
path: Some(String::from("pgcat.toml")),
|
|
||||||
general: General::default(),
|
general: General::default(),
|
||||||
user: User::default(),
|
user: User::default(),
|
||||||
shards: HashMap::from([(String::from("1"), Shard::default())]),
|
shards: HashMap::from([(String::from("1"), Shard::default())]),
|
||||||
@@ -183,50 +151,7 @@ impl Default for Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&Config> for std::collections::HashMap<String, String> {
|
|
||||||
fn from(config: &Config) -> HashMap<String, String> {
|
|
||||||
HashMap::from([
|
|
||||||
("host".to_string(), config.general.host.to_string()),
|
|
||||||
("port".to_string(), config.general.port.to_string()),
|
|
||||||
(
|
|
||||||
"pool_size".to_string(),
|
|
||||||
config.general.pool_size.to_string(),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"pool_mode".to_string(),
|
|
||||||
config.general.pool_mode.to_string(),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"connect_timeout".to_string(),
|
|
||||||
config.general.connect_timeout.to_string(),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"healthcheck_timeout".to_string(),
|
|
||||||
config.general.healthcheck_timeout.to_string(),
|
|
||||||
),
|
|
||||||
("ban_time".to_string(), config.general.ban_time.to_string()),
|
|
||||||
(
|
|
||||||
"default_role".to_string(),
|
|
||||||
config.query_router.default_role.to_string(),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"query_parser_enabled".to_string(),
|
|
||||||
config.query_router.query_parser_enabled.to_string(),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"primary_reads_enabled".to_string(),
|
|
||||||
config.query_router.primary_reads_enabled.to_string(),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"sharding_function".to_string(),
|
|
||||||
config.query_router.sharding_function.to_string(),
|
|
||||||
),
|
|
||||||
])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
/// Print current configuration.
|
|
||||||
pub fn show(&self) {
|
pub fn show(&self) {
|
||||||
info!("Pool size: {}", self.general.pool_size);
|
info!("Pool size: {}", self.general.pool_size);
|
||||||
info!("Pool mode: {}", self.general.pool_mode);
|
info!("Pool mode: {}", self.general.pool_mode);
|
||||||
@@ -241,14 +166,11 @@ impl Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a read-only instance of the configuration
|
|
||||||
/// from anywhere in the app.
|
|
||||||
/// ArcSwap makes this cheap and quick.
|
|
||||||
pub fn get_config() -> Guard<Arc<Config>> {
|
pub fn get_config() -> Guard<Arc<Config>> {
|
||||||
CONFIG.load()
|
CONFIG.load()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse the configuration file located at the path.
|
/// Parse the config.
|
||||||
pub async fn parse(path: &str) -> Result<(), Error> {
|
pub async fn parse(path: &str) -> Result<(), Error> {
|
||||||
let mut contents = String::new();
|
let mut contents = String::new();
|
||||||
let mut file = match File::open(path).await {
|
let mut file = match File::open(path).await {
|
||||||
@@ -267,7 +189,7 @@ pub async fn parse(path: &str) -> Result<(), Error> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut config: Config = match toml::from_str(&contents) {
|
let config: Config = match toml::from_str(&contents) {
|
||||||
Ok(config) => config,
|
Ok(config) => config,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Could not parse config file: {}", err.to_string());
|
error!("Could not parse config file: {}", err.to_string());
|
||||||
@@ -357,9 +279,6 @@ pub async fn parse(path: &str) -> Result<(), Error> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
config.path = Some(path.to_string());
|
|
||||||
|
|
||||||
// Update the configuration globally.
|
|
||||||
CONFIG.store(Arc::new(config.clone()));
|
CONFIG.store(Arc::new(config.clone()));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -377,6 +296,5 @@ mod test {
|
|||||||
assert_eq!(get_config().shards["1"].servers[0].0, "127.0.0.1");
|
assert_eq!(get_config().shards["1"].servers[0].0, "127.0.0.1");
|
||||||
assert_eq!(get_config().shards["0"].servers[0].2, "primary");
|
assert_eq!(get_config().shards["0"].servers[0].2, "primary");
|
||||||
assert_eq!(get_config().query_router.default_role, "any");
|
assert_eq!(get_config().query_router.default_role, "any");
|
||||||
assert_eq!(get_config().path, Some("pgcat.toml".to_string()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
/// Various protocol constants, as defined in
|
/// Various protocol constants, as defined in
|
||||||
/// <https://www.postgresql.org/docs/12/protocol-message-formats.html>
|
/// https://www.postgresql.org/docs/12/protocol-message-formats.html
|
||||||
/// and elsewhere in the source code.
|
/// and elsewhere in the source code.
|
||||||
|
/// Also other constants we use elsewhere.
|
||||||
|
|
||||||
// Used in the StartupMessage to indicate regular handshake.
|
// Used in the StartupMessage to indicate regular handshake.
|
||||||
pub const PROTOCOL_VERSION_NUMBER: i32 = 196608;
|
pub const PROTOCOL_VERSION_NUMBER: i32 = 196608;
|
||||||
@@ -19,8 +20,3 @@ pub const AUTHENTICATION_SUCCESSFUL: i32 = 0;
|
|||||||
|
|
||||||
// ErrorResponse: A code identifying the field type; if zero, this is the message terminator and no string follows.
|
// ErrorResponse: A code identifying the field type; if zero, this is the message terminator and no string follows.
|
||||||
pub const MESSAGE_TERMINATOR: u8 = 0;
|
pub const MESSAGE_TERMINATOR: u8 = 0;
|
||||||
|
|
||||||
//
|
|
||||||
// Data types
|
|
||||||
//
|
|
||||||
pub const _OID_INT8: i32 = 20; // bigint
|
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
/// Errors.
|
|
||||||
|
|
||||||
/// Various errors.
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
SocketError,
|
SocketError,
|
||||||
|
// ClientDisconnected,
|
||||||
ClientBadStartup,
|
ClientBadStartup,
|
||||||
ProtocolSyncError,
|
ProtocolSyncError,
|
||||||
ServerError,
|
ServerError,
|
||||||
|
// ServerTimeout,
|
||||||
|
// DirtyServer,
|
||||||
BadConfig,
|
BadConfig,
|
||||||
AllServersDown,
|
AllServersDown,
|
||||||
}
|
}
|
||||||
|
|||||||
48
src/main.rs
48
src/main.rs
@@ -31,6 +31,7 @@ extern crate once_cell;
|
|||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
extern crate sqlparser;
|
extern crate sqlparser;
|
||||||
|
extern crate statsd;
|
||||||
extern crate tokio;
|
extern crate tokio;
|
||||||
extern crate toml;
|
extern crate toml;
|
||||||
|
|
||||||
@@ -46,7 +47,6 @@ use tokio::{
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
mod admin;
|
|
||||||
mod client;
|
mod client;
|
||||||
mod config;
|
mod config;
|
||||||
mod constants;
|
mod constants;
|
||||||
@@ -58,15 +58,19 @@ mod server;
|
|||||||
mod sharding;
|
mod sharding;
|
||||||
mod stats;
|
mod stats;
|
||||||
|
|
||||||
|
// Support for query cancellation: this maps our process_ids and
|
||||||
|
// secret keys to the backend's.
|
||||||
use config::get_config;
|
use config::get_config;
|
||||||
use pool::{ClientServerMap, ConnectionPool};
|
use pool::{ClientServerMap, ConnectionPool};
|
||||||
use stats::{Collector, Reporter};
|
use stats::{Collector, Reporter};
|
||||||
|
|
||||||
|
/// Main!
|
||||||
#[tokio::main(worker_threads = 4)]
|
#[tokio::main(worker_threads = 4)]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
info!("Welcome to PgCat! Meow.");
|
info!("Welcome to PgCat! Meow.");
|
||||||
|
|
||||||
|
// Prepare regexes
|
||||||
if !query_router::QueryRouter::setup() {
|
if !query_router::QueryRouter::setup() {
|
||||||
error!("Could not setup query router");
|
error!("Could not setup query router");
|
||||||
return;
|
return;
|
||||||
@@ -80,6 +84,7 @@ async fn main() {
|
|||||||
String::from("pgcat.toml")
|
String::from("pgcat.toml")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Prepare the config
|
||||||
match config::parse(&config_file).await {
|
match config::parse(&config_file).await {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -89,8 +94,8 @@ async fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let config = get_config();
|
let config = get_config();
|
||||||
let addr = format!("{}:{}", config.general.host, config.general.port);
|
|
||||||
|
|
||||||
|
let addr = format!("{}:{}", config.general.host, config.general.port);
|
||||||
let listener = match TcpListener::bind(&addr).await {
|
let listener = match TcpListener::bind(&addr).await {
|
||||||
Ok(sock) => sock,
|
Ok(sock) => sock,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -100,28 +105,22 @@ async fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
info!("Running on {}", addr);
|
info!("Running on {}", addr);
|
||||||
|
|
||||||
config.show();
|
config.show();
|
||||||
|
|
||||||
// Tracks which client is connected to which server for query cancellation.
|
// Tracks which client is connected to which server for query cancellation.
|
||||||
let client_server_map: ClientServerMap = Arc::new(Mutex::new(HashMap::new()));
|
let client_server_map: ClientServerMap = Arc::new(Mutex::new(HashMap::new()));
|
||||||
|
|
||||||
// Statistics reporting.
|
// Collect statistics and send them to StatsD
|
||||||
let (tx, rx) = mpsc::channel(100);
|
let (tx, rx) = mpsc::channel(100);
|
||||||
|
let collector_tx = tx.clone();
|
||||||
|
tokio::task::spawn(async move {
|
||||||
|
let mut stats_collector = Collector::new(rx, collector_tx);
|
||||||
|
stats_collector.collect().await;
|
||||||
|
});
|
||||||
|
|
||||||
// Connection pool that allows to query all shards and replicas.
|
|
||||||
let mut pool =
|
let mut pool =
|
||||||
ConnectionPool::from_config(client_server_map.clone(), Reporter::new(tx.clone())).await;
|
ConnectionPool::from_config(client_server_map.clone(), Reporter::new(tx.clone())).await;
|
||||||
|
|
||||||
// Statistics collector task.
|
|
||||||
let collector_tx = tx.clone();
|
|
||||||
let addresses = pool.databases();
|
|
||||||
tokio::task::spawn(async move {
|
|
||||||
let mut stats_collector = Collector::new(rx, collector_tx);
|
|
||||||
stats_collector.collect(addresses).await;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Connect to all servers and validate their versions.
|
|
||||||
let server_info = match pool.validate().await {
|
let server_info = match pool.validate().await {
|
||||||
Ok(info) => info,
|
Ok(info) => info,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -132,7 +131,7 @@ async fn main() {
|
|||||||
|
|
||||||
info!("Waiting for clients");
|
info!("Waiting for clients");
|
||||||
|
|
||||||
// Client connection loop.
|
// Main app runs here.
|
||||||
tokio::task::spawn(async move {
|
tokio::task::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
let pool = pool.clone();
|
let pool = pool.clone();
|
||||||
@@ -148,7 +147,7 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle client.
|
// Client goes to another thread, bye.
|
||||||
tokio::task::spawn(async move {
|
tokio::task::spawn(async move {
|
||||||
let start = chrono::offset::Utc::now().naive_utc();
|
let start = chrono::offset::Utc::now().naive_utc();
|
||||||
match client::Client::startup(socket, client_server_map, server_info, reporter)
|
match client::Client::startup(socket, client_server_map, server_info, reporter)
|
||||||
@@ -182,7 +181,7 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Reload config:
|
// Reload config
|
||||||
// kill -SIGHUP $(pgrep pgcat)
|
// kill -SIGHUP $(pgrep pgcat)
|
||||||
tokio::task::spawn(async move {
|
tokio::task::spawn(async move {
|
||||||
let mut stream = unix_signal(SignalKind::hangup()).unwrap();
|
let mut stream = unix_signal(SignalKind::hangup()).unwrap();
|
||||||
@@ -202,15 +201,16 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Exit on Ctrl-C (SIGINT) and SIGTERM.
|
// Setup shut down sequence
|
||||||
let mut term_signal = unix_signal(SignalKind::terminate()).unwrap();
|
match signal::ctrl_c().await {
|
||||||
|
Ok(()) => {
|
||||||
|
info!("Shutting down...");
|
||||||
|
}
|
||||||
|
|
||||||
tokio::select! {
|
Err(err) => {
|
||||||
_ = signal::ctrl_c() => (),
|
error!("Unable to listen for shutdown signal: {}", err);
|
||||||
_ = term_signal.recv() => (),
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
info!("Shutting down...");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Format chrono::Duration to be more human-friendly.
|
/// Format chrono::Duration to be more human-friendly.
|
||||||
|
|||||||
166
src/messages.rs
166
src/messages.rs
@@ -8,26 +8,9 @@ use tokio::net::{
|
|||||||
TcpStream,
|
TcpStream,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::errors::Error;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
/// Postgres data type mappings
|
use crate::errors::Error;
|
||||||
/// used in RowDescription ('T') message.
|
|
||||||
pub enum DataType {
|
|
||||||
Text,
|
|
||||||
Int4,
|
|
||||||
Numeric,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<&DataType> for i32 {
|
|
||||||
fn from(data_type: &DataType) -> i32 {
|
|
||||||
match data_type {
|
|
||||||
DataType::Text => 25,
|
|
||||||
DataType::Int4 => 23,
|
|
||||||
DataType::Numeric => 1700,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Tell the client that authentication handshake completed successfully.
|
/// Tell the client that authentication handshake completed successfully.
|
||||||
pub async fn auth_ok(stream: &mut TcpStream) -> Result<(), Error> {
|
pub async fn auth_ok(stream: &mut TcpStream) -> Result<(), Error> {
|
||||||
@@ -108,8 +91,9 @@ pub async fn startup(stream: &mut TcpStream, user: &str, database: &str) -> Resu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse the params the server sends as a key/value format.
|
/// Parse StartupMessage parameters.
|
||||||
pub fn parse_params(mut bytes: BytesMut) -> Result<HashMap<String, String>, Error> {
|
/// e.g. user, database, application_name, etc.
|
||||||
|
pub fn parse_startup(mut bytes: BytesMut) -> Result<HashMap<String, String>, Error> {
|
||||||
let mut result = HashMap::new();
|
let mut result = HashMap::new();
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
let mut tmp = String::new();
|
let mut tmp = String::new();
|
||||||
@@ -131,7 +115,7 @@ pub fn parse_params(mut bytes: BytesMut) -> Result<HashMap<String, String>, Erro
|
|||||||
|
|
||||||
// Expect pairs of name and value
|
// Expect pairs of name and value
|
||||||
// and at least one pair to be present.
|
// and at least one pair to be present.
|
||||||
if buf.len() % 2 != 0 || buf.len() < 2 {
|
if buf.len() % 2 != 0 && buf.len() >= 2 {
|
||||||
return Err(Error::ClientBadStartup);
|
return Err(Error::ClientBadStartup);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,14 +127,6 @@ pub fn parse_params(mut bytes: BytesMut) -> Result<HashMap<String, String>, Erro
|
|||||||
i += 2;
|
i += 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parse StartupMessage parameters.
|
|
||||||
/// e.g. user, database, application_name, etc.
|
|
||||||
pub fn parse_startup(bytes: BytesMut) -> Result<HashMap<String, String>, Error> {
|
|
||||||
let result = parse_params(bytes)?;
|
|
||||||
|
|
||||||
// Minimum required parameters
|
// Minimum required parameters
|
||||||
// I want to have the user at the very minimum, according to the protocol spec.
|
// I want to have the user at the very minimum, according to the protocol spec.
|
||||||
if !result.contains_key("user") {
|
if !result.contains_key("user") {
|
||||||
@@ -222,7 +198,7 @@ pub async fn custom_protocol_response_ok(
|
|||||||
|
|
||||||
/// Send a custom error message to the client.
|
/// Send a custom error message to the client.
|
||||||
/// Tell the client we are ready for the next query and no rollback is necessary.
|
/// Tell the client we are ready for the next query and no rollback is necessary.
|
||||||
/// Docs on error codes: <https://www.postgresql.org/docs/12/errcodes-appendix.html>.
|
/// Docs on error codes: https://www.postgresql.org/docs/12/errcodes-appendix.html
|
||||||
pub async fn error_response(stream: &mut OwnedWriteHalf, message: &str) -> Result<(), Error> {
|
pub async fn error_response(stream: &mut OwnedWriteHalf, message: &str) -> Result<(), Error> {
|
||||||
let mut error = BytesMut::new();
|
let mut error = BytesMut::new();
|
||||||
|
|
||||||
@@ -276,17 +252,68 @@ pub async fn show_response(
|
|||||||
// 3. CommandComplete
|
// 3. CommandComplete
|
||||||
// 4. ReadyForQuery
|
// 4. ReadyForQuery
|
||||||
|
|
||||||
|
// RowDescription
|
||||||
|
let mut row_desc = BytesMut::new();
|
||||||
|
|
||||||
|
// Number of columns: 1
|
||||||
|
row_desc.put_i16(1);
|
||||||
|
|
||||||
|
// Column name
|
||||||
|
row_desc.put_slice(&format!("{}\0", name).as_bytes());
|
||||||
|
|
||||||
|
// Doesn't belong to any table
|
||||||
|
row_desc.put_i32(0);
|
||||||
|
|
||||||
|
// Doesn't belong to any table
|
||||||
|
row_desc.put_i16(0);
|
||||||
|
|
||||||
|
// Text
|
||||||
|
row_desc.put_i32(25);
|
||||||
|
|
||||||
|
// Text size = variable (-1)
|
||||||
|
row_desc.put_i16(-1);
|
||||||
|
|
||||||
|
// Type modifier: none that I know
|
||||||
|
row_desc.put_i32(0);
|
||||||
|
|
||||||
|
// Format being used: text (0), binary (1)
|
||||||
|
row_desc.put_i16(0);
|
||||||
|
|
||||||
|
// DataRow
|
||||||
|
let mut data_row = BytesMut::new();
|
||||||
|
|
||||||
|
// Number of columns
|
||||||
|
data_row.put_i16(1);
|
||||||
|
|
||||||
|
// Size of the column content (length of the string really)
|
||||||
|
data_row.put_i32(value.len() as i32);
|
||||||
|
|
||||||
|
// The content
|
||||||
|
data_row.put_slice(value.as_bytes());
|
||||||
|
|
||||||
|
// CommandComplete
|
||||||
|
let mut command_complete = BytesMut::new();
|
||||||
|
|
||||||
|
// Number of rows returned (just one)
|
||||||
|
command_complete.put_slice(&b"SELECT 1\0"[..]);
|
||||||
|
|
||||||
// The final messages sent to the client
|
// The final messages sent to the client
|
||||||
let mut res = BytesMut::new();
|
let mut res = BytesMut::new();
|
||||||
|
|
||||||
// RowDescription
|
// RowDescription
|
||||||
res.put(row_description(&vec![(name, DataType::Text)]));
|
res.put_u8(b'T');
|
||||||
|
res.put_i32(row_desc.len() as i32 + 4);
|
||||||
|
res.put(row_desc);
|
||||||
|
|
||||||
// DataRow
|
// DataRow
|
||||||
res.put(data_row(&vec![value.to_string()]));
|
res.put_u8(b'D');
|
||||||
|
res.put_i32(data_row.len() as i32 + 4);
|
||||||
|
res.put(data_row);
|
||||||
|
|
||||||
// CommandComplete
|
// CommandComplete
|
||||||
res.put(command_complete("SELECT 1"));
|
res.put_u8(b'C');
|
||||||
|
res.put_i32(command_complete.len() as i32 + 4);
|
||||||
|
res.put(command_complete);
|
||||||
|
|
||||||
// ReadyForQuery
|
// ReadyForQuery
|
||||||
res.put_u8(b'Z');
|
res.put_u8(b'Z');
|
||||||
@@ -296,79 +323,6 @@ pub async fn show_response(
|
|||||||
write_all_half(stream, res).await
|
write_all_half(stream, res).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn row_description(columns: &Vec<(&str, DataType)>) -> BytesMut {
|
|
||||||
let mut res = BytesMut::new();
|
|
||||||
let mut row_desc = BytesMut::new();
|
|
||||||
|
|
||||||
// how many colums we are storing
|
|
||||||
row_desc.put_i16(columns.len() as i16);
|
|
||||||
|
|
||||||
for (name, data_type) in columns {
|
|
||||||
// Column name
|
|
||||||
row_desc.put_slice(&format!("{}\0", name).as_bytes());
|
|
||||||
|
|
||||||
// Doesn't belong to any table
|
|
||||||
row_desc.put_i32(0);
|
|
||||||
|
|
||||||
// Doesn't belong to any table
|
|
||||||
row_desc.put_i16(0);
|
|
||||||
|
|
||||||
// Text
|
|
||||||
row_desc.put_i32(data_type.into());
|
|
||||||
|
|
||||||
// Text size = variable (-1)
|
|
||||||
let type_size = match data_type {
|
|
||||||
DataType::Text => -1,
|
|
||||||
DataType::Int4 => 4,
|
|
||||||
DataType::Numeric => -1,
|
|
||||||
};
|
|
||||||
|
|
||||||
row_desc.put_i16(type_size);
|
|
||||||
|
|
||||||
// Type modifier: none that I know
|
|
||||||
row_desc.put_i32(-1);
|
|
||||||
|
|
||||||
// Format being used: text (0), binary (1)
|
|
||||||
row_desc.put_i16(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.put_u8(b'T');
|
|
||||||
res.put_i32(row_desc.len() as i32 + 4);
|
|
||||||
res.put(row_desc);
|
|
||||||
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a DataRow message.
|
|
||||||
pub fn data_row(row: &Vec<String>) -> BytesMut {
|
|
||||||
let mut res = BytesMut::new();
|
|
||||||
let mut data_row = BytesMut::new();
|
|
||||||
|
|
||||||
data_row.put_i16(row.len() as i16);
|
|
||||||
|
|
||||||
for column in row {
|
|
||||||
let column = column.as_bytes();
|
|
||||||
data_row.put_i32(column.len() as i32);
|
|
||||||
data_row.put_slice(&column);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.put_u8(b'D');
|
|
||||||
res.put_i32(data_row.len() as i32 + 4);
|
|
||||||
res.put(data_row);
|
|
||||||
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a CommandComplete message.
|
|
||||||
pub fn command_complete(command: &str) -> BytesMut {
|
|
||||||
let cmd = BytesMut::from(format!("{}\0", command).as_bytes());
|
|
||||||
let mut res = BytesMut::new();
|
|
||||||
res.put_u8(b'C');
|
|
||||||
res.put_i32(cmd.len() as i32 + 4);
|
|
||||||
res.put(cmd);
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Write all data in the buffer to the TcpStream.
|
/// Write all data in the buffer to the TcpStream.
|
||||||
pub async fn write_all(stream: &mut TcpStream, buf: BytesMut) -> Result<(), Error> {
|
pub async fn write_all(stream: &mut TcpStream, buf: BytesMut) -> Result<(), Error> {
|
||||||
match stream.write_all(&buf).await {
|
match stream.write_all(&buf).await {
|
||||||
|
|||||||
121
src/pool.rs
121
src/pool.rs
@@ -1,23 +1,24 @@
|
|||||||
/// Pooling, failover and banlist.
|
/// Pooling and failover and banlist.
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use bb8::{ManageConnection, Pool, PooledConnection};
|
use bb8::{ManageConnection, Pool, PooledConnection};
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use chrono::naive::NaiveDateTime;
|
use chrono::naive::NaiveDateTime;
|
||||||
use log::{debug, error, info, warn};
|
use log::{debug, error, info, warn};
|
||||||
use parking_lot::{Mutex, RwLock};
|
use parking_lot::{Mutex, RwLock};
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
use crate::config::{get_config, Address, Role, User};
|
use crate::config::{get_config, Address, Role, User};
|
||||||
use crate::errors::Error;
|
use crate::errors::Error;
|
||||||
use crate::server::Server;
|
use crate::server::Server;
|
||||||
use crate::stats::Reporter;
|
use crate::stats::Reporter;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
// Banlist: bad servers go in here.
|
||||||
pub type BanList = Arc<RwLock<Vec<HashMap<Address, NaiveDateTime>>>>;
|
pub type BanList = Arc<RwLock<Vec<HashMap<Address, NaiveDateTime>>>>;
|
||||||
pub type ClientServerMap = Arc<Mutex<HashMap<(i32, i32), (i32, i32, String, String)>>>;
|
pub type ClientServerMap = Arc<Mutex<HashMap<(i32, i32), (i32, i32, String, String)>>>;
|
||||||
|
|
||||||
/// The globally accessible connection pool.
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct ConnectionPool {
|
pub struct ConnectionPool {
|
||||||
databases: Vec<Vec<Pool<ServerPool>>>,
|
databases: Vec<Vec<Pool<ServerPool>>>,
|
||||||
@@ -28,7 +29,7 @@ pub struct ConnectionPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ConnectionPool {
|
impl ConnectionPool {
|
||||||
/// Construct the connection pool from the configuration.
|
/// Construct the connection pool from a config file.
|
||||||
pub async fn from_config(
|
pub async fn from_config(
|
||||||
client_server_map: ClientServerMap,
|
client_server_map: ClientServerMap,
|
||||||
stats: Reporter,
|
stats: Reporter,
|
||||||
@@ -37,7 +38,6 @@ impl ConnectionPool {
|
|||||||
let mut shards = Vec::new();
|
let mut shards = Vec::new();
|
||||||
let mut addresses = Vec::new();
|
let mut addresses = Vec::new();
|
||||||
let mut banlist = Vec::new();
|
let mut banlist = Vec::new();
|
||||||
let mut address_id = 0;
|
|
||||||
let mut shard_ids = config
|
let mut shard_ids = config
|
||||||
.shards
|
.shards
|
||||||
.clone()
|
.clone()
|
||||||
@@ -49,8 +49,7 @@ impl ConnectionPool {
|
|||||||
for shard_idx in shard_ids {
|
for shard_idx in shard_ids {
|
||||||
let shard = &config.shards[&shard_idx];
|
let shard = &config.shards[&shard_idx];
|
||||||
let mut pools = Vec::new();
|
let mut pools = Vec::new();
|
||||||
let mut servers = Vec::new();
|
let mut replica_addresses = Vec::new();
|
||||||
let mut replica_number = 0;
|
|
||||||
|
|
||||||
for server in shard.servers.iter() {
|
for server in shard.servers.iter() {
|
||||||
let role = match server.2.as_ref() {
|
let role = match server.2.as_ref() {
|
||||||
@@ -63,20 +62,12 @@ impl ConnectionPool {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let address = Address {
|
let address = Address {
|
||||||
id: address_id,
|
|
||||||
host: server.0.clone(),
|
host: server.0.clone(),
|
||||||
port: server.1.to_string(),
|
port: server.1.to_string(),
|
||||||
role: role,
|
role: role,
|
||||||
replica_number,
|
|
||||||
shard: shard_idx.parse::<usize>().unwrap(),
|
shard: shard_idx.parse::<usize>().unwrap(),
|
||||||
};
|
};
|
||||||
|
|
||||||
address_id += 1;
|
|
||||||
|
|
||||||
if role == Role::Replica {
|
|
||||||
replica_number += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
let manager = ServerPool::new(
|
let manager = ServerPool::new(
|
||||||
address.clone(),
|
address.clone(),
|
||||||
config.user.clone(),
|
config.user.clone(),
|
||||||
@@ -96,11 +87,11 @@ impl ConnectionPool {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
pools.push(pool);
|
pools.push(pool);
|
||||||
servers.push(address);
|
replica_addresses.push(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
shards.push(pools);
|
shards.push(pools);
|
||||||
addresses.push(servers);
|
addresses.push(replica_addresses);
|
||||||
banlist.push(HashMap::new());
|
banlist.push(HashMap::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,13 +115,9 @@ impl ConnectionPool {
|
|||||||
pub async fn validate(&mut self) -> Result<BytesMut, Error> {
|
pub async fn validate(&mut self) -> Result<BytesMut, Error> {
|
||||||
let mut server_infos = Vec::new();
|
let mut server_infos = Vec::new();
|
||||||
|
|
||||||
let stats = self.stats.clone();
|
|
||||||
for shard in 0..self.shards() {
|
for shard in 0..self.shards() {
|
||||||
for _ in 0..self.servers(shard) {
|
for _ in 0..self.servers(shard) {
|
||||||
// To keep stats consistent.
|
let connection = match self.get(shard, None).await {
|
||||||
let fake_process_id = 0;
|
|
||||||
|
|
||||||
let connection = match self.get(shard, None, fake_process_id).await {
|
|
||||||
Ok(conn) => conn,
|
Ok(conn) => conn,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Shard {} down or misconfigured: {:?}", shard, err);
|
error!("Shard {} down or misconfigured: {:?}", shard, err);
|
||||||
@@ -139,24 +126,10 @@ impl ConnectionPool {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut proxy = connection.0;
|
let mut proxy = connection.0;
|
||||||
let address = connection.1;
|
let _address = connection.1;
|
||||||
let server = &mut *proxy;
|
let server = &mut *proxy;
|
||||||
|
|
||||||
let server_info = server.server_info();
|
server_infos.push(server.server_info());
|
||||||
|
|
||||||
stats.client_disconnecting(fake_process_id, address.id);
|
|
||||||
|
|
||||||
if server_infos.len() > 0 {
|
|
||||||
// Compare against the last server checked.
|
|
||||||
if server_info != server_infos[server_infos.len() - 1] {
|
|
||||||
warn!(
|
|
||||||
"{:?} has different server configuration than the last server",
|
|
||||||
address
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
server_infos.push(server_info);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,7 +147,6 @@ impl ConnectionPool {
|
|||||||
&mut self,
|
&mut self,
|
||||||
shard: usize,
|
shard: usize,
|
||||||
role: Option<Role>,
|
role: Option<Role>,
|
||||||
process_id: i32,
|
|
||||||
) -> Result<(PooledConnection<'_, ServerPool>, Address), Error> {
|
) -> Result<(PooledConnection<'_, ServerPool>, Address), Error> {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let addresses = &self.addresses[shard];
|
let addresses = &self.addresses[shard];
|
||||||
@@ -203,9 +175,10 @@ impl ConnectionPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
while allowed_attempts > 0 {
|
while allowed_attempts > 0 {
|
||||||
// Round-robin replicas.
|
// Round-robin each client's queries.
|
||||||
|
// If a client only sends one query and then disconnects, it doesn't matter
|
||||||
|
// which replica it'll go to.
|
||||||
self.round_robin += 1;
|
self.round_robin += 1;
|
||||||
|
|
||||||
let index = self.round_robin % addresses.len();
|
let index = self.round_robin % addresses.len();
|
||||||
let address = &addresses[index];
|
let address = &addresses[index];
|
||||||
|
|
||||||
@@ -222,27 +195,21 @@ impl ConnectionPool {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Indicate we're waiting on a server connection from a pool.
|
|
||||||
self.stats.client_waiting(process_id, address.id);
|
|
||||||
|
|
||||||
// Check if we can connect
|
// Check if we can connect
|
||||||
let mut conn = match self.databases[shard][index].get().await {
|
let mut conn = match self.databases[shard][index].get().await {
|
||||||
Ok(conn) => conn,
|
Ok(conn) => conn,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Banning replica {}, error: {:?}", index, err);
|
error!("Banning replica {}, error: {:?}", index, err);
|
||||||
self.ban(address, shard);
|
self.ban(address, shard);
|
||||||
self.stats.client_disconnecting(process_id, address.id);
|
|
||||||
self.stats
|
|
||||||
.checkout_time(now.elapsed().as_micros(), process_id, address.id);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// // Check if this server is alive with a health check.
|
// // Check if this server is alive with a health check
|
||||||
let server = &mut *conn;
|
let server = &mut *conn;
|
||||||
let healthcheck_timeout = get_config().general.healthcheck_timeout;
|
let healthcheck_timeout = get_config().general.healthcheck_timeout;
|
||||||
|
|
||||||
self.stats.server_tested(server.process_id(), address.id);
|
self.stats.server_tested(server.process_id());
|
||||||
|
|
||||||
match tokio::time::timeout(
|
match tokio::time::timeout(
|
||||||
tokio::time::Duration::from_millis(healthcheck_timeout),
|
tokio::time::Duration::from_millis(healthcheck_timeout),
|
||||||
@@ -250,40 +217,29 @@ impl ConnectionPool {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
// Check if health check succeeded.
|
// Check if health check succeeded
|
||||||
Ok(res) => match res {
|
Ok(res) => match res {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
self.stats
|
self.stats.checkout_time(now.elapsed().as_micros());
|
||||||
.checkout_time(now.elapsed().as_micros(), process_id, address.id);
|
self.stats.server_idle(conn.process_id());
|
||||||
self.stats.server_idle(conn.process_id(), address.id);
|
|
||||||
return Ok((conn, address.clone()));
|
return Ok((conn, address.clone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Health check failed.
|
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
error!("Banning replica {} because of failed health check", index);
|
error!("Banning replica {} because of failed health check", index);
|
||||||
|
|
||||||
// Don't leave a bad connection in the pool.
|
// Don't leave a bad connection in the pool.
|
||||||
server.mark_bad();
|
server.mark_bad();
|
||||||
|
|
||||||
self.ban(address, shard);
|
self.ban(address, shard);
|
||||||
self.stats.client_disconnecting(process_id, address.id);
|
|
||||||
self.stats
|
|
||||||
.checkout_time(now.elapsed().as_micros(), process_id, address.id);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
// Health check never came back, database is really really down
|
||||||
// Health check timed out.
|
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
error!("Banning replica {} because of health check timeout", index);
|
error!("Banning replica {} because of health check timeout", index);
|
||||||
// Don't leave a bad connection in the pool.
|
// Don't leave a bad connection in the pool.
|
||||||
server.mark_bad();
|
server.mark_bad();
|
||||||
|
|
||||||
self.ban(address, shard);
|
self.ban(address, shard);
|
||||||
self.stats.client_disconnecting(process_id, address.id);
|
|
||||||
self.stats
|
|
||||||
.checkout_time(now.elapsed().as_micros(), process_id, address.id);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -361,38 +317,15 @@ impl ConnectionPool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the number of configured shards.
|
|
||||||
pub fn shards(&self) -> usize {
|
pub fn shards(&self) -> usize {
|
||||||
self.databases.len()
|
self.databases.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the number of servers (primary and replicas)
|
|
||||||
/// configured for a shard.
|
|
||||||
pub fn servers(&self, shard: usize) -> usize {
|
pub fn servers(&self, shard: usize) -> usize {
|
||||||
self.addresses[shard].len()
|
self.addresses[shard].len()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the total number of servers (databases) we are connected to.
|
|
||||||
pub fn databases(&self) -> usize {
|
|
||||||
let mut databases = 0;
|
|
||||||
for shard in 0..self.shards() {
|
|
||||||
databases += self.servers(shard);
|
|
||||||
}
|
|
||||||
databases
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get pool state for a particular shard server as reported by bb8.
|
|
||||||
pub fn pool_state(&self, shard: usize, server: usize) -> bb8::State {
|
|
||||||
self.databases[shard][server].state()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the address information for a shard server.
|
|
||||||
pub fn address(&self, shard: usize, server: usize) -> &Address {
|
|
||||||
&self.addresses[shard][server]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wrapper for the bb8 connection pool.
|
|
||||||
pub struct ServerPool {
|
pub struct ServerPool {
|
||||||
address: Address,
|
address: Address,
|
||||||
user: User,
|
user: User,
|
||||||
@@ -428,16 +361,14 @@ impl ManageConnection for ServerPool {
|
|||||||
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
|
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
|
||||||
info!(
|
info!(
|
||||||
"Creating a new connection to {:?} using user {:?}",
|
"Creating a new connection to {:?} using user {:?}",
|
||||||
self.address.name(),
|
self.address, self.user.name
|
||||||
self.user.name
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Put a temporary process_id into the stats
|
// Put a temporary process_id into the stats
|
||||||
// for server login.
|
// for server login.
|
||||||
let process_id = rand::random::<i32>();
|
let process_id = rand::random::<i32>();
|
||||||
self.stats.server_login(process_id, self.address.id);
|
self.stats.server_login(process_id);
|
||||||
|
|
||||||
// Connect to the PostgreSQL server.
|
|
||||||
match Server::startup(
|
match Server::startup(
|
||||||
&self.address,
|
&self.address,
|
||||||
&self.user,
|
&self.user,
|
||||||
@@ -449,12 +380,12 @@ impl ManageConnection for ServerPool {
|
|||||||
{
|
{
|
||||||
Ok(conn) => {
|
Ok(conn) => {
|
||||||
// Remove the temporary process_id from the stats.
|
// Remove the temporary process_id from the stats.
|
||||||
self.stats.server_disconnecting(process_id, self.address.id);
|
self.stats.server_disconnecting(process_id);
|
||||||
Ok(conn)
|
Ok(conn)
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
// Remove the temporary process_id from the stats.
|
// Remove the temporary process_id from the stats.
|
||||||
self.stats.server_disconnecting(process_id, self.address.id);
|
self.stats.server_disconnecting(process_id);
|
||||||
Err(err)
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use crate::config::{get_config, Role};
|
||||||
|
use crate::sharding::{Sharder, ShardingFunction};
|
||||||
/// Route queries automatically based on explicitely requested
|
/// Route queries automatically based on explicitely requested
|
||||||
/// or implied query characteristics.
|
/// or implied query characteristics.
|
||||||
use bytes::{Buf, BytesMut};
|
use bytes::{Buf, BytesMut};
|
||||||
@@ -8,19 +10,14 @@ use sqlparser::ast::Statement::{Query, StartTransaction};
|
|||||||
use sqlparser::dialect::PostgreSqlDialect;
|
use sqlparser::dialect::PostgreSqlDialect;
|
||||||
use sqlparser::parser::Parser;
|
use sqlparser::parser::Parser;
|
||||||
|
|
||||||
use crate::config::{get_config, Role};
|
|
||||||
use crate::sharding::{Sharder, ShardingFunction};
|
|
||||||
|
|
||||||
/// Regexes used to parse custom commands.
|
|
||||||
const CUSTOM_SQL_REGEXES: [&str; 5] = [
|
const CUSTOM_SQL_REGEXES: [&str; 5] = [
|
||||||
r"(?i)^ *SET SHARDING KEY TO '?([0-9]+)'? *;? *$",
|
r"(?i)^ *SET SHARDING KEY TO '?([0-9]+)'? *;? *$",
|
||||||
r"(?i)^ *SET SHARD TO '?([0-9]+|ANY)'? *;? *$",
|
r"(?i)^ *SET SHARD TO '?([0-9]+)'? *;? *$",
|
||||||
r"(?i)^ *SHOW SHARD *;? *$",
|
r"(?i)^ *SHOW SHARD *;? *$",
|
||||||
r"(?i)^ *SET SERVER ROLE TO '(PRIMARY|REPLICA|ANY|AUTO|DEFAULT)' *;? *$",
|
r"(?i)^ *SET SERVER ROLE TO '(PRIMARY|REPLICA|ANY|AUTO|DEFAULT)' *;? *$",
|
||||||
r"(?i)^ *SHOW SERVER ROLE *;? *$",
|
r"(?i)^ *SHOW SERVER ROLE *;? *$",
|
||||||
];
|
];
|
||||||
|
|
||||||
/// Custom commands.
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
SetShardingKey,
|
SetShardingKey,
|
||||||
@@ -30,39 +27,37 @@ pub enum Command {
|
|||||||
ShowServerRole,
|
ShowServerRole,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Quickly test for match when a query is received.
|
// Quick test
|
||||||
static CUSTOM_SQL_REGEX_SET: OnceCell<RegexSet> = OnceCell::new();
|
static CUSTOM_SQL_REGEX_SET: OnceCell<RegexSet> = OnceCell::new();
|
||||||
|
|
||||||
// Get the value inside the custom command.
|
// Capture value
|
||||||
static CUSTOM_SQL_REGEX_LIST: OnceCell<Vec<Regex>> = OnceCell::new();
|
static CUSTOM_SQL_REGEX_LIST: OnceCell<Vec<Regex>> = OnceCell::new();
|
||||||
|
|
||||||
/// The query router.
|
|
||||||
pub struct QueryRouter {
|
pub struct QueryRouter {
|
||||||
/// By default, queries go here, unless we have better information
|
// By default, queries go here, unless we have better information
|
||||||
/// about what the client wants.
|
// about what the client wants.
|
||||||
default_server_role: Option<Role>,
|
default_server_role: Option<Role>,
|
||||||
|
|
||||||
/// Number of shards in the cluster.
|
// Number of shards in the cluster.
|
||||||
shards: usize,
|
shards: usize,
|
||||||
|
|
||||||
/// Which shard we should be talking to right now.
|
// Which shard we should be talking to right now.
|
||||||
active_shard: Option<usize>,
|
active_shard: Option<usize>,
|
||||||
|
|
||||||
/// Which server should we be talking to.
|
// Should we be talking to a primary or a replica?
|
||||||
active_role: Option<Role>,
|
active_role: Option<Role>,
|
||||||
|
|
||||||
/// Include the primary into the replica pool for reads.
|
// Include the primary into the replica pool?
|
||||||
primary_reads_enabled: bool,
|
primary_reads_enabled: bool,
|
||||||
|
|
||||||
/// Should we try to parse queries to route them to replicas or primary automatically.
|
// Should we try to parse queries?
|
||||||
query_parser_enabled: bool,
|
query_parser_enabled: bool,
|
||||||
|
|
||||||
/// Which sharding function we're using.
|
// Which sharding function are we using?
|
||||||
sharding_function: ShardingFunction,
|
sharding_function: ShardingFunction,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl QueryRouter {
|
impl QueryRouter {
|
||||||
/// One-time initialization of regexes.
|
|
||||||
pub fn setup() -> bool {
|
pub fn setup() -> bool {
|
||||||
let set = match RegexSet::new(&CUSTOM_SQL_REGEXES) {
|
let set = match RegexSet::new(&CUSTOM_SQL_REGEXES) {
|
||||||
Ok(rgx) => rgx,
|
Ok(rgx) => rgx,
|
||||||
@@ -93,7 +88,6 @@ impl QueryRouter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new instance of the query router. Each client gets its own.
|
|
||||||
pub fn new() -> QueryRouter {
|
pub fn new() -> QueryRouter {
|
||||||
let config = get_config();
|
let config = get_config();
|
||||||
|
|
||||||
@@ -126,7 +120,6 @@ impl QueryRouter {
|
|||||||
pub fn try_execute_command(&mut self, mut buf: BytesMut) -> Option<(Command, String)> {
|
pub fn try_execute_command(&mut self, mut buf: BytesMut) -> Option<(Command, String)> {
|
||||||
let code = buf.get_u8() as char;
|
let code = buf.get_u8() as char;
|
||||||
|
|
||||||
// Only simple protocol supported for commands.
|
|
||||||
if code != 'Q' {
|
if code != 'Q' {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
@@ -165,7 +158,8 @@ impl QueryRouter {
|
|||||||
// figured out a better way just yet. I think I can write a single Regex
|
// figured out a better way just yet. I think I can write a single Regex
|
||||||
// that matches all 5 custom SQL patterns, but maybe that's not very legible?
|
// that matches all 5 custom SQL patterns, but maybe that's not very legible?
|
||||||
//
|
//
|
||||||
// I think this is faster than running the Regex engine 5 times.
|
// I think this is faster than running the Regex engine 5 times, so
|
||||||
|
// this is a strong maybe for me so far.
|
||||||
match regex_list[matches[0]].captures(&query) {
|
match regex_list[matches[0]].captures(&query) {
|
||||||
Some(captures) => match captures.get(1) {
|
Some(captures) => match captures.get(1) {
|
||||||
Some(value) => value.as_str().to_string(),
|
Some(value) => value.as_str().to_string(),
|
||||||
@@ -198,10 +192,7 @@ impl QueryRouter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Command::SetShard => {
|
Command::SetShard => {
|
||||||
self.active_shard = match value.to_ascii_uppercase().as_ref() {
|
self.active_shard = Some(value.parse::<usize>().unwrap());
|
||||||
"ANY" => Some(rand::random::<usize>() % self.shards),
|
|
||||||
_ => Some(value.parse::<usize>().unwrap()),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Command::SetServerRole => {
|
Command::SetServerRole => {
|
||||||
@@ -227,6 +218,7 @@ impl QueryRouter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
"default" => {
|
"default" => {
|
||||||
|
// TODO: reset query parser to default here.
|
||||||
self.active_role = self.default_server_role;
|
self.active_role = self.default_server_role;
|
||||||
self.query_parser_enabled = get_config().query_router.query_parser_enabled;
|
self.query_parser_enabled = get_config().query_router.query_parser_enabled;
|
||||||
self.active_role
|
self.active_role
|
||||||
@@ -248,14 +240,12 @@ impl QueryRouter {
|
|||||||
let len = buf.get_i32() as usize;
|
let len = buf.get_i32() as usize;
|
||||||
|
|
||||||
let query = match code {
|
let query = match code {
|
||||||
// Query
|
|
||||||
'Q' => {
|
'Q' => {
|
||||||
let query = String::from_utf8_lossy(&buf[..len - 5]).to_string();
|
let query = String::from_utf8_lossy(&buf[..len - 5]).to_string();
|
||||||
debug!("Query: '{}'", query);
|
debug!("Query: '{}'", query);
|
||||||
query
|
query
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse (prepared statement)
|
|
||||||
'P' => {
|
'P' => {
|
||||||
let mut start = 0;
|
let mut start = 0;
|
||||||
let mut end;
|
let mut end;
|
||||||
@@ -278,7 +268,6 @@ impl QueryRouter {
|
|||||||
|
|
||||||
query.replace("$", "") // Remove placeholders turning them into "values"
|
query.replace("$", "") // Remove placeholders turning them into "values"
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => return false,
|
_ => return false,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -342,7 +331,6 @@ impl QueryRouter {
|
|||||||
self.query_parser_enabled
|
self.query_parser_enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Allows to toggle primary reads in tests.
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn toggle_primary_reads(&mut self, value: bool) {
|
pub fn toggle_primary_reads(&mut self, value: bool) {
|
||||||
self.primary_reads_enabled = value;
|
self.primary_reads_enabled = value;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/// Implementation of the PostgreSQL server (database) protocol.
|
|
||||||
/// Here we are pretending to the a Postgres client.
|
|
||||||
use bytes::{Buf, BufMut, BytesMut};
|
use bytes::{Buf, BufMut, BytesMut};
|
||||||
|
///! Implementation of the PostgreSQL server (database) protocol.
|
||||||
|
///! Here we are pretending to the a Postgres client.
|
||||||
use log::{debug, error, info, trace};
|
use log::{debug, error, info, trace};
|
||||||
use tokio::io::{AsyncReadExt, BufReader};
|
use tokio::io::{AsyncReadExt, BufReader};
|
||||||
use tokio::net::{
|
use tokio::net::{
|
||||||
@@ -17,42 +17,42 @@ use crate::ClientServerMap;
|
|||||||
|
|
||||||
/// Server state.
|
/// Server state.
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
/// Server host, e.g. localhost,
|
// Server host, e.g. localhost,
|
||||||
/// port, e.g. 5432, and role, e.g. primary or replica.
|
// port, e.g. 5432, and role, e.g. primary or replica.
|
||||||
address: Address,
|
address: Address,
|
||||||
|
|
||||||
/// Buffered read socket.
|
// Buffered read socket.
|
||||||
read: BufReader<OwnedReadHalf>,
|
read: BufReader<OwnedReadHalf>,
|
||||||
|
|
||||||
/// Unbuffered write socket (our client code buffers).
|
// Unbuffered write socket (our client code buffers).
|
||||||
write: OwnedWriteHalf,
|
write: OwnedWriteHalf,
|
||||||
|
|
||||||
/// Our server response buffer. We buffer data before we give it to the client.
|
// Our server response buffer. We buffer data before we give it to the client.
|
||||||
buffer: BytesMut,
|
buffer: BytesMut,
|
||||||
|
|
||||||
/// Server information the server sent us over on startup.
|
// Server information the server sent us over on startup.
|
||||||
server_info: BytesMut,
|
server_info: BytesMut,
|
||||||
|
|
||||||
/// Backend id and secret key used for query cancellation.
|
// Backend id and secret key used for query cancellation.
|
||||||
process_id: i32,
|
process_id: i32,
|
||||||
secret_key: i32,
|
secret_key: i32,
|
||||||
|
|
||||||
/// Is the server inside a transaction or idle.
|
// Is the server inside a transaction or idle.
|
||||||
in_transaction: bool,
|
in_transaction: bool,
|
||||||
|
|
||||||
/// Is there more data for the client to read.
|
// Is there more data for the client to read.
|
||||||
data_available: bool,
|
data_available: bool,
|
||||||
|
|
||||||
/// Is the server broken? We'll remote it from the pool if so.
|
// Is the server broken? We'll remote it from the pool if so.
|
||||||
bad: bool,
|
bad: bool,
|
||||||
|
|
||||||
/// Mapping of clients and servers used for query cancellation.
|
// Mapping of clients and servers used for query cancellation.
|
||||||
client_server_map: ClientServerMap,
|
client_server_map: ClientServerMap,
|
||||||
|
|
||||||
/// Server connected at.
|
// Server connected at.
|
||||||
connected_at: chrono::naive::NaiveDateTime,
|
connected_at: chrono::naive::NaiveDateTime,
|
||||||
|
|
||||||
/// Reports various metrics, e.g. data sent & received.
|
// Reports various metrics, e.g. data sent & received.
|
||||||
stats: Reporter,
|
stats: Reporter,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ impl Server {
|
|||||||
|
|
||||||
trace!("Sending StartupMessage");
|
trace!("Sending StartupMessage");
|
||||||
|
|
||||||
// StartupMessage
|
// Send the startup packet telling the server we're a normal Postgres client.
|
||||||
startup(&mut stream, &user.name, database).await?;
|
startup(&mut stream, &user.name, database).await?;
|
||||||
|
|
||||||
let mut server_info = BytesMut::new();
|
let mut server_info = BytesMut::new();
|
||||||
@@ -187,7 +187,7 @@ impl Server {
|
|||||||
// BackendKeyData
|
// BackendKeyData
|
||||||
'K' => {
|
'K' => {
|
||||||
// The frontend must save these values if it wishes to be able to issue CancelRequest messages later.
|
// The frontend must save these values if it wishes to be able to issue CancelRequest messages later.
|
||||||
// See: <https://www.postgresql.org/docs/12/protocol-message-formats.html>.
|
// See: https://www.postgresql.org/docs/12/protocol-message-formats.html
|
||||||
process_id = match stream.read_i32().await {
|
process_id = match stream.read_i32().await {
|
||||||
Ok(id) => id,
|
Ok(id) => id,
|
||||||
Err(_) => return Err(Error::SocketError),
|
Err(_) => return Err(Error::SocketError),
|
||||||
@@ -208,6 +208,8 @@ impl Server {
|
|||||||
Err(_) => return Err(Error::SocketError),
|
Err(_) => return Err(Error::SocketError),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// This is the last step in the client-server connection setup,
|
||||||
|
// and indicates the server is ready for to query it.
|
||||||
let (read, write) = stream.into_split();
|
let (read, write) = stream.into_split();
|
||||||
|
|
||||||
return Ok(Server {
|
return Ok(Server {
|
||||||
@@ -266,8 +268,7 @@ impl Server {
|
|||||||
|
|
||||||
/// Send messages to the server from the client.
|
/// Send messages to the server from the client.
|
||||||
pub async fn send(&mut self, messages: BytesMut) -> Result<(), Error> {
|
pub async fn send(&mut self, messages: BytesMut) -> Result<(), Error> {
|
||||||
self.stats
|
self.stats.data_sent(messages.len());
|
||||||
.data_sent(messages.len(), self.process_id, self.address.id);
|
|
||||||
|
|
||||||
match write_all_half(&mut self.write, messages).await {
|
match write_all_half(&mut self.write, messages).await {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
@@ -340,7 +341,8 @@ impl Server {
|
|||||||
// More data is available after this message, this is not the end of the reply.
|
// More data is available after this message, this is not the end of the reply.
|
||||||
self.data_available = true;
|
self.data_available = true;
|
||||||
|
|
||||||
// Don't flush yet, the more we buffer, the faster this goes...up to a limit.
|
// Don't flush yet, the more we buffer, the faster this goes...
|
||||||
|
// up to a limit of course.
|
||||||
if self.buffer.len() >= 8196 {
|
if self.buffer.len() >= 8196 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -372,8 +374,7 @@ impl Server {
|
|||||||
let bytes = self.buffer.clone();
|
let bytes = self.buffer.clone();
|
||||||
|
|
||||||
// Keep track of how much data we got from the server for stats.
|
// Keep track of how much data we got from the server for stats.
|
||||||
self.stats
|
self.stats.data_received(bytes.len());
|
||||||
.data_received(bytes.len(), self.process_id, self.address.id);
|
|
||||||
|
|
||||||
// Clear the buffer for next query.
|
// Clear the buffer for next query.
|
||||||
self.buffer.clear();
|
self.buffer.clear();
|
||||||
@@ -408,7 +409,7 @@ impl Server {
|
|||||||
|
|
||||||
/// Indicate that this server connection cannot be re-used and must be discarded.
|
/// Indicate that this server connection cannot be re-used and must be discarded.
|
||||||
pub fn mark_bad(&mut self) {
|
pub fn mark_bad(&mut self) {
|
||||||
error!("Server {:?} marked bad", self.address);
|
error!("Server marked bad");
|
||||||
self.bad = true;
|
self.bad = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -459,7 +460,6 @@ impl Server {
|
|||||||
self.address.clone()
|
self.address.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the server's unique identifier.
|
|
||||||
pub fn process_id(&self) -> i32 {
|
pub fn process_id(&self) -> i32 {
|
||||||
self.process_id
|
self.process_id
|
||||||
}
|
}
|
||||||
@@ -470,8 +470,7 @@ impl Drop for Server {
|
|||||||
/// the socket is in non-blocking mode, so it may not be ready
|
/// the socket is in non-blocking mode, so it may not be ready
|
||||||
/// for a write.
|
/// for a write.
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.stats
|
self.stats.server_disconnecting(self.process_id());
|
||||||
.server_disconnecting(self.process_id(), self.address.id);
|
|
||||||
|
|
||||||
let mut bytes = BytesMut::with_capacity(4);
|
let mut bytes = BytesMut::with_capacity(4);
|
||||||
bytes.put_u8(b'X');
|
bytes.put_u8(b'X');
|
||||||
@@ -479,10 +478,9 @@ impl Drop for Server {
|
|||||||
|
|
||||||
match self.write.try_write(&bytes) {
|
match self.write.try_write(&bytes) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(_) => debug!("Dirty shutdown"),
|
Err(_) => (),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Should not matter.
|
|
||||||
self.bad = true;
|
self.bad = true;
|
||||||
|
|
||||||
let now = chrono::offset::Utc::now().naive_utc();
|
let now = chrono::offset::Utc::now().naive_utc();
|
||||||
|
|||||||
@@ -1,27 +1,20 @@
|
|||||||
/// Implements various sharding functions.
|
|
||||||
use sha1::{Digest, Sha1};
|
use sha1::{Digest, Sha1};
|
||||||
|
|
||||||
/// See: <https://github.com/postgres/postgres/blob/27b77ecf9f4d5be211900eda54d8155ada50d696/src/include/catalog/partition.h#L20>.
|
// https://github.com/postgres/postgres/blob/27b77ecf9f4d5be211900eda54d8155ada50d696/src/include/catalog/partition.h#L20
|
||||||
const PARTITION_HASH_SEED: u64 = 0x7A5B22367996DCFD;
|
const PARTITION_HASH_SEED: u64 = 0x7A5B22367996DCFD;
|
||||||
|
|
||||||
/// The sharding functions we support.
|
|
||||||
#[derive(Debug, PartialEq, Copy, Clone)]
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||||
pub enum ShardingFunction {
|
pub enum ShardingFunction {
|
||||||
PgBigintHash,
|
PgBigintHash,
|
||||||
Sha1,
|
Sha1,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The sharder.
|
|
||||||
pub struct Sharder {
|
pub struct Sharder {
|
||||||
/// Number of shards in the cluster.
|
|
||||||
shards: usize,
|
shards: usize,
|
||||||
|
|
||||||
/// The sharding function in use.
|
|
||||||
sharding_function: ShardingFunction,
|
sharding_function: ShardingFunction,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sharder {
|
impl Sharder {
|
||||||
/// Create new instance of the sharder.
|
|
||||||
pub fn new(shards: usize, sharding_function: ShardingFunction) -> Sharder {
|
pub fn new(shards: usize, sharding_function: ShardingFunction) -> Sharder {
|
||||||
Sharder {
|
Sharder {
|
||||||
shards,
|
shards,
|
||||||
@@ -29,7 +22,6 @@ impl Sharder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compute the shard given sharding key.
|
|
||||||
pub fn shard(&self, key: i64) -> usize {
|
pub fn shard(&self, key: i64) -> usize {
|
||||||
match self.sharding_function {
|
match self.sharding_function {
|
||||||
ShardingFunction::PgBigintHash => self.pg_bigint_hash(key),
|
ShardingFunction::PgBigintHash => self.pg_bigint_hash(key),
|
||||||
@@ -39,7 +31,7 @@ impl Sharder {
|
|||||||
|
|
||||||
/// Hash function used by Postgres to determine which partition
|
/// Hash function used by Postgres to determine which partition
|
||||||
/// to put the row in when using HASH(column) partitioning.
|
/// to put the row in when using HASH(column) partitioning.
|
||||||
/// Source: <https://github.com/postgres/postgres/blob/27b77ecf9f4d5be211900eda54d8155ada50d696/src/common/hashfn.c#L631>.
|
/// Source: https://github.com/postgres/postgres/blob/27b77ecf9f4d5be211900eda54d8155ada50d696/src/common/hashfn.c#L631
|
||||||
/// Supports only 1 bigint at the moment, but we can add more later.
|
/// Supports only 1 bigint at the moment, but we can add more later.
|
||||||
fn pg_bigint_hash(&self, key: i64) -> usize {
|
fn pg_bigint_hash(&self, key: i64) -> usize {
|
||||||
let mut lohalf = key as u32;
|
let mut lohalf = key as u32;
|
||||||
@@ -127,7 +119,6 @@ impl Sharder {
|
|||||||
a
|
a
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn pg_u32_hash(k: u32) -> u64 {
|
fn pg_u32_hash(k: u32) -> u64 {
|
||||||
let mut a: u32 = 0x9e3779b9 as u32 + std::mem::size_of::<u32>() as u32 + 3923095 as u32;
|
let mut a: u32 = 0x9e3779b9 as u32 + std::mem::size_of::<u32>() as u32 + 3923095 as u32;
|
||||||
let mut b = a;
|
let mut b = a;
|
||||||
|
|||||||
308
src/stats.rs
308
src/stats.rs
@@ -1,20 +1,12 @@
|
|||||||
/// Statistics and reporting.
|
|
||||||
use log::info;
|
use log::info;
|
||||||
use once_cell::sync::Lazy;
|
use statsd::Client;
|
||||||
use parking_lot::Mutex;
|
/// Events collector and publisher.
|
||||||
use std::collections::HashMap;
|
|
||||||
use tokio::sync::mpsc::{Receiver, Sender};
|
use tokio::sync::mpsc::{Receiver, Sender};
|
||||||
|
|
||||||
/// Latest stats updated every second; used in SHOW STATS and other admin commands.
|
use std::collections::HashMap;
|
||||||
static LATEST_STATS: Lazy<Mutex<HashMap<usize, HashMap<String, i64>>>> =
|
|
||||||
Lazy::new(|| Mutex::new(HashMap::new()));
|
|
||||||
|
|
||||||
/// Statistics period used for average calculations.
|
use crate::config::get_config;
|
||||||
/// 15 seconds.
|
|
||||||
static STAT_PERIOD: u64 = 15000;
|
|
||||||
|
|
||||||
/// The names for the events reported
|
|
||||||
/// to the statistics collector.
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
enum EventName {
|
enum EventName {
|
||||||
CheckoutTime,
|
CheckoutTime,
|
||||||
@@ -31,266 +23,201 @@ enum EventName {
|
|||||||
ServerTested,
|
ServerTested,
|
||||||
ServerLogin,
|
ServerLogin,
|
||||||
ServerDisconnecting,
|
ServerDisconnecting,
|
||||||
UpdateStats,
|
FlushStatsToStatsD,
|
||||||
UpdateAverages,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Event data sent to the collector
|
|
||||||
/// from clients and servers.
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Event {
|
pub struct Event {
|
||||||
/// The name of the event being reported.
|
|
||||||
name: EventName,
|
name: EventName,
|
||||||
|
|
||||||
/// The value being reported. Meaning differs based on event name.
|
|
||||||
value: i64,
|
value: i64,
|
||||||
|
process_id: Option<i32>,
|
||||||
/// The client or server connection reporting the event.
|
|
||||||
process_id: i32,
|
|
||||||
|
|
||||||
/// The server the client is connected to.
|
|
||||||
address_id: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The statistics reporter. An instance is given
|
|
||||||
/// to each possible source of statistics,
|
|
||||||
/// e.g. clients, servers, connection pool.
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Reporter {
|
pub struct Reporter {
|
||||||
tx: Sender<Event>,
|
tx: Sender<Event>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Reporter {
|
impl Reporter {
|
||||||
/// Create a new Reporter instance.
|
|
||||||
pub fn new(tx: Sender<Event>) -> Reporter {
|
pub fn new(tx: Sender<Event>) -> Reporter {
|
||||||
Reporter { tx: tx }
|
Reporter { tx: tx }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Report a query executed by a client against
|
pub fn query(&self) {
|
||||||
/// a server identified by the `address_id`.
|
|
||||||
pub fn query(&self, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::Query,
|
name: EventName::Query,
|
||||||
value: 1,
|
value: 1,
|
||||||
process_id: process_id,
|
process_id: None,
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Report a transaction executed by a client against
|
pub fn transaction(&self) {
|
||||||
/// a server identified by the `address_id`.
|
|
||||||
pub fn transaction(&self, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::Transaction,
|
name: EventName::Transaction,
|
||||||
value: 1,
|
value: 1,
|
||||||
process_id: process_id,
|
process_id: None,
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Report data sent to a server identified by `address_id`.
|
pub fn data_sent(&self, amount: usize) {
|
||||||
/// The `amount` is measured in bytes.
|
|
||||||
pub fn data_sent(&self, amount: usize, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::DataSent,
|
name: EventName::DataSent,
|
||||||
value: amount as i64,
|
value: amount as i64,
|
||||||
process_id: process_id,
|
process_id: None,
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Report data received from a server identified by `address_id`.
|
pub fn data_received(&self, amount: usize) {
|
||||||
/// The `amount` is measured in bytes.
|
|
||||||
pub fn data_received(&self, amount: usize, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::DataReceived,
|
name: EventName::DataReceived,
|
||||||
value: amount as i64,
|
value: amount as i64,
|
||||||
process_id: process_id,
|
process_id: None,
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Time spent waiting to get a healthy connection from the pool
|
pub fn checkout_time(&self, ms: u128) {
|
||||||
/// for a server identified by `address_id`.
|
|
||||||
/// Measured in milliseconds.
|
|
||||||
pub fn checkout_time(&self, ms: u128, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::CheckoutTime,
|
name: EventName::CheckoutTime,
|
||||||
value: ms as i64,
|
value: ms as i64,
|
||||||
process_id: process_id,
|
process_id: None,
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reports a client identified by `process_id` waiting for a connection
|
pub fn client_waiting(&self, process_id: i32) {
|
||||||
/// to a server identified by `address_id`.
|
|
||||||
pub fn client_waiting(&self, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::ClientWaiting,
|
name: EventName::ClientWaiting,
|
||||||
value: 1,
|
value: 1,
|
||||||
process_id: process_id,
|
process_id: Some(process_id),
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reports a client identified by `process_id` is done waiting for a connection
|
pub fn client_active(&self, process_id: i32) {
|
||||||
/// to a server identified by `address_id` and is about to query the server.
|
|
||||||
pub fn client_active(&self, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::ClientActive,
|
name: EventName::ClientActive,
|
||||||
value: 1,
|
value: 1,
|
||||||
process_id: process_id,
|
process_id: Some(process_id),
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reports a client identified by `process_id` is done querying the server
|
pub fn client_idle(&self, process_id: i32) {
|
||||||
/// identified by `address_id` and is no longer active.
|
|
||||||
pub fn client_idle(&self, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::ClientIdle,
|
name: EventName::ClientIdle,
|
||||||
value: 1,
|
value: 1,
|
||||||
process_id: process_id,
|
process_id: Some(process_id),
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reports a client identified by `process_id` is disconecting from the pooler.
|
pub fn client_disconnecting(&self, process_id: i32) {
|
||||||
/// The last server it was connected to is identified by `address_id`.
|
|
||||||
pub fn client_disconnecting(&self, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::ClientDisconnecting,
|
name: EventName::ClientDisconnecting,
|
||||||
value: 1,
|
value: 1,
|
||||||
process_id: process_id,
|
process_id: Some(process_id),
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reports a server connection identified by `process_id` for
|
pub fn server_active(&self, process_id: i32) {
|
||||||
/// a configured server identified by `address_id` is actively used
|
|
||||||
/// by a client.
|
|
||||||
pub fn server_active(&self, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::ServerActive,
|
name: EventName::ServerActive,
|
||||||
value: 1,
|
value: 1,
|
||||||
process_id: process_id,
|
process_id: Some(process_id),
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reports a server connection identified by `process_id` for
|
pub fn server_idle(&self, process_id: i32) {
|
||||||
/// a configured server identified by `address_id` is no longer
|
|
||||||
/// actively used by a client and is now idle.
|
|
||||||
pub fn server_idle(&self, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::ServerIdle,
|
name: EventName::ServerIdle,
|
||||||
value: 1,
|
value: 1,
|
||||||
process_id: process_id,
|
process_id: Some(process_id),
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reports a server connection identified by `process_id` for
|
pub fn server_login(&self, process_id: i32) {
|
||||||
/// a configured server identified by `address_id` is attempting
|
|
||||||
/// to login.
|
|
||||||
pub fn server_login(&self, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::ServerLogin,
|
name: EventName::ServerLogin,
|
||||||
value: 1,
|
value: 1,
|
||||||
process_id: process_id,
|
process_id: Some(process_id),
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reports a server connection identified by `process_id` for
|
pub fn server_tested(&self, process_id: i32) {
|
||||||
/// a configured server identified by `address_id` is being
|
|
||||||
/// tested before being given to a client.
|
|
||||||
pub fn server_tested(&self, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::ServerTested,
|
name: EventName::ServerTested,
|
||||||
value: 1,
|
value: 1,
|
||||||
process_id: process_id,
|
process_id: Some(process_id),
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reports a server connection identified by `process_id` is disconecting from the pooler.
|
pub fn server_disconnecting(&self, process_id: i32) {
|
||||||
/// The configured server it was connected to is identified by `address_id`.
|
|
||||||
pub fn server_disconnecting(&self, process_id: i32, address_id: usize) {
|
|
||||||
let event = Event {
|
let event = Event {
|
||||||
name: EventName::ServerDisconnecting,
|
name: EventName::ServerDisconnecting,
|
||||||
value: 1,
|
value: 1,
|
||||||
process_id: process_id,
|
process_id: Some(process_id),
|
||||||
address_id: address_id,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = self.tx.try_send(event);
|
let _ = self.tx.try_send(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pub fn flush_to_statsd(&self) {
|
||||||
|
// let event = Event {
|
||||||
|
// name: EventName::FlushStatsToStatsD,
|
||||||
|
// value: 0,
|
||||||
|
// process_id: None,
|
||||||
|
// };
|
||||||
|
|
||||||
|
// let _ = self.tx.try_send(event);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The statistics collector which is receiving statistics
|
|
||||||
/// from clients, servers, and the connection pool. There is
|
|
||||||
/// only one collector (kind of like a singleton).
|
|
||||||
/// The collector can trigger events on its own, e.g.
|
|
||||||
/// it updates aggregates every second and averages every
|
|
||||||
/// 15 seconds.
|
|
||||||
pub struct Collector {
|
pub struct Collector {
|
||||||
rx: Receiver<Event>,
|
rx: Receiver<Event>,
|
||||||
tx: Sender<Event>,
|
tx: Sender<Event>,
|
||||||
|
client: Client,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Collector {
|
impl Collector {
|
||||||
/// Create a new collector instance. There should only be one instance
|
|
||||||
/// at a time. This is ensured by mpsc which allows only one receiver.
|
|
||||||
pub fn new(rx: Receiver<Event>, tx: Sender<Event>) -> Collector {
|
pub fn new(rx: Receiver<Event>, tx: Sender<Event>) -> Collector {
|
||||||
Collector { rx, tx }
|
Collector {
|
||||||
|
rx,
|
||||||
|
tx,
|
||||||
|
client: Client::new(&get_config().general.statsd_address, "pgcat").unwrap(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The statistics collection handler. It will collect statistics
|
pub async fn collect(&mut self) {
|
||||||
/// for `address_id`s starting at 0 up to `addresses`.
|
|
||||||
pub async fn collect(&mut self, addresses: usize) {
|
|
||||||
info!("Events reporter started");
|
info!("Events reporter started");
|
||||||
|
|
||||||
let stats_template = HashMap::from([
|
let mut stats = HashMap::from([
|
||||||
("total_query_count", 0),
|
("total_query_count", 0),
|
||||||
("total_xact_count", 0),
|
("total_xact_count", 0),
|
||||||
("total_sent", 0),
|
("total_sent", 0),
|
||||||
("total_received", 0),
|
("total_received", 0),
|
||||||
("total_xact_time", 0),
|
|
||||||
("total_query_time", 0),
|
|
||||||
("total_wait_time", 0),
|
("total_wait_time", 0),
|
||||||
("avg_xact_time", 0),
|
|
||||||
("avg_query_time", 0),
|
|
||||||
("avg_xact_count", 0),
|
|
||||||
("avg_sent", 0),
|
|
||||||
("avg_received", 0),
|
|
||||||
("avg_wait_time", 0),
|
|
||||||
("maxwait_us", 0),
|
("maxwait_us", 0),
|
||||||
("maxwait", 0),
|
("maxwait", 0),
|
||||||
("cl_waiting", 0),
|
("cl_waiting", 0),
|
||||||
@@ -302,51 +229,21 @@ impl Collector {
|
|||||||
("sv_tested", 0),
|
("sv_tested", 0),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let mut stats = HashMap::new();
|
let mut client_server_states: HashMap<i32, EventName> = HashMap::new();
|
||||||
|
|
||||||
// Stats saved after each iteration of the flush event. Used in calculation
|
|
||||||
// of averages in the last flush period.
|
|
||||||
let mut old_stats: HashMap<usize, HashMap<String, i64>> = HashMap::new();
|
|
||||||
|
|
||||||
// Track which state the client and server are at any given time.
|
|
||||||
let mut client_server_states: HashMap<usize, HashMap<i32, EventName>> = HashMap::new();
|
|
||||||
|
|
||||||
// Flush stats to StatsD and calculate averages every 15 seconds.
|
|
||||||
let tx = self.tx.clone();
|
let tx = self.tx.clone();
|
||||||
|
|
||||||
tokio::task::spawn(async move {
|
tokio::task::spawn(async move {
|
||||||
let mut interval =
|
let mut interval = tokio::time::interval(tokio::time::Duration::from_millis(15000));
|
||||||
tokio::time::interval(tokio::time::Duration::from_millis(STAT_PERIOD / 15));
|
|
||||||
loop {
|
loop {
|
||||||
interval.tick().await;
|
interval.tick().await;
|
||||||
for address_id in 0..addresses {
|
let _ = tx.try_send(Event {
|
||||||
let _ = tx.try_send(Event {
|
name: EventName::FlushStatsToStatsD,
|
||||||
name: EventName::UpdateStats,
|
value: 0,
|
||||||
value: 0,
|
process_id: None,
|
||||||
process_id: -1,
|
});
|
||||||
address_id: address_id,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let tx = self.tx.clone();
|
|
||||||
tokio::task::spawn(async move {
|
|
||||||
let mut interval =
|
|
||||||
tokio::time::interval(tokio::time::Duration::from_millis(STAT_PERIOD));
|
|
||||||
loop {
|
|
||||||
interval.tick().await;
|
|
||||||
for address_id in 0..addresses {
|
|
||||||
let _ = tx.try_send(Event {
|
|
||||||
name: EventName::UpdateAverages,
|
|
||||||
value: 0,
|
|
||||||
process_id: -1,
|
|
||||||
address_id: address_id,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// The collector loop
|
|
||||||
loop {
|
loop {
|
||||||
let stat = match self.rx.recv().await {
|
let stat = match self.rx.recv().await {
|
||||||
Some(stat) => stat,
|
Some(stat) => stat,
|
||||||
@@ -356,14 +253,6 @@ impl Collector {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let stats = stats
|
|
||||||
.entry(stat.address_id)
|
|
||||||
.or_insert(stats_template.clone());
|
|
||||||
let client_server_states = client_server_states
|
|
||||||
.entry(stat.address_id)
|
|
||||||
.or_insert(HashMap::new());
|
|
||||||
let old_stats = old_stats.entry(stat.address_id).or_insert(HashMap::new());
|
|
||||||
|
|
||||||
// Some are counters, some are gauges...
|
// Some are counters, some are gauges...
|
||||||
match stat.name {
|
match stat.name {
|
||||||
EventName::Query => {
|
EventName::Query => {
|
||||||
@@ -391,11 +280,10 @@ impl Collector {
|
|||||||
*counter += stat.value;
|
*counter += stat.value;
|
||||||
|
|
||||||
let counter = stats.entry("maxwait_us").or_insert(0);
|
let counter = stats.entry("maxwait_us").or_insert(0);
|
||||||
let mic_part = stat.value % 1_000_000;
|
|
||||||
|
|
||||||
// Report max time here
|
// Report max time here
|
||||||
if mic_part > *counter {
|
if stat.value > *counter {
|
||||||
*counter = mic_part;
|
*counter = stat.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
let counter = stats.entry("maxwait").or_insert(0);
|
let counter = stats.entry("maxwait").or_insert(0);
|
||||||
@@ -413,16 +301,15 @@ impl Collector {
|
|||||||
| EventName::ServerIdle
|
| EventName::ServerIdle
|
||||||
| EventName::ServerTested
|
| EventName::ServerTested
|
||||||
| EventName::ServerLogin => {
|
| EventName::ServerLogin => {
|
||||||
client_server_states.insert(stat.process_id, stat.name);
|
client_server_states.insert(stat.process_id.unwrap(), stat.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
EventName::ClientDisconnecting | EventName::ServerDisconnecting => {
|
EventName::ClientDisconnecting | EventName::ServerDisconnecting => {
|
||||||
client_server_states.remove(&stat.process_id);
|
client_server_states.remove(&stat.process_id.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
EventName::UpdateStats => {
|
EventName::FlushStatsToStatsD => {
|
||||||
// Calculate connection states
|
for (_, state) in &client_server_states {
|
||||||
for (_, state) in client_server_states.iter() {
|
|
||||||
match state {
|
match state {
|
||||||
EventName::ClientActive => {
|
EventName::ClientActive => {
|
||||||
let counter = stats.entry("cl_active").or_insert(0);
|
let counter = stats.entry("cl_active").or_insert(0);
|
||||||
@@ -434,6 +321,11 @@ impl Collector {
|
|||||||
*counter += 1;
|
*counter += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EventName::ClientIdle => {
|
||||||
|
let counter = stats.entry("cl_idle").or_insert(0);
|
||||||
|
*counter += 1;
|
||||||
|
}
|
||||||
|
|
||||||
EventName::ServerIdle => {
|
EventName::ServerIdle => {
|
||||||
let counter = stats.entry("sv_idle").or_insert(0);
|
let counter = stats.entry("sv_idle").or_insert(0);
|
||||||
*counter += 1;
|
*counter += 1;
|
||||||
@@ -454,64 +346,22 @@ impl Collector {
|
|||||||
*counter += 1;
|
*counter += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
EventName::ClientIdle => {
|
|
||||||
let counter = stats.entry("cl_idle").or_insert(0);
|
|
||||||
*counter += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update latest stats used in SHOW STATS
|
info!("{:?}", stats);
|
||||||
let mut guard = LATEST_STATS.lock();
|
|
||||||
for (key, value) in stats.iter() {
|
let mut pipeline = self.client.pipeline();
|
||||||
let entry = guard.entry(stat.address_id).or_insert(HashMap::new());
|
|
||||||
entry.insert(key.to_string(), value.clone());
|
for (key, value) in stats.iter_mut() {
|
||||||
|
pipeline.gauge(key, *value as f64);
|
||||||
|
*value = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// These are re-calculated every iteration of the loop, so we don't want to add values
|
pipeline.send(&self.client);
|
||||||
// from the last iteration.
|
|
||||||
for stat in &[
|
|
||||||
"cl_active",
|
|
||||||
"cl_waiting",
|
|
||||||
"cl_idle",
|
|
||||||
"sv_idle",
|
|
||||||
"sv_active",
|
|
||||||
"sv_tested",
|
|
||||||
"sv_login",
|
|
||||||
"maxwait",
|
|
||||||
"maxwait_us",
|
|
||||||
] {
|
|
||||||
stats.insert(stat, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
EventName::UpdateAverages => {
|
|
||||||
// Calculate averages
|
|
||||||
for stat in &[
|
|
||||||
"avg_query_count",
|
|
||||||
"avgxact_count",
|
|
||||||
"avg_sent",
|
|
||||||
"avg_received",
|
|
||||||
"avg_wait_time",
|
|
||||||
] {
|
|
||||||
let total_name = stat.replace("avg_", "total_");
|
|
||||||
let old_value = old_stats.entry(total_name.clone()).or_insert(0);
|
|
||||||
let new_value = stats.get(total_name.as_str()).unwrap_or(&0).to_owned();
|
|
||||||
let avg = (new_value - *old_value) / (STAT_PERIOD as i64 / 1_000); // Avg / second
|
|
||||||
|
|
||||||
stats.insert(stat, avg);
|
|
||||||
*old_value = new_value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a snapshot of statistics. Updated once a second
|
|
||||||
/// by the `Collector`.
|
|
||||||
pub fn get_stats() -> HashMap<usize, HashMap<String, i64>> {
|
|
||||||
LATEST_STATS.lock().clone()
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
|
|
||||||
-- \setrandom aid 1 :naccounts
|
|
||||||
\set aid random(1, 100000)
|
|
||||||
-- \setrandom bid 1 :nbranches
|
|
||||||
\set bid random(1, 100000)
|
|
||||||
-- \setrandom tid 1 :ntellers
|
|
||||||
\set tid random(1, 100000)
|
|
||||||
-- \setrandom delta -5000 5000
|
|
||||||
\set delta random(-5000,5000)
|
|
||||||
|
|
||||||
\set shard random(0, 2)
|
|
||||||
|
|
||||||
SET SHARD TO :shard;
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
|
|
||||||
|
|
||||||
SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
|
|
||||||
|
|
||||||
UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
|
|
||||||
|
|
||||||
UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
|
|
||||||
|
|
||||||
INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
|
|
||||||
|
|
||||||
END;
|
|
||||||
|
|
||||||
@@ -2,4 +2,3 @@ source "https://rubygems.org"
|
|||||||
|
|
||||||
gem "pg"
|
gem "pg"
|
||||||
gem "activerecord"
|
gem "activerecord"
|
||||||
gem "rubocop"
|
|
||||||
|
|||||||
@@ -11,33 +11,13 @@ GEM
|
|||||||
i18n (>= 1.6, < 2)
|
i18n (>= 1.6, < 2)
|
||||||
minitest (>= 5.1)
|
minitest (>= 5.1)
|
||||||
tzinfo (~> 2.0)
|
tzinfo (~> 2.0)
|
||||||
ast (2.4.2)
|
|
||||||
concurrent-ruby (1.1.9)
|
concurrent-ruby (1.1.9)
|
||||||
i18n (1.10.0)
|
i18n (1.10.0)
|
||||||
concurrent-ruby (~> 1.0)
|
concurrent-ruby (~> 1.0)
|
||||||
minitest (5.15.0)
|
minitest (5.15.0)
|
||||||
parallel (1.22.1)
|
|
||||||
parser (3.1.2.0)
|
|
||||||
ast (~> 2.4.1)
|
|
||||||
pg (1.3.2)
|
pg (1.3.2)
|
||||||
rainbow (3.1.1)
|
|
||||||
regexp_parser (2.3.1)
|
|
||||||
rexml (3.2.5)
|
|
||||||
rubocop (1.29.0)
|
|
||||||
parallel (~> 1.10)
|
|
||||||
parser (>= 3.1.0.0)
|
|
||||||
rainbow (>= 2.2.2, < 4.0)
|
|
||||||
regexp_parser (>= 1.8, < 3.0)
|
|
||||||
rexml (>= 3.2.5, < 4.0)
|
|
||||||
rubocop-ast (>= 1.17.0, < 2.0)
|
|
||||||
ruby-progressbar (~> 1.7)
|
|
||||||
unicode-display_width (>= 1.4.0, < 3.0)
|
|
||||||
rubocop-ast (1.17.0)
|
|
||||||
parser (>= 3.1.1.0)
|
|
||||||
ruby-progressbar (1.11.0)
|
|
||||||
tzinfo (2.0.4)
|
tzinfo (2.0.4)
|
||||||
concurrent-ruby (~> 1.0)
|
concurrent-ruby (~> 1.0)
|
||||||
unicode-display_width (2.1.0)
|
|
||||||
|
|
||||||
PLATFORMS
|
PLATFORMS
|
||||||
x86_64-linux
|
x86_64-linux
|
||||||
@@ -45,7 +25,6 @@ PLATFORMS
|
|||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
activerecord
|
activerecord
|
||||||
pg
|
pg
|
||||||
rubocop
|
|
||||||
|
|
||||||
BUNDLED WITH
|
BUNDLED WITH
|
||||||
2.3.7
|
2.3.7
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'active_record'
|
require 'active_record'
|
||||||
require 'pg'
|
|
||||||
|
|
||||||
$stdout.sync = true
|
|
||||||
|
|
||||||
# Uncomment these two to see all queries.
|
# Uncomment these two to see all queries.
|
||||||
# ActiveRecord.verbose_query_logs = true
|
# ActiveRecord.verbose_query_logs = true
|
||||||
@@ -113,17 +110,3 @@ begin
|
|||||||
rescue ActiveRecord::StatementInvalid
|
rescue ActiveRecord::StatementInvalid
|
||||||
puts 'OK'
|
puts 'OK'
|
||||||
end
|
end
|
||||||
|
|
||||||
# Test evil clients
|
|
||||||
def poorly_behaved_client
|
|
||||||
conn = PG::connect("postgres://sharding_user:sharding_user@127.0.0.1:6432/rails_dev")
|
|
||||||
conn.async_exec 'BEGIN'
|
|
||||||
conn.async_exec 'SELECT 1'
|
|
||||||
|
|
||||||
conn.close
|
|
||||||
puts 'Bad client ok'
|
|
||||||
end
|
|
||||||
|
|
||||||
25.times do
|
|
||||||
poorly_behaved_client
|
|
||||||
end
|
|
||||||
|
|||||||
Reference in New Issue
Block a user