diff --git a/include/maxscale/query_classifier.h b/include/maxscale/query_classifier.h index 1db463115..9dfab54ad 100644 --- a/include/maxscale/query_classifier.h +++ b/include/maxscale/query_classifier.h @@ -19,6 +19,16 @@ MXS_BEGIN_DECLS #define QUERY_CLASSIFIER_VERSION {1, 1, 0} +/** + * qc_init_kind_t specifies what kind of initialization should be performed. + */ +typedef enum qc_init_kind +{ + QC_INIT_SELF = 0x01, /*< Initialize/finalize the query classifier itself. */ + QC_INIT_PLUGIN = 0x02, /*< Initialize/finalize the plugin. */ + QC_INIT_BOTH = 0x03 +} qc_init_kind_t; + /** * qc_query_type_t defines bits that provide information about a * particular statement. @@ -377,11 +387,14 @@ bool qc_setup(const char* plugin_name, const char* plugin_args); * * MaxScale calls this functions, so plugins should not do that. * + * @param kind What kind of initialization should be performed. + * Combination of qc_init_kind_t. + * * @return True, if the process wide initialization could be performed. * * @see qc_process_end qc_thread_init */ -bool qc_process_init(void); +bool qc_process_init(uint32_t kind); /** * Finalizes the query classifier. @@ -390,9 +403,12 @@ bool qc_process_init(void); * by a call to this function. MaxScale calls this function, so plugins * should not do that. * + * @param kind What kind of finalization should be performed. + * Combination of qc_init_kind_t. + * * @see qc_process_init qc_thread_end */ -void qc_process_end(void); +void qc_process_end(uint32_t kind); /** * Loads a particular query classifier. @@ -426,11 +442,14 @@ void qc_unload(QUERY_CLASSIFIER* classifier); * * MaxScale calls this function, so plugins should not do that. * + * @param kind What kind of initialization should be performed. + * Combination of qc_init_kind_t. + * * @return True if the initialization succeeded, false otherwise. * * @see qc_thread_end */ -bool qc_thread_init(void); +bool qc_thread_init(uint32_t kind); /** * Performs thread finalization needed by the query classifier. @@ -439,9 +458,12 @@ bool qc_thread_init(void); * * MaxScale calls this function, so plugins should not do that. * + * @param kind What kind of finalization should be performed. + * Combination of qc_init_kind_t. + * * @see qc_thread_init */ -void qc_thread_end(void); +void qc_thread_end(uint32_t kind); /** * Parses the statement in the provided buffer and returns a value specifying diff --git a/query_classifier/test/canonical_tests/canonizer.c b/query_classifier/test/canonical_tests/canonizer.c index 580b68955..ad8e5fcba 100644 --- a/query_classifier/test/canonical_tests/canonizer.c +++ b/query_classifier/test/canonical_tests/canonizer.c @@ -47,7 +47,7 @@ int main(int argc, char** argv) set_process_datadir(strdup("/tmp")); qc_setup("qc_sqlite", NULL); - qc_process_init(); + qc_process_init(QC_INIT_BOTH); infile = fopen(argv[1], "rb"); outfile = fopen(argv[2], "wb"); @@ -83,6 +83,6 @@ int main(int argc, char** argv) } fclose(infile); fclose(outfile); - qc_process_end(); + qc_process_end(QC_INIT_BOTH); return 0; } diff --git a/query_classifier/test/classify.c b/query_classifier/test/classify.c index 6815dd151..894672b50 100644 --- a/query_classifier/test/classify.c +++ b/query_classifier/test/classify.c @@ -314,10 +314,10 @@ int main(int argc, char** argv) if (mxs_log_init(NULL, ".", MXS_LOG_TARGET_DEFAULT)) { - if (qc_setup(lib, NULL) && qc_process_init()) + if (qc_setup(lib, NULL) && qc_process_init(QC_INIT_BOTH)) { rc = run(input_name, expected_name); - qc_process_end(); + qc_process_end(QC_INIT_BOTH); } else { diff --git a/query_classifier/test/crash_qc_sqlite.c b/query_classifier/test/crash_qc_sqlite.c index 3c9e3c51a..991b56dbb 100644 --- a/query_classifier/test/crash_qc_sqlite.c +++ b/query_classifier/test/crash_qc_sqlite.c @@ -41,7 +41,7 @@ int main() set_libdir(strdup("../qc_sqlite")); - if (qc_setup("qc_sqlite", NULL) && qc_process_init()) + if (qc_setup("qc_sqlite", NULL) && qc_process_init(QC_INIT_BOTH)) { const char s[] = "SELECT @@global.max_allowed_packet"; @@ -53,7 +53,7 @@ int main() // code generator. qc_parse(stmt); - qc_process_end(); + qc_process_end(QC_INIT_BOTH); rv = EXIT_SUCCESS; } diff --git a/server/core/query_classifier.c b/server/core/query_classifier.c index cf8e372d4..631ee2b5b 100644 --- a/server/core/query_classifier.c +++ b/server/core/query_classifier.c @@ -38,7 +38,6 @@ static const char default_qc_name[] = "qc_sqlite"; static QUERY_CLASSIFIER* classifier; - bool qc_setup(const char* plugin_name, const char* plugin_args) { QC_TRACE(); @@ -67,21 +66,31 @@ bool qc_setup(const char* plugin_name, const char* plugin_args) return (rv == QC_RESULT_OK) ? true : false; } -bool qc_process_init(void) +bool qc_process_init(uint32_t kind) { QC_TRACE(); ss_dassert(classifier); - return classifier->qc_process_init() == 0; + bool rc = true; + + if (kind & QC_INIT_PLUGIN) + { + rc = classifier->qc_process_init() == 0; + } + + return rc; } -void qc_process_end(void) +void qc_process_end(uint32_t kind) { QC_TRACE(); ss_dassert(classifier); - classifier->qc_process_end(); - classifier = NULL; + if (kind & QC_INIT_PLUGIN) + { + classifier->qc_process_end(); + classifier = NULL; + } } QUERY_CLASSIFIER* qc_load(const char* plugin_name) @@ -106,20 +115,30 @@ void qc_unload(QUERY_CLASSIFIER* classifier) // TODO: actually can unload something. } -bool qc_thread_init(void) +bool qc_thread_init(uint32_t kind) { QC_TRACE(); ss_dassert(classifier); - return classifier->qc_thread_init() == 0; + bool rc = true; + + if (kind & QC_INIT_PLUGIN) + { + rc = classifier->qc_thread_init() == 0; + } + + return rc; } -void qc_thread_end(void) +void qc_thread_end(uint32_t kind) { QC_TRACE(); ss_dassert(classifier); - return classifier->qc_thread_end(); + if (kind & QC_INIT_PLUGIN) + { + classifier->qc_thread_end(); + } } qc_parse_result_t qc_parse(GWBUF* query) diff --git a/server/modules/filter/cache/test/testkeygeneration.cc b/server/modules/filter/cache/test/testkeygeneration.cc index 53151e97c..3756428a9 100644 --- a/server/modules/filter/cache/test/testkeygeneration.cc +++ b/server/modules/filter/cache/test/testkeygeneration.cc @@ -127,7 +127,7 @@ int main(int argc, char* argv[]) { if (mxs_log_init(NULL, ".", MXS_LOG_TARGET_DEFAULT)) { - if (qc_setup(NULL, NULL) && qc_process_init()) + if (qc_setup(NULL, NULL) && qc_process_init(QC_INIT_BOTH)) { const char* zModule = argv[1]; @@ -158,7 +158,7 @@ int main(int argc, char* argv[]) cerr << "error: Could not initialize factory." << endl; } - qc_process_end(); + qc_process_end(QC_INIT_BOTH); } else { diff --git a/server/modules/filter/cache/test/testrules.cc b/server/modules/filter/cache/test/testrules.cc index 289535788..68c5754b2 100644 --- a/server/modules/filter/cache/test/testrules.cc +++ b/server/modules/filter/cache/test/testrules.cc @@ -237,12 +237,12 @@ int main() if (mxs_log_init(NULL, ".", MXS_LOG_TARGET_DEFAULT)) { set_libdir(MXS_STRDUP_A("../../../../../query_classifier/qc_sqlite/")); - if (qc_setup("qc_sqlite", "") && qc_process_init()) + if (qc_setup("qc_sqlite", "") && qc_process_init(QC_INIT_BOTH)) { set_libdir(MXS_STRDUP_A("../")); rc = test(); - qc_process_end(); + qc_process_end(QC_INIT_BOTH); } else { diff --git a/server/modules/filter/cache/test/teststorage.cc b/server/modules/filter/cache/test/teststorage.cc index 5a2be1d9f..8f243b85d 100644 --- a/server/modules/filter/cache/test/teststorage.cc +++ b/server/modules/filter/cache/test/teststorage.cc @@ -50,7 +50,7 @@ int TestStorage::run(int argc, char** argv) { if (mxs_log_init(NULL, ".", MXS_LOG_TARGET_DEFAULT)) { - if (qc_setup(NULL, NULL) && qc_process_init()) + if (qc_setup(NULL, NULL) && qc_process_init(QC_INIT_BOTH)) { const char* zModule = NULL; size_t threads = m_threads; @@ -113,6 +113,8 @@ int TestStorage::run(int argc, char** argv) { cerr << "error: Could not initialize factory " << zModule << "." << endl; } + + qc_process_end(QC_INIT_BOTH); } else {