00001 /****************************************************** 00002 Transaction system 00003 00004 (c) 1996 Innobase Oy 00005 00006 Created 3/26/1996 Heikki Tuuri 00007 *******************************************************/ 00008 00009 #ifndef trx0sys_h 00010 #define trx0sys_h 00011 00012 #include "univ.i" 00013 00014 #include "trx0types.h" 00015 #include "mtr0mtr.h" 00016 #include "mtr0log.h" 00017 #include "ut0byte.h" 00018 #include "mem0mem.h" 00019 #include "sync0sync.h" 00020 #include "ut0lst.h" 00021 #include "buf0buf.h" 00022 #include "fil0fil.h" 00023 #include "fut0lst.h" 00024 #include "fsp0fsp.h" 00025 #include "read0types.h" 00026 00027 /* In a MySQL replication slave, in crash recovery we store the master log 00028 file name and position here. We have successfully got the updates to InnoDB 00029 up to this position. If .._pos is -1, it means no crash recovery was needed, 00030 or there was no master log position info inside InnoDB. */ 00031 00032 extern char trx_sys_mysql_master_log_name[]; 00033 extern ib_longlong trx_sys_mysql_master_log_pos; 00034 00035 /* If this MySQL server uses binary logging, after InnoDB has been inited 00036 and if it has done a crash recovery, we store the binlog file name and position 00037 here. If .._pos is -1, it means there was no binlog position info inside 00038 InnoDB. */ 00039 00040 extern char trx_sys_mysql_bin_log_name[]; 00041 extern ib_longlong trx_sys_mysql_bin_log_pos; 00042 00043 /* The transaction system */ 00044 extern trx_sys_t* trx_sys; 00045 00046 /* Doublewrite system */ 00047 extern trx_doublewrite_t* trx_doublewrite; 00048 extern ibool trx_doublewrite_must_reset_space_ids; 00049 extern ibool trx_sys_multiple_tablespace_format; 00050 00051 /******************************************************************** 00052 Creates the doublewrite buffer to a new InnoDB installation. The header of the 00053 doublewrite buffer is placed on the trx system header page. */ 00054 00055 void 00056 trx_sys_create_doublewrite_buf(void); 00057 /*================================*/ 00058 /******************************************************************** 00059 At a database startup initializes the doublewrite buffer memory structure if 00060 we already have a doublewrite buffer created in the data files. If we are 00061 upgrading to an InnoDB version which supports multiple tablespaces, then this 00062 function performs the necessary update operations. If we are in a crash 00063 recovery, this function uses a possible doublewrite buffer to restore 00064 half-written pages in the data files. */ 00065 00066 void 00067 trx_sys_doublewrite_init_or_restore_pages( 00068 /*======================================*/ 00069 ibool restore_corrupt_pages); 00070 /******************************************************************** 00071 Marks the trx sys header when we have successfully upgraded to the >= 4.1.x 00072 multiple tablespace format. */ 00073 00074 void 00075 trx_sys_mark_upgraded_to_multiple_tablespaces(void); 00076 /*===============================================*/ 00077 /******************************************************************** 00078 Determines if a page number is located inside the doublewrite buffer. */ 00079 00080 ibool 00081 trx_doublewrite_page_inside( 00082 /*========================*/ 00083 /* out: TRUE if the location is inside 00084 the two blocks of the doublewrite buffer */ 00085 ulint page_no); /* in: page number */ 00086 /******************************************************************* 00087 Checks if a page address is the trx sys header page. */ 00088 UNIV_INLINE 00089 ibool 00090 trx_sys_hdr_page( 00091 /*=============*/ 00092 /* out: TRUE if trx sys header page */ 00093 ulint space, /* in: space */ 00094 ulint page_no);/* in: page number */ 00095 /********************************************************************* 00096 Creates and initializes the central memory structures for the transaction 00097 system. This is called when the database is started. */ 00098 00099 void 00100 trx_sys_init_at_db_start(void); 00101 /*==========================*/ 00102 /********************************************************************* 00103 Creates and initializes the transaction system at the database creation. */ 00104 00105 void 00106 trx_sys_create(void); 00107 /*================*/ 00108 /******************************************************************** 00109 Looks for a free slot for a rollback segment in the trx system file copy. */ 00110 00111 ulint 00112 trx_sysf_rseg_find_free( 00113 /*====================*/ 00114 /* out: slot index or ULINT_UNDEFINED 00115 if not found */ 00116 mtr_t* mtr); /* in: mtr */ 00117 /******************************************************************* 00118 Gets the pointer in the nth slot of the rseg array. */ 00119 UNIV_INLINE 00120 trx_rseg_t* 00121 trx_sys_get_nth_rseg( 00122 /*=================*/ 00123 /* out: pointer to rseg object, NULL if slot 00124 not in use */ 00125 trx_sys_t* sys, /* in: trx system */ 00126 ulint n); /* in: index of slot */ 00127 /******************************************************************* 00128 Sets the pointer in the nth slot of the rseg array. */ 00129 UNIV_INLINE 00130 void 00131 trx_sys_set_nth_rseg( 00132 /*=================*/ 00133 trx_sys_t* sys, /* in: trx system */ 00134 ulint n, /* in: index of slot */ 00135 trx_rseg_t* rseg); /* in: pointer to rseg object, NULL if slot 00136 not in use */ 00137 /************************************************************************** 00138 Gets a pointer to the transaction system file copy and x-locks its page. */ 00139 UNIV_INLINE 00140 trx_sysf_t* 00141 trx_sysf_get( 00142 /*=========*/ 00143 /* out: pointer to system file copy, page x-locked */ 00144 mtr_t* mtr); /* in: mtr */ 00145 /********************************************************************* 00146 Gets the space of the nth rollback segment slot in the trx system 00147 file copy. */ 00148 UNIV_INLINE 00149 ulint 00150 trx_sysf_rseg_get_space( 00151 /*====================*/ 00152 /* out: space id */ 00153 trx_sysf_t* sys_header, /* in: trx sys file copy */ 00154 ulint i, /* in: slot index == rseg id */ 00155 mtr_t* mtr); /* in: mtr */ 00156 /********************************************************************* 00157 Gets the page number of the nth rollback segment slot in the trx system 00158 file copy. */ 00159 UNIV_INLINE 00160 ulint 00161 trx_sysf_rseg_get_page_no( 00162 /*======================*/ 00163 /* out: page number, FIL_NULL 00164 if slot unused */ 00165 trx_sysf_t* sys_header, /* in: trx sys file copy */ 00166 ulint i, /* in: slot index == rseg id */ 00167 mtr_t* mtr); /* in: mtr */ 00168 /********************************************************************* 00169 Sets the space id of the nth rollback segment slot in the trx system 00170 file copy. */ 00171 UNIV_INLINE 00172 void 00173 trx_sysf_rseg_set_space( 00174 /*====================*/ 00175 trx_sysf_t* sys_header, /* in: trx sys file copy */ 00176 ulint i, /* in: slot index == rseg id */ 00177 ulint space, /* in: space id */ 00178 mtr_t* mtr); /* in: mtr */ 00179 /********************************************************************* 00180 Sets the page number of the nth rollback segment slot in the trx system 00181 file copy. */ 00182 UNIV_INLINE 00183 void 00184 trx_sysf_rseg_set_page_no( 00185 /*======================*/ 00186 trx_sysf_t* sys_header, /* in: trx sys file copy */ 00187 ulint i, /* in: slot index == rseg id */ 00188 ulint page_no, /* in: page number, FIL_NULL if 00189 the slot is reset to unused */ 00190 mtr_t* mtr); /* in: mtr */ 00191 /********************************************************************* 00192 Allocates a new transaction id. */ 00193 UNIV_INLINE 00194 dulint 00195 trx_sys_get_new_trx_id(void); 00196 /*========================*/ 00197 /* out: new, allocated trx id */ 00198 /********************************************************************* 00199 Allocates a new transaction number. */ 00200 UNIV_INLINE 00201 dulint 00202 trx_sys_get_new_trx_no(void); 00203 /*========================*/ 00204 /* out: new, allocated trx number */ 00205 /********************************************************************* 00206 Writes a trx id to an index page. In case that the id size changes in 00207 some future version, this function should be used instead of 00208 mach_write_... */ 00209 UNIV_INLINE 00210 void 00211 trx_write_trx_id( 00212 /*=============*/ 00213 byte* ptr, /* in: pointer to memory where written */ 00214 dulint id); /* in: id */ 00215 /********************************************************************* 00216 Reads a trx id from an index page. In case that the id size changes in 00217 some future version, this function should be used instead of 00218 mach_read_... */ 00219 UNIV_INLINE 00220 dulint 00221 trx_read_trx_id( 00222 /*============*/ 00223 /* out: id */ 00224 byte* ptr); /* in: pointer to memory from where to read */ 00225 /******************************************************************** 00226 Looks for the trx handle with the given id in trx_list. */ 00227 UNIV_INLINE 00228 trx_t* 00229 trx_get_on_id( 00230 /*==========*/ 00231 /* out: the trx handle or NULL if not found */ 00232 dulint trx_id); /* in: trx id to search for */ 00233 /******************************************************************** 00234 Returns the minumum trx id in trx list. This is the smallest id for which 00235 the trx can possibly be active. (But, you must look at the trx->conc_state to 00236 find out if the minimum trx id transaction itself is active, or already 00237 committed.) */ 00238 UNIV_INLINE 00239 dulint 00240 trx_list_get_min_trx_id(void); 00241 /*=========================*/ 00242 /* out: the minimum trx id, or trx_sys->max_trx_id 00243 if the trx list is empty */ 00244 /******************************************************************** 00245 Checks if a transaction with the given id is active. */ 00246 UNIV_INLINE 00247 ibool 00248 trx_is_active( 00249 /*==========*/ 00250 /* out: TRUE if active */ 00251 dulint trx_id);/* in: trx id of the transaction */ 00252 /******************************************************************** 00253 Checks that trx is in the trx list. */ 00254 00255 ibool 00256 trx_in_trx_list( 00257 /*============*/ 00258 /* out: TRUE if is in */ 00259 trx_t* in_trx);/* in: trx */ 00260 /********************************************************************* 00261 Updates the offset information about the end of the MySQL binlog entry 00262 which corresponds to the transaction just being committed. In a MySQL 00263 replication slave updates the latest master binlog position up to which 00264 replication has proceeded. */ 00265 00266 void 00267 trx_sys_update_mysql_binlog_offset( 00268 /*===============================*/ 00269 const char* file_name,/* in: MySQL log file name */ 00270 ib_longlong offset, /* in: position in that log file */ 00271 ulint field, /* in: offset of the MySQL log info field in 00272 the trx sys header */ 00273 mtr_t* mtr); /* in: mtr */ 00274 /********************************************************************* 00275 Prints to stderr the MySQL binlog offset info in the trx system header if 00276 the magic number shows it valid. */ 00277 00278 void 00279 trx_sys_print_mysql_binlog_offset(void); 00280 /*===================================*/ 00281 #ifdef UNIV_HOTBACKUP 00282 /********************************************************************* 00283 Prints to stderr the MySQL binlog info in the system header if the 00284 magic number shows it valid. */ 00285 00286 void 00287 trx_sys_print_mysql_binlog_offset_from_page( 00288 /*========================================*/ 00289 byte* page); /* in: buffer containing the trx system header page, 00290 i.e., page number TRX_SYS_PAGE_NO in the tablespace */ 00291 #endif /* UNIV_HOTBACKUP */ 00292 /********************************************************************* 00293 Prints to stderr the MySQL master log offset info in the trx system header if 00294 the magic number shows it valid. */ 00295 00296 void 00297 trx_sys_print_mysql_master_log_pos(void); 00298 /*====================================*/ 00299 00300 /* The automatically created system rollback segment has this id */ 00301 #define TRX_SYS_SYSTEM_RSEG_ID 0 00302 00303 /* Space id and page no where the trx system file copy resides */ 00304 #define TRX_SYS_SPACE 0 /* the SYSTEM tablespace */ 00305 #define TRX_SYS_PAGE_NO FSP_TRX_SYS_PAGE_NO 00306 00307 /* The offset of the transaction system header on the page */ 00308 #define TRX_SYS FSEG_PAGE_DATA 00309 00310 /* Transaction system header */ 00311 /*-------------------------------------------------------------*/ 00312 #define TRX_SYS_TRX_ID_STORE 0 /* the maximum trx id or trx number 00313 modulo TRX_SYS_TRX_ID_UPDATE_MARGIN 00314 written to a file page by any 00315 transaction; the assignment of 00316 transaction ids continues from this 00317 number rounded up by .._MARGIN plus 00318 .._MARGIN when the database is 00319 started */ 00320 #define TRX_SYS_FSEG_HEADER 8 /* segment header for the tablespace 00321 segment the trx system is created 00322 into */ 00323 #define TRX_SYS_RSEGS (8 + FSEG_HEADER_SIZE) 00324 /* the start of the array of rollback 00325 segment specification slots */ 00326 /*-------------------------------------------------------------*/ 00327 00328 /* Max number of rollback segments: the number of segment specification slots 00329 in the transaction system array; rollback segment id must fit in one byte, 00330 therefore 256; each slot is currently 8 bytes in size */ 00331 #define TRX_SYS_N_RSEGS 256 00332 00333 #define TRX_SYS_MYSQL_LOG_NAME_LEN 512 00334 #define TRX_SYS_MYSQL_LOG_MAGIC_N 873422344 00335 00336 /* The offset of the MySQL replication info in the trx system header; 00337 this contains the same fields as TRX_SYS_MYSQL_LOG_INFO below */ 00338 #define TRX_SYS_MYSQL_MASTER_LOG_INFO (UNIV_PAGE_SIZE - 2000) 00339 00340 /* The offset of the MySQL binlog offset info in the trx system header */ 00341 #define TRX_SYS_MYSQL_LOG_INFO (UNIV_PAGE_SIZE - 1000) 00342 #define TRX_SYS_MYSQL_LOG_MAGIC_N_FLD 0 /* magic number which shows 00343 if we have valid data in the 00344 MySQL binlog info; the value 00345 is ..._MAGIC_N if yes */ 00346 #define TRX_SYS_MYSQL_LOG_OFFSET_HIGH 4 /* high 4 bytes of the offset 00347 within that file */ 00348 #define TRX_SYS_MYSQL_LOG_OFFSET_LOW 8 /* low 4 bytes of the offset 00349 within that file */ 00350 #define TRX_SYS_MYSQL_LOG_NAME 12 /* MySQL log file name */ 00351 00352 /* The offset of the doublewrite buffer header on the trx system header page */ 00353 #define TRX_SYS_DOUBLEWRITE (UNIV_PAGE_SIZE - 200) 00354 /*-------------------------------------------------------------*/ 00355 #define TRX_SYS_DOUBLEWRITE_FSEG 0 /* fseg header of the fseg 00356 containing the doublewrite 00357 buffer */ 00358 #define TRX_SYS_DOUBLEWRITE_MAGIC FSEG_HEADER_SIZE 00359 /* 4-byte magic number which 00360 shows if we already have 00361 created the doublewrite 00362 buffer */ 00363 #define TRX_SYS_DOUBLEWRITE_BLOCK1 (4 + FSEG_HEADER_SIZE) 00364 /* page number of the 00365 first page in the first 00366 sequence of 64 00367 (= FSP_EXTENT_SIZE) consecutive 00368 pages in the doublewrite 00369 buffer */ 00370 #define TRX_SYS_DOUBLEWRITE_BLOCK2 (8 + FSEG_HEADER_SIZE) 00371 /* page number of the 00372 first page in the second 00373 sequence of 64 consecutive 00374 pages in the doublewrite 00375 buffer */ 00376 #define TRX_SYS_DOUBLEWRITE_REPEAT 12 /* we repeat the above 3 00377 numbers so that if the trx 00378 sys header is half-written 00379 to disk, we still may be able 00380 to recover the information */ 00381 #define TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED (24 + FSEG_HEADER_SIZE) 00382 /* If this is not yet set to 00383 .._N, we must reset the 00384 doublewrite buffer, because 00385 starting from 4.1.x the space 00386 id of a data page is stored to 00387 FIL_PAGE_ARCH_LOG_NO_OR_SPACE_NO */ 00388 /*-------------------------------------------------------------*/ 00389 #define TRX_SYS_DOUBLEWRITE_MAGIC_N 536853855 00390 #define TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED_N 1783657386 00391 00392 00393 #define TRX_SYS_DOUBLEWRITE_BLOCK_SIZE FSP_EXTENT_SIZE 00394 00395 /* Doublewrite control struct */ 00396 struct trx_doublewrite_struct{ 00397 mutex_t mutex; /* mutex protecting the first_free field and 00398 write_buf */ 00399 ulint block1; /* the page number of the first 00400 doublewrite block (64 pages) */ 00401 ulint block2; /* page number of the second block */ 00402 ulint first_free; /* first free position in write_buf measured 00403 in units of UNIV_PAGE_SIZE */ 00404 byte* write_buf; /* write buffer used in writing to the 00405 doublewrite buffer, aligned to an 00406 address divisible by UNIV_PAGE_SIZE 00407 (which is required by Windows aio) */ 00408 byte* write_buf_unaligned; /* pointer to write_buf, but unaligned */ 00409 buf_block_t** 00410 buf_block_arr; /* array to store pointers to the buffer 00411 blocks which have been cached to write_buf */ 00412 }; 00413 00414 /* The transaction system central memory data structure; protected by the 00415 kernel mutex */ 00416 struct trx_sys_struct{ 00417 dulint max_trx_id; /* The smallest number not yet 00418 assigned as a transaction id or 00419 transaction number */ 00420 UT_LIST_BASE_NODE_T(trx_t) trx_list; 00421 /* List of active and committed in 00422 memory transactions, sorted on trx id, 00423 biggest first */ 00424 UT_LIST_BASE_NODE_T(trx_t) mysql_trx_list; 00425 /* List of transactions created 00426 for MySQL */ 00427 UT_LIST_BASE_NODE_T(trx_rseg_t) rseg_list; 00428 /* List of rollback segment objects */ 00429 trx_rseg_t* latest_rseg; /* Latest rollback segment in the 00430 round-robin assignment of rollback 00431 segments to transactions */ 00432 trx_rseg_t* rseg_array[TRX_SYS_N_RSEGS]; 00433 /* Pointer array to rollback segments; 00434 NULL if slot not in use */ 00435 ulint rseg_history_len;/* Length of the TRX_RSEG_HISTORY 00436 list (update undo logs for committed 00437 transactions), protected by 00438 rseg->mutex */ 00439 UT_LIST_BASE_NODE_T(read_view_t) view_list; 00440 /* List of read views sorted on trx no, 00441 biggest first */ 00442 }; 00443 00444 /* When a trx id which is zero modulo this number (which must be a power of 00445 two) is assigned, the field TRX_SYS_TRX_ID_STORE on the transaction system 00446 page is updated */ 00447 #define TRX_SYS_TRX_ID_WRITE_MARGIN 256 00448 00449 #ifndef UNIV_NONINL 00450 #include "trx0sys.ic" 00451 #endif 00452 00453 #endif
1.4.7

