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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
|
#ifndef TARANTOOL_BOX_TXN_H_INCLUDED
#define TARANTOOL_BOX_TXN_H_INCLUDED
/*
* Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdbool.h>
#include "salad/stailq.h"
#include "trigger.h"
#include "fiber.h"
#include "space.h"
#if defined(__cplusplus)
extern "C" {
#endif /* defined(__cplusplus) */
/** box statistics */
extern struct rmean *rmean_box;
struct journal_entry;
struct engine;
struct space;
struct tuple;
struct xrow_header;
struct Vdbe;
enum txn_flag {
/** Transaction has been processed. */
TXN_IS_DONE,
/**
* Transaction has been aborted by fiber yield so
* should be rolled back at commit.
*/
TXN_IS_ABORTED_BY_YIELD,
/**
* fiber_yield() is allowed inside the transaction.
* See txn_can_yield() for more details.
*/
TXN_CAN_YIELD,
/** on_commit and/or on_rollback list is not empty. */
TXN_HAS_TRIGGERS,
/**
* Synchronous transaction touched sync spaces, or an
* asynchronous transaction blocked by a sync one until it
* is confirmed.
*/
TXN_WAIT_SYNC,
/**
* Synchronous transaction 'waiting for ACKs' state before
* commit. In this state it waits until it is replicated
* onto a quorum of replicas, and only then finishes
* commit and returns success to a user.
* TXN_WAIT_SYNC is always set, if TXN_WAIT_ACK is set.
*/
TXN_WAIT_ACK,
/**
* A transaction may be forced to be asynchronous, not
* wait for any ACKs, and not depend on prepending sync
* transactions. This happens in a few special cases. For
* example, when applier receives snapshot from master.
*/
TXN_FORCE_ASYNC,
};
enum {
/**
* Maximum recursion depth for on_replace triggers.
* Large numbers may corrupt C stack.
*/
TXN_SUB_STMT_MAX = 3
};
enum {
/** Signature set for empty transactions. */
TXN_SIGNATURE_NOP = 0,
/**
* The default signature value for failed transactions.
* Indicates either write failure or any other failure
* not caused by synchronous transaction processing.
*/
TXN_SIGNATURE_ROLLBACK = -1,
/**
* A value set for failed synchronous transactions
* on master, when not enough acks were collected.
*/
TXN_SIGNATURE_QUORUM_TIMEOUT = -2,
/**
* A value set for failed synchronous transactions
* on replica (or any instance during recovery), when a
* transaction is rolled back because ROLLBACK message was
* read.
*/
TXN_SIGNATURE_SYNC_ROLLBACK = -3,
};
/**
* A single statement of a multi-statement
* transaction: undo and redo info.
*/
struct txn_stmt {
/* (!) Please update txn_stmt_new() after changing members */
/** A linked list of all statements. */
struct stailq_entry next;
/** Owner of that statement. */
struct txn *txn;
/** Undo info. */
struct space *space;
struct tuple *old_tuple;
struct tuple *new_tuple;
/** Engine savepoint for the start of this statement. */
void *engine_savepoint;
/** Redo info: the binary log row */
struct xrow_header *row;
/** on_commit and/or on_rollback list is not empty. */
bool has_triggers;
/** Commit/rollback triggers associated with this statement. */
struct rlist on_commit;
struct rlist on_rollback;
};
/**
* Transaction savepoint object. Allocated on a transaction
* region and becames invalid after the transaction's end.
* Allows to rollback a transaction partially.
*/
struct txn_savepoint {
/**
* Saved substatement level at the time of a savepoint
* creation.
*/
int in_sub_stmt;
/**
* Statement, on which a savepoint is created. On rollback
* to this savepoint all newer statements are rolled back.
* Initialized to NULL in case a savepoint is created in
* an empty transaction.
*/
struct stailq_entry *stmt;
/**
* Each foreign key constraint is classified as either
* immediate (by default) or deferred. In autocommit mode
* they mean the same. Inside separate transaction,
* deferred FK constraints are not checked until the
* transaction tries to commit. For as long as
* a transaction is open, it is allowed to exist in a
* state violating any number of deferred FK constraints.
*/
uint32_t fk_deferred_count;
/** Organize savepoints into linked list. */
struct rlist link;
/**
* Optional name of savepoint. If savepoint lacks
* name (i.e. anonymous savepoint available only by
* reference to the object), name[0] == ''. Otherwise,
* memory for name is reserved in the same memory chunk
* as struct txn_savepoint itself - name is placed
* right after structure (see txn_savepoint_new()).
*/
char name[1];
};
extern double too_long_threshold;
/**
* An element of list of autogenerated ids, being returned as SQL
* response metadata.
*/
struct autoinc_id_entry {
struct stailq_entry link;
int64_t id;
};
struct txn {
/** A stailq_entry to hold a txn in a cache. */
struct stailq_entry in_txn_cache;
/**
* A memory region to put all transaction relative data in.
* Detaching transaction data from a fiber temporary storage
* is required to allow an applier fiber to manage multiple
* transactions simultaneously. Also interactive and autonomous
* transactions will require this.
*/
struct region region;
/**
* A sequentially growing transaction id, assigned when
* a transaction is initiated. Used to identify
* a transaction after it has possibly been destroyed.
*
* Valid IDs start from 1.
*/
int64_t id;
/** List of statements in a transaction. */
struct stailq stmts;
/** Number of new rows without an assigned LSN. */
int n_new_rows;
/**
* Number of local new rows, no assigned LSN and
* replication group_id=local (not replicated anywhere).
*/
int n_local_rows;
/**
* Number of rows coming from the applier, with an
* already assigned LSN.
*/
int n_applier_rows;
/** Bit mask of transaction flags, see txn_flag. */
unsigned flags;
/** The number of active nested statement-level transactions. */
int8_t in_sub_stmt;
/**
* First statement at each statement-level.
* Needed to rollback sub statements.
*/
struct stailq_entry *sub_stmt_begin[TXN_SUB_STMT_MAX + 1];
/** LSN of this transaction when written to WAL. */
int64_t signature;
/** Engine involved in multi-statement transaction. */
struct engine *engine;
/** Engine-specific transaction data */
void *engine_tx;
/* A fiber to wake up when transaction is finished. */
struct fiber *fiber;
/** Timestampt of entry write start. */
double start_tm;
/**
* Triggers on fiber yield to abort transaction for
* for in-memory engine.
*/
struct trigger fiber_on_yield;
/**
* Trigger on fiber stop, to rollback transaction
* in case a fiber stops (all engines).
*/
struct trigger fiber_on_stop;
/** Commit and rollback triggers. */
struct rlist on_commit, on_rollback, on_wal_write;
/**
* This member represents counter of deferred foreign key
* violations within transaction. DEFERRED mode means
* that until transaction is committed violations are
* allowed to appear. However, transaction can't be
* committed in presence of violations, i.e. if this
* counter is not equal to zero. In the normal mode
* violations of FK are checked at the end of each
* statement processing. Note that at the moment it is
* SQL specific property.
*/
uint32_t fk_deferred_count;
/** List of savepoints to find savepoint by name. */
struct rlist savepoints;
};
static inline bool
txn_has_flag(struct txn *txn, enum txn_flag flag)
{
return (txn->flags & (1 << flag)) != 0;
}
static inline void
txn_set_flag(struct txn *txn, enum txn_flag flag)
{
txn->flags |= 1 << flag;
}
static inline void
txn_clear_flag(struct txn *txn, enum txn_flag flag)
{
txn->flags &= ~(1 << flag);
}
/* Pointer to the current transaction (if any) */
static inline struct txn *
in_txn(void)
{
return fiber()->storage.txn;
}
/* Set to the current transaction (if any) */
static inline void
fiber_set_txn(struct fiber *fiber, struct txn *txn)
{
fiber->storage.txn = txn;
}
/**
* Start a transaction explicitly.
* @pre no transaction is active
*/
struct txn *
txn_begin(void);
/**
* Complete transaction processing.
*/
void
txn_complete(struct txn *txn);
/**
* Commit a transaction.
* @pre txn == in_txn()
*
* Return 0 on success. On error, rollback
* the transaction and return -1.
*/
int
txn_commit(struct txn *txn);
/**
* Rollback a transaction.
* @pre txn == in_txn()
*/
void
txn_rollback(struct txn *txn);
/**
* Complete asynchronous transaction.
*/
void
txn_complete_async(struct journal_entry *entry);
/**
* Submit a transaction to the journal.
* @pre txn == in_txn()
*
* On success 0 is returned, and the transaction will be freed upon
* journal write completion. Note, the journal write may still fail.
* To track transaction status, one is supposed to use on_commit and
* on_rollback triggers.
*
* On failure -1 is returned and the transaction is rolled back and
* freed.
*/
int
txn_commit_async(struct txn *txn);
/**
* Most txns don't have triggers, and txn objects
* are created on every access to data, so txns
* are partially initialized.
*/
static inline void
txn_init_triggers(struct txn *txn)
{
if (!txn_has_flag(txn, TXN_HAS_TRIGGERS)) {
rlist_create(&txn->on_commit);
rlist_create(&txn->on_rollback);
rlist_create(&txn->on_wal_write);
txn_set_flag(txn, TXN_HAS_TRIGGERS);
}
}
static inline void
txn_on_commit(struct txn *txn, struct trigger *trigger)
{
txn_init_triggers(txn);
trigger_add(&txn->on_commit, trigger);
}
static inline void
txn_on_rollback(struct txn *txn, struct trigger *trigger)
{
txn_init_triggers(txn);
trigger_add(&txn->on_rollback, trigger);
}
static inline void
txn_on_wal_write(struct txn *txn, struct trigger *trigger)
{
txn_init_triggers(txn);
trigger_add(&txn->on_wal_write, trigger);
}
/**
* Most statements don't have triggers, and txn objects
* are created on every access to data, so statements
* are partially initialized.
*/
static inline void
txn_stmt_init_triggers(struct txn_stmt *stmt)
{
if (!stmt->has_triggers) {
rlist_create(&stmt->on_commit);
rlist_create(&stmt->on_rollback);
stmt->has_triggers = true;
}
}
static inline void
txn_stmt_on_commit(struct txn_stmt *stmt, struct trigger *trigger)
{
txn_stmt_init_triggers(stmt);
trigger_add(&stmt->on_commit, trigger);
}
static inline void
txn_stmt_on_rollback(struct txn_stmt *stmt, struct trigger *trigger)
{
txn_stmt_init_triggers(stmt);
trigger_add(&stmt->on_rollback, trigger);
}
/*
* Return the total number of rows committed in the txn.
*/
static inline int
txn_n_rows(struct txn *txn)
{
return txn->n_new_rows + txn->n_applier_rows;
}
/**
* Start a new statement.
*/
int
txn_begin_stmt(struct txn *txn, struct space *space);
int
txn_begin_in_engine(struct engine *engine, struct txn *txn);
/**
* This is an optimization, which exists to speed up selects
* in autocommit mode. For such selects, we only need to
* manage fiber garbage heap. If autocommit mode is
* off, however, we must start engine transaction with the first
* select.
*/
static inline int
txn_begin_ro_stmt(struct space *space, struct txn **txn)
{
*txn = in_txn();
if (*txn != NULL) {
struct engine *engine = space->engine;
return txn_begin_in_engine(engine, *txn);
}
return 0;
}
static inline void
txn_commit_ro_stmt(struct txn *txn)
{
assert(txn == in_txn());
if (txn) {
/* nothing to do */
} else {
fiber_gc();
}
}
/**
* Check whether a transaction which is used to apply
* remote master rows generated some local changes.
* Such transaction must be aborted, since we wouldn't
* be able to *consistently* apply the local changes
* to the remote master.
*/
bool
txn_is_distributed(struct txn *txn);
/**
* End a statement. In autocommit mode, end
* the current transaction as well.
*
* Return 0 on success. On error, rollback
* the statement and return -1.
*/
int
txn_commit_stmt(struct txn *txn, struct request *request);
/**
* Rollback a statement. In autocommit mode,
* rolls back the entire transaction.
*/
void
txn_rollback_stmt(struct txn *txn);
/**
* Raise an error if this is a multi-statement transaction:
* a yielding DDL operation, such as index build or space format
* check, can not be part of a multi-statement transaction,
* because there may be uncommitted objects in the schema cache,
* which would be revealed to other fibers on yield.
*/
int
txn_check_singlestatement(struct txn *txn, const char *where);
/**
* Enables or disables fiber yields inside the current transaction
* depending on the value of the given flag. Yields are disabled
* by installing a fiber-on-yield trigger that marks the transaction
* as aborted, which results in rolling back the transaction on
* commit.
*
* This function is used by the memtx engine, because it doesn't
* support yields inside transactions. It is also used to temporarily
* enable yields for long DDL operations such as building an index
* or checking a space format.
*/
void
txn_can_yield(struct txn *txn, bool set);
/**
* Returns true if the transaction has a single statement.
* Supposed to be used from a space on_replace trigger to
* detect transaction boundaries.
*/
static inline bool
txn_is_first_statement(struct txn *txn)
{
return stailq_last(&txn->stmts) == stailq_first(&txn->stmts);
}
/** The current statement of the transaction. */
static inline struct txn_stmt *
txn_current_stmt(struct txn *txn)
{
if (txn->in_sub_stmt == 0)
return NULL;
struct stailq_entry *stmt = txn->sub_stmt_begin[txn->in_sub_stmt - 1];
stmt = stmt != NULL ? stailq_next(stmt) : stailq_first(&txn->stmts);
return stailq_entry(stmt, struct txn_stmt, next);
}
/**
* Allocate new savepoint object using region allocator.
* Savepoint is allowed to be anonymous (i.e. without
* name).
*/
struct txn_savepoint *
txn_savepoint_new(struct txn *txn, const char *name);
/** Find savepoint by its name in savepoint list. */
struct txn_savepoint *
txn_savepoint_by_name(struct txn *txn, const char *name);
/** Remove given and all newer entries from savepoint list. */
void
txn_savepoint_release(struct txn_savepoint *svp);
/**
* FFI bindings: do not throw exceptions, do not accept extra
* arguments
*/
/** \cond public */
/**
* Transaction id - a non-persistent unique identifier
* of the current transaction. -1 if there is no current
* transaction.
*/
API_EXPORT int64_t
box_txn_id(void);
/**
* Return true if there is an active transaction.
*/
API_EXPORT bool
box_txn(void);
/**
* Begin a transaction in the current fiber.
*
* A transaction is attached to caller fiber, therefore one fiber can have
* only one active transaction.
*
* @retval 0 - success
* @retval -1 - failed, perhaps a transaction has already been
* started
*/
API_EXPORT int
box_txn_begin(void);
/**
* Commit the current transaction.
* @retval 0 - success
* @retval -1 - failed, perhaps a disk write failure.
* started
*/
API_EXPORT int
box_txn_commit(void);
/**
* Rollback the current transaction.
* May fail if called from a nested
* statement.
*/
API_EXPORT int
box_txn_rollback(void);
/**
* Allocate memory on txn memory pool.
* The memory is automatically deallocated when the transaction
* is committed or rolled back.
*
* @retval NULL out of memory
*/
API_EXPORT void *
box_txn_alloc(size_t size);
/** \endcond public */
typedef struct txn_savepoint box_txn_savepoint_t;
/**
* Create a new savepoint.
* @retval not NULL Savepoint object.
* @retval NULL Client or memory error.
*/
API_EXPORT box_txn_savepoint_t *
box_txn_savepoint(void);
/**
* Rollback to @a savepoint. Rollback all statements newer than a
* saved statement. @A savepoint can be rolled back multiple
* times. All existing savepoints, newer than @a savepoint, are
* deleted and can not be used.
* @A savepoint must be from a current transaction, else the
* rollback crashes. To validate savepoints store transaction id
* together with @a savepoint.
* @retval 0 Success.
* @retval -1 Client error.
*/
API_EXPORT int
box_txn_rollback_to_savepoint(box_txn_savepoint_t *savepoint);
#if defined(__cplusplus)
} /* extern "C" */
#endif /* defined(__cplusplus) */
#endif /* TARANTOOL_BOX_TXN_H_INCLUDED */
|