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.
910use mysql_async::Conn;
11use mysql_async::prelude::Queryable;
1213use crate::MySqlError;
1415/// Query a MySQL System Variable
16pub async fn query_sys_var(conn: &mut Conn, name: &str) -> Result<String, MySqlError> {
17let value: String = conn
18 .query_first(format!("SELECT @@{}", name))
19 .await?
20.unwrap();
21Ok(value)
22}
2324/// Verify a MySQL System Variable matches the expected value
25async fn verify_sys_setting(
26 conn: &mut Conn,
27 setting: &str,
28 expected: &str,
29) -> Result<(), MySqlError> {
30match query_sys_var(conn, setting).await?.as_str() {
31 actual if actual == expected => Ok(()),
32 actual => Err(MySqlError::InvalidSystemSetting {
33 setting: setting.to_string(),
34 expected: expected.to_string(),
35 actual: actual.to_string(),
36 }),
37 }
38}
3940pub async fn ensure_full_row_binlog_format(conn: &mut Conn) -> Result<(), MySqlError> {
41 verify_sys_setting(conn, "log_bin", "1").await?;
42 verify_sys_setting(conn, "binlog_format", "ROW").await?;
43 verify_sys_setting(conn, "binlog_row_image", "FULL").await?;
44Ok(())
45}
4647pub async fn ensure_gtid_consistency(conn: &mut Conn) -> Result<(), MySqlError> {
48 verify_sys_setting(conn, "gtid_mode", "ON").await?;
49 verify_sys_setting(conn, "enforce_gtid_consistency", "ON").await?;
50 verify_sys_setting(conn, "gtid_next", "AUTOMATIC").await?;
51Ok(())
52}
5354/// In case this is a MySQL replica, we ensure that the replication settings are such that
55/// the replica would commit all transactions in the order they were committed on the primary.
56/// We don't really know that this is a replica, but if the settings indicate multi-threaded
57/// replication and the preserve-commit-order setting is not on, then it _could_ be a replica
58/// with correctness issues.
59/// We used to check `performance_schema.replication_connection_configuration` to determine if
60/// this was in-fact a replica but that requires non-standard privileges.
61/// Before MySQL 8.0.27, single-threaded was default and preserve-commit-order was not, and after
62/// 8.0.27 multi-threaded is default and preserve-commit-order is default on. So both of those
63/// default scenarios are fine. Unfortunately on some versions of MySQL on RDS, the default
64/// parameters use multi-threading without the preserve-commit-order setting on.
65pub async fn ensure_replication_commit_order(conn: &mut Conn) -> Result<(), MySqlError> {
66// This system variables were renamed between MySQL 5.7 and 8.0
67let is_multi_threaded = match query_sys_var(conn, "replica_parallel_workers").await {
68Ok(val) => val != "0" && val != "1",
69Err(_) => match query_sys_var(conn, "slave_parallel_workers").await {
70Ok(val) => val != "0" && val != "1",
71Err(err) => return Err(err),
72 },
73 };
7475if is_multi_threaded {
76match verify_sys_setting(conn, "replica_preserve_commit_order", "1").await {
77Ok(_) => Ok(()),
78Err(_) => verify_sys_setting(conn, "slave_preserve_commit_order", "1").await,
79 }
80 } else {
81Ok(())
82 }
83}