1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-01 03:47:19 +03:00
Files
mariadb/mysql-test/suite/perfschema/t/socket_connect.test
Sergei Golubchik bead24b7f3 mariadb-test: wait on disconnect
Remove one of the major sources of race condiitons in mariadb-test.
Normally, mariadb_close() sends COM_QUIT to the server and immediately
disconnects. In mariadb-test it means the test can switch to another
connection and sends queries to the server before the server even
started parsing the COM_QUIT packet and these queries can see the
connection as fully active, as it didn't reach dispatch_command yet.

This is a major source of instability in tests and many - but not all,
still less than a half - tests employ workarounds. The correct one
is a pair count_sessions.inc/wait_until_count_sessions.inc.
Also very popular was wait_until_disconnected.inc, which was completely
useless, because it verifies that the connection is closed, and after
disconnect it always is, it didn't verify whether the server processed
COM_QUIT. Sadly the placebo was as widely used as the real thing.

Let's fix this by making mariadb-test `disconnect` command _to wait_ for
the server to confirm. This makes almost all workarounds redundant.

In some cases count_sessions.inc/wait_until_count_sessions.inc is still
needed, though, as only `disconnect` command is changed:

 * after external tools, like `exec $MYSQL`
 * after failed `connect` command
 * replication, after `STOP SLAVE`
 * Federated/CONNECT/SPIDER/etc after `DROP TABLE`

and also in some XA tests, because an XA transaction is dissociated from
the THD very late, after the server has closed the client connection.

Collateral cleanups: fix comments, remove some redundant statements:
 * DROP IF EXISTS if nothing is known to exist
 * DROP table/view before DROP DATABASE
 * REVOKE privileges before DROP USER
 etc
2025-07-16 09:14:33 +07:00

292 lines
9.0 KiB
Plaintext

#
# Check the handling of TCP/IP connections in the performance_schema socket
# instance tables
#
#==============================================================================
--source include/not_embedded.inc
--source include/have_perfschema.inc
--source ../include/wait_for_pfs_thread_count.inc
# Set this to enable debugging output
let $my_socket_debug_dbug=0;
--echo #==============================================================================
--echo # Establish the level of IPV6 support
--echo #==============================================================================
--source ../include/socket_ipv6.inc
--disable_cursor_protocol
--echo #==============================================================================
--echo # Get hostname, port number
--echo #==============================================================================
SELECT @@hostname INTO @MY_HOSTNAME;
SELECT @@port INTO @MY_MASTER_PORT;
if ($my_socket_debug)
{
SELECT @MY_HOSTNAME AS 'Hostname';
SELECT @MY_MASTER_PORT AS 'Master Port';
}
#==============================================================================
# Utility queries
#==============================================================================
let $count_client_connections=
SELECT COUNT(*) INTO @my_client_connections
FROM performance_schema.socket_instances
WHERE EVENT_NAME LIKE "%client_connection%";
let $get_thread_id=
SELECT THREAD_ID INTO @my_thread_id
FROM performance_schema.threads
WHERE PROCESSLIST_ID = CONNECTION_ID();
let $get_object_instance_begin=
SELECT OBJECT_INSTANCE_BEGIN INTO @my_object_instance_begin
FROM performance_schema.socket_instances
WHERE THREAD_ID = @my_thread_id;
let $get_port=
SELECT PORT INTO @my_port
FROM performance_schema.socket_instances
WHERE THREAD_ID = @my_thread_id;
let $ip_localhost=
(IP LIKE '%127.0.0.1' OR IP LIKE '%::1');
--echo #==============================================================================
--echo # 1.0 Get the default connection object_instance_begin, thread id and verify
--echo # the expected number of client connections.
--echo #==============================================================================
--echo #
--echo # 1.1 Confirm only one client connection
--echo #
eval $count_client_connections;
if (`SELECT @my_client_connections != 1`)
{
--echo # There should be one client connection
SELECT * FROM performance_schema.socket_instances;
--echo # abort
exit;
}
--echo #
--echo # 1.2 Get the default THREAD_ID;
--echo #
eval $get_thread_id;
let $default_thread_id= `SELECT @my_thread_id`;
--echo #
--echo # 1.3 Get the default OBJECT_INSTANCE_BEGIN
--echo #
eval $get_object_instance_begin;
let $default_object_instance_begin= `SELECT @my_object_instance_begin`;
--disable_query_log
SELECT @my_object_instance_begin INTO @default_object_instance_begin;
--enable_query_log
if ($my_socket_debug)
{
--echo # Default object instance begin = $default_object_instance_begin
--echo # Default thread id = $default_thread_id
}
--echo #==============================================================================
--echo # 2.0 ESTABLISH TCP/IP CONNECTION 1
--echo # Connect with IP = localhost (127.0.0.1 or ::1)
--echo #==============================================================================
--disable_query_log
--connect (con1,$my_localhost,root,,test,,$MASTER_MYPORT)
--enable_query_log
--echo #
--echo # 2.1 Get the connection thread id
--echo #
eval $get_thread_id;
--echo #
--echo # 2.2 Get the connection object instance begin
--echo #
eval $get_object_instance_begin;
--echo #
--echo # 2.3 Get the connection port
--echo #
eval $get_port;
--disable_query_log
SELECT @my_thread_id INTO @con1_thread_id;
SELECT @my_port INTO @con1_port;
SELECT @my_object_instance_begin INTO @con1_object_id;
let $con1_thread_id= `SELECT @con1_thread_id`;
let $con1_port= `SELECT @con1_port`;
let $con1_object_id= `SELECT @con1_object_id`;
let $con1_name1= "127.0.0.1:$con1_port";
let $con1_name2= "::1:$con1_port";
--enable_query_log
if ($my_socket_debug)
{
--echo # Con1 thread id = $con1_thread_id
--echo # Con1 object_id = $con1_object_id
--echo # Con1 port = $con1_port
--echo # Con1 name1 = $con1_name1
--echo # Con1 name2 = $con1_name2
}
--echo #
--echo # 2.4 Verify that the connection is 127.0.0.1 or ::1
--echo #
eval SELECT COUNT(*) = 1 AS 'Expect 1'
FROM performance_schema.socket_instances
WHERE EVENT_NAME LIKE '%client_connection%'
AND $ip_localhost
AND PORT= @con1_port
AND OBJECT_INSTANCE_BEGIN= @con1_object_id;
--echo #
--echo # 2.5 Verify that the same connection is in the summary instance table
--echo #
eval SELECT COUNT(*) = 1 AS 'Expect 1'
FROM performance_schema.socket_summary_by_instance
WHERE EVENT_NAME LIKE '%client_connection%'
AND OBJECT_INSTANCE_BEGIN= @con1_object_id;
--echo #
--echo # Switch to connection default
--echo #
--connection default
--echo #==============================================================================
--echo # 3.0 ESTABLISH TCP/IP CONNECTION 2
--echo # Connect with IP = localhost (127.0.0.1 or ::1)
--echo #==============================================================================
--disable_query_log
--connect (con2,$my_localhost,root,,test,,$MASTER_MYPORT)
--enable_query_log
--echo #
--echo # 3.1 Get the connection thread id
--echo #
eval $get_thread_id;
--echo #
--echo # 3.2 Get the connection object instance begin
--echo #
eval $get_object_instance_begin;
--echo #
--echo # 3.3 Get the connection port
--echo #
eval $get_port;
--disable_query_log
SELECT @my_thread_id INTO @con2_thread_id;
SELECT @my_port INTO @con2_port;
SELECT @my_object_instance_begin INTO @con2_object_id;
let $con2_thread_id= `SELECT @con2_thread_id`;
let $con2_port= `SELECT @con2_port`;
let $con2_object_id= `SELECT @con2_object_id`;
let $con2_name1= "127.0.0.1:$con2_port";
let $con2_name2= "::1:$con2_port";
--enable_query_log
if ($my_socket_debug)
{
--echo # con2 thread id = $con2_thread_id
--echo # con2 object_id = $con2_object_id
--echo # con2 port = $con2_port
--echo # con2 name1 = $con2_name1
--echo # con2 name2 = $con2_name2
}
--echo #
--echo # 3.4 Verify that the connection is 127.0.0.1 or ::1
--echo #
eval SELECT COUNT(*) = 1 AS 'Expect 1'
FROM performance_schema.socket_instances
WHERE EVENT_NAME LIKE '%client_connection%'
AND $ip_localhost
AND PORT= @con2_port
AND OBJECT_INSTANCE_BEGIN= @con2_object_id;
--echo #
--echo # 3.5 Verify that the same connection is in the summary instance table
--echo #
eval SELECT COUNT(*) = 1 AS 'Expect 1'
FROM performance_schema.socket_summary_by_instance
WHERE EVENT_NAME LIKE '%client_connection%'
AND OBJECT_INSTANCE_BEGIN= @con2_object_id;
--echo #
--echo # 3.6 Verify that the connection is 127.0.0.1 or ::1
--echo #
eval SELECT COUNT(*) = 1 AS 'Expect 1'
FROM performance_schema.socket_instances
WHERE EVENT_NAME LIKE '%client_connection%'
AND $ip_localhost
AND PORT= @con2_port
AND OBJECT_INSTANCE_BEGIN= @con2_object_id;
--echo #==============================================================================
--echo # 4.0 Verify both connections exist in the instance tables
--echo #==============================================================================
--connection default
--echo #
--echo # 4.1 Verify that there are two TCP/IP connections in the socket instance table
--echo #
eval SELECT COUNT(*) = 2 AS 'Expect 2'
FROM performance_schema.socket_instances
WHERE EVENT_NAME LIKE '%client_connection%'
AND OBJECT_INSTANCE_BEGIN <> @default_object_instance_begin
AND $ip_localhost;
--echo #
--echo # 4.2 Verify that there are two TCP/IP connections in the summary instance table
--echo #
eval SELECT COUNT(*) = 2 AS 'Expect 2'
FROM performance_schema.socket_summary_by_instance
WHERE EVENT_NAME LIKE '%client_connection%'
AND OBJECT_INSTANCE_BEGIN <> @default_object_instance_begin;
--echo #==============================================================================
--echo # 5.0 Drop the client connections
--echo #==============================================================================
--echo # 5.1 Disconnect con1
--disconnect con1
--echo # 5.2 Disconnect con2
--disconnect con2
--connection default
--echo #==============================================================================
--echo # 6.0 Verify sockets were removed from the instance tables
--echo #==============================================================================
--echo #
--echo # 6.1 Verify that there are no TCP/IP connections in the socket instance table
--echo #
let $wait_condition=
SELECT COUNT(*) = 0 AS 'Expect 0'
FROM performance_schema.socket_instances
WHERE EVENT_NAME LIKE '%client_connection%'
AND OBJECT_INSTANCE_BEGIN <> @default_object_instance_begin
AND $ip_localhost;
--source include/wait_condition.inc
--echo #
--echo # 6.2 Verify that there are no TCP/IP connections in the summary instance table
--echo #
let $wait_condition=
SELECT COUNT(*) = 0 AS 'Expect 0'
FROM performance_schema.socket_summary_by_instance
WHERE EVENT_NAME LIKE '%client_connection%'
AND OBJECT_INSTANCE_BEGIN <> @default_object_instance_begin;
--source include/wait_condition.inc
exit;
--enable_cursor_protocol