1184 Commits

Author SHA1 Message Date
vraatikka
3769a02957 query_classifier.cc
resolve_query_type, added GSYSVAR_FUNC type for functions that read system variables and can be executed in Maxscale instead of backend server.

dcb.c
	dcb_read, if read returns value <= 0 and if error is EAGAIN/EWOULDBLOCK so there was nothing to read in sthe socket, that is not an error because some other thread may have read the data that was expected to be available.

mysql_backend.c
	gw_read_backend_event, used dcb->authlock to ensure that dcb's protocol state is read and modified serially. This removes issues with false authentication failures which may happen when two threads modify and read the state without any control.

mysql_client.c 
	removed dead code.

readconnroute.c, readwritesplit.c
	removed invalid assert which assumed that spinlock can not have larger value than one when itis locked.
2013-10-31 22:24:51 +02:00
vraatikka
e803acb036 dcb.c, gateway.c little tuning.
poll.c
	Removed mutex from epoll_wait.
	Removed read and write mutexes from poll_waitevents.

session.c
	If session_alloc fails, instead of calling directly free(session), call session_free, which decreases refcounter and only frees session when there are no references left. 
	Added session_unlink_dcb function which removes link from session and optionally sets the dcb->session pointer to NULL.

readconnection.h, readwritesplit.h
	Added check fields to ROUTER_CLIENT_SES strct as well as lock, version number (not used yet) and closed flag.

mysql_backend.c
	gw_read_backend_event: if backend_protocol->state was set to MYSQL_AUTH_RECV, function returned, which was unnecessary. If mysql state became MYSQL_AUTH_FAILED, router client session was closed. Removed unnecessary NULL checks because rsession is not allowed to be NULL. Similarly, removed other NULL checks and replaced them with asserts checking that router client session is not NULL at any phase.

mysql_client.c
	Removed unused code blocks. Polished log commands. Replaced router client sessions NULL checks with asserts.

mysql_common.c
	mysql_send_custom_error: if called with dcb == NULL, return.

readconnroute.c
	Replaced malloc with calloc. Added functions rses_begin_router_action and rses_exit_router_action. If router client session is not closed, they take a lock and release it, respectively. Those functions are used for protecting all operations which modify the contents of router client session struct.

readwritesplit.c
	Identical changes than in readconnroute.c

skygw_debug.h
	Added check number and - macro for ROUTER_CLIENT_SES, and added COM_QUIT to STRPACKETTYPE.
2013-10-30 22:07:51 +02:00
Massimiliano Pinto
2cbe46b506 gwbuf_consume is protected by spinlock 2013-10-24 17:53:38 +02:00
Massimiliano Pinto
4a189696bd In mysql_backend.c:gw_read_backend_event
the delay queue is now consumed when backend_protocol->state == MYSQL_AUTH_FAILED.

This will avoid sending more times the custom error in the threaded configuration.
2013-10-21 16:28:07 +02:00
Massimiliano Pinto
b5da527555 myql_common.c:gw_send_change_user_to_backend checks func.write for 0 and 1
mysql_client.c:gw_MySQLWrite_client is simply return dcb_write(dcb, queue);
The return codes are not the same of the code replaced, but they are not checked in the caller routines.
2013-10-14 18:41:49 +02:00
vraatikka
66dbb8ec23 dcb.c
dcb_write, if write failed, gwbuf wasn't freed.
mysql_backend.c 
	gw_MySQLWrite_backend, if dcb wasn'r in POLLING state then write was skpiped but gwbuf wasn't freed.
mysql_client.c
	gw_MySQLWrite_client, disabled the use of dcb_write because it returns different values than the code it replaced. Also removed erroneous call of gwbuf_consume.
mysql_common.c
	mysql_send_custom_error, removed erroneous call of gwbuf_free.
2013-10-14 15:12:30 +03:00
vraatikka
0f2eeff243 gw_buffer was not freed. 2013-10-14 13:16:33 +03:00
Massimiliano Pinto
e1c7887726 gw_MySQLWrite_client is now simply a call to dcb_write 2013-10-07 16:34:15 +02:00
vraatikka
655a6537b2 mysql_client_server_protocol.h :
changed gw_receive_backend_auth declaration to return int instead of boolean.
mysql_backend.c:
	gw_read_backend_event calls gw_receive_backend_auth which either fails (== -1), succeeds with nothing to read (== 0) or succeeds (== 1). For each case there is handling. If dcb_read succeeds without read bytes, return asap.
mysql_client.c:
	gw_error_client_event, gw_client_close, gw_client_hangup_event : all close client dcb but now they also close backend dcb.
mysql_common.c:
	gw_receive_backend_auth, return -1, 0, or 1 if read from backend failed, was empty, or succeed, respectively.:
2013-10-07 14:00:44 +03:00
vraatikka
80b67d1083 log_manager.cc:
State update for filewriter was missing and that caused Maxscale to fail if opening of any log file failed.
dcb.c:
	Added EAGAIN and EWOULDBLOCK handling to dcb_read. If dcb_close is called for freshly created dcb, dcb is only freed.
gateway.c:
	Added file_write_footer and write_footer of which the latter is called at exit time. It simply draws a line to screen.
gw_utils.c:
	Some macros for helping comparison between gw_read_gwbuff and dcb_read, which overlap.
poll.c:
	Some macros to help enable/disable mutexing in poll_waitevents
service.c:
	Check return value of listen and session_alloc and behave accordingly.

mysql_client.c:
	If ioctl returned successfully with b==0 it earlier caused closing the client and backend dcbs. Since that doesn't reliably indicate that client has closed socket on its side, Maxscale doesn't close its sockets either.
mysql_common.c:
	In gw_receive_backend_auth, if dcb_read returns n==0, it is not considered as an error anymore. The implemented behavior is not yet complete and correct. Result should be successful but the protocol state shouldn't change to MYSQL_IDLE before backend return is received.
	In gw_send_authentication_to_backend protocol state was always set to MYSQL_AUTH_RECV even if gw_rwite had failed. Now, return value is read and state is set in caller's context basen on the return value.
skygw_utils.cc:
	Removed ss_dassert from skyge_file_init because it prevented from returning meaningful error meassage to the client.:
2013-10-06 22:31:32 +03:00
vraatikka
e3a4be8b9d gw_write_backend_event, if dcb is not in POLLING state but writeq is NULL it is ok. Only attempt to write when dcb's socket isn't open anymore is failure. 2013-10-04 16:44:21 +03:00
vraatikka
849a366e95 log_manager.cc :
tuned error printing and log writing output format
dcb.c : 
	dcb_connect, check return value of poll_add_dcb and behave accordingly.
	dcb_write, in case of SIFPIPE, only write to trace log.
	dcb_close, dassert with incorrect dcb states.
gateway.c :
	added file_write_header to print header similar than in logs to stderr.
	main, add signal handler for SIGPIPE
poll.c : 
	poll_remove_dcb, don't fail if dcb is in NOPOLLING or in ZOMBIE states.
	poll_waitevents, write EPOLLHUPs to trace log, don't even attempt to write to closed socket.
readconnection.h : 
	shortened comment.
readwritesplit.h : 
	replaced generic names with more specific ones. 
httpd.c : 
	Check listen return value and behave accordingly.
mysql_backend.c : 
	 Tiny clean up.
mysql_client.c : 
	gw_MySQLListener, Check listen return value and behave accordingly. 
mysql_common.c : 
	Shortened a header.
telnetd.c : 
	telnetd_listen, check listen return value and behave accordingly.
readconnroute.c : 
	Tuned log writing format.
readwritesplit.c : 
	Added function search_backend_servers, which chooses suitable backend and master server among those known by Maxscale. Fixed clean-up routines. Not ready yet but works somehow.
testroute.c : 
	Cleanup.
skygw_utils.cc : 
	Log writing clean up.
2013-10-04 12:06:44 +03:00
Massimiliano Pinto
9f2f0ac006 session = dcb->session; was missing in the previous check in mysql_client.c:gw_read_client_event() 2013-10-02 15:42:55 +02:00
Massimiliano Pinto
37bd63b33b Detected closed socket in mysql_client.c:gw_read_client_event now calls the closeSession.
This will fix left opened backend connections if the client connection aborts
2013-10-02 11:15:04 +02:00
Massimiliano Pinto
c55ca0aaee Backed dcb close if now forced after routing COM_QUERY and close client dcb, in mysql_client.c gw_read_client_event
/** close backends connection */
router->closeSession(router_instance, rsession);

Tested only with readconroute router module
2013-10-02 09:27:10 +02:00
Massimiliano Pinto
c48dd6028f Modified dcb_read return: n = 0 means 0 bytes read without errors
if (rc < 0) is now used testing dcb_read() return, instead of if (rc <= 0)
2013-09-27 19:24:23 +02:00
vraatikka
e03d2b3ffe In gw_read_client_event, if ioctl returns non-zero and 'b' (number of readable bytes) is zero, that indicates that socket is closed. Thus, read is now skipped and dcb closed if it is in DSC_STATE_POLLING state. 2013-09-27 14:57:53 +03:00
Massimiliano Pinto
46c5564313 Removed states MYSQL_WAITING_RESULT and MYSQL_ROUTING 2013-09-26 18:01:48 +02:00
Mark Riddoch
ee7b799bf1 Bug 160 - fixed memory leaks with telnetd and debug CLI 2013-09-26 17:38:35 +02:00
vraatikka
20c4a60f01 Maxscale doesn't assert if tested with different errors in gw_MySQLAccept. 2013-09-24 16:09:11 +03:00
vraatikka
09cc20b7be Improved behavior of debug cli API command 'fail accept'. It takes now _two_ arguments, Syntax:
fail accept <errno> <repeat count>

Changed fail_next_accept from boolean to decreasing counter.
2013-09-24 15:48:59 +03:00
vraatikka
a7c3cd5f30 Related to bug #217, added command 'fail accept <errno>' to debug client's commands.
Reproduce #217 by connecting with telnet to debug client interface and execute command:

fail accept 23

and try to execute read query, for example, with

mysql -h 127.0.0.1 -P 4008 -u maxuser -pmaxpwd -e 'select current_user(), @@server_id'
2013-09-24 15:04:12 +03:00
vraatikka
3b3d3dbfe0 Merge 2013-09-20 15:05:19 +03:00
vraatikka
302090a168 Removed unused local variable. 2013-09-20 15:03:20 +03:00
vraatikka
74aa3638f9 dcb.c
dcb_final_free: Router session is not closed anymore when dcb is closed. Router session is shared among all dcbs and is closed and freed with session.
	dcb_connect: dcb's state must be switched fro DCB_STATE_ALLOC to DCB_STATE_DISCONNECTED before dcb_final_free can be called for it.
	dcb_close: poll_remove_dcb encapsulates dcb's state transition in the same way as poll_add_dcb. Removed state modification from dcb_close. Read return value of poll_remove_dcb and log accordingly.
	dcb_set state: remove dassert if dcb_set_state_nomutex returned false. False can be returned indicating that intented change didn't occur but the end state, for example, may be acceptable. Failures in state transitions are asserted in dcb_Set_state_nomutex.

poll.c
	poll_add_dcb: dcb state is now set here to either DCB_STATE_LISTENING or to DCB_STATE_POLLING according to dcb's role. Failures in state setting and in epoll_ctl are detected, logged and handled.
	poll_remove_dcb: Failures in state setting and epoll_ctl are detected, logged, and handled.

mysql_client_server_protocol.h 
	Removed macros MYSQL_FAILED_AUTHENTICATION & MYSQL_SUCCESFUL_AUTHENTICATION as they were not necessary and used with constant values 0 and 1 depending on the case.
	Renamed variable 'conn' to 'protocol' in cases where it meant protocol.

mysql_backend.c
	gw_read_backend_event: In case when there was nothing to read or read failed, backend dcb is closed because situation is assumed to be such that backend server closed its side of the socket. Removed macros MYSQL_FAILED/SUCCESFUL_AUTHENTICATION
	gw_create_backend_connection: Assigned protocol with fd which is connected to backend.
	backend_write_delayqueue: In case where dcb_write fails to write anything, close backend dcb to avoid it getting hanging.

mysql_client.c
	gw_read_client_event: Read return value of routeQuery and if it isn't == 1, call mysql_send_custom_error with client dcb and set client's protocol state to MYSQL_IDLE instead of MYSQL_ROUTING.
	gw_MySQLAccept: Static reply counter was erroneously used as a criteria for jumping to return point where return value was constantly 'success' (=0). Replaced static reply counter with private. Fixed return value in different cases.

mysql_common.c
	gw_receive_backend_auth: Changed to return boolean values indicating of success or failue. Used integers which were assigned to macroed values on the caller side. Added length check before accessing buffer. 

readconnroute.c
	Cut too long lines and removed statements with side effects.

skygw_debug.h
	Added macro STRPROTOCOLSTATE(s) to produce string representation of a given protocol state.
2013-09-20 14:32:28 +03:00
Massimiliano Pinto
85ac4d78dd HTTPD is now working:
changed dcb role to DCB_ROLE_REQUEST_HANDLER

removed session_alloc and put client->session = NULL instead
2013-09-19 12:23:17 +02:00
Massimiliano Pinto
ace4252832 Added closeSession to gw_error_backend_event: the backend failure is handled without faults
Modified gw_send_change_user_to_backend ret code
2013-09-18 11:50:19 +02:00
vraatikka
8bf73ea154 Replaced write system function with wrapper gw_write. It allows for generating failures by using telnet commands, fail backendfd, fail clientfd, which are available in debug build only. 2013-09-17 00:07:56 +03:00
vraatikka
db7004e6ae dcb's fd wasn't stored to client's protocol structure. Changed mysql_protocol_init to take fd as the Second argument. 2013-09-14 23:01:26 +03:00
vraatikka
4815856017 session.c : session_alloc
If backend connection can't be created, backend_dcb is not create, router client session is not created and what already is created in session_alloc, is freed.
mysql_client.c : gw_read_client_event
If session creation failed, then - instead of sending ok to client, "failed to create new session" is sent and client dcb is closed.
 : gw_MySQLAccept
removed loop where accept was called again and again. With single thread looping forever is not possible because there's no one to free previously allocated resources.
If accept fails ten times in a row, then return without new client dcb.
2013-09-13 23:55:26 +03:00
vraatikka
710fc5cfa6 dcb.c
: dcb_alloc switched malloc to calloc, dcb->fd is initialized to -1. 
: dcb_process_zombies return value of close(fd) is checked. Closed fds are stored to conn_open array. 
: dcb_connect assigns new fd only if backend connection succeeds. Dcb is added to poll set in the same way, only if connect succeed.
gateway.c : conn_open array is initialized in main. For each created socket, a true is set to corresponding slot. Max. number of slots is 1024.
mysql_client_server_protocol.h : gw_do_connect_to declaration 
mysql_backend.c : gw_create_backend_connection returns rv >= 0 and a protocol which is assigned to backend_dcb. In error, -1 is returned and fd is not set.
mysql_client.c : conn_open array is kept up-to-date and protocol pointer is assigned also to dcb outside mysql_protocol_init.
mysql_common.c : gw_do_connect_to_backend 3rd argument is pointer to fd, not protocol. dcb is added to poll set later in dcb_connect.
skygw_debug.h : define conn_open[1024] array where open connections can be marked in debug build.
2013-09-13 22:10:40 +03:00
Massimiliano Pinto
b2b715fcae Fixed failed handshake handling, taking care of dcb->delayq before sending reply one time and not twice. 2013-09-13 18:22:24 +02:00
vraatikka
88174865a0 If accept fails due too many open fails, now ENFILE (system level file limit exceeded) and EMFILE (process limit) are separated and logged diffenretly. 2013-09-13 10:10:02 +03:00
vraatikka
d7e793a411 poll.c:Returned checks for zombie
mysql_backend.c:don't write to session->client if session->client is NULL
mysql_common.c:assert if called with dcb==NULL
2013-09-12 22:34:49 +03:00
vraatikka
bbc9dcc9a3 poll.c: added maxscale thread id to log
session.c: Replaced free(session->router_sesision) with call to freeSession callback
users.c: Removed reference to uninitialized variable.
router.h: Added freeSession callback to function block.
mysql_backend.c: try to ensure that client dcb is still listening in epoll_wait when writing reply to it.
mysql_common.c: assert debug build is mysql_protocol_init is called with dcb == NULL
readconnroute.c, readwritesplit.c, debugcli.c and testroute.c : Added freeSession to function block and an inmplementation of it.
2013-09-12 22:17:11 +03:00
Massimiliano Pinto
775b318889 Added constants to gw_decode_mysql_server_handshake
Check status of gw_read_backend_handshake and gw_send_authentication_to_backend in gw_read_backend_event
2013-09-12 16:08:31 +02:00
vraatikka
6813f760a5 Fix for bug #205 - http://bugs.skysql.com/show_bug.cgi?id=205 . In gw_read_backend_event, read client_protocol by using dcb->session->client pointer but only after it is sure that there is something to write to client. This doesn't ensure that client pointer in session is valid, but it should be.
Return value of dcb_read is checked and buffer pointer is not used if nothing was read.
2013-09-09 15:00:37 +03:00
vraatikka
e1d7a5640d Put structure check fields behind SS_DEBUG macros. Thus, they are only included in debug builds. 2013-09-09 10:05:46 +03:00
vraatikka
d2a61c3f82 Added some checks and renamed according to changes made in elsewhere. 2013-09-08 23:38:38 +03:00
vraatikka
e7e44667c3 Added some checks for return values. 2013-09-08 23:37:34 +03:00
vraatikka
66e9be814b dcb.h
-------
Removed DCB states DCB_STATE_IDLE, and DCB_STATE_PROCESSING.
Added DCB_STATE_UNDEFINED for initial content for state variable which doesn't have any specific value set, and DCB_STATE_NOPOLLING to indicate that dcb has been removed from poll set.

Added following dcb roles: DCB_ROLE_SERVICE_LISTENER for listeners of services, and DCB_ROLE_REQUEST_HANDLER for client/backend dcbs. Listeners may have state DCB_STATE_LISTENING, but not DCB_STATE_POLLING. Request handlers may have DCB_STATE_POLLING but not DCB_STATE_LISTENING. Role is passed as an argument to dcb.c:dcb_alloc.

From now on, struct check numbers of DCB are included and checked in DEBUG build only.

Added dcb_role_t dcb_role-member to DCB as well as SPINLOCK dcb_initlock, which protects state changes.

Removed extern keyword from function declarations because functions are by default externally visible if they are declared in header.

dcb.b
------
Function dcb_set_state, and dcb_set_state_nomutex provide functions for changing dcb states. Latter implements a state machine for dcb.
Function dcb_add_to_zombieslist replaces dcb_free. It adds in atomic step dcb to zombieslist and changes state to DCB_STATE_ZOMBIE.
Function dcb_final_free removes dcb from allDCBs list, terminates router and client sessions, and frees dcb and related memory.
Function dcb_process_zombies removes executing thread from dcb's bitmask, and it there are no further thread bits, moves dcb to a victim list, and finally, for each dcb on victim list, closes fd and sets state to DCB_STATE_DISCONNECTED.
Function dcb_close sets dcb state to DCB_STATE_NOPOLLIN, removes dcb from poll set and sets bit to bitmask for each server thread in an atomic step.  

poll.c
------
Function poll_add_dcb sets either DCB_STATE_LISTENING or DCB_STATE_POLLING state for newly created dcb, depending whether the role of dcb is DCB_ROLE_SERVICE_LISTENER, or DCB_ROLE_REQUEST_HANDLER, respectively. Then dcb is set to poll set.

poll_waitevents : commented out code which skipped event if dcb was added to zombieslist or if fd was closed. Added state checks.

service.c : Minor changes.
httpd.c : Removed dcb state changes. They are done in core.
mysql_backend.c : Added checks, removed dcb state changes.
mysql_client.c : Removed dcb state changes. Added checks.
mysql_common.c : Minor changes
telnetd.c : Removed state changes. Replaced some typecasts and pointer references with local variable reads.
skygw_debug.h : Removed two states, and added two to state printing macro.
2013-09-05 22:00:02 +03:00
Massimiliano Pinto
b01cf2365e Added log error or ss_dassert instead of silent returning 2013-09-04 15:31:04 +02:00
Mark Riddoch
f74f67540f Added spinlock protection for the session association to the DCB and
reworked the reference count mechanism on the session.  Introduced a
FREE state for the session and alter the session destruction flow so
that we only remove the session when all the DCB's have singled they
have finished processing events for the DCB rather than when the first
thread decides to clsoe the DCB.
2013-09-04 12:24:59 +02:00
Massimiliano Pinto
be27d36f8c Added dcb NULL check in mysql_send_custom_error() 2013-09-04 11:26:14 +02:00
Massimiliano Pinto
61df7eb07d Added checks against NULL in gw_read_backend_event for dcb->session and dcb->session->client 2013-09-04 10:57:08 +02:00
Massimiliano Pinto
8e97974216 Fixed return rc after:
routeQuery()
(dcb->func).close(dcb);

This will prevent setting again dcb->sate = DCB_STATE_POLLING in return_rc label.

The log entry for 

[gw_read_client_event] ioctl FIONREAD for fd 21 failed. errno 9, Bad file descriptor. dcb->state = 3

now is not present anymore.
2013-09-03 14:42:47 +02:00
Massimiliano Pinto
31cda5ad65 Added session refcount in session.h
The refcount is incremented in dcb_connect and in mysql_client.c after session_alloc
2013-09-02 10:26:34 +02:00
vraatikka
757a043386 Replaced oinlined protocol creation with call to mysql_protocol_init. Clean up. 2013-09-01 00:36:31 +03:00
vraatikka
9df2040a8a Removed unused variables, changed call to mysql_protocol_init to reflect new prototype. 2013-09-01 00:32:57 +03:00
vraatikka
88ffcaa3d4 Removed 'extern' keyword from function declaration in headers. Functions are 'extern' by default.
Changed mysql_protocol_init prototype and implementation to return MySQLProtocol pointer instead of boolean.
2013-09-01 00:30:46 +03:00