Reindent server/core/session.c

This commit is contained in:
Johan Wikman
2015-11-30 19:59:34 +02:00
parent 8601068dc2
commit 15df33a93f
2 changed files with 638 additions and 630 deletions

View File

@ -152,7 +152,6 @@ session_alloc(SERVICE *service, DCB *client_dcb)
pthread_self(), pthread_self(),
__func__, __func__,
service->name); service->name);
} }
/* /*
* Pending filter chain being setup set the head of the chain to * Pending filter chain being setup set the head of the chain to
@ -316,8 +315,7 @@ session_link_dcb(SESSION *session, DCB *dcb)
return true; return true;
} }
int session_unlink_dcb( int session_unlink_dcb(SESSION* session,
SESSION* session,
DCB* dcb) DCB* dcb)
{ {
int nlink; int nlink;
@ -386,7 +384,6 @@ session_simple_free(SESSION *session, DCB *dcb)
free(session); free(session);
} }
/** /**
* Deallocate the specified session * Deallocate the specified session
* *
@ -440,8 +437,7 @@ session_free(SESSION *session)
*/ */
if (!session->ses_is_child && session->router_session) if (!session->ses_is_child && session->router_session)
{ {
session->service->router->freeSession( session->service->router->freeSession(session->service->router_instance,
session->service->router_instance,
session->router_session); session->router_session);
} }
if (session->n_filters) if (session->n_filters)
@ -450,17 +446,19 @@ session_free(SESSION *session)
for (i = 0; i < session->n_filters; i++) for (i = 0; i < session->n_filters; i++)
{ {
if (session->filters[i].filter) if (session->filters[i].filter)
session->filters[i].filter->obj->closeSession( {
session->filters[i].instance, session->filters[i].filter->obj->closeSession(session->filters[i].instance,
session->filters[i].session); session->filters[i].session);
} }
}
for (i = 0; i < session->n_filters; i++) for (i = 0; i < session->n_filters; i++)
{ {
if (session->filters[i].filter) if (session->filters[i].filter)
session->filters[i].filter->obj->freeSession( {
session->filters[i].instance, session->filters[i].filter->obj->freeSession(session->filters[i].instance,
session->filters[i].session); session->filters[i].session);
} }
}
free(session->filters); free(session->filters);
} }
@ -494,8 +492,8 @@ session_free(SESSION *session)
int int
session_isvalid(SESSION *session) session_isvalid(SESSION *session)
{ {
SESSION *ptr; SESSION *ptr;
int rval = 0; int rval = 0;
spinlock_acquire(&session_spin); spinlock_acquire(&session_spin);
ptr = allSessions; ptr = allSessions;
@ -521,8 +519,8 @@ int rval = 0;
void void
printSession(SESSION *session) printSession(SESSION *session)
{ {
struct tm result; struct tm result;
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(session->state));
@ -541,7 +539,7 @@ char timebuf[40];
void void
printAllSessions() printAllSessions()
{ {
SESSION *ptr; SESSION *ptr;
spinlock_acquire(&session_spin); spinlock_acquire(&session_spin);
ptr = allSessions; ptr = allSessions;
@ -563,9 +561,9 @@ SESSION *ptr;
void void
CheckSessions() CheckSessions()
{ {
SESSION *ptr; SESSION *ptr;
int noclients = 0; int noclients = 0;
int norouter = 0; int norouter = 0;
spinlock_acquire(&session_spin); spinlock_acquire(&session_spin);
ptr = allSessions; ptr = allSessions;
@ -589,7 +587,9 @@ int norouter = 0;
} }
spinlock_release(&session_spin); spinlock_release(&session_spin);
if (noclients) if (noclients)
{
printf("%d Sessions have no clients\n", noclients); printf("%d Sessions have no clients\n", noclients);
}
spinlock_acquire(&session_spin); spinlock_acquire(&session_spin);
ptr = allSessions; ptr = allSessions;
while (ptr) while (ptr)
@ -612,7 +612,9 @@ int norouter = 0;
} }
spinlock_release(&session_spin); spinlock_release(&session_spin);
if (norouter) if (norouter)
{
printf("%d Sessions have no router session\n", norouter); printf("%d Sessions have no router session\n", norouter);
}
} }
/** /**
@ -626,15 +628,14 @@ int norouter = 0;
void void
dprintAllSessions(DCB *dcb) dprintAllSessions(DCB *dcb)
{ {
struct tm result; struct tm result;
char timebuf[40]; char timebuf[40];
SESSION *ptr; SESSION *ptr;
spinlock_acquire(&session_spin); spinlock_acquire(&session_spin);
ptr = allSessions; ptr = allSessions;
while (ptr) while (ptr)
{ {
dcb_printf(dcb, "Session %d (%p)\n",ptr->ses_id, ptr); dcb_printf(dcb, "Session %d (%p)\n",ptr->ses_id, ptr);
dcb_printf(dcb, "\tState: %s\n", session_state(ptr->state)); dcb_printf(dcb, "\tState: %s\n", session_state(ptr->state));
dcb_printf(dcb, "\tService: %s (%p)\n", ptr->service->name, ptr->service); dcb_printf(dcb, "\tService: %s (%p)\n", ptr->service->name, ptr->service);
@ -651,7 +652,7 @@ SESSION *ptr;
dcb_printf(dcb, "\tConnected: %s", dcb_printf(dcb, "\tConnected: %s",
asctime_r(localtime_r(&ptr->stats.connect, &result), timebuf)); asctime_r(localtime_r(&ptr->stats.connect, &result), timebuf));
if(ptr->client && ptr->client->state == DCB_STATE_POLLING) if (ptr->client && ptr->client->state == DCB_STATE_POLLING)
{ {
double idle = (hkheartbeat - ptr->client->last_read); double idle = (hkheartbeat - ptr->client->last_read);
idle = idle > 0 ? idle/10.0:0; idle = idle > 0 ? idle/10.0:0;
@ -675,10 +676,9 @@ SESSION *ptr;
void void
dprintSession(DCB *dcb, SESSION *ptr) dprintSession(DCB *dcb, SESSION *ptr)
{ {
struct tm result; struct tm result;
char buf[30]; char buf[30];
int i; int i;
dcb_printf(dcb, "Session %d (%p)\n",ptr->ses_id, ptr); dcb_printf(dcb, "Session %d (%p)\n",ptr->ses_id, ptr);
dcb_printf(dcb, "\tState: %s\n", session_state(ptr->state)); dcb_printf(dcb, "\tState: %s\n", session_state(ptr->state));
@ -687,14 +687,14 @@ int i;
if (ptr->client && ptr->client->remote) if (ptr->client && ptr->client->remote)
{ {
double idle = (hkheartbeat - ptr->client->last_read); double idle = (hkheartbeat - ptr->client->last_read);
idle = idle > 0 ? idle/10.f:0; idle = idle > 0 ? idle/10.f : 0;
dcb_printf(dcb, "\tClient Address: %s%s%s\n", dcb_printf(dcb, "\tClient Address: %s%s%s\n",
ptr->client->user?ptr->client->user:"", ptr->client->user?ptr->client->user:"",
ptr->client->user?"@":"", ptr->client->user?"@":"",
ptr->client->remote); ptr->client->remote);
dcb_printf(dcb, "\tConnected: %s\n", dcb_printf(dcb, "\tConnected: %s\n",
asctime_r(localtime_r(&ptr->stats.connect, &result), buf)); asctime_r(localtime_r(&ptr->stats.connect, &result), buf));
if(ptr->client->state == DCB_STATE_POLLING) if (ptr->client->state == DCB_STATE_POLLING)
{ {
dcb_printf(dcb, "\tIdle: %.0f seconds\n",idle); dcb_printf(dcb, "\tIdle: %.0f seconds\n",idle);
} }
@ -706,8 +706,7 @@ int i;
{ {
dcb_printf(dcb, "\tFilter: %s\n", dcb_printf(dcb, "\tFilter: %s\n",
ptr->filters[i].filter->name); ptr->filters[i].filter->name);
ptr->filters[i].filter->obj->diagnostics( ptr->filters[i].filter->obj->diagnostics(ptr->filters[i].instance,
ptr->filters[i].instance,
ptr->filters[i].session, ptr->filters[i].session,
dcb); dcb);
} }
@ -725,7 +724,7 @@ int i;
void void
dListSessions(DCB *dcb) dListSessions(DCB *dcb)
{ {
SESSION *ptr; SESSION *ptr;
spinlock_acquire(&session_spin); spinlock_acquire(&session_spin);
ptr = allSessions; ptr = allSessions;
@ -747,7 +746,10 @@ SESSION *ptr;
ptr = ptr->next; ptr = ptr->next;
} }
if (allSessions) if (allSessions)
dcb_printf(dcb, "-----------------+-----------------+----------------+--------------------------\n\n"); {
dcb_printf(dcb,
"-----------------+-----------------+----------------+--------------------------\n\n");
}
spinlock_release(&session_spin); spinlock_release(&session_spin);
} }
@ -781,20 +783,20 @@ session_state(int state)
return "Session to be freed"; return "Session to be freed";
case SESSION_STATE_FREE: case SESSION_STATE_FREE:
return "Freed session"; return "Freed session";
#endif #endif
default: default:
return "Invalid State"; return "Invalid State";
} }
} }
SESSION* get_session_by_router_ses( SESSION* get_session_by_router_ses(void* rses)
void* rses)
{ {
SESSION* ses = allSessions; SESSION* ses = allSessions;
while (ses->router_session != rses && ses->next != NULL) while (ses->router_session != rses && ses->next != NULL)
{
ses = ses->next; ses = ses->next;
}
if (ses->router_session != rses) if (ses->router_session != rses)
{ {
@ -819,10 +821,10 @@ SESSION* get_session_by_router_ses(
static int static int
session_setup_filters(SESSION *session) session_setup_filters(SESSION *session)
{ {
SERVICE *service = session->service; SERVICE *service = session->service;
DOWNSTREAM *head; DOWNSTREAM *head;
UPSTREAM *tail; UPSTREAM *tail;
int i; int i;
if ((session->filters = calloc(service->n_filters, if ((session->filters = calloc(service->n_filters,
sizeof(SESSION_FILTER))) == NULL) sizeof(SESSION_FILTER))) == NULL)
@ -893,7 +895,7 @@ int i;
int int
session_reply(void *instance, void *session, GWBUF *data) session_reply(void *instance, void *session, GWBUF *data)
{ {
SESSION *the_session = (SESSION *)session; SESSION *the_session = (SESSION *)session;
return the_session->client->func.write(the_session->client, data); return the_session->client->func.write(the_session->client, data);
} }
@ -907,13 +909,13 @@ char *
session_get_remote(SESSION *session) session_get_remote(SESSION *session)
{ {
if (session && session->client) if (session && session->client)
{
return session->client->remote; return session->client->remote;
}
return NULL; return NULL;
} }
bool session_route_query ( bool session_route_query(SESSION* ses, GWBUF* buf)
SESSION* ses,
GWBUF* buf)
{ {
bool succp; bool succp;
@ -975,9 +977,9 @@ void session_close_timeouts(void* data)
ses = get_all_sessions(); ses = get_all_sessions();
spinlock_release(&session_spin); spinlock_release(&session_spin);
while(ses) while (ses)
{ {
if(ses->client && ses->client->state == DCB_STATE_POLLING && if (ses->client && ses->client->state == DCB_STATE_POLLING &&
ses->service->conn_timeout > 0 && ses->service->conn_timeout > 0 &&
hkheartbeat - ses->client->last_read > ses->service->conn_timeout * 10) hkheartbeat - ses->client->last_read > ses->service->conn_timeout * 10)
{ {
@ -987,14 +989,14 @@ void session_close_timeouts(void* data)
spinlock_acquire(&session_spin); spinlock_acquire(&session_spin);
ses = ses->next; ses = ses->next;
spinlock_release(&session_spin); spinlock_release(&session_spin);
} }
} }
/** /**
* Callback structure for the session list extraction * Callback structure for the session list extraction
*/ */
typedef struct { typedef struct
{
int index; int index;
SESSIONLISTFILTER filter; SESSIONLISTFILTER filter;
} SESSIONFILTER; } SESSIONFILTER;
@ -1009,11 +1011,11 @@ typedef struct {
static RESULT_ROW * static RESULT_ROW *
sessionRowCallback(RESULTSET *set, void *data) sessionRowCallback(RESULTSET *set, void *data)
{ {
SESSIONFILTER *cbdata = (SESSIONFILTER *)data; SESSIONFILTER *cbdata = (SESSIONFILTER *)data;
int i = 0; int i = 0;
char buf[20]; char buf[20];
RESULT_ROW *row; RESULT_ROW *row;
SESSION *ptr; SESSION *ptr;
spinlock_acquire(&session_spin); spinlock_acquire(&session_spin);
ptr = allSessions; ptr = allSessions;
@ -1070,11 +1072,13 @@ SESSION *ptr;
RESULTSET * RESULTSET *
sessionGetList(SESSIONLISTFILTER filter) sessionGetList(SESSIONLISTFILTER filter)
{ {
RESULTSET *set; RESULTSET *set;
SESSIONFILTER *data; SESSIONFILTER *data;
if ((data = (SESSIONFILTER *)malloc(sizeof(SESSIONFILTER))) == NULL) if ((data = (SESSIONFILTER *)malloc(sizeof(SESSIONFILTER))) == NULL)
{
return NULL; return NULL;
}
data->index = 0; data->index = 0;
data->filter = filter; data->filter = filter;
if ((set = resultset_create(sessionRowCallback, data)) == NULL) if ((set = resultset_create(sessionRowCallback, data)) == NULL)

View File

@ -52,11 +52,13 @@ struct filter_def;
/** /**
* The session statistics structure * The session statistics structure
*/ */
typedef struct { typedef struct
{
time_t connect; /**< Time when the session was started */ time_t connect; /**< Time when the session was started */
} SESSION_STATS; } SESSION_STATS;
typedef enum { typedef enum
{
SESSION_STATE_ALLOC, /*< for all sessions */ SESSION_STATE_ALLOC, /*< for all sessions */
SESSION_STATE_READY, /*< for router session */ SESSION_STATE_READY, /*< for router session */
SESSION_STATE_ROUTER_READY, /*< for router session */ SESSION_STATE_ROUTER_READY, /*< for router session */
@ -72,22 +74,22 @@ typedef enum {
* The downstream element in the filter chain. This may refer to * The downstream element in the filter chain. This may refer to
* another filter or to a router. * another filter or to a router.
*/ */
typedef struct { typedef struct
{
void *instance; void *instance;
void *session; void *session;
int (*routeQuery)(void *instance, void *session, int (*routeQuery)(void *instance, void *session, GWBUF *request);
GWBUF *request);
} DOWNSTREAM; } DOWNSTREAM;
/** /**
* The upstream element in the filter chain. This may refer to * The upstream element in the filter chain. This may refer to
* another filter or to the protocol implementation. * another filter or to the protocol implementation.
*/ */
typedef struct { typedef struct
{
void *instance; void *instance;
void *session; void *session;
int (*clientReply)(void *instance, int (*clientReply)(void *instance, void *session, GWBUF *response);
void *session, GWBUF *response);
int (*error)(void *instance, void *session, void *); int (*error)(void *instance, void *session, void *);
} UPSTREAM; } UPSTREAM;
@ -95,9 +97,9 @@ typedef struct {
* Structure used to track the filter instances and sessions of the filters * Structure used to track the filter instances and sessions of the filters
* that are in use within a session. * that are in use within a session.
*/ */
typedef struct { typedef struct
struct filter_def {
*filter; struct filter_def *filter;
void *instance; void *instance;
void *session; void *session;
} SESSION_FILTER; } SESSION_FILTER;
@ -105,7 +107,8 @@ typedef struct {
/** /**
* Filter type for the sessionGetList call * Filter type for the sessionGetList call
*/ */
typedef enum { typedef enum
{
SESSION_LIST_ALL, SESSION_LIST_ALL,
SESSION_LIST_CONNECTION SESSION_LIST_CONNECTION
} SESSIONLISTFILTER; } SESSIONLISTFILTER;
@ -117,7 +120,8 @@ typedef enum {
* to the database, it links the descriptors, routing implementation * to the database, it links the descriptors, routing implementation
* and originating service together for the client session. * and originating service together for the client session.
*/ */
typedef struct session { typedef struct session
{
#if defined(SS_DEBUG) #if defined(SS_DEBUG)
skygw_chk_t ses_chk_top; skygw_chk_t ses_chk_top;
#endif #endif