MXS-2196: Allocate a session before allocating DCBs

Allocating the session before a DCB guarantees that at no point will a DCB
have a null session. This further clarifies the concept of the session and
also allows the listener reference to be moved there.

Ideally, the session itself would allocate and assign the client DCB but
since the Listener is the only one who does it, it's acceptable for now.
This commit is contained in:
Markus Mäkelä
2018-12-03 19:25:26 +02:00
parent 692127a2cb
commit 43c33e9f4a
20 changed files with 106 additions and 115 deletions

View File

@ -29,8 +29,6 @@
#include <memory>
class SERVICE;
class Listener;
using SListener = std::shared_ptr<Listener>;
MXS_BEGIN_DECLS
@ -153,7 +151,7 @@ typedef enum
*/
struct DCB : public MXB_POLL_DATA
{
DCB(dcb_role_t role, const SListener& listener, SERVICE* service);
DCB(dcb_role_t role, MXS_SESSION*);
~DCB();
/**
@ -173,7 +171,6 @@ struct DCB : public MXB_POLL_DATA
size_t protocol_packet_length = 0; /**< protocol packet length */
size_t protocol_bytes_processed = 0; /**< How many bytes have been read */
MXS_SESSION* session; /**< The owning session */
SListener listener; /**< The origin of the connection */
MXS_PROTOCOL func = {}; /**< Protocol functions for the DCB */
MXS_AUTHENTICATOR authfunc = {}; /**< Authenticator functions for the DCB */
uint64_t writeqlen = 0; /**< Bytes in writeq */
@ -243,7 +240,7 @@ typedef enum
void dcb_global_init();
int dcb_write(DCB*, GWBUF*);
DCB* dcb_alloc(dcb_role_t, const SListener&, SERVICE* service);
DCB* dcb_alloc(dcb_role_t, MXS_SESSION*);
DCB* dcb_connect(struct server*, MXS_SESSION*, const char*);
int dcb_read(DCB*, GWBUF**, int);
int dcb_drain_writeq(DCB*);

View File

@ -30,6 +30,8 @@ struct mxs_filter;
struct mxs_filter_session;
struct mxs_router_session;
struct server;
class Listener;
using SListener = std::shared_ptr<Listener>;
typedef enum
{
@ -199,12 +201,13 @@ typedef char* (* session_variable_handler_t)(void* context,
*/
struct MXS_SESSION
{
MXS_SESSION(DCB* client_dcb);
~MXS_SESSION();
MXS_SESSION(const SListener& listener);
virtual ~MXS_SESSION();
mxs_session_state_t state; /*< Current descriptor state */
uint64_t ses_id; /*< Unique session identifier */
DCB* client_dcb; /*< The client connection */
SListener listener; /*< The origin of the connection */
struct mxs_router_session* router_session; /*< The router instance data */
MXS_SESSION_STATS stats; /*< Session statistics */
@ -274,19 +277,6 @@ bool session_route_reply(MXS_SESSION* session, GWBUF* buffer);
*/
#define MXS_SESSION_ROUTE_REPLY(sess, buf) session_route_reply(sess, buf)
/**
* Allocate a new session for a new client of the specified service.
*
* The start_session function needs to be called after the user authentication is done. This will
* trigger the creation of router and filter sessions.
*
* @param service The service this connection was established by
* @param client_dcb The client side DCB
*
* @return The newly created session or NULL if an error occurred
*/
MXS_SESSION* session_alloc(SERVICE*, DCB*);
/**
* Start the session
*