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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/* Copyright (c) 2015, 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 */
#ifndef TC_LOG_H
#define TC_LOG_H
#include <assert.h>
#include <stddef.h>
#include <sys/types.h>
#include "my_inttypes.h"
#include "my_io.h"
#include "my_sys.h" // my_msync
#include "mysql/components/services/bits/mysql_cond_bits.h"
#include "mysql/components/services/bits/mysql_mutex_bits.h"
#include "mysql/psi/mysql_cond.h"
class THD;
typedef ulonglong my_xid;
#define TC_LOG_MIN_PAGES 6
namespace trx_coordinator {
/**
Commits detached XA transaction by XID in all storage engines.
@pre thd->transaction.flags.commit_low == true
@post thd->transaction.flags.commit_low == false
@param thd The THD session object holding the detached XA/XID.
@param run_after_commit
True by default, otherwise, does not execute the
after_commit hook in the function.
@return false if execution succeeded, true otherwise.
*/
bool commit_detached_by_xid(THD *thd, bool run_after_commit = true);
/**
Rolls back detached XA transaction by XID in all storage engines.
@param thd The THD session object holding the detached XA/XID.
@return false if execution succeeded, true otherwise.
*/
bool rollback_detached_by_xid(THD *thd);
/**
Commits the underlying transaction in storage engines.
Determines if the transaction to commit is attached to the `thd`
parameter or, instead, the `thd` parameter holds the XID for a detached
transaction to be committed.
@param thd THD session object.
@param all Is set in case of explicit commit (COMMIT statement), or
implicit commit issued by DDL. Is not set when called at the
end of statement, even if autocommit=1.
@param run_after_commit
True by default, otherwise, does not execute the
after_commit hook in the function.
@return false if the transaction was committed, true if an error
occurred.
*/
bool commit_in_engines(THD *thd, bool all = false,
bool run_after_commit = true);
/**
Rolls back the underlying transaction in storage engines.
Determines if the transaction to rollback is attached to the `thd`
parameter or, instead, the `thd` parameter holds the XID for a detached
transaction to be rolled back.
@param thd THD session object.
@param all Is set in case of explicit commit (COMMIT statement), or
implicit commit issued by DDL. Is not set when called at the
end of statement, even if autocommit=1.
@return false if the transaction was rolled back, true if an error
occurred.
*/
bool rollback_in_engines(THD *thd, bool all = false);
/**
Marks the underlying transaction as `PREPARED_IN_TC` in storage engines.
The underlying executing statement is tested in order to understand if
the transaction should be marked. The accepted statements are:
- XA PREPARE [xid]
@param thd THD session object.
@param all Is set in case of explicit commit (COMMIT statement), or
implicit commit issued by DDL. Is not set when called at the
end of statement, even if autocommit=1.
@return 0 id the transaction was marked as `PREPARED_IN_TC` in
storage engines, error code otherwise.
*/
int set_prepared_in_tc_in_engines(THD *thd, bool all = false);
/**
Checks whether or not the underlying statement should trigger setting the
transaction to `PREPARED_IN_TC` state. The accepted statemets are:
- XA PREPARE [xid]
@param thd THD session object.
@return true if the underlying statement should trigger setting the
transaction as `PREPARED_IN_TC`, false if not
*/
bool should_statement_set_prepared_in_tc(THD *thd);
} // namespace trx_coordinator
/**
Transaction Coordinator Log.
A base abstract class for three different implementations of the
transaction coordinator.
The server uses the transaction coordinator to order transactions
correctly and there are three different implementations: one using
an in-memory structure, one dummy that does not do anything, and one
using the binary log for transaction coordination.
*/
class TC_LOG {
public:
/**
Perform heuristic recovery, if --tc-heuristic-recover was used.
@note no matter whether heuristic recovery was successful or not
mysqld must exit. So, return value is the same in both cases.
@retval false no heuristic recovery was requested
@retval true heuristic recovery was performed
*/
bool using_heuristic_recover();
TC_LOG() = default;
virtual ~TC_LOG() = default;
enum enum_result { RESULT_SUCCESS, RESULT_ABORTED, RESULT_INCONSISTENT };
/**
Initialize and open the coordinator log.
Do recovery if necessary. Called during server startup.
@param opt_name Name of logfile.
@retval 0 success
@retval 1 failed
*/
virtual int open(const char *opt_name) = 0;
/**
Close the transaction coordinator log and free any resources.
Called during server shutdown.
*/
virtual void close() = 0;
/**
Log a commit record of the transaction to the transaction
coordinator log.
When the function returns, the transaction commit is properly
logged to the transaction coordinator log and can be committed in
the storage engines.
@param thd Session to log transaction for.
@param all @c True if this is a "real" commit, @c false if it is a
"statement" commit.
@return Error code on failure, zero on success.
*/
virtual enum_result commit(THD *thd, bool all) = 0;
/**
Log a rollback record of the transaction to the transaction
coordinator log.
When the function returns, the transaction have been aborted in
the transaction coordinator log.
@param thd Session to log transaction record for.
@param all @c true if an explicit commit or an implicit commit
for a statement, @c false if an internal commit of the statement.
@return Error code on failure, zero on success.
*/
virtual int rollback(THD *thd, bool all) = 0;
/**
Log a prepare record of the transaction to the storage engines.
@param thd Session to log transaction record for.
@param all @c true if an explicit commit or an implicit commit
for a statement, @c false if an internal commit of the statement.
@return Error code on failure, zero on success.
*/
virtual int prepare(THD *thd, bool all) = 0;
};
class TC_LOG_DUMMY : public TC_LOG // use it to disable the logging
{
public:
TC_LOG_DUMMY() = default;
int open(const char *) override;
void close() override {}
enum_result commit(THD *thd, bool all) override;
int rollback(THD *thd, bool all) override;
int prepare(THD *thd, bool all) override;
};
class TC_LOG_MMAP : public TC_LOG {
public: // only to keep Sun Forte on sol9x86 happy
typedef enum {
PS_POOL, // page is in pool
PS_ERROR, // last sync failed
PS_DIRTY // new xids added since last sync
} PAGE_STATE;
private:
struct PAGE {
PAGE *next; // pages are linked in a fifo queue
my_xid *start, *end; // usable area of a page
my_xid *ptr; // next xid will be written here
int size, free; // max and current number of free xid slots on the page
int waiters; // number of waiters on condition
PAGE_STATE state; // see above
/**
Signalled when syncing of this page is done or when
this page is in "active" slot and syncing slot just
became free.
*/
mysql_cond_t cond;
};
char logname[FN_REFLEN];
File fd;
my_off_t file_length;
uint npages, inited;
uchar *data;
PAGE *pages, *syncing, *active, *pool, **pool_last_ptr;
/*
LOCK_tc is used to protect access both to data members 'syncing',
'active', 'pool' and to the content of PAGE objects.
*/
mysql_mutex_t LOCK_tc;
/**
Signalled when active PAGE is moved to syncing state,
thus member "active" becomes 0.
*/
mysql_cond_t COND_active;
/**
Signalled when one more page becomes available in the
pool which we might select as active.
*/
mysql_cond_t COND_pool;
public:
TC_LOG_MMAP() : inited(0) {}
int open(const char *opt_name) override;
void close() override;
enum_result commit(THD *thd, bool all) override;
int rollback(THD *thd, bool all) override;
int prepare(THD *thd, bool all) override;
int recover();
uint size() const;
private:
ulong log_xid(my_xid xid);
void unlog(ulong cookie, my_xid xid);
PAGE *get_active_from_pool();
bool sync();
void overflow();
protected:
// We want to mock away syncing to disk in unit tests.
virtual int do_msync_and_fsync(int fd_arg, void *addr, size_t len,
int flags) {
return my_msync(fd_arg, addr, len, flags);
}
private:
/**
Find empty slot in the page and write xid value there.
@param xid value of xid to store in the page
@param p pointer to the page where to store xid
@param data_arg pointer to the top of the mapped to memory file
to calculate offset value (cookie)
@return offset value from the top of the page where the xid was stored.
*/
ulong store_xid_in_empty_slot(my_xid xid, PAGE *p, uchar *data_arg) {
/* searching for an empty slot */
while (*p->ptr) {
p->ptr++;
assert(p->ptr < p->end); // because p->free > 0
}
/* found! store xid there and mark the page dirty */
ulong cookie = (ulong)((uchar *)p->ptr - data_arg); // can never be zero
*p->ptr++ = xid;
p->free--;
p->state = PS_DIRTY;
return cookie;
}
/**
Wait for until page data will be written to the disk.
@param p pointer to the PAGE to store to the disk
@retval false Success
@retval true Failure
*/
bool wait_sync_completion(PAGE *p) {
p->waiters++;
while (p->state == PS_DIRTY && syncing) {
mysql_cond_wait(&p->cond, &LOCK_tc);
}
p->waiters--;
return p->state == PS_ERROR;
}
/*
the following friend declaration is to grant access from TCLogMMapTest
to methods log_xid()/unlog() that are private.
*/
friend class TCLogMMapTest;
};
extern TC_LOG *tc_log;
extern TC_LOG_MMAP tc_log_mmap;
extern TC_LOG_DUMMY tc_log_dummy;
#endif // TC_LOG_H
|