MXS-2271 Move Monitor inside maxscale-namespace

Rearranged monitor.cc by namespace.
This commit is contained in:
Esa Korhonen 2019-03-13 10:38:13 +02:00
parent 5e3f837b42
commit 14b4fa632a
33 changed files with 538 additions and 504 deletions

View File

@ -96,7 +96,7 @@ struct arg_node
SERVER* server;
MXS_SESSION* session;
DCB* dcb;
Monitor* monitor;
mxs::Monitor* monitor;
MXS_FILTER_DEF* filter;
} value;
};

View File

@ -28,10 +28,15 @@
#include <maxscale/server.hh>
#include <maxscale/protocol/mysql.hh>
namespace maxscale
{
class Monitor;
}
struct DCB;
struct json_t;
struct EXTERNCMD;
class MonitorManager;
/**
* @verbatim
@ -56,9 +61,23 @@ struct MXS_MONITOR_API
* @param module Module name of the monitor
* @return Monitor object
*/
Monitor* (* createInstance)(const std::string& name, const std::string& module);
maxscale::Monitor* (* createInstance)(const std::string& name, const std::string& module);
};
/**
* Monitor configuration parameters names
*/
extern const char CN_BACKEND_CONNECT_ATTEMPTS[];
extern const char CN_BACKEND_CONNECT_TIMEOUT[];
extern const char CN_BACKEND_READ_TIMEOUT[];
extern const char CN_BACKEND_WRITE_TIMEOUT[];
extern const char CN_DISK_SPACE_CHECK_INTERVAL[];
extern const char CN_EVENTS[];
extern const char CN_JOURNAL_MAX_AGE[];
extern const char CN_MONITOR_INTERVAL[];
extern const char CN_SCRIPT[];
extern const char CN_SCRIPT_TIMEOUT[];
/**
* The monitor API version number. Any change to the monitor module API
* must change these versions using the rules defined in modinfo.h
@ -129,6 +148,9 @@ enum credentials_approach_t
CREDENTIALS_EXCLUDE,
};
namespace maxscale
{
/**
* The linked list of servers that are being monitored by the monitor module.
*/
@ -477,7 +499,7 @@ protected:
Settings m_settings;
private:
friend class MonitorManager;
friend class ::MonitorManager;
/**
* @brief Add a server to a monitor.
@ -532,23 +554,6 @@ private:
int get_data_file_path(char* path) const;
};
/**
* Monitor configuration parameters names
*/
extern const char CN_BACKEND_CONNECT_ATTEMPTS[];
extern const char CN_BACKEND_CONNECT_TIMEOUT[];
extern const char CN_BACKEND_READ_TIMEOUT[];
extern const char CN_BACKEND_WRITE_TIMEOUT[];
extern const char CN_DISK_SPACE_CHECK_INTERVAL[];
extern const char CN_EVENTS[];
extern const char CN_JOURNAL_MAX_AGE[];
extern const char CN_MONITOR_INTERVAL[];
extern const char CN_SCRIPT[];
extern const char CN_SCRIPT_TIMEOUT[];
namespace maxscale
{
/**
* An abstract class which helps implement a monitor based on a maxbase::Worker thread.
*/

View File

@ -68,6 +68,7 @@
using std::set;
using std::string;
using maxscale::Monitor;
const char CN_ACCOUNT[] = "account";
const char CN_ADDRESS[] = "address";

View File

@ -45,6 +45,7 @@ typedef std::set<std::string> StringSet;
typedef std::vector<std::string> StringVector;
using std::tie;
using maxscale::Monitor;
static std::mutex crt_lock;

View File

@ -143,7 +143,7 @@ bool runtime_enable_server_ssl(Server* server,
* @param value New value
* @return True if @c key was one of the supported parameters
*/
bool runtime_alter_monitor(Monitor* monitor, const char* key, const char* value);
bool runtime_alter_monitor(mxs::Monitor* monitor, const char* key, const char* value);
/**
* @brief Alter service parameters
@ -254,7 +254,7 @@ bool runtime_destroy_filter(const SFilterDef& filter);
* @param monitor Monitor to destroy
* @return True if monitor was destroyed
*/
bool runtime_destroy_monitor(Monitor* monitor);
bool runtime_destroy_monitor(mxs::Monitor* monitor);
/**
* Destroy a service
@ -304,7 +304,7 @@ bool runtime_alter_server_relationships_from_json(Server* server, const char* ty
*
* @return Created monitor or NULL on error
*/
Monitor* runtime_create_monitor_from_json(json_t* json);
mxs::Monitor* runtime_create_monitor_from_json(json_t* json);
/**
* @brief Create a new filter from JSON
@ -332,7 +332,7 @@ Service* runtime_create_service_from_json(json_t* json);
*
* @return True if the monitor was successfully modified to represent @c new_json
*/
bool runtime_alter_monitor_from_json(Monitor* monitor, json_t* new_json);
bool runtime_alter_monitor_from_json(mxs::Monitor* monitor, json_t* new_json);
/**
* @brief Alter monitor relationships
@ -342,7 +342,7 @@ bool runtime_alter_monitor_from_json(Monitor* monitor, json_t* new_json);
*
* @return True if the relationships were successfully modified
*/
bool runtime_alter_monitor_relationships_from_json(Monitor* monitor, json_t* json);
bool runtime_alter_monitor_relationships_from_json(mxs::Monitor* monitor, json_t* json);
/**
* @brief Alter a service using JSON

View File

@ -69,8 +69,8 @@ public:
* @param module The module name to load
* @return The newly created monitor, or NULL on error
*/
static Monitor* create_monitor(const std::string& name, const std::string& module,
MXS_CONFIG_PARAMETER* params);
static mxs::Monitor* create_monitor(const std::string& name, const std::string& module,
MXS_CONFIG_PARAMETER* params);
/**
* Mark monitor as deactivated. A deactivated monitor appears not to exist, as if it had been
@ -79,7 +79,7 @@ public:
*
* @param monitor Monitor to deactivate
*/
static void deactivate_monitor(Monitor* monitor);
static void deactivate_monitor(mxs::Monitor* monitor);
/**
* @brief Destroys all monitors. At this point all monitors should
@ -89,19 +89,19 @@ public:
*/
static void destroy_all_monitors();
static void start_monitor(Monitor* monitor);
static void start_monitor(mxs::Monitor* monitor);
/**
* Stop a given monitor
*
* @param monitor The monitor to stop
*/
static void stop_monitor(Monitor* monitor);
static void stop_monitor(mxs::Monitor* monitor);
static void stop_all_monitors();
static void start_all_monitors();
static Monitor* find_monitor(const char* name);
static mxs::Monitor* find_monitor(const char* name);
/**
* @brief Populate services with the servers of the monitors.
@ -130,12 +130,12 @@ public:
* @param server Server that is queried
* @return The monitor watching this server, or NULL if not monitored
*/
static Monitor* server_is_monitored(const SERVER* server);
static mxs::Monitor* server_is_monitored(const SERVER* server);
static Monitor* reactivate_monitor(const char* name, const char* module);
static mxs::Monitor* reactivate_monitor(const char* name, const char* module);
static void show_all_monitors(DCB* dcb);
static void monitor_show(DCB* dcb, Monitor* monitor);
static void monitor_show(DCB* dcb, mxs::Monitor* monitor);
static void monitor_list(DCB*);
@ -149,7 +149,7 @@ public:
* @param monitor Monitor to serialize
* @return True if serialization was successful
*/
static bool monitor_serialize(const Monitor* monitor);
static bool monitor_serialize(const mxs::Monitor* monitor);
/**
* @brief Convert monitor to JSON
@ -159,9 +159,9 @@ public:
*
* @return JSON representation of the monitor
*/
static json_t* monitor_to_json(const Monitor* monitor, const char* host);
static json_t* monitor_to_json(const mxs::Monitor* monitor, const char* host);
static bool create_monitor_config(const Monitor* monitor, const char* filename);
static bool create_monitor_config(const mxs::Monitor* monitor, const char* filename);
/**
* Waits until all running monitors have advanced one tick.

View File

@ -21,7 +21,10 @@
#include "filter.hh"
namespace maxscale
{
class Monitor;
}
/**
* @file service.h - MaxScale internal service functions
@ -106,7 +109,7 @@ public:
mutable std::mutex lock;
// TODO: Make this private.
Monitor* m_monitor { nullptr }; /**< A possibly associated monitor */
mxs::Monitor* m_monitor { nullptr }; /**< A possibly associated monitor */
bool uses_cluster() const
{
@ -413,7 +416,7 @@ json_t* service_relations_to_filter(const SFilterDef& filter, const char* host);
* @param monitor A monitor.
* @param server A server.
*/
void service_add_server(Monitor* pMonitor, SERVER* pServer);
void service_add_server(mxs::Monitor* pMonitor, SERVER* pServer);
/**
* @brief Remove server from all services associated with a monitor
@ -421,7 +424,7 @@ void service_add_server(Monitor* pMonitor, SERVER* pServer);
* @param monitor A monitor.
* @param server A server.
*/
void service_remove_server(Monitor* pMonitor, SERVER* pServer);
void service_remove_server(mxs::Monitor* pMonitor, SERVER* pServer);
std::unique_ptr<ResultSet> serviceGetList(void);
std::unique_ptr<ResultSet> serviceGetListenerList(void);

View File

@ -27,6 +27,8 @@
/** Size of the error buffer */
#define MODULECMD_ERRBUF_SIZE 512
using maxscale::Monitor;
/** Thread local error buffer */
thread_local char* errbuf = NULL;

View File

@ -70,6 +70,8 @@ enum stored_value_type
using std::string;
using std::set;
using Guard = std::lock_guard<std::mutex>;
using maxscale::Monitor;
using maxscale::MXS_MONITORED_SERVER;
const char CN_BACKEND_CONNECT_ATTEMPTS[] = "backend_connect_attempts";
const char CN_BACKEND_CONNECT_TIMEOUT[] = "backend_connect_timeout";
@ -397,6 +399,62 @@ bool process_data_file(Monitor* monitor, MXS_MONITORED_SERVER** master,
return true;
}
json_t* monitor_parameters_to_json(const Monitor* monitor)
{
json_t* rval = json_object();
const MXS_MODULE* mod = get_module(monitor->m_module.c_str(), MODULE_MONITOR);
config_add_module_params_json(&monitor->parameters,
{CN_TYPE, CN_MODULE, CN_SERVERS},
config_monitor_params,
mod->parameters,
rval);
return rval;
}
json_t* monitor_json_data(const Monitor* monitor, const char* host)
{
json_t* rval = json_object();
json_t* attr = json_object();
json_t* rel = json_object();
{
Guard guard(monitor->m_lock);
json_object_set_new(rval, CN_ID, json_string(monitor->m_name));
json_object_set_new(rval, CN_TYPE, json_string(CN_MONITORS));
json_object_set_new(attr, CN_MODULE, json_string(monitor->m_module.c_str()));
json_object_set_new(attr, CN_STATE, json_string(monitor_state_to_string(monitor->state())));
json_object_set_new(attr, CN_TICKS, json_integer(monitor->m_ticks));
/** Monitor parameters */
json_object_set_new(attr, CN_PARAMETERS, monitor_parameters_to_json(monitor));
if (monitor->state() == MONITOR_STATE_RUNNING)
{
json_t* diag = monitor->diagnostics_json();
if (diag)
{
json_object_set_new(attr, CN_MONITOR_DIAGNOSTICS, diag);
}
}
if (!monitor->m_servers.empty())
{
json_t* mon_rel = mxs_json_relationship(host, MXS_JSON_API_SERVERS);
for (MXS_MONITORED_SERVER* db : monitor->m_servers)
{
mxs_json_add_relation(mon_rel, db->server->name(), CN_SERVERS);
}
json_object_set_new(rel, CN_SERVERS, mon_rel);
}
}
json_object_set_new(rval, CN_RELATIONSHIPS, rel);
json_object_set_new(rval, CN_ATTRIBUTES, attr);
json_object_set_new(rval, CN_LINKS, mxs_json_self_link(host, CN_MONITORS, monitor->m_name));
return rval;
}
}
Monitor* MonitorManager::create_monitor(const string& name, const string& module,
@ -429,6 +487,396 @@ Monitor* MonitorManager::create_monitor(const string& name, const string& module
return mon;
}
void MonitorManager::debug_wait_one_tick()
{
using namespace std::chrono;
std::map<Monitor*, uint64_t> ticks;
// Get tick values for all monitors
this_unit.foreach_monitor([&ticks](Monitor* mon) {
ticks[mon] = mxb::atomic::load(&mon->m_ticks);
return true;
});
// Wait for all running monitors to advance at least one tick.
this_unit.foreach_monitor([&ticks](Monitor* mon) {
if (mon->state() == MONITOR_STATE_RUNNING)
{
auto start = steady_clock::now();
// A monitor may have been added in between the two foreach-calls (not if config changes are
// serialized). Check if entry exists.
if (ticks.count(mon) > 0)
{
auto tick = ticks[mon];
while (mxb::atomic::load(&mon->m_ticks) == tick && (steady_clock::now() - start < seconds(60)))
{
std::this_thread::sleep_for(milliseconds(100));
}
}
}
return true;
});
}
void MonitorManager::destroy_all_monitors()
{
auto monitors = this_unit.clear();
for (auto monitor : monitors)
{
mxb_assert(monitor->state() == MONITOR_STATE_STOPPED);
delete monitor;
}
}
void MonitorManager::start_monitor(Monitor* monitor)
{
mxb_assert(monitor);
Guard guard(monitor->m_lock);
// Only start the monitor if it's stopped.
if (monitor->state() == MONITOR_STATE_STOPPED)
{
if (!monitor->start())
{
MXS_ERROR("Failed to start monitor '%s'.", monitor->m_name);
}
}
}
void MonitorManager::populate_services()
{
this_unit.foreach_monitor([](Monitor* pMonitor) -> bool {
pMonitor->populate_services();
return true;
});
}
/**
* Start all monitors
*/
void MonitorManager::start_all_monitors()
{
this_unit.foreach_monitor([](Monitor* monitor) {
if (monitor->m_active)
{
MonitorManager::start_monitor(monitor);
}
return true;
});
}
void MonitorManager::stop_monitor(Monitor* monitor)
{
mxb_assert(monitor);
Guard guard(monitor->m_lock);
/** Only stop the monitor if it is running */
if (monitor->state() == MONITOR_STATE_RUNNING)
{
monitor->stop();
}
}
void MonitorManager::deactivate_monitor(Monitor* monitor)
{
// This cannot be done with configure(), since other, module-specific config settings may depend on the
// "servers"-setting of the base monitor. Directly manipulate monitor field for now, later use a dtor
// to cleanly "deactivate" inherited objects.
stop_monitor(monitor);
while (!monitor->m_servers.empty())
{
monitor->remove_server(monitor->m_servers.front()->server);
}
this_unit.run_behind_lock([monitor](){
monitor->m_active = false;
});
}
/**
* Shutdown all running monitors
*/
void MonitorManager::stop_all_monitors()
{
this_unit.foreach_monitor([](Monitor* monitor) {
if (monitor->m_active)
{
MonitorManager::stop_monitor(monitor);
}
return true;
});
}
/**
* Show all monitors
*
* @param dcb DCB for printing output
*/
void MonitorManager::show_all_monitors(DCB* dcb)
{
this_unit.foreach_monitor([dcb](Monitor* monitor) {
if (monitor->m_active)
{
monitor_show(dcb, monitor);
}
return true;
});
}
/**
* Show a single monitor
*
* @param dcb DCB for printing output
*/
void MonitorManager::monitor_show(DCB* dcb, Monitor* monitor)
{
monitor->show(dcb);
}
/**
* List all the monitors
*
* @param dcb DCB for printing output
*/
void MonitorManager::monitor_list(DCB* dcb)
{
dcb_printf(dcb, "---------------------+---------------------\n");
dcb_printf(dcb, "%-20s | Status\n", "Monitor");
dcb_printf(dcb, "---------------------+---------------------\n");
this_unit.foreach_monitor([dcb](Monitor* ptr){
if (ptr->m_active)
{
dcb_printf(dcb,
"%-20s | %s\n",
ptr->m_name,
ptr->state() == MONITOR_STATE_RUNNING ?
"Running" : "Stopped");
}
return true;
});
dcb_printf(dcb, "---------------------+---------------------\n");
}
/**
* Find a monitor by name
*
* @param name The name of the monitor
* @return Pointer to the monitor or NULL
*/
Monitor* MonitorManager::find_monitor(const char* name)
{
Monitor* rval = nullptr;
this_unit.foreach_monitor([&rval, name](Monitor* ptr) {
if (!strcmp(ptr->m_name, name) && ptr->m_active)
{
rval = ptr;
}
return (rval == nullptr);
});
return rval;
}
/**
* Find a destroyed monitor by name
*
* @param name The name of the monitor
* @return Pointer to the destroyed monitor or NULL if monitor is not found
*/
Monitor* MonitorManager::reactivate_monitor(const char* name, const char* module)
{
Monitor* rval = NULL;
this_unit.foreach_monitor([&rval, name, module](Monitor* monitor) {
if (strcmp(monitor->m_name, name) == 0 && (monitor->m_module == module))
{
mxb_assert(!monitor->m_active);
monitor->m_active = true;
rval = monitor;
}
return (rval == nullptr);
});
return rval;
}
/**
* Return a resultset that has the current set of monitors in it
*
* @return A Result set
*/
std::unique_ptr<ResultSet> MonitorManager::monitor_get_list()
{
std::unique_ptr<ResultSet> set = ResultSet::create({"Monitor", "Status"});
this_unit.foreach_monitor([&set](Monitor* ptr) {
const char* state = ptr->state() == MONITOR_STATE_RUNNING ? "Running" : "Stopped";
set->add_row({ptr->m_name, state});
return true;
});
return set;
}
Monitor* MonitorManager::server_is_monitored(const SERVER* server)
{
Monitor* rval = nullptr;
this_unit.foreach_monitor([&rval, server](Monitor* monitor) {
Guard guard(monitor->m_lock);
if (monitor->m_active)
{
for (MXS_MONITORED_SERVER* db : monitor->m_servers)
{
if (db->server == server)
{
rval = monitor;
break;
}
}
}
return (rval == nullptr);
});
return rval;
}
bool MonitorManager::create_monitor_config(const Monitor* monitor, const char* filename)
{
int file = open(filename, O_EXCL | O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (file == -1)
{
MXS_ERROR("Failed to open file '%s' when serializing monitor '%s': %d, %s",
filename,
monitor->m_name,
errno,
mxs_strerror(errno));
return false;
}
{
Guard guard(monitor->m_lock);
const MXS_MODULE* mod = get_module(monitor->m_module.c_str(), NULL);
mxb_assert(mod);
string config = generate_config_string(monitor->m_name, monitor->parameters,
config_monitor_params, mod->parameters);
if (dprintf(file, "%s", config.c_str()) == -1)
{
MXS_ERROR("Could not write serialized configuration to file '%s': %d, %s",
filename, errno, mxs_strerror(errno));
}
}
close(file);
return true;
}
bool MonitorManager::monitor_serialize(const Monitor* monitor)
{
bool rval = false;
char filename[PATH_MAX];
snprintf(filename,
sizeof(filename),
"%s/%s.cnf.tmp",
get_config_persistdir(),
monitor->m_name);
if (unlink(filename) == -1 && errno != ENOENT)
{
MXS_ERROR("Failed to remove temporary monitor configuration at '%s': %d, %s",
filename,
errno,
mxs_strerror(errno));
}
else if (create_monitor_config(monitor, filename))
{
char final_filename[PATH_MAX];
strcpy(final_filename, filename);
char* dot = strrchr(final_filename, '.');
mxb_assert(dot);
*dot = '\0';
if (rename(filename, final_filename) == 0)
{
rval = true;
}
else
{
MXS_ERROR("Failed to rename temporary monitor configuration at '%s': %d, %s",
filename,
errno,
mxs_strerror(errno));
}
}
return rval;
}
json_t* MonitorManager::monitor_to_json(const Monitor* monitor, const char* host)
{
string self = MXS_JSON_API_MONITORS;
self += monitor->m_name;
return mxs_json_resource(host, self.c_str(), monitor_json_data(monitor, host));
}
json_t* MonitorManager::monitor_list_to_json(const char* host)
{
json_t* rval = json_array();
this_unit.foreach_monitor([rval, host](Monitor* mon) {
if (mon->m_active)
{
json_t* json = monitor_json_data(mon, host);
if (json)
{
json_array_append_new(rval, json);
}
}
return true;
});
return mxs_json_resource(host, MXS_JSON_API_MONITORS, rval);
}
json_t* MonitorManager::monitor_relations_to_server(const SERVER* server, const char* host)
{
std::vector<std::string> names;
this_unit.foreach_monitor([&names, server](Monitor* mon) {
Guard guard(mon->m_lock);
if (mon->m_active)
{
for (MXS_MONITORED_SERVER* db : mon->m_servers)
{
if (db->server == server)
{
names.push_back(mon->m_name);
break;
}
}
}
return true;
});
json_t* rel = NULL;
if (!names.empty())
{
rel = mxs_json_relationship(host, MXS_JSON_API_MONITORS);
for (std::vector<std::string>::iterator it = names.begin();
it != names.end(); it++)
{
mxs_json_add_relation(rel, it->c_str(), CN_MONITORS);
}
}
return rel;
}
namespace maxscale
{
Monitor::Monitor(const string& name, const string& module)
: m_name(MXS_STRDUP_A(name.c_str()))
, m_module(module)
@ -522,97 +970,6 @@ Monitor::~Monitor()
MXS_FREE((const_cast<char*>(m_name)));
}
void MonitorManager::destroy_all_monitors()
{
auto monitors = this_unit.clear();
for (auto monitor : monitors)
{
mxb_assert(monitor->state() == MONITOR_STATE_STOPPED);
delete monitor;
}
}
void MonitorManager::start_monitor(Monitor* monitor)
{
mxb_assert(monitor);
Guard guard(monitor->m_lock);
// Only start the monitor if it's stopped.
if (monitor->state() == MONITOR_STATE_STOPPED)
{
if (!monitor->start())
{
MXS_ERROR("Failed to start monitor '%s'.", monitor->m_name);
}
}
}
void MonitorManager::populate_services()
{
this_unit.foreach_monitor([](Monitor* pMonitor) -> bool {
pMonitor->populate_services();
return true;
});
}
/**
* Start all monitors
*/
void MonitorManager::start_all_monitors()
{
this_unit.foreach_monitor([](Monitor* monitor) {
if (monitor->m_active)
{
MonitorManager::start_monitor(monitor);
}
return true;
});
}
void MonitorManager::stop_monitor(Monitor* monitor)
{
mxb_assert(monitor);
Guard guard(monitor->m_lock);
/** Only stop the monitor if it is running */
if (monitor->state() == MONITOR_STATE_RUNNING)
{
monitor->stop();
}
}
void MonitorManager::deactivate_monitor(Monitor* monitor)
{
// This cannot be done with configure(), since other, module-specific config settings may depend on the
// "servers"-setting of the base monitor. Directly manipulate monitor field for now, later use a dtor
// to cleanly "deactivate" inherited objects.
stop_monitor(monitor);
while (!monitor->m_servers.empty())
{
monitor->remove_server(monitor->m_servers.front()->server);
}
this_unit.run_behind_lock([monitor](){
monitor->m_active = false;
});
}
/**
* Shutdown all running monitors
*/
void MonitorManager::stop_all_monitors()
{
this_unit.foreach_monitor([](Monitor* monitor) {
if (monitor->m_active)
{
MonitorManager::stop_monitor(monitor);
}
return true;
});
}
/**
* @brief Add a server to the monitor.
*
@ -681,32 +1038,6 @@ void Monitor::remove_server(SERVER* server)
}
}
/**
* Show all monitors
*
* @param dcb DCB for printing output
*/
void MonitorManager::show_all_monitors(DCB* dcb)
{
this_unit.foreach_monitor([dcb](Monitor* monitor) {
if (monitor->m_active)
{
monitor_show(dcb, monitor);
}
return true;
});
}
/**
* Show a single monitor
*
* @param dcb DCB for printing output
*/
void MonitorManager::monitor_show(DCB* dcb, Monitor* monitor)
{
monitor->show(dcb);
}
void Monitor::show(DCB* dcb)
{
Monitor* monitor = this;
@ -742,87 +1073,6 @@ void Monitor::show(DCB* dcb)
dcb_printf(dcb, "\n");
}
/**
* List all the monitors
*
* @param dcb DCB for printing output
*/
void MonitorManager::monitor_list(DCB* dcb)
{
dcb_printf(dcb, "---------------------+---------------------\n");
dcb_printf(dcb, "%-20s | Status\n", "Monitor");
dcb_printf(dcb, "---------------------+---------------------\n");
this_unit.foreach_monitor([dcb](Monitor* ptr){
if (ptr->m_active)
{
dcb_printf(dcb,
"%-20s | %s\n",
ptr->m_name,
ptr->state() == MONITOR_STATE_RUNNING ?
"Running" : "Stopped");
}
return true;
});
dcb_printf(dcb, "---------------------+---------------------\n");
}
/**
* Find a monitor by name
*
* @param name The name of the monitor
* @return Pointer to the monitor or NULL
*/
Monitor* MonitorManager::find_monitor(const char* name)
{
Monitor* rval = nullptr;
this_unit.foreach_monitor([&rval, name](Monitor* ptr) {
if (!strcmp(ptr->m_name, name) && ptr->m_active)
{
rval = ptr;
}
return (rval == nullptr);
});
return rval;
}
/**
* Find a destroyed monitor by name
*
* @param name The name of the monitor
* @return Pointer to the destroyed monitor or NULL if monitor is not found
*/
Monitor* MonitorManager::reactivate_monitor(const char* name, const char* module)
{
Monitor* rval = NULL;
this_unit.foreach_monitor([&rval, name, module](Monitor* monitor) {
if (strcmp(monitor->m_name, name) == 0 && (monitor->m_module == module))
{
mxb_assert(!monitor->m_active);
monitor->m_active = true;
rval = monitor;
}
return (rval == nullptr);
});
return rval;
}
/**
* Return a resultset that has the current set of monitors in it
*
* @return A Result set
*/
std::unique_ptr<ResultSet> MonitorManager::monitor_get_list()
{
std::unique_ptr<ResultSet> set = ResultSet::create({"Monitor", "Status"});
this_unit.foreach_monitor([&set](Monitor* ptr) {
const char* state = ptr->state() == MONITOR_STATE_RUNNING ? "Running" : "Stopped";
set->add_row({ptr->m_name, state});
return true;
});
return set;
}
bool Monitor::test_permissions(const string& query)
{
auto monitor = this;
@ -1476,102 +1726,6 @@ void MXS_MONITORED_SERVER::log_state_change()
prev.c_str(), next.c_str());
}
Monitor* MonitorManager::server_is_monitored(const SERVER* server)
{
Monitor* rval = nullptr;
this_unit.foreach_monitor([&rval, server](Monitor* monitor) {
Guard guard(monitor->m_lock);
if (monitor->m_active)
{
for (MXS_MONITORED_SERVER* db : monitor->m_servers)
{
if (db->server == server)
{
rval = monitor;
break;
}
}
}
return (rval == nullptr);
});
return rval;
}
bool MonitorManager::create_monitor_config(const Monitor* monitor, const char* filename)
{
int file = open(filename, O_EXCL | O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (file == -1)
{
MXS_ERROR("Failed to open file '%s' when serializing monitor '%s': %d, %s",
filename,
monitor->m_name,
errno,
mxs_strerror(errno));
return false;
}
{
Guard guard(monitor->m_lock);
const MXS_MODULE* mod = get_module(monitor->m_module.c_str(), NULL);
mxb_assert(mod);
string config = generate_config_string(monitor->m_name, monitor->parameters,
config_monitor_params, mod->parameters);
if (dprintf(file, "%s", config.c_str()) == -1)
{
MXS_ERROR("Could not write serialized configuration to file '%s': %d, %s",
filename, errno, mxs_strerror(errno));
}
}
close(file);
return true;
}
bool MonitorManager::monitor_serialize(const Monitor* monitor)
{
bool rval = false;
char filename[PATH_MAX];
snprintf(filename,
sizeof(filename),
"%s/%s.cnf.tmp",
get_config_persistdir(),
monitor->m_name);
if (unlink(filename) == -1 && errno != ENOENT)
{
MXS_ERROR("Failed to remove temporary monitor configuration at '%s': %d, %s",
filename,
errno,
mxs_strerror(errno));
}
else if (create_monitor_config(monitor, filename))
{
char final_filename[PATH_MAX];
strcpy(final_filename, filename);
char* dot = strrchr(final_filename, '.');
mxb_assert(dot);
*dot = '\0';
if (rename(filename, final_filename) == 0)
{
rval = true;
}
else
{
MXS_ERROR("Failed to rename temporary monitor configuration at '%s': %d, %s",
filename,
errno,
mxs_strerror(errno));
}
}
return rval;
}
void Monitor::hangup_failed_servers()
{
for (MXS_MONITORED_SERVER* ptr : m_servers)
@ -1682,122 +1836,6 @@ void Monitor::detect_handle_state_changes()
}
}
json_t* monitor_parameters_to_json(const Monitor* monitor)
{
json_t* rval = json_object();
const MXS_MODULE* mod = get_module(monitor->m_module.c_str(), MODULE_MONITOR);
config_add_module_params_json(&monitor->parameters,
{CN_TYPE, CN_MODULE, CN_SERVERS},
config_monitor_params,
mod->parameters,
rval);
return rval;
}
json_t* monitor_json_data(const Monitor* monitor, const char* host)
{
json_t* rval = json_object();
json_t* attr = json_object();
json_t* rel = json_object();
{
Guard guard(monitor->m_lock);
json_object_set_new(rval, CN_ID, json_string(monitor->m_name));
json_object_set_new(rval, CN_TYPE, json_string(CN_MONITORS));
json_object_set_new(attr, CN_MODULE, json_string(monitor->m_module.c_str()));
json_object_set_new(attr, CN_STATE, json_string(monitor_state_to_string(monitor->state())));
json_object_set_new(attr, CN_TICKS, json_integer(monitor->m_ticks));
/** Monitor parameters */
json_object_set_new(attr, CN_PARAMETERS, monitor_parameters_to_json(monitor));
if (monitor->state() == MONITOR_STATE_RUNNING)
{
json_t* diag = monitor->diagnostics_json();
if (diag)
{
json_object_set_new(attr, CN_MONITOR_DIAGNOSTICS, diag);
}
}
if (!monitor->m_servers.empty())
{
json_t* mon_rel = mxs_json_relationship(host, MXS_JSON_API_SERVERS);
for (MXS_MONITORED_SERVER* db : monitor->m_servers)
{
mxs_json_add_relation(mon_rel, db->server->name(), CN_SERVERS);
}
json_object_set_new(rel, CN_SERVERS, mon_rel);
}
}
json_object_set_new(rval, CN_RELATIONSHIPS, rel);
json_object_set_new(rval, CN_ATTRIBUTES, attr);
json_object_set_new(rval, CN_LINKS, mxs_json_self_link(host, CN_MONITORS, monitor->m_name));
return rval;
}
json_t* MonitorManager::monitor_to_json(const Monitor* monitor, const char* host)
{
string self = MXS_JSON_API_MONITORS;
self += monitor->m_name;
return mxs_json_resource(host, self.c_str(), monitor_json_data(monitor, host));
}
json_t* MonitorManager::monitor_list_to_json(const char* host)
{
json_t* rval = json_array();
this_unit.foreach_monitor([rval, host](Monitor* mon) {
if (mon->m_active)
{
json_t* json = monitor_json_data(mon, host);
if (json)
{
json_array_append_new(rval, json);
}
}
return true;
});
return mxs_json_resource(host, MXS_JSON_API_MONITORS, rval);
}
json_t* MonitorManager::monitor_relations_to_server(const SERVER* server, const char* host)
{
std::vector<std::string> names;
this_unit.foreach_monitor([&names, server](Monitor* mon) {
Guard guard(mon->m_lock);
if (mon->m_active)
{
for (MXS_MONITORED_SERVER* db : mon->m_servers)
{
if (db->server == server)
{
names.push_back(mon->m_name);
break;
}
}
}
return true;
});
json_t* rel = NULL;
if (!names.empty())
{
rel = mxs_json_relationship(host, MXS_JSON_API_MONITORS);
for (std::vector<std::string>::iterator it = names.begin();
it != names.end(); it++)
{
mxs_json_add_relation(rel, it->c_str(), CN_MONITORS);
}
}
return rel;
}
int Monitor::get_data_file_path(char* path) const
{
int rv = snprintf(path, PATH_MAX, journal_template, get_datadir(), m_name, journal_name);
@ -2244,40 +2282,6 @@ void Monitor::populate_services()
}
}
void MonitorManager::debug_wait_one_tick()
{
using namespace std::chrono;
std::map<Monitor*, uint64_t> ticks;
// Get tick values for all monitors
this_unit.foreach_monitor([&ticks](Monitor* mon) {
ticks[mon] = mxb::atomic::load(&mon->m_ticks);
return true;
});
// Wait for all running monitors to advance at least one tick.
this_unit.foreach_monitor([&ticks](Monitor* mon) {
if (mon->state() == MONITOR_STATE_RUNNING)
{
auto start = steady_clock::now();
// A monitor may have been added in between the two foreach-calls (not if config changes are
// serialized). Check if entry exists.
if (ticks.count(mon) > 0)
{
auto tick = ticks[mon];
while (mxb::atomic::load(&mon->m_ticks) == tick && (steady_clock::now() - start < seconds(60)))
{
std::this_thread::sleep_for(milliseconds(100));
}
}
}
return true;
});
}
namespace maxscale
{
MonitorWorker::MonitorWorker(const string& name, const string& module)
: Monitor(name, module)
, m_master(NULL)

View File

@ -40,6 +40,7 @@ using std::list;
using std::map;
using std::string;
using std::stringstream;
using maxscale::Monitor;
Resource::Resource(ResourceCallback cb, int components, ...)
: m_cb(cb)

View File

@ -53,6 +53,7 @@
using maxbase::Worker;
using maxscale::RoutingWorker;
using maxscale::Monitor;
using std::string;
using Guard = std::lock_guard<std::mutex>;
@ -480,7 +481,7 @@ void Server::print_to_dcb(DCB* dcb) const
dcb_printf(dcb, "\tMaster Id: %ld\n", server->master_id);
dcb_printf(dcb,
"\tLast event: %s\n",
Monitor::get_event_name((mxs_monitor_event_t) server->last_event));
mxs::Monitor::get_event_name((mxs_monitor_event_t) server->last_event));
time_t t = maxscale_started() + MXS_CLOCK_TO_SEC(server->triggered_at);
dcb_printf(dcb, "\tTriggered at: %s\n", http_to_date(t).c_str());
if (server->is_slave() || server->is_relay())

View File

@ -24,6 +24,7 @@
#include <maxscale/modinfo.h>
#include <maxscale/mysql_utils.hh>
using maxscale::MXS_MONITORED_SERVER;
AuroraMonitor::AuroraMonitor(const std::string& name, const std::string& module)
: MonitorWorkerSimple(name, module)

View File

@ -30,7 +30,7 @@ public:
protected:
bool has_sufficient_permissions();
void update_server_status(MXS_MONITORED_SERVER* monitored_server);
void update_server_status(mxs::MXS_MONITORED_SERVER* monitored_server);
private:
AuroraMonitor(const std::string& name, const std::string& module);

View File

@ -14,6 +14,9 @@
#include "clustrix.hh"
#include <maxbase/assert.h>
using maxscale::Monitor;
using maxscale::MXS_MONITORED_SERVER;
namespace
{

View File

@ -65,7 +65,7 @@ bool is_part_of_the_quorum(const char* zName, const SERVER& server, MYSQL* pCon)
*
* @return True, if the node is part of the quorum, false otherwise.
*/
inline bool is_part_of_the_quorum(const char* zName, MXS_MONITORED_SERVER& ms)
inline bool is_part_of_the_quorum(const char* zName, mxs::MXS_MONITORED_SERVER& ms)
{
mxb_assert(ms.server);
mxb_assert(ms.con);
@ -100,7 +100,7 @@ bool is_being_softfailed(const char* zName, const SERVER& server, MYSQL* pCon);
* @note Upon return @c *ppCon will be non-NULL.
*/
bool ping_or_connect_to_hub(const char* zName,
const MXS_MONITORED_SERVER::ConnectionSettings& settings,
const mxs::MXS_MONITORED_SERVER::ConnectionSettings& settings,
Softfailed softfailed,
SERVER& server,
MYSQL** ppCon);
@ -117,9 +117,9 @@ bool ping_or_connect_to_hub(const char* zName,
* @return True, if the server can be used as hub, false otherwise.
*/
inline bool ping_or_connect_to_hub(const char* zName,
const MXS_MONITORED_SERVER::ConnectionSettings& settings,
const mxs::MXS_MONITORED_SERVER::ConnectionSettings& settings,
Softfailed softfailed,
MXS_MONITORED_SERVER& ms)
mxs::MXS_MONITORED_SERVER& ms)
{
return ping_or_connect_to_hub(zName, settings, softfailed, *ms.server, &ms.con);
}

View File

@ -20,6 +20,7 @@
namespace http = mxb::http;
using namespace std;
using maxscale::MXS_MONITORED_SERVER;
#define LOG_JSON_ERROR(ppJson, format, ...) \
do { \

View File

@ -15,7 +15,7 @@
#include "clustrix.hh"
bool ClustrixNode::can_be_used_as_hub(const char* zName,
const MXS_MONITORED_SERVER::ConnectionSettings& settings)
const mxs::MXS_MONITORED_SERVER::ConnectionSettings& settings)
{
mxb_assert(m_pServer);
bool rv = Clustrix::ping_or_connect_to_hub(zName, settings, Clustrix::Softfailed::REJECT,

View File

@ -161,7 +161,7 @@ public:
}
bool can_be_used_as_hub(const char* zName,
const MXS_MONITORED_SERVER::ConnectionSettings& settings);
const mxs::MXS_MONITORED_SERVER::ConnectionSettings& settings);
SERVER* server() const
{

View File

@ -22,6 +22,8 @@
#include <maxscale/modinfo.h>
#include <maxscale/mysql_utils.hh>
using maxscale::MXS_MONITORED_SERVER;
namespace
{

View File

@ -26,7 +26,7 @@ public:
protected:
bool has_sufficient_permissions();
void update_server_status(MXS_MONITORED_SERVER* monitored_server);
void update_server_status(mxs::MXS_MONITORED_SERVER* monitored_server);
private:
CsMonitor(const std::string& name, const std::string& module);

View File

@ -33,6 +33,8 @@
#define DONOR_NODE_NAME_MAX_LEN 60
#define DONOR_LIST_SET_VAR "SET GLOBAL wsrep_sst_donor = \""
using maxscale::MXS_MONITORED_SERVER;
/** Log a warning when a bad 'wsrep_local_index' is found */
static bool warn_erange_on_local_index = true;

View File

@ -32,7 +32,7 @@ struct GaleraNode
std::string cluster_uuid; /**< Cluster UUID */
};
typedef std::unordered_map<MXS_MONITORED_SERVER*, GaleraNode> NodeMap;
typedef std::unordered_map<mxs::MXS_MONITORED_SERVER*, GaleraNode> NodeMap;
class GaleraMonitor : public maxscale::MonitorWorkerSimple
{
@ -48,7 +48,7 @@ public:
protected:
bool configure(const MXS_CONFIG_PARAMETER* param);
bool has_sufficient_permissions();
void update_server_status(MXS_MONITORED_SERVER* monitored_server);
void update_server_status(mxs::MXS_MONITORED_SERVER* monitored_server);
void pre_tick();
void post_tick();
@ -71,7 +71,7 @@ private:
bool detect_cluster_size(const int n_nodes,
const char* candidate_uuid,
const int candidate_size);
MXS_MONITORED_SERVER* get_candidate_master();
mxs::MXS_MONITORED_SERVER* get_candidate_master();
void set_galera_cluster();
void update_sst_donor_nodes(int is_cluster);
};

View File

@ -25,6 +25,7 @@
#include <maxscale/protocol/mysql.hh>
#include <mysqld_error.h>
using maxscale::MXS_MONITORED_SERVER;
GRMon::GRMon(const std::string& name, const std::string& module)
: MonitorWorkerSimple(name, module)

View File

@ -30,7 +30,7 @@ public:
protected:
bool has_sufficient_permissions();
void update_server_status(MXS_MONITORED_SERVER* monitored_server);
void update_server_status(mxs::MXS_MONITORED_SERVER* monitored_server);
private:
GRMon(const std::string& name, const std::string& module);

View File

@ -32,6 +32,8 @@
using std::string;
using maxbase::string_printf;
using maxscale::Monitor;
using maxscale::MXS_MONITORED_SERVER;
// Config parameter names
const char* const CN_AUTO_FAILOVER = "auto_failover";

View File

@ -251,7 +251,7 @@ private:
MariaDBServer* get_server(const std::string& host, int port);
MariaDBServer* get_server(int64_t id);
MariaDBServer* get_server(MXS_MONITORED_SERVER* mon_server);
MariaDBServer* get_server(mxs::MXS_MONITORED_SERVER* mon_server);
MariaDBServer* get_server(SERVER* server);
// Cluster discovery and status assignment methods, top levels

View File

@ -28,6 +28,7 @@ using maxbase::Duration;
using maxbase::StopWatch;
using maxsql::QueryResult;
using Guard = std::lock_guard<std::mutex>;
using maxscale::MXS_MONITORED_SERVER;
MariaDBServer::MariaDBServer(MXS_MONITORED_SERVER* monitored_server, int config_index,
bool assume_unique_hostnames, bool query_events)

View File

@ -73,7 +73,7 @@ struct NodeData
class MariaDBServer
{
public:
MariaDBServer(MXS_MONITORED_SERVER* monitored_server, int config_index,
MariaDBServer(mxs::MXS_MONITORED_SERVER* monitored_server, int config_index,
bool assume_unique_hostnames, bool query_events);
class EventInfo
@ -119,7 +119,7 @@ public:
/* Monitored server base class/struct. MariaDBServer does not own the struct, it is not freed
* (or connection closed) when a MariaDBServer is destroyed. */
MXS_MONITORED_SERVER* m_server_base = NULL;
mxs::MXS_MONITORED_SERVER* m_server_base = NULL;
/* What position this server has in the monitor config? Used for tiebreaking between servers. */
int m_config_index = 0;

View File

@ -25,6 +25,7 @@
using std::string;
using std::cout;
using maxscale::MXS_MONITORED_SERVER;
// Maximum sizes for array types
const int MAX_CYCLE_SIZE = 10;

View File

@ -42,7 +42,7 @@ bool NDBCMonitor::has_sufficient_permissions()
return test_permissions("SHOW STATUS LIKE 'Ndb_number_of_ready_data_nodes'");
}
void NDBCMonitor::update_server_status(MXS_MONITORED_SERVER* monitored_server)
void NDBCMonitor::update_server_status(mxs::MXS_MONITORED_SERVER* monitored_server)
{
MYSQL_ROW row;
MYSQL_RES* result;

View File

@ -30,7 +30,7 @@ public:
protected:
bool has_sufficient_permissions();
void update_server_status(MXS_MONITORED_SERVER* monitored_server);
void update_server_status(mxs::MXS_MONITORED_SERVER* monitored_server);
private:
NDBCMonitor(const std::string& name, const std::string& module);

View File

@ -80,6 +80,8 @@
#define ARG_TYPE_NUMERIC 10
#define ARG_TYPE_OBJECT_NAME 11 // A string where whitespace is replaced with hyphens
using maxscale::Monitor;
/**
* The subcommand structure
*

View File

@ -503,7 +503,7 @@ void exec_shutdown_monitor(DCB* dcb, MAXINFO_TREE* tree)
char errmsg[120];
if (tree && tree->value)
{
Monitor* monitor = MonitorManager::find_monitor(tree->value);
mxs::Monitor* monitor = MonitorManager::find_monitor(tree->value);
if (monitor)
{
MonitorManager::stop_monitor(monitor);
@ -612,7 +612,7 @@ void exec_restart_monitor(DCB* dcb, MAXINFO_TREE* tree)
char errmsg[120];
if (tree && tree->value)
{
Monitor* monitor = MonitorManager::find_monitor(tree->value);
mxs::Monitor* monitor = MonitorManager::find_monitor(tree->value);
if (monitor)
{
MonitorManager::start_monitor(monitor);