Move state checks inside the Backend class

The class now has methods to query its internal state.
This commit is contained in:
Markus Mäkelä
2017-04-03 18:51:13 +03:00
parent 0fde5f22df
commit 0d7f987598
4 changed files with 147 additions and 22 deletions

View File

@ -47,12 +47,12 @@ void Backend::close()
{
m_closed = true;
if (BREF_IS_IN_USE(this))
if (in_use())
{
CHK_DCB(m_dcb);
/** Clean operation counter in bref and in SERVER */
while (BREF_IS_WAITING_RESULT(this))
while (is_waiting_result())
{
clear_state(BREF_WAITING_RESULT);
}
@ -73,7 +73,7 @@ void Backend::close()
bool Backend::execute_sescmd()
{
if (BREF_IS_CLOSED(this) || m_session_commands.size() == 0)
if (is_closed() || m_session_commands.size() == 0)
{
return false;
}
@ -192,3 +192,28 @@ bool Backend::write_stored_command()
return rval;
}
bool Backend::in_use() const
{
return m_state & BREF_IN_USE;
}
bool Backend::is_waiting_result() const
{
return m_num_result_wait > 0;
}
bool Backend::is_query_active() const
{
return m_state & BREF_QUERY_ACTIVE;
}
bool Backend::is_closed() const
{
return m_state & BREF_CLOSED;
}
bool Backend::is_mapped() const
{
return m_mapped;
}

View File

@ -51,14 +51,6 @@ enum bref_state
BREF_DB_MAPPED = 0x10
};
// TODO: Move these as member functions, currently they operate on iterators
#define BREF_IS_NOT_USED(s) ((s)->m_state & ~BREF_IN_USE)
#define BREF_IS_IN_USE(s) ((s)->m_state & BREF_IN_USE)
#define BREF_IS_WAITING_RESULT(s) ((s)->m_num_result_wait > 0)
#define BREF_IS_QUERY_ACTIVE(s) ((s)->m_state & BREF_QUERY_ACTIVE)
#define BREF_IS_CLOSED(s) ((s)->m_state & BREF_CLOSED)
#define BREF_IS_MAPPED(s) ((s)->m_mapped)
namespace schemarouter
{
/**
@ -125,19 +117,127 @@ struct Stats
class Backend
{
public:
/**
* @brief Create new Backend
*
* @param ref Server reference used by this backend
*/
Backend(SERVER_REF *ref);
~Backend();
/**
* @brief Execute the next session command
*
* @return True if the command was executed successfully
*/
bool execute_sescmd();
/**
* @brief Clear state
*
* @param state State to clear
*/
void clear_state(enum bref_state state);
/**
* @brief Set state
*
* @param state State to set
*/
void set_state(enum bref_state state);
/**
* @brief Get pointer to server reference
*
* @return Pointer to server reference
*/
SERVER_REF* backend() const;
bool connect(MXS_SESSION*);
/**
* @brief Create a new connection
*
* @param session The session to which the connection is linked
*
* @return True if connection was successfully created
*/
bool connect(MXS_SESSION* session);
/**
* @brief Close the backend
*
* This will close all active connections created by the backend.
*/
void close();
/**
* @brief Get a pointer to the internal DCB
*
* @return Pointer to internal DCB
*/
DCB* dcb() const;
/**
* @brief Write data to the backend server
*
* @param buffer Buffer containing the data to write
*
* @return True if data was written successfully
*/
bool write(GWBUF* buffer);
/**
* @brief Store a command
*
* The command is stored and executed once the session can execute
* the next command.
*
* @param buffer Buffer to store
*/
void store_command(GWBUF* buffer);
/**
* @brief Write the stored command to the backend server
*
* @return True if command was written successfully
*/
bool write_stored_command();
/**
* @brief Check if backend is in use
*
* @return True if backend is in use
*/
bool in_use() const;
/**
* @brief Check if backend is waiting for a result
*
* @return True if backend is waiting for a result
*/
bool is_waiting_result() const;
/**
* @brief Check if a query is active
*
* @return True if a query is active
*/
bool is_query_active() const;
/**
* @brief Check if the backend is closed
*
* @return True if the backend is closed
*/
bool is_closed() const;
/**
* @brief Check if the backend has been mapped
*
* @return True if the backend has been mapped
*/
bool is_mapped() const;
private:
bool m_closed; /**< True if a connection has been opened and closed */
SERVER_REF* m_backend; /**< Backend server */

View File

@ -231,7 +231,7 @@ bool connect_backend_servers(BackendList& backends, MXS_SESSION* session)
servers_found += 1;
/** Server is already connected */
if (BREF_IS_IN_USE((*it)))
if ((*it)->in_use())
{
slaves_connected += 1;
}
@ -266,7 +266,7 @@ bool connect_backend_servers(BackendList& backends, MXS_SESSION* session)
{
SERVER_REF* b = (*it)->backend();
if (BREF_IS_IN_USE((*it)))
if ((*it)->in_use())
{
MXS_INFO("Connected %s in \t%s:%d",
STRSRVSTATUS(b->server),

View File

@ -513,7 +513,7 @@ void SchemaRouterSession::process_response(SBackend& bref, GWBUF** ppPacket)
bref->clear_state(BREF_WAITING_RESULT);
}
}
else if (BREF_IS_QUERY_ACTIVE(bref))
else if (bref->is_query_active())
{
bref->clear_state(BREF_QUERY_ACTIVE);
/** Set response status as replied */
@ -605,7 +605,7 @@ void SchemaRouterSession::handleError(GWBUF* pMessage,
switch (action)
{
case ERRACT_NEW_CONNECTION:
if (BREF_IS_WAITING_RESULT(bref))
if (bref->is_waiting_result())
{
/** If the client is waiting for a reply, send an error. */
m_client->func.write(m_client, gwbuf_clone(pMessage));
@ -729,7 +729,7 @@ bool SchemaRouterSession::route_session_write(GWBUF* querybuf, uint8_t command)
for (BackendList::iterator it = m_backends.begin(); it != m_backends.end(); it++)
{
if (BREF_IS_IN_USE(*it))
if ((*it)->in_use())
{
GWBUF *buffer = gwbuf_clone(querybuf);
(*it)->m_session_commands.push_back(SessionCommand(buffer, m_sent_sescmd));
@ -794,7 +794,7 @@ bool SchemaRouterSession::have_servers()
{
for (BackendList::iterator it = m_backends.begin(); it != m_backends.end(); it++)
{
if (BREF_IS_IN_USE(*it) && !BREF_IS_CLOSED(*it))
if ((*it)->in_use() && !(*it)->is_closed())
{
return true;
}
@ -1030,7 +1030,7 @@ int SchemaRouterSession::inspect_backend_mapping_states(SBackend& bref,
for (BackendList::iterator it = m_backends.begin(); it != m_backends.end(); it++)
{
if (bref->dcb() == (*it)->dcb() && !BREF_IS_MAPPED(*it))
if (bref->dcb() == (*it)->dcb() && !(*it)->is_mapped())
{
if (bref->m_map_queue)
{
@ -1099,7 +1099,7 @@ int SchemaRouterSession::inspect_backend_mapping_states(SBackend& bref,
}
}
if (BREF_IS_IN_USE(*it) && !BREF_IS_MAPPED(*it))
if ((*it)->in_use() && !(*it)->is_mapped())
{
mapped = false;
MXS_DEBUG("Still waiting for reply to SHOW DATABASES from %s for session %p",
@ -1416,7 +1416,7 @@ void SchemaRouterSession::gen_databaselist()
for (BackendList::iterator it = m_backends.begin(); it != m_backends.end(); it++)
{
if (BREF_IS_IN_USE(*it) && !BREF_IS_CLOSED(*it) &
if ((*it)->in_use() && !(*it)->is_closed() &
SERVER_IS_RUNNING((*it)->backend()->server))
{
clone = gwbuf_clone(buffer);
@ -1577,7 +1577,7 @@ bool SchemaRouterSession::get_shard_dcb(DCB** p_dcb, char* name)
* backend must be in use, name must match, and
* the backend state must be RUNNING
*/
if (BREF_IS_IN_USE((*it)) &&
if ((*it)->in_use() &&
(strncasecmp(name, b->server->unique_name, PATH_MAX) == 0) &&
SERVER_IS_RUNNING(b->server))
{