-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
trx0trx.cc
3185 lines (2466 loc) · 79.7 KB
/
trx0trx.cc
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2015, 2017, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
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 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 Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file trx/trx0trx.cc
The transaction
Created 3/26/1996 Heikki Tuuri
*******************************************************/
#include "ha_prototypes.h"
#include "trx0trx.h"
#ifdef WITH_WSREP
#include <mysql/service_wsrep.h>
#endif
#include <mysql/service_thd_error_context.h>
#include "btr0sea.h"
#include "lock0lock.h"
#include "log0log.h"
#include "os0proc.h"
#include "que0que.h"
#include "read0read.h"
#include "srv0mon.h"
#include "srv0srv.h"
#include "fsp0sysspace.h"
#include "row0mysql.h"
#include "srv0start.h"
#include "trx0purge.h"
#include "trx0rec.h"
#include "trx0roll.h"
#include "trx0rseg.h"
#include "trx0undo.h"
#include "trx0xa.h"
#include "usr0sess.h"
#include "ut0new.h"
#include "ut0pool.h"
#include "ut0vec.h"
#include <set>
#include <new>
extern "C"
int thd_deadlock_victim_preference(const MYSQL_THD thd1, const MYSQL_THD thd2);
static const ulint MAX_DETAILED_ERROR_LEN = 256;
/** Set of table_id */
typedef std::set<
table_id_t,
std::less<table_id_t>,
ut_allocator<table_id_t> > table_id_set;
/** Dummy session used currently in MySQL interface */
sess_t* trx_dummy_sess = NULL;
/** Constructor */
TrxVersion::TrxVersion(trx_t* trx)
:
m_trx(trx),
m_version(trx->version)
{
/* No op */
}
/** Set flush observer for the transaction
@param[in/out] trx transaction struct
@param[in] observer flush observer */
void
trx_set_flush_observer(
trx_t* trx,
FlushObserver* observer)
{
trx->flush_observer = observer;
}
/*************************************************************//**
Set detailed error message for the transaction. */
void
trx_set_detailed_error(
/*===================*/
trx_t* trx, /*!< in: transaction struct */
const char* msg) /*!< in: detailed error message */
{
ut_strlcpy(trx->detailed_error, msg, MAX_DETAILED_ERROR_LEN);
}
/*************************************************************//**
Set detailed error message for the transaction from a file. Note that the
file is rewinded before reading from it. */
void
trx_set_detailed_error_from_file(
/*=============================*/
trx_t* trx, /*!< in: transaction struct */
FILE* file) /*!< in: file to read message from */
{
os_file_read_string(file, trx->detailed_error, MAX_DETAILED_ERROR_LEN);
}
/********************************************************************//**
Initialize transaction object.
@param trx trx to initialize */
static
void
trx_init(
/*=====*/
trx_t* trx)
{
/* This is called at the end of commit, do not reset the
trx_t::state here to NOT_STARTED. The FORCED_ROLLBACK
status is required for asynchronous handling. */
trx->id = 0;
trx->no = TRX_ID_MAX;
trx->is_recovered = false;
trx->op_info = "";
trx->active_commit_ordered = 0;
trx->isolation_level = TRX_ISO_REPEATABLE_READ;
trx->check_foreigns = true;
trx->check_unique_secondary = true;
trx->lock.n_rec_locks = 0;
trx->dict_operation = TRX_DICT_OP_NONE;
trx->table_id = 0;
trx->error_state = DB_SUCCESS;
trx->error_key_num = ULINT_UNDEFINED;
trx->undo_no = 0;
trx->rsegs.m_redo.rseg = NULL;
trx->rsegs.m_noredo.rseg = NULL;
trx->read_only = false;
trx->auto_commit = false;
trx->will_lock = 0;
trx->ddl = false;
trx->internal = false;
ut_d(trx->start_file = 0);
ut_d(trx->start_line = 0);
trx->magic_n = TRX_MAGIC_N;
trx->lock.que_state = TRX_QUE_RUNNING;
trx->last_sql_stat_start.least_undo_no = 0;
ut_ad(!MVCC::is_view_active(trx->read_view));
trx->lock.rec_cached = 0;
trx->lock.table_cached = 0;
/* During asynchronous rollback, we should reset forced rollback flag
only after rollback is complete to avoid race with the thread owning
the transaction. */
if (!TrxInInnoDB::is_async_rollback(trx)) {
my_atomic_storelong(&trx->killed_by, 0);
/* Note: Do not set to 0, the ref count is decremented inside
the TrxInInnoDB() destructor. We only need to clear the flags. */
trx->in_innodb &= TRX_FORCE_ROLLBACK_MASK;
}
/* Note: It's possible that this list is not empty if a transaction
was interrupted after it collected the victim transactions and before
it got a chance to roll them back asynchronously. */
trx->hit_list.clear();
trx->flush_observer = NULL;
++trx->version;
}
/** For managing the life-cycle of the trx_t instance that we get
from the pool. */
struct TrxFactory {
/** Initializes a transaction object. It must be explicitly started
with trx_start_if_not_started() before using it. The default isolation
level is TRX_ISO_REPEATABLE_READ.
@param trx Transaction instance to initialise */
static void init(trx_t* trx)
{
/* Explicitly call the constructor of the already
allocated object. trx_t objects are allocated by
ut_zalloc() in Pool::Pool() which would not call
the constructors of the trx_t members. */
new(&trx->mod_tables) trx_mod_tables_t();
new(&trx->lock.rec_pool) lock_pool_t();
new(&trx->lock.table_pool) lock_pool_t();
new(&trx->lock.table_locks) lock_pool_t();
new(&trx->hit_list) hit_list_t();
trx_init(trx);
trx->state = TRX_STATE_NOT_STARTED;
trx->dict_operation_lock_mode = 0;
trx->xid = UT_NEW_NOKEY(xid_t());
trx->detailed_error = reinterpret_cast<char*>(
ut_zalloc_nokey(MAX_DETAILED_ERROR_LEN));
trx->lock.lock_heap = mem_heap_create_typed(
1024, MEM_HEAP_FOR_LOCK_HEAP);
lock_trx_lock_list_init(&trx->lock.trx_locks);
UT_LIST_INIT(
trx->trx_savepoints,
&trx_named_savept_t::trx_savepoints);
mutex_create(LATCH_ID_TRX, &trx->mutex);
mutex_create(LATCH_ID_TRX_UNDO, &trx->undo_mutex);
lock_trx_alloc_locks(trx);
}
/** Release resources held by the transaction object.
@param trx the transaction for which to release resources */
static void destroy(trx_t* trx)
{
ut_a(trx->magic_n == TRX_MAGIC_N);
ut_ad(!trx->in_rw_trx_list);
ut_ad(!trx->in_mysql_trx_list);
ut_a(trx->lock.wait_lock == NULL);
ut_a(trx->lock.wait_thr == NULL);
trx_assert_no_search_latch(trx);
ut_a(trx->dict_operation_lock_mode == 0);
if (trx->lock.lock_heap != NULL) {
mem_heap_free(trx->lock.lock_heap);
trx->lock.lock_heap = NULL;
}
ut_a(UT_LIST_GET_LEN(trx->lock.trx_locks) == 0);
UT_DELETE(trx->xid);
ut_free(trx->detailed_error);
mutex_free(&trx->mutex);
mutex_free(&trx->undo_mutex);
trx->mod_tables.~trx_mod_tables_t();
ut_ad(trx->read_view == NULL);
if (!trx->lock.rec_pool.empty()) {
/* See lock_trx_alloc_locks() why we only free
the first element. */
ut_free(trx->lock.rec_pool[0]);
}
if (!trx->lock.table_pool.empty()) {
/* See lock_trx_alloc_locks() why we only free
the first element. */
ut_free(trx->lock.table_pool[0]);
}
trx->lock.rec_pool.~lock_pool_t();
trx->lock.table_pool.~lock_pool_t();
trx->lock.table_locks.~lock_pool_t();
trx->hit_list.~hit_list_t();
}
/** Enforce any invariants here, this is called before the transaction
is added to the pool.
@return true if all OK */
static bool debug(const trx_t* trx)
{
ut_a(trx->error_state == DB_SUCCESS);
ut_a(trx->magic_n == TRX_MAGIC_N);
ut_ad(!trx->read_only);
ut_ad(trx->state == TRX_STATE_NOT_STARTED
|| trx->state == TRX_STATE_FORCED_ROLLBACK);
ut_ad(trx->dict_operation == TRX_DICT_OP_NONE);
ut_ad(trx->mysql_thd == 0);
ut_ad(!trx->in_rw_trx_list);
ut_ad(!trx->in_mysql_trx_list);
ut_a(trx->lock.wait_thr == NULL);
ut_a(trx->lock.wait_lock == NULL);
trx_assert_no_search_latch(trx);
ut_a(trx->dict_operation_lock_mode == 0);
ut_a(UT_LIST_GET_LEN(trx->lock.trx_locks) == 0);
ut_ad(trx->autoinc_locks == NULL);
ut_ad(trx->lock.table_locks.empty());
ut_ad(!trx->abort);
ut_ad(trx->hit_list.empty());
ut_ad(trx->killed_by == 0);
return(true);
}
};
/** The lock strategy for TrxPool */
struct TrxPoolLock {
TrxPoolLock() { }
/** Create the mutex */
void create()
{
mutex_create(LATCH_ID_TRX_POOL, &m_mutex);
}
/** Acquire the mutex */
void enter() { mutex_enter(&m_mutex); }
/** Release the mutex */
void exit() { mutex_exit(&m_mutex); }
/** Free the mutex */
void destroy() { mutex_free(&m_mutex); }
/** Mutex to use */
ib_mutex_t m_mutex;
};
/** The lock strategy for the TrxPoolManager */
struct TrxPoolManagerLock {
TrxPoolManagerLock() { }
/** Create the mutex */
void create()
{
mutex_create(LATCH_ID_TRX_POOL_MANAGER, &m_mutex);
}
/** Acquire the mutex */
void enter() { mutex_enter(&m_mutex); }
/** Release the mutex */
void exit() { mutex_exit(&m_mutex); }
/** Free the mutex */
void destroy() { mutex_free(&m_mutex); }
/** Mutex to use */
ib_mutex_t m_mutex;
};
/** Use explicit mutexes for the trx_t pool and its manager. */
typedef Pool<trx_t, TrxFactory, TrxPoolLock> trx_pool_t;
typedef PoolManager<trx_pool_t, TrxPoolManagerLock > trx_pools_t;
/** The trx_t pool manager */
static trx_pools_t* trx_pools;
/** Size of on trx_t pool in bytes. */
static const ulint MAX_TRX_BLOCK_SIZE = 1024 * 1024 * 4;
/** Create the trx_t pool */
void
trx_pool_init()
{
trx_pools = UT_NEW_NOKEY(trx_pools_t(MAX_TRX_BLOCK_SIZE));
ut_a(trx_pools != 0);
}
/** Destroy the trx_t pool */
void
trx_pool_close()
{
UT_DELETE(trx_pools);
trx_pools = 0;
}
/** @return a trx_t instance from trx_pools. */
static
trx_t*
trx_create_low()
{
trx_t* trx = trx_pools->get();
assert_trx_is_free(trx);
mem_heap_t* heap;
ib_alloc_t* alloc;
/* We just got trx from pool, it should be non locking */
ut_ad(trx->will_lock == 0);
/* Background trx should not be forced to rollback,
we will unset the flag for user trx. */
trx->in_innodb |= TRX_FORCE_ROLLBACK_DISABLE;
/* Trx state can be TRX_STATE_FORCED_ROLLBACK if
the trx was forced to rollback before it's reused.*/
trx->state = TRX_STATE_NOT_STARTED;
heap = mem_heap_create(sizeof(ib_vector_t) + sizeof(void*) * 8);
alloc = ib_heap_allocator_create(heap);
/* Remember to free the vector explicitly in trx_free(). */
trx->autoinc_locks = ib_vector_create(alloc, sizeof(void**), 4);
/* Should have been either just initialized or .clear()ed by
trx_free(). */
ut_a(trx->mod_tables.size() == 0);
#ifdef WITH_WSREP
trx->wsrep_event = NULL;
#endif /* WITH_WSREP */
return(trx);
}
/**
Release a trx_t instance back to the pool.
@param trx the instance to release. */
static
void
trx_free(trx_t*& trx)
{
assert_trx_is_free(trx);
trx->mysql_thd = 0;
trx->mysql_log_file_name = 0;
// FIXME: We need to avoid this heap free/alloc for each commit.
if (trx->autoinc_locks != NULL) {
ut_ad(ib_vector_is_empty(trx->autoinc_locks));
/* We allocated a dedicated heap for the vector. */
ib_vector_free(trx->autoinc_locks);
trx->autoinc_locks = NULL;
}
trx->mod_tables.clear();
ut_ad(trx->read_view == NULL);
/* trx locking state should have been reset before returning trx
to pool */
ut_ad(trx->will_lock == 0);
trx_pools->mem_free(trx);
trx = NULL;
}
/********************************************************************//**
Creates a transaction object for background operations by the master thread.
@return own: transaction object */
trx_t*
trx_allocate_for_background(void)
/*=============================*/
{
trx_t* trx;
trx = trx_create_low();
trx->sess = trx_dummy_sess;
return(trx);
}
/********************************************************************//**
Creates a transaction object for MySQL.
@return own: transaction object */
trx_t*
trx_allocate_for_mysql(void)
/*========================*/
{
trx_t* trx;
trx = trx_allocate_for_background();
trx_sys_mutex_enter();
ut_d(trx->in_mysql_trx_list = TRUE);
UT_LIST_ADD_FIRST(trx_sys->mysql_trx_list, trx);
trx_sys_mutex_exit();
return(trx);
}
/** Check state of transaction before freeing it.
@param trx trx object to validate */
static
void
trx_validate_state_before_free(trx_t* trx)
{
if (trx->declared_to_be_inside_innodb) {
ib::error() << "Freeing a trx (" << trx << ", "
<< trx_get_id_for_print(trx) << ") which is declared"
" to be processing inside InnoDB";
trx_print(stderr, trx, 600);
putc('\n', stderr);
/* This is an error but not a fatal error. We must keep
the counters like srv_conc_n_threads accurate. */
srv_conc_force_exit_innodb(trx);
}
if (trx->n_mysql_tables_in_use != 0
|| trx->mysql_n_tables_locked != 0) {
ib::error() << "MySQL is freeing a thd though"
" trx->n_mysql_tables_in_use is "
<< trx->n_mysql_tables_in_use
<< " and trx->mysql_n_tables_locked is "
<< trx->mysql_n_tables_locked << ".";
trx_print(stderr, trx, 600);
ut_print_buf(stderr, trx, sizeof(trx_t));
putc('\n', stderr);
}
trx->dict_operation = TRX_DICT_OP_NONE;
assert_trx_is_inactive(trx);
}
/** Free and initialize a transaction object instantinated during recovery.
@param trx trx object to free and initialize during recovery */
void
trx_free_resurrected(trx_t* trx)
{
trx_validate_state_before_free(trx);
trx_init(trx);
trx_free(trx);
}
/** Free a transaction that was allocated by background or user threads.
@param trx trx object to free */
void
trx_free_for_background(trx_t* trx)
{
trx_validate_state_before_free(trx);
trx_free(trx);
}
/********************************************************************//**
At shutdown, frees a transaction object that is in the PREPARED state. */
void
trx_free_prepared(
/*==============*/
trx_t* trx) /*!< in, own: trx object */
{
ut_a(trx_state_eq(trx, TRX_STATE_PREPARED)
|| (trx_state_eq(trx, TRX_STATE_ACTIVE)
&& trx->is_recovered
&& (!srv_was_started
|| srv_read_only_mode
|| srv_force_recovery >= SRV_FORCE_NO_TRX_UNDO)));
ut_a(trx->magic_n == TRX_MAGIC_N);
lock_trx_release_locks(trx);
trx_undo_free_prepared(trx);
assert_trx_in_rw_list(trx);
ut_a(!trx->read_only);
ut_d(trx->in_rw_trx_list = FALSE);
trx->state = TRX_STATE_NOT_STARTED;
/* Undo trx_resurrect_table_locks(). */
lock_trx_lock_list_init(&trx->lock.trx_locks);
/* Note: This vector is not guaranteed to be empty because the
transaction was never committed and therefore lock_trx_release()
was not called. */
trx->lock.table_locks.clear();
trx_free(trx);
}
/** Disconnect a transaction from MySQL and optionally mark it as if
it's been recovered. For the marking the transaction must be in prepared state.
The recovery-marked transaction is going to survive "alone" so its association
with the mysql handle is destroyed now rather than when it will be
finally freed.
@param[in,out] trx transaction
@param[in] prepared boolean value to specify whether trx is
for recovery or not. */
inline
void
trx_disconnect_from_mysql(
trx_t* trx,
bool prepared)
{
trx_sys_mutex_enter();
ut_ad(trx->in_mysql_trx_list);
ut_d(trx->in_mysql_trx_list = FALSE);
UT_LIST_REMOVE(trx_sys->mysql_trx_list, trx);
if (trx->read_view != NULL) {
trx_sys->mvcc->view_close(trx->read_view, true);
}
ut_ad(trx_sys_validate_trx_list());
if (prepared) {
ut_ad(trx_state_eq(trx, TRX_STATE_PREPARED));
trx->is_recovered = true;
trx_sys->n_prepared_recovered_trx++;
trx->mysql_thd = NULL;
/* todo/fixme: suggest to do it at innodb prepare */
trx->will_lock = 0;
}
trx_sys_mutex_exit();
}
/** Disconnect a transaction from MySQL.
@param[in,out] trx transaction */
inline
void
trx_disconnect_plain(trx_t* trx)
{
trx_disconnect_from_mysql(trx, false);
}
/** Disconnect a prepared transaction from MySQL.
@param[in,out] trx transaction */
void
trx_disconnect_prepared(trx_t* trx)
{
trx_disconnect_from_mysql(trx, true);
}
/** Free a transaction object for MySQL.
@param[in,out] trx transaction */
void
trx_free_for_mysql(trx_t* trx)
{
trx_disconnect_plain(trx);
trx_free_for_background(trx);
}
/****************************************************************//**
Resurrect the table locks for a resurrected transaction. */
static
void
trx_resurrect_table_locks(
/*======================*/
trx_t* trx, /*!< in/out: transaction */
const trx_undo_ptr_t* undo_ptr,
/*!< in: pointer to undo segment. */
const trx_undo_t* undo) /*!< in: undo log */
{
mtr_t mtr;
page_t* undo_page;
trx_undo_rec_t* undo_rec;
table_id_set tables;
ut_ad(undo == undo_ptr->insert_undo || undo == undo_ptr->update_undo);
if (trx_state_eq(trx, TRX_STATE_COMMITTED_IN_MEMORY) || undo->empty) {
return;
}
mtr_start(&mtr);
/* trx_rseg_mem_create() may have acquired an X-latch on this
page, so we cannot acquire an S-latch. */
undo_page = trx_undo_page_get(
page_id_t(undo->space, undo->top_page_no), &mtr);
undo_rec = undo_page + undo->top_offset;
do {
ulint type;
undo_no_t undo_no;
table_id_t table_id;
ulint cmpl_info;
bool updated_extern;
page_t* undo_rec_page = page_align(undo_rec);
if (undo_rec_page != undo_page) {
mtr.release_page(undo_page, MTR_MEMO_PAGE_X_FIX);
undo_page = undo_rec_page;
}
trx_undo_rec_get_pars(
undo_rec, &type, &cmpl_info,
&updated_extern, &undo_no, &table_id);
tables.insert(table_id);
undo_rec = trx_undo_get_prev_rec(
undo_rec, undo->hdr_page_no,
undo->hdr_offset, false, &mtr);
} while (undo_rec);
mtr_commit(&mtr);
for (table_id_set::const_iterator i = tables.begin();
i != tables.end(); i++) {
if (dict_table_t* table = dict_table_open_on_id(
*i, FALSE, DICT_TABLE_OP_LOAD_TABLESPACE)) {
if (table->ibd_file_missing
|| dict_table_is_temporary(table)) {
mutex_enter(&dict_sys->mutex);
dict_table_close(table, TRUE, FALSE);
dict_table_remove_from_cache(table);
mutex_exit(&dict_sys->mutex);
continue;
}
if (trx->state == TRX_STATE_PREPARED) {
trx->mod_tables.insert(table);
}
lock_table_ix_resurrect(table, trx);
DBUG_PRINT("ib_trx",
("resurrect" TRX_ID_FMT
" table '%s' IX lock from %s undo",
trx_get_id_for_print(trx),
table->name.m_name,
undo == undo_ptr->insert_undo
? "insert" : "update"));
dict_table_close(table, FALSE, FALSE);
}
}
}
/****************************************************************//**
Resurrect the transactions that were doing inserts the time of the
crash, they need to be undone.
@return trx_t instance */
static
trx_t*
trx_resurrect_insert(
/*=================*/
trx_undo_t* undo, /*!< in: entry to UNDO */
trx_rseg_t* rseg) /*!< in: rollback segment */
{
trx_t* trx;
trx = trx_allocate_for_background();
ut_d(trx->start_file = __FILE__);
ut_d(trx->start_line = __LINE__);
trx->rsegs.m_redo.rseg = rseg;
/* For transactions with active data will not have rseg size = 1
or will not qualify for purge limit criteria. So it is safe to increment
this trx_ref_count w/o mutex protection. */
++trx->rsegs.m_redo.rseg->trx_ref_count;
*trx->xid = undo->xid;
trx->id = undo->trx_id;
trx->rsegs.m_redo.insert_undo = undo;
trx->is_recovered = true;
/* This is single-threaded startup code, we do not need the
protection of trx->mutex or trx_sys->mutex here. */
if (undo->state != TRX_UNDO_ACTIVE) {
/* Prepared transactions are left in the prepared state
waiting for a commit or abort decision from MySQL */
if (undo->state == TRX_UNDO_PREPARED) {
ib::info() << "Transaction "
<< trx_get_id_for_print(trx)
<< " was in the XA prepared state.";
if (srv_force_recovery == 0) {
trx->state = TRX_STATE_PREPARED;
++trx_sys->n_prepared_trx;
++trx_sys->n_prepared_recovered_trx;
} else {
ib::info() << "Since innodb_force_recovery"
" > 0, we will force a rollback.";
trx->state = TRX_STATE_ACTIVE;
}
} else {
trx->state = TRX_STATE_COMMITTED_IN_MEMORY;
}
/* We give a dummy value for the trx no; this should have no
relevance since purge is not interested in committed
transaction numbers, unless they are in the history
list, in which case it looks the number from the disk based
undo log structure */
trx->no = trx->id;
} else {
trx->state = TRX_STATE_ACTIVE;
/* A running transaction always has the number
field inited to TRX_ID_MAX */
trx->no = TRX_ID_MAX;
}
/* trx_start_low() is not called with resurrect, so need to initialize
start time here.*/
if (trx->state == TRX_STATE_ACTIVE
|| trx->state == TRX_STATE_PREPARED) {
trx->start_time = ut_time();
}
if (undo->dict_operation) {
trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
trx->table_id = undo->table_id;
}
if (!undo->empty) {
trx->undo_no = undo->top_undo_no + 1;
trx->undo_rseg_space = undo->rseg->space;
}
return(trx);
}
/****************************************************************//**
Prepared transactions are left in the prepared state waiting for a
commit or abort decision from MySQL */
static
void
trx_resurrect_update_in_prepared_state(
/*===================================*/
trx_t* trx, /*!< in,out: transaction */
const trx_undo_t* undo) /*!< in: update UNDO record */
{
/* This is single-threaded startup code, we do not need the
protection of trx->mutex or trx_sys->mutex here. */
if (undo->state == TRX_UNDO_PREPARED) {
ib::info() << "Transaction " << trx_get_id_for_print(trx)
<< " was in the XA prepared state.";
if (srv_force_recovery == 0) {
ut_ad(trx->state != TRX_STATE_FORCED_ROLLBACK);
if (trx_state_eq(trx, TRX_STATE_NOT_STARTED)) {
++trx_sys->n_prepared_trx;
++trx_sys->n_prepared_recovered_trx;
} else {
ut_ad(trx_state_eq(trx, TRX_STATE_PREPARED));
}
trx->state = TRX_STATE_PREPARED;
} else {
ib::info() << "Since innodb_force_recovery > 0, we"
" will rollback it anyway.";
trx->state = TRX_STATE_ACTIVE;
}
} else {
trx->state = TRX_STATE_COMMITTED_IN_MEMORY;
}
}
/****************************************************************//**
Resurrect the transactions that were doing updates the time of the
crash, they need to be undone. */
static
void
trx_resurrect_update(
/*=================*/
trx_t* trx, /*!< in/out: transaction */
trx_undo_t* undo, /*!< in/out: update UNDO record */
trx_rseg_t* rseg) /*!< in/out: rollback segment */
{
trx->rsegs.m_redo.rseg = rseg;
/* For transactions with active data will not have rseg size = 1
or will not qualify for purge limit criteria. So it is safe to increment
this trx_ref_count w/o mutex protection. */
++trx->rsegs.m_redo.rseg->trx_ref_count;
*trx->xid = undo->xid;
trx->id = undo->trx_id;
trx->rsegs.m_redo.update_undo = undo;
trx->is_recovered = true;
/* This is single-threaded startup code, we do not need the
protection of trx->mutex or trx_sys->mutex here. */
if (undo->state != TRX_UNDO_ACTIVE) {
trx_resurrect_update_in_prepared_state(trx, undo);
/* We give a dummy value for the trx number */
trx->no = trx->id;
} else {
trx->state = TRX_STATE_ACTIVE;
/* A running transaction always has the number field inited to
TRX_ID_MAX */
trx->no = TRX_ID_MAX;
}
/* trx_start_low() is not called with resurrect, so need to initialize
start time here.*/
if (trx->state == TRX_STATE_ACTIVE
|| trx->state == TRX_STATE_PREPARED) {
trx->start_time = ut_time();
}
if (undo->dict_operation) {
trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
trx->table_id = undo->table_id;
}
if (!undo->empty && undo->top_undo_no >= trx->undo_no) {
trx->undo_no = undo->top_undo_no + 1;
trx->undo_rseg_space = undo->rseg->space;
}
}
/** Initialize (resurrect) transactions at startup. */
void
trx_lists_init_at_db_start()