mariadb/storage/innobase/include/btr0bulk.h
Marko Mäkelä 7ae21b18a6 MDEV-12353: Change the redo log encoding
log_t::FORMAT_10_5: physical redo log format tag

log_phys_t: Buffered records in the physical format.
The log record bytes will follow the last data field,
making use of alignment padding that would otherwise be wasted.
If there are multiple records for the same page, also those
may be appended to an existing log_phys_t object if the memory
is available.

In the physical format, the first byte of a record identifies the
record and its length (up to 15 bytes). For longer records, the
immediately following bytes will encode the remaining length
in a variable-length encoding. Usually, a variable-length-encoded
page identifier will follow, followed by optional payload, whose
length is included in the initially encoded total record length.

When a mini-transaction is updating multiple fields in a page,
it can avoid repeating the tablespace identifier and page number
by setting the same_page flag (most significant bit) in the first
byte of the log record. The byte offset of the record will be
relative to where the previous record for that page ended.

Until MDEV-14425 introduces a separate file-level log for
redo log checkpoints and file operations, we will write the
file-level records in the page-level redo log file.
The record FILE_CHECKPOINT (which replaces MLOG_CHECKPOINT)
will be removed in MDEV-14425, and one sequential scan of the
page recovery log will suffice.

Compared to MLOG_FILE_CREATE2, FILE_CREATE will not include any flags.
If the information is needed, it can be parsed from WRITE records that
modify FSP_SPACE_FLAGS.

MLOG_ZIP_WRITE_STRING: Remove. The record was only introduced temporarily
as part of this work, before being replaced with WRITE (along with
MLOG_WRITE_STRING, MLOG_1BYTE, MLOG_nBYTES).

mtr_buf_t::empty(): Check if the buffer is empty.

mtr_t::m_n_log_recs: Remove. It suffices to check if m_log is empty.

mtr_t::m_last, mtr_t::m_last_offset: End of the latest m_log record,
for the same_page encoding.

page_recv_t::last_offset: Reflects mtr_t::m_last_offset.

Valid values for last_offset during recovery should be 0 or above 8.
(The first 8 bytes of a page are the checksum and the page number,
and neither are ever updated directly by log records.)
Internally, the special value 1 indicates that the same_page form
will not be allowed for the subsequent record.

mtr_t::page_create(): Take the block descriptor as parameter,
so that it can be compared to mtr_t::m_last. The INIT_INDEX_PAGE
record will always followed by a subtype byte, because same_page
records must be longer than 1 byte.

trx_undo_page_init(): Combine the writes in WRITE record.

trx_undo_header_create(): Write 4 bytes using a special MEMSET
record that includes 1 bytes of length and 2 bytes of payload.

flst_write_addr(): Define as a static function. Combine the writes.

flst_zero_both(): Replaces two flst_zero_addr() calls.

flst_init(): Do not inline the function.

fsp_free_seg_inode(): Zerofill the whole inode.

fsp_apply_init_file_page(): Initialize FIL_PAGE_PREV,FIL_PAGE_NEXT
to FIL_NULL when using the physical format.

btr_create(): Assert !page_has_siblings() because fsp_apply_init_file_page()
must have been invoked.

fil_ibd_create(): Do not write FILE_MODIFY after FILE_CREATE.

fil_names_dirty_and_write(): Remove the parameter mtr.
Write the records using a separate mini-transaction object,
because any FILE_ records must be at the start of a mini-transaction log.

recv_recover_page(): Add a fil_space_t* parameter.
After applying log to the a ROW_FORMAT=COMPRESSED page,
invoke buf_zip_decompress() to restore the uncompressed page.

buf_page_io_complete(): Remove the temporary hack to discard the
uncompressed page of a ROW_FORMAT=COMPRESSED page.

page_zip_write_header(): Remove. Use mtr_t::write() or
mtr_t::memset() instead, and update the compressed page frame
separately.

trx_undo_header_add_space_for_xid(): Remove.

trx_undo_seg_create(): Perform the changes that were previously
made by trx_undo_header_add_space_for_xid().

btr_reset_instant(): New function: Reset the table to MariaDB 10.2
or 10.3 format when rolling back an instant ALTER TABLE operation.

page_rec_find_owner_rec(): Merge with the only callers.

page_cur_insert_rec_low(): Combine writes by using a local buffer.
MEMMOVE data from the preceding record whenever feasible
(copying at least 3 bytes).

page_cur_insert_rec_zip(): Combine writes to page header fields.

PageBulk::insertPage(): Issue MEMMOVE records to copy a matching
part from the preceding record.

PageBulk::finishPage(): Combine the writes to the page header
and to the sparse page directory slots.

mtr_t::write(): Only log the least significant (last) bytes
of multi-byte fields that actually differ.

For updating FSP_SIZE, we must always write all 4 bytes to the
redo log, so that the fil_space_set_recv_size() logic in
recv_sys_t::parse() will work.

mtr_t::memcpy(), mtr_t::zmemcpy(): Take a pointer argument
instead of a numeric offset to the page frame. Only log the
last bytes of multi-byte fields that actually differ.

In fil_space_crypt_t::write_page0(), we must log also any
unchanged bytes, so that recovery will recognize the record
and invoke fil_crypt_parse().

Future work:
MDEV-21724 Optimize page_cur_insert_rec_low() redo logging
MDEV-21725 Optimize btr_page_reorganize_low() redo logging
MDEV-21727 Optimize redo logging for ROW_FORMAT=COMPRESSED
2020-02-13 19:12:17 +02:00

369 lines
8.7 KiB
C++

/*****************************************************************************
Copyright (c) 2014, 2015, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2019, 2020, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
*****************************************************************************/
/********************************************************************//**
@file include/btr0bulk.h
The B-tree bulk load
Created 03/11/2014 Shaohua Wang
*************************************************************************/
#ifndef btr0bulk_h
#define btr0bulk_h
#include "dict0dict.h"
#include "rem0types.h"
#include "page0cur.h"
#include <vector>
/** Innodb B-tree index fill factor for bulk load. */
extern uint innobase_fill_factor;
/*
The proper function call sequence of PageBulk is as below:
-- PageBulk::init
-- PageBulk::insert
-- PageBulk::finish
-- PageBulk::compress(COMPRESSED table only)
-- PageBulk::pageSplit(COMPRESSED table only)
-- PageBulk::commit
*/
class PageBulk
{
public:
/** Constructor
@param[in] index B-tree index
@param[in] page_no page number
@param[in] level page level
@param[in] trx_id transaction id */
PageBulk(
dict_index_t* index,
trx_id_t trx_id,
ulint page_no,
ulint level)
:
m_heap(NULL),
m_index(index),
m_mtr(),
m_trx_id(trx_id),
m_block(NULL),
m_page(NULL),
m_page_zip(NULL),
m_cur_rec(NULL),
m_page_no(page_no),
m_level(level),
m_is_comp(dict_table_is_comp(index->table)),
m_heap_top(NULL),
m_rec_no(0),
m_free_space(0),
m_reserved_space(0),
#ifdef UNIV_DEBUG
m_total_data(0),
#endif /* UNIV_DEBUG */
m_modify_clock(0),
m_err(DB_SUCCESS)
{
ut_ad(!dict_index_is_spatial(m_index));
ut_ad(!m_index->table->is_temporary());
}
/** Deconstructor */
~PageBulk()
{
mem_heap_free(m_heap);
}
/** Initialize members and allocate page if needed and start mtr.
Note: must be called and only once right after constructor.
@return error code */
dberr_t init();
/** Insert a record in the page.
@param[in] rec record
@param[in] offsets record offsets */
inline void insert(const rec_t* rec, offset_t* offsets);
private:
/** Page format */
enum format { REDUNDANT, DYNAMIC, COMPRESSED };
/** Mark end of insertion to the page. Scan all records to set page
dirs, and set page header members.
@tparam format the page format */
template<format> inline void finishPage();
/** Insert a record in the page.
@tparam format the page format
@param[in,out] rec record
@param[in] offsets record offsets */
template<format> inline void insertPage(rec_t* rec, offset_t* offsets);
public:
/** Mark end of insertion to the page. Scan all records to set page
dirs, and set page header members. */
inline void finish();
/** Commit mtr for a page
@param[in] success Flag whether all inserts succeed. */
void commit(bool success);
/** Compress if it is compressed table
@return true compress successfully or no need to compress
@return false compress failed. */
bool compress();
/** Check whether the record needs to be stored externally.
@return true
@return false */
bool needExt(const dtuple_t* tuple, ulint rec_size);
/** Store external record
@param[in] big_rec external recrod
@param[in] offsets record offsets
@return error code */
dberr_t storeExt(const big_rec_t* big_rec, offset_t* offsets);
/** Get node pointer
@return node pointer */
dtuple_t* getNodePtr();
/** Get split rec in the page. We split a page in half when compresssion
fails, and the split rec should be copied to the new page.
@return split rec */
rec_t* getSplitRec();
/** Copy all records after split rec including itself.
@param[in] rec split rec */
void copyIn(rec_t* split_rec);
/** Remove all records after split rec including itself.
@param[in] rec split rec */
void copyOut(rec_t* split_rec);
/** Set next page
@param[in] next_page_no next page no */
inline void setNext(ulint next_page_no);
/** Set previous page
@param[in] prev_page_no previous page no */
inline void setPrev(ulint prev_page_no);
/** Release block by commiting mtr */
inline void release();
/** Start mtr and latch block */
inline dberr_t latch();
/** Check if required space is available in the page for the rec
to be inserted. We check fill factor & padding here.
@param[in] length required length
@return true if space is available */
inline bool isSpaceAvailable(ulint rec_size);
/** Get page no */
ulint getPageNo()
{
return(m_page_no);
}
/** Get page level */
ulint getLevel()
{
return(m_level);
}
/** Get record no */
ulint getRecNo()
{
return(m_rec_no);
}
/** Get page */
page_t* getPage()
{
return(m_page);
}
/** Get page zip */
page_zip_des_t* getPageZip()
{
return(m_page_zip);
}
dberr_t getError()
{
return(m_err);
}
void set_modified() { m_mtr.set_modified(); }
/* Memory heap for internal allocation */
mem_heap_t* m_heap;
private:
/** The index B-tree */
dict_index_t* m_index;
/** The mini-transaction */
mtr_t m_mtr;
/** The transaction id */
trx_id_t m_trx_id;
/** The buffer block */
buf_block_t* m_block;
/** The page */
page_t* m_page;
/** The page zip descriptor */
page_zip_des_t* m_page_zip;
/** The current rec, just before the next insert rec */
rec_t* m_cur_rec;
/** The page no */
ulint m_page_no;
/** The page level in B-tree */
ulint m_level;
/** Flag: is page in compact format */
const bool m_is_comp;
/** The heap top in page for next insert */
byte* m_heap_top;
/** User record no */
ulint m_rec_no;
/** The free space left in the page */
ulint m_free_space;
/** The reserved space for fill factor */
ulint m_reserved_space;
/** The padding space for compressed page */
ulint m_padding_space;
#ifdef UNIV_DEBUG
/** Total data in the page */
ulint m_total_data;
#endif /* UNIV_DEBUG */
/** The modify clock value of the buffer block
when the block is re-pinned */
ib_uint64_t m_modify_clock;
/** Operation result DB_SUCCESS or error code */
dberr_t m_err;
};
typedef std::vector<PageBulk*, ut_allocator<PageBulk*> >
page_bulk_vector;
class BtrBulk
{
public:
/** Constructor
@param[in] index B-tree index
@param[in] trx transaction */
BtrBulk(
dict_index_t* index,
const trx_t* trx)
:
m_index(index),
m_trx(trx)
{
ut_ad(!dict_index_is_spatial(index));
}
/** Insert a tuple
@param[in] tuple tuple to insert.
@return error code */
dberr_t insert(dtuple_t* tuple)
{
return(insert(tuple, 0));
}
/** Btree bulk load finish. We commit the last page in each level
and copy the last page in top level to the root page of the index
if no error occurs.
@param[in] err whether bulk load was successful until now
@return error code */
dberr_t finish(dberr_t err);
/** Release all latches */
void release();
/** Re-latch all latches */
void latch();
private:
/** Insert a tuple to a page in a level
@param[in] tuple tuple to insert
@param[in] level B-tree level
@return error code */
dberr_t insert(dtuple_t* tuple, ulint level);
/** Split a page
@param[in] page_bulk page to split
@param[in] next_page_bulk next page
@return error code */
dberr_t pageSplit(PageBulk* page_bulk,
PageBulk* next_page_bulk);
/** Commit(finish) a page. We set next/prev page no, compress a page of
compressed table and split the page if compression fails, insert a node
pointer to father page if needed, and commit mini-transaction.
@param[in] page_bulk page to commit
@param[in] next_page_bulk next page
@param[in] insert_father flag whether need to insert node ptr
@return error code */
dberr_t pageCommit(PageBulk* page_bulk,
PageBulk* next_page_bulk,
bool insert_father);
/** Abort a page when an error occurs
@param[in] page_bulk page bulk object
Note: we should call pageAbort for a PageBulk object, which is not in
m_page_bulks after pageCommit, and we will commit or abort PageBulk
objects in function "finish". */
void pageAbort(PageBulk* page_bulk)
{
page_bulk->commit(false);
}
/** Log free check */
inline void logFreeCheck();
private:
/** B-tree index */
dict_index_t*const m_index;
/** Transaction */
const trx_t*const m_trx;
/** Root page level */
ulint m_root_level;
/** Page cursor vector for all level */
page_bulk_vector m_page_bulks;
};
#endif