Make query_classifier part of core.

The query classifier interface is now built as an integral
part of MaxScale core.
This commit is contained in:
Johan Wikman
2016-02-08 11:26:35 +02:00
parent 834d04ed6c
commit 4a4f22c9a7
14 changed files with 33 additions and 42 deletions

View File

@ -1,4 +1,4 @@
add_library(maxscale-common SHARED adminusers.c atomic.c buffer.c config.c dbusers.c dcb.c filter.c externcmd.c gwbitmask.c gwdirs.c gw_utils.c hashtable.c hint.c housekeeper.c load_utils.c maxscale_pcre2.c memlog.c modutil.c monitor.c poll.c random_jkiss.c resultset.c secrets.c server.c service.c session.c spinlock.c thread.c users.c utils.c ${CMAKE_SOURCE_DIR}/log_manager/log_manager.cc ${CMAKE_SOURCE_DIR}/utils/skygw_utils.cc statistics.c)
add_library(maxscale-common SHARED adminusers.c atomic.c buffer.c config.c dbusers.c dcb.c filter.c externcmd.c gwbitmask.c gwdirs.c gw_utils.c hashtable.c hint.c housekeeper.c load_utils.c maxscale_pcre2.c memlog.c modutil.c monitor.c query_classifier.c poll.c random_jkiss.c resultset.c secrets.c server.c service.c session.c spinlock.c thread.c users.c utils.c ${CMAKE_SOURCE_DIR}/log_manager/log_manager.cc ${CMAKE_SOURCE_DIR}/utils/skygw_utils.cc statistics.c)
target_link_libraries(maxscale-common ${MARIADB_CONNECTOR_LIBRARIES} ${LZMA_LINK_FLAGS} ${PCRE2_LIBRARIES} ${CURL_LIBRARIES} ssl aio pthread crypt dl crypto inih z rt m stdc++)
@ -21,15 +21,15 @@ elseif(WITH_TCMALLOC)
target_link_libraries(maxscale ${TCMALLOC_LIBRARIES})
endif()
target_link_libraries(maxscale maxscale-common query_classifier)
target_link_libraries(maxscale maxscale-common)
install(TARGETS maxscale DESTINATION ${MAXSCALE_BINDIR})
add_executable(maxkeys maxkeys.c)
target_link_libraries(maxkeys maxscale-common query_classifier)
target_link_libraries(maxkeys maxscale-common)
install(TARGETS maxkeys DESTINATION ${MAXSCALE_BINDIR})
add_executable(maxpasswd maxpasswd.c)
target_link_libraries(maxpasswd maxscale-common query_classifier)
target_link_libraries(maxpasswd maxscale-common)
install(TARGETS maxpasswd DESTINATION ${MAXSCALE_BINDIR})
if(BUILD_TESTS)

View File

@ -0,0 +1,183 @@
/**
* @section LICENCE
*
* This file is distributed as part of the MariaDB Corporation MaxScale. It is
* free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the
* Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* Copyright MariaDB Corporation Ab
*
* @file
*
*/
#include <query_classifier.h>
#include <log_manager.h>
#include <modules.h>
//#define QC_TRACE_ENABLED
#undef QC_TRACE_ENABLED
#if defined(QC_TRACE_ENABLED)
#define QC_TRACE() MXS_NOTICE(__func__)
#else
#define QC_TRACE()
#endif
static const char default_qc_name[] = "qc_mysqlembedded";
static QUERY_CLASSIFIER* classifier;
bool qc_init(const char* plugin_name)
{
QC_TRACE();
ss_dassert(!classifier);
if (!plugin_name || (*plugin_name == 0))
{
MXS_NOTICE("No query classifier specified, using default '%s'.", default_qc_name);
plugin_name = default_qc_name;
}
bool success = false;
void* module = load_module(plugin_name, MODULE_QUERY_CLASSIFIER);
if (module)
{
classifier = (QUERY_CLASSIFIER*) module;
MXS_NOTICE("%s loaded.", plugin_name);
success = classifier->qc_init();
}
else
{
MXS_ERROR("Could not load %s.", plugin_name);
}
return success;
}
void qc_end(void)
{
QC_TRACE();
ss_dassert(classifier);
classifier->qc_end();
classifier = NULL;
}
bool qc_thread_init(void)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_thread_init();
}
void qc_thread_end(void)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_thread_end();
}
qc_query_type_t qc_get_type(GWBUF* query)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_get_type(query);
}
qc_query_op_t qc_get_operation(GWBUF* query)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_get_operation(query);
}
char* qc_get_created_table_name(GWBUF* query)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_get_created_table_name(query);
}
bool qc_is_drop_table_query(GWBUF* query)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_is_drop_table_query(query);
}
bool qc_is_real_query(GWBUF* query)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_is_real_query(query);
}
char** qc_get_table_names(GWBUF* query, int* tblsize, bool fullnames)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_get_table_names(query, tblsize, fullnames);
}
char* qc_get_canonical(GWBUF* query)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_get_canonical(query);
}
bool qc_query_has_clause(GWBUF* query)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_query_has_clause(query);
}
char* qc_get_qtype_str(qc_query_type_t qtype)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_get_qtype_str(qtype);
}
char* qc_get_affected_fields(GWBUF* query)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_get_affected_fields(query);
}
char** qc_get_database_names(GWBUF* query, int* sizep)
{
QC_TRACE();
ss_dassert(classifier);
return classifier->qc_get_database_names(query, sizep);
}

View File

@ -15,22 +15,22 @@ add_executable(test_users testusers.c)
add_executable(testfeedback testfeedback.c)
add_executable(testmaxscalepcre2 testmaxscalepcre2.c)
add_executable(testmemlog testmemlog.c)
target_link_libraries(test_adminusers maxscale-common query_classifier)
target_link_libraries(test_buffer maxscale-common query_classifier)
target_link_libraries(test_dcb maxscale-common query_classifier)
target_link_libraries(test_filter maxscale-common query_classifier)
target_link_libraries(test_hash maxscale-common query_classifier)
target_link_libraries(test_hint maxscale-common query_classifier)
target_link_libraries(test_modutil maxscale-common query_classifier)
target_link_libraries(test_mysql_users MySQLClient maxscale-common query_classifier)
target_link_libraries(test_poll maxscale-common query_classifier)
target_link_libraries(test_server maxscale-common query_classifier)
target_link_libraries(test_service maxscale-common query_classifier)
target_link_libraries(test_spinlock maxscale-common query_classifier)
target_link_libraries(test_users maxscale-common query_classifier)
target_link_libraries(testfeedback maxscale-common query_classifier)
target_link_libraries(testmaxscalepcre2 maxscale-common query_classifier)
target_link_libraries(testmemlog maxscale-common query_classifier)
target_link_libraries(test_adminusers maxscale-common)
target_link_libraries(test_buffer maxscale-common)
target_link_libraries(test_dcb maxscale-common)
target_link_libraries(test_filter maxscale-common)
target_link_libraries(test_hash maxscale-common)
target_link_libraries(test_hint maxscale-common)
target_link_libraries(test_modutil maxscale-common)
target_link_libraries(test_mysql_users MySQLClient maxscale-common)
target_link_libraries(test_poll maxscale-common)
target_link_libraries(test_server maxscale-common)
target_link_libraries(test_service maxscale-common)
target_link_libraries(test_spinlock maxscale-common)
target_link_libraries(test_users maxscale-common)
target_link_libraries(testfeedback maxscale-common)
target_link_libraries(testmaxscalepcre2 maxscale-common)
target_link_libraries(testmemlog maxscale-common)
add_test(Internal-TestAdminUsers test_adminusers)
add_test(Internal-TestBuffer test_buffer)
add_test(Internal-TestDCB test_dcb)

View File

@ -0,0 +1,126 @@
#ifndef QUERY_CLASSIFIER_HG
#define QUERY_CLASSIFIER_HG
/*
This file is distributed as part of the MariaDB Corporation MaxScale. It is free
software: you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation,
version 2.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Copyright MariaDB Corporation Ab
*/
#include <skygw_utils.h>
#include <buffer.h>
EXTERN_C_BLOCK_BEGIN
typedef enum
{
QUERY_TYPE_UNKNOWN = 0x000000, /*< Initial value, can't be tested bitwisely */
QUERY_TYPE_LOCAL_READ = 0x000001, /*< Read non-database data, execute in MaxScale:any */
QUERY_TYPE_READ = 0x000002, /*< Read database data:any */
QUERY_TYPE_WRITE = 0x000004, /*< Master data will be modified:master */
QUERY_TYPE_MASTER_READ = 0x000008, /*< Read from the master:master */
QUERY_TYPE_SESSION_WRITE = 0x000010, /*< Session data will be modified:master or all */
/** Not implemented yet */
//QUERY_TYPE_USERVAR_WRITE = 0x000020, /*< Write a user variable:master or all */
QUERY_TYPE_USERVAR_READ = 0x000040, /*< Read a user variable:master or any */
QUERY_TYPE_SYSVAR_READ = 0x000080, /*< Read a system variable:master or any */
/** Not implemented yet */
//QUERY_TYPE_SYSVAR_WRITE = 0x000100, /*< Write a system variable:master or all */
QUERY_TYPE_GSYSVAR_READ = 0x000200, /*< Read global system variable:master or any */
QUERY_TYPE_GSYSVAR_WRITE = 0x000400, /*< Write global system variable:master or all */
QUERY_TYPE_BEGIN_TRX = 0x000800, /*< BEGIN or START TRANSACTION */
QUERY_TYPE_ENABLE_AUTOCOMMIT = 0x001000, /*< SET autocommit=1 */
QUERY_TYPE_DISABLE_AUTOCOMMIT = 0x002000, /*< SET autocommit=0 */
QUERY_TYPE_ROLLBACK = 0x004000, /*< ROLLBACK */
QUERY_TYPE_COMMIT = 0x008000, /*< COMMIT */
QUERY_TYPE_PREPARE_NAMED_STMT = 0x010000, /*< Prepared stmt with name from user:all */
QUERY_TYPE_PREPARE_STMT = 0x020000, /*< Prepared stmt with id provided by server:all */
QUERY_TYPE_EXEC_STMT = 0x040000, /*< Execute prepared statement:master or any */
QUERY_TYPE_CREATE_TMP_TABLE = 0x080000, /*< Create temporary table:master (could be all) */
QUERY_TYPE_READ_TMP_TABLE = 0x100000, /*< Read temporary table:master (could be any) */
QUERY_TYPE_SHOW_DATABASES = 0x200000, /*< Show list of databases */
QUERY_TYPE_SHOW_TABLES = 0x400000 /*< Show list of tables */
} qc_query_type_t;
typedef enum
{
QUERY_OP_UNDEFINED = 0,
QUERY_OP_SELECT = (1 << 0),
QUERY_OP_UPDATE = (1 << 1),
QUERY_OP_INSERT = (1 << 2),
QUERY_OP_DELETE = (1 << 3),
QUERY_OP_INSERT_SELECT = (1 << 4),
QUERY_OP_TRUNCATE = (1 << 5),
QUERY_OP_ALTER_TABLE = (1 << 6),
QUERY_OP_CREATE_TABLE = (1 << 7),
QUERY_OP_CREATE_INDEX = (1 << 8),
QUERY_OP_DROP_TABLE = (1 << 9),
QUERY_OP_DROP_INDEX = (1 << 10),
QUERY_OP_CHANGE_DB = (1 << 11),
QUERY_OP_LOAD = (1 << 12)
} qc_query_op_t;
#define QUERY_IS_TYPE(mask,type) ((mask & type) == type)
bool qc_init(const char* plugin_name);
void qc_end(void);
bool qc_thread_init(void);
void qc_thread_end(void);
/**
* Create THD and use it for creating parse tree. Examine parse tree and
* classify the query.
*/
qc_query_type_t qc_get_type(GWBUF* querybuf);
qc_query_op_t qc_get_operation(GWBUF* querybuf);
char* qc_get_created_table_name(GWBUF* querybuf);
bool qc_is_drop_table_query(GWBUF* querybuf);
bool qc_is_real_query(GWBUF* querybuf);
char** qc_get_table_names(GWBUF* querybuf, int* tblsize, bool fullnames);
char* qc_get_canonical(GWBUF* querybuf);
bool qc_query_has_clause(GWBUF* buf);
char* qc_get_qtype_str(qc_query_type_t qtype);
char* qc_get_affected_fields(GWBUF* buf);
char** qc_get_database_names(GWBUF* querybuf, int* size);
typedef struct query_classifier
{
bool (*qc_init)(void);
void (*qc_end)(void);
bool (*qc_thread_init)(void);
void (*qc_thread_end)(void);
qc_query_type_t (*qc_get_type)(GWBUF* querybuf);
qc_query_op_t (*qc_get_operation)(GWBUF* querybuf);
char* (*qc_get_created_table_name)(GWBUF* querybuf);
bool (*qc_is_drop_table_query)(GWBUF* querybuf);
bool (*qc_is_real_query)(GWBUF* querybuf);
char** (*qc_get_table_names)(GWBUF* querybuf, int* tblsize, bool fullnames);
char* (*qc_get_canonical)(GWBUF* querybuf);
bool (*qc_query_has_clause)(GWBUF* buf);
char* (*qc_get_qtype_str)(qc_query_type_t qtype);
char* (*qc_get_affected_fields)(GWBUF* buf);
char** (*qc_get_database_names)(GWBUF* querybuf, int* size);
} QUERY_CLASSIFIER;
#define QUERY_CLASSIFIER_VERSION {1, 0, 0}
EXTERN_C_BLOCK_END
#endif

View File

@ -2,7 +2,7 @@ if(BUILD_RABBITMQ)
if(RABBITMQ_FOUND)
include_directories(${RABBITMQ_HEADERS})
add_library(mqfilter SHARED mqfilter.c)
target_link_libraries(mqfilter query_classifier maxscale-common ${RABBITMQ_LIBRARIES})
target_link_libraries(mqfilter maxscale-common ${RABBITMQ_LIBRARIES})
add_dependencies(mqfilter pcre2)
install(TARGETS mqfilter DESTINATION ${MAXSCALE_LIBDIR})
else()
@ -37,7 +37,7 @@ set_target_properties(topfilter PROPERTIES VERSION "1.0.1")
install(TARGETS topfilter DESTINATION ${MAXSCALE_LIBDIR})
add_library(dbfwfilter SHARED dbfwfilter.c)
target_link_libraries(dbfwfilter maxscale-common query_classifier)
target_link_libraries(dbfwfilter maxscale-common)
set_target_properties(dbfwfilter PROPERTIES VERSION "1.0.0")
install(TARGETS dbfwfilter DESTINATION ${MAXSCALE_LIBDIR})
@ -49,7 +49,7 @@ install(TARGETS namedserverfilter DESTINATION ${MAXSCALE_LIBDIR})
if(BUILD_SLAVELAG)
add_library(slavelag SHARED slavelag.c)
target_link_libraries(slavelag maxscale-common query_classifier)
target_link_libraries(slavelag maxscale-common)
set_target_properties(slavelag PROPERTIES VERSION "1.1.0")
install(TARGETS slavelag DESTINATION ${MAXSCALE_LIBDIR})
endif()
@ -57,7 +57,7 @@ endif()
if(BUILD_TOOLS)
add_executable(ruleparser dbfwfilter.c)
target_compile_definitions(ruleparser PUBLIC "BUILD_RULE_PARSER")
target_link_libraries(ruleparser maxscale-common query_classifier)
target_link_libraries(ruleparser maxscale-common)
install(TARGETS ruleparser DESTINATION ${MAXSCALE_BINDIR})
endif()

View File

@ -1,8 +1,8 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_executable(harness_ui harness_ui.c harness_common.c)
add_executable(harness harness_util.c harness_common.c)
target_link_libraries(harness_ui maxscale-common query_classifier)
target_link_libraries(harness maxscale-common query_classifier)
target_link_libraries(harness_ui maxscale-common)
target_link_libraries(harness maxscale-common)
execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${ERRMSG} ${CMAKE_CURRENT_BINARY_DIR})
execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/harness.cnf ${CMAKE_CURRENT_BINARY_DIR})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/testdriver.sh ${CMAKE_CURRENT_BINARY_DIR}/testdriver.sh @ONLY)

View File

@ -6,7 +6,7 @@ install(TARGETS binlogrouter DESTINATION ${MAXSCALE_LIBDIR})
add_executable(maxbinlogcheck maxbinlogcheck.c blr_file.c blr_cache.c blr_master.c blr_slave.c blr.c)
# maxbinlogcheck refers to my_uuid_init and my_uuin. They are non-public functions and
# should not be used. They are found only from the embedded lib.
target_link_libraries(maxbinlogcheck maxscale-common query_classifier ${MYSQL_EMBEDDED_LIBRARIES})
target_link_libraries(maxbinlogcheck maxscale-common ${MYSQL_EMBEDDED_LIBRARIES})
install(TARGETS maxbinlogcheck DESTINATION bin)

View File

@ -2,6 +2,6 @@ if(BUILD_TESTS)
add_executable(testbinlogrouter testbinlog.c ../blr.c ../blr_slave.c ../blr_master.c ../blr_file.c ../blr_cache.c)
# testbinlogrouter refers to my_uuid_init and my_uuin. They are non-public functions and
# should not be used. They are found only from the embedded lib.
target_link_libraries(testbinlogrouter maxscale-common query_classifier ${MYSQL_EMBEDDED_LIBRARIES})
target_link_libraries(testbinlogrouter maxscale-common ${MYSQL_EMBEDDED_LIBRARIES})
add_test(TestBinlogRouter ${CMAKE_CURRENT_BINARY_DIR}/testbinlogrouter)
endif()

View File

@ -1,5 +1,5 @@
add_library(readwritesplit SHARED readwritesplit.c)
target_link_libraries(readwritesplit maxscale-common query_classifier)
target_link_libraries(readwritesplit maxscale-common)
set_target_properties(readwritesplit PROPERTIES VERSION "1.0.2")
install(TARGETS readwritesplit DESTINATION ${MAXSCALE_LIBDIR})
if(BUILD_TESTS)

View File

@ -1,11 +1,11 @@
add_library(schemarouter SHARED schemarouter.c sharding_common.c)
target_link_libraries(schemarouter maxscale-common query_classifier)
target_link_libraries(schemarouter maxscale-common)
add_dependencies(schemarouter pcre2)
set_target_properties(schemarouter PROPERTIES VERSION "1.0.0")
install(TARGETS schemarouter DESTINATION ${MAXSCALE_LIBDIR})
add_library(shardrouter SHARED shardrouter.c svcconn.c sharding_common.c)
target_link_libraries(shardrouter maxscale-common query_classifier)
target_link_libraries(shardrouter maxscale-common)
add_dependencies(shardrouter pcre2)
set_target_properties(shardrouter PROPERTIES VERSION "1.0.0")
install(TARGETS shardrouter DESTINATION ${MAXSCALE_LIBDIR})