diff --git a/query_classifier/CMakeLists.txt b/query_classifier/CMakeLists.txt index e934a3b69..d9c6f6b13 100644 --- a/query_classifier/CMakeLists.txt +++ b/query_classifier/CMakeLists.txt @@ -1,6 +1,9 @@ add_library(query_classifier SHARED query_classifier.cc) -target_link_libraries(query_classifier maxscale-common) + +target_link_libraries(query_classifier ${EMBEDDED_LIB} aio crypt crypto dl m ${PCRE_LINK_FLAGS} ssl stdc++ z) set_target_properties(query_classifier PROPERTIES VERSION "1.0.0") +set_target_properties(query_classifier PROPERTIES LINK_FLAGS -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/query_classifier.map) +#set_target_properties(query_classifier PROPERTIES LINK_FLAGS -Wl,-z,defs) install(TARGETS query_classifier COMPONENT lib DESTINATION ${MAXSCALE_LIBDIR}) if(BUILD_TESTS) add_subdirectory(test) diff --git a/query_classifier/query_classifier.cc b/query_classifier/query_classifier.cc index 172892830..b37a90d2d 100644 --- a/query_classifier/query_classifier.cc +++ b/query_classifier/query_classifier.cc @@ -46,6 +46,11 @@ #include #include #include +// In client_settings.h mysql_server_init and mysql_server_end are defined to +// mysql_client_plugin_init and mysql_client_plugin_deinit respectively. +// Those must be undefined, so that we here really call mysql_server_[init|end]. +#undef mysql_server_init +#undef mysql_server_end #include #include #include @@ -1478,14 +1483,13 @@ static parsing_info_t* parsing_info_init(void (*donefun)(void *)) /** Get server handle */ mysql = mysql_init(NULL); - ss_dassert(mysql != NULL); if (mysql == NULL) { MXS_ERROR("Call to mysql_real_connect failed due %d, %s.", mysql_errno(mysql), mysql_error(mysql)); - + ss_dassert(mysql != NULL); goto retblock; } @@ -1770,3 +1774,42 @@ qc_query_op_t qc_get_operation(GWBUF* querybuf) return operation; } + + +bool qc_init(int argc, char** argv, char** groups) +{ + int rc = mysql_library_init(argc, argv, groups); + + if (rc != 0) + { + MXS_ERROR("mysql_library_init() failed. Error code: %d", rc); + } + else + { + MXS_NOTICE("Query classifier initialized."); + } + + return rc == 0; +} + +void qc_end() +{ + mysql_library_end(); +} + +bool qc_thread_init() +{ + bool inited = (mysql_thread_init() == 0); + + if (!inited) + { + MXS_ERROR("mysql_thread_init() failed."); + } + + return inited; +} + +void qc_thread_end() +{ + mysql_thread_end(); +} diff --git a/query_classifier/query_classifier.h b/query_classifier/query_classifier.h index cbfd3072c..525ad6d42 100644 --- a/query_classifier/query_classifier.h +++ b/query_classifier/query_classifier.h @@ -83,6 +83,12 @@ typedef enum #define QUERY_IS_TYPE(mask,type) ((mask & type) == type) +bool qc_init(int argc, char** argv, char** groups); +void qc_end(); + +bool qc_thread_init(); +void qc_thread_end(); + /** * Create THD and use it for creating parse tree. Examine parse tree and * classify the query. diff --git a/query_classifier/query_classifier.map b/query_classifier/query_classifier.map new file mode 100644 index 000000000..04b3716cf --- /dev/null +++ b/query_classifier/query_classifier.map @@ -0,0 +1,20 @@ +{ + global: + qc_end; + qc_get_affected_fields; + qc_get_canonical; + qc_get_created_table_name; + qc_get_database_names; + qc_get_operation; + qc_get_qtype_str; + qc_get_table_names; + qc_get_type; + qc_init; + qc_is_drop_table_query; + qc_is_real_query; + qc_query_has_clause; + qc_thread_end; + qc_thread_init; + local: + *; +}; diff --git a/server/core/CMakeLists.txt b/server/core/CMakeLists.txt index 7d7a03630..e32bc05e9 100644 --- a/server/core/CMakeLists.txt +++ b/server/core/CMakeLists.txt @@ -1,6 +1,6 @@ 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) -target_link_libraries(maxscale-common ${EMBEDDED_LIB} ${LZMA_LINK_FLAGS} ${PCRE2_LIBRARIES} ${PCRE_LINK_FLAGS} ${CURL_LIBRARIES} ssl aio pthread crypt dl crypto inih z rt m stdc++) +target_link_libraries(maxscale-common ${MYSQLCLIENT_LIBRARIES} ${LZMA_LINK_FLAGS} ${PCRE2_LIBRARIES} ${CURL_LIBRARIES} ssl aio pthread crypt dl crypto inih z rt m stdc++) if(WITH_JEMALLOC) target_link_libraries(maxscale-common ${JEMALLOC_LIBRARIES}) @@ -20,15 +20,15 @@ elseif(WITH_TCMALLOC) target_link_libraries(maxscale ${TCMALLOC_LIBRARIES}) endif() -target_link_libraries(maxscale maxscale-common) +target_link_libraries(maxscale maxscale-common query_classifier) install(TARGETS maxscale DESTINATION ${MAXSCALE_BINDIR}) add_executable(maxkeys maxkeys.c) -target_link_libraries(maxkeys maxscale-common) +target_link_libraries(maxkeys maxscale-common query_classifier) install(TARGETS maxkeys DESTINATION ${MAXSCALE_BINDIR}) add_executable(maxpasswd maxpasswd.c) -target_link_libraries(maxpasswd maxscale-common) +target_link_libraries(maxpasswd maxscale-common query_classifier) install(TARGETS maxpasswd DESTINATION ${MAXSCALE_BINDIR}) if(BUILD_TESTS) diff --git a/server/core/gateway.c b/server/core/gateway.c index 9caf27241..41fb828ce 100644 --- a/server/core/gateway.c +++ b/server/core/gateway.c @@ -79,8 +79,9 @@ #include #include -# include -# include +#include +#include +#include #include @@ -990,17 +991,26 @@ static void usage(void) */ void worker_thread_main(void* arg) { - /** Init mysql thread context for use with a mysql handle and a parser */ - if (mysql_thread_init() == 0) + if (qc_thread_init()) { - poll_waitevents(arg); + /** Init mysql thread context for use with a mysql handle and a parser */ + if (mysql_thread_init() == 0) + { + poll_waitevents(arg); - /** Release mysql thread context */ - mysql_thread_end(); + /** Release mysql thread context */ + mysql_thread_end(); + } + else + { + MXS_ERROR("Could not perform thread initialization for MySQL. Exiting thread."); + } + + qc_thread_end(); } else { - MXS_ERROR("Could not perform thread initialization for MySQL. Exiting thread."); + MXS_ERROR("Could not perform thread initialization for query classifier. Exiting thread."); } } @@ -1818,6 +1828,14 @@ int main(int argc, char **argv) } } + if (!qc_init(num_elements, server_options, server_groups)) + { + char* logerr = "Failed to initialise query classifier library."; + print_log_n_stderr(true, true, logerr, logerr, eno); + rc = MAXSCALE_INTERNALERROR; + goto return_main; + } + if (mysql_library_init(num_elements, server_options, server_groups)) { if (!daemon_mode) diff --git a/server/core/poll.c b/server/core/poll.c index b9504bee5..a037e7d82 100644 --- a/server/core/poll.c +++ b/server/core/poll.c @@ -36,6 +36,7 @@ #include #include #include +#include #define PROFILE_POLL 0 diff --git a/server/core/test/CMakeLists.txt b/server/core/test/CMakeLists.txt index 02c48dd76..8fff48cea 100644 --- a/server/core/test/CMakeLists.txt +++ b/server/core/test/CMakeLists.txt @@ -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) -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 ) +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) add_test(Internal-TestAdminUsers test_adminusers) add_test(Internal-TestBuffer test_buffer) add_test(Internal-TestDCB test_dcb) diff --git a/server/modules/filter/test/CMakeLists.txt b/server/modules/filter/test/CMakeLists.txt index efccd4ec4..ddb9f0b35 100644 --- a/server/modules/filter/test/CMakeLists.txt +++ b/server/modules/filter/test/CMakeLists.txt @@ -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) -target_link_libraries(harness maxscale-common) +target_link_libraries(harness_ui maxscale-common query_classifier) +target_link_libraries(harness maxscale-common query_classifier) 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) diff --git a/server/modules/routing/binlog/CMakeLists.txt b/server/modules/routing/binlog/CMakeLists.txt index 2cc50c0a4..8897125d2 100644 --- a/server/modules/routing/binlog/CMakeLists.txt +++ b/server/modules/routing/binlog/CMakeLists.txt @@ -4,7 +4,7 @@ target_link_libraries(binlogrouter maxscale-common) 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) -target_link_libraries(maxbinlogcheck maxscale-common) +target_link_libraries(maxbinlogcheck maxscale-common query_classifier) install(TARGETS maxbinlogcheck DESTINATION bin) diff --git a/server/modules/routing/binlog/test/CMakeLists.txt b/server/modules/routing/binlog/test/CMakeLists.txt index fdf7538a1..ff5392839 100644 --- a/server/modules/routing/binlog/test/CMakeLists.txt +++ b/server/modules/routing/binlog/test/CMakeLists.txt @@ -1,5 +1,5 @@ if(BUILD_TESTS) add_executable(testbinlogrouter testbinlog.c ../blr.c ../blr_slave.c ../blr_master.c ../blr_file.c ../blr_cache.c) - target_link_libraries(testbinlogrouter maxscale-common) + target_link_libraries(testbinlogrouter maxscale-common query_classifier) add_test(TestBinlogRouter ${CMAKE_CURRENT_BINARY_DIR}/testbinlogrouter) endif()