MySQL中如何编写Information Schema Plugin

38次阅读
没有评论

共计 9338 个字符,预计需要花费 24 分钟才能阅读完成。

MySQL 中如何编写 Information Schema Plugin,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

1. 什么是 i_s plugin

在 mysql 里面,默认会有一个 information schema(以下简写为 i_s),用于记录一些与元数据或表的模式相关的信息,与其他数据库不一样,在 data 目录下,并没有为 i_s 建立文件夹,这说明,i_s 并不是物理存在的,而是在需要的时候,才会临时创建。这就可以解释为什么 i_s 库中的表的记录总是无法删除或修改。

2. 为什么使用 i_s plugin

虽然 i_s 中定义了丰富的表,但通过 i_s plugin,我们可以将其功能进行扩展,丰富其中的信息,比如,我们可以把关心信息以表的形式展现出来,可以通过引入 MySQL 的内核代码,来监控内核的运行状态,例如锁资源状态、线程状态、table cache 状态等信息。客户端可以通过 sql 来过滤想要的内容,甚至,我们可以在 plugin 中通过 cond 来进行过滤,而无需在层处理。

3. 如何编写 i_s plugin

1) 之前已经介绍过的,这里不在赘述,在 plugin 间通用的包括:

a. plugin 的声明;

b. 添加系统变量 (show /setvariables)

c. 添加状态变量 (show status)

2) 初始化 I_S 插件

函数原型:name_init(void *p)

函数用于初始化插件,包括指定表的模式、创建表、构造表的函数指针等信息,指针 p 会指向一个结构体 st_schema_table,如下表:

初始化的目的是为了填充结构体 st_schema_table,从而确定表的定义,在查询表的时候,调用相应的函数进行记录填充。由于该结构体与内建的 i_s 表是公用的,因此一些字段我们可以直接忽略掉。在编写 plugin 的时候,我们需要填充的内容包括:

Oslash;  Fields_info

Oslash;  Fill_table

2). 初始化表结构 fields_info

Fields_info 结构体为 st_field_info

通常我们会预定义数组,以 NULL 列结束:

ST_FIELD_INFO  is_field[] = {

  {hellip; hellip;},

  hellip; hellip;

{0, 0, MYSQL_TYPE_NULL, 0, 0, 0, 0}

}

3)fill_table()

函数原型:int fill_table(THD *thd, TABLE_LIST *tables, COND *cond);

参数描述:

为了将记录保存到 i_s 表中,这里不的不提到两个函数:field 类的成员函数 store_系列函数和 schema_table_store_record(),前者用来存储数据到表结构体追踪,后者用来将一行存储好的数据放入到临时表中。

store 函数是 Field 类的方法,有 5 个:

其中 my_declimal 类型或者 MYSQL_TIME 等 MySQL 代码内特有的类型,我们都可以通过引入相应的代码来构建结构体。

注意当列被声明为 MY_I_S_MAYBE_NULL 时,需要做一些额外的处理,见之前关于 st_field_info 结构体的介绍。

当 store 数据到 Field 结构体后,我们还需要将其存储到表中,API 函数如下:

boolschema_table_store_record(THD *thd, TABLE *table);

其中 thd 为当前线程,table 为 tables- table

为了包含这两个函数,我们需要引入如下头文件:

#include mysql_priv.h

4) 使用 COND 进行优化

从 fill_table 的函数原型中,我们可以看到结构体 COND,这在 MySQL 层用作对条件进行记录过滤,实际上在 plugin 里,我们可以直接进行过滤,只返回到 MYSQL 层需要的数据。如下图所示:

如果你接触过源代码,会发现 COND 是一个相当复杂的类型,如果由我们自己编写代码来操作显然要耗费大量的精力,我们可以依葫芦画瓢,找到源代码里是如何使用该结构体的,构造相应的参数,就可以直接调用了,这也是 Plugin 的诱人之处,我们可以根据需求引用已有的代码。

MySQL 层代码大多定义在 sql 文件夹下,我们在编译时指定相应的目录即可。

当我们的操作对系统影响比较大时,需要尽快的得到结果,例如,内建的 I_S 表 COLUMNS,在填充数据时需要打开所有的表,如果在 Plugin 层做过滤,那么当我们找到一个不符合条件的表时,尽快关闭,而不是等到 MYSQL 层来过滤后关闭。

例如函数:

bool calc_lookup_values_from_cond(THD *thd,COND *cond, TABLE_LIST *table, LOOKUP_FIELD_VALUES *lookups);

其中 LOOPUP_FIEDL_VALUES 结构体为:

sql/sql_show.cc:

typedef struct st_lookup_field_values

{

LEX_STRING value1, value2;

bool value1_is_wildcard, value2_is_wildcard;

} LOOKUP_FIELD_VALUES;

这个函数用于处理等值的情况,函数将寻找类似 field1 = constant1 和 field2 = constant2 这样的条件, 如果找到了,将被存储在 LOOKUP_FIELD_VALUES 结构体的 value1 和 value2 中:

lookups.value1.str

lookups.value2.str

当我们找到了在 COND 中定义的条件后,就可以进行字符串匹配了。

该函数用于支持 INFORMATION_SCHEMA.TABLES, INFORMATION_ SCHEMA.COLUMNS, 和其他类型的内建 I_S 表,主要用来存储表名和数据库名,也就是说,value 值为 string 类型,并且只支持两个等值操作,如果想实现更复杂的 cond 遍历,我们需要自己来实现。

示例如下(参考自《mysql plugin development》):

view plain

#include mysql_priv.h  

/* 声明相关的结构体和函数 */ 

typedef struct st_lookup_field_values 

LEX_STRING value1, value2; 

bool value1_is_wildcard,value2_is_wildcard; 

} LOOKUP_FIELD_VALUES; 

bool calc_lookup_values_from_cond(THD *thd,COND *cond, 

TABLE_LIST *table, LOOKUP_FIELD_VALUES*lookups); 

bool schema_table_store_record(THD *thd,TABLE *table); 

/* 定义列类型

* 包括一个整型和一个字符串型

*/ 

ST_FIELD_INFO cond_push_fields[] = 

{NUMBER ,10, MYSQL_TYPE_LONG, 0, 0, 0, 0}, 

{TEXT ,100, MYSQL_TYPE_STRING, 0, 0, 0, 0}, 

{0, 0,MYSQL_TYPE_NULL, 0, 0, 0, 0} 

int fill_cond_push(THD *thd, TABLE_LIST*tables, COND *cond) 

  /* 系统默认字符集:utf-8*/ 

CHARSET_INFO *cs= system_charset_info; 

TABLE *table =tables- table; 

/* 字符串数组 output,用于测试只返回符合条件的字符串 */ 

const char**ptr, *output[] = { hello , world , this , is , a , test , 0}; 

int num; 

/* 声明变量 */ 

LOOKUP_FIELD_VALUESlookups; 

bzero((char*) lookups, sizeof(lookups)); 

/* 调用函数获得 COND 中定义的条件 */ 

if (calc_lookup_values_from_cond(thd, cond, tables, lookups)) 

return 0; 

for (num = 0,ptr = output; *ptr; ptr++) 

if (lookups.value1.str  

my_strnncoll(cs, (const uchar*)*ptr, strlen(*ptr), 

(const uchar*)lookups.value1.str, 

lookups.value1.length)) 

continue; 

  /* 只有满足条件的字符串才会被存储到 table 中 */ 

table- field[0]- store(++num); 

table- field[1]- store(*ptr, strlen(*ptr), cs); 

if (schema_table_store_record(thd, table)) 

return 1; 

return 0; 

/* 初始化 i_s plugin*/ 

int cond_push_init(void *p) 

ST_SCHEMA_TABLE*schema = (ST_SCHEMA_TABLE*) p; 

/* 指定表定义 */ 

schema- fields_info= cond_push_fields; 

/* 指定记录填充函数 */ 

schema- fill_table= fill_cond_push; 

schema- idx_field1= 1; 

return 0; 

struct st_mysql_information_schemacond_push= 

{MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION}; 

mysql_declare_plugin(cond_push) 

MYSQL_INFORMATION_SCHEMA_PLUGIN, 

cond_push, 

COND_PUSH , 

AndrewHutchings (Andrew.Hutchings@Sun.COM) , 

A simplecondition pushdown demo table , 

PLUGIN_LICENSE_GPL, 

cond_push_init, 

NULL, 

0x0010, 

NULL, 

NULL, 

NULL 

mysql_declare_plugin_end; 

5) 例子:获取当前 query cache 中的 QUERY 信息(摘自网络,略改)

Query_cache 中的 query 存储在 query_cache- queries 结构体中,这是一个 hash 表,我们可以遍历其中的记录还获得想要的数据,代码如下:

view plain

#include stdlib.h  

#include ctype.h  

/* 内核中一些代码定义在 MYSQL_SERVER 宏中 */ 

#ifndef MYSQL_SERVER 

#define MYSQL_SERVER 

#endif 

/*sql_cache.cc 中包含了全部跟 querycache 相关的代码 */ 

#include sql_cache.cc  

#include mysql_priv.h  

#include mysql/plugin.h  

#include my_global.h  

#include mysql_version.h  

#include my_dir.h  

/* 创建一个子类,query_cache 的成员 queries 为私有变量

class Accessible_Query_Cache : privateQuery_cache {

public:

 HASH *get_queries()

  {

  return this- queries; // query_cache.queries;

  }

};

bool schema_table_store_record(THD *thd,TABLE *table);

#define MAX_STATEMENT_TEXT_LENGTH 32767

#define COLUMN_STATEMENT_ID 0

#define COLUMN_SCHEMA_NAME 1

#define COLUMN_STATEMENT_TEXT 2

#define COLUMN_RESULT_BLOCKS_COUNT 3

#define COLUMN_RESULT_BLOCKS_SIZE 4

#define COLUMN_RESULT_BLOCKS_SIZE_USED 5

/* 定义表结构 */ 

ST_FIELD_INFOmysql_is_cached_queries_fields[]= 

 {STATEMENT_ID , 21, MYSQL_TYPE_LONG, 0, 0, Id}, 

 {SCHEMA_NAME , 64, MYSQL_TYPE_STRING, 0, 0, Schema}, 

 {STATEMENT_TEXT , MAX_STATEMENT_TEXT_LENGTH,MYSQL_TYPE_STRING, 0, 0, Statment text}, 

 {RESULT_BLOCKS_COUNT , 21, MYSQL_TYPE_LONG, 0, 0, CountResult Blocks}, 

 {RESULT_BLOCKS_SIZE , 21, MYSQL_TYPE_LONGLONG, 0, 0, Size Result Blocks}, 

 {RESULT_BLOCKS_SIZE_USED , 21, MYSQL_TYPE_LONGLONG, 0, 0, Size Used Result Blocks}, 

  {0,0, MYSQL_TYPE_STRING, 0, 0, 0} 

}; 

/* 填充数据函数 */ 

static intmysql_is_cached_queries_fill_table(THD *thd, TABLE_LIST *tables, COND *cond) 

  intstatus; 

 CHARSET_INFO *scs= system_charset_info;  /* need this to store field into table */ 

 TABLE *table= (TABLE *)tables- table; 

 Accessible_Query_Cache *qc; 

 HASH *queries; 

 const uchar *query_cache_block_raw; 

 Query_cache_block* query_cache_block; 

 Query_cache_query* query_cache_query; 

 uint result_blocks_count; 

 ulonglong result_blocks_size; 

 ulonglong result_blocks_size_used; 

 Query_cache_block *first_result_block; 

 Query_cache_block *result_block; 

 const char *statement_text; 

 size_t statement_text_length; 

 const char *key; 

 size_t key_length; 

/* 引用 query_cache 全局变量 */ 

  qc= (Accessible_Query_Cache *) query_cache; 

/* 对 query_cache 加锁 */ 

  query_cache.lock(); 

/* 获取 hash*/ 

 queries = qc- get_queries(); 

  /* 遍历 hash 中的所有记录 /

  for(uint i= 0; i queries- records; i++)

  {

  /* 根据索引号获取记录 */ 

  query_cache_block_raw = hash_element(queries, i); 

  query_cache_block = (Query_cache_block*)query_cache_block_raw; 

query_cache_query= query_cache_block- query(); 

  table- field[COLUMN_STATEMENT_ID]- store(i+1, 0); 

  /* 获取 sql 语句 */ 

  statement_text = (const char*)query_cache_query- query(); 

  statement_text_length = strlen(statement_text); 

/* 当超出长度时需要截断 hellip;*/ 

  table- field[COLUMN_STATEMENT_TEXT]- store(  (char*)statement_text 

   ,statement_text_length MAX_STATEMENT_TEXT_LENGTH? 

MAX_STATEMENT_TEXT_LENGTH 

  :statement_text_length 

  , scs 

  ); 

  /* 获取该查询的 key*/ 

  key = (const char*)query_cache_query_get_key(  query_cache_block_raw 

  , key_length  , 0 ); 

key_length =strlen(key+statement_text_length+1)-1; 

  /* 数据库名是 key 的一部分,适当的偏移 key 指针可以得到数据库名 */ 

  table- field[COLUMN_SCHEMA_NAME]- store((char*)key+statement_text_length+1 

  , key_length 

  ,scs  ); 

  /* 获得结果集所占块的个数 */ 

  first_result_block= query_cache_query- result(); 

  if(first_result_block) 

  { 

  /* initialize so we can loop over the result blocks*/ 

  result_block= first_result_block; 

  result_blocks_count = 1; 

  result_blocks_size = result_block- length; 

  result_blocks_size_used = result_block- used; 

  /* loop over the result blocks*/ 

  while((result_block= result_block- next)!=first_result_block) 

  { 

  /* calculate total number of result blocks */ 

  result_blocks_count++; 

  /* calculate total size of result blocks */ 

  result_blocks_size += result_block- length; 

  /* calculate total of used size of result blocks */ 

  result_blocks_size_used += result_block- used; 

  } 

  } 

  else 

  { 

  result_blocks_count = 0; 

  result_blocks_size = 0; 

  result_blocks_size_used = 0; 

  } 

  /* 存储块的个数 */ 

  table- field[COLUMN_RESULT_BLOCKS_COUNT]- store(result_blocks_count ,0); 

  /* 存储总的所占有块的大小 */ 

  table- field[COLUMN_RESULT_BLOCKS_SIZE]- store(result_blocks_size  , 0); 

  /* 存储总的已使用块的大小 */ 

  table- field[COLUMN_RESULT_BLOCKS_SIZE_USED]- store(result_blocks_size_used , 0); 

  /* 将记录存储到表中 */ 

  status = schema_table_store_record(thd, table); 

  if (status) { 

  status= 1; 

  goto cleanup; 

  } 

 status = 0; 

cleanup: 

  query_cache.unlock(); 

 return status; 

static intmysql_is_cached_queries_plugin_init(void *p) 

 ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE *)p; 

 schema- fields_info= mysql_is_cached_queries_fields; 

 schema- fill_table= mysql_is_cached_queries_fill_table; 

 return 0; 

static int mysql_is_cached_queries_plugin_deinit(void*p) 

  return0; 

struct st_mysql_information_schemamysql_is_cached_queries_plugin= 

{MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION}; 

/*

 Plugin library descriptor

*/ 

mysql_declare_plugin(mysql_is_cached_queries) 

 MYSQL_INFORMATION_SCHEMA_PLUGIN, 

  mysql_is_cached_queries_plugin, 

  MYSQL_CACHED_QUERIES , 

  Roland Bouman , 

  Lists all queries in the query cache. , 

 PLUGIN_LICENSE_GPL, 

 mysql_is_cached_queries_plugin_init, /* Plugin Init */ 

 mysql_is_cached_queries_plugin_deinit, /* Plugin Deinit */ 

 0x0010 /* 1.0 */, 

 NULL,  /*status variables  */ 

 NULL,  /*system variables  */ 

 NULL  /*config options  */ 

mysql_declare_plugin_end; 

view plain

/pre pre name= code >

view plain

看完上述内容,你们掌握 MySQL 中如何编写 Information Schema Plugin 的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注丸趣 TV 行业资讯频道,感谢各位的阅读!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-28发表,共计9338字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)