Skip to main content

mz_mysql_util/
replication.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use mysql_async::Conn;
11use mysql_async::prelude::Queryable;
12
13use crate::MySqlError;
14
15/// Query a MySQL System Variable
16pub async fn query_sys_var(conn: &mut Conn, name: &str) -> Result<String, MySqlError> {
17    if !is_safe_sys_var_name(name) {
18        return Err(anyhow::anyhow!("invalid MySQL system variable name: {name}").into());
19    }
20
21    let value: String = conn.query_first(format!("SELECT @@{name}")).await?.unwrap();
22    Ok(value)
23}
24
25fn is_safe_sys_var_name(name: &str) -> bool {
26    !name.is_empty()
27        && name.split('.').all(|segment| {
28            !segment.is_empty()
29                && segment
30                    .chars()
31                    .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$')
32        })
33}
34
35/// Verify a MySQL System Variable matches the expected value
36async fn verify_sys_setting(
37    conn: &mut Conn,
38    setting: &str,
39    expected: &str,
40) -> Result<(), MySqlError> {
41    match query_sys_var(conn, setting).await?.as_str() {
42        actual if actual == expected => Ok(()),
43        actual => Err(MySqlError::InvalidSystemSetting {
44            setting: setting.to_string(),
45            expected: expected.to_string(),
46            actual: actual.to_string(),
47        }),
48    }
49}
50
51pub async fn ensure_full_row_binlog_format(conn: &mut Conn) -> Result<(), MySqlError> {
52    verify_sys_setting(conn, "log_bin", "1").await?;
53    verify_sys_setting(conn, "binlog_format", "ROW").await?;
54    verify_sys_setting(conn, "binlog_row_image", "FULL").await?;
55    Ok(())
56}
57
58pub async fn ensure_gtid_consistency(conn: &mut Conn) -> Result<(), MySqlError> {
59    verify_sys_setting(conn, "gtid_mode", "ON").await?;
60    verify_sys_setting(conn, "enforce_gtid_consistency", "ON").await?;
61    verify_sys_setting(conn, "gtid_next", "AUTOMATIC").await?;
62    Ok(())
63}
64
65/// In case this is a MySQL replica, we ensure that the replication settings are such that
66/// the replica would commit all transactions in the order they were committed on the primary.
67/// We don't really know that this is a replica, but if the settings indicate multi-threaded
68/// replication and the preserve-commit-order setting is not on, then it _could_ be a replica
69/// with correctness issues.
70/// We used to check `performance_schema.replication_connection_configuration` to determine if
71/// this was in-fact a replica but that requires non-standard privileges.
72/// Before MySQL 8.0.27, single-threaded was default and preserve-commit-order was not, and after
73/// 8.0.27 multi-threaded is default and preserve-commit-order is default on. So both of those
74/// default scenarios are fine. Unfortunately on some versions of MySQL on RDS, the default
75/// parameters use multi-threading without the preserve-commit-order setting on.
76pub async fn ensure_replication_commit_order(conn: &mut Conn) -> Result<(), MySqlError> {
77    // This system variables were renamed between MySQL 5.7 and 8.0
78    let is_multi_threaded = match query_sys_var(conn, "replica_parallel_workers").await {
79        Ok(val) => val != "0" && val != "1",
80        Err(_) => match query_sys_var(conn, "slave_parallel_workers").await {
81            Ok(val) => val != "0" && val != "1",
82            Err(err) => return Err(err),
83        },
84    };
85
86    if is_multi_threaded {
87        match verify_sys_setting(conn, "replica_preserve_commit_order", "1").await {
88            Ok(_) => Ok(()),
89            Err(_) => verify_sys_setting(conn, "slave_preserve_commit_order", "1").await,
90        }
91    } else {
92        Ok(())
93    }
94}