Skip to content

Commit

Permalink
PXC-4433: Create file with current MySQL state
Browse files Browse the repository at this point in the history
https://perconadev.atlassian.net/browse/PXC-4433

server.state file containing current server state is being created
in data directory if the server is started with
create_server_state_file=ON option.
create_server_state_file default value is OFF.
  • Loading branch information
kamil-holubicki committed Sep 12, 2024
1 parent 77bee08 commit b9977ce
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mysys/my_init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
#include "mysys/mysys_priv.h"
#include "mysys_err.h"

#include "sql/server_status_file.h"

#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
Expand Down
1 change: 1 addition & 0 deletions sql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ SET(SQL_SHARED_SOURCES
partition_info.cc
partitioning/partition_handler.cc
persisted_variable.cc
server_status_file.cc
protocol_classic.cc
psi_memory_key.cc
psi_memory_resource.cc
Expand Down
2 changes: 2 additions & 0 deletions sql/dd/impl/bootstrap/bootstrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
#include "sql/sd_notify.h" // sysd::notify
#include "sql/sql_zip_dict.h"
#include "sql/thd_raii.h"
#include "sql/server_status_file.h"
#include "storage/perfschema/pfs_dd_version.h" // PFS_DD_VERSION

using namespace dd;
Expand Down Expand Up @@ -1408,6 +1409,7 @@ bool initialize_dd_properties(THD *thd) {
if (bootstrap::DD_bootstrap_ctx::instance().is_dd_upgrade()) {
LogErr(SYSTEM_LEVEL, ER_DD_UPGRADE, actual_dd_version, dd::DD_VERSION);
sysd::notify("STATUS=Data Dictionary upgrade in progress\n");
Server_status_file::set_status(Server_status_file::Status::UPGRADING);
}
if (bootstrap::DD_bootstrap_ctx::instance().is_server_upgrade()) {
// This condition is hit only if upgrade has been skipped before
Expand Down
5 changes: 5 additions & 0 deletions sql/dd/impl/upgrade/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "sql/thd_raii.h"
#include "sql/trigger.h" // Trigger
#include "sql/trigger_def.h"
#include "sql/server_status_file.h"

typedef ulonglong sql_mode_t;
extern const char *mysql_sys_schema[];
Expand Down Expand Up @@ -977,11 +978,15 @@ bool upgrade_system_schemas(THD *thd) {
LogErr(SYSTEM_LEVEL, ER_SERVER_DOWNGRADE_STATUS, server_version,
MYSQL_VERSION_ID, "started");
sysd::notify("STATUS=Server downgrade in progress\n");
Server_status_file::set_status(Server_status_file::Status::DOWNGRADING);

/* purecov: end */
} else {
LogErr(SYSTEM_LEVEL, ER_SERVER_UPGRADE_STATUS, server_version,
MYSQL_VERSION_ID, "started");
sysd::notify("STATUS=Server upgrade in progress\n");
Server_status_file::set_status(Server_status_file::Status::UPGRADING);

}

bootstrap_error_handler.set_log_error(false);
Expand Down
2 changes: 2 additions & 0 deletions sql/dd/upgrade_57/upgrade.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
#include "sql/table.h"
#include "sql/thd_raii.h"
#include "sql/transaction.h" // trans_rollback
#include "sql/server_status_file.h"

namespace dd {

Expand Down Expand Up @@ -904,6 +905,7 @@ bool do_pre_checks_and_initialize_dd(THD *thd) {
*/
LogErr(SYSTEM_LEVEL, ER_DD_UPGRADE_START);
sysd::notify("STATUS=Data Dictionary upgrade from MySQL 5.7 in progress\n");
Server_status_file::set_status(Server_status_file::Status::UPGRADING);
}

/*
Expand Down
14 changes: 13 additions & 1 deletion sql/mysqld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ MySQL clients support the protocol:
#include "sql/options_mysqld.h" // OPT_THREAD_CACHE_SIZE
#include "sql/partitioning/partition_handler.h" // partitioning_init
#include "sql/persisted_variable.h" // Persisted_variables_cache
#include "sql/server_status_file.h"
#include "sql/plugin_table.h"
#include "sql/protocol.h"
#include "sql/psi_memory_key.h" // key_memory_MYSQL_RELAY_LOG_index
Expand Down Expand Up @@ -1448,6 +1449,8 @@ bool avoid_temporal_upgrade;

bool persisted_globals_load = true;

bool create_server_state_file = false;

bool opt_keyring_operations = true;

bool opt_table_encryption_privilege_check = false;
Expand Down Expand Up @@ -2773,6 +2776,7 @@ static void unireg_abort(int exit_code) {
#endif /* WITH_WSREP */
DBUG_TRACE;

Server_status_file::set_status(Server_status_file::Status::STOPPING);
if (errno) {
sysd::notify("ERRNO=", errno, "\n");
}
Expand Down Expand Up @@ -2899,6 +2903,8 @@ static void mysqld_exit(int exit_code) {
close_service_status_pipe_in_mysqld();
#endif // _WIN32

Server_status_file::set_status(Server_status_file::Status::STOPPED);

exit(exit_code); /* purecov: inspected */
}

Expand Down Expand Up @@ -8424,6 +8430,9 @@ int mysqld_main(int argc, char **argv)
strmake(mysql_real_data_home, get_relative_path(MYSQL_DATADIR),
sizeof(mysql_real_data_home) - 1);

Server_status_file::init(&argc, &argv);
Server_status_file::set_status(Server_status_file::Status::INITIALIZING);

/* Must be initialized early for comparison of options name */
system_charset_info = &my_charset_utf8mb3_general_ci;

Expand Down Expand Up @@ -9468,9 +9477,12 @@ int mysqld_main(int argc, char **argv)
}

mysqld_socket_acceptor->check_and_spawn_admin_connection_handler_thread();
mysqld_socket_acceptor->connection_event_loop();
Server_status_file::set_status(Server_status_file::Status::READY);
mysqld_socket_acceptor->connection_event_loop(); // KH: end of limbo state
#endif /* _WIN32 */
server_operational_state = SERVER_SHUTTING_DOWN;
Server_status_file::set_status(Server_status_file::Status::STOPPING);

sysd::notify("STOPPING=1\nSTATUS=Server shutdown in progress\n");

DBUG_PRINT("info", ("No longer listening for incoming connections"));
Expand Down
1 change: 1 addition & 0 deletions sql/mysqld.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ extern ulonglong utility_user_privileges;
extern char *utility_user_dynamic_privileges;

extern bool persisted_globals_load;
extern bool create_server_state_file;
extern bool opt_keyring_operations;
extern bool opt_table_encryption_privilege_check;
extern char *opt_keyring_migration_user;
Expand Down
12 changes: 12 additions & 0 deletions sql/signal_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "sql/mysqld.h"
#include "sql/sql_class.h"
#include "sql/sql_const.h"
#include "sql/server_status_file.h"

#ifdef WITH_WSREP
#include "wsrep_mysqld.h"
Expand Down Expand Up @@ -227,9 +228,11 @@ void print_fatal_signal(int sig) {
extern "C" void handle_fatal_signal(int sig) {
if (s_handler_being_processed) {
my_safe_printf_stderr("Fatal " SIGNAL_FMT " while backtracing\n", sig);
Server_status_file::set_status(Server_status_file::Status::STOPPED);
_exit(MYSQLD_FAILURE_EXIT); /* Quit without running destructors */
}

Server_status_file::set_status(Server_status_file::Status::STOPPING);
s_handler_being_processed = true;

#ifdef WITH_WSREP
Expand All @@ -246,6 +249,10 @@ extern "C" void handle_fatal_signal(int sig) {

buffered_error_log.write_to_disk();

// This is the last chance to report in the file.
// Below coredump will exit the process
Server_status_file::set_status(Server_status_file::Status::STOPPED);

if ((test_flags & TEST_CORE_ON_SIGNAL) != 0) {
#if HAVE_LIBCOREDUMPER
if (opt_libcoredumper) {
Expand Down Expand Up @@ -290,6 +297,9 @@ void my_server_abort() {
static std::atomic_bool abort_processing{false};
/* Broadcast that this thread wants to print the signal info. */
aborts_pending++;

Server_status_file::set_status(Server_status_file::Status::STOPPING);

/*
Wait for the exclusive right to print the signal info. This assures the
output is not interleaved.
Expand Down Expand Up @@ -319,6 +329,8 @@ void my_server_abort() {
while (abort_processing.exchange(true)) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}

Server_status_file::set_status(Server_status_file::Status::STOPPED);
abort();
}
/*
Expand Down
8 changes: 8 additions & 0 deletions sql/sys_vars.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7868,6 +7868,14 @@ static Sys_var_bool Sys_persisted_globals_load(
DEFAULT(true), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
ON_UPDATE(nullptr));

static Sys_var_bool Sys_create_server_state_file(
"create_server_state_file",
"When this option is enabled, server reports its state in server.state "
"file located in datadir.",
READ_ONLY NON_PERSIST GLOBAL_VAR(create_server_state_file), CMD_LINE(OPT_ARG),
DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
ON_UPDATE(nullptr));

static bool sysvar_check_authid_string(sys_var *, THD *thd, set_var *var) {
/*
Since mandatory_roles is similar to a GRANT role statement without a
Expand Down
2 changes: 2 additions & 0 deletions storage/innobase/ut/ut0dbg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ this program; if not, write to the Free Software Foundation, Inc.,
#endif /* !UNIV_HOTBACKUP */

#include "ut0dbg.h"
#include "sql/server_status_file.h"

static std::function<void()> assert_callback;

Expand Down Expand Up @@ -78,6 +79,7 @@ void ut_set_assert_callback(std::function<void()> &callback) {
to_string(std::this_thread::get_id()).c_str());
#endif /* !UNIV_HOTBACKUP */

Server_status_file::set_status(Server_status_file::Status::STOPPING);
fputs(
"InnoDB: We intentionally generate a memory trap.\n"
"InnoDB: Submit a detailed bug report"
Expand Down

0 comments on commit b9977ce

Please sign in to comment.