From 13893cca3dfa0f0cef7b3454bc31353c0ce0d0c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Sat, 16 Jun 2018 05:29:50 +0300 Subject: [PATCH] MXS-1914: Move maxscale_shutdown() into the core The core library now contains the maxscale_shutdown() command. This makes it possible to resolve all symbols at link time even for administrative modules. --- include/maxscale/log_manager.h | 12 ++++++ server/core/gateway.cc | 72 +--------------------------------- server/core/log_manager.cc | 28 +++++++++++++ server/core/misc.cc | 19 +++++++++ 4 files changed, 61 insertions(+), 70 deletions(-) diff --git a/include/maxscale/log_manager.h b/include/maxscale/log_manager.h index e845d3b77..b0d90a498 100644 --- a/include/maxscale/log_manager.h +++ b/include/maxscale/log_manager.h @@ -96,6 +96,18 @@ typedef struct mxs_log_throttling bool mxs_log_init(const char* ident, const char* logdir, mxs_log_target_t target); void mxs_log_finish(void); +/** + * Start log flushing thread + * + * @return True if log flusher thread was started + */ +bool mxs_log_start_flush_thr(); + +/** + * Stop log flushing thread + */ +void mxs_log_stop_flush_thr(); + int mxs_log_flush(); int mxs_log_flush_sync(); int mxs_log_rotate(); diff --git a/server/core/gateway.cc b/server/core/gateway.cc index 78774962b..27b439618 100644 --- a/server/core/gateway.cc +++ b/server/core/gateway.cc @@ -90,11 +90,6 @@ static bool datadir_defined = false; /*< If the datadir was already set */ static char pidfile[PATH_MAX + 1] = ""; static int pidfd = PIDFD_CLOSED; -/** - * exit flag for log flusher. - */ -static bool do_exit = false; - /** * If MaxScale is started to run in daemon process the value is true. */ @@ -146,8 +141,6 @@ static bool unload_modules_at_exit = true; static std::string redirect_output_to; static int cnf_preparser(void* data, const char* section, const char* name, const char* value); -static void log_flush_shutdown(void); -static void log_flush_cb(void* arg); static int write_pid_file(); /* write MaxScale pidfile */ static void unlink_pidfile(void); /* remove pidfile */ static void unlock_pidfile(); @@ -1420,7 +1413,6 @@ int main(int argc, char **argv) int child_status; char* cnf_file_path = NULL; /*< conf file, to be freed */ char* cnf_file_arg = NULL; /*< conf filename from cmd-line arg */ - THREAD log_flush_thr; char* tmp_path; int option_index; MXS_CONFIG* cnf = config_get_global_options(); @@ -1428,7 +1420,6 @@ int main(int argc, char **argv) int *syslog_enabled = &cnf->syslog; /** Log to syslog */ int *maxlog_enabled = &cnf->maxlog; /** Log with MaxScale */ int *log_to_shm = &cnf->log_to_shm; /** Log to shared memory */ - ssize_t log_flush_timeout_ms = 0; sigset_t sigpipe_mask; sigset_t saved_mask; bool to_stdout = false; @@ -2185,12 +2176,7 @@ int main(int argc, char **argv) goto return_main; } - /*< - * Start periodic log flusher thread. - */ - log_flush_timeout_ms = 1000; - - if (thread_start(&log_flush_thr, log_flush_cb, (void *) &log_flush_timeout_ms, 0) == NULL) + if (!mxs_log_start_flush_thr()) { const char* logerr = "Failed to start log flushing thread."; print_log_n_stderr(true, true, logerr, logerr, 0); @@ -2283,10 +2269,7 @@ int main(int argc, char **argv) Worker::finish(); MessageQueue::finish(); - /*< - * Wait the flush thread. - */ - thread_wait(log_flush_thr); + mxs_log_stop_flush_thr(); /*< Call finish on all modules. */ modules_process_finish(); @@ -2336,57 +2319,6 @@ return_main: return rc; } /*< End of main */ -/*< - * Shutdown MaxScale server - */ -int maxscale_shutdown() -{ - static int n_shutdowns = 0; - - int n = atomic_add(&n_shutdowns, 1); - - if (n == 0) - { - service_shutdown(); - Worker::shutdown_all(); - log_flush_shutdown(); - } - - return n + 1; -} - -static void log_flush_shutdown(void) -{ - do_exit = true; -} - - -/** - * Periodic log flusher to ensure that log buffers are - * written to log even if block buffer used for temporarily - * storing log contents are not full. - * - * @param arg - Flush frequency in milliseconds - * - * - */ -static void log_flush_cb(void* arg) -{ - ssize_t timeout_ms = *(ssize_t *)arg; - struct timespec ts1; - - ts1.tv_sec = timeout_ms / 1000; - ts1.tv_nsec = (timeout_ms % 1000) * 1000000; - - MXS_NOTICE("Started MaxScale log flusher."); - while (!do_exit) - { - mxs_log_flush(); - nanosleep(&ts1, NULL); - } - MXS_NOTICE("Finished MaxScale log flusher."); -} - static void unlock_pidfile() { if (pidfd != PIDFD_CLOSED) diff --git a/server/core/log_manager.cc b/server/core/log_manager.cc index 35a43b151..437bf60ee 100644 --- a/server/core/log_manager.cc +++ b/server/core/log_manager.cc @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -34,6 +35,7 @@ #include #include #include +#include #include #include "internal/mlist.h" @@ -821,6 +823,32 @@ void mxs_log_finish(void) spinlock_release(&lmlock); } +static struct +{ + THREAD thr; + std::atomic running{true}; +} log_flusher; + +static void log_flush_cb(void* arg) +{ + while (log_flusher.running) + { + mxs_log_flush(); + sleep(1); + } +} + +bool mxs_log_start_flush_thr() +{ + return thread_start(&log_flusher.thr, log_flush_cb, NULL, 0); +} + +void mxs_log_stop_flush_thr() +{ + log_flusher.running = false; + thread_wait(log_flusher.thr); +} + static logfile_t* logmanager_get_logfile(logmanager_t* lmgr) { logfile_t* lf; diff --git a/server/core/misc.cc b/server/core/misc.cc index c70b8ac23..d823a3194 100644 --- a/server/core/misc.cc +++ b/server/core/misc.cc @@ -14,7 +14,11 @@ #include #include + +#include + #include "internal/maxscale.h" +#include "internal/service.h" static time_t started; @@ -32,3 +36,18 @@ int maxscale_uptime() { return time(0) - started; } + +int maxscale_shutdown() +{ + static int n_shutdowns = 0; + + int n = atomic_add(&n_shutdowns, 1); + + if (n == 0) + { + service_shutdown(); + mxs::Worker::shutdown_all(); + } + + return n + 1; +}