~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

« back to all changes in this revision

Viewing changes to plugin/innobase/trx/trx0undo.c

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2011-01-04 09:31:58 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110104093158-smhgvkfdi2y9au3i
Tags: 2011.01.07-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 
3
 
Copyright (C) 1996, 2009, Innobase Oy. All Rights Reserved.
4
 
 
5
 
This program is free software; you can redistribute it and/or modify it under
6
 
the terms of the GNU General Public License as published by the Free Software
7
 
Foundation; version 2 of the License.
8
 
 
9
 
This program is distributed in the hope that it will be useful, but WITHOUT
10
 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
 
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
 
 
13
 
You should have received a copy of the GNU General Public License along with
14
 
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
 
St, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
*****************************************************************************/
18
 
 
19
 
/**************************************************//**
20
 
@file trx/trx0undo.c
21
 
Transaction undo log
22
 
 
23
 
Created 3/26/1996 Heikki Tuuri
24
 
*******************************************************/
25
 
 
26
 
#include "trx0undo.h"
27
 
 
28
 
#ifdef UNIV_NONINL
29
 
#include "trx0undo.ic"
30
 
#endif
31
 
 
32
 
#include "fsp0fsp.h"
33
 
#ifndef UNIV_HOTBACKUP
34
 
#include "mach0data.h"
35
 
#include "mtr0log.h"
36
 
#include "trx0rseg.h"
37
 
#include "trx0trx.h"
38
 
#include "srv0srv.h"
39
 
#include "trx0rec.h"
40
 
#include "trx0purge.h"
41
 
 
42
 
/* How should the old versions in the history list be managed?
43
 
   ----------------------------------------------------------
44
 
If each transaction is given a whole page for its update undo log, file
45
 
space consumption can be 10 times higher than necessary. Therefore,
46
 
partly filled update undo log pages should be reusable. But then there
47
 
is no way individual pages can be ordered so that the ordering agrees
48
 
with the serialization numbers of the transactions on the pages. Thus,
49
 
the history list must be formed of undo logs, not their header pages as
50
 
it was in the old implementation.
51
 
        However, on a single header page the transactions are placed in
52
 
the order of their serialization numbers. As old versions are purged, we
53
 
may free the page when the last transaction on the page has been purged.
54
 
        A problem is that the purge has to go through the transactions
55
 
in the serialization order. This means that we have to look through all
56
 
rollback segments for the one that has the smallest transaction number
57
 
in its history list.
58
 
        When should we do a purge? A purge is necessary when space is
59
 
running out in any of the rollback segments. Then we may have to purge
60
 
also old version which might be needed by some consistent read. How do
61
 
we trigger the start of a purge? When a transaction writes to an undo log,
62
 
it may notice that the space is running out. When a read view is closed,
63
 
it may make some history superfluous. The server can have an utility which
64
 
periodically checks if it can purge some history.
65
 
        In a parallellized purge we have the problem that a query thread
66
 
can remove a delete marked clustered index record before another query
67
 
thread has processed an earlier version of the record, which cannot then
68
 
be done because the row cannot be constructed from the clustered index
69
 
record. To avoid this problem, we will store in the update and delete mark
70
 
undo record also the columns necessary to construct the secondary index
71
 
entries which are modified.
72
 
        We can latch the stack of versions of a single clustered index record
73
 
by taking a latch on the clustered index page. As long as the latch is held,
74
 
no new versions can be added and no versions removed by undo. But, a purge
75
 
can still remove old versions from the bottom of the stack. */
76
 
 
77
 
/* How to protect rollback segments, undo logs, and history lists with
78
 
   -------------------------------------------------------------------
79
 
latches?
80
 
-------
81
 
The contention of the kernel mutex should be minimized. When a transaction
82
 
does its first insert or modify in an index, an undo log is assigned for it.
83
 
Then we must have an x-latch to the rollback segment header.
84
 
        When the transaction does more modifys or rolls back, the undo log is
85
 
protected with undo_mutex in the transaction.
86
 
        When the transaction commits, its insert undo log is either reset and
87
 
cached for a fast reuse, or freed. In these cases we must have an x-latch on
88
 
the rollback segment page. The update undo log is put to the history list. If
89
 
it is not suitable for reuse, its slot in the rollback segment is reset. In
90
 
both cases, an x-latch must be acquired on the rollback segment.
91
 
        The purge operation steps through the history list without modifying
92
 
it until a truncate operation occurs, which can remove undo logs from the end
93
 
of the list and release undo log segments. In stepping through the list,
94
 
s-latches on the undo log pages are enough, but in a truncate, x-latches must
95
 
be obtained on the rollback segment and individual pages. */
96
 
#endif /* !UNIV_HOTBACKUP */
97
 
 
98
 
/********************************************************************//**
99
 
Initializes the fields in an undo log segment page. */
100
 
static
101
 
void
102
 
trx_undo_page_init(
103
 
/*===============*/
104
 
        page_t* undo_page,      /*!< in: undo log segment page */
105
 
        ulint   type,           /*!< in: undo log segment type */
106
 
        mtr_t*  mtr);           /*!< in: mtr */
107
 
 
108
 
#ifndef UNIV_HOTBACKUP
109
 
/********************************************************************//**
110
 
Creates and initializes an undo log memory object.
111
 
@return own: the undo log memory object */
112
 
static
113
 
trx_undo_t*
114
 
trx_undo_mem_create(
115
 
/*================*/
116
 
        trx_rseg_t*     rseg,   /*!< in: rollback segment memory object */
117
 
        ulint           id,     /*!< in: slot index within rseg */
118
 
        ulint           type,   /*!< in: type of the log: TRX_UNDO_INSERT or
119
 
                                TRX_UNDO_UPDATE */
120
 
        trx_id_t        trx_id, /*!< in: id of the trx for which the undo log
121
 
                                is created */
122
 
        const XID*      xid,    /*!< in: X/Open XA transaction identification*/
123
 
        ulint           page_no,/*!< in: undo log header page number */
124
 
        ulint           offset);/*!< in: undo log header byte offset on page */
125
 
#endif /* !UNIV_HOTBACKUP */
126
 
/***************************************************************//**
127
 
Initializes a cached insert undo log header page for new use. NOTE that this
128
 
function has its own log record type MLOG_UNDO_HDR_REUSE. You must NOT change
129
 
the operation of this function!
130
 
@return undo log header byte offset on page */
131
 
static
132
 
ulint
133
 
trx_undo_insert_header_reuse(
134
 
/*=========================*/
135
 
        page_t*         undo_page,      /*!< in/out: insert undo log segment
136
 
                                        header page, x-latched */
137
 
        trx_id_t        trx_id,         /*!< in: transaction id */
138
 
        mtr_t*          mtr);           /*!< in: mtr */
139
 
/**********************************************************************//**
140
 
If an update undo log can be discarded immediately, this function frees the
141
 
space, resetting the page to the proper state for caching. */
142
 
static
143
 
void
144
 
trx_undo_discard_latest_update_undo(
145
 
/*================================*/
146
 
        page_t* undo_page,      /*!< in: header page of an undo log of size 1 */
147
 
        mtr_t*  mtr);           /*!< in: mtr */
148
 
 
149
 
#ifndef UNIV_HOTBACKUP
150
 
/***********************************************************************//**
151
 
Gets the previous record in an undo log from the previous page.
152
 
@return undo log record, the page s-latched, NULL if none */
153
 
static
154
 
trx_undo_rec_t*
155
 
trx_undo_get_prev_rec_from_prev_page(
156
 
/*=================================*/
157
 
        trx_undo_rec_t* rec,    /*!< in: undo record */
158
 
        ulint           page_no,/*!< in: undo log header page number */
159
 
        ulint           offset, /*!< in: undo log header offset on page */
160
 
        mtr_t*          mtr)    /*!< in: mtr */
161
 
{
162
 
        ulint   space;
163
 
        ulint   zip_size;
164
 
        ulint   prev_page_no;
165
 
        page_t* prev_page;
166
 
        page_t* undo_page;
167
 
 
168
 
        undo_page = page_align(rec);
169
 
 
170
 
        prev_page_no = flst_get_prev_addr(undo_page + TRX_UNDO_PAGE_HDR
171
 
                                          + TRX_UNDO_PAGE_NODE, mtr)
172
 
                .page;
173
 
 
174
 
        if (prev_page_no == FIL_NULL) {
175
 
 
176
 
                return(NULL);
177
 
        }
178
 
 
179
 
        space = page_get_space_id(undo_page);
180
 
        zip_size = fil_space_get_zip_size(space);
181
 
 
182
 
        prev_page = trx_undo_page_get_s_latched(space, zip_size,
183
 
                                                prev_page_no, mtr);
184
 
 
185
 
        return(trx_undo_page_get_last_rec(prev_page, page_no, offset));
186
 
}
187
 
 
188
 
/***********************************************************************//**
189
 
Gets the previous record in an undo log.
190
 
@return undo log record, the page s-latched, NULL if none */
191
 
UNIV_INTERN
192
 
trx_undo_rec_t*
193
 
trx_undo_get_prev_rec(
194
 
/*==================*/
195
 
        trx_undo_rec_t* rec,    /*!< in: undo record */
196
 
        ulint           page_no,/*!< in: undo log header page number */
197
 
        ulint           offset, /*!< in: undo log header offset on page */
198
 
        mtr_t*          mtr)    /*!< in: mtr */
199
 
{
200
 
        trx_undo_rec_t* prev_rec;
201
 
 
202
 
        prev_rec = trx_undo_page_get_prev_rec(rec, page_no, offset);
203
 
 
204
 
        if (prev_rec) {
205
 
 
206
 
                return(prev_rec);
207
 
        }
208
 
 
209
 
        /* We have to go to the previous undo log page to look for the
210
 
        previous record */
211
 
 
212
 
        return(trx_undo_get_prev_rec_from_prev_page(rec, page_no, offset,
213
 
                                                    mtr));
214
 
}
215
 
 
216
 
/***********************************************************************//**
217
 
Gets the next record in an undo log from the next page.
218
 
@return undo log record, the page latched, NULL if none */
219
 
static
220
 
trx_undo_rec_t*
221
 
trx_undo_get_next_rec_from_next_page(
222
 
/*=================================*/
223
 
        ulint   space,  /*!< in: undo log header space */
224
 
        ulint   zip_size,/*!< in: compressed page size in bytes
225
 
                        or 0 for uncompressed pages */
226
 
        page_t* undo_page, /*!< in: undo log page */
227
 
        ulint   page_no,/*!< in: undo log header page number */
228
 
        ulint   offset, /*!< in: undo log header offset on page */
229
 
        ulint   mode,   /*!< in: latch mode: RW_S_LATCH or RW_X_LATCH */
230
 
        mtr_t*  mtr)    /*!< in: mtr */
231
 
{
232
 
        trx_ulogf_t*    log_hdr;
233
 
        ulint           next_page_no;
234
 
        page_t*         next_page;
235
 
        ulint           next;
236
 
 
237
 
        if (page_no == page_get_page_no(undo_page)) {
238
 
 
239
 
                log_hdr = undo_page + offset;
240
 
                next = mach_read_from_2(log_hdr + TRX_UNDO_NEXT_LOG);
241
 
 
242
 
                if (next != 0) {
243
 
 
244
 
                        return(NULL);
245
 
                }
246
 
        }
247
 
 
248
 
        next_page_no = flst_get_next_addr(undo_page + TRX_UNDO_PAGE_HDR
249
 
                                          + TRX_UNDO_PAGE_NODE, mtr)
250
 
                .page;
251
 
        if (next_page_no == FIL_NULL) {
252
 
 
253
 
                return(NULL);
254
 
        }
255
 
 
256
 
        if (mode == RW_S_LATCH) {
257
 
                next_page = trx_undo_page_get_s_latched(space, zip_size,
258
 
                                                        next_page_no, mtr);
259
 
        } else {
260
 
                ut_ad(mode == RW_X_LATCH);
261
 
                next_page = trx_undo_page_get(space, zip_size,
262
 
                                              next_page_no, mtr);
263
 
        }
264
 
 
265
 
        return(trx_undo_page_get_first_rec(next_page, page_no, offset));
266
 
}
267
 
 
268
 
/***********************************************************************//**
269
 
Gets the next record in an undo log.
270
 
@return undo log record, the page s-latched, NULL if none */
271
 
UNIV_INTERN
272
 
trx_undo_rec_t*
273
 
trx_undo_get_next_rec(
274
 
/*==================*/
275
 
        trx_undo_rec_t* rec,    /*!< in: undo record */
276
 
        ulint           page_no,/*!< in: undo log header page number */
277
 
        ulint           offset, /*!< in: undo log header offset on page */
278
 
        mtr_t*          mtr)    /*!< in: mtr */
279
 
{
280
 
        ulint           space;
281
 
        ulint           zip_size;
282
 
        trx_undo_rec_t* next_rec;
283
 
 
284
 
        next_rec = trx_undo_page_get_next_rec(rec, page_no, offset);
285
 
 
286
 
        if (next_rec) {
287
 
                return(next_rec);
288
 
        }
289
 
 
290
 
        space = page_get_space_id(page_align(rec));
291
 
        zip_size = fil_space_get_zip_size(space);
292
 
 
293
 
        return(trx_undo_get_next_rec_from_next_page(space, zip_size,
294
 
                                                    page_align(rec),
295
 
                                                    page_no, offset,
296
 
                                                    RW_S_LATCH, mtr));
297
 
}
298
 
 
299
 
/***********************************************************************//**
300
 
Gets the first record in an undo log.
301
 
@return undo log record, the page latched, NULL if none */
302
 
UNIV_INTERN
303
 
trx_undo_rec_t*
304
 
trx_undo_get_first_rec(
305
 
/*===================*/
306
 
        ulint   space,  /*!< in: undo log header space */
307
 
        ulint   zip_size,/*!< in: compressed page size in bytes
308
 
                        or 0 for uncompressed pages */
309
 
        ulint   page_no,/*!< in: undo log header page number */
310
 
        ulint   offset, /*!< in: undo log header offset on page */
311
 
        ulint   mode,   /*!< in: latching mode: RW_S_LATCH or RW_X_LATCH */
312
 
        mtr_t*  mtr)    /*!< in: mtr */
313
 
{
314
 
        page_t*         undo_page;
315
 
        trx_undo_rec_t* rec;
316
 
 
317
 
        if (mode == RW_S_LATCH) {
318
 
                undo_page = trx_undo_page_get_s_latched(space, zip_size,
319
 
                                                        page_no, mtr);
320
 
        } else {
321
 
                undo_page = trx_undo_page_get(space, zip_size, page_no, mtr);
322
 
        }
323
 
 
324
 
        rec = trx_undo_page_get_first_rec(undo_page, page_no, offset);
325
 
 
326
 
        if (rec) {
327
 
                return(rec);
328
 
        }
329
 
 
330
 
        return(trx_undo_get_next_rec_from_next_page(space, zip_size,
331
 
                                                    undo_page, page_no, offset,
332
 
                                                    mode, mtr));
333
 
}
334
 
 
335
 
/*============== UNDO LOG FILE COPY CREATION AND FREEING ==================*/
336
 
 
337
 
/**********************************************************************//**
338
 
Writes the mtr log entry of an undo log page initialization. */
339
 
UNIV_INLINE
340
 
void
341
 
trx_undo_page_init_log(
342
 
/*===================*/
343
 
        page_t* undo_page,      /*!< in: undo log page */
344
 
        ulint   type,           /*!< in: undo log type */
345
 
        mtr_t*  mtr)            /*!< in: mtr */
346
 
{
347
 
        mlog_write_initial_log_record(undo_page, MLOG_UNDO_INIT, mtr);
348
 
 
349
 
        mlog_catenate_ulint_compressed(mtr, type);
350
 
}
351
 
#else /* !UNIV_HOTBACKUP */
352
 
# define trx_undo_page_init_log(undo_page,type,mtr) ((void) 0)
353
 
#endif /* !UNIV_HOTBACKUP */
354
 
 
355
 
/***********************************************************//**
356
 
Parses the redo log entry of an undo log page initialization.
357
 
@return end of log record or NULL */
358
 
UNIV_INTERN
359
 
byte*
360
 
trx_undo_parse_page_init(
361
 
/*=====================*/
362
 
        byte*   ptr,    /*!< in: buffer */
363
 
        byte*   end_ptr,/*!< in: buffer end */
364
 
        page_t* page,   /*!< in: page or NULL */
365
 
        mtr_t*  mtr)    /*!< in: mtr or NULL */
366
 
{
367
 
        ulint   type;
368
 
 
369
 
        ptr = mach_parse_compressed(ptr, end_ptr, &type);
370
 
 
371
 
        if (ptr == NULL) {
372
 
 
373
 
                return(NULL);
374
 
        }
375
 
 
376
 
        if (page) {
377
 
                trx_undo_page_init(page, type, mtr);
378
 
        }
379
 
 
380
 
        return(ptr);
381
 
}
382
 
 
383
 
/********************************************************************//**
384
 
Initializes the fields in an undo log segment page. */
385
 
static
386
 
void
387
 
trx_undo_page_init(
388
 
/*===============*/
389
 
        page_t* undo_page,      /*!< in: undo log segment page */
390
 
        ulint   type,           /*!< in: undo log segment type */
391
 
        mtr_t*  mtr)            /*!< in: mtr */
392
 
{
393
 
        trx_upagef_t*   page_hdr;
394
 
 
395
 
        page_hdr = undo_page + TRX_UNDO_PAGE_HDR;
396
 
 
397
 
        mach_write_to_2(page_hdr + TRX_UNDO_PAGE_TYPE, type);
398
 
 
399
 
        mach_write_to_2(page_hdr + TRX_UNDO_PAGE_START,
400
 
                        TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_HDR_SIZE);
401
 
        mach_write_to_2(page_hdr + TRX_UNDO_PAGE_FREE,
402
 
                        TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_HDR_SIZE);
403
 
 
404
 
        fil_page_set_type(undo_page, FIL_PAGE_UNDO_LOG);
405
 
 
406
 
        trx_undo_page_init_log(undo_page, type, mtr);
407
 
}
408
 
 
409
 
#ifndef UNIV_HOTBACKUP
410
 
/***************************************************************//**
411
 
Creates a new undo log segment in file.
412
 
@return DB_SUCCESS if page creation OK possible error codes are:
413
 
DB_TOO_MANY_CONCURRENT_TRXS DB_OUT_OF_FILE_SPACE */
414
 
static
415
 
ulint
416
 
trx_undo_seg_create(
417
 
/*================*/
418
 
        trx_rseg_t*     rseg __attribute__((unused)),/*!< in: rollback segment */
419
 
        trx_rsegf_t*    rseg_hdr,/*!< in: rollback segment header, page
420
 
                                x-latched */
421
 
        ulint           type,   /*!< in: type of the segment: TRX_UNDO_INSERT or
422
 
                                TRX_UNDO_UPDATE */
423
 
        ulint*          id,     /*!< out: slot index within rseg header */
424
 
        page_t**        undo_page,
425
 
                                /*!< out: segment header page x-latched, NULL
426
 
                                if there was an error */
427
 
        mtr_t*          mtr)    /*!< in: mtr */
428
 
{
429
 
        ulint           slot_no;
430
 
        ulint           space;
431
 
        buf_block_t*    block;
432
 
        trx_upagef_t*   page_hdr;
433
 
        trx_usegf_t*    seg_hdr;
434
 
        ulint           n_reserved;
435
 
        ibool           success;
436
 
        ulint           err = DB_SUCCESS;
437
 
 
438
 
        ut_ad(mtr && id && rseg_hdr);
439
 
        ut_ad(mutex_own(&(rseg->mutex)));
440
 
 
441
 
        /*      fputs(type == TRX_UNDO_INSERT
442
 
        ? "Creating insert undo log segment\n"
443
 
        : "Creating update undo log segment\n", stderr); */
444
 
        slot_no = trx_rsegf_undo_find_free(rseg_hdr, mtr);
445
 
 
446
 
        if (slot_no == ULINT_UNDEFINED) {
447
 
                ut_print_timestamp(stderr);
448
 
                fprintf(stderr,
449
 
                        "  InnoDB: Warning: cannot find a free slot for"
450
 
                        " an undo log. Do you have too\n"
451
 
                        "InnoDB: many active transactions"
452
 
                        " running concurrently?\n");
453
 
 
454
 
                return(DB_TOO_MANY_CONCURRENT_TRXS);
455
 
        }
456
 
 
457
 
        space = page_get_space_id(page_align(rseg_hdr));
458
 
 
459
 
        success = fsp_reserve_free_extents(&n_reserved, space, 2, FSP_UNDO,
460
 
                                           mtr);
461
 
        if (!success) {
462
 
 
463
 
                return(DB_OUT_OF_FILE_SPACE);
464
 
        }
465
 
 
466
 
        /* Allocate a new file segment for the undo log */
467
 
        block = fseg_create_general(space, 0,
468
 
                                    TRX_UNDO_SEG_HDR
469
 
                                    + TRX_UNDO_FSEG_HEADER, TRUE, mtr);
470
 
 
471
 
        fil_space_release_free_extents(space, n_reserved);
472
 
 
473
 
        if (block == NULL) {
474
 
                /* No space left */
475
 
 
476
 
                return(DB_OUT_OF_FILE_SPACE);
477
 
        }
478
 
 
479
 
        buf_block_dbg_add_level(block, SYNC_TRX_UNDO_PAGE);
480
 
 
481
 
        *undo_page = buf_block_get_frame(block);
482
 
 
483
 
        page_hdr = *undo_page + TRX_UNDO_PAGE_HDR;
484
 
        seg_hdr = *undo_page + TRX_UNDO_SEG_HDR;
485
 
 
486
 
        trx_undo_page_init(*undo_page, type, mtr);
487
 
 
488
 
        mlog_write_ulint(page_hdr + TRX_UNDO_PAGE_FREE,
489
 
                         TRX_UNDO_SEG_HDR + TRX_UNDO_SEG_HDR_SIZE,
490
 
                         MLOG_2BYTES, mtr);
491
 
 
492
 
        mlog_write_ulint(seg_hdr + TRX_UNDO_LAST_LOG, 0, MLOG_2BYTES, mtr);
493
 
 
494
 
        flst_init(seg_hdr + TRX_UNDO_PAGE_LIST, mtr);
495
 
 
496
 
        flst_add_last(seg_hdr + TRX_UNDO_PAGE_LIST,
497
 
                      page_hdr + TRX_UNDO_PAGE_NODE, mtr);
498
 
 
499
 
        trx_rsegf_set_nth_undo(rseg_hdr, slot_no,
500
 
                               page_get_page_no(*undo_page), mtr);
501
 
        *id = slot_no;
502
 
 
503
 
        return(err);
504
 
}
505
 
 
506
 
/**********************************************************************//**
507
 
Writes the mtr log entry of an undo log header initialization. */
508
 
UNIV_INLINE
509
 
void
510
 
trx_undo_header_create_log(
511
 
/*=======================*/
512
 
        const page_t*   undo_page,      /*!< in: undo log header page */
513
 
        trx_id_t        trx_id,         /*!< in: transaction id */
514
 
        mtr_t*          mtr)            /*!< in: mtr */
515
 
{
516
 
        mlog_write_initial_log_record(undo_page, MLOG_UNDO_HDR_CREATE, mtr);
517
 
 
518
 
        mlog_catenate_ull_compressed(mtr, trx_id);
519
 
}
520
 
#else /* !UNIV_HOTBACKUP */
521
 
# define trx_undo_header_create_log(undo_page,trx_id,mtr) ((void) 0)
522
 
#endif /* !UNIV_HOTBACKUP */
523
 
 
524
 
/***************************************************************//**
525
 
Creates a new undo log header in file. NOTE that this function has its own
526
 
log record type MLOG_UNDO_HDR_CREATE. You must NOT change the operation of
527
 
this function!
528
 
@return header byte offset on page */
529
 
static
530
 
ulint
531
 
trx_undo_header_create(
532
 
/*===================*/
533
 
        page_t*         undo_page,      /*!< in/out: undo log segment
534
 
                                        header page, x-latched; it is
535
 
                                        assumed that there is
536
 
                                        TRX_UNDO_LOG_XA_HDR_SIZE bytes
537
 
                                        free space on it */
538
 
        trx_id_t        trx_id,         /*!< in: transaction id */
539
 
        mtr_t*          mtr)            /*!< in: mtr */
540
 
{
541
 
        trx_upagef_t*   page_hdr;
542
 
        trx_usegf_t*    seg_hdr;
543
 
        trx_ulogf_t*    log_hdr;
544
 
        trx_ulogf_t*    prev_log_hdr;
545
 
        ulint           prev_log;
546
 
        ulint           free;
547
 
        ulint           new_free;
548
 
 
549
 
        ut_ad(mtr && undo_page);
550
 
 
551
 
        page_hdr = undo_page + TRX_UNDO_PAGE_HDR;
552
 
        seg_hdr = undo_page + TRX_UNDO_SEG_HDR;
553
 
 
554
 
        free = mach_read_from_2(page_hdr + TRX_UNDO_PAGE_FREE);
555
 
 
556
 
        log_hdr = undo_page + free;
557
 
 
558
 
        new_free = free + TRX_UNDO_LOG_OLD_HDR_SIZE;
559
 
 
560
 
        ut_a(free + TRX_UNDO_LOG_XA_HDR_SIZE < UNIV_PAGE_SIZE - 100);
561
 
 
562
 
        mach_write_to_2(page_hdr + TRX_UNDO_PAGE_START, new_free);
563
 
 
564
 
        mach_write_to_2(page_hdr + TRX_UNDO_PAGE_FREE, new_free);
565
 
 
566
 
        mach_write_to_2(seg_hdr + TRX_UNDO_STATE, TRX_UNDO_ACTIVE);
567
 
 
568
 
        prev_log = mach_read_from_2(seg_hdr + TRX_UNDO_LAST_LOG);
569
 
 
570
 
        if (prev_log != 0) {
571
 
                prev_log_hdr = undo_page + prev_log;
572
 
 
573
 
                mach_write_to_2(prev_log_hdr + TRX_UNDO_NEXT_LOG, free);
574
 
        }
575
 
 
576
 
        mach_write_to_2(seg_hdr + TRX_UNDO_LAST_LOG, free);
577
 
 
578
 
        log_hdr = undo_page + free;
579
 
 
580
 
        mach_write_to_2(log_hdr + TRX_UNDO_DEL_MARKS, TRUE);
581
 
 
582
 
        mach_write_to_8(log_hdr + TRX_UNDO_TRX_ID, trx_id);
583
 
        mach_write_to_2(log_hdr + TRX_UNDO_LOG_START, new_free);
584
 
 
585
 
        mach_write_to_1(log_hdr + TRX_UNDO_XID_EXISTS, FALSE);
586
 
        mach_write_to_1(log_hdr + TRX_UNDO_DICT_TRANS, FALSE);
587
 
 
588
 
        mach_write_to_2(log_hdr + TRX_UNDO_NEXT_LOG, 0);
589
 
        mach_write_to_2(log_hdr + TRX_UNDO_PREV_LOG, prev_log);
590
 
 
591
 
        /* Write the log record about the header creation */
592
 
        trx_undo_header_create_log(undo_page, trx_id, mtr);
593
 
 
594
 
        return(free);
595
 
}
596
 
 
597
 
#ifndef UNIV_HOTBACKUP
598
 
/********************************************************************//**
599
 
Write X/Open XA Transaction Identification (XID) to undo log header */
600
 
static
601
 
void
602
 
trx_undo_write_xid(
603
 
/*===============*/
604
 
        trx_ulogf_t*    log_hdr,/*!< in: undo log header */
605
 
        const XID*      xid,    /*!< in: X/Open XA Transaction Identification */
606
 
        mtr_t*          mtr)    /*!< in: mtr */
607
 
{
608
 
        mlog_write_ulint(log_hdr + TRX_UNDO_XA_FORMAT,
609
 
                         (ulint)xid->formatID, MLOG_4BYTES, mtr);
610
 
 
611
 
        mlog_write_ulint(log_hdr + TRX_UNDO_XA_TRID_LEN,
612
 
                         (ulint)xid->gtrid_length, MLOG_4BYTES, mtr);
613
 
 
614
 
        mlog_write_ulint(log_hdr + TRX_UNDO_XA_BQUAL_LEN,
615
 
                         (ulint)xid->bqual_length, MLOG_4BYTES, mtr);
616
 
 
617
 
        mlog_write_string(log_hdr + TRX_UNDO_XA_XID, (const byte*) xid->data,
618
 
                          XIDDATASIZE, mtr);
619
 
}
620
 
 
621
 
/********************************************************************//**
622
 
Read X/Open XA Transaction Identification (XID) from undo log header */
623
 
static
624
 
void
625
 
trx_undo_read_xid(
626
 
/*==============*/
627
 
        trx_ulogf_t*    log_hdr,/*!< in: undo log header */
628
 
        XID*            xid)    /*!< out: X/Open XA Transaction Identification */
629
 
{
630
 
        xid->formatID = (long)mach_read_from_4(log_hdr + TRX_UNDO_XA_FORMAT);
631
 
 
632
 
        xid->gtrid_length
633
 
                = (long) mach_read_from_4(log_hdr + TRX_UNDO_XA_TRID_LEN);
634
 
        xid->bqual_length
635
 
                = (long) mach_read_from_4(log_hdr + TRX_UNDO_XA_BQUAL_LEN);
636
 
 
637
 
        memcpy(xid->data, log_hdr + TRX_UNDO_XA_XID, XIDDATASIZE);
638
 
}
639
 
 
640
 
/***************************************************************//**
641
 
Adds space for the XA XID after an undo log old-style header. */
642
 
static
643
 
void
644
 
trx_undo_header_add_space_for_xid(
645
 
/*==============================*/
646
 
        page_t*         undo_page,/*!< in: undo log segment header page */
647
 
        trx_ulogf_t*    log_hdr,/*!< in: undo log header */
648
 
        mtr_t*          mtr)    /*!< in: mtr */
649
 
{
650
 
        trx_upagef_t*   page_hdr;
651
 
        ulint           free;
652
 
        ulint           new_free;
653
 
 
654
 
        page_hdr = undo_page + TRX_UNDO_PAGE_HDR;
655
 
 
656
 
        free = mach_read_from_2(page_hdr + TRX_UNDO_PAGE_FREE);
657
 
 
658
 
        /* free is now the end offset of the old style undo log header */
659
 
 
660
 
        ut_a(free == (ulint)(log_hdr - undo_page) + TRX_UNDO_LOG_OLD_HDR_SIZE);
661
 
 
662
 
        new_free = free + (TRX_UNDO_LOG_XA_HDR_SIZE
663
 
                           - TRX_UNDO_LOG_OLD_HDR_SIZE);
664
 
 
665
 
        /* Add space for a XID after the header, update the free offset
666
 
        fields on the undo log page and in the undo log header */
667
 
 
668
 
        mlog_write_ulint(page_hdr + TRX_UNDO_PAGE_START, new_free,
669
 
                         MLOG_2BYTES, mtr);
670
 
 
671
 
        mlog_write_ulint(page_hdr + TRX_UNDO_PAGE_FREE, new_free,
672
 
                         MLOG_2BYTES, mtr);
673
 
 
674
 
        mlog_write_ulint(log_hdr + TRX_UNDO_LOG_START, new_free,
675
 
                         MLOG_2BYTES, mtr);
676
 
}
677
 
 
678
 
/**********************************************************************//**
679
 
Writes the mtr log entry of an undo log header reuse. */
680
 
UNIV_INLINE
681
 
void
682
 
trx_undo_insert_header_reuse_log(
683
 
/*=============================*/
684
 
        const page_t*   undo_page,      /*!< in: undo log header page */
685
 
        trx_id_t        trx_id,         /*!< in: transaction id */
686
 
        mtr_t*          mtr)            /*!< in: mtr */
687
 
{
688
 
        mlog_write_initial_log_record(undo_page, MLOG_UNDO_HDR_REUSE, mtr);
689
 
 
690
 
        mlog_catenate_ull_compressed(mtr, trx_id);
691
 
}
692
 
#else /* !UNIV_HOTBACKUP */
693
 
# define trx_undo_insert_header_reuse_log(undo_page,trx_id,mtr) ((void) 0)
694
 
#endif /* !UNIV_HOTBACKUP */
695
 
 
696
 
/***********************************************************//**
697
 
Parses the redo log entry of an undo log page header create or reuse.
698
 
@return end of log record or NULL */
699
 
UNIV_INTERN
700
 
byte*
701
 
trx_undo_parse_page_header(
702
 
/*=======================*/
703
 
        ulint   type,   /*!< in: MLOG_UNDO_HDR_CREATE or MLOG_UNDO_HDR_REUSE */
704
 
        byte*   ptr,    /*!< in: buffer */
705
 
        byte*   end_ptr,/*!< in: buffer end */
706
 
        page_t* page,   /*!< in: page or NULL */
707
 
        mtr_t*  mtr)    /*!< in: mtr or NULL */
708
 
{
709
 
        trx_id_t        trx_id;
710
 
        /* Silence a GCC warning about possibly uninitialized variable
711
 
        when mach_ull_parse_compressed() is not inlined. */
712
 
        ut_d(trx_id = 0);
713
 
        /* Declare the variable uninitialized in Valgrind, so that the
714
 
        above initialization will not mask any bugs. */
715
 
        UNIV_MEM_INVALID(&trx_id, sizeof trx_id);
716
 
 
717
 
        ptr = mach_ull_parse_compressed(ptr, end_ptr, &trx_id);
718
 
 
719
 
        if (ptr == NULL) {
720
 
 
721
 
                return(NULL);
722
 
        }
723
 
 
724
 
        if (page) {
725
 
                if (type == MLOG_UNDO_HDR_CREATE) {
726
 
                        trx_undo_header_create(page, trx_id, mtr);
727
 
                } else {
728
 
                        ut_ad(type == MLOG_UNDO_HDR_REUSE);
729
 
                        trx_undo_insert_header_reuse(page, trx_id, mtr);
730
 
                }
731
 
        }
732
 
 
733
 
        return(ptr);
734
 
}
735
 
 
736
 
/***************************************************************//**
737
 
Initializes a cached insert undo log header page for new use. NOTE that this
738
 
function has its own log record type MLOG_UNDO_HDR_REUSE. You must NOT change
739
 
the operation of this function!
740
 
@return undo log header byte offset on page */
741
 
static
742
 
ulint
743
 
trx_undo_insert_header_reuse(
744
 
/*=========================*/
745
 
        page_t*         undo_page,      /*!< in/out: insert undo log segment
746
 
                                        header page, x-latched */
747
 
        trx_id_t        trx_id,         /*!< in: transaction id */
748
 
        mtr_t*          mtr)            /*!< in: mtr */
749
 
{
750
 
        trx_upagef_t*   page_hdr;
751
 
        trx_usegf_t*    seg_hdr;
752
 
        trx_ulogf_t*    log_hdr;
753
 
        ulint           free;
754
 
        ulint           new_free;
755
 
 
756
 
        ut_ad(mtr && undo_page);
757
 
 
758
 
        page_hdr = undo_page + TRX_UNDO_PAGE_HDR;
759
 
        seg_hdr = undo_page + TRX_UNDO_SEG_HDR;
760
 
 
761
 
        free = TRX_UNDO_SEG_HDR + TRX_UNDO_SEG_HDR_SIZE;
762
 
 
763
 
        ut_a(free + TRX_UNDO_LOG_XA_HDR_SIZE < UNIV_PAGE_SIZE - 100);
764
 
 
765
 
        log_hdr = undo_page + free;
766
 
 
767
 
        new_free = free + TRX_UNDO_LOG_OLD_HDR_SIZE;
768
 
 
769
 
        /* Insert undo data is not needed after commit: we may free all
770
 
        the space on the page */
771
 
 
772
 
        ut_a(mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR
773
 
                              + TRX_UNDO_PAGE_TYPE)
774
 
             == TRX_UNDO_INSERT);
775
 
 
776
 
        mach_write_to_2(page_hdr + TRX_UNDO_PAGE_START, new_free);
777
 
 
778
 
        mach_write_to_2(page_hdr + TRX_UNDO_PAGE_FREE, new_free);
779
 
 
780
 
        mach_write_to_2(seg_hdr + TRX_UNDO_STATE, TRX_UNDO_ACTIVE);
781
 
 
782
 
        log_hdr = undo_page + free;
783
 
 
784
 
        mach_write_to_8(log_hdr + TRX_UNDO_TRX_ID, trx_id);
785
 
        mach_write_to_2(log_hdr + TRX_UNDO_LOG_START, new_free);
786
 
 
787
 
        mach_write_to_1(log_hdr + TRX_UNDO_XID_EXISTS, FALSE);
788
 
        mach_write_to_1(log_hdr + TRX_UNDO_DICT_TRANS, FALSE);
789
 
 
790
 
        /* Write the log record MLOG_UNDO_HDR_REUSE */
791
 
        trx_undo_insert_header_reuse_log(undo_page, trx_id, mtr);
792
 
 
793
 
        return(free);
794
 
}
795
 
 
796
 
#ifndef UNIV_HOTBACKUP
797
 
/**********************************************************************//**
798
 
Writes the redo log entry of an update undo log header discard. */
799
 
UNIV_INLINE
800
 
void
801
 
trx_undo_discard_latest_log(
802
 
/*========================*/
803
 
        page_t* undo_page,      /*!< in: undo log header page */
804
 
        mtr_t*  mtr)            /*!< in: mtr */
805
 
{
806
 
        mlog_write_initial_log_record(undo_page, MLOG_UNDO_HDR_DISCARD, mtr);
807
 
}
808
 
#else /* !UNIV_HOTBACKUP */
809
 
# define trx_undo_discard_latest_log(undo_page, mtr) ((void) 0)
810
 
#endif /* !UNIV_HOTBACKUP */
811
 
 
812
 
/***********************************************************//**
813
 
Parses the redo log entry of an undo log page header discard.
814
 
@return end of log record or NULL */
815
 
UNIV_INTERN
816
 
byte*
817
 
trx_undo_parse_discard_latest(
818
 
/*==========================*/
819
 
        byte*   ptr,    /*!< in: buffer */
820
 
        byte*   end_ptr __attribute__((unused)), /*!< in: buffer end */
821
 
        page_t* page,   /*!< in: page or NULL */
822
 
        mtr_t*  mtr)    /*!< in: mtr or NULL */
823
 
{
824
 
        ut_ad(end_ptr);
825
 
 
826
 
        if (page) {
827
 
                trx_undo_discard_latest_update_undo(page, mtr);
828
 
        }
829
 
 
830
 
        return(ptr);
831
 
}
832
 
 
833
 
/**********************************************************************//**
834
 
If an update undo log can be discarded immediately, this function frees the
835
 
space, resetting the page to the proper state for caching. */
836
 
static
837
 
void
838
 
trx_undo_discard_latest_update_undo(
839
 
/*================================*/
840
 
        page_t* undo_page,      /*!< in: header page of an undo log of size 1 */
841
 
        mtr_t*  mtr)            /*!< in: mtr */
842
 
{
843
 
        trx_usegf_t*    seg_hdr;
844
 
        trx_upagef_t*   page_hdr;
845
 
        trx_ulogf_t*    log_hdr;
846
 
        trx_ulogf_t*    prev_log_hdr;
847
 
        ulint           free;
848
 
        ulint           prev_hdr_offset;
849
 
 
850
 
        seg_hdr = undo_page + TRX_UNDO_SEG_HDR;
851
 
        page_hdr = undo_page + TRX_UNDO_PAGE_HDR;
852
 
 
853
 
        free = mach_read_from_2(seg_hdr + TRX_UNDO_LAST_LOG);
854
 
        log_hdr = undo_page + free;
855
 
 
856
 
        prev_hdr_offset = mach_read_from_2(log_hdr + TRX_UNDO_PREV_LOG);
857
 
 
858
 
        if (prev_hdr_offset != 0) {
859
 
                prev_log_hdr = undo_page + prev_hdr_offset;
860
 
 
861
 
                mach_write_to_2(page_hdr + TRX_UNDO_PAGE_START,
862
 
                                mach_read_from_2(prev_log_hdr
863
 
                                                 + TRX_UNDO_LOG_START));
864
 
                mach_write_to_2(prev_log_hdr + TRX_UNDO_NEXT_LOG, 0);
865
 
        }
866
 
 
867
 
        mach_write_to_2(page_hdr + TRX_UNDO_PAGE_FREE, free);
868
 
 
869
 
        mach_write_to_2(seg_hdr + TRX_UNDO_STATE, TRX_UNDO_CACHED);
870
 
        mach_write_to_2(seg_hdr + TRX_UNDO_LAST_LOG, prev_hdr_offset);
871
 
 
872
 
        trx_undo_discard_latest_log(undo_page, mtr);
873
 
}
874
 
 
875
 
#ifndef UNIV_HOTBACKUP
876
 
/********************************************************************//**
877
 
Tries to add a page to the undo log segment where the undo log is placed.
878
 
@return page number if success, else FIL_NULL */
879
 
UNIV_INTERN
880
 
ulint
881
 
trx_undo_add_page(
882
 
/*==============*/
883
 
        trx_t*          trx,    /*!< in: transaction */
884
 
        trx_undo_t*     undo,   /*!< in: undo log memory object */
885
 
        mtr_t*          mtr)    /*!< in: mtr which does not have a latch to any
886
 
                                undo log page; the caller must have reserved
887
 
                                the rollback segment mutex */
888
 
{
889
 
        page_t*         header_page;
890
 
        page_t*         new_page;
891
 
        trx_rseg_t*     rseg;
892
 
        ulint           page_no;
893
 
        ulint           n_reserved;
894
 
        ibool           success;
895
 
 
896
 
        ut_ad(mutex_own(&(trx->undo_mutex)));
897
 
        ut_ad(!mutex_own(&kernel_mutex));
898
 
        ut_ad(mutex_own(&(trx->rseg->mutex)));
899
 
 
900
 
        rseg = trx->rseg;
901
 
 
902
 
        if (rseg->curr_size == rseg->max_size) {
903
 
 
904
 
                return(FIL_NULL);
905
 
        }
906
 
 
907
 
        header_page = trx_undo_page_get(undo->space, undo->zip_size,
908
 
                                        undo->hdr_page_no, mtr);
909
 
 
910
 
        success = fsp_reserve_free_extents(&n_reserved, undo->space, 1,
911
 
                                           FSP_UNDO, mtr);
912
 
        if (!success) {
913
 
 
914
 
                return(FIL_NULL);
915
 
        }
916
 
 
917
 
        page_no = fseg_alloc_free_page_general(header_page + TRX_UNDO_SEG_HDR
918
 
                                               + TRX_UNDO_FSEG_HEADER,
919
 
                                               undo->top_page_no + 1, FSP_UP,
920
 
                                               TRUE, mtr);
921
 
 
922
 
        fil_space_release_free_extents(undo->space, n_reserved);
923
 
 
924
 
        if (page_no == FIL_NULL) {
925
 
 
926
 
                /* No space left */
927
 
 
928
 
                return(FIL_NULL);
929
 
        }
930
 
 
931
 
        undo->last_page_no = page_no;
932
 
 
933
 
        new_page = trx_undo_page_get(undo->space, undo->zip_size,
934
 
                                     page_no, mtr);
935
 
 
936
 
        trx_undo_page_init(new_page, undo->type, mtr);
937
 
 
938
 
        flst_add_last(header_page + TRX_UNDO_SEG_HDR + TRX_UNDO_PAGE_LIST,
939
 
                      new_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_NODE, mtr);
940
 
        undo->size++;
941
 
        rseg->curr_size++;
942
 
 
943
 
        return(page_no);
944
 
}
945
 
 
946
 
/********************************************************************//**
947
 
Frees an undo log page that is not the header page.
948
 
@return last page number in remaining log */
949
 
static
950
 
ulint
951
 
trx_undo_free_page(
952
 
/*===============*/
953
 
        trx_rseg_t* rseg,       /*!< in: rollback segment */
954
 
        ibool   in_history,     /*!< in: TRUE if the undo log is in the history
955
 
                                list */
956
 
        ulint   space,          /*!< in: space */
957
 
        ulint   hdr_page_no,    /*!< in: header page number */
958
 
        ulint   page_no,        /*!< in: page number to free: must not be the
959
 
                                header page */
960
 
        mtr_t*  mtr)            /*!< in: mtr which does not have a latch to any
961
 
                                undo log page; the caller must have reserved
962
 
                                the rollback segment mutex */
963
 
{
964
 
        page_t*         header_page;
965
 
        page_t*         undo_page;
966
 
        fil_addr_t      last_addr;
967
 
        trx_rsegf_t*    rseg_header;
968
 
        ulint           hist_size;
969
 
        ulint           zip_size;
970
 
 
971
 
        ut_a(hdr_page_no != page_no);
972
 
        ut_ad(!mutex_own(&kernel_mutex));
973
 
        ut_ad(mutex_own(&(rseg->mutex)));
974
 
 
975
 
        zip_size = rseg->zip_size;
976
 
 
977
 
        undo_page = trx_undo_page_get(space, zip_size, page_no, mtr);
978
 
 
979
 
        header_page = trx_undo_page_get(space, zip_size, hdr_page_no, mtr);
980
 
 
981
 
        flst_remove(header_page + TRX_UNDO_SEG_HDR + TRX_UNDO_PAGE_LIST,
982
 
                    undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_NODE, mtr);
983
 
 
984
 
        fseg_free_page(header_page + TRX_UNDO_SEG_HDR + TRX_UNDO_FSEG_HEADER,
985
 
                       space, page_no, mtr);
986
 
 
987
 
        last_addr = flst_get_last(header_page + TRX_UNDO_SEG_HDR
988
 
                                  + TRX_UNDO_PAGE_LIST, mtr);
989
 
        rseg->curr_size--;
990
 
 
991
 
        if (in_history) {
992
 
                rseg_header = trx_rsegf_get(space, zip_size,
993
 
                                            rseg->page_no, mtr);
994
 
 
995
 
                hist_size = mtr_read_ulint(rseg_header + TRX_RSEG_HISTORY_SIZE,
996
 
                                           MLOG_4BYTES, mtr);
997
 
                ut_ad(hist_size > 0);
998
 
                mlog_write_ulint(rseg_header + TRX_RSEG_HISTORY_SIZE,
999
 
                                 hist_size - 1, MLOG_4BYTES, mtr);
1000
 
        }
1001
 
 
1002
 
        return(last_addr.page);
1003
 
}
1004
 
 
1005
 
/********************************************************************//**
1006
 
Frees an undo log page when there is also the memory object for the undo
1007
 
log. */
1008
 
static
1009
 
void
1010
 
trx_undo_free_page_in_rollback(
1011
 
/*===========================*/
1012
 
        trx_t*          trx __attribute__((unused)), /*!< in: transaction */
1013
 
        trx_undo_t*     undo,   /*!< in: undo log memory copy */
1014
 
        ulint           page_no,/*!< in: page number to free: must not be the
1015
 
                                header page */
1016
 
        mtr_t*          mtr)    /*!< in: mtr which does not have a latch to any
1017
 
                                undo log page; the caller must have reserved
1018
 
                                the rollback segment mutex */
1019
 
{
1020
 
        ulint   last_page_no;
1021
 
 
1022
 
        ut_ad(undo->hdr_page_no != page_no);
1023
 
        ut_ad(mutex_own(&(trx->undo_mutex)));
1024
 
 
1025
 
        last_page_no = trx_undo_free_page(undo->rseg, FALSE, undo->space,
1026
 
                                          undo->hdr_page_no, page_no, mtr);
1027
 
 
1028
 
        undo->last_page_no = last_page_no;
1029
 
        undo->size--;
1030
 
}
1031
 
 
1032
 
/********************************************************************//**
1033
 
Empties an undo log header page of undo records for that undo log. Other
1034
 
undo logs may still have records on that page, if it is an update undo log. */
1035
 
static
1036
 
void
1037
 
trx_undo_empty_header_page(
1038
 
/*=======================*/
1039
 
        ulint   space,          /*!< in: space */
1040
 
        ulint   zip_size,       /*!< in: compressed page size in bytes
1041
 
                                or 0 for uncompressed pages */
1042
 
        ulint   hdr_page_no,    /*!< in: header page number */
1043
 
        ulint   hdr_offset,     /*!< in: header offset */
1044
 
        mtr_t*  mtr)            /*!< in: mtr */
1045
 
{
1046
 
        page_t*         header_page;
1047
 
        trx_ulogf_t*    log_hdr;
1048
 
        ulint           end;
1049
 
 
1050
 
        header_page = trx_undo_page_get(space, zip_size, hdr_page_no, mtr);
1051
 
 
1052
 
        log_hdr = header_page + hdr_offset;
1053
 
 
1054
 
        end = trx_undo_page_get_end(header_page, hdr_page_no, hdr_offset);
1055
 
 
1056
 
        mlog_write_ulint(log_hdr + TRX_UNDO_LOG_START, end, MLOG_2BYTES, mtr);
1057
 
}
1058
 
 
1059
 
/***********************************************************************//**
1060
 
Truncates an undo log from the end. This function is used during a rollback
1061
 
to free space from an undo log. */
1062
 
UNIV_INTERN
1063
 
void
1064
 
trx_undo_truncate_end(
1065
 
/*==================*/
1066
 
        trx_t*          trx,    /*!< in: transaction whose undo log it is */
1067
 
        trx_undo_t*     undo,   /*!< in: undo log */
1068
 
        undo_no_t       limit)  /*!< in: all undo records with undo number
1069
 
                                >= this value should be truncated */
1070
 
{
1071
 
        page_t*         undo_page;
1072
 
        ulint           last_page_no;
1073
 
        trx_undo_rec_t* rec;
1074
 
        trx_undo_rec_t* trunc_here;
1075
 
        mtr_t           mtr;
1076
 
 
1077
 
        ut_ad(mutex_own(&(trx->undo_mutex)));
1078
 
        ut_ad(mutex_own(&(trx->rseg->mutex)));
1079
 
 
1080
 
        for (;;) {
1081
 
                mtr_start(&mtr);
1082
 
 
1083
 
                trunc_here = NULL;
1084
 
 
1085
 
                last_page_no = undo->last_page_no;
1086
 
 
1087
 
                undo_page = trx_undo_page_get(undo->space, undo->zip_size,
1088
 
                                              last_page_no, &mtr);
1089
 
 
1090
 
                rec = trx_undo_page_get_last_rec(undo_page, undo->hdr_page_no,
1091
 
                                                 undo->hdr_offset);
1092
 
                for (;;) {
1093
 
                        if (rec == NULL) {
1094
 
                                if (last_page_no == undo->hdr_page_no) {
1095
 
 
1096
 
                                        goto function_exit;
1097
 
                                }
1098
 
 
1099
 
                                trx_undo_free_page_in_rollback(
1100
 
                                        trx, undo, last_page_no, &mtr);
1101
 
                                break;
1102
 
                        }
1103
 
 
1104
 
                        if (trx_undo_rec_get_undo_no(rec) >= limit) {
1105
 
                                /* Truncate at least this record off, maybe
1106
 
                                more */
1107
 
                                trunc_here = rec;
1108
 
                        } else {
1109
 
                                goto function_exit;
1110
 
                        }
1111
 
 
1112
 
                        rec = trx_undo_page_get_prev_rec(rec,
1113
 
                                                         undo->hdr_page_no,
1114
 
                                                         undo->hdr_offset);
1115
 
                }
1116
 
 
1117
 
                mtr_commit(&mtr);
1118
 
        }
1119
 
 
1120
 
function_exit:
1121
 
        if (trunc_here) {
1122
 
                mlog_write_ulint(undo_page + TRX_UNDO_PAGE_HDR
1123
 
                                 + TRX_UNDO_PAGE_FREE,
1124
 
                                 trunc_here - undo_page, MLOG_2BYTES, &mtr);
1125
 
        }
1126
 
 
1127
 
        mtr_commit(&mtr);
1128
 
}
1129
 
 
1130
 
/***********************************************************************//**
1131
 
Truncates an undo log from the start. This function is used during a purge
1132
 
operation. */
1133
 
UNIV_INTERN
1134
 
void
1135
 
trx_undo_truncate_start(
1136
 
/*====================*/
1137
 
        trx_rseg_t*     rseg,           /*!< in: rollback segment */
1138
 
        ulint           space,          /*!< in: space id of the log */
1139
 
        ulint           hdr_page_no,    /*!< in: header page number */
1140
 
        ulint           hdr_offset,     /*!< in: header offset on the page */
1141
 
        undo_no_t       limit)          /*!< in: all undo pages with
1142
 
                                        undo numbers < this value
1143
 
                                        should be truncated; NOTE that
1144
 
                                        the function only frees whole
1145
 
                                        pages; the header page is not
1146
 
                                        freed, but emptied, if all the
1147
 
                                        records there are < limit */
1148
 
{
1149
 
        page_t*         undo_page;
1150
 
        trx_undo_rec_t* rec;
1151
 
        trx_undo_rec_t* last_rec;
1152
 
        ulint           page_no;
1153
 
        mtr_t           mtr;
1154
 
 
1155
 
        ut_ad(mutex_own(&(rseg->mutex)));
1156
 
 
1157
 
        if (!limit) {
1158
 
 
1159
 
                return;
1160
 
        }
1161
 
loop:
1162
 
        mtr_start(&mtr);
1163
 
 
1164
 
        rec = trx_undo_get_first_rec(space, rseg->zip_size,
1165
 
                                     hdr_page_no, hdr_offset,
1166
 
                                     RW_X_LATCH, &mtr);
1167
 
        if (rec == NULL) {
1168
 
                /* Already empty */
1169
 
 
1170
 
                mtr_commit(&mtr);
1171
 
 
1172
 
                return;
1173
 
        }
1174
 
 
1175
 
        undo_page = page_align(rec);
1176
 
 
1177
 
        last_rec = trx_undo_page_get_last_rec(undo_page, hdr_page_no,
1178
 
                                              hdr_offset);
1179
 
        if (trx_undo_rec_get_undo_no(last_rec) >= limit) {
1180
 
 
1181
 
                mtr_commit(&mtr);
1182
 
 
1183
 
                return;
1184
 
        }
1185
 
 
1186
 
        page_no = page_get_page_no(undo_page);
1187
 
 
1188
 
        if (page_no == hdr_page_no) {
1189
 
                trx_undo_empty_header_page(space, rseg->zip_size,
1190
 
                                           hdr_page_no, hdr_offset,
1191
 
                                           &mtr);
1192
 
        } else {
1193
 
                trx_undo_free_page(rseg, TRUE, space, hdr_page_no,
1194
 
                                   page_no, &mtr);
1195
 
        }
1196
 
 
1197
 
        mtr_commit(&mtr);
1198
 
 
1199
 
        goto loop;
1200
 
}
1201
 
 
1202
 
/**********************************************************************//**
1203
 
Frees an undo log segment which is not in the history list. */
1204
 
static
1205
 
void
1206
 
trx_undo_seg_free(
1207
 
/*==============*/
1208
 
        trx_undo_t*     undo)   /*!< in: undo log */
1209
 
{
1210
 
        trx_rseg_t*     rseg;
1211
 
        fseg_header_t*  file_seg;
1212
 
        trx_rsegf_t*    rseg_header;
1213
 
        trx_usegf_t*    seg_header;
1214
 
        ibool           finished;
1215
 
        mtr_t           mtr;
1216
 
 
1217
 
        rseg = undo->rseg;
1218
 
 
1219
 
        do {
1220
 
 
1221
 
                mtr_start(&mtr);
1222
 
 
1223
 
                ut_ad(!mutex_own(&kernel_mutex));
1224
 
 
1225
 
                mutex_enter(&(rseg->mutex));
1226
 
 
1227
 
                seg_header = trx_undo_page_get(undo->space, undo->zip_size,
1228
 
                                               undo->hdr_page_no,
1229
 
                                               &mtr) + TRX_UNDO_SEG_HDR;
1230
 
 
1231
 
                file_seg = seg_header + TRX_UNDO_FSEG_HEADER;
1232
 
 
1233
 
                finished = fseg_free_step(file_seg, &mtr);
1234
 
 
1235
 
                if (finished) {
1236
 
                        /* Update the rseg header */
1237
 
                        rseg_header = trx_rsegf_get(
1238
 
                                rseg->space, rseg->zip_size, rseg->page_no,
1239
 
                                &mtr);
1240
 
                        trx_rsegf_set_nth_undo(rseg_header, undo->id, FIL_NULL,
1241
 
                                               &mtr);
1242
 
                }
1243
 
 
1244
 
                mutex_exit(&(rseg->mutex));
1245
 
                mtr_commit(&mtr);
1246
 
        } while (!finished);
1247
 
}
1248
 
 
1249
 
/*========== UNDO LOG MEMORY COPY INITIALIZATION =====================*/
1250
 
 
1251
 
/********************************************************************//**
1252
 
Creates and initializes an undo log memory object according to the values
1253
 
in the header in file, when the database is started. The memory object is
1254
 
inserted in the appropriate list of rseg.
1255
 
@return own: the undo log memory object */
1256
 
static
1257
 
trx_undo_t*
1258
 
trx_undo_mem_create_at_db_start(
1259
 
/*============================*/
1260
 
        trx_rseg_t*     rseg,   /*!< in: rollback segment memory object */
1261
 
        ulint           id,     /*!< in: slot index within rseg */
1262
 
        ulint           page_no,/*!< in: undo log segment page number */
1263
 
        mtr_t*          mtr)    /*!< in: mtr */
1264
 
{
1265
 
        page_t*         undo_page;
1266
 
        trx_upagef_t*   page_header;
1267
 
        trx_usegf_t*    seg_header;
1268
 
        trx_ulogf_t*    undo_header;
1269
 
        trx_undo_t*     undo;
1270
 
        ulint           type;
1271
 
        ulint           state;
1272
 
        trx_id_t        trx_id;
1273
 
        ulint           offset;
1274
 
        fil_addr_t      last_addr;
1275
 
        page_t*         last_page;
1276
 
        trx_undo_rec_t* rec;
1277
 
        XID             xid;
1278
 
        ibool           xid_exists = FALSE;
1279
 
 
1280
 
        if (id >= TRX_RSEG_N_SLOTS) {
1281
 
                fprintf(stderr,
1282
 
                        "InnoDB: Error: undo->id is %lu\n", (ulong) id);
1283
 
                ut_error;
1284
 
        }
1285
 
 
1286
 
        undo_page = trx_undo_page_get(rseg->space, rseg->zip_size,
1287
 
                                      page_no, mtr);
1288
 
 
1289
 
        page_header = undo_page + TRX_UNDO_PAGE_HDR;
1290
 
 
1291
 
        type = mtr_read_ulint(page_header + TRX_UNDO_PAGE_TYPE, MLOG_2BYTES,
1292
 
                              mtr);
1293
 
        seg_header = undo_page + TRX_UNDO_SEG_HDR;
1294
 
 
1295
 
        state = mach_read_from_2(seg_header + TRX_UNDO_STATE);
1296
 
 
1297
 
        offset = mach_read_from_2(seg_header + TRX_UNDO_LAST_LOG);
1298
 
 
1299
 
        undo_header = undo_page + offset;
1300
 
 
1301
 
        trx_id = mach_read_from_8(undo_header + TRX_UNDO_TRX_ID);
1302
 
 
1303
 
        xid_exists = mtr_read_ulint(undo_header + TRX_UNDO_XID_EXISTS,
1304
 
                                    MLOG_1BYTE, mtr);
1305
 
 
1306
 
        /* Read X/Open XA transaction identification if it exists, or
1307
 
        set it to NULL. */
1308
 
 
1309
 
        memset(&xid, 0, sizeof(xid));
1310
 
        xid.formatID = -1;
1311
 
 
1312
 
        if (xid_exists == TRUE) {
1313
 
                trx_undo_read_xid(undo_header, &xid);
1314
 
        }
1315
 
 
1316
 
        mutex_enter(&(rseg->mutex));
1317
 
 
1318
 
        undo = trx_undo_mem_create(rseg, id, type, trx_id, &xid,
1319
 
                                   page_no, offset);
1320
 
        mutex_exit(&(rseg->mutex));
1321
 
 
1322
 
        undo->dict_operation =  mtr_read_ulint(
1323
 
                undo_header + TRX_UNDO_DICT_TRANS, MLOG_1BYTE, mtr);
1324
 
 
1325
 
        undo->table_id = mach_read_from_8(undo_header + TRX_UNDO_TABLE_ID);
1326
 
        undo->state = state;
1327
 
        undo->size = flst_get_len(seg_header + TRX_UNDO_PAGE_LIST, mtr);
1328
 
 
1329
 
        /* If the log segment is being freed, the page list is inconsistent! */
1330
 
        if (state == TRX_UNDO_TO_FREE) {
1331
 
 
1332
 
                goto add_to_list;
1333
 
        }
1334
 
 
1335
 
        last_addr = flst_get_last(seg_header + TRX_UNDO_PAGE_LIST, mtr);
1336
 
 
1337
 
        undo->last_page_no = last_addr.page;
1338
 
        undo->top_page_no = last_addr.page;
1339
 
 
1340
 
        last_page = trx_undo_page_get(rseg->space, rseg->zip_size,
1341
 
                                      undo->last_page_no, mtr);
1342
 
 
1343
 
        rec = trx_undo_page_get_last_rec(last_page, page_no, offset);
1344
 
 
1345
 
        if (rec == NULL) {
1346
 
                undo->empty = TRUE;
1347
 
        } else {
1348
 
                undo->empty = FALSE;
1349
 
                undo->top_offset = rec - last_page;
1350
 
                undo->top_undo_no = trx_undo_rec_get_undo_no(rec);
1351
 
        }
1352
 
add_to_list:
1353
 
        if (type == TRX_UNDO_INSERT) {
1354
 
                if (state != TRX_UNDO_CACHED) {
1355
 
                        UT_LIST_ADD_LAST(undo_list, rseg->insert_undo_list,
1356
 
                                         undo);
1357
 
                } else {
1358
 
                        UT_LIST_ADD_LAST(undo_list, rseg->insert_undo_cached,
1359
 
                                         undo);
1360
 
                }
1361
 
        } else {
1362
 
                ut_ad(type == TRX_UNDO_UPDATE);
1363
 
                if (state != TRX_UNDO_CACHED) {
1364
 
                        UT_LIST_ADD_LAST(undo_list, rseg->update_undo_list,
1365
 
                                         undo);
1366
 
                } else {
1367
 
                        UT_LIST_ADD_LAST(undo_list, rseg->update_undo_cached,
1368
 
                                         undo);
1369
 
                }
1370
 
        }
1371
 
 
1372
 
        return(undo);
1373
 
}
1374
 
 
1375
 
/********************************************************************//**
1376
 
Initializes the undo log lists for a rollback segment memory copy. This
1377
 
function is only called when the database is started or a new rollback
1378
 
segment is created.
1379
 
@return the combined size of undo log segments in pages */
1380
 
UNIV_INTERN
1381
 
ulint
1382
 
trx_undo_lists_init(
1383
 
/*================*/
1384
 
        trx_rseg_t*     rseg)   /*!< in: rollback segment memory object */
1385
 
{
1386
 
        ulint           page_no;
1387
 
        trx_undo_t*     undo;
1388
 
        ulint           size    = 0;
1389
 
        trx_rsegf_t*    rseg_header;
1390
 
        ulint           i;
1391
 
        mtr_t           mtr;
1392
 
 
1393
 
        UT_LIST_INIT(rseg->update_undo_list);
1394
 
        UT_LIST_INIT(rseg->update_undo_cached);
1395
 
        UT_LIST_INIT(rseg->insert_undo_list);
1396
 
        UT_LIST_INIT(rseg->insert_undo_cached);
1397
 
 
1398
 
        mtr_start(&mtr);
1399
 
 
1400
 
        rseg_header = trx_rsegf_get_new(rseg->space, rseg->zip_size,
1401
 
                                        rseg->page_no, &mtr);
1402
 
 
1403
 
        for (i = 0; i < TRX_RSEG_N_SLOTS; i++) {
1404
 
                page_no = trx_rsegf_get_nth_undo(rseg_header, i, &mtr);
1405
 
 
1406
 
                /* In forced recovery: try to avoid operations which look
1407
 
                at database pages; undo logs are rapidly changing data, and
1408
 
                the probability that they are in an inconsistent state is
1409
 
                high */
1410
 
 
1411
 
                if (page_no != FIL_NULL
1412
 
                    && srv_force_recovery < SRV_FORCE_NO_UNDO_LOG_SCAN) {
1413
 
 
1414
 
                        undo = trx_undo_mem_create_at_db_start(rseg, i,
1415
 
                                                               page_no, &mtr);
1416
 
                        size += undo->size;
1417
 
 
1418
 
                        mtr_commit(&mtr);
1419
 
 
1420
 
                        mtr_start(&mtr);
1421
 
 
1422
 
                        rseg_header = trx_rsegf_get(
1423
 
                                rseg->space, rseg->zip_size, rseg->page_no,
1424
 
                                &mtr);
1425
 
                }
1426
 
        }
1427
 
 
1428
 
        mtr_commit(&mtr);
1429
 
 
1430
 
        return(size);
1431
 
}
1432
 
 
1433
 
/********************************************************************//**
1434
 
Creates and initializes an undo log memory object.
1435
 
@return own: the undo log memory object */
1436
 
static
1437
 
trx_undo_t*
1438
 
trx_undo_mem_create(
1439
 
/*================*/
1440
 
        trx_rseg_t*     rseg,   /*!< in: rollback segment memory object */
1441
 
        ulint           id,     /*!< in: slot index within rseg */
1442
 
        ulint           type,   /*!< in: type of the log: TRX_UNDO_INSERT or
1443
 
                                TRX_UNDO_UPDATE */
1444
 
        trx_id_t        trx_id, /*!< in: id of the trx for which the undo log
1445
 
                                is created */
1446
 
        const XID*      xid,    /*!< in: X/Open transaction identification */
1447
 
        ulint           page_no,/*!< in: undo log header page number */
1448
 
        ulint           offset) /*!< in: undo log header byte offset on page */
1449
 
{
1450
 
        trx_undo_t*     undo;
1451
 
 
1452
 
        ut_ad(mutex_own(&(rseg->mutex)));
1453
 
 
1454
 
        if (id >= TRX_RSEG_N_SLOTS) {
1455
 
                fprintf(stderr,
1456
 
                        "InnoDB: Error: undo->id is %lu\n", (ulong) id);
1457
 
                ut_error;
1458
 
        }
1459
 
 
1460
 
        undo = mem_alloc(sizeof(trx_undo_t));
1461
 
 
1462
 
        if (undo == NULL) {
1463
 
 
1464
 
                return NULL;
1465
 
        }
1466
 
 
1467
 
        undo->id = id;
1468
 
        undo->type = type;
1469
 
        undo->state = TRX_UNDO_ACTIVE;
1470
 
        undo->del_marks = FALSE;
1471
 
        undo->trx_id = trx_id;
1472
 
        undo->xid = *xid;
1473
 
 
1474
 
        undo->dict_operation = FALSE;
1475
 
 
1476
 
        undo->rseg = rseg;
1477
 
 
1478
 
        undo->space = rseg->space;
1479
 
        undo->zip_size = rseg->zip_size;
1480
 
        undo->hdr_page_no = page_no;
1481
 
        undo->hdr_offset = offset;
1482
 
        undo->last_page_no = page_no;
1483
 
        undo->size = 1;
1484
 
 
1485
 
        undo->empty = TRUE;
1486
 
        undo->top_page_no = page_no;
1487
 
        undo->guess_block = NULL;
1488
 
 
1489
 
        return(undo);
1490
 
}
1491
 
 
1492
 
/********************************************************************//**
1493
 
Initializes a cached undo log object for new use. */
1494
 
static
1495
 
void
1496
 
trx_undo_mem_init_for_reuse(
1497
 
/*========================*/
1498
 
        trx_undo_t*     undo,   /*!< in: undo log to init */
1499
 
        trx_id_t        trx_id, /*!< in: id of the trx for which the undo log
1500
 
                                is created */
1501
 
        const XID*      xid,    /*!< in: X/Open XA transaction identification*/
1502
 
        ulint           offset) /*!< in: undo log header byte offset on page */
1503
 
{
1504
 
        ut_ad(mutex_own(&((undo->rseg)->mutex)));
1505
 
 
1506
 
        if (UNIV_UNLIKELY(undo->id >= TRX_RSEG_N_SLOTS)) {
1507
 
                fprintf(stderr, "InnoDB: Error: undo->id is %lu\n",
1508
 
                        (ulong) undo->id);
1509
 
 
1510
 
                mem_analyze_corruption(undo);
1511
 
                ut_error;
1512
 
        }
1513
 
 
1514
 
        undo->state = TRX_UNDO_ACTIVE;
1515
 
        undo->del_marks = FALSE;
1516
 
        undo->trx_id = trx_id;
1517
 
        undo->xid = *xid;
1518
 
 
1519
 
        undo->dict_operation = FALSE;
1520
 
 
1521
 
        undo->hdr_offset = offset;
1522
 
        undo->empty = TRUE;
1523
 
}
1524
 
 
1525
 
/********************************************************************//**
1526
 
Frees an undo log memory copy. */
1527
 
UNIV_INTERN
1528
 
void
1529
 
trx_undo_mem_free(
1530
 
/*==============*/
1531
 
        trx_undo_t*     undo)   /*!< in: the undo object to be freed */
1532
 
{
1533
 
        if (undo->id >= TRX_RSEG_N_SLOTS) {
1534
 
                fprintf(stderr,
1535
 
                        "InnoDB: Error: undo->id is %lu\n", (ulong) undo->id);
1536
 
                ut_error;
1537
 
        }
1538
 
 
1539
 
        mem_free(undo);
1540
 
}
1541
 
 
1542
 
/**********************************************************************//**
1543
 
Creates a new undo log.
1544
 
@return DB_SUCCESS if successful in creating the new undo lob object,
1545
 
possible error codes are: DB_TOO_MANY_CONCURRENT_TRXS
1546
 
DB_OUT_OF_FILE_SPACE DB_OUT_OF_MEMORY */
1547
 
static
1548
 
ulint
1549
 
trx_undo_create(
1550
 
/*============*/
1551
 
        trx_t*          trx,    /*!< in: transaction */
1552
 
        trx_rseg_t*     rseg,   /*!< in: rollback segment memory copy */
1553
 
        ulint           type,   /*!< in: type of the log: TRX_UNDO_INSERT or
1554
 
                                TRX_UNDO_UPDATE */
1555
 
        trx_id_t        trx_id, /*!< in: id of the trx for which the undo log
1556
 
                                is created */
1557
 
        const XID*      xid,    /*!< in: X/Open transaction identification*/
1558
 
        trx_undo_t**    undo,   /*!< out: the new undo log object, undefined
1559
 
                                 * if did not succeed */
1560
 
        mtr_t*          mtr)    /*!< in: mtr */
1561
 
{
1562
 
        trx_rsegf_t*    rseg_header;
1563
 
        ulint           page_no;
1564
 
        ulint           offset;
1565
 
        ulint           id;
1566
 
        page_t*         undo_page;
1567
 
        ulint           err;
1568
 
 
1569
 
        ut_ad(mutex_own(&(rseg->mutex)));
1570
 
 
1571
 
        if (rseg->curr_size == rseg->max_size) {
1572
 
 
1573
 
                return(DB_OUT_OF_FILE_SPACE);
1574
 
        }
1575
 
 
1576
 
        rseg->curr_size++;
1577
 
 
1578
 
        rseg_header = trx_rsegf_get(rseg->space, rseg->zip_size, rseg->page_no,
1579
 
                                    mtr);
1580
 
 
1581
 
        err = trx_undo_seg_create(rseg, rseg_header, type, &id,
1582
 
                                  &undo_page, mtr);
1583
 
 
1584
 
        if (err != DB_SUCCESS) {
1585
 
                /* Did not succeed */
1586
 
 
1587
 
                rseg->curr_size--;
1588
 
 
1589
 
                return(err);
1590
 
        }
1591
 
 
1592
 
        page_no = page_get_page_no(undo_page);
1593
 
 
1594
 
        offset = trx_undo_header_create(undo_page, trx_id, mtr);
1595
 
 
1596
 
        if (trx->support_xa) {
1597
 
                trx_undo_header_add_space_for_xid(undo_page,
1598
 
                                                  undo_page + offset, mtr);
1599
 
        }
1600
 
 
1601
 
        *undo = trx_undo_mem_create(rseg, id, type, trx_id, xid,
1602
 
                                   page_no, offset);
1603
 
        if (*undo == NULL) {
1604
 
 
1605
 
                err = DB_OUT_OF_MEMORY;
1606
 
        }
1607
 
 
1608
 
        return(err);
1609
 
}
1610
 
 
1611
 
/*================ UNDO LOG ASSIGNMENT AND CLEANUP =====================*/
1612
 
 
1613
 
/********************************************************************//**
1614
 
Reuses a cached undo log.
1615
 
@return the undo log memory object, NULL if none cached */
1616
 
static
1617
 
trx_undo_t*
1618
 
trx_undo_reuse_cached(
1619
 
/*==================*/
1620
 
        trx_t*          trx,    /*!< in: transaction */
1621
 
        trx_rseg_t*     rseg,   /*!< in: rollback segment memory object */
1622
 
        ulint           type,   /*!< in: type of the log: TRX_UNDO_INSERT or
1623
 
                                TRX_UNDO_UPDATE */
1624
 
        trx_id_t        trx_id, /*!< in: id of the trx for which the undo log
1625
 
                                is used */
1626
 
        const XID*      xid,    /*!< in: X/Open XA transaction identification */
1627
 
        mtr_t*          mtr)    /*!< in: mtr */
1628
 
{
1629
 
        trx_undo_t*     undo;
1630
 
        page_t*         undo_page;
1631
 
        ulint           offset;
1632
 
 
1633
 
        ut_ad(mutex_own(&(rseg->mutex)));
1634
 
 
1635
 
        if (type == TRX_UNDO_INSERT) {
1636
 
 
1637
 
                undo = UT_LIST_GET_FIRST(rseg->insert_undo_cached);
1638
 
                if (undo == NULL) {
1639
 
 
1640
 
                        return(NULL);
1641
 
                }
1642
 
 
1643
 
                UT_LIST_REMOVE(undo_list, rseg->insert_undo_cached, undo);
1644
 
        } else {
1645
 
                ut_ad(type == TRX_UNDO_UPDATE);
1646
 
 
1647
 
                undo = UT_LIST_GET_FIRST(rseg->update_undo_cached);
1648
 
                if (undo == NULL) {
1649
 
 
1650
 
                        return(NULL);
1651
 
                }
1652
 
 
1653
 
                UT_LIST_REMOVE(undo_list, rseg->update_undo_cached, undo);
1654
 
        }
1655
 
 
1656
 
        ut_ad(undo->size == 1);
1657
 
 
1658
 
        if (undo->id >= TRX_RSEG_N_SLOTS) {
1659
 
                fprintf(stderr, "InnoDB: Error: undo->id is %lu\n",
1660
 
                        (ulong) undo->id);
1661
 
                mem_analyze_corruption(undo);
1662
 
                ut_error;
1663
 
        }
1664
 
 
1665
 
        undo_page = trx_undo_page_get(undo->space, undo->zip_size,
1666
 
                                      undo->hdr_page_no, mtr);
1667
 
 
1668
 
        if (type == TRX_UNDO_INSERT) {
1669
 
                offset = trx_undo_insert_header_reuse(undo_page, trx_id, mtr);
1670
 
 
1671
 
                if (trx->support_xa) {
1672
 
                        trx_undo_header_add_space_for_xid(
1673
 
                                undo_page, undo_page + offset, mtr);
1674
 
                }
1675
 
        } else {
1676
 
                ut_a(mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR
1677
 
                                      + TRX_UNDO_PAGE_TYPE)
1678
 
                     == TRX_UNDO_UPDATE);
1679
 
 
1680
 
                offset = trx_undo_header_create(undo_page, trx_id, mtr);
1681
 
 
1682
 
                if (trx->support_xa) {
1683
 
                        trx_undo_header_add_space_for_xid(
1684
 
                                undo_page, undo_page + offset, mtr);
1685
 
                }
1686
 
        }
1687
 
 
1688
 
        trx_undo_mem_init_for_reuse(undo, trx_id, xid, offset);
1689
 
 
1690
 
        return(undo);
1691
 
}
1692
 
 
1693
 
/**********************************************************************//**
1694
 
Marks an undo log header as a header of a data dictionary operation
1695
 
transaction. */
1696
 
static
1697
 
void
1698
 
trx_undo_mark_as_dict_operation(
1699
 
/*============================*/
1700
 
        trx_t*          trx,    /*!< in: dict op transaction */
1701
 
        trx_undo_t*     undo,   /*!< in: assigned undo log */
1702
 
        mtr_t*          mtr)    /*!< in: mtr */
1703
 
{
1704
 
        page_t* hdr_page;
1705
 
 
1706
 
        hdr_page = trx_undo_page_get(undo->space, undo->zip_size,
1707
 
                                     undo->hdr_page_no, mtr);
1708
 
 
1709
 
        switch (trx_get_dict_operation(trx)) {
1710
 
        case TRX_DICT_OP_NONE:
1711
 
                ut_error;
1712
 
        case TRX_DICT_OP_INDEX:
1713
 
                /* Do not discard the table on recovery. */
1714
 
                undo->table_id = 0;
1715
 
                break;
1716
 
        case TRX_DICT_OP_TABLE:
1717
 
                undo->table_id = trx->table_id;
1718
 
                break;
1719
 
        }
1720
 
 
1721
 
        mlog_write_ulint(hdr_page + undo->hdr_offset
1722
 
                         + TRX_UNDO_DICT_TRANS,
1723
 
                         TRUE, MLOG_1BYTE, mtr);
1724
 
 
1725
 
        mlog_write_ull(hdr_page + undo->hdr_offset + TRX_UNDO_TABLE_ID,
1726
 
                       undo->table_id, mtr);
1727
 
 
1728
 
        undo->dict_operation = TRUE;
1729
 
}
1730
 
 
1731
 
/**********************************************************************//**
1732
 
Assigns an undo log for a transaction. A new undo log is created or a cached
1733
 
undo log reused.
1734
 
@return DB_SUCCESS if undo log assign successful, possible error codes
1735
 
are: DB_TOO_MANY_CONCURRENT_TRXS DB_OUT_OF_FILE_SPACE
1736
 
DB_OUT_OF_MEMORY */
1737
 
UNIV_INTERN
1738
 
ulint
1739
 
trx_undo_assign_undo(
1740
 
/*=================*/
1741
 
        trx_t*          trx,    /*!< in: transaction */
1742
 
        ulint           type)   /*!< in: TRX_UNDO_INSERT or TRX_UNDO_UPDATE */
1743
 
{
1744
 
        trx_rseg_t*     rseg;
1745
 
        trx_undo_t*     undo;
1746
 
        mtr_t           mtr;
1747
 
        ulint           err = DB_SUCCESS;
1748
 
 
1749
 
        ut_ad(trx);
1750
 
        ut_ad(trx->rseg);
1751
 
 
1752
 
        rseg = trx->rseg;
1753
 
 
1754
 
        ut_ad(mutex_own(&(trx->undo_mutex)));
1755
 
 
1756
 
        mtr_start(&mtr);
1757
 
 
1758
 
        ut_ad(!mutex_own(&kernel_mutex));
1759
 
 
1760
 
        mutex_enter(&(rseg->mutex));
1761
 
 
1762
 
        undo = trx_undo_reuse_cached(trx, rseg, type, trx->id, &trx->xid,
1763
 
                                     &mtr);
1764
 
        if (undo == NULL) {
1765
 
                err = trx_undo_create(trx, rseg, type, trx->id, &trx->xid,
1766
 
                                                                &undo, &mtr);
1767
 
                if (err != DB_SUCCESS) {
1768
 
 
1769
 
                        goto func_exit;
1770
 
                }
1771
 
        }
1772
 
 
1773
 
        if (type == TRX_UNDO_INSERT) {
1774
 
                UT_LIST_ADD_FIRST(undo_list, rseg->insert_undo_list, undo);
1775
 
                ut_ad(trx->insert_undo == NULL);
1776
 
                trx->insert_undo = undo;
1777
 
        } else {
1778
 
                UT_LIST_ADD_FIRST(undo_list, rseg->update_undo_list, undo);
1779
 
                ut_ad(trx->update_undo == NULL);
1780
 
                trx->update_undo = undo;
1781
 
        }
1782
 
 
1783
 
        if (trx_get_dict_operation(trx) != TRX_DICT_OP_NONE) {
1784
 
                trx_undo_mark_as_dict_operation(trx, undo, &mtr);
1785
 
        }
1786
 
 
1787
 
func_exit:
1788
 
        mutex_exit(&(rseg->mutex));
1789
 
        mtr_commit(&mtr);
1790
 
 
1791
 
        return err;
1792
 
}
1793
 
 
1794
 
/******************************************************************//**
1795
 
Sets the state of the undo log segment at a transaction finish.
1796
 
@return undo log segment header page, x-latched */
1797
 
UNIV_INTERN
1798
 
page_t*
1799
 
trx_undo_set_state_at_finish(
1800
 
/*=========================*/
1801
 
        trx_rseg_t*     rseg,   /*!< in: rollback segment memory object */
1802
 
        trx_t*          trx __attribute__((unused)), /*!< in: transaction */
1803
 
        trx_undo_t*     undo,   /*!< in: undo log memory copy */
1804
 
        mtr_t*          mtr)    /*!< in: mtr */
1805
 
{
1806
 
        trx_usegf_t*    seg_hdr;
1807
 
        trx_upagef_t*   page_hdr;
1808
 
        page_t*         undo_page;
1809
 
        ulint           state;
1810
 
 
1811
 
        ut_ad(trx);
1812
 
        ut_ad(undo);
1813
 
        ut_ad(mtr);
1814
 
        ut_ad(mutex_own(&rseg->mutex));
1815
 
 
1816
 
        if (undo->id >= TRX_RSEG_N_SLOTS) {
1817
 
                fprintf(stderr, "InnoDB: Error: undo->id is %lu\n",
1818
 
                        (ulong) undo->id);
1819
 
                mem_analyze_corruption(undo);
1820
 
                ut_error;
1821
 
        }
1822
 
 
1823
 
        undo_page = trx_undo_page_get(undo->space, undo->zip_size,
1824
 
                                      undo->hdr_page_no, mtr);
1825
 
 
1826
 
        seg_hdr = undo_page + TRX_UNDO_SEG_HDR;
1827
 
        page_hdr = undo_page + TRX_UNDO_PAGE_HDR;
1828
 
 
1829
 
        if (undo->size == 1
1830
 
            && mach_read_from_2(page_hdr + TRX_UNDO_PAGE_FREE)
1831
 
               < TRX_UNDO_PAGE_REUSE_LIMIT) {
1832
 
 
1833
 
                /* This is a heuristic to avoid the problem of all UNDO
1834
 
                slots ending up in one of the UNDO lists. Previously if
1835
 
                the server crashed with all the slots in one of the lists,
1836
 
                transactions that required the slots of a different type
1837
 
                would fail for lack of slots. */
1838
 
 
1839
 
                if (UT_LIST_GET_LEN(rseg->update_undo_list) < 500
1840
 
                    && UT_LIST_GET_LEN(rseg->insert_undo_list) < 500) {
1841
 
 
1842
 
                        state = TRX_UNDO_CACHED;
1843
 
                } else {
1844
 
                        state = TRX_UNDO_TO_FREE;
1845
 
                }
1846
 
 
1847
 
        } else if (undo->type == TRX_UNDO_INSERT) {
1848
 
 
1849
 
                state = TRX_UNDO_TO_FREE;
1850
 
        } else {
1851
 
                state = TRX_UNDO_TO_PURGE;
1852
 
        }
1853
 
 
1854
 
        undo->state = state;
1855
 
 
1856
 
        mlog_write_ulint(seg_hdr + TRX_UNDO_STATE, state, MLOG_2BYTES, mtr);
1857
 
 
1858
 
        return(undo_page);
1859
 
}
1860
 
 
1861
 
/******************************************************************//**
1862
 
Sets the state of the undo log segment at a transaction prepare.
1863
 
@return undo log segment header page, x-latched */
1864
 
UNIV_INTERN
1865
 
page_t*
1866
 
trx_undo_set_state_at_prepare(
1867
 
/*==========================*/
1868
 
        trx_t*          trx,    /*!< in: transaction */
1869
 
        trx_undo_t*     undo,   /*!< in: undo log memory copy */
1870
 
        mtr_t*          mtr)    /*!< in: mtr */
1871
 
{
1872
 
        trx_usegf_t*    seg_hdr;
1873
 
        trx_ulogf_t*    undo_header;
1874
 
        page_t*         undo_page;
1875
 
        ulint           offset;
1876
 
 
1877
 
        ut_ad(trx && undo && mtr);
1878
 
 
1879
 
        if (undo->id >= TRX_RSEG_N_SLOTS) {
1880
 
                fprintf(stderr, "InnoDB: Error: undo->id is %lu\n",
1881
 
                        (ulong) undo->id);
1882
 
                mem_analyze_corruption(undo);
1883
 
                ut_error;
1884
 
        }
1885
 
 
1886
 
        undo_page = trx_undo_page_get(undo->space, undo->zip_size,
1887
 
                                      undo->hdr_page_no, mtr);
1888
 
 
1889
 
        seg_hdr = undo_page + TRX_UNDO_SEG_HDR;
1890
 
 
1891
 
        /*------------------------------*/
1892
 
        undo->state = TRX_UNDO_PREPARED;
1893
 
        undo->xid   = trx->xid;
1894
 
        /*------------------------------*/
1895
 
 
1896
 
        mlog_write_ulint(seg_hdr + TRX_UNDO_STATE, undo->state,
1897
 
                         MLOG_2BYTES, mtr);
1898
 
 
1899
 
        offset = mach_read_from_2(seg_hdr + TRX_UNDO_LAST_LOG);
1900
 
        undo_header = undo_page + offset;
1901
 
 
1902
 
        mlog_write_ulint(undo_header + TRX_UNDO_XID_EXISTS,
1903
 
                         TRUE, MLOG_1BYTE, mtr);
1904
 
 
1905
 
        trx_undo_write_xid(undo_header, &undo->xid, mtr);
1906
 
 
1907
 
        return(undo_page);
1908
 
}
1909
 
 
1910
 
/**********************************************************************//**
1911
 
Adds the update undo log header as the first in the history list, and
1912
 
frees the memory object, or puts it to the list of cached update undo log
1913
 
segments. */
1914
 
UNIV_INTERN
1915
 
void
1916
 
trx_undo_update_cleanup(
1917
 
/*====================*/
1918
 
        trx_t*  trx,            /*!< in: trx owning the update undo log */
1919
 
        page_t* undo_page,      /*!< in: update undo log header page,
1920
 
                                x-latched */
1921
 
        mtr_t*  mtr)            /*!< in: mtr */
1922
 
{
1923
 
        trx_rseg_t*     rseg;
1924
 
        trx_undo_t*     undo;
1925
 
 
1926
 
        undo = trx->update_undo;
1927
 
        rseg = trx->rseg;
1928
 
 
1929
 
        ut_ad(mutex_own(&(rseg->mutex)));
1930
 
 
1931
 
        trx_purge_add_update_undo_to_history(trx, undo_page, mtr);
1932
 
 
1933
 
        UT_LIST_REMOVE(undo_list, rseg->update_undo_list, undo);
1934
 
 
1935
 
        trx->update_undo = NULL;
1936
 
 
1937
 
        if (undo->state == TRX_UNDO_CACHED) {
1938
 
 
1939
 
                UT_LIST_ADD_FIRST(undo_list, rseg->update_undo_cached, undo);
1940
 
        } else {
1941
 
                ut_ad(undo->state == TRX_UNDO_TO_PURGE
1942
 
                      || undo->state == TRX_UNDO_TO_FREE);
1943
 
 
1944
 
                trx_undo_mem_free(undo);
1945
 
        }
1946
 
}
1947
 
 
1948
 
/******************************************************************//**
1949
 
Frees or caches an insert undo log after a transaction commit or rollback.
1950
 
Knowledge of inserts is not needed after a commit or rollback, therefore
1951
 
the data can be discarded. */
1952
 
UNIV_INTERN
1953
 
void
1954
 
trx_undo_insert_cleanup(
1955
 
/*====================*/
1956
 
        trx_t*  trx)    /*!< in: transaction handle */
1957
 
{
1958
 
        trx_undo_t*     undo;
1959
 
        trx_rseg_t*     rseg;
1960
 
 
1961
 
        undo = trx->insert_undo;
1962
 
        ut_ad(undo);
1963
 
 
1964
 
        rseg = trx->rseg;
1965
 
 
1966
 
        mutex_enter(&(rseg->mutex));
1967
 
 
1968
 
        UT_LIST_REMOVE(undo_list, rseg->insert_undo_list, undo);
1969
 
        trx->insert_undo = NULL;
1970
 
 
1971
 
        if (undo->state == TRX_UNDO_CACHED) {
1972
 
 
1973
 
                UT_LIST_ADD_FIRST(undo_list, rseg->insert_undo_cached, undo);
1974
 
        } else {
1975
 
                ut_ad(undo->state == TRX_UNDO_TO_FREE);
1976
 
 
1977
 
                /* Delete first the undo log segment in the file */
1978
 
 
1979
 
                mutex_exit(&(rseg->mutex));
1980
 
 
1981
 
                trx_undo_seg_free(undo);
1982
 
 
1983
 
                mutex_enter(&(rseg->mutex));
1984
 
 
1985
 
                ut_ad(rseg->curr_size > undo->size);
1986
 
 
1987
 
                rseg->curr_size -= undo->size;
1988
 
 
1989
 
                trx_undo_mem_free(undo);
1990
 
        }
1991
 
 
1992
 
        mutex_exit(&(rseg->mutex));
1993
 
}
1994
 
#endif /* !UNIV_HOTBACKUP */