MXS-1441 Create proper json error objects

This commit is contained in:
Johan Wikman 2017-10-02 13:09:54 +03:00
parent e41f60fd2e
commit f29d8209cc

View File

@ -22,6 +22,7 @@
#include <maxscale/alloc.h> #include <maxscale/alloc.h>
#include <maxscale/dcb.h> #include <maxscale/dcb.h>
#include <maxscale/debug.h> #include <maxscale/debug.h>
#include <maxscale/json_api.h>
#include <maxscale/modulecmd.h> #include <maxscale/modulecmd.h>
#include <maxscale/modutil.h> #include <maxscale/modutil.h>
#include <maxscale/mysql_utils.h> #include <maxscale/mysql_utils.h>
@ -81,16 +82,16 @@ static const char CN_SWITCHOVER_TIMEOUT[] = "switchover_timeout";
* *
* @param current_master The specified current master. * @param current_master The specified current master.
* @param server The server to check against. * @param server The server to check against.
* @param result Result object, for adding error information.
* @param current_master_found On output, true if @server is @c current_master. * @param current_master_found On output, true if @server is @c current_master.
* @param error On output, error object if function failed.
* *
* @return False, if there is some error with the specified current master, * @return False, if there is some error with the specified current master,
* True otherwise. * True otherwise.
*/ */
bool mysql_switchover_check_current(SERVER* current_master, bool mysql_switchover_check_current(SERVER* current_master,
SERVER* server, SERVER* server,
json_t* result, bool* current_master_found,
bool* current_master_found) json_t** error)
{ {
bool rv = true; bool rv = true;
bool is_master = SERVER_IS_MASTER(server); bool is_master = SERVER_IS_MASTER(server);
@ -101,33 +102,15 @@ bool mysql_switchover_check_current(SERVER* current_master,
if (!is_master) if (!is_master)
{ {
std::string s; *error = mxs_json_error("Specified %s is a server, but not the current master.",
s += "Specified current master "; current_master->unique_name);
s += current_master->unique_name;
s += " is a server, but it is not the current master.";
json_t* error = json_string(s.c_str());
if (error)
{
json_object_set_new(result, "error", error);
}
rv = false; rv = false;
} }
} }
else if (is_master) else if (is_master)
{ {
std::string s; *error = mxs_json_error("Current master not specified, even though there is "
s += "Current master not specified, although there is a master, "; "a master (%s).", server->unique_name);
s += server->unique_name;
s += ".";
json_t* error = json_string(s.c_str());
if (error)
{
json_object_set_new(result, "error", error);
}
rv = false; rv = false;
} }
@ -139,16 +122,16 @@ bool mysql_switchover_check_current(SERVER* current_master,
* *
* @param new_master The specified new master. * @param new_master The specified new master.
* @param server The server to check against. * @param server The server to check against.
* @param result Result object, for adding error information.
* @param new_master_found On output, true if the @c server is @c new_master. * @param new_master_found On output, true if the @c server is @c new_master.
* @param error On output, error object if function failed.
* *
* @return False, if there is some error with the specified current master, * @return False, if there is some error with the specified current master,
* True otherwise. * True otherwise.
*/ */
bool mysql_switchover_check_new(SERVER* new_master, bool mysql_switchover_check_new(SERVER* new_master,
SERVER* server, SERVER* server,
json_t* result, bool* new_master_found,
bool* new_master_found) json_t** error)
{ {
bool rv = true; bool rv = true;
bool is_master = SERVER_IS_MASTER(server); bool is_master = SERVER_IS_MASTER(server);
@ -159,17 +142,8 @@ bool mysql_switchover_check_new(SERVER* new_master,
if (is_master) if (is_master)
{ {
std::string s; *error = mxs_json_error("Specified new master %s is already the current master.",
s += "Specified new master "; new_master->unique_name);
s += new_master->unique_name;
s += " is already master.";
json_t* error = json_string(s.c_str());
if (error)
{
json_object_set_new(result, "error", error);
}
rv = false; rv = false;
} }
} }
@ -180,15 +154,14 @@ bool mysql_switchover_check_new(SERVER* new_master,
/** /**
* Check whether specified current and new master are acceptable. * Check whether specified current and new master are acceptable.
* *
* @param mon The monitor.
* @param new_master The specified new master. * @param new_master The specified new master.
* @param server The server to check against. * @param current_master The specifiec current master (may be NULL).
* @param result Result object, for adding error information. * @param error On output, error object if function failed.
* @param new_master_found On output, true if the @c server is @c new_master.
* *
* @return False, if there is some error with the specified current master, * @return True if switchover can proceeed, false otherwise.
* True otherwise.
*/ */
bool mysql_switchover_check(MXS_MONITOR* mon, SERVER* new_master, SERVER* current_master, json_t* result) bool mysql_switchover_check(MXS_MONITOR* mon, SERVER* new_master, SERVER* current_master, json_t** error)
{ {
bool rv = true; bool rv = true;
@ -204,12 +177,12 @@ bool mysql_switchover_check(MXS_MONITOR* mon, SERVER* new_master, SERVER* curren
if (!current_master_found) if (!current_master_found)
{ {
rv = mysql_switchover_check_current(current_master, server, result, &current_master_found); rv = mysql_switchover_check_current(current_master, server, &current_master_found, error);
} }
if (rv) if (rv)
{ {
rv = mysql_switchover_check_new(new_master, server, result, &new_master_found); rv = mysql_switchover_check_new(new_master, server, &new_master_found, error);
} }
monitored_server = monitored_server->next; monitored_server = monitored_server->next;
@ -217,26 +190,19 @@ bool mysql_switchover_check(MXS_MONITOR* mon, SERVER* new_master, SERVER* curren
if (rv && ((current_master && !current_master_found) || !new_master_found)) if (rv && ((current_master && !current_master_found) || !new_master_found))
{ {
std::string s; *error = NULL;
if (current_master && !current_master_found) if (current_master && !current_master_found)
{ {
s += "Current master "; *error = mxs_json_error("Specified current master %s is not found amongst "
s += current_master->unique_name; "existing servers.", current_master->unique_name);
s += " specified, but not found amongst existing servers. ";
} }
if (!new_master_found) if (!new_master_found)
{ {
s += "Specified new master "; *error = mxs_json_error_append(*error,
s += new_master->unique_name; "Specified new master %s is not found amongst "
s += " not found amongst existing servers."; "existing servers.", new_master->unique_name);
}
json_t* error = json_string(s.c_str());
if (error)
{
json_object_set_new(result, "error", error);
} }
rv = false; rv = false;
@ -245,7 +211,7 @@ bool mysql_switchover_check(MXS_MONITOR* mon, SERVER* new_master, SERVER* curren
return rv; return rv;
} }
bool mysql_switchover_perform(MXS_MONITOR* mon, SERVER* new_master, SERVER* current_master, json_t* result) bool mysql_switchover_perform(MXS_MONITOR* mon, SERVER* new_master, SERVER* current_master, json_t** result)
{ {
// TODO: Launch actual switchover command. // TODO: Launch actual switchover command.
@ -261,9 +227,10 @@ bool mysql_switchover_perform(MXS_MONITOR* mon, SERVER* new_master, SERVER* curr
s += new_master->unique_name; s += new_master->unique_name;
s += "."; s += ".";
*result = json_object();
json_t* data = json_string(s.c_str()); json_t* data = json_string(s.c_str());
json_object_set_new(result, "data", data); json_object_set_new(*result, "data", data);
return true; return true;
} }
@ -286,11 +253,6 @@ bool mysql_switchover(MXS_MONITOR* mon, SERVER* new_master, SERVER* current_mast
*output = NULL; *output = NULL;
json_t* result = json_object();
if (result)
{
*output = result;
bool stopped = stop_monitor(mon); bool stopped = stop_monitor(mon);
if (stopped) if (stopped)
@ -302,13 +264,13 @@ bool mysql_switchover(MXS_MONITOR* mon, SERVER* new_master, SERVER* current_mast
MXS_NOTICE("Monitor %s already stopped, switchover can proceed.", mon->name); MXS_NOTICE("Monitor %s already stopped, switchover can proceed.", mon->name);
} }
rv = mysql_switchover_check(mon, new_master, current_master, result); rv = mysql_switchover_check(mon, new_master, current_master, output);
if (rv) if (rv)
{ {
bool failover = config_get_bool(mon->parameters, CN_FAILOVER); bool failover = config_get_bool(mon->parameters, CN_FAILOVER);
rv = mysql_switchover_perform(mon, new_master, current_master, result); rv = mysql_switchover_perform(mon, new_master, current_master, output);
if (rv) if (rv)
{ {
@ -351,11 +313,6 @@ bool mysql_switchover(MXS_MONITOR* mon, SERVER* new_master, SERVER* current_mast
startMonitor(mon, mon->parameters); startMonitor(mon, mon->parameters);
} }
} }
}
else
{
rv = false;
}
return rv; return rv;
} }
@ -370,17 +327,15 @@ bool mysql_switchover(MXS_MONITOR* mon, SERVER* new_master, SERVER* current_mast
*/ */
bool mysql_handle_switchover(const MODULECMD_ARG* args, json_t** output) bool mysql_handle_switchover(const MODULECMD_ARG* args, json_t** output)
{ {
ss_dassert(args->argc == 3); ss_dassert((args->argc == 2) || (args->argc == 3));
ss_dassert(MODULECMD_GET_TYPE(&args->argv[0].type) == MODULECMD_ARG_MONITOR); ss_dassert(MODULECMD_GET_TYPE(&args->argv[0].type) == MODULECMD_ARG_MONITOR);
ss_dassert(MODULECMD_GET_TYPE(&args->argv[1].type) == MODULECMD_ARG_SERVER); ss_dassert(MODULECMD_GET_TYPE(&args->argv[1].type) == MODULECMD_ARG_SERVER);
ss_dassert((MODULECMD_GET_TYPE(&args->argv[2].type) == MODULECMD_ARG_SERVER) || ss_dassert((args->argc == 2) ||
(MODULECMD_GET_TYPE(&args->argv[2].type) == MODULECMD_ARG_NONE)); (MODULECMD_GET_TYPE(&args->argv[2].type) == MODULECMD_ARG_SERVER));
MXS_MONITOR* mon = args->argv[0].value.monitor; MXS_MONITOR* mon = args->argv[0].value.monitor;
SERVER* new_master = args->argv[1].value.server; SERVER* new_master = args->argv[1].value.server;
SERVER* current_master = SERVER* current_master = (args->argc == 3) ? args->argv[2].value.server : NULL;
(MODULECMD_GET_TYPE(&args->argv[2].type) == MODULECMD_ARG_SERVER) ?
args->argv[2].value.server : NULL;
return mysql_switchover(mon, new_master, current_master, output); return mysql_switchover(mon, new_master, current_master, output);
} }