MXS-2196: Rename session states

Minor renaming of the session state enum values. Also exposed the session
state stringification function in the public header and removed the
stringification macro.
This commit is contained in:
Markus Mäkelä
2018-12-05 14:34:45 +02:00
parent 7aaadfc550
commit 20fe9b9dca
15 changed files with 39 additions and 92 deletions

View File

@ -375,7 +375,7 @@ void RRRouter::handle_error(RRRouterSession* rses,
case ERRACT_REPLY_CLIENT: case ERRACT_REPLY_CLIENT:
{ {
/* React to failed authentication, send message to client */ /* React to failed authentication, send message to client */
if (sesstate == SESSION_STATE_ROUTER_READY) if (sesstate == SESSION_STATE_STARTED)
{ {
/* Send error report to client */ /* Send error report to client */
GWBUF* copy = gwbuf_clone(message); GWBUF* copy = gwbuf_clone(message);

View File

@ -34,52 +34,14 @@ using SListener = std::shared_ptr<Listener>;
typedef enum typedef enum
{ {
SESSION_STATE_ALLOC, /*< for all sessions */ SESSION_STATE_CREATED, /*< Session created but not started */
SESSION_STATE_READY, /*< for router session */ SESSION_STATE_STARTED, /*< Session is fully functional */
SESSION_STATE_ROUTER_READY, /*< for router session */
SESSION_STATE_STOPPING, /*< session and router are being closed */ SESSION_STATE_STOPPING, /*< session and router are being closed */
SESSION_STATE_LISTENER, /*< for listener session */
SESSION_STATE_LISTENER_STOPPED, /*< for listener session */
SESSION_STATE_TO_BE_FREED, /*< ready to be freed as soon as there are no references */ SESSION_STATE_TO_BE_FREED, /*< ready to be freed as soon as there are no references */
SESSION_STATE_FREE, /*< for all sessions */ SESSION_STATE_FREE, /*< for all sessions */
} mxs_session_state_t; } mxs_session_state_t;
#define STRSESSIONSTATE(s) \ const char* session_state_to_string(mxs_session_state_t);
((s) == SESSION_STATE_ALLOC ? "SESSION_STATE_ALLOC" \
: ((s) == SESSION_STATE_READY ? "SESSION_STATE_READY" \
: ((s) \
== SESSION_STATE_ROUTER_READY \
? "SESSION_STATE_ROUTER_READY" \
: ((s) \
== \
SESSION_STATE_STOPPING \
? \
"SESSION_STATE_STOPPING" \
: (( \
s) \
== \
SESSION_STATE_LISTENER \
? \
"SESSION_STATE_LISTENER" \
: (( \
s) \
== \
SESSION_STATE_LISTENER_STOPPED \
? \
"SESSION_STATE_LISTENER_STOPPED" \
: (( \
s) \
== \
SESSION_STATE_TO_BE_FREED \
? \
"SESSION_STATE_TO_BE_FREED" \
: (( \
s) \
== \
SESSION_STATE_FREE \
? \
"SESSION_STATE_TO_BE_FREE" \
: "SESSION_STATE_UNKNOWN"))))))))
typedef enum typedef enum
{ {

View File

@ -33,11 +33,6 @@
#define MXS_UPSTREAM_INIT {0} #define MXS_UPSTREAM_INIT {0}
#define SESSION_FILTER_INIT {0} #define SESSION_FILTER_INIT {0}
#define SESSION_INIT \
{.ses_chk_top = CHK_NUM_SESSION, \
.stats = SESSION_STATS_INIT, .head = MXS_DOWNSTREAM_INIT, .tail = MXS_UPSTREAM_INIT, \
.state = SESSION_STATE_ALLOC, .client_protocol_data = 0, .ses_chk_tail = CHK_NUM_SESSION}
#define SESSION_PROTOCOL(x, type) DCB_PROTOCOL((x)->client_dcb, type) #define SESSION_PROTOCOL(x, type) DCB_PROTOCOL((x)->client_dcb, type)
MXS_BEGIN_DECLS MXS_BEGIN_DECLS
@ -52,7 +47,6 @@ typedef enum
} SESSIONLISTFILTER; } SESSIONLISTFILTER;
int session_isvalid(MXS_SESSION*); int session_isvalid(MXS_SESSION*);
const char* session_state(mxs_session_state_t);
/** /**
* Link a session to a backend DCB. * Link a session to a backend DCB.

View File

@ -87,7 +87,7 @@ static void session_deliver_response(MXS_SESSION* session);
static int session_reply(MXS_FILTER* inst, MXS_FILTER_SESSION* session, GWBUF* data); static int session_reply(MXS_FILTER* inst, MXS_FILTER_SESSION* session, GWBUF* data);
MXS_SESSION::MXS_SESSION(const SListener& listener) MXS_SESSION::MXS_SESSION(const SListener& listener)
: state(SESSION_STATE_READY) : state(SESSION_STATE_CREATED)
, ses_id(session_get_next_id()) , ses_id(session_get_next_id())
, client_dcb(nullptr) , client_dcb(nullptr)
, listener(listener) , listener(listener)
@ -155,7 +155,7 @@ bool session_start(MXS_SESSION* session)
return false; return false;
} }
session->state = SESSION_STATE_ROUTER_READY; session->state = SESSION_STATE_STARTED;
mxb::atomic::add(&session->service->stats.n_sessions, 1, mxb::atomic::RELAXED); mxb::atomic::add(&session->service->stats.n_sessions, 1, mxb::atomic::RELAXED);
mxb::atomic::add(&session->service->stats.n_current, 1, mxb::atomic::RELAXED); mxb::atomic::add(&session->service->stats.n_current, 1, mxb::atomic::RELAXED);
@ -317,7 +317,7 @@ void printSession(MXS_SESSION* session)
char timebuf[40]; char timebuf[40];
printf("Session %p\n", session); printf("Session %p\n", session);
printf("\tState: %s\n", session_state(session->state)); printf("\tState: %s\n", session_state_to_string(session->state));
printf("\tService: %s (%p)\n", session->service->name, session->service); printf("\tService: %s (%p)\n", session->service->name, session->service);
printf("\tClient DCB: %p\n", session->client_dcb); printf("\tClient DCB: %p\n", session->client_dcb);
printf("\tConnected: %s\n", printf("\tConnected: %s\n",
@ -386,7 +386,7 @@ void dprintSession(DCB* dcb, MXS_SESSION* print_session)
int i; int i;
dcb_printf(dcb, "Session %" PRIu64 "\n", print_session->ses_id); dcb_printf(dcb, "Session %" PRIu64 "\n", print_session->ses_id);
dcb_printf(dcb, "\tState: %s\n", session_state(print_session->state)); dcb_printf(dcb, "\tState: %s\n", session_state_to_string(print_session->state));
dcb_printf(dcb, "\tService: %s\n", print_session->service->name); dcb_printf(dcb, "\tService: %s\n", print_session->service->name);
if (print_session->client_dcb && print_session->client_dcb->remote) if (print_session->client_dcb && print_session->client_dcb->remote)
@ -429,7 +429,7 @@ bool dListSessions_cb(DCB* dcb, void* data)
session->client_dcb->remote : "", session->client_dcb->remote : "",
session->service && session->service->name ? session->service && session->service->name ?
session->service->name : "", session->service->name : "",
session_state(session->state)); session_state_to_string(session->state));
} }
return true; return true;
@ -459,24 +459,15 @@ void dListSessions(DCB* dcb)
* @param state The session state * @param state The session state
* @return A string representation of the session state * @return A string representation of the session state
*/ */
const char* session_state(mxs_session_state_t state) const char* session_state_to_string(mxs_session_state_t state)
{ {
switch (state) switch (state)
{ {
case SESSION_STATE_ALLOC: case SESSION_STATE_CREATED:
return "Session Allocated"; return "Session created";
case SESSION_STATE_READY: case SESSION_STATE_STARTED:
return "Session Ready"; return "Session started";
case SESSION_STATE_ROUTER_READY:
return "Session ready for routing";
case SESSION_STATE_LISTENER:
return "Listener Session";
case SESSION_STATE_LISTENER_STOPPED:
return "Stopped Listener Session";
case SESSION_STATE_STOPPING: case SESSION_STATE_STOPPING:
return "Stopping session"; return "Stopping session";
@ -610,7 +601,7 @@ bool dcb_iter_cb(DCB* dcb, void* data)
char buf[20]; char buf[20];
snprintf(buf, sizeof(buf), "%p", ses); snprintf(buf, sizeof(buf), "%p", ses);
set->add_row({buf, ses->client_dcb->remote, ses->service->name, session_state(ses->state)}); set->add_row({buf, ses->client_dcb->remote, ses->service->name, session_state_to_string(ses->state)});
} }
return true; return true;
@ -761,7 +752,7 @@ json_t* session_json_data(const Session* session, const char* host)
/** Session attributes */ /** Session attributes */
json_t* attr = json_object(); json_t* attr = json_object();
json_object_set_new(attr, "state", json_string(session_state(session->state))); json_object_set_new(attr, "state", json_string(session_state_to_string(session->state)));
if (session->client_dcb->user) if (session->client_dcb->user)
{ {
@ -999,7 +990,7 @@ public:
void execute() void execute()
{ {
if (m_session->state == SESSION_STATE_ROUTER_READY) if (m_session->state == SESSION_STATE_STARTED)
{ {
GWBUF* buffer = m_buffer; GWBUF* buffer = m_buffer;
m_buffer = NULL; m_buffer = NULL;

View File

@ -28,7 +28,7 @@ Session::Session(Client* pClient, const SListener& listener)
memset((void*)pSession, 0, sizeof(MXS_SESSION)); memset((void*)pSession, 0, sizeof(MXS_SESSION));
pSession->state = SESSION_STATE_ALLOC; pSession->state = SESSION_STATE_CREATED;
pSession->client_dcb = &m_client_dcb; pSession->client_dcb = &m_client_dcb;

View File

@ -624,7 +624,7 @@ static inline bool session_ok_to_route(DCB* dcb)
{ {
bool rval = false; bool rval = false;
if (dcb->session->state == SESSION_STATE_ROUTER_READY if (dcb->session->state == SESSION_STATE_STARTED
&& dcb->session->client_dcb != NULL && dcb->session->client_dcb != NULL
&& dcb->session->client_dcb->state == DCB_STATE_POLLING && dcb->session->client_dcb->state == DCB_STATE_POLLING
&& (dcb->session->router_session && (dcb->session->router_session
@ -1292,7 +1292,7 @@ static int gw_error_backend_event(DCB* dcb)
} }
dcb_close(dcb); dcb_close(dcb);
} }
else if (dcb->state != DCB_STATE_POLLING || session->state != SESSION_STATE_ROUTER_READY) else if (dcb->state != DCB_STATE_POLLING || session->state != SESSION_STATE_STARTED)
{ {
int error; int error;
int len = sizeof(error); int len = sizeof(error);
@ -1336,7 +1336,7 @@ static int gw_backend_hangup(DCB* dcb)
if (!dcb->persistentstart) if (!dcb->persistentstart)
{ {
if (session->state != SESSION_STATE_ROUTER_READY) if (session->state != SESSION_STATE_STARTED)
{ {
int error; int error;
int len = sizeof(error); int len = sizeof(error);

View File

@ -773,7 +773,7 @@ static int gw_read_do_authentication(DCB* dcb, GWBUF* read_buffer, int nbytes_re
*/ */
if (session_start(dcb->session)) if (session_start(dcb->session))
{ {
mxb_assert(dcb->session->state != SESSION_STATE_ALLOC); mxb_assert(dcb->session->state != SESSION_STATE_CREATED);
// For the time being only the sql_mode is stored in MXS_SESSION::client_protocol_data. // For the time being only the sql_mode is stored in MXS_SESSION::client_protocol_data.
dcb->session->client_protocol_data = QC_SQL_MODE_DEFAULT; dcb->session->client_protocol_data = QC_SQL_MODE_DEFAULT;
protocol->protocol_auth_state = MXS_AUTH_STATE_COMPLETE; protocol->protocol_auth_state = MXS_AUTH_STATE_COMPLETE;
@ -1048,12 +1048,12 @@ static int gw_read_normal_data(DCB* dcb, GWBUF* read_buffer, int nbytes_read)
session = dcb->session; session = dcb->session;
session_state_value = session->state; session_state_value = session->state;
if (session_state_value != SESSION_STATE_ROUTER_READY) if (session_state_value != SESSION_STATE_STARTED)
{ {
if (session_state_value != SESSION_STATE_STOPPING) if (session_state_value != SESSION_STATE_STOPPING)
{ {
MXS_ERROR("Session received a query in incorrect state %s", MXS_ERROR("Session received a query in incorrect state: %s",
STRSESSIONSTATE(session_state_value)); session_state_to_string(session_state_value));
} }
gwbuf_free(read_buffer); gwbuf_free(read_buffer);
dcb_close(dcb); dcb_close(dcb);
@ -1462,7 +1462,7 @@ static int gw_client_close(DCB* dcb)
{ {
MXS_SESSION* target = dcb->session; MXS_SESSION* target = dcb->session;
if (target->state == SESSION_STATE_ROUTER_READY || target->state == SESSION_STATE_STOPPING) if (target->state == SESSION_STATE_STARTED || target->state == SESSION_STATE_STOPPING)
{ {
MXB_AT_DEBUG(bool removed = ) mxs_rworker_deregister_session(target->ses_id); MXB_AT_DEBUG(bool removed = ) mxs_rworker_deregister_session(target->ses_id);
mxb_assert(removed); mxb_assert(removed);

View File

@ -595,15 +595,15 @@ bool gw_get_shared_session_auth_info(DCB* dcb, MYSQL_session* session)
mxb_assert(dcb->data); mxb_assert(dcb->data);
memcpy(session, dcb->data, sizeof(MYSQL_session)); memcpy(session, dcb->data, sizeof(MYSQL_session));
} }
else if (dcb->session->state != SESSION_STATE_ALLOC) else if (dcb->session->state != SESSION_STATE_CREATED)
{ {
memcpy(session, dcb->session->client_dcb->data, sizeof(MYSQL_session)); memcpy(session, dcb->session->client_dcb->data, sizeof(MYSQL_session));
} }
else else
{ {
mxb_assert(false); mxb_assert(false);
MXS_ERROR("Couldn't get session authentication info. Session in a wrong state %s.", MXS_ERROR("Couldn't get session authentication info. Session in wrong state: %s.",
STRSESSIONSTATE(dcb->session->state)); session_state_to_string(dcb->session->state));
rval = false; rval = false;
} }
@ -970,8 +970,8 @@ mxs_auth_state_t gw_send_backend_auth(DCB* dcb)
mxs_auth_state_t rval = MXS_AUTH_STATE_FAILED; mxs_auth_state_t rval = MXS_AUTH_STATE_FAILED;
if (dcb->session == NULL if (dcb->session == NULL
|| (dcb->session->state != SESSION_STATE_READY || (dcb->session->state != SESSION_STATE_CREATED
&& dcb->session->state != SESSION_STATE_ROUTER_READY) && dcb->session->state != SESSION_STATE_STARTED)
|| (dcb->server->server_ssl || (dcb->server->server_ssl
&& dcb->ssl_state == SSL_HANDSHAKE_FAILED)) && dcb->ssl_state == SSL_HANDSHAKE_FAILED))
{ {

View File

@ -150,7 +150,7 @@ static MXS_ROUTER_SESSION* newSession(MXS_ROUTER* instance, MXS_SESSION* session
inst->sessions = client; inst->sessions = client;
pthread_mutex_unlock(&inst->lock); pthread_mutex_unlock(&inst->lock);
session->state = SESSION_STATE_READY; session->state = SESSION_STATE_CREATED;
return reinterpret_cast<MXS_ROUTER_SESSION*>(client); return reinterpret_cast<MXS_ROUTER_SESSION*>(client);
} }

View File

@ -164,7 +164,7 @@ static MXS_ROUTER_SESSION* newSession(MXS_ROUTER* instance, MXS_SESSION* session
inst->sessions = client; inst->sessions = client;
pthread_mutex_unlock(&inst->lock); pthread_mutex_unlock(&inst->lock);
session->state = SESSION_STATE_READY; session->state = SESSION_STATE_CREATED;
dcb_printf(session->client_dcb, "Welcome to the MariaDB Corporation MaxScale Debug Interface.\n"); dcb_printf(session->client_dcb, "Welcome to the MariaDB Corporation MaxScale Debug Interface.\n");
dcb_printf(session->client_dcb, "Type help for a list of available commands.\n\n"); dcb_printf(session->client_dcb, "Type help for a list of available commands.\n\n");

View File

@ -168,7 +168,7 @@ void HintRouterSession::handleError(GWBUF* pMessage,
case ERRACT_REPLY_CLIENT: case ERRACT_REPLY_CLIENT:
{ {
/* React to failed authentication, send message to client */ /* React to failed authentication, send message to client */
if (sesstate == SESSION_STATE_ROUTER_READY) if (sesstate == SESSION_STATE_STARTED)
{ {
/* Send error report to client */ /* Send error report to client */
GWBUF* pCopy = gwbuf_clone(pMessage); GWBUF* pCopy = gwbuf_clone(pMessage);

View File

@ -192,7 +192,7 @@ static MXS_ROUTER_SESSION* newSession(MXS_ROUTER* instance, MXS_SESSION* session
inst->sessions = client; inst->sessions = client;
pthread_mutex_unlock(&inst->lock); pthread_mutex_unlock(&inst->lock);
session->state = SESSION_STATE_READY; session->state = SESSION_STATE_CREATED;
return reinterpret_cast<MXS_ROUTER_SESSION*>(client); return reinterpret_cast<MXS_ROUTER_SESSION*>(client);
} }
@ -274,7 +274,7 @@ static void handleError(MXS_ROUTER* instance,
client_dcb = session->client_dcb; client_dcb = session->client_dcb;
if (session->state == SESSION_STATE_ROUTER_READY) if (session->state == SESSION_STATE_STARTED)
{ {
client_dcb->func.write(client_dcb, gwbuf_clone(errbuf)); client_dcb->func.write(client_dcb, gwbuf_clone(errbuf));
} }

View File

@ -711,7 +711,7 @@ static void handleError(MXS_ROUTER* instance,
{ {
mxb_assert(problem_dcb->role == DCB::Role::BACKEND); mxb_assert(problem_dcb->role == DCB::Role::BACKEND);
mxb_assert(problem_dcb->session->state == SESSION_STATE_ROUTER_READY); mxb_assert(problem_dcb->session->state == SESSION_STATE_STARTED);
DCB* client_dcb = problem_dcb->session->client_dcb; DCB* client_dcb = problem_dcb->session->client_dcb;
client_dcb->func.write(client_dcb, gwbuf_clone(errbuf)); client_dcb->func.write(client_dcb, gwbuf_clone(errbuf));

View File

@ -1123,7 +1123,7 @@ void RWSplitSession::handle_error_reply_client(DCB* backend_dcb, GWBUF* errmsg)
backend->close(); backend->close();
if (sesstate == SESSION_STATE_ROUTER_READY) if (sesstate == SESSION_STATE_STARTED)
{ {
m_client->func.write(m_client, gwbuf_clone(errmsg)); m_client->func.write(m_client, gwbuf_clone(errmsg));
} }

View File

@ -638,7 +638,7 @@ void SchemaRouterSession::handleError(GWBUF* pMessage,
case ERRACT_REPLY_CLIENT: case ERRACT_REPLY_CLIENT:
// The session pointer can be NULL if the creation fails when filters are being set up // The session pointer can be NULL if the creation fails when filters are being set up
if (m_client->session && m_client->session->state == SESSION_STATE_ROUTER_READY) if (m_client->session && m_client->session->state == SESSION_STATE_STARTED)
{ {
m_client->func.write(m_client, gwbuf_clone(pMessage)); m_client->func.write(m_client, gwbuf_clone(pMessage));
} }