Skip to content

Commit dce1e5e

Browse files
Add verbosity level to log messages
This commit adds a verbosity level to log messages. The verbosity level can be configure in the .cnf file or else Note will be assumed by default. This is to avoid the scheduler from flooding the logs with messages that indicate the scheduler is running as expected. There are 4 log levels: - Info - Note - Warning - Error The last two are obvious, but the first two are as follows: - Info: This is just and information, the action that follow this will not change the state of the proxy. - Note: This is a note to the user that the action that follows this will most likely change the state of the proxy. Closes #16
1 parent bbecb53 commit dce1e5e

File tree

10 files changed

+91
-23
lines changed

10 files changed

+91
-23
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ target/
1010
*.pdb
1111

1212
.vscode/
13+
14+
# Demo config
15+
config_demo.cnf

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ toml = "0.8.12"
1212
serde = "1.0"
1313
chrono = "0.4.35"
1414
file-guard = "0.2.0"
15+
once_cell = "1.10.0"
1516

1617

1718
[package.metadata.generate-rpm]

build/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ services:
1313
environment:
1414
- MYSQL_ROOT_PASSWORD=noria
1515
- MYSQL_DATABASE=noria
16-
command: --server-id=1
16+
command: --server-id=1 --local-infile=1
1717
ports:
1818
- "3306:3306"
1919
healthcheck:
@@ -28,7 +28,7 @@ services:
2828
- "3307:3307"
2929
- "6034:6034"
3030
environment:
31-
- UPSTREAM_DB_URL=mysql://root:[email protected]:3306/noria
31+
- UPSTREAM_DB_URL=mysql://root:[email protected]:3306/imdb
3232
- LISTEN_ADDRESS=0.0.0.0:3307
3333
depends_on:
3434
mysql-master:

readyset_proxysql_scheduler.cnf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ source_hostgroup = 11
88
readyset_hostgroup = 99
99
warmup_time_s = 60
1010
lock_file = '/tmp/readyset_scheduler.lock'
11-
operation_mode="All"
11+
operation_mode='All'
1212
number_of_queries=10
13-
query_discovery_mode='SumTime'
13+
query_discovery_mode='SumTime'
14+
log_verbosity='Note'

src/config.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use std::{
44
io::Read,
55
};
66

7-
#[derive(serde::Deserialize, Clone, Copy, PartialEq, PartialOrd, Default)]
7+
use crate::messages::MessageType;
8+
9+
#[derive(serde::Deserialize, Clone, Copy, PartialEq, PartialOrd, Default, Debug)]
810
pub enum OperationMode {
911
HealthCheck,
1012
QueryDiscovery,
@@ -33,7 +35,7 @@ impl Display for OperationMode {
3335
}
3436
}
3537

36-
#[derive(serde::Deserialize, Clone, Copy, PartialEq, PartialOrd, Default)]
38+
#[derive(serde::Deserialize, Clone, Copy, PartialEq, PartialOrd, Default, Debug)]
3739
pub enum QueryDiscoveryMode {
3840
#[default]
3941
CountStar,
@@ -66,7 +68,7 @@ impl From<String> for QueryDiscoveryMode {
6668
}
6769
}
6870

69-
#[derive(serde::Deserialize, Clone)]
71+
#[derive(serde::Deserialize, Clone, Debug)]
7072
pub struct Config {
7173
pub proxysql_user: String,
7274
pub proxysql_password: String,
@@ -83,6 +85,7 @@ pub struct Config {
8385
pub query_discovery_mode: Option<QueryDiscoveryMode>,
8486
pub query_discovery_min_execution: Option<u64>,
8587
pub query_discovery_min_row_sent: Option<u64>,
88+
pub log_verbosity: Option<MessageType>,
8689
}
8790

8891
pub fn read_config_file(path: &str) -> Result<String, std::io::Error> {

src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod queries;
77
use clap::Parser;
88
use config::read_config_file;
99
use file_guard::Lock;
10+
use messages::MessageType;
1011
use mysql::{Conn, OptsBuilder};
1112
use proxysql::ProxySQL;
1213
use std::fs::OpenOptions;
@@ -22,10 +23,11 @@ struct Args {
2223
}
2324

2425
fn main() {
25-
messages::print_info("Running readyset_scheduler");
2626
let args = Args::parse();
2727
let config_file = read_config_file(&args.config).expect("Failed to read config file");
2828
let config = config::parse_config_file(&config_file).expect("Failed to parse config file");
29+
messages::set_log_verbosity(config.clone().log_verbosity.unwrap_or(MessageType::Note));
30+
messages::print_info("Running readyset_scheduler");
2931
let file = match OpenOptions::new()
3032
.read(true)
3133
.write(true)

src/messages.rs

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,77 @@
11
use std::process;
22

33
use chrono::{DateTime, Local};
4+
use once_cell::sync::Lazy;
5+
use std::sync::Mutex;
46

5-
enum MessageType {
7+
#[derive(Clone, Copy, serde::Deserialize, Debug, PartialEq, PartialOrd)]
8+
pub enum MessageType {
9+
/// Information message, this will not result in any action
610
Info,
11+
/// Note message, this will result in some action that changes state
12+
Note,
13+
/// Warning message
714
Warning,
15+
/// Error message
816
Error,
917
}
18+
19+
impl std::fmt::Display for MessageType {
20+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
21+
match self {
22+
MessageType::Info => write!(f, "Info"),
23+
MessageType::Note => write!(f, "Note"),
24+
MessageType::Warning => write!(f, "Warning"),
25+
MessageType::Error => write!(f, "Error"),
26+
}
27+
}
28+
}
29+
30+
impl Default for MessageType {
31+
fn default() -> Self {
32+
MessageType::Note
33+
}
34+
}
35+
36+
static LOG_VERBOSITY: Lazy<Mutex<MessageType>> = Lazy::new(|| Mutex::new(MessageType::default()));
37+
38+
pub fn set_log_verbosity(level: MessageType) {
39+
let mut verbosity = LOG_VERBOSITY.lock().unwrap();
40+
*verbosity = level;
41+
}
42+
43+
pub fn get_log_verbosity() -> MessageType {
44+
let verbosity = LOG_VERBOSITY.lock().unwrap();
45+
*verbosity
46+
}
47+
1048
fn print_message_with_ts(message: &str, message_type: MessageType) {
1149
let datetime_now: DateTime<Local> = Local::now();
1250
let date_formatted = datetime_now.format("%Y-%m-%d %H:%M:%S");
1351
let pid = process::id();
1452
match message_type {
15-
MessageType::Info => println!("{} [INFO] Readyset[{}]: {}", date_formatted, pid, message),
16-
MessageType::Warning => println!(
17-
"{} [WARNING] Readyset[{}]: {}",
18-
date_formatted, pid, message
19-
),
53+
MessageType::Info => {
54+
if MessageType::Info >= get_log_verbosity() {
55+
println!("{} [INFO] Readyset[{}]: {}", date_formatted, pid, message);
56+
}
57+
}
58+
MessageType::Note => {
59+
if MessageType::Note >= get_log_verbosity() {
60+
println!("{} [NOTE] Readyset[{}]: {}", date_formatted, pid, message);
61+
}
62+
}
63+
MessageType::Warning => {
64+
if MessageType::Warning >= get_log_verbosity() {
65+
eprintln!(
66+
"{} [WARNING] Readyset[{}]: {}",
67+
date_formatted, pid, message
68+
);
69+
}
70+
}
2071
MessageType::Error => {
21-
eprintln!("{} [ERROR] Readyset[{}]: {}", date_formatted, pid, message)
72+
if MessageType::Error >= get_log_verbosity() {
73+
eprintln!("{} [ERROR] Readyset[{}]: {}", date_formatted, pid, message);
74+
}
2275
}
2376
}
2477
}
@@ -27,6 +80,10 @@ pub fn print_info(message: &str) {
2780
print_message_with_ts(message, MessageType::Info);
2881
}
2982

83+
pub fn print_note(message: &str) {
84+
print_message_with_ts(message, MessageType::Note);
85+
}
86+
3087
pub fn print_warning(message: &str) {
3188
print_message_with_ts(message, MessageType::Warning);
3289
}

src/proxysql.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ impl ProxySQL {
7676
let date_formatted = datetime_now.format("%Y-%m-%d %H:%M:%S");
7777
if self.warmup_time_s > 0 {
7878
self.conn.query_drop(format!("INSERT INTO mysql_query_rules (username, mirror_hostgroup, active, digest, apply, comment) VALUES ('{}', {}, 1, '{}', 1, '{}: {}')", query.get_user(), self.readyset_hostgroup, query.get_digest(), MIRROR_QUERY_TOKEN, date_formatted)).expect("Failed to insert into mysql_query_rules");
79-
messages::print_info("Inserted warm-up rule");
79+
messages::print_note("Inserted warm-up rule");
8080
} else {
8181
self.conn.query_drop(format!("INSERT INTO mysql_query_rules (username, destination_hostgroup, active, digest, apply, comment) VALUES ('{}', {}, 1, '{}', 1, '{}: {}')", query.get_user(), self.readyset_hostgroup, query.get_digest(), DESTINATION_QUERY_TOKEN, date_formatted)).expect("Failed to insert into mysql_query_rules");
82-
messages::print_info("Inserted destination rule");
82+
messages::print_note("Inserted destination rule");
8383
}
8484
Ok(true)
8585
}
@@ -147,7 +147,7 @@ impl ProxySQL {
147147
comment, date_formatted
148148
);
149149
self.conn.query_drop(format!("UPDATE mysql_query_rules SET mirror_hostgroup = NULL, destination_hostgroup = {}, comment = '{}' WHERE rule_id = {}", self.readyset_hostgroup, comment, rule_id)).expect("Failed to update rule");
150-
messages::print_info(
150+
messages::print_note(
151151
format!("Updated rule ID {} from warmup to destination", rule_id).as_str(),
152152
);
153153
updated_rules = true;
@@ -168,7 +168,7 @@ impl ProxySQL {
168168
if ready {
169169
status_changes.push((host, HostStatus::Online));
170170
} else {
171-
messages::print_info("Readyset is still running Snapshot.");
171+
messages::print_note("Readyset is still running Snapshot.");
172172
status_changes.push((host, HostStatus::Shunned));
173173
}
174174
}
@@ -187,7 +187,7 @@ impl ProxySQL {
187187
host.get_hostname(),
188188
host.get_port()
189189
);
190-
messages::print_info(
190+
messages::print_note(
191191
format!(
192192
"Server HG: {}, Host: {}, Port: {} is currently {}. Changing to {}",
193193
self.readyset_hostgroup,

src/queries.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl QueryDiscovery {
178178
break;
179179
}
180180
let digest_text = self.replace_placeholders(query.get_digest_text());
181-
messages::print_info(
181+
messages::print_note(
182182
format!("Going to test query support for {}", digest_text).as_str(),
183183
);
184184
let supported = proxysql
@@ -187,7 +187,7 @@ impl QueryDiscovery {
187187
.check_query_support(&digest_text, query.get_schema()); // Safe to unwrap because we checked if hosts is empty
188188
match supported {
189189
Ok(true) => {
190-
messages::print_info(
190+
messages::print_note(
191191
"Query is supported, adding it to proxysql and readyset"
192192
.to_string()
193193
.as_str(),
@@ -203,7 +203,7 @@ impl QueryDiscovery {
203203
current_queries_digest.push(query.get_digest().to_string());
204204
}
205205
Ok(false) => {
206-
messages::print_info("Query is not supported");
206+
messages::print_note("Query is not supported");
207207
}
208208
Err(err) => {
209209
messages::print_warning(

0 commit comments

Comments
 (0)