File: sql_xa_second_phase.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (187 lines) | stat: -rw-r--r-- 7,745 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Copyright (c) 2022, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include "sql/xa/sql_xa_second_phase.h"  // Sql_cmd_xa_second_phase
#include "mysqld_error.h"                // Error codes
#include "sql/debug_sync.h"              // DEBUG_SYNC
#include "sql/mdl_context_backup.h"      // MDL_context_backup_manager
#include "sql/raii/sentry.h"             // raii::Sentry<>
#include "sql/rpl_gtid.h"                // gtid_state_commit_or_rollback
#include "sql/rpl_replica_commit_order_manager.h"  // Commit_order_manager
#include "sql/sql_class.h"                         // THD
#include "sql/transaction_info.h"                  // Transaction_ctx
#include "sql/xa/transaction_cache.h"              // xa::Transaction_cache

Sql_cmd_xa_second_phase::Sql_cmd_xa_second_phase(xid_t *xid_arg)
    : m_xid(xid_arg) {}

bool Sql_cmd_xa_second_phase::find_and_initialize_xa_context(THD *thd) {
  // XID_STATE object used in the TC_LOG/handler execution stack
  auto thd_xs = thd->get_transaction()->xid_state();
  this->m_detached_trx_context =
      find_trn_for_recover_and_check_its_state(thd, this->m_xid, thd_xs);

  if (this->m_detached_trx_context == nullptr) return (this->m_result = true);

  assert(*this->m_detached_trx_context->xid_state()->get_xid() == *this->m_xid);

  return false;
}

bool Sql_cmd_xa_second_phase::acquire_locks(THD *thd) {
  assert(this->m_detached_trx_context != nullptr);
  auto thd_xs = thd->get_transaction()->xid_state();
  auto detached_xs = this->m_detached_trx_context->xid_state();

  /*
    Metadata locks taken during XA COMMIT should be released when
    there is error in commit order execution, so we take a savepoint
    and rollback to it in case of error. The error during commit order
    execution can be temporary like commit order deadlock (check header
    comment for check_and_report_deadlock()) and can be recovered after
    retrying unlike other commit errors. And to do so we need to restore
    status of metadata locks i.e. rollback to savepoint, before the retry
    attempt to ensure order for applier threads.
  */
  this->m_mdl_savepoint = thd->mdl_context.mdl_savepoint();

  DEBUG_SYNC(thd, "detached_xa_commit_before_acquire_xa_lock");
  /*
    Acquire XID_STATE::m_xa_lock to prevent concurrent running of two
    XA COMMIT/XA ROLLBACK statements. Without acquiring this lock an attempt
    to run two XA COMMIT/XA ROLLBACK statement for the same xid value may lead
    to writing two events for the same xid into the binlog (e.g. twice
    XA COMMIT event, that is an event for XA COMMIT some_xid_value
    followed by an another event XA COMMIT with the same xid value).
    As a consequences, presence of two XA COMMIT/XA ROLLACK statements for
    the same xid value in binlog would break replication.
  */
  detached_xs->get_xa_lock().lock();

  raii::Sentry<> mutex_guard_on_error{[detached_xs, this]() -> void {
    if (this->m_result) detached_xs->get_xa_lock().unlock();
  }};

  /*
    Double check that the XA transaction still does exist since the
    transaction could be removed from the cache by another XA COMMIT/XA
    ROLLBACK statement being executed concurrently from parallel session with
    the same xid value.
  */
  if (find_trn_for_recover_and_check_its_state(thd, this->m_xid, thd_xs) ==
      nullptr)
    return (this->m_result = true);

  if (acquire_mandatory_metadata_locks(thd, this->m_xid)) {
    /*
      We can't rollback an XA transaction on lock failure due to
      Innodb redo log and bin log update is involved in rollback.
      Return error to user for a retry.
    */
    my_error(ER_XA_RETRY, MYF(0));
    return (this->m_result = true);
  }

  DEBUG_SYNC(thd, "detached_xa_commit_after_acquire_commit_lock");

  return false;
}

void Sql_cmd_xa_second_phase::release_locks() const {
  assert(this->m_detached_trx_context != nullptr);
  auto detached_xs = this->m_detached_trx_context->xid_state();
  detached_xs->get_xa_lock().unlock();
}

void Sql_cmd_xa_second_phase::setup_thd_context(THD *thd) {
  assert(this->m_detached_trx_context != nullptr);
  auto thd_xs = thd->get_transaction()->xid_state();
  auto detached_xs = this->m_detached_trx_context->xid_state();

  std::tie(this->m_gtid_error, this->m_need_clear_owned_gtid) =
      commit_owned_gtids(thd, true);
  if (this->m_gtid_error) my_error(ER_XA_RBROLLBACK, MYF(0));
  this->m_result = detached_xs->xa_trans_rolled_back() || this->m_gtid_error;

  assert(thd_xs->is_binlogged() == false);
  /*
    Detached transaction XID_STATE::is_binlogged() is passed through THD
    XID_STATE class member to low-level logging routines for deciding how
    to log.
  */
  if (detached_xs->is_binlogged())
    thd_xs->set_binlogged();
  else
    thd_xs->unset_binlogged();
}

bool Sql_cmd_xa_second_phase::enter_commit_order(THD *thd) {
  auto thd_xs = thd->get_transaction()->xid_state();

  if (Commit_order_manager::wait(
          thd)) {  // Ensure externalization order for applier threads
                   // (no-op for non-applier threads).

    Commit_order_manager::wait_and_finish(
        thd, true);  // Remove from commit order queue and allow for a retry

    gtid_state_commit_or_rollback(thd, /* needs_to */ true,
                                  /* do_commit */ false);
    thd->mdl_context.rollback_to_savepoint(this->m_mdl_savepoint);
    thd_xs->unset_binlogged();
    return (this->m_result = true);
  }
  return false;
}

void Sql_cmd_xa_second_phase::assign_xid_to_thd(THD *thd) const {
  assert(this->m_detached_trx_context != nullptr);
  auto thd_xs = thd->get_transaction()->xid_state();
  thd_xs->start_detached_xa(this->m_xid, thd_xs->is_binlogged());
}

void Sql_cmd_xa_second_phase::exit_commit_order(THD *thd) const {
  Commit_order_manager::wait_and_finish(
      thd, this->m_result);  // Remove from the commit order queue (no-op for
                             // non-applier threads).
}

void Sql_cmd_xa_second_phase::cleanup_context(THD *thd) const {
  assert(this->m_detached_trx_context != nullptr);
  auto thd_xs = thd->get_transaction()->xid_state();

  // Restoring the binlogged status of the thd_xid_state after borrowing it
  // to pass the binlogged flag to binlog_xa_commit().
  thd_xs->unset_binlogged();

  MDL_context_backup_manager::instance().delete_backup(
      this->m_xid->key(), this->m_xid->key_length());

  xa::Transaction_cache::remove(this->m_detached_trx_context.get());
  gtid_state_commit_or_rollback(thd, this->m_need_clear_owned_gtid,
                                !this->m_result);
}

void Sql_cmd_xa_second_phase::dispose() {
  this->m_detached_trx_context.reset();
}