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

MySQL plug-in call

2025-04-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Keep a simple record for study. Please point out if there is any mistake.

1. Core class

Observer_info: observer rpl_handler.h

Class Observer_info {/ / plug-in observer public: void * observer; / / this void pointer is the specific observer. Use the pointer function to implement polymorphic st_plugin_int * plugin_int; plugin_ref plugin Observer_info (void * ob, st_plugin_int * p);}

Actual observer

And void* point to the object, all of which are function pointers, here through the function pointer to the specific function, to achieve the function of the plug-in. The actual function pointed to by the function pointer needs to be implemented by the user.

Trans_observer structure Server_state_observer structure Binlog_transmit_observer structure Binlog_relay_IO_observer structure

In fact, when looking at the specific implementation, search for the names of these structures, and the actual function names are defined if the implementation in the plug-in. As shown in MGR:

Trans_observer trans_observer = {sizeof (Trans_observer), group_replication_trans_before_dml, group_replication_trans_before_commit, group_replication_trans_before_rollback, group_replication_trans_after_commit, group_replication_trans_after_rollback,}

Delegate: delegate base class

It contains

Observer_info_list observer_info_list; / / Observer linked list, that is, a linked list of Observer_info mysql_rwlock_t lock;// read-write lock MEM_ROOT memroot;// memory space bool inited;// initialization

And implemented some general functions, such as adding and deleting plug-ins

The specific delegator inherits from Delegate

Trans_delegate: thing related typedef Trans_observer Observer; Server_state_delegate: server related typedef Server_state_observer Observer;Binlog_transmit_delegate: transport related typedef Binlog_transmit_observer Observer;Binlog_relay_IO_delegate: slave related typedef Binlog_relay_IO_observer Observer; II, registration function

For example, in rpl_handler.cc

Int register_trans_observer (Trans_observer * observer, void * p) {return transaction_delegate- > add_observer (observer, (st_plugin_int *) p);}

Observer has been initialized, just register. It's added to the observer queue. Once the linked list is added, the corresponding function will be executed by traversing the entire linked list in actual use.

Important macros

RUN_HOOK macro

The definition is as follows:

# define RUN_HOOK (group, hook, args)\ (group # # _ delegate- > is_empty ()?\ 0: group # # delegate- > hook args) # define NO_HOOK (group) (group # # _ delegate- > is_empty ())

This macro will be called at the appropriate location in the code in MySQL to enter the logic of the plug-in definition.

FOREACH_OBSERVER macro

The definition is as follows:

# define FOREACH_OBSERVER (r, f, thd, args)\ / * Use a struct to make sure that they are allocated adjacent, check delete_dynamic (). * /\ Prealloced_array plugins (PSI_NOT_INSTRUMENTED);\ / / define an array of plug-ins read_lock ();\ Observer_info_iterator iter= observer_info_iter () \ / / iterator Observer_info * info= iter++;\ / / for (; info Info= iter++)\ {\ plugin_ref plugin=\ my_plugin_lock (0, & info- > plugin) \ if (! plugin)\ {\ / * plugin is not intialized or deleted, this is not an error * /\ r = 0 \ break;\}\ plugins.push_back (plugin) \ if ((Observer *) info- > observer)-> f\ & & ((Observer *) info- > observer)-> f args)\ {\ r = 1 \ sql_print_error ("Run function'" # f "'in plugin'% s' failed",\ info- > plugin_int- > name.str);\ break \}\\

You can actually see that you are traversing the corresponding actual delegate's linked list observer_info_list and executing the corresponding back-table function.

4. An actual column child RUN_HOOK (transaction, before_commit, (thd, all, thd_get_cache_mngr (thd)-> get_binlog_cache_log (true), thd_get_cache_mngr (thd)-> get_binlog_cache_log (false), max (max_binlog_cache_size) Max_binlog_stmt_cache_size))

According to the definition of RUN_HOOK, group # # _ delegate- > hook args is converted to:

Transaction_delegate- > before_commit (thd, all, thd_get_cache_mngr (thd)-> get_binlog_cache_log (true), thd_get_cache_mngr (thd)-> get_binlog_cache_log (false), max (max_binlog_cache_size, max_binlog_stmt_cache_size)

The transaction_delegate here is a global object of the Trans_delegate class that has been initialized and registered with the plug-in. Because Trans_delegate inherits from Delegate, before_commit logic is implemented in Trans_delegate. Calls to macros contained in it

FOREACH_OBSERVER (ret, before_commit, thd, (& param)); / / callback function macro definition will be executed here: (# define FOREACH_OBSERVER (r, f, thd, args))

To make a callback, he actually traverses the observers in the entire transaction_delegate, which are the specific GROUP functions implemented by each plug-in, so this sentence of the FOREACH_ OBSERVER macro

((Observer *) info- > observer)-> f args

Replace it for (Trans_observer *) info- > observer)-> before_commit (& param) where info is an Observer_info object that contains a VOID pointer observer, which can be converted to the desired type, while Trans_observer is a structure where all function pointers before_commit is a function pointer to group_replication_trans_before_commit, and the whole callback process is completed.

5. A call diagram

The call figure is as follows:

RUN_HOOK.png

Author Wechat:

Wechat .jpg

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: 0

*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