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.
This commit is contained in:
Markus Mäkelä 2018-06-16 05:29:50 +03:00
parent df24f09ce5
commit 13893cca3d
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
4 changed files with 61 additions and 70 deletions

View File

@ -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();

View File

@ -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)

View File

@ -25,6 +25,7 @@
#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>
#include <atomic>
#include <maxscale/alloc.h>
#include <maxscale/atomic.h>
@ -34,6 +35,7 @@
#include <maxscale/platform.h>
#include <maxscale/session.h>
#include <maxscale/spinlock.hh>
#include <maxscale/thread.h>
#include <maxscale/utils.h>
#include "internal/mlist.h"
@ -821,6 +823,32 @@ void mxs_log_finish(void)
spinlock_release(&lmlock);
}
static struct
{
THREAD thr;
std::atomic<bool> 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;

View File

@ -14,7 +14,11 @@
#include <maxscale/maxscale.h>
#include <time.h>
#include <maxscale/worker.hh>
#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;
}