Bug #122010 CSA: starting or configuring a channel can crash mysqld, hang STOP REPLICA, or break commit order

Submitted: 30 Nov 22:10
Reporter: Derrick Yang (OCA) Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Replication Severity:S2 (Serious)
Version:26.7.0, trunk OS:Linux
Assigned to: CPU Architecture:Any
Tags: CSA, replication

[30 Nov 22:10] Derrick Yang
Description:
1. A failed START REPLICA crashes mysqld.
   Csa_service::run() adds the channel to csa_channels before it creates
   the worker thread pool, scheduler, session pool and relay log reader.
   If the worker threads cannot be created (thread_pool->init() fails, for
   example with a low ulimit -u, memory pressure or a large
   APPLIER_WORKER_COUNT), or an exception is caught, run() reports the
   error and returns with the entry still in the map and those members
   null. The SQL thread then always calls Csa_service::remove(), and
   clean_up_context() calls thread_pool->size() on the null pointer:
   SIGSEGV. The other early returns of run() leave the channel's
   conflict manager thread running.

2. STOP REPLICA hangs if the conflict manager thread was not created.
   Transaction_conflict_manager::start() does not check whether its thread
   was created. stop() then waits for m_end, which only that thread sets,
   so the SQL thread never finishes stopping and STOP REPLICA (or any end
   of the run) hangs until mysqld is restarted. While the channel runs, a
   commit order deadlock is never resolved, because no thread performs the
   rollback.

3. Creating and deleting named channels runs out of CSA instance ids.
   Each channel gets a channel_instance_id, used as an index into CSA
   arrays of 256 entries (Constants::max_instances).
   Multisource_info::delete_mi() gives the id back for reuse only when the
   default channel is deleted, so every CHANGE REPLICATION SOURCE ... FOR
   CHANNEL 'c' / RESET REPLICA ALL FOR CHANNEL 'c' cycle uses up an id. After
   256 cycles, START REPLICA on a CSA channel calls
   Transaction_conflict_monitor::get(256), which only asserts the range:
   - debug builds abort: Assertion `instance_id < max_instances' failed;
   - release builds write one slot past the array, then fail with
     ER_SERVER_OUT_OF_RESOURCES and leave the conflict manager thread
     running, and the next START REPLICA hangs.

4. An APPLIER_EVENT_MEMORY_LIMIT below 1 GiB is accepted, then rejected at
   restart.
   CHANGE REPLICATION SOURCE accepts any value at or above
   replica_max_allowed_packet (smaller values are raised to it with a
   warning). Relay_log_info::read_info() rejects any stored value below
   1 GiB. With replica_max_allowed_packet below 1 GiB, a limit the channel
   accepted and used makes it fail to load after the next restart:
     [ERROR] [MY-011059] [Repl] Error reading relay log configuration.
   START REPLICA then fails with ER_REPLICA_AM_INIT_REPOSITORY (1872)
   until the stored row is fixed and the server restarted again. The tests
   rpl_csa_memory_limit and rpl_applier_metadata_persistence_csa_config
   use small limits on purpose, so values below 1 GiB are intended.

5. An APPLIER_EVENT_MEMORY_LIMIT of 4 GiB or more is cut to 32 bits.
   Relay_log_info keeps the limit in a 32-bit field and the
   mysql.slave_relay_log_info column is INTEGER UNSIGNED, but nothing
   rejects a larger value. The parser stores it in an unsigned long, so on
   Windows, where unsigned long is 32 bits, it wraps already while parsing;
   on Linux it wraps when it is stored. APPLIER_EVENT_MEMORY_LIMIT =
   4294968296 (4 GiB + 1000) is accepted without a warning and becomes 1000. The channel then runs
   with a 1000-byte budget, and fails to load after a restart (problem 4).

6. With one CSA worker, transactions commit out of source order.
   handle_slave_sql() creates the Commit_order_manager only when there is
   more than one worker. That is correct for the classic applier, which
   applies one transaction at a time in relay log order. CSA does not: its
   scheduler releases a transaction as soon as the one it depends on has
   finished. With no Commit_order_manager, Job_binlog::run() prepares and
   commits in one phase, so the release order becomes the commit order.
   With APPLIER_WORKER_COUNT = 1 and replica_preserve_commit_order = ON (the
   default), independent transactions can commit in a different order
   from the source. Readers see states the source never had, the replica's
   binary log has a different order, and gtid_executed has gaps. The final
   data is correct.

How to repeat:
Problem 1 (debug build):
  The patch adds a debug point that makes the worker thread creation fail.
  On trunk, add it by hand in Csa_service::run():
    -    if (thread_pool->init()) {
    +    if (DBUG_EVALUATE_IF("simulate_csa_thread_pool_init_failure", true,
    +                         thread_pool->init())) {
  Then, on a CSA replica with the applier stopped:
    SET GLOBAL debug = '+d,simulate_csa_thread_pool_init_failure';
    START REPLICA SQL_THREAD;
  mysqld crashes with signal 11 in Csa_service::clean_up_context(). With the
  fix, the SQL thread stops with ER_REPLICA_FATAL_ERROR and a later START
  REPLICA works. The MTR test rpl_gtid.rpl_csa_start_failure in the patch
  covers this and problem 3.

Problem 2 (Linux, as a non-root user, with unit tests):
  The thread creation is made to fail by lowering RLIMIT_NPROC around
  Transaction_conflict_manager::start(), in a death-test child process:
    ninja merge_large_tests-t
    runtime_output_directory/merge_large_tests-t \
      --gtest_filter='RplCsaConflictManagerTest.*'
  On trunk (with the test's start() return value ignored),
  FailedThreadCreation fails because stop() does not return within 5
  seconds. StartAndStop passes.

Problem 3 (any build):
  On a replica with GTID_MODE=ON:
    -- repeat 256 times
    CHANGE REPLICATION SOURCE TO SOURCE_HOST = '127.0.0.1',
      SOURCE_PORT = <port>, SOURCE_USER = 'root' FOR CHANNEL 'ch';
    RESET REPLICA ALL FOR CHANNEL 'ch';
    -- then
    CHANGE REPLICATION SOURCE TO SOURCE_HOST = '127.0.0.1',
      SOURCE_PORT = <port>, SOURCE_USER = 'root', SOURCE_AUTO_POSITION = 1,
      REQUIRE_ROW_FORMAT = 1, GTID_ONLY = 1, APPLIER_VERSION = 2
      FOR CHANNEL 'ch';
    START REPLICA SQL_THREAD FOR CHANNEL 'ch';
  Debug build: mysqld aborts with Assertion `instance_id < max_instances'.
  Release build: the SQL thread stops with "Out of memory"
  (ER_SERVER_OUT_OF_RESOURCES), and a second START REPLICA hangs.

Problem 4 (any build):
  On a CSA replica (SOURCE_AUTO_POSITION = 1, REQUIRE_ROW_FORMAT = 1,
  GTID_ONLY = 1, APPLIER_VERSION = 2):
    STOP REPLICA;
    SET GLOBAL replica_max_allowed_packet = 1048576;
    CHANGE REPLICATION SOURCE TO APPLIER_EVENT_MEMORY_LIMIT = 67108864;
  Restart the replica. The error log has "Error reading relay log
  configuration", and START REPLICA fails with error 1872.

Problem 5 (any build):
  On a CSA replica:
    STOP REPLICA;
    CHANGE REPLICATION SOURCE TO APPLIER_EVENT_MEMORY_LIMIT = 4294968296;
    SHOW WARNINGS;   -- empty
    SELECT APPLIER_EVENT_MEMORY_LIMIT
      FROM performance_schema.replication_applier_configuration;   -- 1000

Problem 6 (any build):
  On the source:
    CREATE TABLE t1 (a INT PRIMARY KEY, b INT);
    CREATE TABLE t2 (a INT PRIMARY KEY, b INT);
    INSERT INTO t1 VALUES (1, 0);
    INSERT INTO t2 VALUES (1, 0);
  On the replica:
    STOP REPLICA;
    CHANGE REPLICATION SOURCE TO SOURCE_AUTO_POSITION = 1,
      REQUIRE_ROW_FORMAT = 1, GTID_ONLY = 1,
      APPLIER_VERSION = 2, APPLIER_WORKER_COUNT = 1;
    START REPLICA;
    FLUSH BINARY LOGS;
  In a second replica session, hold the row that A updates:
    BEGIN; SELECT b FROM t1 WHERE a = 1 FOR UPDATE;
  On the source:
    UPDATE t1 SET b = b + 1 WHERE a = 1;    -- A
    UPDATE t2 SET b = b + 1 WHERE a = 1;    -- B
    UPDATE t2 SET b = b + 10 WHERE a = 1;   -- C, depends on B
    UPDATE t1 SET b = b + 10 WHERE a = 1;   -- D, depends on A
  On the replica, wait until A waits for the row lock
  (performance_schema.data_lock_waits), wait one more second, then COMMIT
  in the second session. After the replica catches up, SHOW BINLOG EVENTS
  on its new binary log shows the GTIDs in the order A, B, D, C. With
  APPLIER_WORKER_COUNT = 2 the order is A, B, C, D.
  The MTR test rpl_gtid.rpl_csa_one_worker_commit_order in the patch does
  this and checks the order.

Suggested fix:
1. In Csa_service::run(), a scope guard removes the csa_channels entry and
   stops the conflict manager thread on any early return. The guard is
   declared after the service write lock is taken, so it runs before the
   lock is released, and no other thread sees a half-built entry. run()
   also rejects a channel_instance_id at or above Constants::max_instances
   with ER_SERVER_OUT_OF_RESOURCES.
2. Transaction_conflict_manager::start() returns true when its thread
   cannot be created, and sets m_end, the flag stop() waits for, so stop()
   returns at once. stop() joins the thread only if it was created. run()
   reports the failure as ER_REPLICA_FATAL_ERROR.
3. Multisource_info::delete_mi() puts the instance id of every deleted
   channel back for reuse. Reuse is safe: delete_mi() runs with the channel
   stopped and under the channel_map write lock, and run() resets the
   per-instance CSA state (conflict manager, statistics, memory budget) on
   every START.
4. Relay_log_info::read_info() accepts any stored limit that CHANGE
   REPLICATION SOURCE can write. It rejects only 0, which is never written
   and means the row is damaged. Like CHANGE REPLICATION SOURCE, it raises a
   value below replica_max_allowed_packet to that value, so the largest
   event fits.
5. evaluate_inter_option_dependencies() rejects an
   APPLIER_EVENT_MEMORY_LIMIT above 4294967295, the largest value the
   32-bit field and the column hold, with ER_WRONG_VALUE before any option
   is applied. LEX_SOURCE_INFO::applier_event_memory_limit is ulonglong and
   the grammar parses the option with ulonglong_num, so the check sees the
   value as typed on every platform.
6. handle_slave_sql() creates a Commit_order_manager for a CSA channel
   whenever replica_preserve_commit_order is ON, even with one worker. The
   classic applier still creates one only with more than one worker
   (Bug#33048169).

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.073 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.