You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

415 lines
14 KiB

17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
Fix for lp:711565 "Index Condition Pushdown can make a thread hold MyISAM locks as well as be unKILLable for long time" - In Maria/MyISAM: Release/re-acquire locks to give queries that wait on them a chance to make progress - In Maria/MyISAM: Change from numeric constants to ICP_RES values. - In Maria: Do check index condition in maria_rprev() (was lost in the merge/backport?) - In Maria/MyISAM/XtraDB: Check if the query was killed, and return immediately if it was. Added new storage engine error: HA_ERR_ABORTED_BY_USER, for handler to signal that it detected a kill of the query and aborted Authors: Sergey Petrunia & Monty include/my_base.h: Added HA_ERR_ABORTED_BY_USER, for handler to signal that it detected a kill of the query and aborted include/my_handler.h: Added comment mysql-test/r/myisam_icp.result: Updated test mysql-test/t/myisam_icp.test: Drop used tables at start of test Added test case that can help with manual testing of killing index condition pushdown query. mysys/my_handler_errors.h: Text for new storage engine error sql/handler.cc: If engine got HA_ERR_ABORTED_BY_USER, send kill message. sql/multi_range_read.cc: Return error code storage/maria/ha_maria.cc: Added ma_killed_in_mariadb() to detect kill. Ensure that file->external_ref points to TABLE object. storage/maria/ma_extra.c: Dummy test-if-killed for standalone storage/maria/ma_key.c: If ma_check_index_cond() fails, set my_errno and info->cur_row.lastpos storage/maria/ma_rkey.c: Release/re-acquire locks to give queries that wait on them a chance to make progress Check if the query was killed, and return immediately if it was storage/maria/ma_rnext.c: Check if the query was killed, and return immediately if it was Added missing fast_ma_writeinfo(info) storage/maria/ma_rnext_same.c: Check if the query was killed, and return immediately if it was Added missing fast_ma_writeinfo(info) storage/maria/ma_rprev.c: Check if the query was killed, and return immediately if it was Added missing fast_ma_writeinfo(info) and ma_check_index_cond() storage/maria/ma_search.c: Give error message if we find a wrong key storage/maria/ma_static.c: Added pointer to test-if-killed function storage/maria/maria_def.h: New prototypes storage/myisam/ha_myisam.cc: Added mi_killed_in_mariadb() Ensure that file->external_ref points to TABLE object. storage/myisam/mi_extra.c: Dummy test-if-killed for standalone storage/myisam/mi_key.c: If ma_check_index_cond() fails, set my_errno and info->lastpos storage/myisam/mi_rkey.c: Ensure that info->lastpos= HA_OFFSET_ERROR in case of error Release/re-acquire locks to give queries that wait on them a chance to make progress Check if the query was killed, and return immediately if it was Reorder code to do less things in case of error. Added missing fast_mi_writeinfo() storage/myisam/mi_rnext.c: Check if the query was killed, and return immediately if it was Simplify old ICP code Added missing fast_ma_writeinfo(info) storage/myisam/mi_rnext_same.c: Check if the query was killed, and return immediately if it was Added missing fast_mi_writeinfo(info) storage/myisam/mi_rprev.c: Check if the query was killed, and return immediately if it was Simplify error handling of ICP Added missing fast_mi_writeinfo(info) storage/myisam/mi_search.c: Give error message if we find a wrong key storage/myisam/mi_static.c: Added pointer to test-if-killed function storage/myisam/myisamdef.h: New prototypes storage/xtradb/handler/ha_innodb.cc: Added DB_SEARCH_ABORTED_BY_USER and ha_innobase::is_thd_killed() Check if the query was killed, and return immediately if it was storage/xtradb/handler/ha_innodb.h: Added prototype storage/xtradb/include/db0err.h: Added DB_SEARCH_ABORTED_BY_USER storage/xtradb/include/row0mysql.h: Added possible ICP errors storage/xtradb/row/row0sel.c: Use ICP errors instead of constants. Detect if killed and return B_SEARCH_ABORTED_BY_USER
15 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
17 years ago
16 years ago
15 years ago
15 years ago
  1. /*****************************************************************************
  2. Copyright (c) 2000, 2010, MySQL AB & Innobase Oy. All Rights Reserved.
  3. Use is subject to license terms
  4. This program is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  13. *****************************************************************************/
  14. /*
  15. This file is based on ha_berkeley.h of MySQL distribution
  16. This file defines the Innodb handler: the interface between MySQL and
  17. Innodb
  18. */
  19. #ifdef USE_PRAGMA_INTERFACE
  20. #pragma interface /* gcc class implementation */
  21. #endif
  22. /* Structure defines translation table between mysql index and innodb
  23. index structures */
  24. typedef struct innodb_idx_translate_struct {
  25. ulint index_count; /*!< number of valid index entries
  26. in the index_mapping array */
  27. ulint array_size; /*!< array size of index_mapping */
  28. dict_index_t** index_mapping; /*!< index pointer array directly
  29. maps to index in Innodb from MySQL
  30. array index */
  31. } innodb_idx_translate_t;
  32. /** InnoDB table share */
  33. typedef struct st_innobase_share {
  34. THR_LOCK lock; /*!< MySQL lock protecting
  35. this structure */
  36. const char* table_name; /*!< InnoDB table name */
  37. uint use_count; /*!< reference count,
  38. incremented in get_share()
  39. and decremented in
  40. free_share() */
  41. void* table_name_hash;/*!< hash table chain node */
  42. innodb_idx_translate_t idx_trans_tbl; /*!< index translation
  43. table between MySQL and
  44. Innodb */
  45. dict_table_t* ib_table;
  46. } INNOBASE_SHARE;
  47. /** InnoDB B-tree index */
  48. struct dict_index_struct;
  49. /** Prebuilt structures in an Innobase table handle used within MySQL */
  50. struct row_prebuilt_struct;
  51. /** InnoDB B-tree index */
  52. typedef struct dict_index_struct dict_index_t;
  53. /** Prebuilt structures in an Innobase table handle used within MySQL */
  54. typedef struct row_prebuilt_struct row_prebuilt_t;
  55. /** The class defining a handle to an Innodb table */
  56. class ha_innobase: public handler
  57. {
  58. row_prebuilt_t* prebuilt; /*!< prebuilt struct in InnoDB, used
  59. to save CPU time with prebuilt data
  60. structures*/
  61. THD* user_thd; /*!< the thread handle of the user
  62. currently using the handle; this is
  63. set in external_lock function */
  64. THR_LOCK_DATA lock;
  65. INNOBASE_SHARE* share; /*!< information for MySQL
  66. table locking */
  67. uchar* upd_buf; /*!< buffer used in updates */
  68. ulint upd_buf_size; /*!< the size of upd_buf in bytes */
  69. uchar srch_key_val1[MAX_KEY_LENGTH + MAX_REF_PARTS*2];
  70. uchar srch_key_val2[MAX_KEY_LENGTH + MAX_REF_PARTS*2];
  71. /*!< buffers used in converting
  72. search key values from MySQL format
  73. to InnoDB format. For each column
  74. 2 bytes are used to store length,
  75. hence MAX_REF_PARTS*2. */
  76. Table_flags int_table_flags;
  77. uint primary_key;
  78. ulong start_of_scan; /*!< this is set to 1 when we are
  79. starting a table scan but have not
  80. yet fetched any row, else 0 */
  81. uint last_match_mode;/* match mode of the latest search:
  82. ROW_SEL_EXACT, ROW_SEL_EXACT_PREFIX,
  83. or undefined */
  84. uint num_write_row; /*!< number of write_row() calls */
  85. uint store_key_val_for_row(uint keynr, char* buff, uint buff_len,
  86. const uchar* record);
  87. inline void update_thd(THD* thd);
  88. void update_thd();
  89. int change_active_index(uint keynr);
  90. int general_fetch(uchar* buf, uint direction, uint match_mode);
  91. ulint innobase_lock_autoinc();
  92. ulonglong innobase_peek_autoinc();
  93. ulint innobase_set_max_autoinc(ulonglong auto_inc);
  94. ulint innobase_reset_autoinc(ulonglong auto_inc);
  95. ulint innobase_get_autoinc(ulonglong* value);
  96. ulint innobase_update_autoinc(ulonglong auto_inc);
  97. void innobase_initialize_autoinc();
  98. dict_index_t* innobase_get_index(uint keynr);
  99. int info_low(uint flag, bool called_from_analyze);
  100. /* Init values for the class: */
  101. public:
  102. ha_innobase(handlerton *hton, TABLE_SHARE *table_arg);
  103. ~ha_innobase();
  104. /*
  105. Get the row type from the storage engine. If this method returns
  106. ROW_TYPE_NOT_USED, the information in HA_CREATE_INFO should be used.
  107. */
  108. enum row_type get_row_type() const;
  109. const char* table_type() const;
  110. const char* index_type(uint key_number);
  111. const char** bas_ext() const;
  112. Table_flags table_flags() const;
  113. ulong index_flags(uint idx, uint part, bool all_parts) const;
  114. uint max_supported_keys() const;
  115. uint max_supported_key_length() const;
  116. uint max_supported_key_part_length() const;
  117. const key_map* keys_to_use_for_scanning();
  118. int open(const char *name, int mode, uint test_if_locked);
  119. handler* clone(const char *name, MEM_ROOT *mem_root);
  120. int close(void);
  121. double scan_time();
  122. double read_time(uint index, uint ranges, ha_rows rows);
  123. my_bool is_fake_change_enabled(THD *thd);
  124. bool is_corrupt() const;
  125. int write_row(uchar * buf);
  126. int update_row(const uchar * old_data, uchar * new_data);
  127. int delete_row(const uchar * buf);
  128. bool was_semi_consistent_read();
  129. void try_semi_consistent_read(bool yes);
  130. void unlock_row();
  131. int index_init(uint index, bool sorted);
  132. int index_end();
  133. int index_read(uchar * buf, const uchar * key,
  134. uint key_len, enum ha_rkey_function find_flag);
  135. int index_read_idx(uchar * buf, uint index, const uchar * key,
  136. uint key_len, enum ha_rkey_function find_flag);
  137. int index_read_last(uchar * buf, const uchar * key, uint key_len);
  138. int index_next(uchar * buf);
  139. int index_next_same(uchar * buf, const uchar *key, uint keylen);
  140. int index_prev(uchar * buf);
  141. int index_first(uchar * buf);
  142. int index_last(uchar * buf);
  143. int rnd_init(bool scan);
  144. int rnd_end();
  145. int rnd_next(uchar *buf);
  146. int rnd_pos(uchar * buf, uchar *pos);
  147. void position(const uchar *record);
  148. int info(uint);
  149. int analyze(THD* thd,HA_CHECK_OPT* check_opt);
  150. int optimize(THD* thd,HA_CHECK_OPT* check_opt);
  151. int discard_or_import_tablespace(my_bool discard);
  152. int extra(enum ha_extra_function operation);
  153. int reset();
  154. int external_lock(THD *thd, int lock_type);
  155. int transactional_table_lock(THD *thd, int lock_type);
  156. int start_stmt(THD *thd, thr_lock_type lock_type);
  157. void position(uchar *record);
  158. ha_rows records_in_range(uint inx, key_range *min_key, key_range
  159. *max_key);
  160. ha_rows estimate_rows_upper_bound();
  161. void update_create_info(HA_CREATE_INFO* create_info);
  162. int create(const char *name, register TABLE *form,
  163. HA_CREATE_INFO *create_info);
  164. int truncate();
  165. int delete_table(const char *name);
  166. int rename_table(const char* from, const char* to);
  167. int check(THD* thd, HA_CHECK_OPT* check_opt);
  168. char* update_table_comment(const char* comment);
  169. char* get_foreign_key_create_info();
  170. int get_foreign_key_list(THD *thd, List<FOREIGN_KEY_INFO> *f_key_list);
  171. int get_parent_foreign_key_list(THD *thd,
  172. List<FOREIGN_KEY_INFO> *f_key_list);
  173. bool can_switch_engines();
  174. uint referenced_by_foreign_key();
  175. void free_foreign_key_create_info(char* str);
  176. THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to,
  177. enum thr_lock_type lock_type);
  178. void init_table_handle_for_HANDLER();
  179. virtual void get_auto_increment(ulonglong offset, ulonglong increment,
  180. ulonglong nb_desired_values,
  181. ulonglong *first_value,
  182. ulonglong *nb_reserved_values);
  183. int reset_auto_increment(ulonglong value);
  184. virtual bool get_error_message(int error, String *buf);
  185. uint8 table_cache_type();
  186. /*
  187. ask handler about permission to cache table during query registration
  188. */
  189. my_bool register_query_cache_table(THD *thd, char *table_key,
  190. uint key_length,
  191. qc_engine_callback *call_back,
  192. ulonglong *engine_data);
  193. static char *get_mysql_bin_log_name();
  194. static ulonglong get_mysql_bin_log_pos();
  195. bool primary_key_is_clustered();
  196. int cmp_ref(const uchar *ref1, const uchar *ref2);
  197. /** Fast index creation (smart ALTER TABLE) @see handler0alter.cc @{ */
  198. int add_index(TABLE *table_arg, KEY *key_info, uint num_of_keys,
  199. handler_add_index **add);
  200. int final_add_index(handler_add_index *add, bool commit);
  201. int prepare_drop_index(TABLE *table_arg, uint *key_num,
  202. uint num_of_keys);
  203. int final_drop_index(TABLE *table_arg);
  204. /** @} */
  205. bool check_if_incompatible_data(HA_CREATE_INFO *info,
  206. uint table_changes);
  207. bool check_if_supported_virtual_columns(void) { return TRUE; }
  208. private:
  209. /** Builds a 'template' to the prebuilt struct.
  210. The template is used in fast retrieval of just those column
  211. values MySQL needs in its processing.
  212. @param whole_row true if access is needed to a whole row,
  213. false if accessing individual fields is enough */
  214. void build_template(bool whole_row);
  215. /** Resets a query execution 'template'.
  216. @see build_template() */
  217. inline void reset_template();
  218. public:
  219. /** @name Multi Range Read interface @{ */
  220. /** Initialize multi range read @see DsMrr_impl::dsmrr_init
  221. * @param seq
  222. * @param seq_init_param
  223. * @param n_ranges
  224. * @param mode
  225. * @param buf
  226. */
  227. int multi_range_read_init(RANGE_SEQ_IF* seq,
  228. void* seq_init_param,
  229. uint n_ranges, uint mode,
  230. HANDLER_BUFFER *buf);
  231. /** Process next multi range read @see DsMrr_impl::dsmrr_next
  232. * @param range_info
  233. */
  234. int multi_range_read_next(range_id_t *range_info);
  235. /** Initialize multi range read and get information.
  236. * @see ha_myisam::multi_range_read_info_const
  237. * @see DsMrr_impl::dsmrr_info_const
  238. * @param keyno
  239. * @param seq
  240. * @param seq_init_param
  241. * @param n_ranges
  242. * @param bufsz
  243. * @param flags
  244. * @param cost
  245. */
  246. ha_rows multi_range_read_info_const(uint keyno, RANGE_SEQ_IF *seq,
  247. void *seq_init_param,
  248. uint n_ranges, uint *bufsz,
  249. uint *flags, COST_VECT *cost);
  250. /** Initialize multi range read and get information.
  251. * @see DsMrr_impl::dsmrr_info
  252. * @param keyno
  253. * @param n_ranges
  254. * @param keys
  255. * @param key_parts
  256. * @param bufsz
  257. * @param flags
  258. * @param cost
  259. */
  260. ha_rows multi_range_read_info(uint keyno, uint n_ranges, uint keys,
  261. uint key_parts, uint *bufsz,
  262. uint *flags, COST_VECT *cost);
  263. int multi_range_read_explain_info(uint mrr_mode,
  264. char *str, size_t size);
  265. /** Attempt to push down an index condition.
  266. * @param[in] keyno MySQL key number
  267. * @param[in] idx_cond Index condition to be checked
  268. * @return idx_cond if pushed; NULL if not pushed
  269. */
  270. class Item* idx_cond_push(uint keyno, class Item* idx_cond);
  271. /* An helper function for index_cond_func_innodb: */
  272. bool is_thd_killed();
  273. private:
  274. /** The multi range read session object */
  275. DsMrr_impl ds_mrr;
  276. /* @} */
  277. };
  278. /* Some accessor functions which the InnoDB plugin needs, but which
  279. can not be added to mysql/plugin.h as part of the public interface;
  280. the definitions are bracketed with #ifdef INNODB_COMPATIBILITY_HOOKS */
  281. #ifndef INNODB_COMPATIBILITY_HOOKS
  282. #error InnoDB needs MySQL to be built with #define INNODB_COMPATIBILITY_HOOKS
  283. #endif
  284. extern "C" {
  285. struct charset_info_st *thd_charset(MYSQL_THD thd);
  286. LEX_STRING *thd_query_string(MYSQL_THD thd);
  287. /**
  288. Check if a user thread is a replication slave thread
  289. @param thd user thread
  290. @retval 0 the user thread is not a replication slave thread
  291. @retval 1 the user thread is a replication slave thread
  292. */
  293. int thd_slave_thread(const MYSQL_THD thd);
  294. /**
  295. Check if a user thread is running a non-transactional update
  296. @param thd user thread
  297. @retval 0 the user thread is not running a non-transactional update
  298. @retval 1 the user thread is running a non-transactional update
  299. */
  300. int thd_non_transactional_update(const MYSQL_THD thd);
  301. /**
  302. Get the user thread's binary logging format
  303. @param thd user thread
  304. @return Value to be used as index into the binlog_format_names array
  305. */
  306. int thd_binlog_format(const MYSQL_THD thd);
  307. /**
  308. Mark transaction to rollback and mark error as fatal to a sub-statement.
  309. @param thd Thread handle
  310. @param all TRUE <=> rollback main transaction.
  311. */
  312. void thd_mark_transaction_to_rollback(MYSQL_THD thd, bool all);
  313. /**
  314. Check if binary logging is filtered for thread's current db.
  315. @param thd Thread handle
  316. @retval 1 the query is not filtered, 0 otherwise.
  317. */
  318. bool thd_binlog_filter_ok(const MYSQL_THD thd);
  319. /**
  320. Check if the query may generate row changes which
  321. may end up in the binary.
  322. @param thd Thread handle
  323. @return 1 the query may generate row changes, 0 otherwise.
  324. */
  325. bool thd_sqlcom_can_generate_row_events(const MYSQL_THD thd);
  326. }
  327. /** Get the file name and position of the MySQL binlog corresponding to the
  328. * current commit.
  329. */
  330. extern void mysql_bin_log_commit_pos(THD *thd, ulonglong *out_pos, const char **out_file);
  331. typedef struct trx_struct trx_t;
  332. /********************************************************************//**
  333. @file handler/ha_innodb.h
  334. Converts an InnoDB error code to a MySQL error code and also tells to MySQL
  335. about a possible transaction rollback inside InnoDB caused by a lock wait
  336. timeout or a deadlock.
  337. @return MySQL error code */
  338. extern "C"
  339. int
  340. convert_error_code_to_mysql(
  341. /*========================*/
  342. int error, /*!< in: InnoDB error code */
  343. ulint flags, /*!< in: InnoDB table flags, or 0 */
  344. MYSQL_THD thd); /*!< in: user thread handle or NULL */
  345. /*********************************************************************//**
  346. Allocates an InnoDB transaction for a MySQL handler object.
  347. @return InnoDB transaction handle */
  348. extern "C"
  349. trx_t*
  350. innobase_trx_allocate(
  351. /*==================*/
  352. MYSQL_THD thd); /*!< in: user thread handle */
  353. /*********************************************************************//**
  354. This function checks each index name for a table against reserved
  355. system default primary index name 'GEN_CLUST_INDEX'. If a name
  356. matches, this function pushes an warning message to the client,
  357. and returns true.
  358. @return true if the index name matches the reserved name */
  359. extern "C"
  360. bool
  361. innobase_index_name_is_reserved(
  362. /*============================*/
  363. THD* thd, /*!< in/out: MySQL connection */
  364. const KEY* key_info, /*!< in: Indexes to be created */
  365. ulint num_of_keys); /*!< in: Number of indexes to
  366. be created. */