summaryrefslogtreecommitdiff
path: root/storage/innobase/include/row0trunc.h
blob: c5f89f7cfdbfc2a69d84348b7d45797be78df376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*****************************************************************************

Copyright (c) 2013, 2015, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2018, 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/row0trunc.h
TRUNCATE implementation

Created 2013-04-25 Krunal Bauskar
*******************************************************/

#ifndef row0trunc_h
#define row0trunc_h

#include "row0mysql.h"
#include "dict0boot.h"
#include "fil0fil.h"
#include "srv0start.h"

#include <vector>

/** The information of TRUNCATE log record.
This class handles the recovery stage of TRUNCATE table. */
class truncate_t {

public:
	/**
	Constructor

	@param old_table_id	old table id assigned to table before truncate
	@param new_table_id	new table id that will be assigned to table
				after truncate
	@param dir_path		directory path */
	truncate_t(
		table_id_t	old_table_id,
		table_id_t	new_table_id,
		const char*	dir_path);

	/**
	Constructor

	@param log_file_name	parse the log file during recovery to populate
				information related to table to truncate */
	truncate_t(const char*	log_file_name);

	/**
	Consturctor

	@param space_id		space in which table reisde
	@param name		table name
	@param tablespace_flags	tablespace flags use for recreating tablespace
	@param log_flags	page format flag
	@param recv_lsn		lsn of redo log record. */
	truncate_t(
		ulint		space_id,
		const char*	name,
		ulint		tablespace_flags,
		ulint		log_flags,
		lsn_t		recv_lsn);

	/** Destructor */
	~truncate_t();

	/** The index information of MLOG_FILE_TRUNCATE redo record */
	struct index_t {

		/* Default copy constructor and destructor should be OK. */

		index_t();

		/**
		Set the truncate log values for a compressed table.
		@return DB_CORRUPTION or error code */
		dberr_t set(const dict_index_t* index);

		typedef std::vector<byte, ut_allocator<byte> >	fields_t;

		/** Index id */
		index_id_t	m_id;

		/** Index type */
		ulint		m_type;

		/** Root Page Number */
		ulint		m_root_page_no;

		/** New Root Page Number.
		Note: This field is not persisted to TRUNCATE log but used
		during truncate table fix-up for updating SYS_XXXX tables. */
		ulint		m_new_root_page_no;

		/** Number of index fields */
		ulint		m_n_fields;

		/** DATA_TRX_ID column position. */
		ulint		m_trx_id_pos;

		/** Compressed table field meta data, encode by
		page_zip_fields_encode. Empty for non-compressed tables.
		Should be NUL terminated. */
		fields_t	m_fields;
	};

	/**
	@return the directory path, can be NULL */
	const char* get_dir_path() const
	{
		return(m_dir_path);
	}

	/**
	Register index information

	@param index	index information logged as part of truncate log. */
	void add(index_t& index)
	{
		m_indexes.push_back(index);
	}

	/**
	Add table to truncate post recovery.

	@param ptr	table information need to complete truncate of table. */
	static void add(truncate_t* ptr)
	{
		s_tables.push_back(ptr);
	}

	/**
	Clear registered index vector */
	void clear()
	{
		m_indexes.clear();
	}

	/**
	@return old table id of the table to truncate */
	table_id_t old_table_id() const
	{
		return(m_old_table_id);
	}

	/**
	@return new table id of the table to truncate */
	table_id_t new_table_id() const
	{
		return(m_new_table_id);
	}

	/**
	Update root page number in SYS_XXXX tables.

	@param trx			transaction object
	@param table_id			table id for which information needs to
					be updated.
	@param reserve_dict_mutex       if TRUE, acquire/release
					dict_sys->mutex around call to pars_sql.
	@param mark_index_corrupted	if true, then mark index corrupted
	@return DB_SUCCESS or error code */
	dberr_t update_root_page_no(
		trx_t*		trx,
		table_id_t	table_id,
		ibool		reserve_dict_mutex,
		bool		mark_index_corrupted) const;

	/** Create an index for a table.
	@param[in]	table_name		table name, for which to create
	the index
	@param[in,out]	space			tablespace
	@param[in]	index_type		type of index to truncate
	@param[in]	index_id		id of index to truncate
	@param[in]	btr_redo_create_info	control info for ::btr_create()
	@param[in,out]	mtr			mini-transaction covering the
	create index
	@return root page no or FIL_NULL on failure */
	inline ulint create_index(
		const char*		table_name,
		fil_space_t*		space,
		ulint			index_type,
		index_id_t      	index_id,
		const btr_create_t&	btr_redo_create_info,
		mtr_t*			mtr) const;

	/** Create the indexes for a table
	@param[in]	table_name	table name, for which to create the
	indexes
	@param[in,out]	space		tablespace
	@param[in]	format_flags	page format flags
	@return DB_SUCCESS or error code. */
	inline dberr_t create_indexes(
		const char*		table_name,
		fil_space_t*		space,
		ulint			format_flags);

	/** Check if index has been modified since TRUNCATE log snapshot
	was recorded.
	@param[in]	space		tablespace
	@param[in]	root_page_no	index root page number
	@return true if modified else false */
	inline bool is_index_modified_since_logged(
		const fil_space_t*	space,
		ulint			root_page_no) const;

	/** Drop indexes for a table.
	@param[in,out] space		tablespace
	@return DB_SUCCESS or error code. */
	void drop_indexes(fil_space_t* space) const;

	/**
	Parses log record during recovery
	@param start_ptr	buffer containing log body to parse
	@param end_ptr		buffer end

	@return DB_SUCCESS or error code */
	dberr_t parse(
		byte*		start_ptr,
		const byte*	end_ptr);

	/** Parse MLOG_TRUNCATE log record from REDO log file during recovery.
	@param[in,out]	start_ptr	buffer containing log body to parse
	@param[in]	end_ptr		buffer end
	@param[in]	space_id	tablespace identifier
	@return parsed upto or NULL. */
	static byte* parse_redo_entry(
		byte*		start_ptr,
		const byte*	end_ptr,
		ulint		space_id);

	/**
	Write a log record for truncating a single-table tablespace.

	@param start_ptr	buffer to write log record
	@param end_ptr		buffer end
	@param space_id		space id
	@param tablename	the table name in the usual
				databasename/tablename format of InnoDB
	@param flags		tablespace flags
	@param format_flags	page format
	@param lsn		lsn while logging */
	dberr_t write(
		byte*		start_ptr,
		byte*		end_ptr,
		ulint		space_id,
		const char*	tablename,
		ulint		flags,
		ulint		format_flags,
		lsn_t		lsn) const;

	/**
	@return number of indexes parsed from the truncate log record */
	size_t indexes() const;

	/**
	Truncate a single-table tablespace. The tablespace must be cached
	in the memory cache.

	Note: This is defined in fil0fil.cc because it needs to access some
	types that are local to that file.

	@param space_id		space id
	@param dir_path		directory path
	@param tablename	the table name in the usual
				databasename/tablename format of InnoDB
	@param flags		tablespace flags
	@param default_size	if true, truncate to default size if tablespace
				is being newly re-initialized.
	@return DB_SUCCESS or error */
	static dberr_t truncate(
		ulint		space_id,
		const char*	dir_path,
		const char*	tablename,
		ulint		flags,
		bool		default_size);

	/**
	Fix the table truncate by applying information parsed from TRUNCATE log.
	Fix-up includes re-creating table (drop and re-create indexes) 
	@return	error code or DB_SUCCESS */
	static dberr_t fixup_tables_in_system_tablespace();

	/**
	Fix the table truncate by applying information parsed from TRUNCATE log.
	Fix-up includes re-creating tablespace.
	@return	error code or DB_SUCCESS */
	static dberr_t fixup_tables_in_non_system_tablespace();

	/**
	Check whether a tablespace was truncated during recovery
	@param space_id		tablespace id to check
	@return true if the tablespace was truncated */
	static bool is_tablespace_truncated(ulint space_id);

	/** Was tablespace truncated (on crash before checkpoint).
	If the MLOG_TRUNCATE redo-record is still available then tablespace
	was truncated and checkpoint is yet to happen.
	@param[in]	space_id	tablespace id to check.
	@return true if tablespace was truncated. */
	static bool was_tablespace_truncated(ulint space_id);

	/** Get the lsn associated with space.
	@param[in]	space_id	tablespace id to check.
	@return associated lsn. */
	static lsn_t get_truncated_tablespace_init_lsn(ulint space_id);

private:
	typedef std::vector<index_t, ut_allocator<index_t> >	indexes_t;

	/** Space ID of tablespace */
	ulint			m_space_id;

	/** ID of table that is being truncated. */
	table_id_t		m_old_table_id;

	/** New ID that will be assigned to table on truncation. */
	table_id_t		m_new_table_id;

	/** Data dir path of tablespace */
	char*			m_dir_path;

	/** Table name */
	char*			m_tablename;

	/** Tablespace Flags */
	ulint			m_tablespace_flags;

	/** Format flags (log flags; stored in page-no field of header) */
	ulint			m_format_flags;

	/** Index meta-data */
	indexes_t		m_indexes;

	/** LSN of TRUNCATE log record. */
	lsn_t			m_log_lsn;

	/** Log file name. */
	char*			m_log_file_name;

	/** Encryption information of the table */
	fil_encryption_t	m_encryption;
	uint32_t		m_key_id;

	/** Vector of tables to truncate. */
	typedef	std::vector<truncate_t*, ut_allocator<truncate_t*> >
		tables_t;

	/** Information about tables to truncate post recovery */
	static	tables_t	s_tables;

	/** Information about truncated table
	This is case when truncate is complete but checkpoint hasn't. */
	typedef std::map<ulint, lsn_t>	truncated_tables_t;
	static truncated_tables_t	s_truncated_tables;

public:
	/** If true then fix-up of table is active and so while creating
	index instead of grabbing information from dict_index_t, grab it
	from parsed truncate log record. */
	static	bool		s_fix_up_active;
};

/**
Parse truncate log file. */
class TruncateLogParser {

public:

	/**
	Scan and Parse truncate log files.

	@param dir_path         look for log directory in following path
	@return DB_SUCCESS or error code. */
	static dberr_t scan_and_parse(
		const char*	dir_path);

private:
	typedef std::vector<char*, ut_allocator<char*> >
		trunc_log_files_t;

private:
	/**
	Scan to find out truncate log file from the given directory path.

	@param dir_path		look for log directory in following path.
	@param log_files	cache to hold truncate log file name found.
	@return DB_SUCCESS or error code. */
	static dberr_t scan(
		const char*		dir_path,
		trunc_log_files_t&	log_files);

	/**
	Parse the log file and populate table to truncate information.
	(Add this table to truncate information to central vector that is then
	used by truncate fix-up routine to fix-up truncate action of the table.)

	@param	log_file_name	log file to parse
	@return DB_SUCCESS or error code. */
	static dberr_t parse(
		const char*		log_file_name);
};

#endif /* row0trunc_h */