MySQL存储引擎-初始化
引言
万丈高楼平地起,由简至繁也是一般的学习方式。若直接分析innodb与Server层的交互,
其复杂性让人望而却步。CSV是MySQL中的文本存储引擎,数据直接以逗号分割以行的
形式存于文本中,并且其实现的Server层功能很有限,非常适合入手。
engine与Server
MySQL架构不多做说明,网上有许多资料。这里核心关注Server与engine的交互。
交互涉及两个核心的数据结构对象如下:
struct handlerton{}; //handler.h
class handler :public Sql_alloc{}; //handler.h
handlerton
engine级别对象,在Server中每个engine只存在单个handlerton实例对象[单例模式],
提供对engine引擎级别的功能的操作。结构中多数成员是函数,大概有50多个函数成员,
功能比较杂:
- savepoint
- transaction
- Serialized Dictionary Information.了解https://yq.aliyun.com/articles/600183
- binlog,files,tablespace,schema,tmp files
- connection
- create handler
- cost,代价评估相关
- other
虽说handlerton功能繁杂,CSV真正自己实现的函数只一个create handler核心的操作。
看如下engine初始化函数,其他未实现的函数默认为空,即为为支持的功能。
//ha_tina.cc
static int tina_init_func(void *p)
{
handlerton *tina_hton;
#ifdef HAVE_PSI_INTERFACE
init_tina_psi_keys();
#endif
tina_hton= (handlerton *)p;
mysql_mutex_init(csv_key_mutex_tina, &tina_mutex, MY_MUTEX_INIT_FAST);
(void) my_hash_init(&tina_open_tables,system_charset_info,32,0,
tina_get_key, nullptr, 0,
csv_key_memory_tina_share);
tina_hton->state= SHOW_OPTION_YES;
tina_hton->db_type= DB_TYPE_CSV_DB;
tina_hton->create= tina_create_handler;
tina_hton->flags= (HTON_CAN_RECREATE | HTON_SUPPORT_LOG_TABLES |
HTON_NO_PARTITION);
tina_hton->file_extensions= ha_tina_exts;
tina_hton->rm_tmp_tables= default_rm_tmp_tables;
return 0;
}
handler
handler是Server与engine交互的核心接口,handler类中多数成员函数都是虚函数,
由底层engine实现。handler.h文件中handler类说明注释高达400行,可见其重要性与复杂性,
这里不再详述,请阅读说明注释。若对ha_tina详细说明,就是对CSV引擎实现的详细分析,
这里不深入探究,但分析其中几个实现,了解handlerton与handler以及Server与engine的交互
处理过程。
//ha_tina.h
class ha_tina: public handler
CSV engine插件如何加载
CSV引擎在ha_tina.cc文件中,通过plugin.h中的宏操作定义了一个st_mysql_plugin对象。
tina_init_func函数实现对handlerton的初始化操作。
如下代码所示:
mysql_declare_plugin(csv) /* plugin.h */
{
MYSQL_STORAGE_ENGINE_PLUGIN,
&csv_storage_engine,
"CSV",
"Brian Aker, MySQL AB",
"CSV storage engine",
PLUGIN_LICENSE_GPL,
tina_init_func, /* Plugin Init */
tina_done_func, /* Plugin Deinit */
0x0100 /* 1.0 */,
NULL, /* status variables */
NULL, /* system variables */
NULL, /* config options */
0, /* flags */
}
mysql_declare_plugin_end; /* plugin.h */
````
实际编译环境中,利用cmake工具将其赋值到全局环境变量,并生成文件。
对CMake工具有基本的了解,便于分析。过程略显复杂,如下:
- cmake/plugin.cmake
定义MYSQL_ADD_PLUGIN宏[*类似C++中宏*]
- storage/csv/CMakeLists.txt
调用宏MYSQL_ADD_PLUGIN,对变量mysql_mandatory_plugins赋值
- CMakeFiles/Makefile.cmake
调用configure_file命令以sql/sql_builtin.cc.in文件输入,
文件中引用变量mysql_mandatory_plugins,将变量替换,并生成文件sql/sql_builtin.cc
- sql/sql_builtin.cc
初始化sql/plugin.cc中mysql_mandatory_plugins变量
- sql/mysqld.cc
mysqld启动初始化过程中,调用函数init_server_components,调用内置插件注册函数,
将builtin plugin注册到plugin_hash并进行初始化,即CSV引擎中的函数tina_init_func。
具体调用函数如下:
<div class="se-preview-section-delimiter"></div>
```C++
init_server_components(); //mysqld.cc
plugin_register_builtin_and_init_core_se(); //sql_plugin.cc
register_builtin();
//sql_plugin.cc
//将插件存入全局插件hash表中,plugin_hash
plugin_initialize();
//sql_plugin.cc,调用handlerton初始化函数
//对于CSV引擎,即为 tina_init_func
总结
至此,简单梳理Mysql中内置engine的初始化流程, 由于CSV引擎简单,整体流程比较简单晰。
现在对CSV的来龙去脉有了一定了解,下节将分析show engines执行过程,
探索简单SQL执行流程。