Add name and uri helpers to Backend

Providing helper functions for the commonly used parts of the server makes
code easier to read. It also removes any possibility for formatting
problems by moving the URI and name string handling inside the Backend
class.
This commit is contained in:
Markus Mäkelä
2017-06-19 14:01:03 +03:00
parent d7543988ee
commit c7520a2156
7 changed files with 61 additions and 32 deletions

View File

@ -15,6 +15,7 @@
#include <maxscale/cppdefs.hh> #include <maxscale/cppdefs.hh>
#include <list> #include <list>
#include <string>
#include <tr1/memory> #include <tr1/memory>
#include <maxscale/service.h> #include <maxscale/service.h>
@ -251,6 +252,21 @@ public:
*/ */
bool has_failed() const; bool has_failed() const;
/**
* @brief Get the object name of this server
*
* @return The unique object name of this server
*/
const char* name() const;
/**
* @brief Get the address and port as a string
*
* @return The address and port combined into one string
*/
const char* uri() const;
private: private:
/** /**
* Internal state of the backend * Internal state of the backend
@ -284,6 +300,7 @@ private:
int m_state; /**< State of the backend */ int m_state; /**< State of the backend */
SessionCommandList m_session_commands; /**< List of session commands that are SessionCommandList m_session_commands; /**< List of session commands that are
* to be executed on this backend server */ * to be executed on this backend server */
std::string m_uri; /**< The combined address and port */
}; };
typedef std::tr1::shared_ptr<Backend> SBackend; typedef std::tr1::shared_ptr<Backend> SBackend;

View File

@ -12,6 +12,9 @@
*/ */
#include <maxscale/backend.hh> #include <maxscale/backend.hh>
#include <sstream>
#include <maxscale/protocol/mysql.h> #include <maxscale/protocol/mysql.h>
#include <maxscale/debug.h> #include <maxscale/debug.h>
@ -23,6 +26,9 @@ Backend::Backend(SERVER_REF *ref):
m_dcb(NULL), m_dcb(NULL),
m_state(0) m_state(0)
{ {
std::stringstream ss;
ss << "[" << server()->name << "]:" << server()->port;
m_uri = ss.str();
} }
Backend::~Backend() Backend::~Backend()
@ -298,3 +304,13 @@ bool Backend::has_failed() const
{ {
return m_state & FATAL_FAILURE; return m_state & FATAL_FAILURE;
} }
const char* Backend::name() const
{
return m_backend->server->unique_name;
}
const char* Backend::uri() const
{
return m_uri.c_str();
}

View File

@ -360,7 +360,7 @@ static bool reroute_stored_statement(ROUTER_CLIENT_SES *rses, const SRWBackend&
/** Found a valid candidate; a non-master slave that's in use */ /** Found a valid candidate; a non-master slave that's in use */
if (backend->write(stored)) if (backend->write(stored))
{ {
MXS_INFO("Retrying failed read at '%s'.", backend->server()->unique_name); MXS_INFO("Retrying failed read at '%s'.", backend->name());
ss_dassert(backend->get_reply_state() == REPLY_STATE_DONE); ss_dassert(backend->get_reply_state() == REPLY_STATE_DONE);
LOG_RS(backend, REPLY_STATE_START); LOG_RS(backend, REPLY_STATE_START);
backend->set_reply_state(REPLY_STATE_START); backend->set_reply_state(REPLY_STATE_START);
@ -379,7 +379,7 @@ static bool reroute_stored_statement(ROUTER_CLIENT_SES *rses, const SRWBackend&
*/ */
if (rses->current_master->write(stored)) if (rses->current_master->write(stored))
{ {
MXS_INFO("Retrying failed read at '%s'.", rses->current_master->server()->unique_name); MXS_INFO("Retrying failed read at '%s'.", rses->current_master->name());
LOG_RS(rses->current_master, REPLY_STATE_START); LOG_RS(rses->current_master, REPLY_STATE_START);
ss_dassert(rses->current_master->get_reply_state() == REPLY_STATE_DONE); ss_dassert(rses->current_master->get_reply_state() == REPLY_STATE_DONE);
rses->current_master->set_reply_state(REPLY_STATE_START); rses->current_master->set_reply_state(REPLY_STATE_START);
@ -1178,8 +1178,8 @@ static void clientReply(MXS_ROUTER *instance,
/** Check pending session commands */ /** Check pending session commands */
else if (!queue_routed && backend->session_command_count()) else if (!queue_routed && backend->session_command_count())
{ {
MXS_INFO("Backend [%s]:%d processed reply and starts to execute active cursor.", MXS_INFO("Backend %s processed reply and starts to execute active cursor.",
backend->server()->name, backend->server()->port); backend->uri());
if (backend->execute_session_command()) if (backend->execute_session_command())
{ {
@ -1331,7 +1331,7 @@ static void handleError(MXS_ROUTER *instance,
{ {
ss_dassert(false); ss_dassert(false);
MXS_ERROR("Backend '%s' is still in use and points to the problem DCB.", MXS_ERROR("Backend '%s' is still in use and points to the problem DCB.",
backend->server()->unique_name); backend->name());
} }
} }
else else

View File

@ -292,9 +292,8 @@ void check_session_command_reply(GWBUF *buffer, SRWBackend backend)
err.append(replybuf + 8, 5); err.append(replybuf + 8, 5);
msg.append(replybuf + 13, replylen - 4 - 5); msg.append(replybuf + 13, replylen - 4 - 5);
MXS_ERROR("Failed to execute session command in [%s]:%d. Error was: %s %s", MXS_ERROR("Failed to execute session command in %s. Error was: %s %s",
backend->server()->name, backend->server()->port, backend->uri(), err.c_str(), msg.c_str());
err.c_str(), msg.c_str());
} }
} }

View File

@ -84,7 +84,7 @@ void handle_connection_keepalive(ROUTER_INSTANCE *inst, ROUTER_CLIENT_SES *rses,
if (diff > keepalive) if (diff > keepalive)
{ {
MXS_INFO("Pinging %s, idle for %d seconds", MXS_INFO("Pinging %s, idle for %d seconds",
backend->server()->unique_name, diff / 10); backend->name(), diff / 10);
modutil_ignorable_ping(backend->dcb()); modutil_ignorable_ping(backend->dcb());
} }
} }
@ -268,14 +268,13 @@ bool route_session_write(ROUTER_CLIENT_SES *rses, GWBUF *querybuf, uint8_t comma
rses->expected_responses++; rses->expected_responses++;
} }
MXS_INFO("Route query to %s \t[%s]:%d", MXS_INFO("Route query to %s \t%s",
backend->is_master() ? "master" : "slave", backend->is_master() ? "master" : "slave",
backend->server()->name, backend->server()->port); backend->uri());
} }
else else
{ {
MXS_ERROR("Failed to execute session command in [%s]:%d", MXS_ERROR("Failed to execute session command in %s", backend->uri());
backend->server()->name, backend->server()->port);
} }
} }
} }
@ -335,7 +334,7 @@ SRWBackend get_target_backend(ROUTER_CLIENT_SES *rses, backend_type_t btype,
if (rses->target_node && session_trx_is_read_only(rses->client_dcb->session)) if (rses->target_node && session_trx_is_read_only(rses->client_dcb->session))
{ {
MXS_DEBUG("In READ ONLY transaction, using server '%s'", MXS_DEBUG("In READ ONLY transaction, using server '%s'",
rses->target_node->server()->unique_name); rses->target_node->name());
return rses->target_node; return rses->target_node;
} }
@ -354,7 +353,7 @@ SRWBackend get_target_backend(ROUTER_CLIENT_SES *rses, backend_type_t btype,
/** The server must be a valid slave, relay server, or master */ /** The server must be a valid slave, relay server, or master */
if (backend->in_use() && backend->is_active() && if (backend->in_use() && backend->is_active() &&
(strcasecmp(name, backend->server()->unique_name) == 0) && (strcasecmp(name, backend->name()) == 0) &&
(backend->is_slave() || (backend->is_slave() ||
backend->is_relay() || backend->is_relay() ||
backend->is_master())) backend->is_master()))
@ -443,10 +442,9 @@ SRWBackend get_target_backend(ROUTER_CLIENT_SES *rses, backend_type_t btype,
} }
else else
{ {
MXS_INFO("Server [%s]:%d is too much behind the master " MXS_INFO("Server %s is too much behind the master "
"(%d seconds) and can't be chosen", "(%d seconds) and can't be chosen",
backend->server()->name, backend->server()->port, backend->uri(), backend->server()->rlag);
backend->server()->rlag);
} }
} }
} /*< for */ } /*< for */
@ -475,14 +473,14 @@ SRWBackend get_target_backend(ROUTER_CLIENT_SES *rses, backend_type_t btype,
{ {
MXS_ERROR("Server '%s' should be master but is %s instead " MXS_ERROR("Server '%s' should be master but is %s instead "
"and can't be chosen as the master.", "and can't be chosen as the master.",
master->server()->unique_name, master->name(),
STRSRVSTATUS(&server)); STRSRVSTATUS(&server));
} }
} }
else else
{ {
MXS_ERROR("Server '%s' is not in use and can't be chosen as the master.", MXS_ERROR("Server '%s' is not in use and can't be chosen as the master.",
master->server()->unique_name); master->name());
} }
} }
} }
@ -908,24 +906,24 @@ static void log_master_routing_failure(ROUTER_CLIENT_SES *rses, bool found,
{ {
/** We found a master but it's not the same connection */ /** We found a master but it's not the same connection */
ss_dassert(old_master != curr_master); ss_dassert(old_master != curr_master);
if (old_master->server() != curr_master->server()) if (old_master != curr_master)
{ {
sprintf(errmsg, "Master server changed from '%s' to '%s'", sprintf(errmsg, "Master server changed from '%s' to '%s'",
old_master->server()->unique_name, old_master->name(),
curr_master->server()->unique_name); curr_master->name());
} }
else else
{ {
ss_dassert(false); // Currently we don't reconnect to the master ss_dassert(false); // Currently we don't reconnect to the master
sprintf(errmsg, "Connection to master '%s' was recreated", sprintf(errmsg, "Connection to master '%s' was recreated",
curr_master->server()->unique_name); curr_master->name());
} }
} }
else if (old_master) else if (old_master)
{ {
/** We have an original master connection but we couldn't find it */ /** We have an original master connection but we couldn't find it */
sprintf(errmsg, "The connection to master server '%s' is not available", sprintf(errmsg, "The connection to master server '%s' is not available",
old_master->server()->unique_name); old_master->name());
} }
else else
{ {
@ -1024,12 +1022,11 @@ handle_got_target(ROUTER_INSTANCE *inst, ROUTER_CLIENT_SES *rses,
{ {
rses->target_node = target; rses->target_node = target;
MXS_DEBUG("Setting forced_node SLAVE to %s within an opened READ ONLY transaction", MXS_DEBUG("Setting forced_node SLAVE to %s within an opened READ ONLY transaction",
target->server()->unique_name); target->name());
} }
MXS_INFO("Route query to %s \t[%s]:%d <", MXS_INFO("Route query to %s \t%s <", target->is_master() ? "master" : "slave",
(target->is_master() ? "master" : "slave"), target->uri());
target->server()->name, target->server()->port);
/** The session command cursor must not be active */ /** The session command cursor must not be active */
ss_dassert(target->session_command_count() == 0); ss_dassert(target->session_command_count() == 0);

View File

@ -419,8 +419,8 @@ bool select_connect_backend_servers(int router_nservers,
SRWBackend& backend = *it; SRWBackend& backend = *it;
if (backend->in_use()) if (backend->in_use())
{ {
MXS_INFO("Selected %s in \t[%s]:%d", STRSRVSTATUS(backend->server()), MXS_INFO("Selected %s in \t%s", STRSRVSTATUS(backend->server()),
backend->server()->name, backend->server()->port); backend->uri());
} }
} }
} }

View File

@ -61,7 +61,7 @@ void process_sescmd_response(ROUTER_CLIENT_SES* rses, SRWBackend& backend,
{ {
MXS_ERROR("Slave server '%s': response differs from master's response. " MXS_ERROR("Slave server '%s': response differs from master's response. "
"Closing connection due to inconsistent session state.", "Closing connection due to inconsistent session state.",
backend->server()->unique_name); backend->name());
backend->close(mxs::Backend::CLOSE_FATAL); backend->close(mxs::Backend::CLOSE_FATAL);
*pReconnect = true; *pReconnect = true;
} }