MySQL  8.0.19
Source Code Documentation
rewriter_plugin.cc File Reference
#include "plugin/rewriter/rewriter_plugin.h"
#include "my_config.h"
#include <mysql/plugin_audit.h>
#include <mysql/psi/mysql_thread.h>
#include <stddef.h>
#include <algorithm>
#include <atomic>
#include <new>
#include <mysql/components/my_service.h>
#include <mysql/components/services/log_builtins.h>
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_psi_config.h"
#include "my_sys.h"
#include "mysql/psi/mysql_rwlock.h"
#include "mysqld_error.h"
#include "plugin/rewriter/rewriter.h"
#include "plugin/rewriter/rule.h"
#include "plugin/rewriter/services.h"
#include "template_utils.h"

Functions

static void update_verbose (MYSQL_THD, SYS_VAR *, void *, const void *save)
 Updater function for the status variable ..._verbose. More...
 
static void update_enabled (MYSQL_THD, SYS_VAR *, void *, const void *value)
 Updater function for the status variable ..._enabled. More...
 
static MYSQL_SYSVAR_INT (verbose, sys_var_verbose, PLUGIN_VAR_NOCMDARG, "Tells " PLUGIN_NAME " how verbose it should be.", NULL, update_verbose, 1, 0, 2, 1)
 
static MYSQL_SYSVAR_BOOL (enabled, sys_var_enabled, PLUGIN_VAR_NOCMDARG, "Whether queries should actually be rewritten.", NULL, update_enabled, 1)
 
MYSQL_PLUGIN get_rewriter_plugin_info ()
 
static void init_rewriter_psi_keys ()
 
static bool reload (MYSQL_THD thd)
 Reloads the rules into the in-memory table. More...
 
static bool lock_and_reload (MYSQL_THD thd)
 
bool refresh_rules_table ()
 
static string shorten_query (MYSQL_LEX_STRING query)
 
static void log_nonrewritten_query (MYSQL_THD thd, const uchar *digest_buf, Rewrite_result result)
 Writes a message in the log saying that the query did not get rewritten. More...
 

Variables

static bool needs_initial_load
 We handle recovery in a kind of lazy manner. More...
 
static const size_t MAX_QUERY_LENGTH_IN_LOG = 100
 
static MYSQL_PLUGIN plugin_info
 
static mysql_rwlock_t LOCK_table
 
static Rewriterrewriter
 
static int sys_var_verbose
 Verbose level. More...
 
static bool sys_var_enabled
 Enabled. More...
 
static const mysql_service_registry_treg_srv = nullptr
 
const mysql_service_log_builtins_tlog_bi = nullptr
 accessor built-ins More...
 
const mysql_service_log_builtins_string_tlog_bs = nullptr
 string built-ins More...
 
SYS_VARrewriter_plugin_sys_vars []
 
PSI_rwlock_key key_rwlock_LOCK_table_
 
static PSI_rwlock_info all_rewrite_rwlocks []
 

Status variables for the plugin.

#define PLUGIN_NAME   "Rewriter"
 
static std::atomic< long long > status_var_number_rewritten_queries
 Number of queries that were rewritten. More...
 
static bool status_var_reload_error
 Indicates if there was an error during the last reload. More...
 
static unsigned status_var_number_loaded_rules
 Number of rules loaded in the in-memory table. More...
 
static long long status_var_number_reloads
 Number of times the in-memory table was reloaded. More...
 
static SHOW_VAR rewriter_plugin_status_vars []
 

Plugin declaration.

static struct st_mysql_audit rewrite_query_descriptor
 
 mysql_declare_plugin_end
 
static int rewrite_query_notify (MYSQL_THD thd, mysql_event_class_t event_class, const void *event)
 Entry point to the plugin. More...
 
static int rewriter_plugin_init (MYSQL_PLUGIN plugin_ref)
 
static int rewriter_plugin_deinit (void *)
 
 mysql_declare_plugin (audit_log)
 

Detailed Description

The plugin declaration part of the Rewriter plugin. This file (alone) creates a post-parse query rewrite plugin. All of the functionality is brought in by including rewriter.h.

Macro Definition Documentation

◆ PLUGIN_NAME

#define PLUGIN_NAME   "Rewriter"

Function Documentation

◆ get_rewriter_plugin_info()

MYSQL_PLUGIN get_rewriter_plugin_info ( )

◆ init_rewriter_psi_keys()

static void init_rewriter_psi_keys ( )
static

◆ lock_and_reload()

static bool lock_and_reload ( MYSQL_THD  thd)
static

◆ log_nonrewritten_query()

static void log_nonrewritten_query ( MYSQL_THD  thd,
const uchar digest_buf,
Rewrite_result  result 
)
static

Writes a message in the log saying that the query did not get rewritten.

(Only if verbose level is high enough.)

◆ mysql_declare_plugin()

mysql_declare_plugin ( audit_log  )

◆ MYSQL_SYSVAR_BOOL()

static MYSQL_SYSVAR_BOOL ( enabled  ,
sys_var_enabled  ,
PLUGIN_VAR_NOCMDARG  ,
"Whether queries should actually be rewritten."  ,
NULL  ,
update_enabled  ,
 
)
static

◆ MYSQL_SYSVAR_INT()

static MYSQL_SYSVAR_INT ( verbose  ,
sys_var_verbose  ,
PLUGIN_VAR_NOCMDARG  ,
"Tells " PLUGIN_NAME " how verbose it should be."  ,
NULL  ,
update_verbose  ,
,
,
,
 
)
static

◆ refresh_rules_table()

bool refresh_rules_table ( )

◆ reload()

static bool reload ( MYSQL_THD  thd)
static

Reloads the rules into the in-memory table.

This function assumes that the appropriate lock is already taken and doesn't concern itself with locks.

◆ rewrite_query_notify()

static int rewrite_query_notify ( MYSQL_THD  thd,
mysql_event_class_t  event_class,
const void *  event 
)
static

Entry point to the plugin.

The server calls this function after each parsed query when the plugin is active. The function extracts the digest of the query. If the digest matches an existing rewrite rule, it is executed.

◆ rewriter_plugin_deinit()

static int rewriter_plugin_deinit ( void *  )
static

◆ rewriter_plugin_init()

static int rewriter_plugin_init ( MYSQL_PLUGIN  plugin_ref)
static

◆ shorten_query()

static string shorten_query ( MYSQL_LEX_STRING  query)
static

◆ update_enabled()

static void update_enabled ( MYSQL_THD  ,
SYS_VAR ,
void *  ,
const void *  value 
)
static

Updater function for the status variable ..._enabled.

◆ update_verbose()

static void update_verbose ( MYSQL_THD  ,
SYS_VAR ,
void *  ,
const void *  save 
)
static

Updater function for the status variable ..._verbose.

Variable Documentation

◆ all_rewrite_rwlocks

PSI_rwlock_info all_rewrite_rwlocks[]
static
Initial value:
"LOCK_plugin_rewriter_table_",

◆ key_rwlock_LOCK_table_

PSI_rwlock_key key_rwlock_LOCK_table_

◆ LOCK_table

mysql_rwlock_t LOCK_table
static

◆ log_bi

const mysql_service_log_builtins_t* log_bi = nullptr

accessor built-ins

accessor built-ins

◆ log_bs

const mysql_service_log_builtins_string_t* log_bs = nullptr

string built-ins

◆ MAX_QUERY_LENGTH_IN_LOG

const size_t MAX_QUERY_LENGTH_IN_LOG = 100
static

◆ mysql_declare_plugin_end

mysql_declare_plugin_end

◆ needs_initial_load

bool needs_initial_load
static

We handle recovery in a kind of lazy manner.

If the server was shut down with this plugin installed, we should ideally load the rules into memory right on the spot in rewriter_plugin_init() as the server comes to. But this plugin is relying on transactions to do that and unfortunately the transaction log is not set up at this point. (It's set up about 200 lines further down in init_server_components() at the time of writing.) That's why we simply set this flag in rewriter_plugin_init() and reload in rewrite_query_post_parse() if it is set.

◆ plugin_info

MYSQL_PLUGIN plugin_info
static

◆ reg_srv

const mysql_service_registry_t* reg_srv = nullptr
static

◆ rewrite_query_descriptor

struct st_mysql_audit rewrite_query_descriptor
static
Initial value:

◆ rewriter

Rewriter* rewriter
static

◆ rewriter_plugin_status_vars

SHOW_VAR rewriter_plugin_status_vars[]
static
Initial value:
= {
{PLUGIN_NAME "_number_rewritten_queries",
{PLUGIN_NAME "_reload_error",
pointer_cast<char *>(&status_var_reload_error), SHOW_BOOL,
{PLUGIN_NAME "_number_loaded_rules",
pointer_cast<char *>(&status_var_number_loaded_rules), SHOW_INT,
{PLUGIN_NAME "_number_reloads",
pointer_cast<char *>(&status_var_number_reloads), SHOW_LONGLONG,

◆ rewriter_plugin_sys_vars

SYS_VAR* rewriter_plugin_sys_vars[]
Initial value:

◆ status_var_number_loaded_rules

unsigned status_var_number_loaded_rules
static

Number of rules loaded in the in-memory table.

◆ status_var_number_reloads

long long status_var_number_reloads
static

Number of times the in-memory table was reloaded.

◆ status_var_number_rewritten_queries

std::atomic<long long> status_var_number_rewritten_queries
static

Number of queries that were rewritten.

◆ status_var_reload_error

bool status_var_reload_error
static

Indicates if there was an error during the last reload.

◆ sys_var_enabled

bool sys_var_enabled
static

Enabled.

◆ sys_var_verbose

int sys_var_verbose
static

Verbose level.

SHOW_BOOL
@ SHOW_BOOL
Definition: status_var.h:31
NULL
#define NULL
Definition: types.h:55
status_var_reload_error
static bool status_var_reload_error
Indicates if there was an error during the last reload.
Definition: rewriter_plugin.cc:83
SHOW_LONGLONG
@ SHOW_LONGLONG
shown as unsigned longlong
Definition: status_var.h:34
status_var_number_reloads
static long long status_var_number_reloads
Number of times the in-memory table was reloaded.
Definition: rewriter_plugin.cc:89
rewrite_query_notify
static int rewrite_query_notify(MYSQL_THD thd, mysql_event_class_t event_class, const void *event)
Entry point to the plugin.
Definition: rewriter_plugin.cc:318
MYSQL_SYSVAR
#define MYSQL_SYSVAR(name)
Definition: plugin.h:278
SHOW_SCOPE_GLOBAL
@ SHOW_SCOPE_GLOBAL
Definition: status_var.h:69
PLUGIN_NAME
#define PLUGIN_NAME
Definition: rewriter_plugin.cc:91
MYSQL_AUDIT_PARSE_ALL
#define MYSQL_AUDIT_PARSE_ALL
Definition: plugin_audit.h:214
status_var_number_rewritten_queries
static std::atomic< long long > status_var_number_rewritten_queries
Number of queries that were rewritten.
Definition: rewriter_plugin.cc:80
key_rwlock_LOCK_table_
PSI_rwlock_key key_rwlock_LOCK_table_
Definition: rewriter_plugin.cc:199
verbose
static uint verbose
Definition: mysqlcheck.cc:64
SHOW_INT
@ SHOW_INT
shown as unsigned int
Definition: status_var.h:32
PSI_DOCUMENT_ME
#define PSI_DOCUMENT_ME
Definition: psi_base.h:46
MYSQL_AUDIT_INTERFACE_VERSION
#define MYSQL_AUDIT_INTERFACE_VERSION
Definition: plugin_audit.h:39
status_var_number_loaded_rules
static unsigned status_var_number_loaded_rules
Number of rules loaded in the in-memory table.
Definition: rewriter_plugin.cc:86