Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and Query Discovery Mode #21

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 61 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ Unlock the full potential of your database integrating ReadySet and ProxySQL by
This scheduler executes the following steps:

1. Locks an in disk file (configured by `lock_file`) to avoid multiple instances of the scheduler to overlap their execution.
2. Check if it can connect to Readyset and validate if `Snapshot Status` is `Completed`. In case it cannot connect or Readyset is still performing snapshot it adjust the server status to `SHUNNED` in ProxySQL.
2. Queries the table `stats_mysql_query_digest` from ProxySQL and validates if each query is supported by Readyset
2. If `mode=(All|HealthCheck)` - Query `mysql_servers` and check all servers that have `comment='Readyset` (case insensitive) and `hostgroup=readyset_hostgroup`. For each server it checks if it can connect to Readyset and validate if `Snapshot Status` is `Completed`. In case it cannot connect or Readyset is still performing snapshot it adjust the server status to `SHUNNED` in ProxySQL.
3. If `mode=(All|QueryDiscovery)` Query the table `stats_mysql_query_digest` finding queries executed at `source_hostgroup` by `readyset_user` and validates if each query is supported by Readyset. The rules to order queries are configured by [Query Discovery](#query-discovery) configurations.
3. If the query is supported it adds a cache in Readyset by executing `CREATE CACHE FROM __query__`.
4. If `warmup_time` is NOT configure, a new query rule will be added redirecting this query to Readyset
5. If `warmup_time` is configured, a new query rule will be added to mirror this query to Readyset. The query will still be redirected to the original hostgroup
6. Once `warmup_time` seconds has elapsed since the query was mirrored, the query rule will be updated to redirect the qury to Readyset instead of mirroring.
6. Once `warmup_time` seconds has elapsed since the query was mirrored, the query rule will be updated to redirect the query to Readyset instead of mirroring.



Expand All @@ -20,11 +20,13 @@ This scheduler executes the following steps:
Assuming you have your ProxySQL already Configured you will need to create a new hostgroup and add Readyset to this hostgroup:

```
INSERT INTO mysql_servers (hostgroup_id, hostname, port) VALUES (99, '127.0.0.1', 3307);
INSERT INTO mysql_servers (hostgroup_id, hostname, port, comment) VALUES (99, '127.0.0.1', 3307, 'Readyset');
LOAD MYSQL SERVERS TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;
```

*NOTE*: It's required to add `Readyset` as a comment to the server to be able to identify it in the scheduler.

To configure the scheduler to run execute:

```
Expand All @@ -40,13 +42,66 @@ Configure `/etc/readyset_proxysql_scheduler.cnf` as follow:
* `proxysql_port` - (Required) - Proxysql admin port
* `readyset_user` - (Required) - Readyset application user
* `readyset_password` - (Required) - Readyset application password
* `readyset_host` - (Required) - Readyset host
* `readyset_port` - (Required) - Readyset port
* `source_hostgroup` - (Required) - Hostgroup running your Read workload
* `readyset_hostgroup` - (Required) - Hostgroup where Readyset is configure
* `warmup_time` - (Optional) - Time in seconds to mirror a query supported before redirecting the query to Readyset (Default 0 - no mirror)
* `lock_file` - (Optional) - Lock file to prevent two instances of the scheduler to run at the same time (Default '/etc/readyset_scheduler.lock')
* `operation_mode` - (Optional) - Operation mode to run the scheduler. The options are described in [Operation Mode](#operation-mode) (Default All).
* `number_of_queries` - (Optional) - Number of queries to cache in Readyset (Default 10).
* `query_discovery_mode` / `query_discovery_min_execution` / `query_discovery_min_row_sent` - (Optional) - Query Discovery configurations. The options are described in [Query Discovery](#query-discovery) (Default CountStar / 0 / 0).


# Query Discovery
The Query Discovery is a set of configuration to find queries that are supported by Readyset. The configurations are defined by the following fields:

* `query_discovery_mode`: (Optional) - Mode to discover queries to automatically cache in Readyset. The options are described in [Query Discovery Mode](#query-discovery-mode) (Default CountStar).
* `query_discovery_min_execution`: (Optional) - Minimum number of executions of a query to be considered a candidate to be cached (Default 0).
* `query_discovery_min_row_sent`: (Optional) - Minimum number of rows sent by a query to be considered a candidate to be cached (Default 0).

# Query Discovery Mode
The Query Discovery Mode is a set of possible rules to discover queries to automatically cache in Readyset. The options are:

1. `CountStar` - Total Number of Query Executions
* Formula: `total_executions = count_star`
* Description: This metric gives the total number of times the query has been executed. It is valuable for understanding how frequently the query runs. A high count_star value suggests that the query is executed often.

2. `SumTime` - Total Time Spent Executing the Query
* Formula: `total_execution_time = sum_time`
* Description: This metric represents the total cumulative time spent (measured in microseconds) executing the query across all its executions. It provides a clear understanding of how much processing time the query is consuming over time. A high total execution time can indicate that the query is either frequently executed or is time-intensive to process.

3. `SumRowsSent` - Total Number of Rows Sent by the Query (sum_rows_sent)
* Formula: `total_rows_sent = sum_rows_sent`
* Description: This metric provides the total number of rows sent to the client across all executions of the query. It helps you understand the query’s output volume and the amount of data being transmitted.

4. `MeanTime` - Average Query Execution Time (Mean)
* Formula: `mean_time = sum_time / count_star`
* Description: The mean time gives you an idea of the typical performance (measured in microseconds) of the query over all executions. It provides a central tendency of how long the query generally takes to execute.

5. `ExecutionTimeDistance` - Time Distance Between Query Executions
* Formula: `execution_time_distance = max_time - min_time`
* Description: This shows the spread between the fastest and slowest executions of the query (measured in microseconds). A large range might indicate variability in system load, input sizes, or external factors affecting performance.

6. `QueryThroughput` - Query Throughput
* Formula: `query_throughput = count_star / sum_time`
* Description: This shows how many queries are processed per unit of time (measured in microseconds). It’s useful for understanding system capacity and how efficiently the database is handling the queries.

7. `WorstBestCase` - Worst Best-Case Query Performance
* Formula: `worst_case = max(min_time)`
* Description: The min_time metric gives the fastest time the query was ever executed (measured in microseconds). It reflects the best-case performance scenario, which could indicate the query’s performance under optimal conditions.

8. `WorstWorstCase` - Worst Worst-Case Query Performance
* Formula: `worst_case = max(max_time)`
* Description: The max_time shows the slowest time the query was executed (measured in microseconds). This can indicate potential bottlenecks or edge cases where the query underperforms, which could be due to larger data sets, locks, or high server load.

9. `DistanceMeanMax` - Distance Between Mean Time and Max Time (mean_time vs max_time)
* Formula: `distance_mean_max = max_time - mean_time`
* Description: The distance between the mean execution time and the maximum execution time provides insight into how much slower the worst-case execution is compared to the average (measured in microseconds). A large gap indicates significant variability in query performance, which could be caused by certain executions encountering performance bottlenecks, such as large datasets, locking, or high system load.

# Operation Mode
The Operation Mode is a set of possible rules to run the scheduler. The options are:
* `All` - Run `HealthCheck` and `QueryDiscovery` operations.
* `HealthCheck` - Run only the health check operation.
* `QueryDiscovery` - Run only the query discovery operation.

# Note
Readyset support of MySQL and this scheduler are alpha quality, meaning they are not currently part of our test cycle. Run your own testing before plugging this to your production system.
1 change: 1 addition & 0 deletions build/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
- "6034:6034"
environment:
- UPSTREAM_DB_URL=mysql://root:[email protected]:3306/noria
- LISTEN_ADDRESS=0.0.0.0:3307
depends_on:
mysql-master:
condition: service_healthy
Expand Down
10 changes: 5 additions & 5 deletions build/proxysql.cnf
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ mysql_variables=
monitor_password="noria"
}

mysql_users:
mysql_users=
(
{
username = "root"
password = "noria"
default_hostgroup = 1
username="root"
password="noria"
default_hostgroup=1
max_connections=1000
default_schema="noria"
active = 1
active=1
}
)
13 changes: 13 additions & 0 deletions build/test.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
proxysql_user = 'radmin'
proxysql_password = 'radmin'
proxysql_host = '127.0.0.1'
proxysql_port = 6032
readyset_user = 'root'
readyset_password = 'noria'
source_hostgroup = 1
readyset_hostgroup = 2
warmup_time_s = 10
lock_file = '/tmp/readyset_scheduler.lock'
operation_mode='All'
number_of_queries=2
query_discovery_mode='SumTime'
3 changes: 1 addition & 2 deletions readyset_proxysql_scheduler.cnf
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ proxysql_host = '127.0.0.1'
proxysql_port = 6032
readyset_user = 'root'
readyset_password = 'root'
readyset_host = '127.0.0.1'
readyset_port = 3307
source_hostgroup = 11
readyset_hostgroup = 99
warmup_time = 60
lock_file = '/tmp/readyset_scheduler.lock'
operation_mode="All"
number_of_queries=10
query_discovery_mode='SumTime'
42 changes: 38 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum OperationMode {

impl From<String> for OperationMode {
fn from(s: String) -> Self {
match s.as_str() {
match s.to_lowercase().as_str() {
"health_check" => OperationMode::HealthCheck,
"query_discovery" => OperationMode::QueryDiscovery,
"all" => OperationMode::All,
Expand All @@ -33,6 +33,39 @@ impl Display for OperationMode {
}
}

#[derive(serde::Deserialize, Clone, Copy, PartialEq, PartialOrd, Default)]
pub enum QueryDiscoveryMode {
#[default]
CountStar,
SumTime,
SumRowsSent,
MeanTime,
ExecutionTimeDistance,
QueryThroughput,
WorstBestCase,
WorstWorstCase,
DistanceMeanMax,
External,
}

impl From<String> for QueryDiscoveryMode {
fn from(s: String) -> Self {
match s.to_lowercase().as_str() {
"count_star" => QueryDiscoveryMode::CountStar,
"sum_time" => QueryDiscoveryMode::SumTime,
"sum_rows_sent" => QueryDiscoveryMode::SumRowsSent,
"mean_time" => QueryDiscoveryMode::MeanTime,
"execution_time_distance" => QueryDiscoveryMode::ExecutionTimeDistance,
"query_throughput" => QueryDiscoveryMode::QueryThroughput,
"worst_best_case" => QueryDiscoveryMode::WorstBestCase,
"worst_worst_case" => QueryDiscoveryMode::WorstWorstCase,
"distance_mean_max" => QueryDiscoveryMode::DistanceMeanMax,
"external" => QueryDiscoveryMode::External,
_ => QueryDiscoveryMode::CountStar,
}
}
}

#[derive(serde::Deserialize, Clone)]
pub struct Config {
pub proxysql_user: String,
Expand All @@ -41,14 +74,15 @@ pub struct Config {
pub proxysql_port: u16,
pub readyset_user: String,
pub readyset_password: String,
pub readyset_host: String,
pub readyset_port: u16,
pub source_hostgroup: u16,
pub readyset_hostgroup: u16,
pub warmup_time: Option<u16>,
pub warmup_time_s: Option<u16>,
pub lock_file: Option<String>,
pub operation_mode: Option<OperationMode>,
pub number_of_queries: u16,
pub query_discovery_mode: Option<QueryDiscoveryMode>,
pub query_discovery_min_execution: Option<u64>,
pub query_discovery_min_row_sent: Option<u64>,
}

pub fn read_config_file(path: &str) -> Result<String, std::io::Error> {
Expand Down
27 changes: 0 additions & 27 deletions src/health_check.rs

This file was deleted.

Loading