📄 trx0trx.c
字号:
node = thr->run_node; ut_ad(que_node_get_type(node) == QUE_NODE_COMMIT); if (thr->prev_node == que_node_get_parent(node)) { node->state = COMMIT_NODE_SEND; } if (node->state == COMMIT_NODE_SEND) { mutex_enter(&kernel_mutex); node->state = COMMIT_NODE_WAIT; next_thr = NULL; thr->state = QUE_THR_SIG_REPLY_WAIT; /* Send the commit signal to the transaction */ success = trx_sig_send(thr_get_trx(thr), TRX_SIG_COMMIT, TRX_SIG_SELF, thr, NULL, &next_thr); mutex_exit(&kernel_mutex); if (!success) { /* Error in delivering the commit signal */ que_thr_handle_error(thr, DB_ERROR, NULL, 0); } return(next_thr); } ut_ad(node->state == COMMIT_NODE_WAIT); node->state = COMMIT_NODE_SEND; thr->run_node = que_node_get_parent(node); return(thr);}/**************************************************************************Does the transaction commit for MySQL. */ulinttrx_commit_for_mysql(/*=================*/ /* out: 0 or error number */ trx_t* trx) /* in: trx handle */{ /* Because we do not do the commit by sending an Innobase sig to the transaction, we must here make sure that trx has been started. */ ut_a(trx); trx->op_info = "committing"; trx_start_if_not_started(trx); mutex_enter(&kernel_mutex); trx_commit_off_kernel(trx); mutex_exit(&kernel_mutex); trx->op_info = ""; return(0);}/**************************************************************************If required, flushes the log to disk if we called trx_commit_for_mysql()with trx->flush_log_later == TRUE. */ulinttrx_commit_complete_for_mysql(/*==========================*/ /* out: 0 or error number */ trx_t* trx) /* in: trx handle */{ dulint lsn = trx->commit_lsn; ut_a(trx); trx->op_info = "flushing log"; if (!trx->must_flush_log_later) { /* Do nothing */ } else if (srv_flush_log_at_trx_commit == 0) { /* Do nothing */ } else if (srv_flush_log_at_trx_commit == 1) { if (srv_unix_file_flush_method == SRV_UNIX_NOSYNC) { /* Write the log but do not flush it to disk */ log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, FALSE); } else { /* Write the log to the log files AND flush them to disk */ log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, TRUE); } } else if (srv_flush_log_at_trx_commit == 2) { /* Write the log but do not flush it to disk */ log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, FALSE); } else { ut_error; } trx->must_flush_log_later = FALSE; trx->op_info = ""; return(0);}/**************************************************************************Marks the latest SQL statement ended. */voidtrx_mark_sql_stat_end(/*==================*/ trx_t* trx) /* in: trx handle */{ ut_a(trx); if (trx->conc_state == TRX_NOT_STARTED) { trx->undo_no = ut_dulint_zero; } trx->last_sql_stat_start.least_undo_no = trx->undo_no;}/**************************************************************************Prints info about a transaction to the given file. The caller must own thekernel mutex and must have calledinnobase_mysql_prepare_print_arbitrary_thd(), unless he knows that MySQLor InnoDB cannot meanwhile change the info printed here. */voidtrx_print(/*======*/ FILE* f, /* in: output stream */ trx_t* trx, /* in: transaction */ uint max_query_len) /* in: max query length to print, or 0 to use the default max length */{ ibool newline; fprintf(f, "TRANSACTION %lu %lu", (ulong) ut_dulint_get_high(trx->id), (ulong) ut_dulint_get_low(trx->id)); switch (trx->conc_state) { case TRX_NOT_STARTED: fputs(", not started", f); break; case TRX_ACTIVE: fprintf(f, ", ACTIVE %lu sec", (ulong)difftime(time(NULL), trx->start_time)); break; case TRX_PREPARED: fprintf(f, ", ACTIVE (PREPARED) %lu sec", (ulong)difftime(time(NULL), trx->start_time)); break; case TRX_COMMITTED_IN_MEMORY: fputs(", COMMITTED IN MEMORY", f); break; default: fprintf(f, " state %lu", (ulong) trx->conc_state); }#ifdef UNIV_LINUX fprintf(f, ", process no %lu", trx->mysql_process_no);#endif fprintf(f, ", OS thread id %lu", (ulong) os_thread_pf(trx->mysql_thread_id)); if (*trx->op_info) { putc(' ', f); fputs(trx->op_info, f); } if (trx->type != TRX_USER) { fputs(" purge trx", f); } if (trx->declared_to_be_inside_innodb) { fprintf(f, ", thread declared inside InnoDB %lu", (ulong) trx->n_tickets_to_enter_innodb); } putc('\n', f); if (trx->n_mysql_tables_in_use > 0 || trx->mysql_n_tables_locked > 0) { fprintf(f, "mysql tables in use %lu, locked %lu\n", (ulong) trx->n_mysql_tables_in_use, (ulong) trx->mysql_n_tables_locked); } newline = TRUE; switch (trx->que_state) { case TRX_QUE_RUNNING: newline = FALSE; break; case TRX_QUE_LOCK_WAIT: fputs("LOCK WAIT ", f); break; case TRX_QUE_ROLLING_BACK: fputs("ROLLING BACK ", f); break; case TRX_QUE_COMMITTING: fputs("COMMITTING ", f); break; default: fprintf(f, "que state %lu ", (ulong) trx->que_state); } if (0 < UT_LIST_GET_LEN(trx->trx_locks) || mem_heap_get_size(trx->lock_heap) > 400) { newline = TRUE; fprintf(f, "%lu lock struct(s), heap size %lu", (ulong) UT_LIST_GET_LEN(trx->trx_locks), (ulong) mem_heap_get_size(trx->lock_heap)); } if (trx->has_search_latch) { newline = TRUE; fputs(", holds adaptive hash latch", f); } if (ut_dulint_cmp(trx->undo_no, ut_dulint_zero) != 0) { newline = TRUE; fprintf(f, ", undo log entries %lu", (ulong) ut_dulint_get_low(trx->undo_no)); } if (newline) { putc('\n', f); } if (trx->mysql_thd != NULL) { innobase_mysql_print_thd(f, trx->mysql_thd, max_query_len); } }/********************************************************************Prepares a transaction. */voidtrx_prepare_off_kernel(/*===================*/ trx_t* trx) /* in: transaction */{ page_t* update_hdr_page; trx_rseg_t* rseg; ibool must_flush_log = FALSE; dulint lsn; mtr_t mtr; #ifdef UNIV_SYNC_DEBUG ut_ad(mutex_own(&kernel_mutex));#endif /* UNIV_SYNC_DEBUG */ rseg = trx->rseg; if (trx->insert_undo != NULL || trx->update_undo != NULL) { mutex_exit(&kernel_mutex); mtr_start(&mtr); must_flush_log = TRUE; /* Change the undo log segment states from TRX_UNDO_ACTIVE to TRX_UNDO_PREPARED: these modifications to the file data structure define the transaction as prepared in the file-based world, at the serialization point of lsn. */ mutex_enter(&(rseg->mutex)); if (trx->insert_undo != NULL) { /* It is not necessary to obtain trx->undo_mutex here because only a single OS thread is allowed to do the transaction prepare for this transaction. */ trx_undo_set_state_at_prepare(trx, trx->insert_undo, &mtr); } if (trx->update_undo) { update_hdr_page = trx_undo_set_state_at_prepare(trx, trx->update_undo, &mtr); } mutex_exit(&(rseg->mutex)); /*--------------*/ mtr_commit(&mtr); /* This mtr commit makes the transaction prepared in the file-based world */ /*--------------*/ lsn = mtr.end_lsn; mutex_enter(&kernel_mutex); }#ifdef UNIV_SYNC_DEBUG ut_ad(mutex_own(&kernel_mutex));#endif /* UNIV_SYNC_DEBUG */ /*--------------------------------------*/ trx->conc_state = TRX_PREPARED; /*--------------------------------------*/ if (must_flush_log) { /* Depending on the my.cnf options, we may now write the log buffer to the log files, making the prepared state of the transaction durable if the OS does not crash. We may also flush the log files to disk, making the prepared state of the transaction durable also at an OS crash or a power outage. The idea in InnoDB's group prepare is that a group of transactions gather behind a trx doing a physical disk write to log files, and when that physical write has been completed, one of those transactions does a write which prepares the whole group. Note that this group prepare will only bring benefit if there are > 2 users in the database. Then at least 2 users can gather behind one doing the physical log write to disk. TODO: find out if MySQL holds some mutex when calling this. That would spoil our group prepare algorithm. */ mutex_exit(&kernel_mutex); if (srv_flush_log_at_trx_commit == 0) { /* Do nothing */ } else if (srv_flush_log_at_trx_commit == 1) { if (srv_unix_file_flush_method == SRV_UNIX_NOSYNC) { /* Write the log but do not flush it to disk */ log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, FALSE); } else { /* Write the log to the log files AND flush them to disk */ log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, TRUE); } } else if (srv_flush_log_at_trx_commit == 2) { /* Write the log but do not flush it to disk */ log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, FALSE); } else { ut_error; } mutex_enter(&kernel_mutex); }}/**************************************************************************Does the transaction prepare for MySQL. */ulinttrx_prepare_for_mysql(/*====-=============*/ /* out: 0 or error number */ trx_t* trx) /* in: trx handle */{ /* Because we do not do the prepare by sending an Innobase sig to the transaction, we must here make sure that trx has been started. */ ut_a(trx); trx->op_info = "preparing"; trx_start_if_not_started(trx); mutex_enter(&kernel_mutex); trx_prepare_off_kernel(trx); mutex_exit(&kernel_mutex); trx->op_info = ""; return(0);}/**************************************************************************This function is used to find number of prepared transactions andtheir transaction objects for a recovery. */inttrx_recover_for_mysql(/*==================*/ /* out: number of prepared transactions stored in xid_list */ XID* xid_list, /* in/out: prepared transactions */ ulint len) /* in: number of slots in xid_list */{ trx_t* trx; int count = 0; ut_ad(xid_list); ut_ad(len); /* We should set those transactions which are in the prepared state to the xid_list */ mutex_enter(&kernel_mutex); trx = UT_LIST_GET_FIRST(trx_sys->trx_list); while (trx) { if (trx->conc_state == TRX_PREPARED) { xid_list[count] = trx->xid; if (count == 0) { ut_print_timestamp(stderr); fprintf(stderr," InnoDB: Starting recovery for XA transactions...\n"); } ut_print_timestamp(stderr); fprintf(stderr," InnoDB: Transaction %lu %lu in prepared state after recovery\n", (ulong) ut_dulint_get_high(trx->id), (ulong) ut_dulint_get_low(trx->id)); ut_print_timestamp(stderr); fprintf(stderr," InnoDB: Transaction contains changes to %lu rows\n", (ulong)ut_conv_dulint_to_longlong(trx->undo_no)); count++; if ((uint)count == len ) { break; } } trx = UT_LIST_GET_NEXT(trx_list, trx); } mutex_exit(&kernel_mutex); if (count > 0){ ut_print_timestamp(stderr); fprintf(stderr," InnoDB: %d transactions in prepared state after recovery\n", count); } return (count); }/***********************************************************************This function is used to find one X/Open XA distributed transactionwhich is in the prepared state */trx_t*trx_get_trx_by_xid(/*===============*/ /* out: trx or NULL */ XID* xid) /* in: X/Open XA transaction identification */{ trx_t* trx; if (xid == NULL) { return (NULL); } mutex_enter(&kernel_mutex); trx = UT_LIST_GET_FIRST(trx_sys->trx_list); while (trx) { /* Compare two X/Open XA transaction id's: their length should be the same and binary comparison of gtrid_lenght+bqual_length bytes should be the same */ if (xid->gtrid_length == trx->xid.gtrid_length && xid->bqual_length == trx->xid.bqual_length && memcmp(xid->data, trx->xid.data, xid->gtrid_length + xid->bqual_length) == 0) { break; } trx = UT_LIST_GET_NEXT(trx_list, trx); } mutex_exit(&kernel_mutex); if (trx) { if (trx->conc_state != TRX_PREPARED) { return(NULL); } return(trx); } else { return(NULL); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -