summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
Diffstat (limited to 'sql')
-rw-r--r--sql/mysqld.cc50
-rw-r--r--sql/mysqld.h8
-rw-r--r--sql/scheduler.cc2
-rw-r--r--sql/scheduler.h7
-rw-r--r--sql/sql_acl.cc6
-rw-r--r--sql/sql_connect.cc5
-rw-r--r--sql/sql_connect.h6
-rw-r--r--sql/threadpool_common.cc2
8 files changed, 24 insertions, 62 deletions
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
index 371e645a469..f4357ef9622 100644
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -706,7 +706,7 @@ mysql_mutex_t
LOCK_crypt,
LOCK_global_system_variables,
LOCK_user_conn,
- LOCK_connection_count, LOCK_error_messages, LOCK_slave_background;
+ LOCK_error_messages, LOCK_slave_background;
mysql_mutex_t LOCK_stats, LOCK_global_user_client_stats,
LOCK_global_table_stats, LOCK_global_index_stats;
@@ -864,7 +864,7 @@ PSI_mutex_key key_BINLOG_LOCK_index, key_BINLOG_LOCK_xid_list,
key_BINLOG_LOCK_binlog_background_thread,
key_LOCK_binlog_end_pos,
key_delayed_insert_mutex, key_hash_filo_lock, key_LOCK_active_mi,
- key_LOCK_connection_count, key_LOCK_crypt, key_LOCK_delayed_create,
+ key_LOCK_crypt, key_LOCK_delayed_create,
key_LOCK_delayed_insert, key_LOCK_delayed_status, key_LOCK_error_log,
key_LOCK_gdl, key_LOCK_global_system_variables,
key_LOCK_manager,
@@ -929,7 +929,6 @@ static PSI_mutex_info all_server_mutexes[]=
{ &key_delayed_insert_mutex, "Delayed_insert::mutex", 0},
{ &key_hash_filo_lock, "hash_filo::lock", 0},
{ &key_LOCK_active_mi, "LOCK_active_mi", PSI_FLAG_GLOBAL},
- { &key_LOCK_connection_count, "LOCK_connection_count", PSI_FLAG_GLOBAL},
{ &key_LOCK_thread_id, "LOCK_thread_id", PSI_FLAG_GLOBAL},
{ &key_LOCK_crypt, "LOCK_crypt", PSI_FLAG_GLOBAL},
{ &key_LOCK_delayed_create, "LOCK_delayed_create", PSI_FLAG_GLOBAL},
@@ -1474,10 +1473,10 @@ struct st_VioSSLFd *ssl_acceptor_fd;
#endif /* HAVE_OPENSSL */
/**
- Number of currently active user connections. The variable is protected by
- LOCK_connection_count.
+ Number of currently active user connections.
*/
-uint connection_count= 0, extra_connection_count= 0;
+Atomic_counter<uint> connection_count;
+static Atomic_counter<uint> extra_connection_count;
my_bool opt_gtid_strict_mode= FALSE;
@@ -2106,7 +2105,6 @@ static void clean_up_mutexes()
mysql_mutex_destroy(&LOCK_delayed_create);
mysql_mutex_destroy(&LOCK_crypt);
mysql_mutex_destroy(&LOCK_user_conn);
- mysql_mutex_destroy(&LOCK_connection_count);
mysql_mutex_destroy(&LOCK_thread_id);
mysql_mutex_destroy(&LOCK_stats);
mysql_mutex_destroy(&LOCK_global_user_client_stats);
@@ -2598,20 +2596,6 @@ extern "C" sig_handler end_mysqld_signal(int sig __attribute__((unused)))
}
#endif /* EMBEDDED_LIBRARY */
-/*
- Decrease number of connections
-
- SYNOPSIS
- dec_connection_count()
-*/
-
-void dec_connection_count(scheduler_functions *scheduler)
-{
- mysql_mutex_lock(&LOCK_connection_count);
- (*scheduler->connection_count)--;
- mysql_mutex_unlock(&LOCK_connection_count);
-}
-
/*
Unlink thd from global list of available connections
@@ -2637,7 +2621,7 @@ void unlink_thd(THD *thd)
*/
if (!thd->wsrep_applier)
#endif /* WITH_WSREP */
- dec_connection_count(thd->scheduler);
+ --*thd->scheduler->connection_count;
thd->free_connection();
@@ -4426,8 +4410,6 @@ static int init_thread_environment()
&LOCK_error_messages, MY_MUTEX_INIT_FAST);
mysql_mutex_init(key_LOCK_uuid_short_generator,
&LOCK_short_uuid_generator, MY_MUTEX_INIT_FAST);
- mysql_mutex_init(key_LOCK_connection_count,
- &LOCK_connection_count, MY_MUTEX_INIT_FAST);
mysql_mutex_init(key_LOCK_thread_id,
&LOCK_thread_id, MY_MUTEX_INIT_FAST);
mysql_mutex_init(key_LOCK_stats, &LOCK_stats, MY_MUTEX_INIT_FAST);
@@ -6172,29 +6154,17 @@ void create_new_thread(CONNECT *connect)
Don't allow too many connections. We roughly check here that we allow
only (max_connections + 1) connections.
*/
-
- mysql_mutex_lock(&LOCK_connection_count);
-
- if (*connect->scheduler->connection_count >=
+ if ((*connect->scheduler->connection_count)++ >=
*connect->scheduler->max_connections + 1)
{
DBUG_PRINT("error",("Too many connections"));
-
- mysql_mutex_unlock(&LOCK_connection_count);
- statistic_increment(denied_connections, &LOCK_status);
- statistic_increment(connection_errors_max_connection, &LOCK_status);
connect->close_with_error(0, NullS, ER_CON_COUNT_ERROR);
DBUG_VOID_RETURN;
}
- ++*connect->scheduler->connection_count;
-
- if (connection_count + extra_connection_count > max_used_connections)
- max_used_connections= connection_count + extra_connection_count;
-
- mysql_mutex_unlock(&LOCK_connection_count);
-
- connect->thread_count_incremented= 1;
+ uint sum= connection_count + extra_connection_count;
+ if (sum > max_used_connections)
+ max_used_connections= sum;
/*
The initialization of thread_id is done in create_embedded_thd() for
diff --git a/sql/mysqld.h b/sql/mysqld.h
index e106ee5bc9a..ec14455201f 100644
--- a/sql/mysqld.h
+++ b/sql/mysqld.h
@@ -83,7 +83,6 @@ CONNECT *cache_thread(THD *thd);
void flush_thread_cache();
void refresh_status(THD *thd);
bool is_secure_file_path(char *path);
-void dec_connection_count(scheduler_functions *scheduler);
extern void init_net_server_extension(THD *thd);
extern void handle_accepted_socket(MYSQL_SOCKET new_sock, MYSQL_SOCKET sock);
extern void create_new_thread(CONNECT *connect);
@@ -119,7 +118,7 @@ extern bool opt_ignore_builtin_innodb;
extern my_bool opt_character_set_client_handshake;
extern my_bool debug_assert_on_not_freed_memory;
extern bool volatile abort_loop;
-extern uint connection_count;
+extern Atomic_counter<uint> connection_count;
extern my_bool opt_safe_user_create;
extern my_bool opt_safe_show_db, opt_local_infile, opt_myisam_use_mmap;
extern my_bool opt_slave_compressed_protocol, use_temp_pool;
@@ -318,7 +317,7 @@ extern PSI_mutex_key key_BINLOG_LOCK_index, key_BINLOG_LOCK_xid_list,
key_BINLOG_LOCK_binlog_background_thread,
key_LOCK_binlog_end_pos,
key_delayed_insert_mutex, key_hash_filo_lock, key_LOCK_active_mi,
- key_LOCK_connection_count, key_LOCK_crypt, key_LOCK_delayed_create,
+ key_LOCK_crypt, key_LOCK_delayed_create,
key_LOCK_delayed_insert, key_LOCK_delayed_status, key_LOCK_error_log,
key_LOCK_gdl, key_LOCK_global_system_variables,
key_LOCK_logger, key_LOCK_manager,
@@ -615,7 +614,7 @@ extern mysql_mutex_t
LOCK_delayed_status, LOCK_delayed_create, LOCK_crypt, LOCK_timezone,
LOCK_active_mi, LOCK_manager,
LOCK_global_system_variables, LOCK_user_conn,
- LOCK_prepared_stmt_count, LOCK_error_messages, LOCK_connection_count,
+ LOCK_prepared_stmt_count, LOCK_error_messages,
LOCK_slave_background;
extern mysql_rwlock_t LOCK_all_status_vars;
extern mysql_mutex_t LOCK_start_thread;
@@ -827,7 +826,6 @@ inline int set_current_thd(THD *thd)
*/
extern handlerton *maria_hton;
-extern uint extra_connection_count;
extern uint64 global_gtid_counter;
extern my_bool opt_gtid_strict_mode;
extern my_bool opt_userstat_running, debug_assert_if_crashed_table;
diff --git a/sql/scheduler.cc b/sql/scheduler.cc
index 5f24495059f..a4b9dd0c34a 100644
--- a/sql/scheduler.cc
+++ b/sql/scheduler.cc
@@ -112,7 +112,7 @@ void post_kill_notification(THD *thd)
void one_thread_per_connection_scheduler(scheduler_functions *func,
ulong *arg_max_connections,
- uint *arg_connection_count)
+ Atomic_counter<uint> *arg_connection_count)
{
scheduler_init();
func->max_threads= *arg_max_connections + 1;
diff --git a/sql/scheduler.h b/sql/scheduler.h
index e77d6b5e80c..6d0bc91d68a 100644
--- a/sql/scheduler.h
+++ b/sql/scheduler.h
@@ -31,7 +31,8 @@ class THD;
struct scheduler_functions
{
- uint max_threads, *connection_count;
+ uint max_threads;
+ Atomic_counter<uint> *connection_count;
ulong *max_connections;
bool (*init)(void);
void (*add_connection)(CONNECT *connect);
@@ -70,7 +71,7 @@ enum scheduler_types
};
void one_thread_per_connection_scheduler(scheduler_functions *func,
- ulong *arg_max_connections, uint *arg_connection_count);
+ ulong *arg_max_connections, Atomic_counter<uint> *arg_connection_count);
void one_thread_scheduler(scheduler_functions *func);
extern void scheduler_init();
@@ -98,7 +99,7 @@ public:
#ifdef HAVE_POOL_OF_THREADS
void pool_of_threads_scheduler(scheduler_functions* func,
ulong *arg_max_connections,
- uint *arg_connection_count);
+ Atomic_counter<uint> *arg_connection_count);
#else
#define pool_of_threads_scheduler(A,B,C) \
one_thread_per_connection_scheduler(A, B, C)
diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc
index 27f2a985931..2841e11d46c 100644
--- a/sql/sql_acl.cc
+++ b/sql/sql_acl.cc
@@ -13947,11 +13947,7 @@ bool acl_authenticate(THD *thd, uint com_change_user_pkt_len)
if (command == COM_CONNECT &&
!(thd->main_security_ctx.master_access & SUPER_ACL))
{
- mysql_mutex_lock(&LOCK_connection_count);
- bool count_ok= (*thd->scheduler->connection_count <=
- *thd->scheduler->max_connections);
- mysql_mutex_unlock(&LOCK_connection_count);
- if (!count_ok)
+ if (*thd->scheduler->connection_count > *thd->scheduler->max_connections)
{ // too many connections
my_error(ER_CON_COUNT_ERROR, MYF(0));
DBUG_RETURN(1);
diff --git a/sql/sql_connect.cc b/sql/sql_connect.cc
index c281769e616..7e2787b46a3 100644
--- a/sql/sql_connect.cc
+++ b/sql/sql_connect.cc
@@ -1350,7 +1350,7 @@ void do_handle_one_connection(CONNECT *connect, bool put_in_cache)
THD *thd;
if (!(thd= connect->create_thd(NULL)))
{
- connect->close_with_error(0, 0, ER_OUT_OF_RESOURCES);
+ connect->close_and_delete();
return;
}
@@ -1483,8 +1483,7 @@ void CONNECT::close_and_delete()
mysql_socket_close(sock);
vio_type= VIO_CLOSED;
- if (thread_count_incremented)
- dec_connection_count(scheduler);
+ --*scheduler->connection_count;
statistic_increment(connection_errors_internal, &LOCK_status);
statistic_increment(aborted_connects,&LOCK_status);
diff --git a/sql/sql_connect.h b/sql/sql_connect.h
index 544270189db..f0e717286f0 100644
--- a/sql/sql_connect.h
+++ b/sql/sql_connect.h
@@ -35,21 +35,19 @@ public:
#ifdef _WIN32
HANDLE pipe;
CONNECT(HANDLE pipe_arg): pipe(pipe_arg), vio_type(VIO_TYPE_NAMEDPIPE),
- scheduler(thread_scheduler), thread_id(0), thread_count_incremented(0),
- prior_thr_create_utime(0) {}
+ scheduler(thread_scheduler), thread_id(0), prior_thr_create_utime(0) {}
#endif
enum enum_vio_type vio_type;
scheduler_functions *scheduler;
my_thread_id thread_id;
/* Own variables */
- bool thread_count_incremented;
ulonglong prior_thr_create_utime;
CONNECT(MYSQL_SOCKET sock_arg, enum enum_vio_type vio_type_arg,
scheduler_functions *scheduler_arg): sock(sock_arg),
vio_type(vio_type_arg), scheduler(scheduler_arg), thread_id(0),
- thread_count_incremented(0), prior_thr_create_utime(0) {}
+ prior_thr_create_utime(0) {}
~CONNECT() { DBUG_ASSERT(vio_type == VIO_CLOSED); }
void close_and_delete();
void close_with_error(uint sql_errno,
diff --git a/sql/threadpool_common.cc b/sql/threadpool_common.cc
index 66f7a94d528..06b74cfe1a6 100644
--- a/sql/threadpool_common.cc
+++ b/sql/threadpool_common.cc
@@ -508,7 +508,7 @@ static scheduler_functions tp_scheduler_functions=
void pool_of_threads_scheduler(struct scheduler_functions *func,
ulong *arg_max_connections,
- uint *arg_connection_count)
+ Atomic_counter<uint> *arg_connection_count)
{
*func = tp_scheduler_functions;
func->max_threads= threadpool_max_threads;