Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write daemon plugin in MySQL

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces how to write daemon plugin in MySQL. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

1. What is DaemonPlugin?

As the name implies, daemon plugin is a plug-in used to run in the background. In the plug-in, we can create some background threads to do something interesting. The famous handlesocket is a daemon plugin. In mysql5.6, the memcached function is also realized through daemon plugin.

two。 Why use DaemonPlugin

Like handlersocket, bold imagination can create unlimited possibilities. The attraction of MySQL Plugin is that it is in the same process space as Mysqld and can take advantage of any function of the mysql kernel. When implementing Handlersocket, it constructs the relevant parameters and directly calls the interface of the storage engine, thus passing through the syntax parsing and optimization part. For the query with simple logic, it can greatly improve the efficiency.

In addition, all plugin provide interfaces for showstatus and show variables commands, so we can use plugin to display some of the information we want, such as the value of global variables within mysql.

In general, daemon plugin can do the following:

1) create background threads to extend the mysql function

2) expand status and variables information

Deamon plugin initializes when mysqld starts and executes the init function, so it is not suitable for communicating with the server, and mysql does not provide any related API

3. How to write daemonplugin

Some of the structures involved here are also common to other types of plugin.

1) st_mysql_plugin

No matter what kind of plugin is declared, at least include the structure

Macros are provided in plugin.h, and we can declare plug-ins through macros:

Mysql_declare_plugin (my_plugin)

{}

{}

……

Mysql_declare_plugin_end

Between two macros, we can declare multiple plug-ins, that is, in a file, we can define multiple Plugin.

Three structures mentioned above need to be defined separately in plugin:

A. St_mysql_daemon

This structure contains only one field, which is used to declare the version information of daemon plugin

Perhaps some students have noticed that there are two version mentioned above, namely, version in st_mysql_plugin and version in st_mysql_daemon, which are different.

St_mysql_plugin.version records the version number of the plugin, which is represented in hexadecimal, the lower 8 bits store the minor version number, and the other stores the major version number.

What is stored in st_mysql_daemon is the version number of the daemonplugin interface, and the interface may change according to different mysql versions.

B. St_mysql_show_var

The structure is as follows:

This structure is used to define the values displayed when show status, and you can see that the last two of the type fields are special relative to the others.

When the type type is SHOW_ARRAY, it indicates that the name field is not a value, but rather points to an array of type st__show_var. The array ends with {0j0lg0}, and the name of the current element becomes the prefix that references the array element name.

When the type type is SHOW_FUNC, the value value is a function pointer, and the parameters include the THD,st_mysql_show_var* of the current thread and a memory region header pointer with a size of 1024 bytes; the purpose of the function is to populate the value of the second field, and buf is used as the memory space to store the construction structure; this allows us to do some calculations first, and then display the results of the calculations.

C. St_mysql_sys_var

The structure body contains a macro MySQL _ PLUGIN_VAR_HEADER, which contains the common part of the variable structure.

Here, MySQL cleverly uses the macro definition of C, for example, when we define a variable:

Struct st_mysql_sys_var* my_sysvars [] = {

MYSQL_SYSVAR (my_var)

NULL}

Expand MYSQL_SYSVAR and take a look:

# define MYSQL_SYSVAR_NAME (name) mysql_sysvar_ # # name

# define MYSQL_SYSVAR (name)\

((struct st_mysql_sys_var *) & (MYSQL_SYSVAR_NAME (name)

Then MYSQL_SYSVAR (my_var) is converted to:

((struct st_mysql_sys_var *) mysql_sysvar_my_var

Therefore, before we do that, we must first create the structure. For different data types, there are many macros to create, which can be divided into two types: a global variable that begins with MYSQL_SYSVAR (can be set global), and a session variable that begins with MYSQL_THDVAR

The parameters in the macro are described as follows:

In the check function, we extract the new value from var and store it in the save pointer. In the update function, we can extract the new value from the save pointer. St_mysql_value is used to extract the structure of the new value. The member is the function pointer, as follows:

For example:

Int a

Var- > val_int (var & a)

Some system variables are allowed to be of type SET or ENUM, which need to be defined by an additional structure st_typelib:

To take a simple example, for example, we want to define an INT variable, which is read-only, that is, it cannot be modified by the set command, with a maximum of 1000000 and a minimum of 0, with a default value of 256:

Static int xx

Static MYSQL_SYSVAR_INT (xx_var, xx, PLUGIN_VAR_READONLY, "a read onlyint var", NULL, NULL,256, 0, 1000000, 10)

For example, if the name of plugin is name, the full name of the variable is name_xx_var. We can assign the system variable through the command line, or we can write it in the configuration file. The variable name is name-xx-var, and the assignment must be divisible by 10, otherwise it will be rejected by mysql.

Define an enumerated type, the session variable

Static const char * mode_names [] = {

"NORMAL", "TURBO", "SUPER", "HYPER", "MEGA"

}

Static TYPELIB modes = {5, NULL,mode_names, NULL}

Static MYSQL_THDVAR_ENUM (mode,PLUGIN_VAR_NOCMDOPT

"one of NORMAL, TURBO, SUPER, HYPER,MEGA"

NULL, NULL, 0, & modes)

This variable belongs to the enumerated type, and each session has its own value and can be modified at run time. Note that when it is a session variable, we need a macro like THDVAR (thd,mode) to get the corresponding variable value.

In addition, there is no need to add mutexes to the system variables in Plugin, and MySQL will automatically add them to us.

Example: start a background thread to monitor the status of the current process every 5 seconds (recorded in log), use system variables to control whether log is recorded, and display the number of records in show status

# include

# include

# include

# include

# include

# include

# include

# define MONITORING_BUFFER1024

/ * the following three variables are declared in sql/mysqld.cc, so extern*/ is required

Extern ulong thread_id; / / current maximum thread id

Extern uint thread_count; / / number of current threads

Maximum number of connections allowed for extern ulong max_connections;//

Static pthread_tmonitoring_thread; / / Thread id

Static int monitoring_file; / / log file fd

Static my_bool monitor_state= 1; / / 1 means logging; 0 means no

Static ulong monitor_num = 0; / / number of background thread loops

Static struct rusage usage

/ * create a system variable, which can be modified through configuration file or set global * /

MYSQL_SYSVAR_BOOL (monitors_state,monitor_state

PLUGIN_VAR_OPCMDARG

"disable monitor if 0nd default TRUE"

NULL, NULL, TRUE)

Struct st_mysql_sys_var*vars_system_var [] = {

MYSQL_SYSVAR (monitors_state)

NULL

}

/ * create a status variable, which can be viewed through showstatus * /

Static structst_mysql_show_var sys_status_var [] =

{

{"monitor_num", (char *) & monitor_num, SHOW_LONG}

{0, 0, 0}

}

/ * Thread function, which will be executed continuously after the background thread starts * /

Pthread_handler_tmonitoring (void * p)

{

Char buffer[MONITORING _ BUFFER]

Char time_str [20]

While (1) {

/ * record every 5 seconds, we can also change 5 to a configurable system variable * /

Sleep (5)

If (! monitor_state)

Continue

Monitor_num++

/ * get the current time, mysql has its own function * /

Get_date (time_str, GETDATE_DATE_TIME,0)

Snprintf (buffer, MONITORING_BUFFER, "% s:% u of% lu clients connected,"

"% lu connections made\ n"

Time_str, thread_count

Max_connections, thread_id)

/ * use the getrusage function to get the running status of the current process, such as man getrusage*/

If (getrusage (RUSAGE_SELF, & usage) = = 0) {

Snprintf (buffer+strlen (buffer))

MONITORING_BUFFER, "user time:%d,system time:%d,"

"maxrss:%d,ixrss:%d,idrss:%d,"

Isrss:%d, minflt:%d, majflt:%d,

"nswap:%d,inblock:%d,oublock:%d,"

Msgsnd:%d, msgrcv:%d,nsignals:%d,

"nvcsw:%d, nivcsw:%d\ n"

Usage.ru_utime

Usage.ru_stime

Usage.ru_maxrss

Usage.ru_ixrss

Usage.ru_idrss

Usage.ru_isrss

Usage.ru_minflt

Usage.ru_majflt

Usage.ru_nswap

Usage.ru_inblock

Usage.ru_oublock

Usage.ru_msgsnd

Usage.ru_msgrcv

Usage.ru_nsignals

Usage.ru_nvcsw

Usage.ru_nivcsw)

/ * write to monitoring_file file * /

Write (monitoring_file, buffer,strlen (buffer))

}

}

}

/ * this function is called when the system starts or loads the plug-in, which is used to create background threads * /

Static int monitoring_plugin_init (void*p)

{

Pthread_attr_t attr

Char monitoring_ filename[FN _ REFLEN]

Char buffer[MONITORING _ BUFFER]

Char time_str [20]

Monitor_num = 0

/ * format the filename

* The fn_format () function is designed tobuild a filename and path compatible

With the current operating system given aset of parameters. More details on its

Functionality can be found inmysys/mf_format.c.

* * /

Fn_format (monitoring_filename, "monitor", "," .log "

MY_REPLACE_EXT | MY_UNPACK_FILENAME)

Unlink (monitoring_filename)

Monitoring_file = open (monitoring_filename

O_CREAT | O_RDWR, 0644)

If (monitoring_file < 0)

{

Fprintf (stderr, "Plugin'monitoring':"

"Could not create file'% s'\ n"

Monitoring_filename)

Return 1

}

Get_date (time_str, GETDATE_DATE_TIME, 0)

Sprintf (buffer, "Monitoring started at%s\ n", time_str)

Write (monitoring_file, buffer,strlen (buffer))

Pthread_attr_init & attr)

Pthread_attr_setdetachstate & attr,PTHREAD_CREATE_JOINABLE)

If (& pthread_create & monitoring_thread,&attr)

Monitoring, NULL)! = 0) {

Fprintf (stderr, "Plugin'monitoring':"

"Could not create monitoringthread!\ n")

Return 1

}

Return 0

}

/ * call * / when uninstalling the plug-in

Static intmonitoring_plugin_deinit (void * p)

{

Char buffer[MONITORING _ BUFFER]

Char time_str [20]

/ * notify the background thread to end * /

Pthread_cancel (monitoring_thread)

Pthread_join (monitoring_thread, NULL)

Get_date (time_str, GETDATE_DATE_TIME, 0)

Sprintf (buffer, "Monitoring stopped at%s\ n", time_str)

Write (monitoring_file, buffer,strlen (buffer))

Close (monitoring_file)

Return 0

}

Struct st_mysql_daemonmonitoring_plugin = {MYSQL_DAEMON_INTERFACE_VERSION}

/ * declare plug-ins * /

Mysql_declare_plugin (monitoring)

{

MYSQL_DAEMON_PLUGIN

& monitoring_plugin

"monitoring"

"yinfeng"

"a daemon montor,log process usagestate"

PLUGIN_LICENSE_GPL

Monitoring_plugin_init

Monitoring_plugin_deinit

0x0100

Sys_status_var

Vars_system_var

NULL

}

Mysql_declare_plugin_end

On how to write daemon plugin in MySQL to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 220

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report