Make connections and command queues internal to Backends
The SERVER_REF and DCB members of the Backend class are now private. Access to the stored SERVER_REF is provided with the backend() function. No accompanying setter function is provided as the backend server should not change during the lifetime of the session. The creation of the internal DCB is hidden behind the connect() function. It simplifies the process of connecting to a server by removing the need to manually do the bookkeeping of the server reference connection counts. Access to the DCB is provided by the dcb() function. The closing of the backend is done with the close() function which contains the code that was previously in closeSession. If the backend isn't closed when the destructor is called, it will be done automatically. This should prevent connection leakage. The pending command queues and the methods used to write them are now also internal to the backends. They are simple wrappers around dcb->func.write and the interfaces provided by the Buffer class. The mapping command queue is still public. It needs to be combined with the generic command queue.
This commit is contained in:
@ -18,27 +18,62 @@
|
||||
using namespace schemarouter;
|
||||
|
||||
Backend::Backend(SERVER_REF *ref):
|
||||
m_closed(false),
|
||||
m_backend(ref),
|
||||
m_dcb(NULL),
|
||||
m_map_queue(NULL),
|
||||
m_mapped(false),
|
||||
m_num_mapping_eof(0),
|
||||
m_num_result_wait(0),
|
||||
m_pending_cmd(NULL),
|
||||
m_state(0)
|
||||
{
|
||||
}
|
||||
|
||||
Backend::~Backend()
|
||||
{
|
||||
ss_dassert(m_closed);
|
||||
|
||||
if (!m_closed)
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
gwbuf_free(m_map_queue);
|
||||
gwbuf_free(m_pending_cmd);
|
||||
}
|
||||
|
||||
void Backend::close()
|
||||
{
|
||||
if (!m_closed)
|
||||
{
|
||||
m_closed = true;
|
||||
|
||||
if (BREF_IS_IN_USE(this))
|
||||
{
|
||||
CHK_DCB(m_dcb);
|
||||
|
||||
/** Clean operation counter in bref and in SERVER */
|
||||
while (BREF_IS_WAITING_RESULT(this))
|
||||
{
|
||||
clear_state(BREF_WAITING_RESULT);
|
||||
}
|
||||
clear_state(BREF_IN_USE);
|
||||
set_state(BREF_CLOSED);
|
||||
|
||||
dcb_close(m_dcb);
|
||||
|
||||
/** decrease server current connection counters */
|
||||
atomic_add(&m_backend->connections, -1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ss_dassert(false);
|
||||
}
|
||||
}
|
||||
|
||||
bool Backend::execute_sescmd()
|
||||
{
|
||||
if (BREF_IS_CLOSED(this))
|
||||
if (BREF_IS_CLOSED(this) || m_session_commands.size() == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -106,3 +141,54 @@ void Backend::set_state(enum bref_state state)
|
||||
ss_dassert(prev2 >= 0);
|
||||
}
|
||||
}
|
||||
|
||||
SERVER_REF* Backend::backend() const
|
||||
{
|
||||
return m_backend;
|
||||
}
|
||||
|
||||
bool Backend::connect(MXS_SESSION* session)
|
||||
{
|
||||
bool rval = false;
|
||||
|
||||
if ((m_dcb = dcb_connect(m_backend->server, session, m_backend->server->protocol)))
|
||||
{
|
||||
m_state = BREF_IN_USE;
|
||||
atomic_add(&m_backend->connections, 1);
|
||||
rval = true;
|
||||
}
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
DCB* Backend::dcb() const
|
||||
{
|
||||
return m_dcb;
|
||||
}
|
||||
|
||||
bool Backend::write(GWBUF* buffer)
|
||||
{
|
||||
return m_dcb->func.write(m_dcb, buffer) != 0;
|
||||
}
|
||||
|
||||
void Backend::store_command(GWBUF* buffer)
|
||||
{
|
||||
m_pending_cmd.reset(buffer);
|
||||
}
|
||||
|
||||
bool Backend::write_stored_command()
|
||||
{
|
||||
bool rval = false;
|
||||
|
||||
if (m_pending_cmd.length())
|
||||
{
|
||||
rval = write(m_pending_cmd.release());
|
||||
|
||||
if (!rval)
|
||||
{
|
||||
MXS_ERROR("Routing of pending query failed.");
|
||||
}
|
||||
}
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
Reference in New Issue
Block a user